question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

astropy.modeling returns too many Parameters when defining a new Model as a subclass of another Model

See original GitHub issue

When creating an astropy.modeling.Model that is a subclass of another astropy.modeling.Model (instead of just being a subclass of a Fittable1DModel or Fittable2DModel, e.g.) the Parameters of both the new Model and the super Model are returned, resulting in TypeError: evaluate() takes X positional arguments but Y were given.

MWE to reproduce the error:

import astropy
print('astropy version', astropy.__version__)

from astropy.modeling import Fittable2DModel, Parameter


class classmodel(Fittable2DModel):
    f = Parameter(default=1)
    x = Parameter(default=0)
    y = Parameter(default=2)

    def __init__(self, f=f.default, x=x.default, y=y.default):
        print(self.param_names)
        print('class input values', f, x, y)
        super().__init__(f, x, y)

    def evaluate(self):
        pass


class subclassmodel(classmodel):
    f = Parameter(default=3)
    x = Parameter(default=10)
    y = Parameter(default=12)
    h = Parameter(default=5)

    def __init__(self, f=f.default, x=x.default, y=y.default, h=h.default):
        print('subclass input values', f, x, y, h)
        super().__init__(f, x, y)

    def evaluate(self):
        pass


print('class should be: f=1, x=0, y=2')
print('subclass should be: f=3, x=10, y=12, h=5')
a = classmodel()
print('class params + names', a.parameters, a.param_names)
b = subclassmodel()
print('subclass params + names', b.parameters, b.param_names)

which gives:

astropy version 3.2.1
class should be: f=1, x=0, y=2
subclass should be: f=3, x=10, y=12, h=5
('f', 'x', 'y')
class input values 1 0 2
class params + names [1. 0. 2.] ('f', 'x', 'y')
subclass input values 3 10 12 5
('f', 'x', 'y', 'h')
class input values 3 10 12
subclass params + names [ 3. 10. 12.  5.] ('f', 'x', 'y', 'h')

and

astropy version 4.0.dev25505
class should be: f=1, x=0, y=2
subclass should be: f=3, x=10, y=12, h=5
('f', 'x', 'y')
class input values 1 0 2
class params + names [1. 0. 2.] ('f', 'x', 'y')
subclass input values 3 10 12 5
('f', 'x', 'y', 'f', 'x', 'y', 'h')
class input values 3 10 12
subclass params + names [ 3. 10. 12.  5.] ('f', 'x', 'y', 'f', 'x', 'y', 'h')

I am not sure what changed in the new v4.0 modeling framework updates from last week, but a cursory dig into the code suggests that the extension of param_names occurs somewhere in here, as param_names is length 4 before the loop and length 7 afterwards. My initial feeling is that that’s a CompoundModel thing which multi-nesting subclasses of Models is tripping up, but I am not an expert on the modeling framework.

See astropy/photutils#921 for a reproduction of a ‘proper’ failing use case, cc @eteq @larrybradley

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:5 (5 by maintainers)

github_iconTop GitHub Comments

2reactions
perrygreenfieldcommented, Aug 9, 2019

Thanks for the report. Yes, there were substantial changes in how parameters were changed and that is almost certainly the cause of the problem. I’ll look into it, probably next week.

1reaction
bsipoczcommented, Aug 13, 2019

@perrygreenfield - photutils has been fixed by removing defaulting the same parameters in the nested subclassing case (https://github.com/astropy/photutils/pull/931). Not sure whether that case is suppose to be supported, I would say it was rather confusing and shouldn’t be used like that.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Defining New Model Classes — Astropy v5.2
If the model takes parameters they should be specified as class attributes in the model's class definition using the Parameter descriptor.
Read more >
Combining Models — Astropy v5.2
It is possible to create new models just by combining existing models using the arithmetic operators + , - , * , /...
Read more >
Source code for astropy.modeling.core
The base class of all models is `~astropy.modeling.Model`. ... The new class is technically a subclass of the original class, so that instance...
Read more >
Model — Astropy v5.1.1
When defining a custom model class the value of this attribute is automatically set by the Parameter attributes defined in the class body....
Read more >
Models — Astropy v5.2
All models have a Model.inverse property which may, for some models, return a new model that is the analytic inverse of the model...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found