|
| 1 | +import keras |
| 2 | +import numpy as np |
| 3 | +import io |
| 4 | +from contextlib import redirect_stdout |
| 5 | + |
| 6 | +from tests.utils import assert_models_equal |
| 7 | + |
| 8 | + |
| 9 | +def test_build(approximator, train_dataset): |
| 10 | + assert approximator.built is False |
| 11 | + |
| 12 | + data_shapes = keras.tree.map_structure(keras.ops.shape, train_dataset[0]) |
| 13 | + approximator.build(data_shapes) |
| 14 | + |
| 15 | + assert approximator.built is True |
| 16 | + assert approximator.classifier_network.built is True |
| 17 | + if approximator.summary_network is not None: |
| 18 | + assert approximator.summary_network.built is True |
| 19 | + |
| 20 | + |
| 21 | +def test_build_adapter(): |
| 22 | + from bayesflow.approximators import ModelComparisonApproximator |
| 23 | + |
| 24 | + _ = ModelComparisonApproximator.build_adapter( |
| 25 | + num_models=2, |
| 26 | + classifier_conditions=["foo", "bar"], |
| 27 | + summary_variables=["observables"], |
| 28 | + model_index_name=["indices"], |
| 29 | + ) |
| 30 | + |
| 31 | + |
| 32 | +def test_build_dataset(simulator, adapter): |
| 33 | + from bayesflow.approximators import ModelComparisonApproximator |
| 34 | + from bayesflow.datasets import OnlineDataset |
| 35 | + |
| 36 | + dataset = ModelComparisonApproximator.build_dataset( |
| 37 | + simulator=simulator, |
| 38 | + memory_budget="20 KiB", |
| 39 | + num_batches=2, |
| 40 | + num_models=2, |
| 41 | + classifier_conditions="foo", |
| 42 | + summary_variables=["x1", "x2"], |
| 43 | + ) |
| 44 | + assert isinstance(dataset, OnlineDataset) |
| 45 | + |
| 46 | + |
| 47 | +def test_fit(approximator, train_dataset, validation_dataset): |
| 48 | + approximator.compile(optimizer="AdamW") |
| 49 | + num_epochs = 1 |
| 50 | + |
| 51 | + # Capture ostream and train model |
| 52 | + with io.StringIO() as stream: |
| 53 | + with redirect_stdout(stream): |
| 54 | + approximator.fit(dataset=train_dataset, validation_data=validation_dataset, epochs=num_epochs) |
| 55 | + |
| 56 | + output = stream.getvalue() |
| 57 | + # check that the loss is shown |
| 58 | + assert "loss" in output |
| 59 | + |
| 60 | + |
| 61 | +def test_save_and_load(tmp_path, approximator, train_dataset, validation_dataset): |
| 62 | + # to save, the model must be built |
| 63 | + data_shapes = keras.tree.map_structure(keras.ops.shape, train_dataset[0]) |
| 64 | + approximator.build(data_shapes) |
| 65 | + approximator.compute_metrics(**train_dataset[0]) |
| 66 | + |
| 67 | + keras.saving.save_model(approximator, tmp_path / "model.keras") |
| 68 | + loaded = keras.saving.load_model(tmp_path / "model.keras") |
| 69 | + |
| 70 | + assert_models_equal(approximator, loaded) |
| 71 | + |
| 72 | + |
| 73 | +def test_predict(approximator, train_dataset, simulator): |
| 74 | + data_shapes = keras.tree.map_structure(keras.ops.shape, train_dataset[0]) |
| 75 | + approximator.build(data_shapes) |
| 76 | + approximator.compute_metrics(**train_dataset[0]) |
| 77 | + |
| 78 | + num_conditions = 2 |
| 79 | + conditions = simulator.sample(num_conditions) |
| 80 | + output = approximator.predict(conditions=conditions) |
| 81 | + assert isinstance(output, np.ndarray) |
| 82 | + assert output.shape[0] == num_conditions |
0 commit comments