Skip to content

LinearLSQFitter does not handle 2D weights properly #7035

Closed

Description

@majkelx

When passing 2D weigths to LinearLSQFitter in 2D mode:

Traceback (most recent call last):
  [....]
  File "/Users/michal/sources/astropy/astropy/modeling/fitting.py", line 456, in __call__
    lhs *= weights[:, np.newaxis]
ValueError: operands could not be broadcast together with shapes (441,10) (21,1,21) (441,10) 

LevMarLSQFitter works properly.

Activity

  1. jehturner commented on Mar 13, 2018

    @jehturner
    Member

    Thanks. I was going to report this but you've saved me the bother. Here's a slightly simpler example that doesn't work FWIW:

    import numpy as np
    from astropy.modeling import models, fitting
    
    fitter = fitting.LinearLSQFitter()
    
    y, x = np.mgrid[0:5,0:5]
    z = x+y
    
    weights = np.ones_like(x)
    
    model = models.Polynomial2D(0)
    
    fit = fitter(model, x, y, z, weights=weights)
    
     [...]
      File "/home/jturner/repos/astropy/astropy/modeling/fitting.py", line 475, in __call__
        lhs *= weights[:, np.newaxis]
    ValueError: non-broadcastable output operand with shape (25,1) doesn't match the broadcast shape (5,25,5)
    
  2. jehturner commented on Mar 13, 2018

    @jehturner
    Member

    Related problem: This should probably be fixed at the same time as 2D weights, since they are both a matter of reworking the same logic. LinearLSQFitter expects the dimensionality of weights to match the x array (like the other fitters), but this doesn't allow for model sets to have different weights for different models. An (ideally-optional) model_set_axis dimension should probably be accepted in the argument to weights, since the correct weighting could depend on different data or noise values in different models in the set. There may be some cases where a common set of weights can be used for all this models; this could probably be inferred from the array dimensionality.

  3. added 2 commits that reference this issue on Aug 28, 2020
    3b37dc3
    2f01db4
  4. added 2 commits that reference this issue on Sep 11, 2020
    d161a24
    18b6dd6
  5. added 2 commits that reference this issue on Sep 23, 2020
    1711e8e
    840ce67
  6. added 2 commits that reference this issue on Oct 5, 2020
    0672929
    69f880c
  7. added a commit that references this issue on Oct 26, 2020
    2bfdd65
  8. added a commit that references this issue on Feb 21, 2021
    873e976
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions