Skip to content

Commit

Permalink
Optimized get_structure_mask
Browse files Browse the repository at this point in the history
  • Loading branch information
sdiebolt committed May 20, 2022
1 parent 70335e4 commit 5cfc930
Showing 1 changed file with 9 additions and 8 deletions.
17 changes: 9 additions & 8 deletions bg_atlasapi/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -293,8 +293,9 @@ def get_structure_descendants(self, structure):

def get_structure_mask(self, structure):
"""Returns a stack with the mask for a specific structure (including all
sub-structures). This function is not particularly optimized, and might
take some hundreds of ms per run for some structures.
sub-structures).
This function might take a few seconds for structures with many children.
Parameters
----------
Expand All @@ -305,17 +306,17 @@ def get_structure_mask(self, structure):
-------
np.array
stack containing the mask array.
"""
structure_id = self.structures[structure]["id"]
descendants = self.get_structure_descendants(structure)

mask_stack = np.zeros(self.shape, self.annotation.dtype)
descendant_ids = [
self.structures[descendant]["id"] for descendant in descendants
]
descendant_ids.append(structure_id)

for descendant in descendants:
descendant_id = self.structures[descendant]["id"]
mask_stack[self.annotation == descendant_id] = structure_id
mask_stack[self.annotation == structure_id] = structure_id
mask_stack = np.zeros(self.shape, self.annotation.dtype)
mask_stack[np.isin(self.annotation, descendant_ids)] = structure_id

return mask_stack

Expand Down

0 comments on commit 5cfc930

Please sign in to comment.