Skip to content

Broken example in modeling docs #1624

Closed

Description

@mdboom

fitting.rst contains the following example.

>>> from astropy.modeling import *

>>> x = np.arange(1, 10, .1)
>>> p1 = models.Poly1DModel(2, param_dim=2)
>>> p1.parameters = [1, 1, 2, 2, 3, 3]
>>> p1.param_sets
array([[ 1.,  1.],
       [ 2.,  2.],
       [ 3.,  3.]])
>>> y = p1(x)
>>> p1.c1.fixed = True
>>> pfit = fitting.LinearLSQFitter(p1)

The last line raises the exception: InputParameterError: Parameter of <type 'list'> could not be converted to float

Activity

  1. ghost assigned on Oct 22, 2013
  2. embray commented on Oct 23, 2013

    @embray
    Member

    FWIW, in my branch for #1086 this example is also broken, but at a different point. Instantiating the LinearLSQFitter works, but then on the next line (pfit(x, y)) I get:

    In [16]: pfit(x, y)
    WARNING: The fit may be poorly conditioned
     [astropy.modeling.fitting]
    ERROR: ValueError: could not broadcast input array from shape (4) into shape (5) [astropy.modeling.fitting]
    ---------------------------------------------------------------------------
    ValueError                                Traceback (most recent call last)
    <ipython-input-16-919afb17028d> in <module>()
    ----> 1 pfit(x, y)
    
    /internal/1/root/src/astropy/astropy/astropy/modeling/fitting.py in __call__(self, x, y, z, weights, rcond)
        484             warnings.warn("The fit may be poorly conditioned\n",
        485                           AstropyUserWarning)
    --> 486         self.fitparams = lacoef.flatten()
        487 
        488 
    
    /internal/1/root/src/astropy/astropy/astropy/modeling/fitting.py in fitparams(self, fps)
        148         """
        149 
    --> 150         self._fitparams[:] = fps
        151 
        152         if any(self.fixed) or any(self.tied):
    
    ValueError: could not broadcast input array from shape (4) into shape (5)
    

    So I'll have to take a look at what's going on there.

  3. embray commented on Oct 24, 2013

    @embray
    Member

    I've got this example working correctly in my branch now, so I'll update that soon. I actually also changed this example in the documentation, because the expository text mentions as an example fixing the zero-th order term, but then this example--which immediately follows--shows fixing the first order term, which I think is confusing. So I updated the example to fix the 0-th coefficient c0. But the old example still works too.

  4. added a commit that references this issue on Oct 24, 2013
    977760a
  5. embray commented on Oct 25, 2013

    @embray
    Member

    Now that #1086 is merged this should be fixed. I just ran through the example again to make sure. The only caveat is that there is now a warning "WARNING: The fit may be poorly conditioned
    [astropy.modeling.fitting]"

    when performing the fit. I don't think that's a problem, but I'm not sure if it will allow the doctest to pass.

  6. embray commented on Oct 25, 2013

    @embray
    Member

    Taking a closer look, I think that might be a bug, albeit a small one. If there are any fixed parameters (as there are in that example), then the rank (effective or otherwise) of the input matrix will generally be smaller than the order of the polynomial. This warning doesn't affect the outcome, but it's raised if rank != model.order, basically. And that won't be the case when there are fixed parameters.

  7. nden commented on Oct 25, 2013

    @nden
    Contributor

    I am working on this.

  8. nden commented on Oct 25, 2013

    @nden
    Contributor

    Another case when it's raised is when fitting a polynomial with multiple parameter sets, in which case it's also benign. But it could indicate a real problem (as in #1681) so instead of trying to figure out whether it's a real problem, we just raise a warning. In most cases it is not a problem.
    This is not a warning from numpy and I don't think doctests should catch it as there are many occasions when warnings are raised.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions