From d78728e06eef91aaf5a11092e84955b5e9c19dee Mon Sep 17 00:00:00 2001 From: Florian Wuennemann Date: Tue, 21 Mar 2023 14:21:24 +0100 Subject: [PATCH] Added function for summing intensity values. (#43) * Added function for summing intensity values. * Renamed function to intensity_sum * Added documentation for intensity_sum --- README.md | 12 ++++++++---- SingleCellDataExtraction.py | 13 ++++++++++--- 2 files changed, 18 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index 3c5ac15..87ee2ed 100644 --- a/README.md +++ b/README.md @@ -20,9 +20,14 @@ Module for single-cell data extraction given a segmentation mask and multi-chann By default only mean intensity is calculated. If the metric doesn't depend on signal intensity, use `--mask-props` instead. See list at https://scikit-image.org/docs/dev/api/skimage.measure.html#regionprops - Additionally available is gini_index, which calculates a single number + + Currently, the following additional properties can be specified: + + * `--intensity_props gini_index` : The Gini index calculates a single number between 0 and 1, representing how unequal the signal is distributed in each region. - See https://en.wikipedia.org/wiki/Gini_coefficient. For example, to calculate the median intensity, specify `--intensity_props median_intensity`. + See https://en.wikipedia.org/wiki/Gini_coefficient for more information. + * `--intensity_props intensity_median` : Will calculate the median of intensity values per labeled object in the mask. + * `--intensity_props intensity_sum` : Will calculate the sum of intensity values per labelled object in the mask. This can be useful if you want to count RNA molecules from FISH based images for example. # Run script `python CommandSingleCellExtraction.py --masks ./segmentation/cellMask.tif ./segmentation/membraneMask.tif --image ./registration/Exemplar_001.h5 --output ./feature_extraction --channel_names ./my_channels.csv` @@ -32,5 +37,4 @@ Denis Schapiro (https://github.com/DenisSch) Joshua Hess (https://github.com/JoshuaHess12) -Jeremy Muhlich (https://github.com/jmuhlich) - +Jeremy Muhlich (https://github.com/jmuhlich) \ No newline at end of file diff --git a/SingleCellDataExtraction.py b/SingleCellDataExtraction.py index faf29b9..cbbbdb3 100644 --- a/SingleCellDataExtraction.py +++ b/SingleCellDataExtraction.py @@ -12,7 +12,17 @@ from pathlib import Path +#### Additional functions that can be specified by the user via intensity_props +## Function to calculate median intensity values per mask +def intensity_median(mask, intensity): + return np.median(intensity[mask]) + +## Function to sum intensity values (or in this case transcript counts) +def intensity_sum(mask, intensity): + return np.sum(intensity[mask]) + +## Function to calculate the gini index: https://en.wikipedia.org/wiki/Gini_coefficient def gini_index(mask, intensity): x = intensity[mask] sorted_x = np.sort(x) @@ -20,9 +30,6 @@ def gini_index(mask, intensity): cumx = np.cumsum(sorted_x, dtype=float) return (n + 1 - 2 * np.sum(cumx) / cumx[-1]) / n -def intensity_median(mask, intensity): - return np.median(intensity[mask]) - def MaskChannel(mask_loaded, image_loaded_z, intensity_props=["intensity_mean"]): """Function for quantifying a single channel image