def test_creation_03(self): ta = WeeklyTimeAxisBuilder()\ .set_start_date(date(2019, 1, 1)) \ .set_end_date(date(2019, 1, 15)) \ .build() self.assertEqual(2, ta.nelem)
def test_average_08(self): from_axis = DailyTimeAxisBuilder()\ .set_start_date(date(2019, 1, 1)) \ .set_n_interval(14) \ .build() to_axis = WeeklyTimeAxisBuilder()\ .set_start_date(date(2019, 1, 1)) \ .set_n_interval(2) \ .build() tc = AxisRemapper(from_axis=from_axis, to_axis=to_axis) from_data = np.moveaxis( np.asarray(list(range(1, 15)) * 12).reshape((3, 4, 14)), 2, 1) to_data = tc.average(from_data, dimension=1) self.assertEqual((3, 2, 4), to_data.shape) self.assertListEqual([4] * 12, np.round(to_data[:, 0, :].reshape( (-1, 1))).astype(np.int).flatten().tolist()) self.assertListEqual([11] * 12, np.round(to_data[:, 1, :].reshape( (-1, 1))).astype(np.int).flatten().tolist())
def test_creation_02(self): # This is short of a week by one day. Hence, the interval of one week does not divide the period # properly with self.assertRaises(ValueError): WeeklyTimeAxisBuilder()\ .set_start_date(date(2019, 1, 1)) \ .set_end_date(date(2019, 1, 7)) \ .build()
def test_creation_01(self): from_axis = DailyTimeAxisBuilder()\ .set_start_date(date(2019, 1, 1)) \ .set_end_date(date(2019, 1, 8)) \ .build() to_axis = WeeklyTimeAxisBuilder()\ .set_start_date(date(2019, 1, 1)) \ .set_end_date(date(2019, 1, 8)) \ .build() tc = AxisRemapper(from_axis=from_axis, to_axis=to_axis)
def test_dask_max(self): from_axis = DailyTimeAxisBuilder(start_date=date(2019, 1, 1), n_interval=14).build() to_axis = WeeklyTimeAxisBuilder(start_date=date(2019, 1, 1), n_interval=3).build() tc = AxisRemapper(from_axis=from_axis, to_axis=to_axis, assure_no_bound_mismatch=False) from_data = da.arange(14, dtype='float64') to_data = tc.max(from_data).compute() np.testing.assert_almost_equal( to_data, np.array([6.0, 13.0, np.nan]).reshape(3, 1))
def test_max_01(self): from_axis = DailyTimeAxisBuilder(start_date=date(2019, 1, 1), n_interval=14).build() to_axis = WeeklyTimeAxisBuilder(start_date=date(2019, 1, 1), n_interval=2).build() tc = AxisRemapper(from_axis=from_axis, to_axis=to_axis) from_data = list(range(1, 15)) to_data = tc.max(from_data) self.assertAlmostEqual(7.0, to_data[0, 0], 0) self.assertAlmostEqual(14.0, to_data[1, 0], 0)
def test_dask_array_01(self): daily_axis = DailyTimeAxisBuilder(start_date=date(2019, 1, 1), n_interval=14).build() weekly_axis = WeeklyTimeAxisBuilder(start_date=date(2019, 1, 1), n_interval=2).build() tc = AxisRemapper(from_axis=daily_axis, to_axis=weekly_axis) from_data = da.arange(14) to_data = tc.average(from_data) self.assertTrue(isinstance(to_data, da.Array)) to_data = to_data.compute() self.assertAlmostEqual(3.0, to_data[0, 0], 0) self.assertAlmostEqual(10.0, to_data[1, 0], 0)
def test_apply_function_01(self): daily_axis = DailyTimeAxisBuilder(start_date=date(2019, 1, 1), n_interval=14).build() weekly_axis = WeeklyTimeAxisBuilder(start_date=date(2019, 1, 1), n_interval=2).build() ac = AxisRemapper(from_axis=daily_axis, to_axis=weekly_axis) daily_data = np.random.random((3, 4, 5, 14)) def user_defined_function(data): return np.nansum(data, axis=0) * 42 weekly_user_defined = ac.apply_function(daily_data, user_defined_function, dimension=3)
def test_creation_05(self): ta = WeeklyTimeAxisBuilder( start_date=date(2019, 1, 1), n_interval=2 ).build() self.assertEqual(2, ta.nelem) self.assertEqual( datetime(2019, 1, 8), datetime.utcfromtimestamp(ta.upper_bound[0, 0] / SECONDS_TO_MICROSECONDS_FACTOR) ) self.assertEqual( datetime(2019, 1, 15), datetime.utcfromtimestamp(ta.upper_bound[0, 1] / SECONDS_TO_MICROSECONDS_FACTOR) )
def test_max_05(self): from_axis = DailyTimeAxisBuilder(start_date=date(2019, 1, 1), n_interval=14).build() to_axis = WeeklyTimeAxisBuilder(start_date=date(2019, 1, 1), n_interval=3).build() tc = AxisRemapper(from_axis=from_axis, to_axis=to_axis, assure_no_bound_mismatch=False) from_data = list(range(1, 15)) to_data = tc.max(from_data) self.assertAlmostEqual(7.0, to_data[0, 0], 0) self.assertAlmostEqual(14.0, to_data[1, 0], 0) self.assertTrue(np.isnan(to_data[2, 0]))
def test_get_coverage_03(self): from_axis = DailyTimeAxisBuilder()\ .set_start_date(date(2019, 1, 1)) \ .set_end_date(date(2019, 1, 8)) \ .build() to_axis = WeeklyTimeAxisBuilder()\ .set_start_date(date(2019, 1, 1)) \ .set_end_date(date(2019, 1, 8)) \ .build() row_idx, col_idx, weights = AxisRemapper._get_coverage( from_axis.lower_bound, from_axis.upper_bound, to_axis.lower_bound, to_axis.upper_bound) self.assertListEqual([0] * 7, row_idx) self.assertListEqual(list(range(7)), col_idx) self.assertListEqual([1.0] * 7, weights)
def test_average_01(self): from_axis = DailyTimeAxisBuilder()\ .set_start_date(date(2019, 1, 1)) \ .set_end_date(date(2019, 1, 8)) \ .build() to_axis = WeeklyTimeAxisBuilder()\ .set_start_date(date(2019, 1, 1)) \ .set_end_date(date(2019, 1, 8)) \ .build() tc = AxisRemapper(from_axis=from_axis, to_axis=to_axis) from_data = [1, 2, 3, 4, 5, 6, 7] to_data = tc.average(from_data) self.assertAlmostEqual(4.0, to_data[0, 0], 0)
def test_average_05(self): from_axis = WeeklyTimeAxisBuilder()\ .set_start_date(date(2019, 1, 1)) \ .set_n_interval(2) \ .build() to_axis = DailyTimeAxisBuilder()\ .set_start_date(date(2019, 1, 1)) \ .set_n_interval(14) \ .build() tc = AxisRemapper(from_axis=from_axis, to_axis=to_axis) from_data = [4.0, 11] to_data = tc.average(from_data) expected = [4] * 7 + [11] * 7 self.assertListEqual(expected, to_data.flatten().tolist())
def test_average_04(self): from_axis = DailyTimeAxisBuilder()\ .set_start_date(date(2019, 1, 1)) \ .set_n_interval(14) \ .build() to_axis = WeeklyTimeAxisBuilder()\ .set_start_date(date(2019, 1, 1)) \ .set_n_interval(2) \ .build() tc = AxisRemapper(from_axis=from_axis, to_axis=to_axis) from_data = list(range(1, 15)) to_data = tc.average(from_data) self.assertAlmostEqual(4.0, to_data[0, 0], 0) self.assertAlmostEqual(11.0, to_data[1, 0], 0)
def test_dask_array_05(self): daily_axis = DailyTimeAxisBuilder(start_date=date(2019, 1, 1), n_interval=14).build() weekly_axis = WeeklyTimeAxisBuilder(start_date=date(2019, 1, 1), n_interval=2).build() tc = AxisRemapper(from_axis=daily_axis, to_axis=weekly_axis) from_data = da.from_array(np.moveaxis( np.asarray(np.arange(14).tolist() * 12).reshape(3, 4, 14), 2, 0), chunks=(2, 1, 1)) to_data = tc.average(from_data) self.assertTrue(isinstance(to_data, da.Array)) to_data = to_data.compute() np.testing.assert_almost_equal( to_data, np.asarray([3] * 12 + [10] * 12).reshape(2, 3, 4))
def test_min_02(self): from_axis = DailyTimeAxisBuilder(start_date=date(2019, 1, 1), n_interval=14).build() to_axis = WeeklyTimeAxisBuilder(start_date=date(2019, 1, 1), n_interval=2).build() tc = AxisRemapper(from_axis=from_axis, to_axis=to_axis) from_data = np.moveaxis( np.asarray(list(range(1, 15)) * 12).reshape((3, 4, 14)), 2, 0).tolist() to_data = tc.min(from_data) self.assertTrue( np.all(np.ones((3, 4), dtype="int") - to_data[0].round() == 0.0)) self.assertTrue( np.all( np.ones((3, 4), dtype="int") * 8 - to_data[1].round() == 0.0))
def test_average_02(self): from_axis = WeeklyTimeAxisBuilder()\ .set_start_date(date(2019, 1, 1)) \ .set_end_date(date(2019, 1, 8)) \ .build() to_axis = DailyTimeAxisBuilder()\ .set_start_date(date(2019, 1, 1)) \ .set_end_date(date(2019, 1, 8)) \ .build() tc = AxisRemapper(from_axis=from_axis, to_axis=to_axis) from_data = [4.0] to_data = tc.average(from_data) self.assertEqual((tc.to_nelem, 1), to_data.shape) for e in to_data: self.assertAlmostEqual(4.0, e[0], 0)
def test_max_03(self): from_axis = DailyTimeAxisBuilder(start_date=date(2019, 1, 1), n_interval=14).build() to_axis = WeeklyTimeAxisBuilder(start_date=date(2019, 1, 1), n_interval=2).build() tc = AxisRemapper(from_axis=from_axis, to_axis=to_axis) from_data = np.moveaxis( np.asarray(list(range(1, 15)) * 12).reshape((3, 4, 14)), 2, 1).tolist() to_data = tc.max(from_data, dimension=1) self.assertTrue( np.all( np.ones((3, 4), dtype="int") * 7 - to_data[:, 0, :].round() == 0.0)) self.assertTrue( np.all( np.ones((3, 4), dtype="int") * 14 - to_data[:, 1, :].round() == 0.0))