def test_forecast_parser(): # verify that logged for estimator, datasets and metrics can be recovered # from their string representation dataset_info, train_ds, test_ds = constant_dataset() estimator = make_estimator(dataset_info.metadata.freq, dataset_info.prediction_length) assert repr(estimator) == repr(load_code(repr(estimator))) predictor = estimator.train(training_data=train_ds) stats = calculate_dataset_statistics(train_ds) assert stats == eval(repr(stats), globals(), {"gluonts": gluonts}) # TODO: use load evaluator = Evaluator(quantiles=[0.1, 0.5, 0.9]) agg_metrics, _ = backtest_metrics(test_ds, predictor, evaluator) # reset infinite metrics to 0 (otherwise the assertion below fails) for key, val in agg_metrics.items(): if not math.isfinite(val): agg_metrics[key] = 0.0 assert agg_metrics == load_code(dump_code(agg_metrics))
def test_dynamic_loading(): code = dedent(""" dict( trainer=gluonts.trainer.Trainer( ctx="cpu(0)", epochs=5, learning_rate=0.001, clip_gradient=10.0, weight_decay=1e-08, patience=5, batch_size=32, num_batches_per_epoch=10, hybridize=False, ), num_hidden_dimensions=[3], context_length=5, prediction_length=2, freq="1H", distr_output=gluonts.distribution.StudentTOutput(), batch_normalization=False, mean_scaling=True ) """) load_code(code)
def make_from_log_contents(log_contents): regexp = re.compile(r"gluonts\[(.*)\]: (.*)") # in case some metric is nan messages = dict(regexp.findall(log_contents)) # avoid to fail if a key is missing for instance in the case a run did # not finish so that we can still get partial information try: return BacktestInformation( train_dataset_stats=eval( messages[train_dataset_stats_key] ), # TODO: use load test_dataset_stats=eval( messages[test_dataset_stats_key] ), # TODO: use load estimator=load_code(messages[estimator_key]), agg_metrics={ k: load_code(v) for k, v in messages.items() if k.startswith("metric-") }, ) except Exception as e: logging.info(str(e)) return None
def test_call_and_repr(func) -> None: global_state = {} x = evaluate(BASE_RECIPE, length=10, global_state=global_state) kwargs = dict(foo=42, bar=23) np.random.seed(0) ret = func( x, field_name="bar", length=10, global_state=global_state.copy(), **kwargs, ) func_reconstructed = load_code(dump_code(func)) np.random.seed(0) ret2 = func_reconstructed( x, field_name="foo", length=10, global_state=global_state.copy(), **kwargs, ) print(ret) np.testing.assert_allclose(ret2, ret)
def assert_serializable(x: transform.Transformation): t = fqname_for(x.__class__) y = load_json(dump_json(x)) z = load_code(dump_code(x)) assert dump_json(x) == dump_json( y), f"Code serialization for transformer {t} does not work" assert dump_code(x) == dump_code( z), f"JSON serialization for transformer {t} does not work"
def test_component_ctor(): random.seed(5_432_671_244) A = 100 B = 200 C = 300 x_list = [ Foo( str(random.randint(0, A)), Complex(x=random.uniform(0, C), y=str(random.uniform(0, C))), b=random.uniform(0, B), ) for i in range(4) ] fields = [ Foo( a=str(random.randint(0, A)), b=random.uniform(0, B), c=Complex(x=str(random.uniform(0, C)), y=random.uniform(0, C)), ) for i in range(5) ] x_dict = { i: Foo( b=random.uniform(0, B), a=str(random.randint(0, A)), c=Complex(x=str(random.uniform(0, C)), y=str(random.uniform(0, C))), ) for i in range(6) } bar01 = Bar(x_list, fields=fields, x_dict=x_dict) bar02 = load_code(dump_code(bar01)) bar03 = load_json(dump_json(bar02)) def compare_tpes(x, y, z, tpe): assert tpe == type(x) == type(y) == type(z) def compare_vals(x, y, z): assert x == y == z compare_tpes(bar02.x_list, bar02.x_list, bar03.x_list, tpe=list) compare_tpes(bar02.x_dict, bar02.x_dict, bar03.x_dict, tpe=dict) compare_tpes(bar02.fields, bar02.fields, bar03.fields, tpe=list) compare_vals(len(bar02.x_list), len(bar02.x_list), len(bar03.x_list)) compare_vals(len(bar02.x_dict), len(bar02.x_dict), len(bar03.x_dict)) compare_vals(len(bar02.fields), len(bar02.fields), len(bar03.fields)) compare_vals(bar02.x_list, bar02.x_list, bar03.x_list) compare_vals(bar02.x_dict, bar02.x_dict, bar03.x_dict) compare_vals(bar02.fields, bar02.fields, bar03.fields) baz01 = Baz(a="0", b="9", c=Complex(x="1", y="2"), d="42") baz02 = load_json(dump_json(baz01)) assert type(baz01) == type(baz02) assert baz01 == baz02
def test_repr(Estimator, hyperparameters): estimator = Estimator.from_hyperparameters(freq=freq, **hyperparameters) assert repr(estimator) == repr(load_code(repr(estimator)))
def test_code_serialization(e) -> None: expected, actual = e, serde.load_code(serde.dump_code(e)) assert check_equality(expected, actual)
[ mx.nd.random.uniform(shape=(3, 5, 2), dtype="float16"), mx.nd.random.uniform(shape=(3, 5, 2), dtype="float32"), mx.nd.random.uniform(shape=(3, 5, 2), dtype="float64"), mx.nd.array([[1, 2, 3], [-1, -2, 0]], dtype=np.uint8), mx.nd.array([[1, 2, 3], [-1, -2, 0]], dtype=np.int32), mx.nd.array([[1, 2, 3], [-1, -2, 0]], dtype=np.int64), mx.nd.array([[1, 2, 3], [1, 2, 0]], dtype=np.uint8), ], ) @pytest.mark.parametrize( "serialize_fn", [ lambda x: serde.load_json(serde.dump_json(x)), lambda x: serde.load_binary(serde.dump_binary(x)), lambda x: serde.load_code(serde.dump_code(x)), ], ) def test_ndarray_serialization(a, serialize_fn) -> None: b = serialize_fn(a) assert type(a) == type(b) assert a.dtype == b.dtype assert a.shape == b.shape assert np.all((a == b).asnumpy()) def test_timestamp_encode_decode() -> None: now = pd.Timestamp.now() assert now == serde.decode(serde.encode(now))
def test_code_serialization(e) -> None: assert e == serde.load_code(serde.dump_code(e))
def test_repr(Estimator, hyperparameters): estimator = from_hyperparameters(Estimator, hyperparameters, dsinfo) assert repr(estimator) == repr(load_code(repr(estimator)))