Skip to content

Commit

Permalink
maint: bigbrain query, pointset, volume. Volume._points_inside takes …
Browse files Browse the repository at this point in the history
…outside_value param
  • Loading branch information
AhmetNSimsek committed Mar 11, 2024
1 parent 4e22b3f commit 2eb0f70
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 23 deletions.
12 changes: 4 additions & 8 deletions siibra/livequeries/bigbrain.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,15 +87,14 @@ def __init__(self):

def query(self, concept: structure.BrainStructure, **kwargs) -> List[bigbrain_intensity_profile.BigBrainIntensityProfile]:
loader = WagstylProfileLoader()
features = []
mesh_vertices = pointset.PointSet(loader._vertices, space='bigbrain')
matched = concept.intersection(mesh_vertices) # returns a reduced PointSet with og indices as labels
if matched is None:
return []
if isinstance(matched, point.Point):
matched = pointset.from_points([matched])
assert isinstance(matched, pointset.PointSet)
assert matched.labels is not None
indices = matched.labels
assert indices is not None
features = []
for i in matched.labels:
anchor = _anchor.AnatomicalAnchor(
location=point.Point(loader._vertices[i], space='bigbrain'),
Expand Down Expand Up @@ -131,12 +130,9 @@ def query(self, concept: structure.BrainStructure, **kwargs) -> List[layerwise_b
matched = concept.intersection(mesh_vertices) # returns a reduced PointSet with og indices as labels
if matched is None:
return []
if isinstance(matched, point.Point):
matched = pointset.from_points([matched])
assert isinstance(matched, pointset.PointSet)
indices = matched.labels
if indices is None:
return []
assert indices is not None
matched_profiles = loader._profiles[indices, :]
boundary_depths = loader._boundary_depths[indices, :]
# compute array of layer labels for all coefficients in profiles_left
Expand Down
4 changes: 2 additions & 2 deletions siibra/locations/pointset.py
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@ def __str__(self):
@property
def boundingbox(self):
"""Return the bounding box of these points."""
XYZ = self.homogeneous[:, :3]
XYZ = self.coordinates
sigma_min = max(self.sigma[i] for i in XYZ.argmin(0))
sigma_max = max(self.sigma[i] for i in XYZ.argmax(0))
return _boundingbox.BoundingBox(
Expand All @@ -255,7 +255,7 @@ def boundingbox(self):

@property
def centroid(self):
return point.Point(self.homogeneous[:, :3].mean(0), space=self.space)
return point.Point(self.coordinates.mean(0), space=self.space)

@property
def volume(self):
Expand Down
28 changes: 15 additions & 13 deletions siibra/volumes/volume.py
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,10 @@ def __repr__(self):
)

def evaluate_points(
self, points: 'pointset.PointSet', outside_value=0, **fetch_kwargs
self,
points: Union['point.Point', 'pointset.PointSet'],
outside_value: Union[int, float] = 0,
**fetch_kwargs
) -> np.ndarray:
"""
Evaluate the image at the positions of the given points.
Expand Down Expand Up @@ -252,11 +255,9 @@ def evaluate_points(
raise NotImplementedError("Filtering of points by pure mesh volumes not yet implemented.")

# make sure the points are in the same physical space as this volume
if isinstance(points, point.Point): # requires to be a PointSet from next step onwards
as_pointset = pointset.from_points([points])
warped = as_pointset.warp(self.space)
else:
warped = points.warp(self.space)
warped = (
pointset.from_points([points]) if isinstance(points, point.Point) else points
).warp(self.space)
assert warped is not None, SpaceWarpingFailedError

# get the voxel array of this volume
Expand All @@ -282,8 +283,10 @@ def evaluate_points(
return values

def _points_inside(
self, points: Union['point.Point', 'pointset.PointSet'],
self,
points: Union['point.Point', 'pointset.PointSet'],
keep_labels: bool = True,
outside_value: Union[int, float] = 0,
**fetch_kwargs
) -> 'pointset.PointSet':
"""
Expand All @@ -308,12 +311,11 @@ def _points_inside(
Labels reflect the indices of the original points if `keep_labels`
is False.
"""
values = self.evaluate_points(points, **fetch_kwargs)
inside = list(np.where(values != 0)[0])
if isinstance(points, point.Point) and len(inside) == 1:
return pointset.from_points([points], newlabels=[0])
ptset = pointset.from_points([points]) if isinstance(points, point.Point) else points
values = self.evaluate_points(ptset, outside_value=outside_value, **fetch_kwargs)
inside = list(np.where(values != outside_value)[0])
return pointset.from_points(
[points[i] for i in inside],
[ptset[i] for i in inside],
newlabels=None if keep_labels else inside
)

Expand All @@ -334,7 +336,7 @@ def intersection(self, other: structure.BrainStructure, **fetch_kwargs) -> struc
points_inside = self._points_inside(other, keep_labels=False, **fetch_kwargs)
if len(points_inside) == 0:
return None # BrainStructure.intersects checks for not None
if isinstance(other, point.Point):
if isinstance(other, point.Point): # preserve the type
return points_inside[0]
return points_inside
elif isinstance(other, boundingbox.BoundingBox):
Expand Down

0 comments on commit 2eb0f70

Please sign in to comment.