diff --git a/test/dataset/test_common.py b/test/dataset/test_common.py index bb46862..197e7af 100644 --- a/test/dataset/test_common.py +++ b/test/dataset/test_common.py @@ -1,5 +1,6 @@ from pts.dataset import FieldName + def test_dataset_fields(): assert ( "feat_static_cat" == FieldName.FEAT_STATIC_CAT diff --git a/test/dataset/test_process.py b/test/dataset/test_process.py index 7f43e33..fe41e5d 100644 --- a/test/dataset/test_process.py +++ b/test/dataset/test_process.py @@ -3,6 +3,7 @@ import pandas as pd from pts.dataset import ProcessStartField, ProcessDataEntry + @pytest.mark.parametrize( "freq, expected", [ diff --git a/test/feature/test_lag.py b/test/feature/test_lag.py index 7019150..bedf8d2 100644 --- a/test/feature/test_lag.py +++ b/test/feature/test_lag.py @@ -225,11 +225,9 @@ expected_lags = { + [28, 42] + [181, 182, 183, 363, 364, 365, 545, 546, 547], # centered around each of the last 3 months (delta = 0) + last 3 years (delta = 1) (assuming 52 weeks per year) - "6D": [1, 2, 3, 4, 5, 6, 7, 9, 14] - + [59, 60, 61, 120, 121, 122, 181, 182, 183], + "6D": [1, 2, 3, 4, 5, 6, 7, 9, 14] + [59, 60, 61, 120, 121, 122, 181, 182, 183], # centered around each of the last 3 months (delta = 0) + last 3 years (delta = 1) (assuming 52 weeks per year) - "W": [1, 2, 3, 4, 5, 6, 7, 8, 12] - + [51, 52, 53, 103, 104, 105, 155, 156, 157], + "W": [1, 2, 3, 4, 5, 6, 7, 8, 12] + [51, 52, 53, 103, 104, 105, 155, 156, 157], # centered around each of the last 3 months (delta = 0) + last 3 years (delta = 1) (assuming 52 weeks per year) "8D": [1, 2, 3, 4, 5, 6, 7, 10] + [44, 45, 46, 90, 91, 92, 135, 136, 137], # centered around each of the last 3 years (delta = 1) diff --git a/test/model/deepar/test_auxillary_outputs.py b/test/model/deepar/test_auxillary_outputs.py index 4d55a5b..e0ef100 100644 --- a/test/model/deepar/test_auxillary_outputs.py +++ b/test/model/deepar/test_auxillary_outputs.py @@ -51,7 +51,7 @@ def test_distribution(): transform=train_output.transformation, batch_size=batch_size, num_batches_per_epoch=estimator.trainer.num_batches_per_epoch, - device=torch.device("cpu") + device=torch.device("cpu"), ) seq_len = 2 * ds_info.prediction_length @@ -63,8 +63,4 @@ def test_distribution(): *[data_entry[k] for k in input_names] ) - assert distr.sample((num_samples,)).shape == ( - num_samples, - batch_size, - seq_len, - ) + assert distr.sample((num_samples,)).shape == (num_samples, batch_size, seq_len,) diff --git a/test/model/deepar/test_lags.py b/test/model/deepar/test_lags.py index 17d6362..afa987e 100644 --- a/test/model/deepar/test_lags.py +++ b/test/model/deepar/test_lags.py @@ -43,10 +43,7 @@ def test_lagged_subsequences(): # checks that lags value behave as described as in the get_lagged_subsequences contract for i, j, k in itertools.product(range(N), range(S), range(I)): assert ( - ( - lagged_subsequences[i, j, :, k] - == sequence[i, -lags[k] - S + j, :] - ) + (lagged_subsequences[i, j, :, k] == sequence[i, -lags[k] - S + j, :]) .numpy() .all() ) diff --git a/test/model/test_forecast.py b/test/model/test_forecast.py index c53a025..738bf45 100644 --- a/test/model/test_forecast.py +++ b/test/model/test_forecast.py @@ -38,9 +38,7 @@ FORECASTS = { forecast_keys=np.array(QUANTILES, str), freq=FREQ, ), - "SampleForecast": SampleForecast( - samples=SAMPLES, start_date=START_DATE, freq=FREQ - ), + "SampleForecast": SampleForecast(samples=SAMPLES, start_date=START_DATE, freq=FREQ), "DistributionForecast": DistributionForecast( distribution=Uniform(low=torch.zeros(1), high=torch.ones(1)), start_date=START_DATE, diff --git a/test/modules/test_feature.py b/test/modules/test_feature.py index 5417c71..e1e9ecb 100644 --- a/test/modules/test_feature.py +++ b/test/modules/test_feature.py @@ -8,45 +8,33 @@ from torch.distributions import Uniform from pts.modules import FeatureEmbedder, FeatureAssembler + @pytest.mark.parametrize( "config", ( lambda N, T: [ # single static feature - dict( - shape=(N, 1), - kwargs=dict(cardinalities=[50], embedding_dims=[10]), - ), + dict(shape=(N, 1), kwargs=dict(cardinalities=[50], embedding_dims=[10]),), # single dynamic feature - dict( - shape=(N, T, 1), - kwargs=dict(cardinalities=[2], embedding_dims=[10]), - ), + dict(shape=(N, T, 1), kwargs=dict(cardinalities=[2], embedding_dims=[10]),), # multiple static features dict( shape=(N, 4), kwargs=dict( - cardinalities=[50, 50, 50, 50], - embedding_dims=[10, 20, 30, 40], + cardinalities=[50, 50, 50, 50], embedding_dims=[10, 20, 30, 40], ), ), # multiple dynamic features dict( shape=(N, T, 3), - kwargs=dict( - cardinalities=[30, 30, 30], embedding_dims=[10, 20, 30] - ), + kwargs=dict(cardinalities=[30, 30, 30], embedding_dims=[10, 20, 30]), ), ] )(10, 20), ) def test_feature_embedder(config): - out_shape = config["shape"][:-1] + ( - sum(config["kwargs"]["embedding_dims"]), - ) - embed_feature = FeatureEmbedder( - **config["kwargs"] - ) + out_shape = config["shape"][:-1] + (sum(config["kwargs"]["embedding_dims"]),) + embed_feature = FeatureEmbedder(**config["kwargs"]) for embed in embed_feature._FeatureEmbedder__embedders: nn.init.constant_(embed.weight, 1.0) @@ -54,17 +42,18 @@ def test_feature_embedder(config): exp_params_len = len([p for p in embed_feature.parameters()]) act_params_len = len(config["kwargs"]["embedding_dims"]) assert exp_params_len == act_params_len - + def test_forward_pass(): act_output = embed_feature(torch.ones(config["shape"]).to(torch.long)) exp_output = torch.ones(out_shape) - + assert act_output.shape == exp_output.shape assert torch.abs(torch.sum(act_output - exp_output)) < 1e-20 test_parameters_length() test_forward_pass() + @pytest.mark.parametrize( "config", ( @@ -76,13 +65,9 @@ def test_feature_embedder(config): static_real=dict(C=5), dynamic_cat=dict(C=3), dynamic_real=dict(C=4), - embed_static=dict( - cardinalities=[2, 4], - embedding_dims=[3, 6], - ), + embed_static=dict(cardinalities=[2, 4], embedding_dims=[3, 6],), embed_dynamic=dict( - cardinalities=[30, 30, 30], - embedding_dims=[10, 20, 30], + cardinalities=[30, 30, 30], embedding_dims=[10, 20, 30], ), ) ] @@ -97,17 +82,15 @@ def test_feature_assembler(config): "dynamic_real", } feature_combs = chain.from_iterable( - combinations(feature_types, r) - for r in range(1, len(feature_types) + 1) + combinations(feature_types, r) for r in range(1, len(feature_types) + 1) ) # iterate over the power-set of all possible feature types, including the empty set embedder_types = {"embed_static", "embed_dynamic"} embedder_combs = chain.from_iterable( - combinations(embedder_types, r) - for r in range(0, len(embedder_types) + 1) + combinations(embedder_types, r) for r in range(0, len(embedder_types) + 1) ) - + for enabled_embedders in embedder_combs: embed_static = ( FeatureEmbedder(**config["embed_static"]) @@ -122,13 +105,10 @@ def test_feature_assembler(config): for enabled_features in feature_combs: assemble_feature = FeatureAssembler( - T=config["T"], - embed_static=embed_static, - embed_dynamic=embed_dynamic, + T=config["T"], embed_static=embed_static, embed_dynamic=embed_dynamic, ) # assemble_feature.collect_params().initialize(mx.initializer.One()) - def test_parameters_length(): exp_params_len = sum( [ @@ -166,11 +146,7 @@ def test_feature_assembler(config): ) out_features.append( torch.ones( - ( - N, - T, - sum(config["embed_static"]["embedding_dims"]), - ) + (N, T, sum(config["embed_static"]["embedding_dims"]),) ) ) else: # not embed_static and 'static_cat' in enabled_features @@ -197,11 +173,9 @@ def test_feature_assembler(config): out_features.append(torch.zeros((N, T, 1))) else: C = config["static_real"]["C"] - static_real = torch.empty((N,C)).uniform_(0,100) + static_real = torch.empty((N, C)).uniform_(0, 100) inp_features.append(static_real) - out_features.append( - static_real.unsqueeze(-2).expand(-1, T, -1) - ) + out_features.append(static_real.unsqueeze(-2).expand(-1, T, -1)) if "dynamic_cat" not in enabled_features: inp_features.append(torch.zeros((N, T, 1))) @@ -213,9 +187,7 @@ def test_feature_assembler(config): [ torch.randint( 0, - config["embed_dynamic"]["cardinalities"][ - c - ], + config["embed_dynamic"]["cardinalities"][c], (N, T, 1), ) for c in range(C) @@ -225,11 +197,7 @@ def test_feature_assembler(config): ) out_features.append( torch.ones( - ( - N, - T, - sum(config["embed_dynamic"]["embedding_dims"]), - ) + (N, T, sum(config["embed_dynamic"]["embedding_dims"]),) ) ) else: # not embed_dynamic and 'dynamic_cat' in enabled_features @@ -239,9 +207,7 @@ def test_feature_assembler(config): [ torch.randint( 0, - config["embed_dynamic"]["cardinalities"][ - c - ], + config["embed_dynamic"]["cardinalities"][c], (N, T, 1), ) for c in range(C) @@ -256,7 +222,7 @@ def test_feature_assembler(config): out_features.append(torch.zeros((N, T, 1))) else: C = config["dynamic_real"]["C"] - dynamic_real = torch.empty((N, T, C)).uniform_(0,100) + dynamic_real = torch.empty((N, T, C)).uniform_(0, 100) inp_features.append(dynamic_real) out_features.append(dynamic_real) diff --git a/test/test_transform.py b/test/test_transform.py index b3a8039..d5cdae0 100644 --- a/test/test_transform.py +++ b/test/test_transform.py @@ -805,4 +805,3 @@ def assert_padded_array( f"Sampled and reference arrays do not match. '" f"Got {sampled_no_padding} but should be {reference_no_padding}." ) -