Skip to content

Commit

Permalink
wip:quick and dirty viewer tooltip
Browse files Browse the repository at this point in the history
  • Loading branch information
alessandrofelder committed Aug 29, 2023
1 parent 63ecf2f commit a4e7c7f
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 0 deletions.
26 changes: 26 additions & 0 deletions brainrender_napari/brainrender_widget.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
from brainrender_napari.napari_atlas_representation import (
NapariAtlasRepresentation,
)
from brainrender_napari.utils.from_viewer import structure_from_viewer
from brainrender_napari.widgets.atlas_table_view import AtlasTableView
from brainrender_napari.widgets.structure_view import StructureView

Expand Down Expand Up @@ -97,6 +98,31 @@ def __init__(self, napari_viewer: Viewer):
self._on_add_structure_requested
)

@self._viewer.mouse_move_callbacks.append
def display_region_info(v, event):
"""
Show brain region info on mouse over in status bar on the right
"""
assert self._viewer == v
self.annotations_layer = self._viewer.layers[
-1
] # temporary hacks!!
self.atlas = BrainGlobeAtlas("allen_mouse_100um")
if v.dims.ndisplay == 2:
if len(v.layers) and self.annotations_layer and self.atlas:
_, _, _, region_info = structure_from_viewer(
self._viewer.cursor.position,
self.annotations_layer,
self.atlas,
)
self._viewer.tooltip.visible = True
self._viewer.tooltip.text = region_info
else:
self._viewer.help = ""

print("annotations", self.annotations_layer)
print("help", self._viewer.help)

def _on_download_atlas_confirmed(self, atlas_name):
"""Ensure structure view is displayed if new atlas downloaded."""
show_structure_names = self.show_structure_names.isChecked()
Expand Down
63 changes: 63 additions & 0 deletions brainrender_napari/utils/from_viewer.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
def structure_from_viewer(coordinates, atlas_layer, atlas):
"""
Get brain region info from mouse position in napari viewer.
Return brainglobe (BG) structure number, name, hemisphere, and a
"pretty" string that can be displayed for example in the status bar.
Parameter
---------
coordinates : tuple, nx3 coordinate of cursor position, from
Viewer.cursor.position
atlas_layer : Napari viewer layer
Layer, which contains the annotation / region
information for every structure in the (registered)
atlas
atlas : Brainglobe atlas (bg_atlasapi.bg_atlas.BrainGlobeAtlas)
Returns
-------
region_info : str
A string containing info about structure
and hemisphere
Returns empty string if not found
"""

# Using a regex, extract list of coordinates from status string
assert hasattr(atlas_layer, "data"), "Atlas layer appears to be empty"
assert atlas_layer.data.ndim == 3, (
"Atlas layer data does not have the right dim "
f'("{atlas_layer.data.ndim}")'
)

coord_list = tuple(
[int(x / r) for x, r in zip(coordinates, atlas.resolution)]
)

# Extract structure number
try:
structure_no = atlas_layer.data[coord_list]
except IndexError:
return None, None, None, ""

if structure_no in [0]: # 0 is "Null" region
return None, None, None, ""

# Extract structure information
try:
structure = atlas.structures[structure_no]["name"]
except KeyError:
return None, None, None, ""

# ... and make string pretty
region_info = []
for struct in structure.split(","):
region_info.append(struct.strip().capitalize())
hemisphere = atlas.hemisphere_from_coords(
coord_list, as_string=True
).capitalize()
region_info.append(hemisphere)
region_info = " | ".join(region_info)

return structure_no, structure, hemisphere, region_info

0 comments on commit a4e7c7f

Please sign in to comment.