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

Linear1D and Planar2D should be able to fit as model sets #10623

Merged
merged 1 commit into from Aug 10, 2020

Conversation

nden
Copy link
Contributor

@nden nden commented Aug 4, 2020

Linear1D and Planar2D should be able to fit as model sets with LinearLSQ fitter which this PR enables.
While fixing the problem I noticed a larger (design) issue with the way the fitter calls the derivative of a model. Currently the call is

model.fit_deriv(x, *model.parameters)

This works fine with single models. However, the signature of this method for non-linear models lists all parameters as keyword parameters. When a model set is fit the list *model.parameters becomes longer than the list of parameters in the signature.
When fitting.py is refactored the call to fit_deriv needs to be changed to solve this problem.

@astropy-bot astropy-bot bot added the modeling label Aug 4, 2020
@nden nden added this to the v4.0.2 milestone Aug 4, 2020
@pllim pllim added the Bug label Aug 4, 2020
Copy link
Member

@perrygreenfield perrygreenfield left a comment

Choose a reason for hiding this comment

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

Mostly minor comments

@@ -969,6 +970,8 @@ astropy.modeling
without units when the expression involves operators other than addition
and subtraction. [#10415]

- Fixed a problem with fitting ``Linear1D`` and ``Planar2D`` ain model sets. [#10623]

Copy link
Member

Choose a reason for hiding this comment

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

typo before model

in the correct shape. The shape depends on the ``model_set_axis`` attribute of the
model to be fit. The rule is that the dependent variable for each model in a set should
be along the ``model_set_axis`` dimension. For example, for a 1D model set with 3 models
with ``model_set_axis == 1`` the shape of ``y`` should be (x, 3)::
Copy link
Member

Choose a reason for hiding this comment

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

I don't know for sure, but I find the use of 'dependent' variable in this description to be confusing, Dependent on what? The input x values or the model set? I think it should be more specific, something like "the index of the dependent variable that corresponds to the model set should be the model_set_axis, for example, if there were 10 x input values and 3 model sets the shape of y should be (10, 3). Or something like that.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks, I was struggling with this sentence.

>>> m = Polynomial1D(1, n_models=3, model_set_axis=1)
>>> mfit = fitter(m, x, y)

For a 2D model set with ``model_set_axis == 0`` the shape of ``z`` should be (2, x, y)::
Copy link
Member

Choose a reason for hiding this comment

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

Here 2D is potentially misunderstood. Perhaps reword as "For 2D models with the model_set_axis = 0" (should we really use '==' instead of '='?)

>>> print(z.shape)
(2, 2, 4)
>>> m = Polynomial2D(1, n_models=2, model_set_axis=0)
>>> mfit = fitter(m, x, y, z)
Copy link
Member

Choose a reason for hiding this comment

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

I always figure it is good to avoid dimensions that are identical in size to prevent confusion. Here we have two that are 2.

fixderivs = self._deriv_with_constraints(model_copy,
fixparam_indices,
x=x)
lhs = np.asarray(self._deriv_with_constraints(model_copy,
Copy link
Member

Choose a reason for hiding this comment

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

Just to understand better, what was being returned before?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Previously, it was a list.


with pytest.raises(ValueError):
p1(x)

with pytest.raises(ValueError):
p1(xx)
with pytest.raises(ValueError):
Copy link
Member

Choose a reason for hiding this comment

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

Why the nesting?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

cut and paste error, removed it

@@ -204,11 +210,43 @@ def test_shapes():
assert t.e.shape == (2,)


def test_linearlsqfitter():
def test_eval():
""" Tests evaluation of Linear1D and Planar2D with differrent model_set_axis."""
Copy link
Member

Choose a reason for hiding this comment

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

different misspelling

@nden
Copy link
Contributor Author

nden commented Aug 7, 2020

@perrygreenfield I addressed your comments. Thanks.

@@ -495,9 +494,6 @@ def __call__(self, model, x, y, z=None, weights=None, rcond=None):
lhs *= weights[:, np.newaxis]
rhs = rhs * weights

if rcond is None:
rcond = len(x) * np.finfo(x.dtype).eps

Copy link
Contributor Author

Choose a reason for hiding this comment

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

To explain: numpy now deals with this, if rcond is None.

Copy link
Member

@perrygreenfield perrygreenfield left a comment

Choose a reason for hiding this comment

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

LGTM

@nden nden merged commit 14b29c6 into astropy:master Aug 10, 2020
astrofrog pushed a commit that referenced this pull request Oct 6, 2020
Linear1D and Planar2D should be able to fit as model sets
astrofrog pushed a commit that referenced this pull request Oct 6, 2020
Linear1D and Planar2D should be able to fit as model sets
eteq pushed a commit to eteq/astropy that referenced this pull request Oct 12, 2020
Linear1D and Planar2D should be able to fit as model sets
eteq pushed a commit that referenced this pull request Oct 13, 2020
Linear1D and Planar2D should be able to fit as model sets
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.

None yet

3 participants