Skip to content

Commit

Permalink
maint: region.supported_spaces and region.mapped_in_space()`
Browse files Browse the repository at this point in the history
  • Loading branch information
AhmetNSimsek committed Dec 20, 2024
1 parent 628bf71 commit 4b6da2e
Showing 1 changed file with 17 additions and 22 deletions.
39 changes: 17 additions & 22 deletions siibra/core/region.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@

import re
import anytree
from typing import List, Union, Iterable, Dict, Callable, Tuple
from typing import List, Union, Iterable, Dict, Callable, Tuple, Set
from difflib import SequenceMatcher
from ebrains_drive import BucketApiClient
import json
Expand Down Expand Up @@ -111,14 +111,14 @@ def __init__(
)

# anytree node will take care to use this appropriately
self.parent = parent
self.children = children
self.parent: "Region" = parent
self.children: List["Region"] = children
# convert hex to int tuple if rgb is given
self.rgb = (
None if rgb is None
else tuple(int(rgb[p:p + 2], 16) for p in [1, 3, 5])
)
self._supported_spaces = None # computed on 1st call of self.supported_spaces
self._supported_spaces: Set[_space.Space] = None # computed on 1st call of self.supported_spaces
self._str_aliases = None
self.find = lru_cache(maxsize=3)(self.find)

Expand Down Expand Up @@ -459,7 +459,7 @@ def get_regional_mask(
except NoMapAvailableError:
# This region is not mapped directly in any map in the registry.
# Try building a map from the child regions
if (len(self.children) > 0) and all(c.mapped_in_space(space) for c in self.children):
if (len(self.children) > 0) and all(c.mapped_in_space(space, recurse=True) for c in self.children):

Check warning on line 462 in siibra/core/region.py

View check run for this annotation

Codecov / codecov/patch

siibra/core/region.py#L462

Added line #L462 was not covered by tests
logger.info(f"{self.name} is not mapped in {space}. Merging the masks of its {len(self.children)} child regions.")
child_volumes = [
child.get_regional_mask(space=space, maptype=maptype, threshold=threshold)
Expand Down Expand Up @@ -521,16 +521,16 @@ def get_regional_map(
" Please try getting the children or getting the mask."
)

def mapped_in_space(self, space, recurse: bool = True) -> bool:
def mapped_in_space(self, space, recurse: bool = False) -> bool:
"""
Verifies wether this region is defined by an explicit map in the given space.
Parameters
----------
space: Space or str
reference space
recurse: bool, default: True
If True, check if all child regions are mapped instead
recurse: bool, default: False
If True, check if itself or all child regions are mapped instead recursively.
Returns
-------
bool
Expand All @@ -551,23 +551,18 @@ def mapped_in_space(self, space, recurse: bool = True) -> bool:
return False

@property
def supported_spaces(self) -> List[_space.Space]:
def supported_spaces(self) -> Set[_space.Space]:
"""
The set of spaces for which a mask could be extracted.
Overwrites the corresponding method of AtlasConcept.
The set of spaces for which a mask could be extracted from an existing
map or combination of masks of its children.
"""
if self._supported_spaces is None:
self._supported_spaces = sorted(
{s for s in _space.Space.registry() if self.mapped_in_space(s)}
)
self._supported_spaces = {

Check warning on line 560 in siibra/core/region.py

View check run for this annotation

Codecov / codecov/patch

siibra/core/region.py#L560

Added line #L560 was not covered by tests
s for s in _space.Space.registry()
if self.mapped_in_space(s, recurse=True)
}
return self._supported_spaces

def supports_space(self, space: _space.Space):
"""
Return true if this region supports the given space, else False.
"""
return any(s.matches(space) for s in self.supported_spaces)

@property
def spaces(self):
return InstanceTable(
Expand Down Expand Up @@ -610,7 +605,7 @@ def assign(self, other: structure.BrainStructure) -> AnatomicalAssignment:
return self._ASSIGNMENT_CACHE[other, self].invert()

if isinstance(other, (location.Location, volume.Volume)):
if self.mapped_in_space(other.space):
if self.mapped_in_space(other.space, recurse=True):
regionmap = self.get_regional_mask(other.space)
self._ASSIGNMENT_CACHE[self, other] = regionmap.assign(other)
return self._ASSIGNMENT_CACHE[self, other]
Expand Down Expand Up @@ -842,7 +837,7 @@ def __iter__(self):
def intersection(self, other: "location.Location") -> "location.Location":
"""Use this region for filtering a location object."""

if self.supports_space(other.space):
if self.mapped_in_space(other.space, recurse=True):

Check warning on line 840 in siibra/core/region.py

View check run for this annotation

Codecov / codecov/patch

siibra/core/region.py#L840

Added line #L840 was not covered by tests
try:
volume = self.get_regional_mask(other.space)
if volume is not None:
Expand Down

0 comments on commit 4b6da2e

Please sign in to comment.