Skip to content

Fixes for weights with linear fitter - #10687

Merged
pllim merged 15 commits into
astropy:masterfrom
saimn:linear-fitter-weights
Oct 30, 2020
Merged

pllim merged 15 commits into
astropy:masterfrom
saimn:linear-fitter-weights

Conversation

@saimn

@saimn saimn commented Aug 28, 2020

Copy link
Copy Markdown
Contributor

Fix #7035.
I still find the modeling code quite confusing between the data axes, model sets, parameters, so I hope that I did it right for all cases (and tests seem to show that it works).

@saimn saimn added the modeling label Aug 28, 2020
@saimn saimn added this to the v4.0.2 milestone Aug 28, 2020
@saimn
saimn requested review from jehturner and nden August 28, 2020 15:39
@saimn
saimn force-pushed the linear-fitter-weights branch 2 times, most recently from 103f69d to cb304ef Compare August 28, 2020 15:42
@pllim pllim added the Bug label Aug 28, 2020
@nden
nden requested a review from perrygreenfield August 29, 2020 16:55
@jehturner

Copy link
Copy Markdown
Member

Thanks, @saimn! I may be wrong, but I think this can be simplified significantly. Let me check...

@jehturner

Copy link
Copy Markdown
Member

No, that idea was just a misunderstanding of how you've done it and why -- but could we maybe combine the separate loops for masked & weights? I could work on that if you want (I also had one or two remaining uncertainties about the 2D weights behaviour that I could check at the same time).

I think it's good to have several tests where the weights aren't all 1, to make sure the results are correct and not just that the code runs. I see you do have one test that uses real weights for a single 2D model and also one for a 1D model set, which covers the main cases of looping & not looping. I wonder about adding some real weights for a 2D model set, to make sure the algebra is still correct in that case, but this is already pretty good and it's great that you're covering all these cases.

@saimn

saimn commented Sep 3, 2020

Copy link
Copy Markdown
Contributor Author

could we maybe combine the separate loops for masked & weights? I could work on that if you want

Sure, another pair of eyes would be useful ;).

I think it's good to have several tests where the weights aren't all 1

Indeed. I needed tests to cover quickly all the different kind of inputs, so I basically copied the existing tests and added weights, but using something else than 1 requires to spend more time understanding each test case. We should certainly do that, if the algorithmic part is on the good path.

Comment thread astropy/modeling/fitting.py Resolved

@jehturner jehturner left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I haven't actually finished reviewing this but I'll do "finish your review" because I think you won't see my couple of line comments otherwise? How do you want any changes I make? I would think a PR on your PR (like Curtis's) is best, since they'll probably be too complicated for github suggestions. At least this should be easier now we can check further changes against your tests...

Comment thread astropy/modeling/fitting.py Resolved
Comment thread astropy/modeling/fitting.py Resolved
@saimn

saimn commented Sep 3, 2020

Copy link
Copy Markdown
Contributor Author

@jehturner - Yes, a PR on my PR would work 👍

@pllim

pllim commented Sep 3, 2020

Copy link
Copy Markdown
Member

PR on my PR

Random thought: One of these days, we need to see how many PR to PR we can chain for a single PR. 😄

@jehturner

Copy link
Copy Markdown
Member

Maybe we could try a circular one?

Comment thread astropy/modeling/fitting.py Resolved
Comment thread astropy/modeling/tests/test_fitters.py Outdated
def test_linear_fit_2d_model_set_common_weight(self):
init_model = models.Polynomial2D(degree=2, c1_0=[1, 2], c0_1=[-0.5, 1],
n_models=2,
fixed={'c1_0': True, 'c0_1': True})

@jehturner jehturner Sep 5, 2020 •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Just wondering whether there's any special reason for fixing parameters here and in the other test?

@nden

nden commented Sep 8, 2020

Copy link
Copy Markdown
Contributor

Is this ready for review?
@jehturner You are most familiar with this code, I'll wait for your approval before I review it.

Comment thread astropy/modeling/fitting.py Resolved
@jehturner

jehturner commented Sep 8, 2020 •

Copy link
Copy Markdown
Member

I think the code is correct now, @nden, except that I was still checking what happens with scl. It would be good if the separate loops for weights.ndim > 1 and len(model_copy) > 1 and masked could be consolidated in a reasonably efficient way though; I was hoping to look at that last week but didn't get to it. I think @saimn and I are both out next week and am not sure if one of us will get to it before then (or convince ourselves it would be too messy).

Comment thread astropy/modeling/fitting.py Resolved
@saimn
saimn force-pushed the linear-fitter-weights branch from add97d5 to 8a91e84 Compare September 11, 2020 10:36
@saimn

saimn commented Sep 11, 2020

Copy link
Copy Markdown
Contributor Author

@nden @perrygreenfield - This is now ready for review, thanks to @jehturner for addressing the remaining items :)

@pllim pllim left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Any plans to address codecov comment about certain LOCs not covered? If it is not covered on purpose, can add # pragma: no cover directive too.

Need another rebase for the change log conflict.

p.s. When this is merge, the weight is off your shoulder! 😉

Comment thread astropy/modeling/tests/test_fitters.py Outdated
assert_allclose(model.c1_0, -0.1, atol=1e-12)
assert_allclose(model.c0_1, 0.2, atol=1e-12)

# # The following 2 cases also happen to work, but model set fitting is

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Why not use pytest.mark.skip?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Not sure if we need to keep those tests, @jehturner ?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I don't think we particularly need to keep them, but if they're there they could just be uncommented when those fitters support model sets. They do also work now, but I'm not sure it's reasonable to require those cases to continue working as long as the fitters aren't advertised to support model sets (so there's no reason for there to be an extra dimension).

@saimn

saimn commented Oct 8, 2020

Copy link
Copy Markdown
Contributor Author

We (with @jehturner) are working on integrating this in our fitting code, it works well, and I just found a little issue when weights for one of the models are all 0 and the data is not a masked array. In that case the lapack routine in Numpy will crash with a rather obscure exception (ValueError: On entry to DLASCL parameter number 4 had an illegal value) so I added a check for this to raise a better exception. If the data is a masked array there will be a RuntimeWarning: invalid value encountered in true_divide but the code does not crash and just return a model with NaN parameters.

ping @nden @perrygreenfield - please review when you can :)

@astrofrog astrofrog removed this from the v4.0.2 milestone Oct 10, 2020
@saimn
saimn force-pushed the linear-fitter-weights branch from 62774a0 to 0c2c831 Compare October 26, 2020 16:08
@saimn

saimn commented Oct 26, 2020

Copy link
Copy Markdown
Contributor Author

@nden - I moved most of the model set tests to test_model_sets.py, but there were already some model set tests in test_fitting.py, and for some of the new model set tests we use the fixtures/class from test_fitting.py.
I also squashed some commits (e.g. changelog related), but I'm not sure what you want me to do here ? Do you want a single commit for this whole blob of changes ? IMO some of them deserve their own commit to be understandable (if one has to browse the history in the future) and some have comments that could be helpful as well...

@saimn

saimn commented Oct 29, 2020

Copy link
Copy Markdown
Contributor Author

Ping @nden, would be super useful for us to have this in the next bugfix releases (i.e. 4.1.1).

@nden

nden commented Oct 30, 2020

Copy link
Copy Markdown
Contributor

@pllim Is readthedocs failure a known issue, they seem unrelated? Please feel free to merge of so.

@pllim pllim left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

RTD might be online again but given the diff, it is not critical for merging even if server still down.

assert_allclose(model.c1_0, -0.1, atol=1e-12)
assert_allclose(model.c0_1, 0.2, atol=1e-12)

# # The following 2 cases also happen to work, but model set fitting is

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I am not sure about this one. Can we use pytest.mark.skip instead?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I talked with @jehturner and I think it's better to remove them, since this is not a supported feature. If we can keep them in the history, maybe at some point we can find them again if it's useful.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I pushed a commit to remove these tests, and skipped CI since this is removing only commented lines.

@pllim

pllim commented Oct 30, 2020

Copy link
Copy Markdown
Member

RTD warnings are real but unrelated. Merging then. Thanks, everyone!

@pllim
pllim merged commit 3bae6fa into astropy:master Oct 30, 2020
@saimn
saimn deleted the linear-fitter-weights branch October 30, 2020 19:23
bsipocz pushed a commit that referenced this pull request Nov 6, 2020
Fixes for weights with linear fitter
bsipocz pushed a commit that referenced this pull request Nov 6, 2020
Fixes for weights with linear fitter
bsipocz pushed a commit that referenced this pull request Nov 7, 2020
Fixes for weights with linear fitter
astrofrog pushed a commit that referenced this pull request Nov 10, 2020
Fixes for weights with linear fitter
bsipocz pushed a commit that referenced this pull request Nov 23, 2020
Fixes for weights with linear fitter
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

LinearLSQFitter does not handle 2D weights properly

5 participants