Skip to content

Commit a29ab04

Browse files
authored
7042 collate common meta dictionary keys (#7054)
Fixes #7042 - handling inconsistent meta keys when collating metatensor ### 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`. - [x] 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 f214b27 commit a29ab04

2 files changed

Lines changed: 10 additions & 2 deletions

File tree

monai/data/utils.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -452,7 +452,11 @@ def collate_meta_tensor(batch):
452452
elem_0 = first(batch)
453453
if isinstance(elem_0, MetaObj):
454454
collated = default_collate(batch)
455-
collated.meta = default_collate([i.meta or TraceKeys.NONE for i in batch])
455+
meta_dicts = [i.meta or TraceKeys.NONE for i in batch]
456+
common_ = set.intersection(*[set(d.keys()) for d in meta_dicts if isinstance(d, dict)])
457+
if common_:
458+
meta_dicts = [{k: d[k] for k in common_} if isinstance(d, dict) else TraceKeys.NONE for d in meta_dicts]
459+
collated.meta = default_collate(meta_dicts)
456460
collated.applied_operations = [i.applied_operations or TraceKeys.NONE for i in batch]
457461
collated.is_batch = True
458462
return collated

tests/test_list_data_collate.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,13 @@
3131
h = (np.array([19, 20, 21]), MetaTensor([22, 23, 24]))
3232
TEST_CASE_2 = [[[e, f], [g, h]], list, torch.Size([4, 3])] # dataset returns a list of tuple data
3333

34+
g_m = (np.array([13, 14, 15]), MetaTensor([16, 7, 18], meta={"key1": 0}))
35+
h_m = (np.array([19, 20, 21]), MetaTensor([22, 23, 24], meta={"key2": 1}))
36+
TEST_CASE_3 = [[[g_m], [h_m]], list, torch.Size([2, 3])]
37+
3438

3539
class TestListDataCollate(unittest.TestCase):
36-
@parameterized.expand([TEST_CASE_1, TEST_CASE_2])
40+
@parameterized.expand([TEST_CASE_1, TEST_CASE_2, TEST_CASE_3])
3741
def test_type_shape(self, input_data, expected_type, expected_shape):
3842
result = list_data_collate(input_data)
3943
self.assertIsInstance(result, expected_type)

0 commit comments

Comments
 (0)