diff --git a/monai/transforms/utils.py b/monai/transforms/utils.py index 11608503c7..33d468b86e 100644 --- a/monai/transforms/utils.py +++ b/monai/transforms/utils.py @@ -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 diff --git a/tests/test_to_contiguous.py b/tests/test_to_contiguous.py index 3a57ae6d8b..03733b9775 100644 --- a/tests/test_to_contiguous.py +++ b/tests/test_to_contiguous.py @@ -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) @@ -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__":