Skip to content

steinbock.measurement

cellprofiler special

data special

intensities

logger

IntensityAggregation (Enum)

An enumeration.

Source code in steinbock/measurement/intensities.py
class IntensityAggregation(Enum):
    SUM = partial(scipy.ndimage.sum_labels)
    MIN = partial(scipy.ndimage.minimum)
    MAX = partial(scipy.ndimage.maximum)
    MEAN = partial(scipy.ndimage.mean)
    MEDIAN = partial(scipy.ndimage.median)
    STD = partial(scipy.ndimage.standard_deviation)
    VAR = partial(scipy.ndimage.variance)

MAX

MEAN

MEDIAN

MIN

STD

SUM

VAR

measure_intensites(img, mask, channel_names, intensity_aggregation)

Source code in steinbock/measurement/intensities.py
def measure_intensites(
    img: np.ndarray,
    mask: np.ndarray,
    channel_names: Sequence[str],
    intensity_aggregation: IntensityAggregation,
) -> pd.DataFrame:
    object_ids = np.unique(mask[mask != 0])
    data = {
        channel_name: intensity_aggregation.value(img[i], labels=mask, index=object_ids)
        for i, channel_name in enumerate(channel_names)
    }
    return pd.DataFrame(
        data=data,
        index=pd.Index(object_ids, dtype=io.mask_dtype, name="Object"),
    )

try_measure_intensities_from_disk(img_files, mask_files, channel_names, intensity_aggregation, mmap=False)

Source code in steinbock/measurement/intensities.py
def try_measure_intensities_from_disk(
    img_files: Sequence[Union[str, PathLike]],
    mask_files: Sequence[Union[str, PathLike]],
    channel_names: Sequence[str],
    intensity_aggregation: IntensityAggregation,
    mmap: bool = False,
) -> Generator[Tuple[Path, Path, pd.DataFrame], None, None]:
    for img_file, mask_file in zip(img_files, mask_files):
        try:
            if mmap:
                img = io.mmap_image(img_file)
                mask = io.mmap_mask(mask_file)
            else:
                img = io.read_image(img_file)
                mask = io.read_mask(mask_file)
            intensities = measure_intensites(
                img, mask, channel_names, intensity_aggregation
            )
            del img, mask
            yield Path(img_file), Path(mask_file), intensities
            del intensities
        except Exception as e:
            logger.exception(f"Error measuring intensities in {img_file}: {e}")

neighbors

logger

NeighborhoodType (Enum)

An enumeration.

Source code in steinbock/measurement/neighbors.py
class NeighborhoodType(Enum):
    CENTROID_DISTANCE = partial(_measure_centroid_distance_neighbors)
    EUCLIDEAN_BORDER_DISTANCE = partial(_measure_euclidean_border_distance_neighbors)
    EUCLIDEAN_PIXEL_EXPANSION = partial(_measure_euclidean_pixel_expansion_neighbors)

CENTROID_DISTANCE

EUCLIDEAN_BORDER_DISTANCE

EUCLIDEAN_PIXEL_EXPANSION

SteinbockNeighborsMeasurementException (SteinbockMeasurementException)

Source code in steinbock/measurement/neighbors.py
class SteinbockNeighborsMeasurementException(SteinbockMeasurementException):
    pass

measure_neighbors(mask, neighborhood_type, metric=None, dmax=None, kmax=None)

Source code in steinbock/measurement/neighbors.py
def measure_neighbors(
    mask: np.ndarray,
    neighborhood_type: NeighborhoodType,
    metric: Optional[str] = None,
    dmax: Optional[float] = None,
    kmax: Optional[int] = None,
) -> pd.DataFrame:
    return neighborhood_type.value(mask, metric=metric, dmax=dmax, kmax=kmax)

try_measure_neighbors_from_disk(mask_files, neighborhood_type, metric=None, dmax=None, kmax=None, mmap=False)

Source code in steinbock/measurement/neighbors.py
def try_measure_neighbors_from_disk(
    mask_files: Sequence[Union[str, PathLike]],
    neighborhood_type: NeighborhoodType,
    metric: Optional[str] = None,
    dmax: Optional[float] = None,
    kmax: Optional[int] = None,
    mmap: bool = False,
) -> Generator[Tuple[Path, pd.DataFrame], None, None]:
    for mask_file in mask_files:
        try:
            if mmap:
                mask = io.mmap_mask(mask_file)
            else:
                mask = io.read_mask(mask_file)
            neighbors = measure_neighbors(
                mask, neighborhood_type, metric=metric, dmax=dmax, kmax=kmax
            )
            del mask
            yield Path(mask_file), neighbors
            del neighbors
        except Exception as e:
            logger.exception(f"Error measuring neighbors in {mask_file}: {e}")

regionprops

logger

measure_regionprops(img, mask, skimage_regionprops)

Source code in steinbock/measurement/regionprops.py
def measure_regionprops(
    img: np.ndarray, mask: np.ndarray, skimage_regionprops: Sequence[str]
) -> pd.DataFrame:
    skimage_regionprops = list(skimage_regionprops)
    if "label" not in skimage_regionprops:
        skimage_regionprops.insert(0, "label")
    data = regionprops_table(
        mask,
        intensity_image=np.moveaxis(img, 0, -1),
        properties=skimage_regionprops,
    )
    object_ids = data.pop("label")
    return pd.DataFrame(
        data=data,
        index=pd.Index(object_ids, dtype=io.mask_dtype, name="Object"),
    )

try_measure_regionprops_from_disk(img_files, mask_files, skimage_regionprops, mmap=False)

Source code in steinbock/measurement/regionprops.py
def try_measure_regionprops_from_disk(
    img_files: Sequence[Union[str, PathLike]],
    mask_files: Sequence[Union[str, PathLike]],
    skimage_regionprops: Sequence[str],
    mmap: bool = False,
) -> Generator[Tuple[Path, Path, pd.DataFrame], None, None]:
    for img_file, mask_file in zip(img_files, mask_files):
        try:
            if mmap:
                img = io.mmap_image(img_file)
                mask = io.mmap_mask(mask_file)
            else:
                img = io.read_image(img_file)
                mask = io.read_mask(mask_file)
            regionprops = measure_regionprops(img, mask, skimage_regionprops)
            del img, mask
            yield Path(img_file), Path(mask_file), regionprops
            del regionprops
        except Exception as e:
            logger.exception(f"Error measuring regionprops in {img_file}: {e}")