Commit f40d7d51 authored by Philipp Arras's avatar Philipp Arras
Browse files

Generalize minisanity

parent ecd73d29
Pipeline #88119 passed with stages
in 11 minutes and 24 seconds
......@@ -126,7 +126,8 @@ def main():
KL = ift.MetricGaussianKL.make(mean, H, N_samples, True)
KL, convergence = minimizer(KL)
mean = KL.position
ift.extra.minisanity(KL, data, sig.inverse, signal_response)
ift.extra.minisanity(data, lambda x: N.inverse, signal_response,
KL.position, list(KL.samples))
# Plot current reconstruction
plot = ift.Plot()
......@@ -22,7 +22,6 @@ import numpy as np
from .domain_tuple import DomainTuple
from .field import Field
from .linearization import Linearization
from import Energy
from .multi_domain import MultiDomain
from .multi_field import MultiField
from .operators.adder import Adder
......@@ -31,7 +30,7 @@ from .operators.linear_operator import LinearOperator
from .operators.operator import Operator
from .operators.scaling_operator import ScalingOperator
from .probing import StatCalculator
from .sugar import from_random, full, is_fieldlike, is_operator
from .sugar import from_random, full, is_endo, is_fieldlike, is_operator
from .utilities import myassert
__all__ = ["check_linear_operator", "check_operator", "assert_allclose", "minisanity"]
......@@ -395,7 +394,7 @@ def _jac_vs_finite_differences(op, loc, tol, ntries, only_r_differentiable):
atol=tol**2, rtol=tol**2)
def minisanity(energy, data, sqrtmetric, modeldata_operator):
def minisanity(data, metric_at_pos, modeldata_operator, mean, samples=None):
"""Log information about the current fit quality and prior compatibility.
Log a table with fitting information for the likelihood and the prior.
......@@ -416,36 +415,46 @@ def minisanity(energy, data, sqrtmetric, modeldata_operator):
energy : Energy
Energy object which contains current mean and potentially samples.
data : Field or MultiField
Data which is subtracted from the output of `model_data`.
sqrtmetric : LinearOperator
Linear operator which applies the inverse of the square root of the
noise covariance.
metric_at_pos : function
Function which takes a `Field` or `MultiField` in the domain of `mean`
and returns an endomorphic operator which applies the inverse of the
noise covariance in the domain of `data`.
model_data : Operator
Operator which generates
Operator which generates model data.
mean : Field or MultiField
Mean of input of `model_data`.
samples : list or tuple of Field or MultiField, optional
Residual samples around `mean`. Default: no samples.
# FIXME @mtr probably we need MPI support here, right?
For computing the reduced chi^2 values and the normalized residuals, the
metric at `mean` is used.
from .logger import logger
if not (
isinstance(energy, Energy)
and isinstance(sqrtmetric, LinearOperator)
and is_operator(modeldata_operator)
and is_fieldlike(data)
and is_fieldlike(mean)
and isinstance(samples, (list, tuple))
raise TypeError
normresi = sqrtmetric @ Adder(data, neg=True) @ modeldata_operator
keylen = 18
for dom in [, energy.position.domain]:
for dom in [data.domain, mean.domain]:
if isinstance(dom, MultiDomain):
keylen = max([max(map(len, dom.keys())), keylen])
keylen = min([keylen, 42])
s0 = _comp_chisq(normresi, energy, keylen)
s1 = _comp_chisq(ScalingOperator(energy.position.domain, 1), energy, keylen)
from .logger import logger
normresi = metric_at_pos(mean).get_sqrt() @ Adder(data, neg=True) @ modeldata_operator
s0 = _comp_chisq(normresi, mean, samples, keylen)
s1 = _comp_chisq(ScalingOperator(mean.domain, 1), mean, samples, keylen)
f =
n = 38 + keylen
......@@ -469,10 +478,8 @@ class _bcolors:
BOLD = "\033[1m"
def _comp_chisq(op, energy, keylen):
p = energy.position
hass = hasattr(energy, "samples")
s = energy.samples if hass else [full(energy.domain, 0.0)]
def _comp_chisq(op, p, samples, keylen):
s = [full(p.domain, 0.0)] if samples is None else samples
mf = isinstance(, MultiDomain)
if not mf:
op = op.ducktape_left("<None>")
Markdown is supported
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