Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

display atlas images and meshes in pixel space #160

Merged
merged 1 commit into from
Sep 30, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 8 additions & 7 deletions brainrender_napari/napari_atlas_representation.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

@dataclass
class NapariAtlasRepresentation:
"""Representation of a BG atlas as napari layers"""
"""Representation of a BG atlas as napari layers, in pixel space."""

bg_atlas: BrainGlobeAtlas
viewer: Viewer
Expand All @@ -39,37 +39,39 @@ def add_to_viewer(self):
"""
reference = self.viewer.add_image(
self.bg_atlas.reference,
scale=self.bg_atlas.resolution,
name=f"{self.bg_atlas.atlas_name}_reference",
visible=False,
)

annotation = self.viewer.add_labels(
self.bg_atlas.annotation,
scale=self.bg_atlas.resolution,
name=f"{self.bg_atlas.atlas_name}_annotation",
)

annotation.mouse_move_callbacks.append(self._on_mouse_move)
reference.mouse_move_callbacks.append(self._on_mouse_move)

def add_structure_to_viewer(self, structure_name: str):
"""Adds the mesh of a structure to the viewer
"""Adds the mesh of a structure to the viewer.
The mesh will be rescaled to pixel space.

structure_name: the id or acronym of the structure.
"""
mesh = self.bg_atlas.mesh_from_structure(structure_name)
scale = [1.0 / resolution for resolution in self.bg_atlas.resolution]
color = self.bg_atlas.structures[structure_name]["rgb_triplet"]
self._add_mesh(
mesh,
scale,
name=f"{self.bg_atlas.atlas_name}_{structure_name}_mesh",
color=color,
)

def _add_mesh(self, mesh: Mesh, name: str, color=None):
def _add_mesh(self, mesh: Mesh, scale: list, name: str, color=None):
"""Helper function to add a mesh as a surface layer to the viewer.

mesh: the mesh to add
scale: List of scaling factors for each axis
name: name for the surface layer
color: RGB values (0-255) as a list to colour mesh with
"""
Expand All @@ -85,15 +87,14 @@ def _add_mesh(self, mesh: Mesh, name: str, color=None):
viewer_kwargs["vertex_colors"] = np.repeat(
[[float(c) / 255 for c in color]], len(points), axis=0
)
self.viewer.add_surface((points, cells), **viewer_kwargs)
self.viewer.add_surface((points, cells), scale=scale, **viewer_kwargs)

def add_additional_reference(self, additional_reference_key: str):
"""Adds a given additional reference as a layer to the viewer.
and connects it to the mouse move callback to set tooltip.
"""
additional_reference = self.viewer.add_image(
self.bg_atlas.additional_references[additional_reference_key],
scale=self.bg_atlas.resolution,
name=f"{self.bg_atlas.atlas_name}_{additional_reference_key}_reference",
)
additional_reference.mouse_move_callbacks.append(self._on_mouse_move)
Expand Down
Loading