Skip to content

Commit 3990cd4

Browse files
authored
6846 convert to contiguous seq type (#6849)
Fixes #6846 ### Types of changes <!--- Put an `x` in all the boxes that apply, and remove the not applicable items --> - [x] Non-breaking change (fix or new feature that would not break existing functionality). - [ ] Breaking change (fix or new feature that would cause existing functionality to change). - [x] New tests added to cover the changes. - [ ] Integration tests passed locally by running `./runtests.sh -f -u --net --coverage`. - [ ] Quick tests passed locally by running `./runtests.sh --quick --unittests --disttests`. - [ ] In-line docstrings updated. - [ ] Documentation updated, tested `make html` command in the `docs/` folder. Signed-off-by: Wenqi Li <wenqil@nvidia.com>
1 parent 8e99af5 commit 3990cd4

File tree

2 files changed

+7
-6
lines changed

2 files changed

+7
-6
lines changed

monai/transforms/utils.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1712,8 +1712,8 @@ def convert_to_contiguous(
17121712
return ascontiguousarray(data, **kwargs)
17131713
elif isinstance(data, Mapping):
17141714
return {k: convert_to_contiguous(v, **kwargs) for k, v in data.items()}
1715-
elif isinstance(data, Sequence) and not isinstance(data, bytes):
1716-
return [convert_to_contiguous(i, **kwargs) for i in data] # type: ignore
1715+
elif isinstance(data, Sequence):
1716+
return type(data)(convert_to_contiguous(i, **kwargs) for i in data) # type: ignore
17171717
else:
17181718
return data
17191719

tests/test_to_contiguous.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121

2222

2323
class TestToContiguous(unittest.TestCase):
24-
def test_decollation_dict(self):
24+
def test_contiguous_dict(self):
2525
tochange = np.moveaxis(np.zeros((2, 3, 4)), 0, -1)
2626
test_dict = {"test_key": [[1]], 0: np.array(0), 1: np.array([0]), "nested": {"nested": [tochange]}}
2727
output = convert_to_contiguous(test_dict)
@@ -30,16 +30,17 @@ def test_decollation_dict(self):
3030
assert_allclose(output[1], np.array([0]))
3131
self.assertTrue(output["nested"]["nested"][0].flags.c_contiguous)
3232

33-
def test_decollation_seq(self):
33+
def test_contiguous_seq(self):
3434
tochange = torch.zeros(2, 3, 4).transpose(0, 1)
35-
test_dict = [[[1]], np.array(0), np.array([0]), torch.tensor(1.0), [[tochange]], "test_string"]
36-
output = convert_to_contiguous(test_dict)
35+
test_seq = [[[1]], np.array(0), np.array([0]), torch.tensor(1.0), [[tochange]], "test_string", (1, 2, 3)]
36+
output = convert_to_contiguous(test_seq)
3737
self.assertEqual(output[0], [[1]])
3838
assert_allclose(output[1], np.array(0))
3939
assert_allclose(output[2], np.array([0]))
4040
assert_allclose(output[3], torch.tensor(1.0))
4141
self.assertTrue(output[4][0][0].is_contiguous())
4242
self.assertEqual(output[5], "test_string")
43+
self.assertEqual(output[6], (1, 2, 3))
4344

4445

4546
if __name__ == "__main__":

0 commit comments

Comments
 (0)