Skip to content

Commit d1be051

Browse files
committed
tensor: Add coverage for improved indexing capability
1 parent 61ec65c commit d1be051

File tree

2 files changed

+36
-9
lines changed

2 files changed

+36
-9
lines changed

pyttb/tensor.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1276,7 +1276,6 @@ def __setitem__(self, key, value):
12761276
"""
12771277
# Figure out if we are doing a subtensor, a list of subscripts or a list of
12781278
# linear indices
1279-
# print(f"Key: {key} {type(key)}")
12801279
access_type = "error"
12811280
# TODO pull out this big decision tree into a function
12821281
if isinstance(key, (float, int, np.generic, slice)):
@@ -1296,15 +1295,11 @@ def __setitem__(self, key, value):
12961295
validSubtensor = [
12971296
isinstance(keyElement, (int, slice, Iterable)) for keyElement in key
12981297
]
1299-
# TODO probably need to confirm the Iterable is in fact numeric
13001298
if np.all(validSubtensor):
13011299
access_type = "subtensor"
13021300
elif isinstance(key, Iterable):
13031301
key = np.array(key)
1304-
# Clean up copy paste
1305-
if len(key.shape) > 1 and key.shape[1] >= self.ndims:
1306-
access_type = "subscripts"
1307-
elif len(key.shape) == 1 or key.shape[1] == 1:
1302+
if len(key.shape) == 1 or key.shape[1] == 1:
13081303
access_type = "linear indices"
13091304

13101305
# Case 1: Rectangular Subtensor
@@ -1351,6 +1346,12 @@ def _set_subtensor(self, key, value):
13511346
else:
13521347
sliceCheck.append(element.stop)
13531348
elif isinstance(element, Iterable):
1349+
if any(
1350+
not isinstance(entry, (float, int, np.generic)) for entry in element
1351+
):
1352+
raise ValueError(
1353+
f"Entries for setitem must be numeric but recieved, {element}"
1354+
)
13541355
sliceCheck.append(max(element))
13551356
else:
13561357
sliceCheck.append(element)

tests/test_tensor.py

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -251,6 +251,13 @@ def test_tensor__setitem__(sample_tensor_2way):
251251
# Subtensor add dimension
252252
empty_tensor[0, 0, 0] = 2
253253

254+
# Subtensor with lists
255+
some_tensor = ttb.tenones((3, 3))
256+
some_tensor[[0, 1], [0, 1]] = 11
257+
assert some_tensor[0, 0] == 11
258+
assert some_tensor[1, 1] == 11
259+
assert np.all(some_tensor[[0, 1], [0, 1]].data == 11)
260+
254261
# Subscripts with constant
255262
tensorInstance[np.array([[1, 1]])] = 13.0
256263
dataGrowth[1, 1] = 13.0
@@ -293,6 +300,13 @@ def test_tensor__setitem__(sample_tensor_2way):
293300
dataGrowth[np.unravel_index([0, 3, 4], dataGrowth.shape, "F")] = 13
294301
assert (tensorInstance.data == dataGrowth).all()
295302

303+
# Linear index with multiple indicies
304+
some_tensor = ttb.tenones((3, 3))
305+
some_tensor[[0, 1]] = 2
306+
assert some_tensor[0] == 2
307+
assert some_tensor[1] == 2
308+
assert np.array_equal(some_tensor[[0, 1]], [2, 2])
309+
296310
# Test Empty Tensor Set Item, subtensor
297311
emptyTensor = ttb.tensor.from_data(np.array([]))
298312
emptyTensor[0, 0, 0] = 0
@@ -313,10 +327,17 @@ def test_tensor__setitem__(sample_tensor_2way):
313327
)
314328

315329
# Attempting to set some other way
316-
# TODO either catch this error ourselves or specify more specific exception we expect here
317-
with pytest.raises(Exception) as excinfo:
330+
with pytest.raises(ValueError) as excinfo:
318331
tensorInstance[0, "a", 5] = 13.0
319-
# assert "Invalid use of tensor setitem" in str(excinfo)
332+
assert "must be numeric" in str(excinfo)
333+
334+
with pytest.raises(AssertionError) as excinfo:
335+
336+
class BadKey:
337+
pass
338+
339+
tensorInstance[BadKey] = 13.0
340+
assert "Invalid use of tensor setitem" in str(excinfo)
320341

321342

322343
@pytest.mark.indevelopment
@@ -346,6 +367,11 @@ def test_tensor__getitem__(sample_tensor_2way):
346367
tensorInstance[np.array([[0, 0], [1, 1]]), "extract"]
347368
== params["data"][([0, 0], [1, 1])]
348369
).all()
370+
# Case 2a: Extract doesn't seem to be needed
371+
assert tensorInstance[np.array([0, 0])] == params["data"][0, 0]
372+
assert (
373+
tensorInstance[np.array([[0, 0], [1, 1]])] == params["data"][([0, 0], [1, 1])]
374+
).all()
349375

350376
# Case 2b: Linear Indexing
351377
assert tensorInstance[np.array([0])] == params["data"][0, 0]

0 commit comments

Comments
 (0)