def test_Planform_idx_z_t_mutual_exclusive(self): golfcube = cube.DataCube(golf_path) with pytest.raises(TypeError, match=r'Cannot .* `z` and `idx`.'): _ = plan.Planform(golfcube, z=5e6, idx=30) with pytest.raises(TypeError, match=r'Cannot .* `t` and `idx`.'): _ = plan.Planform(golfcube, t=3e6, idx=30) with pytest.raises(TypeError, match=r'Cannot .* `z` and `t`.'): _ = plan.Planform(golfcube, t=3e6, z=5e6)
def test_Planform_z_t_thesame(self): golfcube = cube.DataCube(golf_path) plnfrm = plan.Planform(golfcube, t=3e6) plnfrm2 = plan.Planform(golfcube, z=3e6) assert plnfrm.name == 'data' assert plnfrm.idx == 6 assert plnfrm.idx == plnfrm2.idx assert plnfrm.cube == golfcube assert len(plnfrm.variables) > 0
def test_Planform_slicing(self): # make the planforms golfcube = cube.DataCube(golf_path) golfcubestrat = cube.DataCube(golf_path) golfcubestrat.stratigraphy_from('eta', dz=0.1) golfstrat = cube.StratigraphyCube.from_DataCube(golfcube, dz=0.1) plnfrm1 = plan.Planform(golfcube, idx=-1) plnfrm2 = plan.Planform(golfcubestrat, z=-2) plnfrm3 = plan.Planform(golfstrat, z=-6) # note should be deep enough for no nans assert np.all(plnfrm1['eta'] == golfcube['eta'][-1, :, :]) assert np.all( plnfrm2['time'] == golfcubestrat['time'][plnfrm2.idx, :, :]) assert np.all(plnfrm3['time'] == golfstrat['time'][plnfrm3.idx, :, :])
def test_Planform_private_show(self): """Doesn't actually check the plots, just checks that the function runs. """ # make the planforms golfcube = cube.DataCube(golf_path) plnfrm = plan.Planform(golfcube, idx=-1) _field = plnfrm['eta'] _varinfo = golfcube.varset['eta'] # with axis fig, ax = plt.subplots() plnfrm._show(_field, _varinfo, ax=ax) plt.close() # without axis fig, ax = plt.subplots() plnfrm._show(_field, _varinfo) plt.close() # with colorbar_label fig, ax = plt.subplots(1, 2) plnfrm._show(_field, _varinfo, ax=ax[0], colorbar_label='test') plnfrm._show(_field, _varinfo, ax=ax[1], colorbar_label=True) plt.close() # with ticks fig, ax = plt.subplots() plnfrm._show(_field, _varinfo, ax=ax, ticks=True) plt.close() # with title fig, ax = plt.subplots() plnfrm._show(_field, _varinfo, ax=ax, title='some title') plt.close()
def test_Planform_idx(self): golfcube = cube.DataCube(golf_path) plnfrm = plan.Planform(golfcube, idx=40) assert plnfrm.name == 'data' assert plnfrm.idx == 40 assert plnfrm.cube == golfcube assert len(plnfrm.variables) > 0
def test_planforms_slice_op(self): golf = cube.DataCube(golf_path) golf.stratigraphy_from('eta', dz=0.1) golf.register_planform('testplanform', plan.Planform(idx=10)) assert 'testplanform' in golf.planforms.keys() slc = golf.planforms['testplanform'] assert issubclass(type(slc), plan.BasePlanform)
def test_register_plan_legacy_method(self): """This tests the shorthand named version.""" golf = cube.DataCube(golf_path) golf.register_plan('testplanform', plan.Planform(idx=10)) assert golf.planforms is golf.planform_set assert len(golf.planforms.keys()) == 1 assert 'testplanform' in golf.planforms.keys()
def test_register_planform(self): golf = cube.DataCube(golf_path) golf.stratigraphy_from('eta', dz=0.1) golf.register_planform('testplanform', plan.Planform(idx=10)) assert golf.planforms is golf.planform_set assert len(golf.planforms.keys()) == 1 assert 'testplanform' in golf.planforms.keys() with pytest.raises(TypeError, match=r'`PlanformInstance` .*'): golf.register_planform('fail1', 'astring') with pytest.raises(TypeError, match=r'`PlanformInstance` .*'): golf.register_planform('fail2', 22) with pytest.raises(TypeError, match=r'`name` .*'): golf.register_planform(22, plan.Planform(idx=10)) returnedplanform = golf.register_planform('returnedplanform', plan.Planform(idx=10), return_planform=True) assert returnedplanform.name == 'returnedplanform'
def test_Planform_without_cube(self): plfrm = plan.Planform(idx=-1) assert plfrm.name is None assert plfrm._input_z is None assert plfrm._input_t is None assert plfrm._input_idx is -1 assert plfrm.shape is None assert plfrm.cube is None assert plfrm._dim0_idx is None assert plfrm.variables is None with pytest.raises(AttributeError, match=r'No cube connected.*.'): plfrm['velocity']
def test_Planform_public_show(self): golfcube = cube.DataCube(golf_path) plnfrm = plan.Planform(golfcube, idx=-1) plnfrm._show = mock.MagicMock() # test with ax fig, ax = plt.subplots() plnfrm.show('time', ax=ax) plt.close() assert plnfrm._show.call_count == 1 # check that all bogus args are passed to _show plnfrm.show('time', ax=100, title=101, ticks=102, colorbar_label=103) assert plnfrm._show.call_count == 2 # hacky method to pull out the keyword calls only kw_calls = plnfrm._show.mock_calls[1][2:][0] assert kw_calls['ax'] == 100 assert kw_calls['title'] == 101 assert kw_calls['ticks'] == 102 assert kw_calls['colorbar_label'] == 103
def test_show_planform_mocked_Planform_show(self): golf = cube.DataCube(golf_path) golf.register_planform('displayplan', plan.Planform(idx=-1)) golf.planforms['displayplan'].show = mock.MagicMock() mocked = golf.planforms['displayplan'].show # no arguments is an error with pytest.raises(TypeError, match=r'.* missing 2 .*'): golf.show_planform() # one argument is an error with pytest.raises(TypeError, match=r'.* missing 1 .*'): golf.show_planform('displayplan') # three arguments is an error with pytest.raises(TypeError, match=r'.* takes 3 .*'): golf.show_planform('one', 'two', 'three') # two arguments passes to BaseSection.show() golf.show_planform('displayplan', 'eta') assert mocked.call_count == 1 # kwargs should be passed along to BaseSection.show golf.show_planform('displayplan', 'eta', ax=100) assert mocked.call_count == 2 mocked.assert_called_with('eta', ax=100) # first arg must be a string with pytest.raises(TypeError, match=r'`name` was not .*'): golf.show_planform(1, 'two')
def test_Planform_bad_cube(self): badcube = ['some', 'list'] with pytest.raises(TypeError, match=r'Expected type is *.'): _ = plan.Planform(badcube, idx=12)