def test_pipe_transformation(): # SegmentX transform pipe pipe = Pype([('seg', SegmentX()), ('ftr', FeatureRep()), ('scaler', StandardScaler())]) Xt = [np.random.rand(1000, 10), np.random.rand(100, 10), np.random.rand(500, 10)] Xc = np.random.rand(3, 3) X = TS_Data(Xt, Xc) y = [1, 2, 3] transformation_test(pipe, X, y) X = pd.DataFrame(Xc) X['ts_data'] = Xt X = TS_Data.from_df(X) transformation_test(pipe, X, y) # SegmentXY transform pipe pipe = Pype([('seg', SegmentXY()), ('ftr', FeatureRep()), ('scaler', StandardScaler())]) Xt = [np.random.rand(1000, 10), np.random.rand(100, 10), np.random.rand(500, 10)] Xc = np.random.rand(3, 3) X = TS_Data(Xt, Xc) y = [np.random.rand(1000), np.random.rand(100), np.random.rand(500)] transformation_test(pipe, X, y) X = pd.DataFrame(Xc) X['ts_data'] = Xt X = TS_Data.from_df(X) transformation_test(pipe, X, y) # Forecast transform pipe pipe = Pype([('seg', SegmentXYForecast()), ('ftr', FeatureRep()), ('scaler', StandardScaler())]) Xt = [np.random.rand(1000, 10), np.random.rand(100, 10), np.random.rand(500, 10)] Xc = np.random.rand(3, 3) X = TS_Data(Xt, Xc) y = [np.random.rand(1000), np.random.rand(100), np.random.rand(500)] transformation_test(pipe, X, y) X = pd.DataFrame(Xc) X['ts_data'] = Xt X = TS_Data.from_df(X) transformation_test(pipe, X, y) # Padtrunc transform pipe pipe = Pype([('trunc', PadTrunc()), ('ftr', FeatureRep()), ('scaler', StandardScaler())]) Xt = [np.random.rand(1000, 10), np.random.rand(100, 10), np.random.rand(500, 10)] Xc = np.random.rand(3, 3) X = TS_Data(Xt, Xc) y = [1, 2, 3] transformation_test(pipe, X, y) X = pd.DataFrame(Xc) X['ts_data'] = Xt X = TS_Data.from_df(X) transformation_test(pipe, X, y)
def test_pipe_forecast(): # no context data, single time series X = [np.random.rand(1000, 10)] y = [np.random.rand(1000)] pipe = Pype([('seg', SegmentXYForecast()), ('ftr', FeatureRep()), ('ridge', Ridge())]) forecast_test(pipe, X, y) # context data, single time seres Xt = [np.random.rand(1000, 10)] Xc = [np.random.rand(3)] X = TS_Data(Xt, Xc) y = [np.random.rand(1000)] forecast_test(pipe, X, y) X = pd.DataFrame(Xc) X['ts_data'] = Xt X = TS_Data.from_df(X) forecast_test(pipe, X, y) # multiple time seres Xt = [ np.random.rand(1000, 10), np.random.rand(100, 10), np.random.rand(500, 10) ] Xc = np.random.rand(3, 3) X = TS_Data(Xt, Xc) y = [np.random.rand(1000), np.random.rand(100), np.random.rand(500)] forecast_test(pipe, X, y) X = pd.DataFrame(Xc) X['ts_data'] = Xt X = TS_Data.from_df(X) forecast_test(pipe, X, y) # cross val Xt = np.array([np.random.rand(1000, 10)] * 5) Xc = np.random.rand(5, 3) X = TS_Data(Xt, Xc) y = np.array([np.random.rand(1000)] * 5) cross_validate(pipe, X, y, cv=3) X = pd.DataFrame(Xc) Xt = [np.random.rand(1000, 10)] * 5 X['ts_data'] = Xt X = TS_Data.from_df(X) cross_validate(pipe, X, y, cv=3)
def test_pipe_classification(): # no context data, single time series X = [np.random.rand(1000, 10)] y = [5] pipe = Pype([('seg', SegmentX()), ('ftr', FeatureRep()), ('rf', RandomForestClassifier(n_estimators=10))]) classifier_test(pipe, X, y) # context data, single time seres Xt = [np.random.rand(1000, 10)] Xc = [np.random.rand(3)] X = TS_Data(Xt, Xc) y = [5] classifier_test(pipe, X, y) X = pd.DataFrame(Xc) X['ts_data'] = Xt X = TS_Data.from_df(X) classifier_test(pipe, X, y) # multiple time series Xt = [ np.random.rand(1000, 10), np.random.rand(100, 10), np.random.rand(500, 10) ] Xc = np.random.rand(3, 3) X = TS_Data(Xt, Xc) y = [1, 2, 3] classifier_test(pipe, X, y) X = pd.DataFrame(Xc) X['ts_data'] = Xt X = TS_Data.from_df(X) classifier_test(pipe, X, y) # univariate data Xt = [np.random.rand(1000), np.random.rand(100), np.random.rand(500)] Xc = np.random.rand(3) X = TS_Data(Xt, Xc) y = [1, 2, 3] classifier_test(pipe, X, y) X = pd.DataFrame(Xc) X['ts_data'] = Xt X = TS_Data.from_df(X) classifier_test(pipe, X, y)
def test_pd(): ts = np.array([np.random.rand(100, 10), np.random.rand(200, 10), np.random.rand(20, 10)], dtype=object) c = np.random.rand(3, 10) df = pd.DataFrame(c) df['ts_data'] = ts data = TS_Data.from_df(df) assert np.all([np.array_equal(data.ts_data[i], ts[i]) for i in range(len(ts))]) assert np.array_equal(data.context_data, c)
def test_trle(): # Multivariate data Nt = 100 nvars = 5 X = [np.random.rand(Nt, nvars)] y = [ np.concatenate( [np.full(3, 1), np.full(26, 2), np.full(1, 3), np.full(70, 4)]) ] rle = TargetRunLengthEncoder(min_length=5) rle.fit(X) Xt, yt, _ = rle.transform(X, y) assert len(Xt) == len(yt) and len(yt) == 2 assert yt[0] == 2 and yt[1] == 4 assert len(Xt[0]) == 26 and len(Xt[1]) == 70 # Nothing excluded Nt = 100 nvars = 5 X = [np.random.rand(Nt, nvars)] y = [np.concatenate([np.full(50, 1), np.full(50, 2)])] rle = TargetRunLengthEncoder(min_length=5) rle.fit(X, y) Xt, yt, _ = rle.transform(X, y) assert len(Xt) == len(yt) and len(yt) == 2 assert np.all(np.concatenate(Xt) == X) assert yt[0] == 1 and yt[1] == 2 assert len(Xt[0]) == 50 and len(Xt[1]) == 50 # Univariate data with sample weight and context Nt = 100 Xts = [np.random.rand(Nt)] Xc = [5] X = TS_Data(Xts, Xc) y = [ np.concatenate( [np.full(3, 1), np.full(26, 2), np.full(1, 3), np.full(70, 4)]) ] sw = [1] rle = TargetRunLengthEncoder(min_length=5) rle.fit(X) Xt, yt, swt = rle.transform(X, y, sw) Xtc = Xt.context_data assert len(Xt) == len(yt) and len(swt) == len(yt) and len(yt) == 2 assert yt[0] == 2 and yt[1] == 4 assert len(Xt[0]) == 26 and len(Xt[1]) == 70 assert swt[0] == 1 and swt[1] == 1 assert Xtc[0] == 5 and Xtc[1] == 5 X = pd.DataFrame(Xc) X['ts_data'] = Xts X = TS_Data.from_df(X) rle = TargetRunLengthEncoder(min_length=5) rle.fit(X) Xt, yt, swt = rle.transform(X, y, sw) Xtc = Xt.context_data assert len(Xt) == len(yt) and len(swt) == len(yt) and len(yt) == 2 assert yt[0] == 2 and yt[1] == 4 assert len(Xt[0]) == 26 and len(Xt[1]) == 70 assert swt[0] == 1 and swt[1] == 1 assert Xtc[0] == 5 and Xtc[1] == 5