Skip to content

Commit 9bbdd64

Browse files
astrofrogCadair
authored andcommitted
Fix the checking of units for compound models.
The issue was that the compound model called evaluate directly on all submodels without going through __call__ which means that prepare_inputs was being skipped. The unit validation was done inside prepare_units, so the fix is to separate out the validation of the units into a dedicated method. In addition, _model_evaluate_getter previously did not instantiate models when the compound model included a model class instead of an instance and evaluate was a class method - however we definitely need to always instantiate the model since e.g. input_units can be defined as an instance property.
1 parent 22c60fc commit 9bbdd64

1 file changed

Lines changed: 40 additions & 29 deletions

File tree

‎astropy/modeling/core.py‎

Lines changed: 40 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1441,15 +1441,31 @@ def prepare_inputs(self, *inputs, model_set_axis=None, equivalencies=None,
14411441
_validate_input_shapes(inputs, self.inputs, n_models,
14421442
model_set_axis, self.standard_broadcasting)
14431443

1444+
inputs = self._validate_input_units(inputs, equivalencies)
1445+
1446+
# The input formatting required for single models versus a multiple
1447+
# model set are different enough that they've been split into separate
1448+
# subroutines
1449+
if n_models == 1:
1450+
return _prepare_inputs_single_model(self, params, inputs,
1451+
**kwargs)
1452+
else:
1453+
return _prepare_inputs_model_set(self, params, inputs, n_models,
1454+
model_set_axis, **kwargs)
1455+
1456+
def _validate_input_units(self, inputs, equivalencies=None):
1457+
1458+
inputs = list(inputs)
1459+
14441460
# Check that the units are correct, if applicable
14451461

14461462
if self.input_units is not None:
14471463

14481464
# We combine any instance-level input equivalencies with user
14491465
# specified ones at call-time.
14501466
input_units_equivalencies = _combine_equivalency_dict(self.inputs,
1451-
equivalencies,
1452-
self.input_units_equivalencies)
1467+
equivalencies,
1468+
self.input_units_equivalencies)
14531469

14541470
# We now iterate over the different inputs and make sure that their
14551471
# units are consistent with those specified in input_units.
@@ -1510,15 +1526,7 @@ def prepare_inputs(self, *inputs, model_set_axis=None, equivalencies=None,
15101526
"{1} ({2})".format(self.inputs[i], input_unit,
15111527
input_unit.physical_type))
15121528

1513-
# The input formatting required for single models versus a multiple
1514-
# model set are different enough that they've been split into separate
1515-
# subroutines
1516-
if n_models == 1:
1517-
return _prepare_inputs_single_model(self, params, inputs,
1518-
**kwargs)
1519-
else:
1520-
return _prepare_inputs_model_set(self, params, inputs, n_models,
1521-
model_set_axis, **kwargs)
1529+
return inputs
15221530

15231531
def prepare_outputs(self, format_info, *outputs, **kwargs):
15241532
if len(self) == 1:
@@ -2719,31 +2727,34 @@ def _model_evaluate_getter(idx, model):
27192727
n_inputs = model.n_inputs
27202728
n_outputs = model.n_outputs
27212729

2722-
# There is currently an unfortunate inconsistency in some models, which
2723-
# requires them to be instantiated for their evaluate to work. I think
2724-
# that needs to be reconsidered and fixed somehow, but in the meantime
2725-
# we need to check for that case
2726-
if (not isinstance(model, Model) and
2727-
isinstancemethod(model, model.evaluate)):
2730+
# If model is not an instance, we need to instantiate it to make sure
2731+
# that we can call _validate_input_units (since e.g. input_units can
2732+
# be an instance property).
2733+
2734+
if isinstance(model, Model):
2735+
evaluate = model.evaluate
27282736
if n_outputs == 1:
2729-
# Where previously model was a class, now make an instance
27302737
def f(inputs, params):
2731-
param_values = tuple(islice(params, n_params))
2732-
return (model(*param_values).evaluate(
2733-
*chain(inputs, param_values)),)
2738+
model._validate_input_units(inputs)
2739+
return (evaluate(*inputs, *islice(params, n_params)),)
27342740
else:
27352741
def f(inputs, params):
2736-
param_values = tuple(islice(params, n_params))
2737-
return model(*param_values).evaluate(
2738-
*chain(inputs, param_values))
2742+
model._validate_input_units(inputs)
2743+
return evaluate(*inputs, *islice(params, n_params))
27392744
else:
2740-
evaluate = model.evaluate
27412745
if n_outputs == 1:
2742-
f = lambda inputs, params: \
2743-
(evaluate(*chain(inputs, islice(params, n_params))),)
2746+
# Where previously model was a class, now make an instance
2747+
def f(inputs, params):
2748+
param_values = tuple(islice(params, n_params))
2749+
m = model(*param_values)
2750+
m._validate_input_units(inputs)
2751+
return (m.evaluate(*chain(inputs, param_values)),)
27442752
else:
2745-
f = lambda inputs, params: \
2746-
evaluate(*chain(inputs, islice(params, n_params)))
2753+
def f(inputs, params):
2754+
param_values = tuple(islice(params, n_params))
2755+
m = model(*param_values)
2756+
m._validate_input_units(inputs)
2757+
return m.evaluate(*chain(inputs, param_values))
27472758

27482759
return (f, n_inputs, n_outputs)
27492760

0 commit comments

Comments
 (0)