From 6bf01649053268c76cbe28d7adbdaad8e38e332b Mon Sep 17 00:00:00 2001 From: Xiao Gui Date: Fri, 11 Mar 2022 16:22:03 +0100 Subject: [PATCH 1/2] extend region model, catch cases where mapIndex is None --- siibra/core/region.py | 5 ++++- test/core/test_region.py | 15 +++++++++++++++ 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/siibra/core/region.py b/siibra/core/region.py index a1ff991e9..24f7975e7 100644 --- a/siibra/core/region.py +++ b/siibra/core/region.py @@ -878,7 +878,10 @@ def vol_to_id_dict(vol: VolumeSrc): if parcvol.volume_type == "neuroglancer/precomputed" and parcvol.space is space] map_idx = self.index.map - if map_idx is not None: + if map_idx is None: + assert len(ng_parc_volumes) == 1, f"if region.index.map is None, expecting only one neuroglancer map, but got {len(ng_parc_volumes)}" + pev.has_annotation.visualized_in = vol_to_id_dict(ng_parc_volumes[0]) + else: pev.has_annotation.visualized_in = vol_to_id_dict(ng_parc_volumes[map_idx]) except IndexError: pass diff --git a/test/core/test_region.py b/test/core/test_region.py index 294992c4e..280437170 100644 --- a/test/core/test_region.py +++ b/test/core/test_region.py @@ -1,6 +1,7 @@ import unittest from unittest.mock import MagicMock import pytest +import re from siibra import parcellations, spaces, retrieval from siibra.core.region import Region @@ -161,5 +162,19 @@ def test_detail_region(parc_spec,region_spec,space_spec,expect_raise): assert model.has_annotation is not None assert model.has_annotation.best_view_point is not None +has_inspired_by = [ + ("julich 2.9", "hoc1 left", "mni152"), + ("long bundle", "Left short cingulate fibres", "mni152") +] + +@pytest.mark.parametrize('parc_spec, region_spec, space_spec', has_inspired_by) +def test_has_inspired_by(parc_spec, region_spec, space_spec): + p = siibra.parcellations[parc_spec] + r = p.decode_region(region_spec) + model = r.to_model(space=siibra.spaces[space_spec]) + assert model.has_annotation.visualized_in is not None, f"expecting has_annotation.visualized_in is defined" + assert re.match(r"^precomputed:\/\/", model.has_annotation.visualized_in["@id"]), f"expecting has_annotation.visualized_in starts with precomputed://" + + if __name__ == "__main__": unittest.main() From 34ef1fb8e3177d7d6fa668f387215859ca6f46c8 Mon Sep 17 00:00:00 2001 From: Xiao Gui Date: Fri, 11 Mar 2022 17:28:19 +0100 Subject: [PATCH 2/2] fix region.index logic --- siibra/core/region.py | 21 +++++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) diff --git a/siibra/core/region.py b/siibra/core/region.py index 24f7975e7..caa0e8afd 100644 --- a/siibra/core/region.py +++ b/siibra/core/region.py @@ -877,12 +877,21 @@ def vol_to_id_dict(vol: VolumeSrc): for parcvol in parc_volumes if parcvol.volume_type == "neuroglancer/precomputed" and parcvol.space is space] - map_idx = self.index.map - if map_idx is None: - assert len(ng_parc_volumes) == 1, f"if region.index.map is None, expecting only one neuroglancer map, but got {len(ng_parc_volumes)}" - pev.has_annotation.visualized_in = vol_to_id_dict(ng_parc_volumes[0]) - else: - pev.has_annotation.visualized_in = vol_to_id_dict(ng_parc_volumes[map_idx]) + + # self.index.label can sometimes be None. e.g. "basal forebrain" + # in such a case, do not populate visualized in + if self.index.label is not None: + + # self.index.map can sometimes be None, but label is defined + if self.index.map is None: + + # In rare instances, e.g. julich brain 2.9, "Ch 123 (Basal Forebrain)" + # self.index.map is undefined (expect a single volume?) + # but there exist multiple volumes (in the example, one for left/ one for right hemisphere) + if len(ng_parc_volumes) == 1: + pev.has_annotation.visualized_in = vol_to_id_dict(ng_parc_volumes[0]) + else: + pev.has_annotation.visualized_in = vol_to_id_dict(ng_parc_volumes[self.index.map]) except IndexError: pass