# computational-cell-analytics/micro-sam architecture

> Architecture diagram of the GitHub repository computational-cell-analytics/micro-sam, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-03-30). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/computational-cell-analytics/micro-sam
- Repository: https://github.com/computational-cell-analytics/micro-sam

## Overview

##### Main Components
- Core Library: The micro_sam package itself. Within it, identify the various modules:
– Models (in micro_sam/models): where SAM models are built or wrapped (including 3D and PEFT variants).
– Inference and segmentation: modules like micro_sam/automatic_segmentation.py, micro_sam/instance_segmentation.py, micro_sam/multi_dimensional_segmentation.py.
– Annotation Tools: the micro_sam/sam_annotator package contains interactive annotators for 2D, 3D, tracking, and image series.
– BioimageIO Integration: subdirectory micro_sam/bioimageio handles export and adapter functionality.
– Training and Fine-tuning: the micro_sam/training package is responsible for training routines, along with the separate finetuning directories (finetuning/, finetuning/generalists, finetuning/specialists, etc.) for different use cases.
– Utility and Evaluation: supporting modules for precomputation, evaluation (micro_sam/evaluation), visualization (micro_sam/visualization), and helper utilities.

- User Interfaces and Interaction Layers:
– napari Integration: Although not a separate directory, the README emphasizes interactive segmentation with napari. Hence, clearly mark the GUI components (e.g., the sam_annotator interactive tools) which connect with napari’s ecosystem.

- Deployment and CI/CD:
– The .github workflows and deployment directories highlight build, test, and deployment pipelines. Identify these as separate “DevOps/CI/CD” components that trigger unit testing, documentation building, installer creation, etc.

- Examples, Scripts, and Notebooks:
– The examples, scripts, and notebooks directories provide usage examples (e.g., annotator_2d.py, annotator_3d.py) and benchmarking. These can be grouped as “Client/Demo Applications” showing how end users interact with the core modules.

- Fine-tuning and Evaluation Tools:
– Multiple directories (finetuning, scripts/for_benchmarking_ais, scripts/plotting, workshops) indicate that there are rich experimental pipelines for training, evaluation, and model export.

##### Relationships and Flow Between Components
– The UI layer (napari/sam_annotator) to the core segmentation modules.
– The training/finetuning modules to the models in micro_sam/models.
– The evaluation and benchmarking scripts (scripts, notebooks, test) interacting with the core inference modules.
– The CI/CD pipeline (GitHub workflows, deployment scripts) triggering integration tests, packaging, and release operations.
– Input images and annotations flowing from the UI layer into the segmentation engine.
– Model training outputs (finetuned models) feeding back to inference modules for improved segmentation.
– Communication between export modules (bioimageio) and external tools or publications.

##### Architectural Patterns and Key Technologies
- Modular Structure: Emphasize the separation of concerns where segmentation operations, training routines, and deployments are clearly segregated into specific packages.
- Extensibility: The presence of many “finetuning” and “specialist” directories illustrates an extensible design meant to support different microscopy modalities and experiments.
- Use of Python and CI/CD: Based on the extensive scripts and configuration files (pyproject.toml, environment.yaml, etc.), note that a modern Python ecosystem is used.

##### Essential Architectural Elements
- Detail every major code component and external dependency.
- If possible, highlight sample flows such as “Interactive Segmentation → Model Evaluation → Finetuning → Updated Model Export.”

– The core segmentation engine (micro_sam) with its submodules (models, inference, annotation tools, training).
– The user interface integration layer (napari-based interactive annotation).
– The data flow connecting image inputs, segmentation processing, and model updates.
– The training and fine-tuning pipelines (with experimental, specialist, and generalist pathways).
– The evaluation/benchmarking scripts and external integration points (e.g., BioimageIO, CI/CD workflows).
– CI/CD, deployment, and testing frameworks that ensure robust and reproducible builds.

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Core Segmentation Engine
    subgraph "Core Segmentation Engine"
        direction TB
        A1["Core Library (micro_sam)"]:::core
        subgraph "Model Management"
            direction TB
            A2["SAM Models (micro_sam/models)"]:::core
        end
        subgraph "Inference Modules"
            direction TB
            A3["Automatic Segmentation (micro_sam/automatic_segmentation.py)"]:::core
            A4["Instance Segmentation (micro_sam/instance_segmentation.py)"]:::core
            A5["Multi-dimensional Segmentation (micro_sam/multi_dimensional_segmentation.py)"]:::core
        end
        subgraph "Annotation Tools"
            direction TB
            A6["Interactive Annotation (sam_annotator)"]:::ui
            A7["napari Integration (micro_sam/napari.yaml)"]:::ui
        end
        subgraph "Export & Adapter"
            direction TB
            A8["BioimageIO Integration (micro_sam/bioimageio)"]:::core
        end
        subgraph "Training & Fine-Tuning"
            direction TB
            A9["Training Routines (micro_sam/training)"]:::training
            A10["Fine-Tuning Modules (finetuning)"]:::training
        end
        subgraph "Utility & Evaluation"
            direction TB
            A11["Evaluation (micro_sam/evaluation)"]:::evaluation
            A12["Utilities (micro_sam/util.py, micro_sam/visualization.py)"]:::evaluation
        end
    end

    %% DevOps & CI/CD
    subgraph "DevOps & CI/CD"
        direction TB
        B1["CI/CD Pipelines (.github/workflows)"]:::devops
        B2["Deployment Scripts (deployment)"]:::devops
    end

    %% Client/Demo Applications
    subgraph "Client/Demo Applications"
        direction TB
        C1["Usage Examples (examples)"]:::client
        C2["Benchmarking & Scripts (scripts)"]:::client
        C3["Notebooks (notebooks)"]:::client
    end

    %% Connections inside Core Segmentation Engine
    A6 -->|"users_input"| A3
    A6 -->|"users_input"| A4
    A6 -->|"users_input"| A5
    A3 -->|"segmentation_output"| A11
    A4 -->|"segmentation_output"| A11
    A5 -->|"segmentation_output"| A11
    A9 -->|"updates_models"| A2
    A10 -->|"fine_tuned_models"| A2
    A2 -->|"used_in_inference"| A3
    A2 -->|"used_in_inference"| A4
    A2 -->|"used_in_inference"| A5
    A8 -->|"export_data"| C2
    A6 -->|"interactive_export"| A8

    %% Cross-layer Connections
    B1 -->|"trigger_tests"| A1
    B1 -->|"trigger_tests"| A11
    B2 -->|"deployment"| A1
    C2 -->|"benchmarking"| A11

    %% Click Events
    click A1 "https://github.com/computational-cell-analytics/micro-sam/tree/master/micro_sam/"
    click A2 "https://github.com/computational-cell-analytics/micro-sam/tree/master/micro_sam/models"
    click A3 "https://github.com/computational-cell-analytics/micro-sam/blob/master/micro_sam/automatic_segmentation.py"
    click A4 "https://github.com/computational-cell-analytics/micro-sam/blob/master/micro_sam/instance_segmentation.py"
    click A5 "https://github.com/computational-cell-analytics/micro-sam/blob/master/micro_sam/multi_dimensional_segmentation.py"
    click A6 "https://github.com/computational-cell-analytics/micro-sam/tree/master/micro_sam/sam_annotator"
    click A7 "https://github.com/computational-cell-analytics/micro-sam/blob/master/micro_sam/napari.yaml"
    click A8 "https://github.com/computational-cell-analytics/micro-sam/tree/master/micro_sam/bioimageio"
    click A9 "https://github.com/computational-cell-analytics/micro-sam/tree/master/micro_sam/training"
    click A10 "https://github.com/computational-cell-analytics/micro-sam/tree/master/finetuning"
    click A11 "https://github.com/computational-cell-analytics/micro-sam/tree/master/micro_sam/evaluation"
    click A12 "https://github.com/computational-cell-analytics/micro-sam/blob/master/micro_sam/util.py"
    click B1 "https://github.com/computational-cell-analytics/micro-sam/tree/master/.github/workflows"
    click B2 "https://github.com/computational-cell-analytics/micro-sam/tree/master/deployment"
    click C1 "https://github.com/computational-cell-analytics/micro-sam/tree/master/examples"
    click C2 "https://github.com/computational-cell-analytics/micro-sam/tree/master/scripts"
    click C3 "https://github.com/computational-cell-analytics/micro-sam/tree/master/notebooks"

    %% Styles
    classDef core fill:#cce5ff,stroke:#002a54,stroke-width:2px;
    classDef ui fill:#d4edda,stroke:#155724,stroke-width:2px;
    classDef training fill:#fff3cd,stroke:#856404,stroke-width:2px;
    classDef evaluation fill:#f8d7da,stroke:#721c24,stroke-width:2px;
    classDef devops fill:#d1ecf1,stroke:#0c5460,stroke-width:2px;
    classDef client fill:#e2e3e5,stroke:#41464b,stroke-width:2px;
```
