Skip to content

Implement StarDist deep learning segmentation plugin with optional dependencies#66

Draft
Copilot wants to merge 7 commits intomainfrom
copilot/fix-56
Draft

Implement StarDist deep learning segmentation plugin with optional dependencies#66
Copilot wants to merge 7 commits intomainfrom
copilot/fix-56

Conversation

Copy link
Contributor

Copilot AI commented Sep 23, 2025

This PR adds StarDist deep learning segmentation capabilities to ZarrNii, enabling accurate cell and nuclei instance segmentation using pre-trained or custom models.

Key Features

StarDist Plugin Implementation

  • New StarDistSegmentation plugin extending the existing SegmentationPlugin base class
  • Support for both 2D and 3D pre-trained models (2D_versatile_fluo, 2D_versatile_he, 3D_demo)
  • Custom model loading from file paths
  • Configurable GPU acceleration, probability thresholds, and NMS parameters
  • Smart memory management with automatic tile size determination

Optional Dependencies Management

  • StarDist dependencies are optional via zarrnii[stardist] extras
  • Graceful fallback when dependencies are unavailable
  • Proper import error handling with helpful installation messages

Large Image Processing

  • Integration with dask_relabeling for efficient tiled processing of large images
  • Configurable overlap sizes for better edge handling in tiles
  • Automatic detection of when tiled processing should be used based on image size

User-Friendly API

  • Convenience method segment_stardist() added to ZarrNii class
  • Consistent with existing segmentation API patterns
  • Support for multi-channel images (automatically uses first channel)

Usage Examples

from zarrnii import ZarrNii

# Load image
znimg = ZarrNii.from_ome_zarr("image.ome.zarr")

# Basic StarDist segmentation
segmented = znimg.segment_stardist(
    model_name="2D_versatile_fluo",
    prob_thresh=0.5,
    use_gpu=True
)

# For large images with tiled processing
segmented = znimg.segment_stardist(
    model_name="2D_versatile_fluo",
    use_dask_relabeling=True,
    overlap=64
)

# Custom model
segmented = znimg.segment_stardist(
    model_path="/path/to/custom/model"
)

Installation

# Install with StarDist support
pip install zarrnii[stardist]

# Optional: For large image tiled processing
pip install git+https://github.com/TheJacksonLaboratory/dask_relabeling.git

Testing & Quality

  • Comprehensive test suite with 15 test cases covering all functionality
  • Tests gracefully skip when StarDist dependencies are unavailable
  • Proper mocking of external dependencies for reliable CI/CD
  • All code quality checks pass (Black, isort, flake8)
  • Extensive documentation with examples and usage patterns

The implementation maintains backward compatibility and follows existing ZarrNii plugin architecture patterns while adding powerful deep learning segmentation capabilities.

Fixes #56.


💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

Co-authored-by: akhanf <11492701+akhanf@users.noreply.github.com>
Copilot AI changed the title [WIP] Implement a DL module (eg stardist) as a segmentation plugin Implement StarDist deep learning segmentation plugin with optional dependencies Sep 23, 2025
Copilot AI requested a review from akhanf September 23, 2025 16:12
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.

Implement a DL module (eg stardist) as a segmentation plugin

2 participants