Commit 3a8d202d authored by Philipp Arras's avatar Philipp Arras
Browse files

Add some testing

parent 6d5b914b
Pipeline #75220 passed with stages
in 8 minutes and 27 seconds
...@@ -108,7 +108,7 @@ class EnergyHistory(object): ...@@ -108,7 +108,7 @@ class EnergyHistory(object):
self._lst = [] self._lst = []
@property @property
def timestamps(self): def time_stamps(self):
return [x for x, _ in self._lst] return [x for x, _ in self._lst]
@property @property
...@@ -128,6 +128,9 @@ class EnergyHistory(object): ...@@ -128,6 +128,9 @@ class EnergyHistory(object):
self._lst += other._lst self._lst += other._lst
return self return self
def __len__(self):
return len(self._lst)
def append_history(func): def append_history(func):
@functools.wraps(func) @functools.wraps(func)
......
...@@ -288,7 +288,7 @@ def _plot_history(f, ax, **kwargs): ...@@ -288,7 +288,7 @@ def _plot_history(f, ax, **kwargs):
plt.yscale(kwargs.pop("yscale", "linear")) plt.yscale(kwargs.pop("yscale", "linear"))
mi, ma = np.inf, -np.inf mi, ma = np.inf, -np.inf
for i, fld in enumerate(f): for i, fld in enumerate(f):
xcoord = date2num([dt.fromtimestamp(ts) for ts in fld.timestamps]) xcoord = date2num([dt.fromtimestamp(ts) for ts in fld.time_stamps])
ycoord = fld.energy_values ycoord = fld.energy_values
ax.scatter(xcoord, ycoord, label=label[i], alpha=alpha[i], ax.scatter(xcoord, ycoord, label=label[i], alpha=alpha[i],
color=color[i], s=size[i]) color=color[i], s=size[i])
......
...@@ -39,7 +39,7 @@ def test_kl(constants, point_estimates, mirror_samples, mf): ...@@ -39,7 +39,7 @@ def test_kl(constants, point_estimates, mirror_samples, mf):
import numpy as np import numpy as np
lh = ift.GaussianEnergy(domain=op.target, sampling_dtype=np.float64) @ op lh = ift.GaussianEnergy(domain=op.target, sampling_dtype=np.float64) @ op
ic = ift.GradientNormController(iteration_limit=5) ic = ift.GradientNormController(iteration_limit=5)
ic.activate_and_reset_logging() ic.enable_logging()
h = ift.StandardHamiltonian(lh, ic_samp=ic) h = ift.StandardHamiltonian(lh, ic_samp=ic)
mean0 = ift.from_random('normal', h.domain) mean0 = ift.from_random('normal', h.domain)
...@@ -51,7 +51,14 @@ def test_kl(constants, point_estimates, mirror_samples, mf): ...@@ -51,7 +51,14 @@ def test_kl(constants, point_estimates, mirror_samples, mf):
point_estimates=point_estimates, point_estimates=point_estimates,
mirror_samples=mirror_samples, mirror_samples=mirror_samples,
napprox=0) napprox=0)
ic.pop_history() assert_(len(ic.history) > 0)
assert_(len(ic.history) == len(ic.history.time_stamps))
assert_(len(ic.history) == len(ic.history.energy_values))
ic.history.reset()
assert_(len(ic.history) == 0)
assert_(len(ic.history) == len(ic.history.time_stamps))
assert_(len(ic.history) == len(ic.history.energy_values))
locsamp = kl._local_samples locsamp = kl._local_samples
klpure = ift.MetricGaussianKL(mean0, klpure = ift.MetricGaussianKL(mean0,
h, h,
...@@ -87,8 +94,10 @@ def test_kl(constants, point_estimates, mirror_samples, mf): ...@@ -87,8 +94,10 @@ def test_kl(constants, point_estimates, mirror_samples, mf):
# Test constants (after some minimization) # Test constants (after some minimization)
cg = ift.GradientNormController(iteration_limit=5) cg = ift.GradientNormController(iteration_limit=5)
minimizer = ift.NewtonCG(cg) minimizer = ift.NewtonCG(cg, activate_logging=True)
kl, _ = minimizer(kl) kl, _ = minimizer(kl)
if len(constants) != 2:
assert_(len(minimizer.inversion_history) > 0)
diff = (mean0 - kl.position).to_dict() diff = (mean0 - kl.position).to_dict()
for kk in constants: for kk in constants:
assert_allclose(diff[kk].val, 0*diff[kk].val) assert_allclose(diff[kk].val, 0*diff[kk].val)
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment