def test_accumulate__steps(padding, sample_key): ts = TimeSeries(sample_data[sample_key]) ts.accumulate(1, "Month", padding=padding) for i in range(1, len(ts.data)): assert ((ts.data[i][0] - ts.data[i - 1][0]).days == calendar.monthrange( ts.data[i - 1][0].year, ts.data[i - 1][0].month)[1])
def test_accumulate__steps(padding, sample_key): ts = TimeSeries(sample_data[sample_key]) ts.accumulate(1, "Year", padding=padding) for i in range(1, len(ts.data)): assert (ts.data[i - 1][0].day == 1) assert (ts.data[i - 1][0].month == 1) assert (ts.data[i][0].year - ts.data[i - 1][0].year == 1)
def test_get_y(): ts = TimeSeries(sample_data) y = ts.get_y() #Every value should be equal to the to the first column in ts.data for i in range(len(y)): assert(y[i]==ts.data[i][1])
def test_accumulate__len(padding, sample_key): ts = TimeSeries(sample_data[sample_key]) original_len = len(ts.data) start_date = expected_start_date(ts.data[0][0], padding) end_date = expected_end_date(ts.data[-1][0], padding) expected_len = ((end_date - start_date).days + 7) / 7 ts.accumulate(1, "Week", padding=padding) assert (len(ts.data) == expected_len)
def test_accumulate__len(padding,sample_key,delta): ts = TimeSeries(sample_data[sample_key]) original_len = len(ts.data) start_date = expected_start_date(ts.data[0][0],delta,original_len,padding) end_date = expected_end_date(ts.data[-1][0],delta,original_len,padding) expected_len = (end_date-start_date).days/delta+1 ts.accumulate(delta,"Day",padding=padding) assert(len(ts.data) == expected_len)
def test_accumulate__len(padding, sample_key): ts = TimeSeries(sample_data[sample_key]) original_len = len(ts.data) start_date = expected_start_date(ts.data[0][0], padding) end_date = expected_end_date(ts.data[-1][0], padding) expected_len = end_date.year - start_date.year + 1 ts.accumulate(1, "Year", padding=padding) assert (len(ts.data) == expected_len)
def test_accumulate_twice(padding): #If you accumulate twice, it should be the same as delta1*delta2 #This is unimplemented ts1 = TimeSeries(sample_data) ts2 = TimeSeries(sample_data) ts1.accumulate(10,padding=padding) ts2.accumulate(2,padding=padding) with pytest.raises(NotImplementedError): ts2.accumulate(5,padding=padding)
def test_accumulate__sum(padding,sample_key,delta): sd = sample_data[sample_key] ts = TimeSeries(sd) ts.accumulate(delta,"Day",padding=padding) for i in range(len(ts.data)): cum_sum = 0 for data in sd: if ts.data[i][0]<=data[0]<ts.data[i][0]+datetime.timedelta(delta): cum_sum+=data[1] assert(ts.data[i][1]==cum_sum)
def test_accumulate__sum(padding, sample_key): sd = sample_data[sample_key] ts = TimeSeries(sd) ts.accumulate(1, "Month", padding=padding) for i in range(len(ts.data)): cum_sum = 0 for data in sd: if ts.data[i][0] <= data[0] < ts.data[i][0] + datetime.timedelta( calendar.monthrange(ts.data[i][0].year, ts.data[i][0].month)[1]): cum_sum += data[1] assert (ts.data[i][1] == cum_sum)
def test_accumulate__sum(padding, sample_key): #If padding false, the first day should be skipped (in this case) #If padding is true, only the first day should be summed (together with two empty days) sd = sample_data[sample_key] ts = TimeSeries(sd) ts.accumulate(1, "Week", padding=padding) for i in range(len(ts.data)): cum_sum = 0 for data in sd: if ts.data[i][0] <= data[0] < ts.data[i][0] + datetime.timedelta( 7): cum_sum += data[1] assert (ts.data[i][1] == cum_sum)
def test_init__no_missing_dates(): ts = TimeSeries(sample_data) #All dates should be present between the first and the last, an only occur once last_date = ts.data[0][0] for data in ts.data[1:]: assert(last_date+datetime.timedelta(1)==data[0]) last_date = data[0]
def test_init__correct_sum(): ts = TimeSeries(sample_data) #The total should be the sum of all the individual posts in sample_data sum1 = 0 for data in ts.data: sum1 += data[1] assert(sum1==200+50+200+400-300+100)
def test_accumulate__end_date(padding, sample_key): sd = sample_data[sample_key] ts = TimeSeries(sd) ts.accumulate(1, "Year", padding=padding) assert (ts.get_x()[-1] == expected_end_date(sd[-1][0], padding)) assert (ts.get_x()[-1].day == 1) assert (ts.get_x()[-1].month == 1)
def test_accumulate__empty(): #It should be possible to initialize with an empty list ts = TimeSeries([]) ts.accumulate(2) assert(ts.data==[])
def test_accumulate__start_date(padding, sample_key): sd = sample_data[sample_key] ts = TimeSeries(sd) ts.accumulate(1, "Month", padding=padding) assert (ts.get_x()[0] == expected_start_date(sd[0][0], padding)) assert (ts.get_x()[0].day == 1)
def test_accumulate__end_date(padding,sample_key,delta): sd = sample_data[sample_key] ts = TimeSeries(sd) original_len = len(ts.data) ts.accumulate(delta,"Day",padding=padding) assert(ts.get_x()[-1]==expected_end_date(sd[-1][0],delta,original_len,padding))
def test_moving_average__empty(): #It should be possible to initialize with an empty list ts = TimeSeries([]) ts.moving_average(3) assert(ts.data==[])
def test_moving_average__first_val(): ts = TimeSeries(sample_data) original_len = len(ts.data) ts.moving_average(3) assert(ts.data[0][-1]==(200+50)/3)
def test_moving_average__last_day(window): ts = TimeSeries(sample_data) original_len = len(ts.data) ts.moving_average(window) assert(ts.data[-1][0]==sample_data[-1][0])
def test_moving_average__first_day(window): ts = TimeSeries(sample_data) ts.moving_average(window) assert(ts.data[0][0]==sample_data[0][0]+datetime.timedelta(window-1))
def test_accumulate__end_date(padding, sample_key): sd = sample_data[sample_key] ts = TimeSeries(sd) ts.accumulate(1, "Week", padding=padding) assert (ts.get_x()[-1] == expected_end_date(sd[-1][0], padding)) assert (ts.get_x()[-1].weekday() == 0)
def test_init__empty(): #It should be possible to initialize with an empty list ts = TimeSeries([]) assert(ts.data==[])
def test_accumulate__steps(padding, sample_key): ts = TimeSeries(sample_data[sample_key]) ts.accumulate(1, "Week", padding=padding) for i in range(1, len(ts.data)): assert ((ts.data[i][0] - ts.data[i - 1][0]).days == 7)
def test_moving_average__len(window): ts = TimeSeries(sample_data) original_len = len(ts.data) ts.moving_average(window) assert(len(ts.data)==original_len-window+1)
def get_timeseries(self): return TimeSeries(self.get_data(), daterange=self._f_daterange)
def test_init__length(): ts = TimeSeries(sample_data) #The lenght of the timeseries should be the number of number of days between first and last day of data 1 assert(len(ts.data)==11)