Source code for ddd_pylib.labels_manip._labels_to_points_operator

from ddd_pylib._base import MeasurementsOperator
import pandas as pd
from skimage.measure import regionprops
from typing import Any


[docs] class LabelsToPointsOperator(MeasurementsOperator): """ Takes a label map and transforms it into a list of points corresponding to the centroids of the labels. The coordinates of the centroids are expressed in pixels, to which the calibration of the input labels can be applied to convert them into physical units. Produces: result (getResult): The DataFrame containing the centroids of the labels. Each line is a point and each column is an axis. """ def __init__(self): super().__init__()
[docs] def getPrefix(self) -> str: return "AsPoints-"
def _extractCentroids(self, frame): rows = [] complementary = dict() if 'Z' in frame.dims and frame.sizes['Z'] == 1: frame = frame.squeeze(dim='Z') complementary = {'Z': 0.0} lab = frame.values axes = [str(i) for i in frame.dims] for rp in regionprops(lab): pos = {str(a): float(c) for c, a in zip(rp.centroid, axes)} row: dict[str, Any] = { "original label": int(rp.label), "num voxels": int(rp.area) } row.update(pos) row.update(complementary) rows.append(row) return pd.DataFrame(rows) def _applyToFrame(self, frameData, t, nT): labels = frameData[0] return self._extractCentroids(labels)