def process_dataframe(self, dataframe, start, end, sources, resample='T'): source_boundary = max( [x.boundary for x in sources] if sources else [timedelta(0)]) fields = [f for f in dataframe.columns if f not in self._HIDDEN_FIELDS] dataframe = self.sort_dataframe(dataframe)[fields + ['source']] if dataframe.empty: return TimeSeries.from_dataframe( dataframe[fields], source_boundary).dataframe.reset_index() output_dataframe = None for src in sources: src_timeseries = TimeSeries.from_dataframe( dataframe[dataframe['source'] == src.slug][fields], source_boundary) src_dataframe = src_timeseries.query(start, end, 'T') if output_dataframe is None: output_dataframe = src_dataframe continue output_dataframe = output_dataframe.combine_first(src_dataframe) dataframe = output_dataframe dataframe = self.model.postprocess_dataframe(dataframe) dataframe = dataframe.asfreq(resample) return dataframe.reset_index()
def test_iters(self): ts = TimeSeries([1, 2, 3], [4, 5, 6]) times = ts.times() count = 0 for item in ts: self.assertEqual(item, ts[times[count]]) count += 1 self.assertEqual(count, len(ts)) count = 0 for item in ts.itervalues(): self.assertEqual(item, ts[times[count]]) count += 1 self.assertEqual(count, len(ts)) count = 0 for item in ts.itertimes(): self.assertEqual(item, times[count]) count += 1 self.assertEqual(count, len(ts)) count = 0 for item in ts.iteritems(): self.assertEqual(item, (times[count], ts[times[count]])) count += 1 self.assertEqual(count, len(ts))
def test_add(self): a = TimeSeries([1, 2, 3], [1, 2, 3]) b = TimeSeries([1, 2, 3], [4, 5, 6]) c = TimeSeries([4, 5, 6], [4, 5, 6]) self.assertEqual(a + b, TimeSeries([1, 2, 3], [5, 7, 9])) with self.assertRaises(ValueError): d = b + c
def process_dataframe(self, dataframe, start, end, sources, resample='T'): source_boundary = max([x.boundary for x in sources] if sources else [timedelta(0)]) fields = [f for f in dataframe.columns if f not in self._HIDDEN_FIELDS] dataframe = self.sort_dataframe(dataframe)[fields + ['source']] if dataframe.empty: return TimeSeries.from_dataframe( dataframe[fields], source_boundary).dataframe.reset_index() output_dataframe = None for src in sources: src_timeseries = TimeSeries.from_dataframe( dataframe[dataframe['source'] == src.slug][fields], source_boundary) src_dataframe = src_timeseries.query(start, end, 'T') if output_dataframe is None: output_dataframe = src_dataframe continue output_dataframe = output_dataframe.combine_first(src_dataframe) dataframe = output_dataframe dataframe = self.model.postprocess_dataframe(dataframe) dataframe = dataframe.asfreq(resample) return dataframe.reset_index()
def test_mul(self): a = TimeSeries([1, 2, 3], [1, 2, 3]) b = TimeSeries([1, 2, 3], [4, 5, 6]) c = TimeSeries([4, 5, 6], [4, 5, 6]) self.assertEqual(a * b, TimeSeries([1, 2, 3], [4, 10, 18])) with self.assertRaises(ValueError): d = b * c
def test_neg(self): self.assertListEqual( list(TimeSeries([1, 2, 3], [-1, 2, -4]).__neg__()), [1, -2, 4]) self.assertListEqual(list(TimeSeries([1, 2, 3], [1, 2, 4]).__neg__()), [-1, -2, -4]) self.assertListEqual( list(TimeSeries([1, 2, 3], [-4, -6, -7]).__neg__()), [4, 6, 7]) with self.assertRaises(ValueError): TimeSeries([], []).__neg__()
def test_range_slice_with_step(self): t = TimeSeries(range(0, 10), range(0, 10)) t_slice = t[0:8:2] self.assertEqual(t_slice.times, [0, 2, 4, 6]) self.assertEqual(t_slice.values, [0, 2, 4, 6]) t = TimeSeries(range(2, 10, 2), range(2, 10, 2)) t_slice = t[3:7:2] self.assertEqual(t_slice.times, [3, 5]) self.assertEqual(t_slice.values, [2, 4])
def test_range_slice_with_interpolation_and_step(self): t = TimeSeries(range(2, 10, 2), range(2, 10, 2), interpolate=True) t_slice = t[3:8:2] self.assertEqual(t_slice.times, [3, 5, 7]) self.assertEqual(t_slice.values, [3, 5, 7]) t_slice = t[3:7:2] self.assertEqual(t_slice.times, [3, 5]) self.assertEqual(t_slice.values, [3, 5])
def test_range_slice_with_first_val(self): t = TimeSeries(range(2, 10, 2), range(2, 10, 2), first_val=-1) t_slice = t[0:6] self.assertEqual(t_slice.times, [0, 2, 4]) self.assertEqual(t_slice.values, [-1, 2, 4]) t_slice = t[0:] self.assertEqual(t_slice.times, [0, 2, 4, 6, 8]) self.assertEqual(t_slice.values, [-1, 2, 4, 6, 8])
def test_range_slice(self): t = TimeSeries(range(2, 10, 2), range(2, 10, 2)) t_slice = t[2:] self.assertEqual(t_slice.times, [2, 4, 6, 8]) self.assertEqual(t_slice.values, [2, 4, 6, 8]) t_slice = t[2:8] self.assertEqual(t_slice.times, [2, 4, 6]) self.assertEqual(t_slice.values, [2, 4, 6]) t_slice = t[:6] self.assertEqual(t_slice.times, [2, 4]) self.assertEqual(t_slice.values, [2, 4])
def draw_time_series(self, *args, **kwargs): """ Draw a time series visualization on this :class:`~Drawable`. The arguments and keyword arguments are those supported by the :class:`~timeseries.timeseries.TimeSeries`' :func:`~timeseries.timeseries.TimeSeries.draw` method. :return: A tuple made up of the drawn plot and label. :rtype: tuple """ self.timeseries = self.timeseries or TimeSeries(self) return self.timeseries.draw(*args, **kwargs)
def draw_time_series(self, *args, **kwargs): """ Draw a time series visualization on this :class:`~Drawable`. The arguments and keyword arguments are those supported by :meth:`~text.annotation.TextAnnotation.draw` method. :return: A tuple made up of the drawn plot and label. :rtype: tuple """ self.timeseries = self.timeseries if self.timeseries is not None else TimeSeries( self) return self.timeseries.draw(*args, **kwargs)
def random_ts(self, a): ''' Randomly generate a time series with 100 time-value points. The time of the time series are 100 equally spaced values between 0 and 1. The values of the time series are 100 randomly generated values between 0 and a. Param: a: int, a multiplicative constant to generate 100 values for the time series Return: A time series with 100 time-value points ''' t = np.arange(0.0, 1.0, 0.01) v = a * np.random.random(100) # return ts.TimeSeries(t, v) return TimeSeries(t, v)
def stand(self, x, m, s): ''' Standardize a variable x, using its mean m and its standard deviation s. Param: x : TimeSeries, the time series variable to standardize m : float, the time series values' mean s : float, the time series values' standard deviation Return: The standardized time series. ''' time = x._times value = x._data for i in range(len(value)): value[i] = (value[i] - m) / float(s) return TimeSeries(time, value)
def test_similarity(): num_ts = 50 num_vp = 20 generate_ts(num_ts) #with raises(Exception): generateDB(num_ts, num_vp) #curr_dir = os.getcwd().split('/') #print(curr_dir) file_name = 'vp_origin_idx.dat' vantage_point_index_list = np.loadtxt(file_name, delimiter=',') x = np.loadtxt('tsfiles/ts_1.dat', delimiter=',') ##### query_ts = TimeSeries(x[:, 0], x[:, 1]) nearest_ts = vp_similarity_search(query_ts, list(vantage_point_index_list)) #print(nearest_ts) assert len(nearest_ts) == 10 and nearest_ts[0] == 'ts_1'
def tsmaker(self, m, s, j): ''' Generate a time series with 100 time-value points. The time of the time series are 100 equally spaced values between 0 and 1. The values of the time series are 100 values generated from a normal distribution with mean s and standard deviation s and random noises from 0 and j. Param: m : float, the time series values' mean s : float, the time series values' standard deviation j : float, the multiplicative constant to generate noises Return: TimeSeries, a time series with 100 time-value points ''' t = np.arange(0.0, 1.0, 0.01) v = norm.pdf(t, m, s) + j * np.random.randn(100) # return meta, ts.TimeSeries(t, v) return TimeSeries(t, v)
def test_shifted(self): t = shifted(TimeSeries(range(5), range(5)), 1) self.assertEqual(t.times, [0, 1, 2, 3]) self.assertEqual(t.values, [1, 2, 3, 4])
def test_creation_from_dict(self): t = TimeSeries({x: x for x in range(5)}) self.assertEqual(t.times, [0, 1, 2, 3, 4]) self.assertEqual(t.values, [0, 1, 2, 3, 4])
def test_bool(self): self.assertEqual(bool(TimeSeries([1, 2, 3], [-1, 2, -4])), True) self.assertEqual(bool(TimeSeries([1, 2, 3], [1, 2, 4])), True) self.assertEqual(bool(TimeSeries([1, 2, 3], [0, 0, 0])), False)
def test_abs(self): self.assertEqual(abs(TimeSeries([1, 2, 3], [-1, 2, -4])), np.sqrt(21)) self.assertEqual(abs(TimeSeries([1, 2, 3], [1, 2, 4])), np.sqrt(21)) self.assertEqual(abs(TimeSeries([1, 2, 3], [0, 0, 0])), 0) with self.assertRaises(ValueError): abs(TimeSeries([], []))
def test_median(self): self.assertEqual(TimeSeries([1, 2, 3], [2, 2, 2]).median(), 2) self.assertEqual(TimeSeries([1, 2, 3], [0, 2, 0]).median(), 0) self.assertEqual(TimeSeries([1, 2, 3, 4], [0, 2, 2, 0]).median(), 1) with self.assertRaises(ValueError): TimeSeries([], []).median()
def test_sincle_slice_with_interpolation(self): t = TimeSeries(range(2, 6, 2), range(2, 6, 2), interpolate=True) self.assertEqual(t[0], (0, 0)) self.assertEqual(t[2], (2, 2)) self.assertEqual(t[3], (3, 3))
def test_single_slice(self): t = TimeSeries(range(2, 6, 2), range(2, 6, 2)) self.assertEqual(t[0], (0, 0)) self.assertEqual(t[1], (1, 0)) self.assertEqual(t[2], (2, 2)) self.assertEqual(t[3], (3, 2))
def test_sincle_slice_with_first_val(self): t = TimeSeries(range(2, 6, 2), range(2, 6, 2), first_val=-1) self.assertEqual(t[0], (0, -1)) self.assertEqual(t[1], (1, -1)) self.assertEqual(t[2], (2, 2))
def test_aligned_subtract(self): t = TimeSeries(range(5), range(5)) - TimeSeries(range(5), range(5)) self.assertEqual(t.times, [0, 1, 2, 3, 4]) self.assertEqual(t.values, [0, 0, 0, 0, 0])
def test_misaligned_add(self): t = TimeSeries([0, 2, 4], [0, 2, 4]) + TimeSeries([1, 2, 3], [1, 2, 3]) self.assertEqual(t.times, [0, 1, 2, 3, 4]) self.assertEqual(t.values, [0, 1, 4, 5, 7])
def test_creation_from_lists(self): t = TimeSeries(range(5), range(5)) self.assertEqual(t.times, [0, 1, 2, 3, 4]) self.assertEqual(t.values, [0, 1, 2, 3, 4])
def test_pruned(self): t = pruned(TimeSeries(range(5), range(5)), 2) self.assertEqual(t.times, [0, 2, 4]) self.assertEqual(t.values, [0, 2, 4])
def test_tilted(self): t = tilted(TimeSeries(range(5), range(5)), 8) self.assertEqual(t.times, [0, 1, 2, 3, 4]) self.assertEqual(t.values, [0, 2, 4, 6, 8])
def test_aligned_add(self): t = TimeSeries(range(5), range(5)) + TimeSeries(range(5), range(5)) self.assertEqual(t.times, [0, 1, 2, 3, 4]) self.assertEqual(t.values, [0, 2, 4, 6, 8])
def test_range_slice_with_interpolation(self): t = TimeSeries(range(2, 10, 2), range(2, 10, 2), interpolate=True) t_slice = t[3:6] self.assertEqual(t_slice.times, [3, 4]) self.assertEqual(t_slice.values, [3, 4])