Skip to content

Commit a9b67fb

Browse files
Potential fix for issue astropy#10170
Added tests to show that issue astropy#10170 is fixed. Added `towncrier` change log. Fixed codestyle check by adding specific exceptions to check. Possible fixes for issue astropy#11360. More investigation is necessary. Cleaned up last bugfix. Updated change log entry. Added tests for astropy#11360 fixes. Fixed whitespace Fix for jwst and gwcs test failures. Added testing for issue causing fails in jwst and gwcs Fixed a typo. Added some of astropy#10362 tests Changes in astropy#10362 to pass new tests Code cleanup More code cleanup Added additional testing from astropy#10362 Possible fix for issue astropy#11060. Added test to show astropy#11060 has been fixed Fixed failing test under no-scipy Added future warning ignore to newest test Modified tabular so that both inputs in issue astropy#11060 produce same output
1 parent 4a94fc8 commit a9b67fb

7 files changed

Lines changed: 250 additions & 18 deletions

File tree

‎astropy/modeling/core.py‎

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3893,15 +3893,6 @@ def _prepare_inputs_single_model(model, params, inputs, **kwargs):
38933893
broadcasts.append(max_broadcast)
38943894

38953895
if model.n_outputs > model.n_inputs:
3896-
if len(set(broadcasts)) > 1:
3897-
raise ValueError(
3898-
"For models with n_outputs > n_inputs, the combination of "
3899-
"all inputs and parameters must broadcast to the same shape, "
3900-
"which will be used as the shape of all outputs. In this "
3901-
"case some of the inputs had different shapes, so it is "
3902-
"ambiguous how to format outputs for this model. Try using "
3903-
"inputs that are all the same size and shape.")
3904-
# Extend the broadcasts list to include shapes for all outputs
39053896
extra_outputs = model.n_outputs - model.n_inputs
39063897
if not broadcasts:
39073898
# If there were no inputs then the broadcasts list is empty
@@ -3914,16 +3905,24 @@ def _prepare_inputs_single_model(model, params, inputs, **kwargs):
39143905

39153906

39163907
def _prepare_outputs_single_model(outputs, format_info):
3917-
broadcasts = format_info[0]
39183908
outputs = list(outputs)
39193909
for idx, output in enumerate(outputs):
3920-
broadcast_shape = broadcasts[idx]
3910+
try:
3911+
broadcast_shape = check_broadcast(*format_info[0])
3912+
except (IndexError, TypeError) as e:
3913+
broadcast_shape = format_info[0][idx]
3914+
39213915
if broadcast_shape is not None:
39223916
if not broadcast_shape:
3923-
# Shape is (), i.e. a scalar should be returned
39243917
outputs[idx] = output.item()
39253918
else:
3926-
outputs[idx] = output.reshape(broadcast_shape)
3919+
try:
3920+
outputs[idx] = output.reshape(broadcast_shape)
3921+
except ValueError:
3922+
try:
3923+
outputs[idx] = output.item()
3924+
except ValueError:
3925+
outputs[idx] = output
39273926

39283927
return tuple(outputs)
39293928

@@ -4050,7 +4049,7 @@ def _validate_input_shapes(inputs, argnames, n_models, model_set_axis,
40504049
n_models))
40514050
all_shapes.append(input_shape)
40524051

4053-
input_shape = check_consistent_shapes(*all_shapes)
4052+
input_shape = check_broadcast(*all_shapes)
40544053
if input_shape is None:
40554054
raise ValueError(
40564055
"All inputs must have identical shapes or must be scalars.")

‎astropy/modeling/polynomial.py‎

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1037,8 +1037,6 @@ def prepare_inputs(self, x, y, **kwargs):
10371037
inputs, format_info = super().prepare_inputs(x, y, **kwargs)
10381038

10391039
x, y = inputs
1040-
if x.shape != y.shape:
1041-
raise ValueError("Expected input arrays to have the same shape")
10421040
return (x, y), format_info
10431041

10441042
def evaluate(self, x, y, *coeffs):

‎astropy/modeling/tabular.py‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,8 @@ def evaluate(self, *inputs):
206206
Input coordinates. The number of inputs must be equal
207207
to the dimensions of the lookup table.
208208
"""
209+
inputs = np.broadcast_arrays(*inputs)
210+
209211
if isinstance(inputs, u.Quantity):
210212
inputs = inputs.value
211213
shape = inputs[0].shape

‎astropy/modeling/tests/test_core.py‎

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -520,6 +520,114 @@ def test_rename_inputs_outputs():
520520
g2.outputs = ("w", "e")
521521

522522

523+
def test_prepare_outputs_mixed_broadcast():
524+
"""
525+
Tests that _prepare_outputs_single_model does not fail when a smaller
526+
array is passed as first input, but output is broadcast to larger
527+
array.
528+
Issue #10170
529+
"""
530+
531+
model = models.Gaussian2D(1, 2, 3, 4, 5)
532+
533+
output = model([1, 2], 3)
534+
assert output.shape == (2,)
535+
np.testing.assert_array_equal(output, [0.9692332344763441, 1.0])
536+
537+
output = model(4, [5, 6])
538+
assert output.shape == (2,)
539+
np.testing.assert_array_equal(output, [0.8146473164114145, 0.7371233743916278])
540+
541+
542+
def test_prepare_outputs_complex_reshape():
543+
x = np.array([[1, 2, 3, 4, 5],
544+
[6, 7, 8, 9, 10],
545+
[11, 12, 13, 14, 15]])
546+
y = np.array([[16, 17, 18, 19, 20],
547+
[21, 22, 23, 24, 25],
548+
[26, 27, 28, 29, 30]])
549+
550+
m = models.Identity(3) | models.Mapping((2, 1, 0))
551+
m.bounding_box = ((0, 100), (0, 200), (0, 50))
552+
mf = models.fix_inputs(m, {2: 22})
553+
t = mf | models.Mapping((2, 1), n_inputs=3)
554+
555+
output = mf(1, 2)
556+
assert output == (22, 2, 1)
557+
558+
output = t(1, 2)
559+
assert output == (1, 2)
560+
561+
output = t(x, y)
562+
assert len(output) == 2
563+
np.testing.assert_array_equal(output[0], x)
564+
np.testing.assert_array_equal(output[1], y)
565+
566+
m = models.Identity(3) | models.Mapping((0, 1, 2))
567+
m.bounding_box = ((0, 100), (0, 200), (0, 50))
568+
mf = models.fix_inputs(m, {2: 22})
569+
t = mf | models.Mapping((0, 1), n_inputs=3)
570+
571+
output = mf(1, 2)
572+
assert output == (1, 2, 22)
573+
574+
output = t(1, 2)
575+
assert output == (1, 2)
576+
577+
output = t(x, y)
578+
assert len(output) == 2
579+
np.testing.assert_array_equal(output[0], x)
580+
np.testing.assert_array_equal(output[1], y)
581+
582+
583+
def test_prepare_outputs_single_entry_vector():
584+
"""
585+
jwst and gwcs both require that single entry vectors produce single entry output vectors, not scalars. This
586+
tests for that behavior.
587+
"""
588+
589+
model = models.Gaussian2D(1, 2, 3, 4, 5)
590+
591+
output = model(np.array([1]), np.array([2]))
592+
assert output.shape == (1,)
593+
np.testing.assert_array_equal(output, [0.9500411305585278])
594+
595+
596+
@pytest.mark.skipif('not HAS_SCIPY')
597+
@pytest.mark.filterwarnings('ignore: Using a non-tuple')
598+
def test_prepare_outputs_sparse_grid():
599+
"""
600+
Test to show that #11060 has been solved.
601+
"""
602+
603+
shape = (3, 3)
604+
data = np.arange(np.product(shape)).reshape(shape) * u.m / u.s
605+
606+
points_unit = u.pix
607+
points = [np.arange(size) * points_unit for size in shape]
608+
609+
kwargs = {
610+
'bounds_error': False,
611+
'fill_value': np.nan,
612+
'method': 'nearest',
613+
}
614+
615+
transform = models.Tabular2D(points, data, **kwargs)
616+
truth = np.array([[0., 1., 2.],
617+
[3., 4., 5.],
618+
[6., 7., 8.]]) * u.m / u.s
619+
620+
points = np.meshgrid(np.arange(3), np.arange(3), indexing='ij', sparse=True)
621+
x = points[0] * u.pix
622+
y = points[1] * u.pix
623+
value = transform(x, y)
624+
assert (value == truth).all()
625+
626+
points = np.meshgrid(np.arange(3), np.arange(3), indexing='ij', sparse=False) * u.pix
627+
value = transform(*points)
628+
assert (value == truth).all()
629+
630+
523631
def test_coerce_units():
524632
model = models.Polynomial1D(1, c0=1, c1=2)
525633

‎astropy/modeling/tests/test_input.py‎

Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -816,6 +816,130 @@ def test_mixed_array_parameters_1d_array_input(self):
816816
[131.10, 232.11, 333.12]]]])
817817

818818

819+
# test broadcasting rules
820+
broadcast_models = [
821+
{
822+
'model': models.Identity(2),
823+
'inputs': [0, [1, 1]],
824+
'outputs': [0, [1, 1]]
825+
},
826+
{
827+
'model': models.Identity(2),
828+
'inputs': [[1, 1], 0],
829+
'outputs': [[1, 1], 0]
830+
},
831+
{
832+
'model': models.Mapping((0, 1)),
833+
'inputs': [0, [1, 1]],
834+
'outputs': [0, [1, 1]]
835+
},
836+
{
837+
'model': models.Mapping((1, 0)),
838+
'inputs': [0, [1, 1]],
839+
'outputs': [[1, 1], 0]
840+
},
841+
{
842+
'model': models.Mapping((1, 0), n_inputs=3),
843+
'inputs': [0, [1, 1], 2],
844+
'outputs': [[1, 1], 0]
845+
},
846+
{
847+
'model': models.Mapping((0, 1, 0)),
848+
'inputs': [0, [1, 1]],
849+
'outputs': [0, [1, 1], 0]
850+
},
851+
{
852+
'model': models.Mapping((0, 1, 1)),
853+
'inputs': [0, [1, 1]],
854+
'outputs': [0, [1, 1], [1, 1]]
855+
},
856+
{
857+
'model': models.Polynomial2D(1, c0_0=1),
858+
'inputs': [0, [1, 1]],
859+
'outputs': [1, 1]
860+
},
861+
{
862+
'model': models.Polynomial2D(1, c0_0=1),
863+
'inputs': [0, 1],
864+
'outputs': 1
865+
},
866+
{
867+
'model': models.Gaussian2D(1, 1, 2, 1, 1.2),
868+
'inputs': [0, [1, 1]],
869+
'outputs': [0.42860385, 0.42860385]
870+
},
871+
{
872+
'model': models.Gaussian2D(1, 1, 2, 1, 1.2),
873+
'inputs': [0, 1],
874+
'outputs': 0.428603846153
875+
},
876+
{
877+
'model': models.Polynomial2D(1, c0_0=1) & models.Polynomial2D(1, c0_0=2),
878+
'inputs': [1, 1, 1, 1],
879+
'outputs': (1, 2)
880+
},
881+
{
882+
'model': models.Polynomial2D(1, c0_0=1) & models.Polynomial2D(1, c0_0=2),
883+
'inputs': [1, 1, [1, 1], [1, 1]],
884+
'outputs': (1, [2, 2])
885+
},
886+
{
887+
'model': models.math.MultiplyUfunc(),
888+
'inputs': [np.array([np.linspace(0, 1, 5)]).T, np.arange(2)],
889+
'outputs': np.array([[0., 0.],
890+
[0., 0.25],
891+
[0., 0.5],
892+
[0., 0.75],
893+
[0., 1.]])
894+
}
895+
]
896+
897+
898+
@pytest.mark.filterwarnings(
899+
r'ignore:Models in math_functions are experimental\.')
900+
@pytest.mark.parametrize('model', broadcast_models)
901+
def test_mixed_input(model):
902+
result = model['model'](*model['inputs'])
903+
if np.isscalar(result):
904+
assert_allclose(result, model['outputs'])
905+
else:
906+
for i in range(len(result)):
907+
assert_allclose(result[i], model['outputs'][i])
908+
909+
910+
def test_more_outputs():
911+
912+
class M(FittableModel):
913+
standard_broadcasting = False
914+
n_inputs = 2
915+
n_outputs = 3
916+
917+
a = Parameter()
918+
919+
def evaluate(self, x, y, a):
920+
return a*x, a-x, a+y
921+
922+
def __call__(self, *args, **kwargs):
923+
inputs, format_info = super().prepare_inputs(*args, **kwargs)
924+
925+
outputs = self.evaluate(*inputs, *self.parameters)
926+
output_shapes = [out.shape for out in outputs]
927+
output_shapes = [() if shape == (1,) else shape for shape in output_shapes]
928+
return self.prepare_outputs((tuple(output_shapes),), *outputs, **kwargs)
929+
930+
c = M(1)
931+
result = c([1, 1], 1)
932+
expected = [[1., 1.], [0., 0.], 2.]
933+
for r, e in zip(result, expected):
934+
assert_allclose(r, e)
935+
936+
c = M(1)
937+
result = c(1, [1, 1])
938+
expected = [1., 0., [2., 2.]]
939+
for r, e in zip(result, expected):
940+
assert_allclose(r, e)
941+
942+
819943
class TInputFormatter(Model):
820944
"""
821945
A toy model to test input/output formatting.

‎astropy/modeling/tests/test_models.py‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,8 +93,8 @@ def test_inconsistent_input_shapes():
9393
# but not array broadcasting
9494
x.shape = (10, 1)
9595
y.shape = (1, 10)
96-
with pytest.raises(ValueError):
97-
g(x, y)
96+
result = g(x, y)
97+
assert result.shape == (10, 10)
9898

9999

100100
def test_custom_model_bounding_box():
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fixes ``Model`` crashes when some inputs are scalars and during some types of output reshaping.

0 commit comments

Comments
 (0)