Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_plot_creation_02_array_default_dims(self):
# add a default value for the y dimension
psy.register_plotter('test_plotter',
import_plotter=True, module='test_plotter',
plotter_name='TestPlotter',
default_dims={'y': 0})
ds = psy.open_dataset(bt.get_file('test-t2m-u-v.nc'))
sp = psy.plot.test_plotter(ds, name='t2m')
self.assertEqual(len(sp), 1)
self.assertEqual(sp[0].name, 't2m')
self.assertEqual(sp[0].shape, ds.t2m.isel(lat=0).shape)
self.assertEqual(sp[0].values.tolist(),
ds.t2m.isel(lat=0).values.tolist())
psy.close()
psy.unregister_plotter('test_plotter')
# third test file
shutil.copyfile(bt.get_file('test-t2m-u-v.nc'),
osp.join(tempdir3, 'test-t2m-u-v.nc'))
psy.plot.test_plotter(osp.join(tempdir3, 'test-t2m-u-v.nc'),
name='t2m', t=[3, 4])
# fourth test file with different name
psy.plot.test_plotter(bt.get_file('circumpolar_test.nc'), name='t2m',
t=[0, 1])
mp = psy.gcp(True)
mp.save_project(osp.join(outdir, 'test.pkl'), pack=True)
files = {'test-t2m-u-v.nc', 'test-t2m-u-v-1.nc',
'test-t2m-u-v-2.nc', 'test.pkl', 'circumpolar_test.nc'}
self.assertEqual(set(os.listdir(outdir)), files)
psy.close(mp)
# move the directory to check whether it is still working
outdir2 = tempfile.mkdtemp(prefix='psyplot_test_')
self._created_files.add(outdir2)
for f in files:
shutil.move(osp.join(outdir, f), osp.join(outdir2, f))
mp = psy.Project.load_project(osp.join(outdir2, 'test.pkl'), main=True,
)
self.assertEqual(len(mp), 8, msg=mp)
paths = {osp.join(outdir2, 'test-t2m-u-v.nc'),
osp.join(outdir2, 'test-t2m-u-v-1.nc'),
osp.join(outdir2, 'test-t2m-u-v-2.nc')}
found = set()
self.assertEqual(sp[0][1].name, 'u')
self.assertEqual(sp[1][0].name, 't2m')
self.assertEqual(sp[1][1].name, 'u')
self.assertEqual(sp[0][0].shape, ds.t2m.isel(lon=1).shape)
self.assertEqual(sp[0][1].shape, ds.u.isel(lon=1).shape)
self.assertEqual(sp[1][0].shape, ds.t2m.isel(lon=2).shape)
self.assertEqual(sp[1][1].shape, ds.u.isel(lon=2).shape)
self.assertEqual(sp[0][0].values.tolist(),
ds.t2m.isel(lon=1).values.tolist())
self.assertEqual(sp[0][1].values.tolist(),
ds.u.isel(lon=1).values.tolist())
self.assertEqual(sp[1][0].values.tolist(),
ds.t2m.isel(lon=2).values.tolist())
self.assertEqual(sp[1][1].values.tolist(),
ds.u.isel(lon=2).values.tolist())
psy.close()
ds.close()
psy.unregister_plotter('test_plotter')
def test_main_01_from_project(self):
"""Test the :func:`psyplot.__main__.main` function"""
if not six.PY2:
with self.assertRaisesRegex(ValueError, 'filename'):
main.main(['-o', 'test.pdf'])
sp, fname1 = self._create_and_save_test_project()
fname2 = tempfile.NamedTemporaryFile(
suffix='.pdf', prefix='test_psyplot_').name
self._created_files.add(fname2)
sp.save_project(fname1, use_rel_paths=False)
psy.close('all')
if six.PY2:
main.main(['-p', fname1, '-o', fname2])
else:
with self.assertWarnsRegex(UserWarning, 'ignored'):
main.main(['-p', fname1, '-o', fname2, '-n', 't2m'])
self.assertTrue(osp.exists(fname2), msg='Missing ' + fname2)
self.assertEqual(len(psy.gcp(True)), 4)
self.assertEqual(sp[0].psy.ax.rowNum, 0)
self.assertEqual(sp[0].psy.ax.colNum, 0)
self.assertEqual(sp[0].psy.ax.numCols, 2)
self.assertEqual(sp[0].psy.ax.numRows, 2)
self.assertEqual(sp[1].psy.ax.get_figure().number, 2)
self.assertEqual(sp[1].psy.ax.rowNum, 0)
self.assertEqual(sp[1].psy.ax.colNum, 0)
self.assertEqual(sp[1].psy.ax.numCols, 2)
self.assertEqual(sp[1].psy.ax.numRows, 2)
arr_names = sp.arr_names
self.assertEqual(tp.results[arr_names[0] + '.fmt1'], 'test')
self.assertEqual(tp.results[arr_names[1] + '.fmt1'], 'test')
fname = 'test.pkl'
self._created_files.add(fname)
sp.save_project(fname)
psy.close()
tp.results.clear()
fig, axes = plt.subplots(1, 2)
ds = psy.open_dataset(bt.get_file('circumpolar_test.nc'))
sp = psy.Project.load_project(fname, datasets=[ds], new_fig=False)
self.assertEqual(len(sp), 2)
self.assertEqual(sp[0].psy.ax.get_figure().number, 1)
self.assertEqual(sp[0].psy.ax.rowNum, 0)
self.assertEqual(sp[0].psy.ax.colNum, 0)
self.assertEqual(sp[0].psy.ax.numCols, 2)
self.assertEqual(sp[0].psy.ax.numRows, 2)
self.assertEqual(sp[1].psy.ax.get_figure().number, 2)
self.assertEqual(sp[1].psy.ax.rowNum, 0)
self.assertEqual(sp[1].psy.ax.colNum, 0)
self.assertEqual(sp[1].psy.ax.numCols, 2)
self.assertEqual(sp[1].psy.ax.numRows, 2)
self.assertIs(sp[0].psy.base, ds)
def tearDown(self):
for identifier in list(psy.registered_plotters):
psy.unregister_plotter(identifier)
psy.close('all')
plt.close('all')
tp.results.clear()
if remove_temp_files:
for f in self._created_files:
if osp.exists(f) and osp.isdir(f):
shutil.rmtree(f)
elif osp.exists(f):
os.remove(f)
self._created_files.clear()
self.assertEqual(check_mains, [False],
msg="projects: %s" % (projects, ))
self.assertIs(projects[0], p)
p.pop(1)
# close a part of the project
check_mains = []
projects = []
sp[:1].close(True, True)
self.assertEqual(check_mains, [True])
self.assertEqual(len(projects[0]), 1, msg=str(projects[0]))
# close the remaining part of the project
check_mains = []
projects = []
psy.close()
self.assertEqual(len(check_mains), 2,
msg="%s, %s" % (check_mains, projects))
self.assertIn(False, check_mains)
self.assertIn(True, check_mains)
self.assertEqual(len(projects[0]), 0, msg=str(projects[0]))
self.assertEqual(len(projects[1]), 0, msg=str(projects[1]))
psy.Project.oncpchange.disconnect(check)
self.assertEqual(list(axes[1].get_xlim()), [5, 10])
# save the project
fname = 'test.pkl'
self._created_files.add(fname)
sp.save_project(fname)
psy.close('all')
# load the project
sp = psy.Project.load_project(fname)
self.assertEqual(len(sp.axes), 3, msg=sp.axes)
sp[0].psy.ax.set_xlim(10, 15)
self.assertEqual(list(sp[1].psy.ax.get_xlim()), [10, 15])
# now we test, if it still works, if we remove the source axes
names2use = sp.arr_names[1:]
psy.close('all')
sp = psy.Project.load_project(fname, only=names2use)
self.assertEqual(len(sp.axes), 2, msg=sp.axes)
sp[0].psy.ax.set_xlim(10, 15)
self.assertEqual(list(sp[1].psy.ax.get_xlim()), [10, 15])
self.assertEqual(sp[1].shape, ds.t2m.isel(lat=0, lon=2).shape)
self.assertEqual(list(sp[2].shape),
[2] + list(ds.u.isel(lat=0, lon=1).shape))
self.assertEqual(list(sp[2].shape),
[2] + list(ds.u.isel(lat=0, lon=2).shape))
self.assertEqual(sp[0].values.tolist(),
ds.t2m.isel(lat=0, lon=1).values.tolist())
self.assertEqual(sp[1].values.tolist(),
ds.t2m.isel(lat=0, lon=2).values.tolist())
self.assertEqual(sp[2].values.tolist(),
ds[['u', 'v']].isel(
lat=0, lon=1).to_array().values.tolist())
self.assertEqual(sp[3].values.tolist(),
ds[['u', 'v']].isel(
lat=0, lon=2).to_array().values.tolist())
psy.close()
psy.unregister_plotter('test_plotter')