def test_cancel(self): m = ConsoleMonitor() with fetch_std_streams(): m.start('task A', total_work=10) self.assertFalse(m.is_cancelled()) m.cancel() self.assertTrue(m.is_cancelled())
def test_8days(self): """ Test nominal execution with a 8 Days frequency input dataset """ ds = xr.Dataset({ 'first': (['lat', 'lon', 'time'], np.ones([45, 90, 46])), 'second': (['lat', 'lon', 'time'], np.ones([45, 90, 46])), 'lat': np.linspace(-88, 88, 45), 'lon': np.linspace(-178, 178, 90), 'time': pd.date_range('2000-01-01', '2000-12-31', freq='8D') }) ds = adjust_temporal_attrs(ds) ex = xr.Dataset({ 'first': (['lat', 'lon', 'time'], np.ones([45, 90, 12])), 'second': (['lat', 'lon', 'time'], np.ones([45, 90, 12])), 'lat': np.linspace(-88, 88, 45), 'lon': np.linspace(-178, 178, 90), 'time': pd.date_range('2000-01-01', freq='MS', periods=12) }) ex.first.attrs['cell_methods'] = 'time: mean within years' ex.second.attrs['cell_methods'] = 'time: mean within years' m = ConsoleMonitor() actual = temporal_aggregation(ds, monitor=m) self.assertTrue(actual.broadcast_equals(ex))
def test_nominal(self): """ Test nominal execution """ ds = xr.Dataset({ 'first': (['lat', 'lon', 'time'], np.ones([45, 90, 366])), 'second': (['lat', 'lon', 'time'], np.ones([45, 90, 366])), 'lat': np.linspace(-88, 88, 45), 'lon': np.linspace(-178, 178, 90), 'time': pd.date_range('2000-01-01', '2000-12-31')}) ds = adjust_temporal_attrs(ds) ex = xr.Dataset({ 'first': (['lat', 'lon'], np.ones([45, 90])), 'second': (['lat', 'lon'], np.ones([45, 90])), 'lat': np.linspace(-88, 88, 45), 'lon': np.linspace(-178, 178, 90), 'time': pd.date_range('2000-01-01', '2000-12-31')}) m = ConsoleMonitor() actual = reduce(ds, monitor=m) self.assertTrue(actual.broadcast_equals(ex)) actual = reduce(ds, dim=['lat', 'time', 'there_is_no_spoon'], monitor=m) ex = xr.Dataset({ 'first': (['lon'], np.ones([90])), 'second': (['lon'], np.ones([90])), 'lat': np.linspace(-88, 88, 45), 'lon': np.linspace(-178, 178, 90), 'time': pd.date_range('2000-01-01', '2000-12-31')}) self.assertTrue(actual.broadcast_equals(ex))
def test_nominal(self): """ Test nominal execution """ ds = xr.Dataset({ 'first': (['lat', 'lon', 'time'], np.ones([45, 90, 24])), 'second': (['lat', 'lon', 'time'], np.ones([45, 90, 24])), 'lat': np.linspace(-88, 88, 45), 'lon': np.linspace(-178, 178, 90), 'time': pd.date_range('2000-01-01', freq='MS', periods=24)}) ds = adjust_temporal_attrs(ds) # Test monitor m = ConsoleMonitor() actual = long_term_average(ds, monitor=m) self.assertEqual(m._percentage, 100) # Test CF attributes self.assertEqual(actual['first'].attrs['cell_methods'], 'time: mean over years') self.assertEqual(actual.dims, {'time': 12, 'nv': 2, 'lat': 45, 'lon': 90}) self.assertEqual(actual.time.attrs['climatology'], 'climatology_bounds') # Test variable selection actual = long_term_average(ds, var='first') with self.assertRaises(KeyError): actual['second']
def test_monitor(self): """ Test monitor integration """ ref = xr.Dataset({ 'first': (['lat', 'lon', 'time'], np.ones([45, 90, 12])), 'second': (['lat', 'lon', 'time'], np.ones([45, 90, 12])), 'lat': np.linspace(-88, 88, 45), 'lon': np.linspace(-178, 178, 90) }) ds = xr.Dataset({ 'first': (['lat', 'lon', 'time'], np.ones([45, 90, 24])), 'lat': np.linspace(-88, 88, 45), 'lon': np.linspace(-178, 178, 90), 'time': [datetime(2000, x, 1) for x in range(1, 13)] + [datetime(2001, x, 1) for x in range(1, 13)] }) with create_tmp_file() as tmp_file: ref.to_netcdf(tmp_file, 'w') m = ConsoleMonitor() anomaly.anomaly_external(ds, tmp_file, monitor=m) self.assertEqual(m._percentage, 100)
def test_seasonal(self): """ Test aggregation to a seasonal dataset """ # daily -> seasonal ds = xr.Dataset({ 'first': (['lat', 'lon', 'time'], np.ones([45, 90, 366])), 'second': (['lat', 'lon', 'time'], np.ones([45, 90, 366])), 'lat': np.linspace(-88, 88, 45), 'lon': np.linspace(-178, 178, 90), 'time': pd.date_range('2000-01-01', '2000-12-31')}) ds = adjust_temporal_attrs(ds) ex = xr.Dataset({ 'first': (['lat', 'lon', 'time'], np.ones([45, 90, 5])), 'second': (['lat', 'lon', 'time'], np.ones([45, 90, 5])), 'lat': np.linspace(-88, 88, 45), 'lon': np.linspace(-178, 178, 90), 'time': pd.date_range('1999-12-01', freq='QS-DEC', periods=5)}) ex.first.attrs['cell_methods'] = 'time: mean within years' ex.second.attrs['cell_methods'] = 'time: mean within years' m = ConsoleMonitor() actual = temporal_aggregation(ds, output_resolution='season', monitor=m) self.assertTrue(actual.broadcast_equals(ex)) # monthly -> seasonal ds = xr.Dataset({ 'first': (['lat', 'lon', 'time'], np.ones([45, 90, 12])), 'second': (['lat', 'lon', 'time'], np.ones([45, 90, 12])), 'lat': np.linspace(-88, 88, 45), 'lon': np.linspace(-178, 178, 90), 'time': pd.date_range('2000-01-01', freq='MS', periods=12)}) ds = adjust_temporal_attrs(ds) actual = temporal_aggregation(ds, output_resolution='season', monitor=m) self.assertTrue(actual.broadcast_equals(ex))
def test_custom(self): """ Test aggergating to custom temporal resolutions """ # daily -> 8 days ds = xr.Dataset({ 'first': (['lat', 'lon', 'time'], np.ones([45, 90, 366])), 'second': (['lat', 'lon', 'time'], np.ones([45, 90, 366])), 'lat': np.linspace(-88, 88, 45), 'lon': np.linspace(-178, 178, 90), 'time': pd.date_range('2000-01-01', '2000-12-31')}) ds = adjust_temporal_attrs(ds) ex = xr.Dataset({ 'first': (['lat', 'lon', 'time'], np.ones([45, 90, 46])), 'second': (['lat', 'lon', 'time'], np.ones([45, 90, 46])), 'lat': np.linspace(-88, 88, 45), 'lon': np.linspace(-178, 178, 90), 'time': pd.date_range('2000-01-01', '2000-12-31', freq='8D')}) ex.first.attrs['cell_methods'] = 'time: mean within years' ex.second.attrs['cell_methods'] = 'time: mean within years' m = ConsoleMonitor() actual = temporal_aggregation(ds, custom_resolution='8D', monitor=m) self.assertTrue(actual.broadcast_equals(ex)) # daily -> weekly ex = xr.Dataset({ 'first': (['lat', 'lon', 'time'], np.ones([45, 90, 53])), 'second': (['lat', 'lon', 'time'], np.ones([45, 90, 53])), 'lat': np.linspace(-88, 88, 45), 'lon': np.linspace(-178, 178, 90), 'time': pd.date_range('2000-01-01', '2000-12-31', freq='W')}) ex.first.attrs['cell_methods'] = 'time: mean within years' ex.second.attrs['cell_methods'] = 'time: mean within years' actual = temporal_aggregation(ds, custom_resolution='W', monitor=m) self.assertTrue(actual.broadcast_equals(ex)) # monthly -> 4 month seasons ds = xr.Dataset({ 'first': (['lat', 'lon', 'time'], np.ones([45, 90, 12])), 'second': (['lat', 'lon', 'time'], np.ones([45, 90, 12])), 'lat': np.linspace(-88, 88, 45), 'lon': np.linspace(-178, 178, 90), 'time': pd.date_range('2000-01-01', freq='MS', periods=12)}) ds = adjust_temporal_attrs(ds) ex = xr.Dataset({ 'first': (['lat', 'lon', 'time'], np.ones([45, 90, 4])), 'second': (['lat', 'lon', 'time'], np.ones([45, 90, 4])), 'lat': np.linspace(-88, 88, 45), 'lon': np.linspace(-178, 178, 90), 'time': pd.date_range('2000-01-01', freq='4M', periods=4)}) ex.first.attrs['cell_methods'] = 'time: mean within years' ex.second.attrs['cell_methods'] = 'time: mean within years' actual = temporal_aggregation(ds, custom_resolution='4M', monitor=m) print(actual) self.assertTrue(actual.broadcast_equals(ex))
def test_child_monitor(self): monitor = ConsoleMonitor() child_monitor = monitor.child(work=7.5) self.assertIsInstance(child_monitor, ChildMonitor)
def test_label_required(self): monitor = ConsoleMonitor() with fetch_std_streams(): with self.assertRaises(ValueError): # "ValueError: label must be given" monitor.start('', total_work=10)
def test_console_monitor_with_progress_bar(self): m = ConsoleMonitor(progress_bar_size=10) with fetch_std_streams() as (stdout, stderr): m.start('task A', total_work=10) m.progress(work=1) m.progress(work=5, msg='phase 1') m.progress(msg='phase 2') m.progress(work=4) m.progress() m.done() actual_stdout = stdout.getvalue() expected_stdout = 'task A: started\n' + \ 'task A: [#---------] 10% \n' + \ 'task A: [######----] 60% phase 1\n' + \ 'task A: phase 2\n' + \ 'task A: [##########] 100% \n' + \ 'task A: in progress...\n' + \ 'task A: done\n' self.assertEqual(actual_stdout, expected_stdout) self.assertEqual(stderr.getvalue(), '')
def test_console_monitor(self): m = ConsoleMonitor() with fetch_std_streams() as (stdout, stderr): m.start('task A', total_work=10) m.progress(work=1) m.progress(work=5, msg='phase 1') m.progress(msg='phase 2') m.progress(work=4) m.progress() m.done() actual_stdout = stdout.getvalue() expected_stdout = 'task A: started\n' + \ 'task A: 10%\n' + \ 'task A: 60% phase 1\n' + \ 'task A: phase 2\n' + \ 'task A: 100%\n' + \ 'task A: progress\n' + \ 'task A: done\n' self.assertEqual(actual_stdout, expected_stdout) self.assertEqual(stderr.getvalue(), '') self.assertTrue(True)