Skip to content

Commit

Permalink
Maint: anchor
Browse files Browse the repository at this point in the history
  • Loading branch information
AhmetNSimsek committed Nov 3, 2023
1 parent 2d1301b commit 96cc202
Showing 1 changed file with 11 additions and 14 deletions.
25 changes: 11 additions & 14 deletions siibra/features/anchor.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ def __init__(self, species: Union[List[Species], Species, str], location: Locati
self._aliases_cached = None

@property
def location(self):
def location(self) -> Location:
# allow to overwrite in derived classes
return self._location_cached

Expand Down Expand Up @@ -105,12 +105,11 @@ def regions(self) -> Dict[Region, AssignmentQualification]:
if self._regions_cached is not None:
return self._regions_cached

elif self._regionspec is None:
self._regions_cached = {}
if self._regionspec is None:
self._regions_cached = dict()
return self._regions_cached

elif self._regionspec not in self.__class__._MATCH_MEMO:
self._regions_cached = {}
if self._regionspec not in self.__class__._MATCH_MEMO:
# decode the region specification into a set of region objects
regions = dict()
for p in Parcellation.registry():
Expand All @@ -126,7 +125,7 @@ def regions(self) -> Dict[Region, AssignmentQualification]:
for r in Parcellation.find_regions(regionspec):
if r.species != alt_species:
continue
if r not in self._regions_cached:
if r not in regions:
regions[r] = AssignmentQualification[qualificationspec.upper()]

self.__class__._MATCH_MEMO[self._regionspec] = regions
Expand All @@ -140,20 +139,18 @@ def __str__(self):
separator = " " if min(len(region), len(location)) > 0 else ""
return region + separator + location

def assign(self, concept: AtlasConcept):
def assign(self, concept: Union[AtlasConcept, Location]):
"""
Match this anchor to a query concept.
Assignments are cached at runtime,
Match this anchor to a query concept. Assignments are cached at runtime,
so repeated assignment with the same concept will be cheap.
"""
if concept not in self._assignments:
matches: List[AnatomicalAssignment] = []
assignments: List[AnatomicalAssignment] = []
if self.location is not None:
matches.append(self.location.assign(concept))
assignments.append(self.location.assign(concept))
for region in self.regions:
matches.append(region.assign(concept))

self._assignments[concept] = sorted(m for m in matches if m is not None)
assignments.append(region.assign(concept))
self._assignments[concept] = sorted(a for a in assignments if a is not None)

self._last_matched_concept = concept \
if len(self._assignments[concept]) > 0 \
Expand Down

0 comments on commit 96cc202

Please sign in to comment.