Skip to content

Enable pip install by deferring conda-only imports#1213

Closed
keejkrej wants to merge 5 commits into
computational-cell-analytics:masterfrom
keejkrej:cellacdc
Closed

Enable pip install by deferring conda-only imports#1213
keejkrej wants to merge 5 commits into
computational-cell-analytics:masterfrom
keejkrej:cellacdc

Conversation

@keejkrej

Copy link
Copy Markdown

Summary

This PR makes the core micro-sam Python API importable and usable via pip without requiring conda-forge-only packages (nifty, vigra, python-elf).

Changes:

  • Defer imports of nifty, vigra, and python-elf to the functions that use them
  • Add pip-installable install_requires to setup.cfg (PyPI-available deps only)

Relates to #1113.

Motivation

Several dependencies used by micro-sam are only available on conda-forge. Importing micro_sam.util currently pulls them in at module load time, which breaks pip-only installs — even when the caller only needs the core SAM workflow (load model → compute embeddings → segment from prompts).

This matters for:

  • Integration into applications that manage their own pip environments (e.g. Cell-ACDC)
  • HPC / headless setups without a full conda stack

Why this doesn't affect the core neural network workflow

The SAM inference path (get_sam_model, precompute_image_embeddings, segment_from_points) depends on torch, segment-anything, and standard pip packages. It does not use nifty, vigra, or python-elf.

The deferred packages serve orthogonal infrastructure, not the model itself:

Package Used for When needed
nifty Block tiling for large images Tiled 2D/3D inference only
vigra Eccentricity-based centroids (mode="v") Optional prompt generation
python-elf H5/N5/Zarr I/O, parallel connected components Keyed file loading, mask post-processing

None of these participate in encoder/decoder forward passes. Users on pip who don't need tiling, vigra centroids, or elf I/O can install and run the core workflow without conda. Users who need those features can still conda install -c conda-forge nifty vigra python-elf as today.

Test plan

  • from micro_sam.util import get_sam_model succeeds without nifty/vigra/python-elf installed
  • from micro_sam.prompt_based_segmentation import segment_from_points succeeds without nifty installed
  • Existing conda CI/tests pass unchanged
  • Tiled inference still works when nifty is installed
  • load_image_data(..., key=...) still works when python-elf is installed

Made with Cursor

keejkrej and others added 5 commits May 20, 2026 17:25
- Move nifty.tools blocking import inside functions that use it
- Move vigra import inside get_centers_and_bounding_boxes
- Move elf.io open_file import inside load_image_data
- Avoids import errors when optional deps not installed

Co-authored-by: Cursor <cursoragent@cursor.com>
Co-authored-by: Cursor <cursoragent@cursor.com>
Move project metadata from setup.cfg into pyproject.toml with pip-installable
core deps and optional extras, while lazy-importing nifty, vigra, and python-elf
only where needed so headless imports avoid conda-forge packages.

Co-authored-by: Cursor <cursoragent@cursor.com>
Co-authored-by: Cursor <cursoragent@cursor.com>
Keep packaging changes minimal for Cell-ACDC integration by declaring
pip-only install_requires in setup.cfg instead of duplicating metadata
in pyproject.toml.

Co-authored-by: Cursor <cursoragent@cursor.com>
@constantinpape

Copy link
Copy Markdown
Contributor

Hi @keejkrej ,

thank you for your interest in improving the usability of micro_sam. We are currently working on another solution for this issue though and are creating a dedicated C++ library with all functionality that is needed and that can be installed via pip:
https://github.com/computational-cell-analytics/bioimage-cpp

This has the advantage that all features of micro_sam will be available to pip users. We are already in the process of migrating elf, see constantinpape/elf#141 and will migrate torch_em and then micro_sam next. I expect this to be done in 2-3 weeks.

In general, contributions to micro_sam (or other libraries developed by us) are very welcome, but I recommend to first open an issue for any larger contribution to see if working on this makes sense.

@keejkrej

Copy link
Copy Markdown
Author

Thanks! Very interested in your new lib, for now I'll just install from my fork.

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