Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DM-39639: Refactor linearity task, add Astier spline-based fit, and add full tests. #202

Merged
merged 9 commits into from Jun 26, 2023

Conversation

erykoff
Copy link
Contributor

@erykoff erykoff commented Jun 22, 2023

No description provided.

@erykoff erykoff requested a review from czwa June 22, 2023 20:10
@erykoff erykoff force-pushed the tickets/DM-39639 branch 2 times, most recently from 2e6e1ce to 58a7257 Compare June 22, 2023 21:57
Copy link
Contributor

@czwa czwa left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Mostly documentation questions, but I have a few questions interspersed. The new detailed tests are great.

python/lsst/cp/pipe/linearity.py Show resolved Hide resolved
to allow for different linearity coefficients with different
photodiode settings. The minimization is a least-squares
fit with the residual of
Sum[(S(mu_i) + mu_i)/(k_j * D_i) - 1]**2, where mu_i is the
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

S(mu_i) here is the spline model at mu_i, right? If not, then there's even more reason for it to be explicitly stated.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, a reference (to paper or code) for the original Astier model is good to add for the future.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So I can put in a link to the code, though I don't know how long it will live there.

Array of flat mean values.
mask : `np.ndarray` (M,), optional
Input mask (True is good point, False is bad point).
log : `blah`, optional
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

logging.Logger

self._pd = pd
self._mu = mu
self._grouping_values = grouping_values
self.log = log
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

self.log = log if log else logging.getLogger(__name__)? I see you check the existence of the logger below before writing to it, but it's easier to just have one.

# to allow for different linearity coefficients with different
# photodiode settings. The minimization is a least-squares
# fit with the residual of
# Sum[(S(mu_i) + mu_i)/(k_j * D_i) - 1]**2, where mu_i is the
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same comment as below (utils.py/AstierSplineLinearityFitter) about the docstring/comment. This doesn't need a reference, but defining S is still useful.

Returns
-------
ratio_models : `np.ndarray` (N,)
Model ratio, (mu_i + S(mu_i))/(k_j * D_i)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

mu_i - S(mu_i)?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great catch!

self.assertFloatsAlmostEqual(
linearizer.linearityCoeffs[amp_name][n_nodes:],
non_lin_spline_values,
atol=7.0,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this tolerance needed for all of the spline parameters? I'm less concerned about a 70/7 or 185/7 S/N, but a 1/7 would be concerning.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's not, it's just at the large ones. But the ratio comparison wasn't looking good. The proof is in the linearity of the residuals, but I wanted to add some sort of test here. If you think it's better to do different tolerances at low and high flux I can do that instead.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok. If my specific worry isn't justified, then this is fine with me.

ratio[0] = 1.0
p0[0: len(self._nodes)] = (ratio - 1) * self._nodes

# ratio_model3 = self.compute_ratio_model(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add a comment explaining why if this should be retained.


The fit has additional constraints to ensure that the spline
goes through the (0, 0) point, as well as a normalization
condition so that the average of the spline over the full
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you explain this normalization condition more? Is this trying to conserve charge? I ask because if we attempted to spline fit something with a simple squared correction, it seems like this would cause problems.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is from Pierre's code, and what I think it's basically doing is what I said: the total sum of the spline term should be zero, so that the linear part is picked up by the linear fit. I think this is basically the same thing that happens in the squared correction as well, as it's a squared deviation from linear, but the linear term is dropped in the application.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've added this comment:

    The fit has additional constraints to ensure that the spline
    goes through the (0, 0) point, as well as a normalization
    condition so that the average of the spline over the full
    range is 0. The normalization ensures that the spline only
    fits deviations from linearity, rather than the linear
    function itself which is degenerate with the gain.

@erykoff erykoff merged commit d3785e6 into main Jun 26, 2023
1 check passed
@erykoff erykoff deleted the tickets/DM-39639 branch June 26, 2023 21:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants