def test_segmentxy(): # test illegal parameter settings with pytest.raises(ValueError): transform.SegmentXY(width=0) # illegal width value with pytest.raises(ValueError): transform.SegmentXY( overlap=None, step=None) # either overlap or step must be defined with pytest.raises(ValueError): transform.SegmentXY(overlap=-1, step=None) # illegal overlap value with pytest.raises(ValueError): transform.SegmentXY(step=0) # illegal step value with pytest.raises(ValueError): transform.SegmentXY(order=None) # illegal order # test _step property working as expected seg = transform.SegmentXY(width=10, overlap=0.5) assert seg._step == 5 # test precedence of step over overlap seg = transform.SegmentXY(width=10, overlap=1, step=1) assert seg._step == 1 # illegal overlap value, but valid step value seg = transform.SegmentXY(overlap=-1, step=1) assert seg._step == 1 # test shape of segmented data Nt = 100 width = 5 nvars = 5 seg = transform.SegmentXY(width=width) # multivariate ts data without context data X = [ np.random.rand(Nt, nvars), np.random.rand(Nt, nvars), np.random.rand(Nt, nvars) ] y = [np.random.rand(Nt), np.random.rand(Nt), np.random.rand(Nt)] seg.fit(X, y) Xs, ys, _ = seg.transform(X, y) N = len(ys) assert Xs.shape == (N, width, nvars) # univariate ts data without context data X = [np.random.rand(Nt), np.random.rand(2 * Nt), np.random.rand(3 * Nt)] y = [np.random.rand(Nt), np.random.rand(2 * Nt), np.random.rand(3 * Nt)] seg.fit(X, y) Xs, ys, _ = seg.transform(X, y) N = len(ys) assert Xs.shape == (N, width) # multivariate ts data with context data Xt = [ np.random.rand(Nt, nvars), np.random.rand(2 * Nt, nvars), np.random.rand(Nt, nvars) ] Xc = np.random.rand(3, 4) y = [np.random.rand(Nt), np.random.rand(2 * Nt), np.random.rand(Nt)] X = TS_Data(Xt, Xc) seg.fit(X, y) Xs, ys, _ = seg.transform(X, y) Xst, Xsc = get_ts_data_parts(Xs) N = len(ys) assert Xst.shape == (N, width, nvars) assert Xsc.shape == (N, 4) # ts data with univariate context data Xt = [ np.random.rand(Nt, nvars), np.random.rand(2 * Nt, nvars), np.random.rand(Nt, nvars) ] Xc = np.random.rand(3) y = [np.random.rand(Nt), np.random.rand(2 * Nt), np.random.rand(Nt)] X = TS_Data(Xt, Xc) seg.fit(X, y) Xs, ys, _ = seg.transform(X, y) Xst, Xsc = get_ts_data_parts(Xs) N = len(ys) assert Xst.shape == (N, width, nvars) assert Xsc.shape == (N, 1) # same number as context vars and time vars # this would cause broadcasting failure before implementation of TS_Data class Xt = [ np.random.rand(Nt, nvars), np.random.rand(2 * Nt, nvars), np.random.rand(Nt, nvars) ] Xc = np.random.rand(3, nvars) y = [np.random.rand(Nt), np.random.rand(2 * Nt), np.random.rand(Nt)] X = TS_Data(Xt, Xc) seg.fit(X, y) Xs, ys, _ = seg.transform(X, y) Xst, Xsc = get_ts_data_parts(Xs) N = len(ys) assert Xst.shape == (N, width, nvars) assert Xsc.shape == (N, 5)
def test_segmentxy(): Nt = 100 width = 5 nvars = 5 seg = transform.SegmentXY(width=width) # multivariate ts data without context data X = [ np.random.rand(Nt, nvars), np.random.rand(Nt, nvars), np.random.rand(Nt, nvars) ] y = [np.random.rand(Nt), np.random.rand(Nt), np.random.rand(Nt)] seg.fit(X, y) Xs, ys, _ = seg.transform(X, y) N = len(ys) assert Xs.shape == (N, width, nvars) # univariate ts data without context data X = [np.random.rand(Nt), np.random.rand(2 * Nt), np.random.rand(3 * Nt)] y = [np.random.rand(Nt), np.random.rand(2 * Nt), np.random.rand(3 * Nt)] seg.fit(X, y) Xs, ys, _ = seg.transform(X, y) N = len(ys) assert Xs.shape == (N, width) # multivariate ts data with context data Xt = [ np.random.rand(Nt, nvars), np.random.rand(2 * Nt, nvars), np.random.rand(Nt, nvars) ] Xc = np.random.rand(3, 4) y = [np.random.rand(Nt), np.random.rand(2 * Nt), np.random.rand(Nt)] X = TS_Data(Xt, Xc) seg.fit(X, y) Xs, ys, _ = seg.transform(X, y) Xst, Xsc = get_ts_data_parts(Xs) N = len(ys) assert Xst.shape == (N, width, nvars) assert Xsc.shape == (N, 4) # ts data with univariate context data Xt = [ np.random.rand(Nt, nvars), np.random.rand(2 * Nt, nvars), np.random.rand(Nt, nvars) ] Xc = np.random.rand(3) y = [np.random.rand(Nt), np.random.rand(2 * Nt), np.random.rand(Nt)] X = TS_Data(Xt, Xc) seg.fit(X, y) Xs, ys, _ = seg.transform(X, y) Xst, Xsc = get_ts_data_parts(Xs) N = len(ys) assert Xst.shape == (N, width, nvars) assert Xsc.shape == (N, 1) # same number as context vars and time vars # this would cause broadcasting failure before implementation of TS_Data class Xt = [ np.random.rand(Nt, nvars), np.random.rand(2 * Nt, nvars), np.random.rand(Nt, nvars) ] Xc = np.random.rand(3, nvars) y = [np.random.rand(Nt), np.random.rand(2 * Nt), np.random.rand(Nt)] X = TS_Data(Xt, Xc) seg.fit(X, y) Xs, ys, _ = seg.transform(X, y) Xst, Xsc = get_ts_data_parts(Xs) N = len(ys) assert Xst.shape == (N, width, nvars) assert Xsc.shape == (N, 5)