spacr.measure
=============

.. py:module:: spacr.measure




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

.. py:function:: get_components(cell_mask, nucleus_mask, pathogen_mask)

   Map each cell to its enclosed nucleus/pathogen labels via mask lookup.

   :param cell_mask: Label mask of cells.
   :param nucleus_mask: Label mask of nuclei.
   :param pathogen_mask: Label mask of pathogens.
   :returns: Tuple ``(nucleus_df, pathogen_df)`` where each DataFrame has one
       row per (cell, child) pair with columns ``cell_id`` and either
       ``nucleus`` or ``pathogen``.


.. py:function:: save_and_add_image_to_grid(png_channels, img_path, grid, plot=False)

   Add an image to a grid and save it as PNG.

   :param png_channels: The array representing the image channels.
   :type png_channels: ndarray
   :param img_path: The path to save the image as PNG.
   :type img_path: str
   :param grid: The grid of images to be plotted later.
   :type grid: list

   :returns: Updated grid with the new image added.
   :rtype: grid (list)


.. py:function:: img_list_to_grid(grid, titles=None)

   Plot a grid of images with optional titles.

   :param grid: List of images to be plotted.
   :type grid: list
   :param titles: List of titles for the images.
   :type titles: list

   :returns: The matplotlib figure object containing the image grid.
   :rtype: fig (Figure)


.. py:function:: measure_crop(settings)

   Measure the crop of an image based on the provided settings.

   :param settings: The settings for measuring the crop.
   :type settings: dict

   :returns: None


.. py:function:: process_meassure_crop_results(partial_results, settings)

   Process the results, display, and optionally save the figures.

   :param partial_results: List of partial results.
   :type partial_results: list
   :param settings: Settings dictionary.
   :type settings: dict
   :param save_figures: Flag to save figures or not.
   :type save_figures: bool


.. py:function:: generate_cellpose_train_set(folders, dst, min_objects=5)

   Copy image/mask pairs from source folders into a Cellpose training set.

   Only pairs whose mask contains at least ``min_objects`` labeled objects
   (background label 0 excluded) are copied. Files are renamed with their
   source folder name as prefix to avoid collisions.

   :param folders: Iterable of source folders, each containing a ``masks/``
       subfolder and the raw images alongside it.
   :param dst: Destination folder; ``imgs/`` and ``masks/`` subfolders are
       created if missing.
   :param min_objects: Minimum number of unique object labels required in a
       mask for the pair to be included. Default ``5``.
   :returns: None.


.. py:function:: get_object_counts(src)

   Return per-count-type totals and per-file averages from the measurements DB.

   Reads the ``object_counts`` table from ``<src>/measurements/measurements.db``
   and aggregates by ``count_type``.

   :param src: Path to the run folder containing ``measurements/measurements.db``.
   :returns: DataFrame with columns ``count_type``, ``total_object_count``, and
       ``avg_object_count_per_file_name``.


