Skip to content

Commit

Permalink
Add spline chi-squared calculation.
Browse files Browse the repository at this point in the history
  • Loading branch information
erykoff committed Apr 19, 2024
1 parent 41d53da commit d7494a3
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 1 deletion.
4 changes: 3 additions & 1 deletion python/lsst/cp/pipe/linearity.py
Original file line number Diff line number Diff line change
Expand Up @@ -577,6 +577,8 @@ def run(self, inputPtc, dummy, camera, inputDims,
"be consistent with zero.")
pars[0] = 0.0

linearityChisq = fitter.compute_chisq_dof(pars)

linearityCoeffs = np.concatenate([nodes, pars[fitter.par_indices["values"]]])
linearFit = np.array([0.0, np.mean(pars[fitter.par_indices["groups"]])])

Expand Down Expand Up @@ -608,7 +610,7 @@ def run(self, inputPtc, dummy, camera, inputDims,
pars[fitter.par_indices["temperature_coeff"]],
))
polyFitErr = np.zeros_like(polyFit)
chiSq = np.nan
chiSq = linearityChisq

# Update mask based on what the fitter rejected.
mask = fitter.mask
Expand Down
25 changes: 25 additions & 0 deletions python/lsst/cp/pipe/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -1242,6 +1242,31 @@ def mask(self):
def good_points(self):
return self.mask.nonzero()[0]

def compute_chisq_dof(self, pars):
"""Compute the chi-squared per degree of freedom for a set of pars.
Parameters
----------
pars : `np.ndarray`
Parameter array.
Returns
-------
chisq_dof : `float`
Chi-squared per degree of freedom.
"""
resids = self(pars)[0: len(self.mask)]
chisq = np.sum(resids[self.mask]**2.)
dof = self.mask.sum() - self.ngroup
if self._fit_temperature:
dof -= 1
if self._fit_offset:
dof -= 1
if self._fit_weights:
dof -= 2

return chisq/dof

def __call__(self, pars):

ratio_model, spl = self.compute_ratio_model(
Expand Down
3 changes: 3 additions & 0 deletions tests/test_linearity.py
Original file line number Diff line number Diff line change
Expand Up @@ -399,6 +399,9 @@ def _check_linearity_spline(
atol=resid_atol,
)

# Loose check on the chi-squared.
self.assertLess(linearizer.fitChiSq[amp_name], 2.0)

# If we apply the linearity correction, we should get the true
# linear values out.
image = lsst.afw.image.ImageF(len(mu_values), 1)
Expand Down

0 comments on commit d7494a3

Please sign in to comment.