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_keys(self):
"""Test the :meth:`psyplot.project.Project.keys` method"""
import test_plotter as tp
import psyplot.plotter as psyp
psy.register_plotter('test_plotter', import_plotter=True,
module='test_plotter', plotter_name='TestPlotter')
class TestPlotter2(tp.TestPlotter):
fmt2 = None
psy.register_plotter('test_plotter2', module='something',
plotter_name='anyway', plotter_cls=TestPlotter2)
variables, coords = self._from_dataset_test_variables
ds = xr.Dataset(variables, coords)
sp1 = psy.plot.test_plotter(ds, name='v0')
# add a second project without a fmt2 formatoption
sp2 = psy.plot.test_plotter2(ds, name='v1')
mp = sp1 + sp2
self.assertEqual(sp1.keys(['fmt1', 'fmt2', 'fmt3'], func=str),
'+------+------+------+\n'
'| fmt1 | fmt2 | fmt3 |\n'
def test_share_03_method_by(self):
"""Test the :meth:`psyplot.project.Project.share` method by axes/figure
"""
import matplotlib.pyplot as plt
psy.register_plotter('test_plotter', module='test_plotter',
plotter_name='TestPlotter')
fig1, ax1 = plt.subplots()
fig2, axes = plt.subplots(1, 2)
ax2, ax3 = axes
sp = psy.plot.test_plotter(bt.get_file('test-t2m-u-v.nc'), name='t2m',
time=range(4), ax=[ax1, ax2, ax1, ax3])
self.assertEqual(len(sp), 4, msg=sp)
# share by axes
sp.share(by='axes', keys='something')
self.assertEqual(sp.plotters[0].fmt3.shared,
{sp.plotters[2].fmt3})
self.assertFalse(sp.plotters[1].fmt3.shared)
self.assertFalse(sp.plotters[3].fmt3.shared)
sp[0].psy.update(fmt3='test3')
def test_close_global(self):
"""Test the :func:`psyplot.project.close` function"""
psy.register_plotter('test_plotter', module='test_plotter',
plotter_name='TestPlotter')
with psy.open_dataset(bt.get_file('test-t2m-u-v.nc')) as ds:
time = ds.time.values
lev = ds.lev.values
mp0 = psy.plot.test_plotter(bt.get_file('test-t2m-u-v.nc'), name='t2m',
lev=[0, 1]).main
mp1 = psy.project()
psy.plot.test_plotter(bt.get_file('test-t2m-u-v.nc'), name='t2m',
time=[1, 2])
mp2 = psy.project()
sp1 = psy.plot.test_plotter(bt.get_file('test-t2m-u-v.nc'), name='t2m',
time=[3, 4])
sp2 = psy.plot.test_plotter(bt.get_file('test-t2m-u-v.nc'), name='t2m',
lev=[2, 3])
# some checks in the beginning
self.assertEqual(len(mp0), 2)
def test_update(self):
"""Test the update of an :class:`psyplot.data.ArrayList`"""
variables, coords = self._from_dataset_test_variables
ds = xr.Dataset(variables, coords)
psy.register_plotter('test_plotter', module='something',
plotter_name='unimportant',
plotter_cls=tp.TestPlotter)
# add 2 arrays
psy.plot.test_plotter(ds, name=['v0', 'v1'], t=0)
# add a list
psy.plot.test_plotter(ds, name=['v0', 'v1'], t=0, prefer_list=True)
mp = psy.gcp(True)
self.assertEqual(len(mp), 3, msg=mp)
self.assertEqual(len(mp.plotters), 3, msg=mp)
# update the list
mp.update(t=1, fmt2='updated')
for i, plotter in enumerate(mp.plotters):
def test_docs(self):
"""Test the :meth:`psyplot.project.Project.docs` method"""
import test_plotter as tp
import psyplot.plotter as psyp
psy.register_plotter('test_plotter', import_plotter=True,
module='test_plotter', plotter_name='TestPlotter')
class TestPlotter2(tp.TestPlotter):
fmt2 = None
psy.register_plotter('test_plotter2', module='something',
plotter_name='anyway', plotter_cls=TestPlotter2)
variables, coords = self._from_dataset_test_variables
ds = xr.Dataset(variables, coords)
sp1 = psy.plot.test_plotter(ds, name='v0')
# add a second project without a fmt2 formatoption
sp2 = psy.plot.test_plotter2(ds, name='v1')
mp = sp1 + sp2
self.assertEqual(sp1.docs(func=str), '\n'.join([
'fmt1', '====', tp.SimpleFmt.__doc__, '',
'fmt2', '====', tp.SimpleFmt2.__doc__, '',
'fmt3', '====', tp.SimpleFmt3.__doc__, '',
'post', '====', psyp.PostProcessing.__doc__, '',
'post_timing', '===========', psyp.PostTiming.__doc__, '']))
# test summed project
self.assertEqual(mp.docs(func=str), '\n'.join([
'fmt1', '====', tp.SimpleFmt.__doc__, '',
def test_summaries(self):
"""Test the :meth:`psyplot.project.Project.summaries` method"""
import test_plotter as tp
import psyplot.plotter as psyp
psy.register_plotter('test_plotter', import_plotter=True,
module='test_plotter', plotter_name='TestPlotter')
class TestPlotter2(tp.TestPlotter):
fmt2 = None
psy.register_plotter('test_plotter2', module='something',
plotter_name='anyway', plotter_cls=TestPlotter2)
variables, coords = self._from_dataset_test_variables
ds = xr.Dataset(variables, coords)
sp1 = psy.plot.test_plotter(ds, name='v0')
# add a second project without a fmt2 formatoption
sp2 = psy.plot.test_plotter2(ds, name='v1')
mp = sp1 + sp2
self.assertEqual(sp1.summaries(func=str), '\n'.join([
'fmt1', tp.indent(tp.SimpleFmt.__doc__.splitlines()[0], ' '),
'fmt2', tp.indent(tp.SimpleFmt2.__doc__.splitlines()[0], ' '),
def test_oncpchange_signal(self):
"""Test whether the correct signal is fired"""
psy.register_plotter('test_plotter', module='test_plotter',
plotter_name='TestPlotter')
check_mains = []
projects = []
def check(p):
check_mains.append(p.is_main)
projects.append(p)
psy.Project.oncpchange.connect(check)
ds = psy.open_dataset(bt.get_file('test-t2m-u-v.nc')).load()
sp = psy.plot.test_plotter(ds, name='t2m', lev=[0, 1])
# the signal should have been fired 2 times, one times from the
# subproject, one times from the project
self.assertEqual(len(check_mains), 2)
self.assertIn(False, check_mains)
self.assertIn(True, check_mains)
def test_figs(self):
"""Test the :attr:`psyplot.project.Project.figs` attribute"""
psy.register_plotter('test_plotter', import_plotter=True,
module='test_plotter', plotter_name='TestPlotter')
ds = psy.open_dataset(bt.get_file('test-t2m-u-v.nc'))
sp = psy.plot.test_plotter(ds, name='t2m', time=[1, 2])
self.assertEqual(sp[0].psy.ax.figure.number, 1)
self.assertEqual(sp[1].psy.ax.figure.number, 2)
figs = sp.figs
self.assertIn(sp[0].psy.ax.figure, figs)
self.assertIs(figs[sp[0].psy.ax.figure][0], sp[0])
self.assertIn(sp[1].psy.ax.figure, figs)
self.assertIs(figs[sp[1].psy.ax.figure][0], sp[1])
def test_plotter_registration(self):
"""Test the registration of a plotter"""
psy.register_plotter('test_plotter',
import_plotter=True, module='test_plotter',
plotter_name='TestPlotter')
self.assertTrue(hasattr(psy.plot, 'test_plotter'))
self.assertIs(psy.plot.test_plotter.plotter_cls, tp.TestPlotter)
psy.plot.test_plotter.print_func = str
self.assertEqual(psy.plot.test_plotter.fmt1(), tp.SimpleFmt.__doc__)
psy.plot.test_plotter.print_func = None
# test the warning
if not six.PY2:
with self.assertWarnsRegex(UserWarning, "not_existent_module"):
psy.register_plotter('something', "not_existent_module",
'not_important', import_plotter=True)
psy.unregister_plotter('test_plotter')
self.assertFalse(hasattr(psy.Project, 'test_plotter'))
self.assertFalse(hasattr(psy.plot, 'test_plotter'))
def test_plot_creation_06_list(self):
"""Test the plot creation with a plotter that takes a list of arrays"""
psy.register_plotter('test_plotter',
import_plotter=True, module='test_plotter',
plotter_name='TestPlotter', prefer_list=True)
ds = psy.open_dataset(bt.get_file('test-t2m-u-v.nc'))
# test the creation of one list
# psyplot.project.Project([arr4: psyplot.data.InteractiveList([
# arr0: 4-dim DataArray of t2m, with
# (time, lev, lat, lon)=(5, 4, 96, 192), ,
# arr1: 4-dim DataArray of u, with
# (time, lev, lat, lon)=(5, 4, 96, 192), ])])
sp = psy.plot.test_plotter(ds, name=['t2m', 'u'])
self.assertEqual(len(sp), 1)
self.assertEqual(len(sp[0]), 2)
self.assertEqual(sp[0][0].name, 't2m')
self.assertEqual(sp[0][1].name, 'u')
self.assertEqual(sp[0][0].shape, ds.t2m.shape)
self.assertEqual(sp[0][1].shape, ds.u.shape)