Repository: christianhbye/21cmVAE

Issue:
write tests

Discussion:

Patch:
diff --git a/VeryAccurateEmulator/emulator.py b/VeryAccurateEmulator/emulator.py
index 96452d6..f797d61 100644
--- a/VeryAccurateEmulator/emulator.py
+++ b/VeryAccurateEmulator/emulator.py
@@ -651,7 +651,7 @@ class AutoEncoderEmulator:
         )
 
         # build autoencoder by calling it on a batch of data
-        _ = autoencoder(pp.preproc(self.signal_test, self.signal_train))
+        autoencoder.build((None, self.signal_train.shape[-1]))
         self.autoencoder = autoencoder
 
         self.emulator = _gen_model(
@@ -823,7 +823,12 @@ class AutoEncoderEmulator:
 
         """
         if use_autoencoder:
-            pred = self.autoencoder(self.par_test)
+            pred = pp.unpreproc(
+                    self.autoencoder(
+                        pp.preproc(self.signal_test, self.signal_train)
+                    ),
+                    self.signal_train
+                    )
         else:
             pred = self.predict(self.par_test)
         err = error(


Test patch:
diff --git a/tests/test_emulator.py b/tests/test_emulator.py
index d31b075..6aba63c 100644
--- a/tests/test_emulator.py
+++ b/tests/test_emulator.py
@@ -45,3 +45,61 @@ def test_error():
     assert np.allclose(
         emulator.error(signal_train, signal_train), np.zeros(len(signal_train))
     )
+
+# direct emulator class
+direm = emulator.DirectEmulator()
+direm.load_model()
+def test_predict():
+    # some random parameters:
+    pars = direm.par_test[0]
+    pred = direm.predict(pars)
+    true = direm.signal_test[0]
+    assert pred.shape == true.shape
+    # the emulator has a max error of 1.84 %
+    assert np.sqrt(np.mean((pred-true)**2))/np.max(np.abs(true)) < 0.02
+
+    # vectorized call
+    pars = direm.par_test[:10]
+    pred_signals = direm.predict(pars)
+    assert pred_signals[0].shape == pred.shape
+    assert np.allclose(pred_signals[0], pred, atol=5e-5)
+    assert pred_signals.shape == (10, true.shape[0])
+    
+def test_test_error():
+    err = direm.test_error()
+    assert err.shape == (direm.signal_test.shape[0],)
+    # compare to table 1 in Bye et al. (2021)
+    assert np.allclose(err.mean(), 0.34, atol=1e-2)
+    assert np.allclose(np.median(err), 0.29, atol=1e-2)
+    err_mk = direm.test_error(relative=False)
+    assert np.allclose(err_mk.mean(), 0.54, atol=1e-2)
+    assert np.allclose(np.median(err_mk), 0.50, atol=1e-2)
+
+# autoencoder-based emulator class
+ae_em = emulator.AutoEncoderEmulator()
+ae_em.load_model()
+def test_predict_ae():
+    # some random parameters:
+    pars = ae_em.par_test[0]
+    pred = ae_em.predict(pars)
+    true = ae_em.signal_test[0]
+    assert pred.shape == true.shape
+    # error should be less than 5 % in all cases
+    assert np.sqrt(np.mean((pred-true)**2))/np.max(np.abs(true)) < 0.05
+
+    # vectorized call
+    pars = ae_em.par_test[:10]
+    pred_signals = ae_em.predict(pars)
+    assert pred_signals[0].shape == pred.shape
+    assert np.allclose(pred_signals[0], pred, atol=5e-5)
+    assert pred_signals.shape == (10, true.shape[0])
+    
+def test_test_error():
+    err = ae_em.test_error()
+    assert err.shape == (direm.signal_test.shape[0],)
+    # compare to appendix A in Bye et al. (2021)
+    assert np.allclose(err.mean(), 0.39, atol=1e-2)
+    assert np.allclose(np.median(err), 0.35, atol=1e-2)
+    err_ae = ae_em.test_error(use_autoencoder=True)
+    assert np.allclose(err_ae.mean(), 0.33, atol=1e-2)
+    assert np.allclose(np.median(err_ae), 0.29, atol=1e-2)