ó
    voi(0  ã                   óX   • S SK rS SKJr  SSKJrJr  / SQrS
S jrS
S jr	S
S jr
S
S	 jrg)é    N)Úpearsonré   )Úcheck_shape_equalityÚas_binary_ndarray)Úpearson_corr_coeffÚmanders_coloc_coeffÚmanders_overlap_coeffÚintersection_coeffc                 ó.  • [         R                  " U 5      n [         R                  " U5      nUb  [        USS9n[        XU5        X   n X   nO-[        X5        U R	                  S5      n UR	                  S5      n[        S [        X5       5       5      $ )u  Calculate Pearson's Correlation Coefficient between pixel intensities
in channels.

Parameters
----------
image0 : (M, N) ndarray
    Image of channel A.
image1 : (M, N) ndarray
    Image of channel 2 to be correlated with channel B.
    Must have same dimensions as `image0`.
mask : (M, N) ndarray of dtype bool, optional
    Only `image0` and `image1` pixels within this region of interest mask
    are included in the calculation. Must have same dimensions as `image0`.

Returns
-------
pcc : float
    Pearson's correlation coefficient of the pixel intensities between
    the two images, within the mask if provided.
p-value : float
    Two-tailed p-value.

Notes
-----
Pearson's Correlation Coefficient (PCC) measures the linear correlation
between the pixel intensities of the two images. Its value ranges from -1
for perfect linear anti-correlation to +1 for perfect linear correlation.
The calculation of the p-value assumes that the intensities of pixels in
each input image are normally distributed.

Scipy's implementation of Pearson's correlation coefficient is used. Please
refer to it for further information and caveats [1]_.

.. math::
    r = \frac{\sum (A_i - m_A_i) (B_i - m_B_i)}
    {\sqrt{\sum (A_i - m_A_i)^2 \sum (B_i - m_B_i)^2}}

where
    :math:`A_i` is the value of the :math:`i^{th}` pixel in `image0`
    :math:`B_i` is the value of the :math:`i^{th}` pixel in `image1`,
    :math:`m_A_i` is the mean of the pixel values in `image0`
    :math:`m_B_i` is the mean of the pixel values in `image1`

A low PCC value does not necessarily mean that there is no correlation
between the two channel intensities, just that there is no linear
correlation. You may wish to plot the pixel intensities of each of the two
channels in a 2D scatterplot and use Spearman's rank correlation if a
non-linear correlation is visually identified [2]_. Also consider if you
are interested in correlation or co-occurence, in which case a method
involving segmentation masks (e.g. MCC or intersection coefficient) may be
more suitable [3]_ [4]_.

Providing the mask of only relevant sections of the image (e.g., cells, or
particular cellular compartments) and removing noise is important as the
PCC is sensitive to these measures [3]_ [4]_.

References
----------
.. [1] https://docs.scipy.org/doc/scipy/reference/generated/scipy.stats.pearsonr.html
.. [2] https://docs.scipy.org/doc/scipy/reference/generated/scipy.stats.spearmanr.html
.. [3] Dunn, K. W., Kamocka, M. M., & McDonald, J. H. (2011). A practical
       guide to evaluating colocalization in biological microscopy.
       American journal of physiology. Cell physiology, 300(4), C723â€“C742.
       https://doi.org/10.1152/ajpcell.00462.2010
.. [4] Bolte, S. and CordeliÃ¨res, F.P. (2006), A guided tour into
       subcellular colocalization analysis in light microscopy. Journal of
       Microscopy, 224: 213-232.
       https://doi.org/10.1111/j.1365-2818.2006.01706.x
Úmask©Úvariable_nameéÿÿÿÿc              3   ó8   #   • U  H  n[        U5      v •  M     g 7f©N)Úfloat)Ú.0Úvs     Ú[/var/www/html/land-ocr/venv/lib/python3.13/site-packages/skimage/measure/_colocalization.pyÚ	<genexpr>Ú%pearson_corr_coeff.<locals>.<genexpr>a   s   é € Ð<Ò#;˜a”q—Ò#;ùs   ‚)ÚnpÚasarrayr   r   ÚreshapeÚtupler   )Úimage0Úimage1r   s      r   r   r      s…   € ôL ZŠZ˜Ó€FÜZŠZ˜Ó€FØÑÜ  °VÑ<ˆÜ˜V¨TÔ2Ø‘ˆØ‘‰ä˜VÔ,à—‘ Ó#ˆØ—‘ Ó#ˆäÑ<¤8¨FÔ#;Ó<Ó<Ð<ó    c                 óJ  • [         R                  " U 5      n [        USS9nUb  [        USS9n[        XU5        X   n X   nO[        X5        U R	                  5       S:  a  [        S5      e[         R                  " U 5      nUS:X  a  g[         R                  " X-  5      U-  $ )u­	  Manders' colocalization coefficient between two image channels.

Parameters
----------
image0 : (M, N) ndarray
    Input image (first channel). All pixel values should be non-negative.
image1_mask : (M, N) ndarray of dtype bool
    Binary image giving the regions of interest in the second channel.
    Must have same shape as `image0`.
mask : (M, N) ndarray of dtype bool, optional
    Only `image0` pixel values within `mask` are included in the calculation.
    Must have same shape as `image0`.

Returns
-------
mcc : float
    Manders' colocalization coefficient.

Notes
-----
Manders' colocalization coefficient (MCC) was developed in the context of
confocal biological microscopy, to measure the fraction of colocalizing
objects in each component of a dual-channel image. Out of the total
intensity of, say, channel A, how much is found within the features
(objects) of, say, channel B [1]_? The measure thus ranges from 0 for no
colocalization to 1 for complete colocalization.

MCC is commonly used to measure the colocalization of a particular protein
in a subcelullar compartment. Typically, the mask for channel B is
obtained by thresholding, to segment the features from the background.
In this implementation, channel B is passed directly as a mask
(`image1_mask`), leaving the segmentation step to the user (upstream).

The implemented equation is:

.. math::

   mcc = \frac{\sum_i A_{i,coloc}}{\sum_i A_i}

where

- :math:`A_i` is the value of the :math:`i^{th}` pixel in `image0`, and
- :math:`A_{i, coloc} = A_i B_i`, considering that :math:`B_i` is the
  (``True`` or ``False``) value of the :math:`i^{th}` pixel in
  `image1_mask` cast into int or float (``1`` or ``0``, respectively).

MCC is sensitive to noise, with diffuse signal in the first channel
inflating its value. Therefore, images should be processed beforehand to
remove out-of-focus and background light [2]_.

References
----------
.. [1] Manders, E.M.M., Verbeek, F.J. and Aten, J.A. (1993), Measurement of
       co-localization of objects in dual-colour confocal images. Journal
       of Microscopy, 169: 375-382.
       https://doi.org/10.1111/j.1365-2818.1993.tb03313.x
       https://imagej.net/media/manders.pdf
.. [2] Dunn, K. W., Kamocka, M. M., & McDonald, J. H. (2011). A practical
       guide to evaluating colocalization in biological microscopy.
       American journal of physiology. Cell physiology, 300(4), C723â€“C742.
       https://doi.org/10.1152/ajpcell.00462.2010

Úimage1_maskr   r   r   zimage contains negative values)r   r   r   r   ÚminÚ
ValueErrorÚsum)r   r    r   r#   s       r   r   r   d   sš   € ô@ ZŠZ˜Ó€FÜ# K¸}ÑM€KØÑÜ  °VÑ<ˆÜ˜V°$Ô7Ø‘ˆØ!Ñ'‰ä˜VÔ1à‡zzƒ|aÓÜÐ9Ó:Ð:ä
&Š&‹.€CØ
ˆaƒxØÜ6Š6&Ñ&Ó'¨#Ñ-Ð-r   c                 ó:  • [         R                  " U 5      n [         R                  " U5      nUb  [        USS9n[        XU5        X   n X   nO[        X5        U R	                  5       S:  a  [        S5      eUR	                  5       S:  a  [        S5      e[         R                  " [         R                  " U 5      5      [         R                  " [         R                  " U5      5      -  S-  n[         R                  " [         R                  " X5      5      U-  $ )u
  Manders' overlap coefficient

Parameters
----------
image0 : (M, N) ndarray
    Image of channel A. All pixel values should be non-negative.
image1 : (M, N) ndarray
    Image of channel B. All pixel values should be non-negative.
    Must have same dimensions as `image0`
mask : (M, N) ndarray of dtype bool, optional
    Only `image0` and `image1` pixel values within this region of interest
    mask are included in the calculation.
    Must have â™£same dimensions as `image0`.

Returns
-------
moc: float
    Manders' Overlap Coefficient of pixel intensities between the two
    images.

Notes
-----
Manders' Overlap Coefficient (MOC) is given by the equation [1]_:

.. math::
    r = \frac{\sum A_i B_i}{\sqrt{\sum A_i^2 \sum B_i^2}}

where
    :math:`A_i` is the value of the :math:`i^{th}` pixel in `image0`
    :math:`B_i` is the value of the :math:`i^{th}` pixel in `image1`

It ranges between 0 for no colocalization and 1 for complete colocalization
of all pixels.

MOC does not take into account pixel intensities, just the fraction of
pixels that have positive values for both channels[2]_ [3]_. Its usefulness
has been criticized as it changes in response to differences in both
co-occurence and correlation and so a particular MOC value could indicate
a wide range of colocalization patterns [4]_ [5]_.

References
----------
.. [1] Manders, E.M.M., Verbeek, F.J. and Aten, J.A. (1993), Measurement of
       co-localization of objects in dual-colour confocal images. Journal
       of Microscopy, 169: 375-382.
       https://doi.org/10.1111/j.1365-2818.1993.tb03313.x
       https://imagej.net/media/manders.pdf
.. [2] Dunn, K. W., Kamocka, M. M., & McDonald, J. H. (2011). A practical
       guide to evaluating colocalization in biological microscopy.
       American journal of physiology. Cell physiology, 300(4), C723â€“C742.
       https://doi.org/10.1152/ajpcell.00462.2010
.. [3] Bolte, S. and CordeliÃ¨res, F.P. (2006), A guided tour into
       subcellular colocalization analysis in light microscopy. Journal of
       Microscopy, 224: 213-232.
       https://doi.org/10.1111/j.1365-2818.2006.01
.. [4] Adler J, Parmryd I. (2010), Quantifying colocalization by
       correlation: the Pearson correlation coefficient is
       superior to the Mander's overlap coefficient. Cytometry A.
       Aug;77(8):733-42.https://doi.org/10.1002/cyto.a.20896
.. [5] Adler, J, Parmryd, I. Quantifying colocalization: The case for
       discarding the Manders overlap coefficient. Cytometry. 2021; 99:
       910â€“ 920. https://doi.org/10.1002/cyto.a.24336

r   r   r   zimage0 contains negative valueszimage1 contains negative valuesg      à?)	r   r   r   r   r!   r"   r#   ÚsquareÚmultiply)r   r   r   Údenoms       r   r	   r	   ·   sÙ   € ôB ZŠZ˜Ó€FÜZŠZ˜Ó€FØÑÜ  °VÑ<ˆÜ˜V¨TÔ2Ø‘ˆØ‘‰ä˜VÔ,ð ‡zzƒ|aÓÜÐ:Ó;Ð;Ø‡zzƒ|aÓÜÐ:Ó;Ð;äVŠV”B—I’I˜fÓ%Ó&¬"¯&ª&´·²¸6Ó1BÓ*CÑDÈÑL€EÜ6Š6”"—+’+˜fÓ-Ó.°Ñ6Ð6r   c                 ó  • [        U SS9n [        USS9nUb  [        USS9n[        XU5        X   n X   nO[        X5        [        R                  " U 5      nUS:X  a  g[        R                  " [        R                  " X5      5      nXC-  $ )a  Fraction of a channel's segmented binary mask that overlaps with a
second channel's segmented binary mask.

Parameters
----------
image0_mask : (M, N) ndarray of dtype bool
    Image mask of channel A.
image1_mask : (M, N) ndarray of dtype bool
    Image mask of channel B.
    Must have same dimensions as `image0_mask`.
mask : (M, N) ndarray of dtype bool, optional
    Only `image0_mask` and `image1_mask` pixels within this region of
    interest
    mask are included in the calculation.
    Must have same dimensions as `image0_mask`.

Returns
-------
Intersection coefficient, float
    Fraction of `image0_mask` that overlaps with `image1_mask`.

Úimage0_maskr   r    r   r   )r   r   r   Úcount_nonzeroÚlogical_and)r)   r    r   Únonzero_image0Únonzero_joints        r   r
   r
     s   € ô. $ K¸}ÑM€KÜ# K¸}ÑM€KØÑÜ  °VÑ<ˆÜ˜[°tÔ<Ø!Ñ'ˆØ!Ñ'‰ä˜[Ô6ä×%Ò% kÓ2€NØ˜ÓØÜ×$Ò$¤R§^¢^°KÓ%MÓN€MØÑ)Ð)r   r   )Únumpyr   Úscipy.statsr   Ú_shared.utilsr   r   Ú__all__r   r   r	   r
   © r   r   Ú<module>r3      s/   ðÛ Ý  ç Cò€ôS=ôlP.ôfR7õj%*r   