Skip to content

Commit 0d66a05

Browse files
committed
fix model slicing corner cases
1 parent 6a24f59 commit 0d66a05

2 files changed

Lines changed: 43 additions & 5 deletions

File tree

‎astropy/modeling/core.py‎

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1752,7 +1752,17 @@ def get_index_from_name(name):
17521752
except ValueError:
17531753
raise IndexError(
17541754
'Compound model {0} does not have a component named '
1755-
'{1}'.format(cls.name, index))
1755+
'{1}'.format(cls.name, name))
1756+
1757+
def check_for_negative_index(index):
1758+
if index < 0:
1759+
ind = len(cls.submodel_names) + index
1760+
if ind < 0:
1761+
# If still < 0 then this is an invalid index
1762+
raise IndexError("Model index {0} out of range.".format(index))
1763+
else:
1764+
return ind
1765+
return index
17561766

17571767
if isinstance(index, six.string_types):
17581768
return get_index_from_name(index)
@@ -1766,11 +1776,14 @@ def get_index_from_name(name):
17661776
start = index.start if index.start is not None else 0
17671777
stop = (index.stop
17681778
if index.stop is not None else len(cls.submodel_names))
1779+
if isinstance(start, int):
1780+
start = check_for_negative_index(start)
1781+
if isinstance(stop, int):
1782+
stop = check_for_negative_index(stop)
17691783
if isinstance(start, six.string_types):
1770-
start = cls.submodel_names.index(start)
1784+
start = get_index_from_name(start)
17711785
if isinstance(stop, six.string_types):
1772-
stop = cls.submodel_names.index(stop) + 1
1773-
1786+
stop = get_index_from_name(stop) + 1
17741787
length = stop - start
17751788

17761789
if length == 1:
@@ -1782,7 +1795,6 @@ def get_index_from_name(name):
17821795
elif isinstance(index, int):
17831796
if index < 0:
17841797
index = len(cls.submodel_names) + index
1785-
17861798
if index < 0 or index >= len(cls.submodel_names):
17871799
# If still < 0 then this is an invalid index
17881800
raise IndexError("Model index out of range.")

‎astropy/modeling/tests/test_compound.py‎

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -495,3 +495,29 @@ def test_mapping_inverse():
495495
m = M(12.1, 13.2, 14.3, 15.4)
496496

497497
assert_allclose((0, 1, 2), m.inverse(*m(0, 1, 2)), atol=1e-08)
498+
499+
500+
def test_slicing_on_instances_2():
501+
"""More slicing tests."""
502+
503+
model_a = Shift(1, name='a')
504+
model_b = Shift(2, name='b')
505+
model_c = Rotation2D(3, name='c')
506+
model_d = Scale(2, name='d')
507+
model_e = Scale(3, name='e')
508+
509+
m = (model_a & model_b) | model_c | (model_d & model_e)
510+
511+
assert m[1:].submodel_names == ('b', 'c', 'd', 'e')
512+
assert m[:].submodel_names == ('a', 'b', 'c', 'd', 'e')
513+
assert m['a':].submodel_names == ('a', 'b', 'c', 'd', 'e')
514+
assert m['c':'d'].submodel_names == ('c', 'd')
515+
assert m[1:2].name == 'b'
516+
assert m[2:7].submodel_names == ('c', 'd', 'e')
517+
with pytest.raises(IndexError):
518+
m['x']
519+
with pytest.raises(IndexError):
520+
m['a' : 'r']
521+
assert m[-4:4].submodel_names == ('b', 'c', 'd')
522+
assert m[-4:-2].submodel_names == ('b', 'c')
523+

0 commit comments

Comments
 (0)