spacr.object
============

.. py:module:: spacr.object




Module Contents
---------------

.. py:function:: merge_split_filter_masks(masks, intensity_images, settings, object_type, batch_filenames=None)

   Apply merge/split/filter operations directly to in-memory masks.

   Skips work when no operation is enabled for ``object_type``; otherwise
   processes each FOV serially so progress reporting stays in order.

   :param masks: 2D/3D ndarray or iterable of 2D masks (one per FOV).
   :param intensity_images: Matching intensity arrays for scoring merges/splits.
   :param settings: Dict of pipeline settings; per-object-type suffixes control
       which operations run (e.g. ``<type>_perimeter_fraction``,
       ``<type>_intensity_merge``, ``<type>_min_area``).
   :param object_type: Label used to look up per-object settings (``'cell'``,
       ``'nucleus'``, ``'pathogen'``, ``'organelle'``).
   :param batch_filenames: Optional per-FOV filenames used only for logging.
   :returns: Original ``masks`` unchanged when no operation is enabled, else a
       list of filtered mask arrays (one per FOV).


.. py:function:: generate_cellpose_masks_sam(src, settings, object_type)

   Segment one object channel across all ``.npz`` batches under ``src`` using Cellpose-SAM.

   Loads the ``cpsam`` pretrained model, iterates over each pre-batched
   ``.npz`` file, runs merge/split/filter on the resulting masks, optionally
   tracks timelapse objects, saves per-image ``.npy`` masks, and records
   per-object counts to the run's SQLite database.

   :param src: Directory containing the pre-batched ``.npz`` image stacks.
   :param settings: Pipeline settings dict; canonicalized via
       :func:`spacr.settings.set_default_settings_preprocess_generate_masks`.
   :param object_type: ``'cell'``, ``'nucleus'``, ``'pathogen'`` or
       ``'organelle'``; drives channel/threshold lookups and output folder name.
   :returns: None.


.. py:function:: generate_cellpose_masks(src, settings, object_type)

   Segment one object channel across all ``.npz`` batches under ``src`` using a chosen Cellpose model.

   Selects the model via :func:`spacr.utils._choose_model` (stock or custom),
   runs per-batch inference with the object-specific channel/threshold
   settings, applies :func:`spacr.utils._filter_cp_masks`, optionally tracks
   timelapse objects, and writes ``.npy`` masks plus per-object counts.

   :param src: Directory containing the pre-batched ``.npz`` image stacks.
   :param settings: Pipeline settings dict; canonicalized via
       :func:`spacr.settings.set_default_settings_preprocess_generate_masks`.
   :param object_type: ``'cell'``, ``'nucleus'``, or ``'pathogen'``; drives
       channel/threshold lookups and output folder name.
   :returns: None.


.. py:function:: generate_organelle_masks_sam(src, settings, object_type)

   Generate organelle masks using one of several morphology-aware strategies.

   Supported morphology modes and backends:

   - ``spots``: punctate structures (lipid droplets, vesicles, peroxisomes) via
     ``otsu``, ``adaptive``, ``log``, ``dog``, ``cellpose``.
   - ``network``: filamentous/reticular structures (mitochondria, microtubules,
     ER tubules) via ``otsu``, ``adaptive``, ``ridge``, ``hysteresis``,
     ``cellpose``, ``unet``.
   - ``irregular``: irregular-shaped organelles (Golgi, ER cisternae, lysosomes)
     via ``otsu``, ``adaptive``, ``cellpose``.
   - ``ring``: hollow/ring-shaped structures (endosomes, autophagosomes) via
     ``otsu``, ``adaptive``, ``dog``, ``log``, ``cellpose``.

   :param src: Path to the mask source directory containing ``.npz`` stacks.
   :param settings: Configuration dict. Organelle-specific keys are prefixed
       with ``organelle_`` and are documented in ``_set_organelle_defaults``.
   :param object_type: Object label (typically ``'organelle'``); drives the
       output folder name ``<object_type>_mask_stack``.
   :returns: None. Masks are written as ``.npy`` files in
       ``<src>/<object_type>_mask_stack/``.


