Skip to content

Commit

Permalink
6846 convert to contiguous seq type (#6849)
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
wyli authored Aug 10, 2023
1 parent 8e99af5 commit 3990cd4
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 6 deletions.
4 changes: 2 additions & 2 deletions monai/transforms/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -1712,8 +1712,8 @@ def convert_to_contiguous(
return ascontiguousarray(data, **kwargs)
elif isinstance(data, Mapping):
return {k: convert_to_contiguous(v, **kwargs) for k, v in data.items()}
elif isinstance(data, Sequence) and not isinstance(data, bytes):
return [convert_to_contiguous(i, **kwargs) for i in data] # type: ignore
elif isinstance(data, Sequence):
return type(data)(convert_to_contiguous(i, **kwargs) for i in data) # type: ignore
else:
return data

Expand Down
9 changes: 5 additions & 4 deletions tests/test_to_contiguous.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@


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

def test_decollation_seq(self):
def test_contiguous_seq(self):
tochange = torch.zeros(2, 3, 4).transpose(0, 1)
test_dict = [[[1]], np.array(0), np.array([0]), torch.tensor(1.0), [[tochange]], "test_string"]
output = convert_to_contiguous(test_dict)
test_seq = [[[1]], np.array(0), np.array([0]), torch.tensor(1.0), [[tochange]], "test_string", (1, 2, 3)]
output = convert_to_contiguous(test_seq)
self.assertEqual(output[0], [[1]])
assert_allclose(output[1], np.array(0))
assert_allclose(output[2], np.array([0]))
assert_allclose(output[3], torch.tensor(1.0))
self.assertTrue(output[4][0][0].is_contiguous())
self.assertEqual(output[5], "test_string")
self.assertEqual(output[6], (1, 2, 3))


if __name__ == "__main__":
Expand Down

0 comments on commit 3990cd4

Please sign in to comment.