astropy.modeling returns too many Parameters when defining a new Model as a subclass of another Model
See original GitHub issueWhen 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 Model
s 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:
- Created 4 years ago
- Comments:5 (5 by maintainers)
Top GitHub Comments
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.
@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.