Skip to content

Commit 99ea741

Browse files
embrayAkshat1Nar
authored andcommitted
[astropy#11094] Remove use of OrderedDescriptor for astropy.modeling
Parameter.__set_name__ is used to set the names of Parameters upon class initialization. As before, the Model._parameters_ dict is intialized in _ModelMeta.__new__ (which can't easily be replaced with __init_subclass__ as it has other functionalities). Although the removed function astropy.utils.misc.get_parameters was technically public, it was arguably an implementation detail of astropy.modeling.core and never should have been there; it is easily replaced now with a one-liner.
1 parent f8c3c6e commit 99ea741

3 files changed

Lines changed: 11 additions & 32 deletions

File tree

‎astropy/modeling/core.py‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@
3636
check_broadcast, IncompatibleShapeError, isiterable)
3737
from astropy.utils.codegen import make_function_with_signature
3838
from astropy.utils.exceptions import AstropyDeprecationWarning
39-
from astropy.utils.misc import get_parameters
4039
from astropy.nddata.utils import add_array, extract_array
4140
from .utils import (combine_labels, make_binary_operator_eval,
4241
get_inputs_and_params, _BoundingBox, _combine_equivalency_dict,
@@ -90,7 +89,6 @@ def __new__(mcls, name, bases, members):
9089
# See the docstring for _is_dynamic above
9190
if '_is_dynamic' not in members:
9291
members['_is_dynamic'] = mcls._is_dynamic
93-
get_parameters(members)
9492
opermethods = [
9593
('__add__', _model_oper('+')),
9694
('__sub__', _model_oper('-')),
@@ -102,6 +100,9 @@ def __new__(mcls, name, bases, members):
102100
('_fix_inputs', _model_oper('fix_inputs'))
103101
]
104102

103+
members['_parameters_'] = {k: v for k, v in members.items()
104+
if isinstance(v, Parameter)}
105+
105106
for opermethod, opercall in opermethods:
106107
members[opermethod] = opercall
107108
cls = super().__new__(mcls, name, bases, members)

‎astropy/modeling/parameters.py‎

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
import numpy as np
1717

1818
from astropy.units import Quantity
19-
from astropy.utils import isiterable, OrderedDescriptor
19+
from astropy.utils import isiterable
2020
from .utils import array_repr_oneline
2121
from .utils import get_inputs_and_params
2222

@@ -111,14 +111,13 @@ def wrapper(self):
111111
return wrapper
112112

113113

114-
class Parameter(OrderedDescriptor):
114+
class Parameter:
115115
"""
116116
Wraps individual parameters.
117117
118-
Since 4.0 Parameters are no longer descriptors (despite the fact that
119-
it inherits from OrderedDescriptor) and are based on a new implementation
120-
of the Parameter class. Parameters now (as of 4.0) store values locally
121-
(as instead previously in the associated model)
118+
Since 4.0 Parameters are no longer descriptors and are based on a new
119+
implementation of the Parameter class. Parameters now (as of 4.0) store
120+
values locally (as instead previously in the associated model)
122121
123122
This class represents a model's parameter (in a somewhat broad sense). It
124123
serves a number of purposes:
@@ -191,10 +190,6 @@ class Parameter(OrderedDescriptor):
191190
fitters as of this writing.
192191
"""
193192

194-
# Settings for OrderedDescriptor
195-
_class_attribute_ = '_parameters_'
196-
_name_attribute_ = '_name'
197-
198193
def __init__(self, name='', description='', default=None, unit=None,
199194
getter=None, setter=None, fixed=False, tied=False, min=None,
200195
max=None, bounds=None):
@@ -251,6 +246,9 @@ def __init__(self, name='', description='', default=None, unit=None,
251246

252247
self._std = None
253248

249+
def __set_name__(self, owner, name):
250+
self._name = name
251+
254252
def __len__(self):
255253
val = self.value
256254
if val.shape == ():

‎astropy/utils/misc.py‎

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -778,26 +778,6 @@ def __init__(cls, cls_name, bases, members):
778778
members)
779779

780780

781-
def get_parameters(members):
782-
"""
783-
Looks for ordered descriptors in a class definition and
784-
copies such definitions in two new class attributes,
785-
one being a dictionary of these objects keyed by their
786-
attribute names, and the other a simple list of those names.
787-
788-
"""
789-
pdict = OrderedDict()
790-
for name, obj in members.items():
791-
if (not isinstance(obj, OrderedDescriptor)):
792-
continue
793-
if obj._name_attribute_ is not None:
794-
setattr(obj, '_name', name)
795-
pdict[name] = obj
796-
797-
# members['_parameter_vals_'] = pdict
798-
members['_parameters_'] = pdict
799-
800-
801781
LOCALE_LOCK = threading.Lock()
802782

803783

0 commit comments

Comments
 (0)