Skip to content

Commit

Permalink
Raise when invalid space spec is supplied to get_map. Fixes #453
Browse files Browse the repository at this point in the history
  • Loading branch information
AhmetNSimsek committed Oct 24, 2023
1 parent 8161f73 commit 72578ac
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 17 deletions.
13 changes: 10 additions & 3 deletions siibra/core/parcellation.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,12 @@ def matches(self, spec):
return True
return super().matches(spec)

def get_map(self, space=None, maptype: Union[str, MapType] = MapType.LABELLED, spec: str = ""):
def get_map(
self,
space=None,
maptype: Union[str, MapType] = MapType.LABELLED,
spec: str = ""
):
"""
Get the maps for the parcellation in the requested template space.
Expand Down Expand Up @@ -169,15 +174,17 @@ def get_map(self, space=None, maptype: Union[str, MapType] = MapType.LABELLED, s
if not isinstance(maptype, MapType):
maptype = MapType[maptype.upper()]

space_inst = parcellationmap._space.Space.registry()[space]

candidates = [
m for m in parcellationmap.Map.registry()
if m.space.matches(space)
if m.space.matches(space_inst)
and m.maptype == maptype
and m.parcellation
and m.parcellation.matches(self)
]
if len(candidates) == 0:
logger.error(f"No {maptype} map in {space} available for {str(self)}")
logger.error(f"No {maptype} map in '{space}' available for {str(self)}")
return None
if len(candidates) > 1:
spec_candidates = [
Expand Down
26 changes: 15 additions & 11 deletions siibra/volumes/parcellationmap.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
Species,
CompareMapsResult
)
from ..core import concept, space, parcellation, region as _region
from ..core import concept, space as _space, parcellation, region as _region
from ..locations import point, pointset
from ..retrieval import requests

Expand All @@ -45,22 +45,26 @@
from ..core.region import Region


class ExcessiveArgumentException(ValueError): pass
class ExcessiveArgumentException(ValueError):
pass


class InsufficientArgumentException(ValueError): pass
class InsufficientArgumentException(ValueError):
pass


class ConflictingArgumentException(ValueError): pass
class ConflictingArgumentException(ValueError):
pass


class NonUniqueIndexError(RuntimeError): pass
class NonUniqueIndexError(RuntimeError):
pass


@dataclass
class Assignment:
input_structure: int
centroid: Union[Tuple[np.ndarray], point.Point]
centroid: Union[Tuple[np.ndarray], point.Point]
volume: int
fragment: str
map_value: np.ndarray
Expand Down Expand Up @@ -286,8 +290,8 @@ def get_region(self, label: int = None, volume: int = 0, index: MapIndex = None)
def space(self):
for key in ["@id", "name"]:
if key in self._space_spec:
return space.Space.get_instance(self._space_spec[key])
return space.Space(None, "Unspecified space", species=Species.UNSPECIFIED_SPECIES)
return _space.Space.get_instance(self._space_spec[key])
return _space.Space(None, "Unspecified space", species=Species.UNSPECIFIED_SPECIES)

@property
def parcellation(self):
Expand Down Expand Up @@ -794,7 +798,7 @@ def _assign(
minsize_voxel=1,
lower_threshold=0.0,
**kwargs
) -> List[Union[Assignment,AssignImageResult]]:
) -> List[Union[Assignment, AssignImageResult]]:
"""
For internal use only. Returns a dataclass, which provides better static type checking.
"""
Expand All @@ -805,7 +809,7 @@ def _assign(
return self._assign_points(item, lower_threshold)
if isinstance(item, Nifti1Image):
return self._assign_image(item, minsize_voxel, lower_threshold, **kwargs)

raise RuntimeError(
f"Items of type {item.__class__.__name__} cannot be used for region assignment."
)
Expand Down Expand Up @@ -1078,7 +1082,7 @@ def progress(it, N: int = None, desc: str = "", min_elements=5):
seqlen = N or len(it)
return iter(it) if seqlen < min_elements \
else siibra_tqdm(it, desc=desc, total=N)

iter_func = iterate_connected_components if split_components \
else lambda img: [(1, img)]

Expand Down
6 changes: 3 additions & 3 deletions test/volumes/test_parcellationmap.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import unittest
from unittest.mock import patch, MagicMock
from siibra.volumes.parcellationmap import Map, space, parcellation, MapType, MapIndex, ExcessiveArgumentException, InsufficientArgumentException, ConflictingArgumentException, NonUniqueIndexError
from siibra.volumes.parcellationmap import Map, _space, parcellation, MapType, MapIndex, ExcessiveArgumentException, InsufficientArgumentException, ConflictingArgumentException, NonUniqueIndexError
from siibra.commons import Species
from siibra.core.region import Region
from uuid import uuid4
Expand Down Expand Up @@ -95,8 +95,8 @@ def test_space(self, set_space_spec, called_arg, called_get_instance, return_spa

self.map = TestMap.get_instance(space_spec=set_space_spec)

with patch.object(space.Space, 'get_instance') as mock_get_instance:
return_space = space.Space(None, "Returned Space", species=Species.HOMO_SAPIENS) if return_space_flag else None
with patch.object(_space.Space, 'get_instance') as mock_get_instance:
return_space = _space.Space(None, "Returned Space", species=Species.HOMO_SAPIENS) if return_space_flag else None
mock_get_instance.return_value = return_space

actual_returned_space = self.map.space
Expand Down

0 comments on commit 72578ac

Please sign in to comment.