Exemple #1
0
    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())
Exemple #2
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)
Exemple #3
0
    def test_average_11(self):
        from_axis = DailyTimeAxisBuilder(start_date=date(2019, 1, 1),
                                         n_interval=14).build()

        to_axis = RollingWindowTimeAxisBuilder(start_date=date(2019, 1, 1),
                                               end_date=date(2019, 1, 15),
                                               window_size=7).build()

        tc = AxisRemapper(from_axis=from_axis, to_axis=to_axis)

        from_data = list(range(1, 15))

        to_data = tc.average(from_data)
        self.assertEqual(8, to_data.size)

        for idx in range(8):
            self.assertAlmostEqual(idx + 4, to_data[idx, 0], 0)
Exemple #4
0
    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)
Exemple #5
0
    def test_average_10(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.average(from_data)

        self.assertAlmostEqual(4.0, to_data[0, 0], 0)
        self.assertAlmostEqual(11.0, to_data[1, 0], 0)
        self.assertTrue(np.isnan(to_data[2, 0]))
Exemple #6
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())
Exemple #7
0
    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)
Exemple #8
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))
Exemple #9
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)