def test_creation_01(self): lower_bound = self._sample_date[0]["lower_bound"] upper_bound = self._sample_date[0]["upper_bound"] data_ticks = self._sample_date[0]["data_ticks"] ta = Axis(lower_bound, upper_bound, data_ticks=data_ticks) print("Sample TimeAxis Initialized: ", ta.asJson()) expected = '{"nelem": 6, "lower_bound": [1546300800000000, 1546387200000000, 1546473600000000, 1546560000000000, 1546646400000000, 1546732800000000], ' \ '"upper_bound": [1546387200000000, 1546473600000000, 1546560000000000, 1546646400000000, 1546732800000000, 1546819200000000], ' \ '"data_ticks": [1546344000000000, 1546430400000000, 1546516800000000, 1546603200000000, 1546689600000000, 1546776000000000], ' \ '"fraction": [0.5], ' \ '"binding": "middle"}' self.assertEqual(expected, ta.asJson()) self.assertListEqual(lower_bound, ta.lower_bound.tolist()[0]) self.assertListEqual(upper_bound, ta.upper_bound.tolist()[0]) self.assertListEqual(data_ticks, ta.data_ticks.tolist()[0]) expected = [0.5] self.assertListEqual(expected, ta.fraction.tolist()[0]) self.assertEqual(len(lower_bound), ta.nelem) self.assertEqual("2019-01-01 12:00:00", ta[0].asDict()["data_tick"]) self.assertEqual("2019-01-02 12:00:00", ta[1].asDict()["data_tick"]) self.assertEqual("2019-01-03 12:00:00", ta[2].asDict()["data_tick"]) self.assertEqual("2019-01-04 12:00:00", ta[3].asDict()["data_tick"]) self.assertEqual("2019-01-05 12:00:00", ta[4].asDict()["data_tick"]) self.assertEqual("2019-01-06 12:00:00", ta[5].asDict()["data_tick"])
def test_get_coverage_01(self): from_axis = Axis(lower_bound=np.arange(0, 7) * 24, upper_bound=np.arange(1, 8) * 24, fraction=0.5) to_axis = Axis(lower_bound=[0], upper_bound=[168], data_ticks=[84]) 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, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0], weights)
def test_creation_04(self): lower_bound = [i * 24 for i in range(7)] upper_bound = [lower_bound[i] + 24 for i in range(7)] axis = Axis(lower_bound=lower_bound, upper_bound=upper_bound, binding="middle") self.assertEqual( '{"nelem": 7, ' '"lower_bound": [0, 24, 48, 72, 96, 120, 144], ' '"upper_bound": [24, 48, 72, 96, 120, 144, 168], ' '"data_ticks": [12, 36, 60, 84, 108, 132, 156], ' '"fraction": [0.5], ' '"binding": "middle"}', axis.asJson())
def build(self) -> Axis: if self.prebuild_check(): lower_bound, data_tickes, upper_bound = TimeAxisBuilderFromDataTicks._calculate_bounds( data_ticks=self._data_ticks, boundary_type=self._boundary_type ) return Axis( lower_bound=lower_bound, upper_bound=upper_bound, data_ticks=data_tickes )
def test_creation_05(self): lower_bound = [i * 24 for i in range(7)] upper_bound = [lower_bound[i] + 24 for i in range(7)] from numpy.random import random fraction = random(7) axis = Axis(lower_bound=lower_bound, upper_bound=upper_bound, fraction=fraction) fraction_str = ", ".join(map(lambda e: str(e), fraction)) data_ticks = [ (1 - fraction[i]) * lower_bound[i] + fraction[i] * upper_bound[i] for i in range(7) ] data_ticks_str = ", ".join(map(lambda e: str(int(e)), data_ticks)) self.assertEqual( '{"nelem": 7, ' '"lower_bound": [0, 24, 48, 72, 96, 120, 144], ' '"upper_bound": [24, 48, 72, 96, 120, 144, 168], ' f'"data_ticks": [{data_ticks_str}], ' f'"fraction": [{fraction_str}], ' '"binding": "custom_fraction"}', axis.asJson())
def build(self) -> Axis: if self.prebuild_check(): bounds = TimeAxisBuilder.to_utc_timestamp( [date(year, 1, 1) for year in range(self._start_year, self._end_year+1)], self.second_conversion_factor ).reshape((-1,)) lower_bound = bounds[:-1] upper_bound = bounds[1:] data_ticks = 0.5 * (lower_bound + upper_bound) return Axis( lower_bound=lower_bound, upper_bound=upper_bound, data_ticks=data_ticks )
def build(self) -> Axis: if self.prebuild_check(): if self._end is not None: self._n_window = np.ceil((self._end - self._start) / self._base) - (self._window_size - 1) if self._n_window < 1: raise ValueError( "the provided end_date and start_date resulted in 0 n_window." ) lower_bound = self._start + \ np.arange(self._n_window, dtype="int64") * self._base window_length = self._window_size * self._base upper_bound = lower_bound + window_length data_tick = 0.5 * (lower_bound + upper_bound) return Axis(lower_bound=lower_bound, upper_bound=upper_bound, data_ticks=data_tick)
def build(self) -> Axis: if self.prebuild_check(): if self._mask == 7: # this means start, end, and interval are provided self._n_interval = int( (self._end - self._start) / self._interval) if self._n_interval < 1: raise ValueError( "provided input leaded to wrong n_interval.") if (self._start + self._n_interval * self._interval) != self._end: raise ValueError( "provided interval does not divide the start to end interval properly." ) if self._mask == 11: # this means start, end, and n_interval are provided self._interval = int( (self._end - self._start) / self._n_interval) if self._mask == 13: # this means start, interval, and n_interval are provided self._end = self._start + self._n_interval * self._interval if self._mask == 14: # this means end, interval, and n_interval are provided self._start = self._end - self._n_interval * self._interval lower_bound = self._start + np.arange( self._n_interval, dtype="int64") * self._interval upper_bound = lower_bound + self._interval if upper_bound[-1] != self._end: raise ValueError( f"last element of upper_bound (i.e. {upper_bound[-1]}) is not the same " f"as provided end (i.e. {self._end}).") data_ticks = (1 - self._fraction ) * lower_bound + self._fraction * upper_bound return Axis(lower_bound, upper_bound, data_ticks=data_ticks)
def build(self) -> Axis: if self.prebuild_check(): if isinstance(self._interval, int): lower_bound = np.arange(self._start, self._end, self._interval) upper_bound = np.arange(self._start + self._interval, self._end + 1, self._interval, dtype="int64") elif isinstance(self._interval, np.ndarray): interval_cumsum = np.concatenate((np.zeros( (1, 1)), self._interval.cumsum().reshape((1, -1))), axis=1).astype(dtype="int64") lower_bound = self._start + interval_cumsum[0, :-1] upper_bound = self._start + interval_cumsum[0, 1:] else: raise TypeError( "Somehow interval ended up to be of a type other than an int or numpy.ndarray " "(Iterable)") data_ticks = (1 - self._fraction ) * lower_bound + self._fraction * upper_bound return Axis(lower_bound, upper_bound, data_ticks=data_ticks)
def build(self) -> Axis: if self.prebuild_check(): start = self._start.year * 12 + (self._start.month - 1) end = self._end.year * 12 + (self._end.month - 1) + 1 lower_bound = TimeAxisBuilder.to_utc_timestamp( [date(v // 12, (v % 12) + 1, 1) for v in range(start, end)], self.second_conversion_factor ).reshape((-1, )) upper_bound = TimeAxisBuilder.to_utc_timestamp( [date(v // 12, (v % 12) + 1, 1) for v in range(start + 1, end + 1)], self.second_conversion_factor ).reshape((-1,)) data_ticks = 0.5 * (lower_bound + upper_bound) return Axis( lower_bound=lower_bound, upper_bound=upper_bound, data_ticks=data_ticks )
def test_fromJson(self): json_str = '{"nelem": 7, "lower_bound": [0, 24, 48, 72, 96, 120, 144], "upper_bound": [24, 48, 72, 96, 120, 144, 168], "data_ticks": [12, 36, 60, 84, 108, 132, 156], "fraction": [0.5], "binding": "middle"}' axis = Axis.fromJson(json_str)