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-15241 log error on large chi2 #103

Merged
merged 3 commits into from Aug 8, 2018
Merged

DM-15241 log error on large chi2 #103

merged 3 commits into from Aug 8, 2018

Conversation

parejkoj
Copy link
Collaborator

No description provided.

Copy link

@mrawls mrawls left a comment

Choose a reason for hiding this comment

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

It generally looks fine, and most of my comments are asking for clarification in how the test works.


# log a message for a large final chi2, TODO: DM-15247 for something better
if chi2.chi2/chi2.ndof >= 4.0:
self.log.error("Potentially bad fit: High chi-squared/ndof.")
Copy link

Choose a reason for hiding this comment

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

Also print the value so the user can decide for themselves if the fit is bad or not? Would this be more appropriate as a warning or do you definitely want it to be an error?

Copy link
Collaborator Author

@parejkoj parejkoj Aug 7, 2018

Choose a reason for hiding this comment

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

The value itself is printed by the log.info line just above this, so no need to repeat it.

I think an error is correct for now: a high chi2 suggests there is something wrong with the fit. In the future, we'll probably want to have two different values, one with a warning and one with an error, but I'd rather be loud for now, while we're learning what the possible failure modes are.


class TestJointcalIterateFit(lsst.utils.tests.TestCase):
def setUp(self):
struct = lsst.jointcal.testUtils.createTwoFakeCcdImages(100, 100)
Copy link

Choose a reason for hiding this comment

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

This is a delightful name for this particular testUtil 💯

self.jointcal = lsst.jointcal.JointcalTask(config=self.config)

self.goodChi2 = lsst.jointcal.chi2.Chi2Statistic()
self.goodChi2.chi2 = 200.0
Copy link

Choose a reason for hiding this comment

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

Statistically speaking, shouldn't a "good" reduced chi2 be close to 1, not close to 2?

Copy link
Collaborator Author

@parejkoj parejkoj Aug 7, 2018

Choose a reason for hiding this comment

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

Yes, but most of the photometry fits are coming in around 2, likely because we don't have a color correction term, and possibly because the model is not complex enough. I'll add a comment though.

self.fitter.computeChi2.return_value = self.goodChi2
self.fitter.minimize.return_value = MinimizeResult.Converged
self.associations = unittest.mock.Mock(spec=lsst.jointcal.Associations)
self.associations.getCcdImageList.return_value = self.ccdImageList
Copy link

Choose a reason for hiding this comment

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

I think I've managed to follow what is happening here, but a couple of comments might help future users spend less time staring at it than I did. Something like "Create a mock photometry fitter and a mock astrometry fitter / Set the desired return values for a successful jointcal fit"

self.associations.getCcdImageList.return_value = self.ccdImageList

def test_iterateFit_success(self):
chi2 = self.jointcal._iterate_fit(self.associations, self.fitter, 20, "testing", "")
Copy link

Choose a reason for hiding this comment

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

For these and subsequent lines where you feed a function self.associations, self.fitter, and other stuff, can you include the arg names so it's clearer what things like "20" and "testing" and "random blank entry" are for?


chi2 = self.jointcal._iterate_fit(self.associations, self.fitter, 20, "testing", "")
self.assertEqual(chi2, self.badChi2)
log.error.assert_called_with("Potentially bad fit: High chi-squared/ndof.")
Copy link

Choose a reason for hiding this comment

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

Again, also log the value of chi2/ndof.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I've added a test that checks whether the such a log message is emitted.

self.fitter.minimize.return_value = MinimizeResult.Failed

with self.assertRaises(RuntimeError):
self.jointcal._iterate_fit(self.associations, self.fitter, 20, "testing", "")
Copy link

Choose a reason for hiding this comment

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

How does this bit work? You tell python to raise a RuntimeError and then do some stuff? And because it fails, the call_count is only equal to 1 rather than 2?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I set minimize.return_value to a value that should cause _iterate_fit to raise. If minimize fails, we raise immediately and there's no "final" minimize call (jointcal.py:782, if you care), so only one call to minimize.

chi2 = self.jointcal._iterate_fit(self.associations, self.fitter, maxSteps, "testing", "")
self.assertEqual(chi2, self.goodChi2)
self.assertEqual(self.fitter.minimize.call_count, maxSteps)
log.error.assert_called_with("testing failed to converge after 3 steps")
Copy link

Choose a reason for hiding this comment

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

Use maxSteps variable in the mock log message

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Good catch!

log.error.assert_called_with("Potentially bad fit: High chi-squared/ndof.")

def test_iterateFit_exceedMaxSteps(self):
log = unittest.mock.Mock(spec=lsst.log.Log)
Copy link

Choose a reason for hiding this comment

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

I understand why you make a mock jointcal associations and fitter above, but what purpose does a mock log serve? It doesn't look like you do much of anything with it.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Having the mock log lets me check whether the logger was called with particular values, instead of trying to either capture stdout and scan it for a particular log message, or interrogate the lsst.log object itself (and I'm not even sure that's possible).

@parejkoj parejkoj merged commit 02ced20 into master Aug 8, 2018
@ktlim ktlim deleted the tickets/DM-15241 branch August 25, 2018 06:44
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
2 participants