Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 8 additions & 7 deletions pina/label_tensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -380,20 +380,21 @@ def _update_single_label(self, old_labels, to_update_labels, index, dim,
"""
old_dof = old_labels[to_update_dim]['dof']
label_name = old_labels[dim]['name']

# Handle slicing
if isinstance(index, slice):
# Handle slicing
to_update_labels[dim] = {'dof': old_dof[index], 'name': label_name}
# Handle single integer index
elif isinstance(index, int):
# Handle single integer index
to_update_labels[dim] = {'dof': [old_dof[index]],
'name': label_name}
# Handle lists or tensors
elif isinstance(index, (list, torch.Tensor)):
# Handle lists or tensors
indices = [index] if isinstance(index, (int, str)) else index
# Handle list of bools
if isinstance(index, torch.Tensor) and index.dtype == torch.bool:
index = index.nonzero().squeeze()
to_update_labels[dim] = {
'dof': [old_dof[i] for i in indices] if isinstance(old_dof,
list) else indices,
'dof': [old_dof[i] for i in index] if isinstance(old_dof,
list) else index,
'name': label_name
}
else:
Expand Down
14 changes: 14 additions & 0 deletions tests/test_label_tensor/test_label_tensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -281,3 +281,17 @@ def test_sorting():
assert torch.eq(lt_sorted.tensor[:, 1, :], torch.ones(20, 5) * 2).all()
assert torch.eq(lt_sorted.tensor[:, 2, :], torch.ones(20, 5) * 3).all()
assert torch.eq(lt_sorted.tensor[:, 3, :], torch.ones(20, 5) * 4).all()


@pytest.mark.parametrize("labels",
[[f's{i}' for i in range(10)],
{0: {'dof': ['a', 'b', 'c']},
1: {'dof': [f's{i}' for i in range(10)]}}])
def test_cat_bool(labels):
out = torch.randn((3, 10))
out = LabelTensor(out, labels)
selected = out[torch.tensor([True, True, False])]
assert selected.shape == (2, 10)
assert selected.stored_labels[1]['dof'] == [f's{i}' for i in range(10)]
if isinstance(labels, dict):
assert selected.stored_labels[0]['dof'] == ['a', 'b']
Loading