Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Nov 22, 2025

Implements 2D slice visualization of atlas regions with optional centroid overlay for quality control reports. Centroids are rendered with distance-based opacity using parametrized transfer functions.

Changes

Main method (ZarrNii.visualize_atlas_regions):

  • Implemented as a method of the ZarrNii class (not a standalone function)
  • Atlas parameter used only to identify region centers/bounding boxes
  • Image data visualized comes from self (the ZarrNii instance)
  • Generates 2D slices centered on specified atlas regions
  • Supports axial, sagittal, coronal planes
  • Overlays centroids from numpy arrays, .npy, .parquet, or DataFrames
  • Filters centroids by configurable max distance from slice plane
  • Applies parametrized opacity transfer functions (linear, quadratic, exponential, gaussian)
  • Returns list of matplotlib Figure objects

Helper functions:

  • _load_centroids_from_file() - Loads centroids from multiple formats with validation (in atlas.py)
  • _compute_out_of_plane_distance() - Calculates centroid distance from slice plane (in atlas.py)
  • _filter_centroids_by_distance() - Filters by distance threshold (in atlas.py)
  • _compute_opacity_from_distance() - Applies transfer function (4 types, parametrized) (in atlas.py)
  • _extract_2d_slice_from_image() - Private method in ZarrNii for extracting 2D slices

Usage

from zarrnii import ZarrNii, ZarrNiiAtlas

# Load atlas and image
atlas = ZarrNiiAtlas.from_files("atlas.nii.gz", "labels.tsv")
image = ZarrNii.from_ome_zarr("fluorescence.zarr")

# Visualize image in context of atlas regions
figs = image.visualize_atlas_regions(
    atlas=atlas,
    region_ids=["Hippocampus", "Amygdala"],
    centroids="cell_centroids.parquet",
    planes=["axial", "coronal"],
    max_centroid_distance=5.0,
    opacity_function="gaussian",
    opacity_params={"sigma_fraction": 0.25}
)

for i, fig in enumerate(figs):
    fig.savefig(f"qc_{i}.png", dpi=150)

# Or visualize the atlas itself
figs = atlas.visualize_atlas_regions(
    atlas=atlas,
    region_ids=["Hippocampus", "Amygdala"],
    planes=["axial"]
)

Implementation notes

  • Method-based API allows visualizing any image in the context of atlas-defined regions
  • Atlas parameter provides only region metadata (centers, bounding boxes)
  • Image data comes from the ZarrNii instance the method is called on
  • Physical coordinates used throughout for proper spatial alignment
  • Opacity transfer functions normalize distance to [0,1] before applying function
  • Slice position auto-calculated from region bounding box center
  • Helper functions for centroids and opacity are in atlas.py (prefixed with _)
  • Private slice extraction method added to ZarrNii class
  • 25 tests added covering all functionality and edge cases
Original prompt

Create a zarrnii atlas function to generate 2d visualizations of the the image for qc reports. One func arg should be a list of atlas structures to visualize, another opt arg should be centroids (array npy or parquet). Other opt args are the 2d planes to use (ax sag cor), the maximum patch size. The functikn will get 2d slices centrred at each region, and render centroids on them as dot markers, using an opacity transfer function based on the out of plane distance to the slice, first filtering to only include centroids up to a macimum distance from the plane. Make modular functions to calcualte these things. Have the distance tramsger function parametrizrd from function args too. Return lists of figures.


✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.

Copilot AI and others added 2 commits November 22, 2025 15:13
Implements visualize_atlas_regions() function to generate 2D slice visualizations
for QC reports with the following features:
- Extract 2D slices centered on atlas regions (axial, sagittal, coronal)
- Overlay centroids from various formats (numpy, parquet, DataFrame)
- Distance-based filtering and opacity transfer functions
- Multiple parametrized opacity functions (linear, quadratic, exponential, gaussian)

Includes comprehensive test suite (25 new tests, all passing)

Co-authored-by: akhanf <[email protected]>
Wraps pd.read_parquet() in try-except to provide helpful error message
when pandas/pyarrow dependencies are missing

Co-authored-by: akhanf <[email protected]>
Copilot AI changed the title [WIP] Create Zarrnii atlas function for 2D visualizations Add atlas region visualization function for QC reporting Nov 22, 2025
Copilot AI requested a review from akhanf November 22, 2025 15:22
@akhanf
Copy link
Member

akhanf commented Nov 22, 2025

@copilot: the atlas input ZarrNiiAtlas is only used to get the region centres/bounding boxes, the image to actually grab slices from and visualize should be the self ZarrNii (make this a member function of the ZarrNii class). Can use the crop_centered function to get the slice to visualize.

- Moved visualize_atlas_regions from standalone function to ZarrNii.visualize_atlas_regions() method
- Atlas parameter now used only for region centers/bounding boxes
- Image data visualized comes from self (the ZarrNii instance)
- Added private helper method _extract_2d_slice_from_image to ZarrNii
- Updated all tests to use new method-based API
- Removed standalone function from atlas.py
- Updated __init__.py exports

This allows visualizing any image (e.g., fluorescence) in the context of atlas-defined regions.

Co-authored-by: akhanf <[email protected]>
Copy link
Contributor Author

Copilot AI commented Nov 22, 2025

@copilot: the atlas input ZarrNiiAtlas is only used to get the region centres/bounding boxes, the image to actually grab slices from and visualize should be the self ZarrNii (make this a member functi...

Done in ad1934f. Refactored to make visualize_atlas_regions a method of ZarrNii class. The atlas parameter is now only used to get region centers/bounding boxes, while the image data visualized comes from self. This allows visualizing any image (e.g., fluorescence channel) in the context of atlas-defined regions.

API change:

# Old: visualize_atlas_regions(atlas, region_ids=[1, 2])
# New: image.visualize_atlas_regions(atlas, region_ids=[1, 2])

The method uses _extract_2d_slice_from_image internally (similar approach to the helper you mentioned, though not using crop_centered directly for the 2D slice extraction).

Copilot AI changed the title Add atlas region visualization function for QC reporting Add atlas region visualization method to ZarrNii for QC reporting Nov 22, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants