Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
124 changes: 41 additions & 83 deletions src/cp_measure/core/measureobjectsizeshape.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,13 @@
import numpy
import scipy.ndimage
import skimage.measure
from cp_measure.primitives._moments import (
axes_eccentricity_orientation,
inertia_2d,
moment_feature_dict,
spatial_moments_2d,
)
from cp_measure.primitives.segment import label_to_idx_lut
from cp_measure.utils import masks_to_ijv

__doc__ = """\
Expand Down Expand Up @@ -585,39 +592,25 @@ def get_sizeshape(
"centroid",
"euler_number",
"extent",
"axis_major_length",
"axis_minor_length",
]

# Features not in CellProfiler 4
if new_features:
desired_properties += ["area_filled"]

# 2d specific properties
if masks.ndim == 2:
desired_properties += [
"eccentricity",
"orientation",
"perimeter",
"solidity",
]
# 2D requests NOTHING moment-related from regionprops: the spatial / central / normalized
# / Hu moments, the inertia tensor, AND the axis lengths / eccentricity / orientation are
# all derived from the `spatial_moments_2d` central moments below (option B), so
# regionprops never runs its per-region moment einsum.
desired_properties += ["perimeter", "solidity"]
if new_features:
desired_properties += ["perimeter_crofton"]

if calculate_advanced:
if masks.ndim == 2:
desired_properties += [
"inertia_tensor",
"inertia_tensor_eigvals",
"moments",
"moments_hu",
"moments_central",
"moments_normalized",
]

else:
else:
# 3D: axis lengths still come from regionprops (3D moments are out of scope here).
desired_properties += ["axis_major_length", "axis_minor_length"]
if calculate_advanced:
desired_properties += ["solidity"]

# These advanced props were not in CP4 for 3D images
if new_features:
desired_properties += [
Expand All @@ -629,13 +622,25 @@ def get_sizeshape(
]

labels = masks
nobjects = (numpy.unique(masks) > 0).sum()
nobjects = label_to_idx_lut(masks)[1]
results: dict[str, NDArray[numpy.floating]] = {}
if labels.ndim == 2:
props = skimage.measure.regionprops_table(
labels, pixels, properties=desired_properties
)

# Option B: every moment-derived 2D feature (spatial / central / normalized / Hu moments,
# the inertia tensor, AND axis lengths / eccentricity / orientation) comes from this one
# scatter pass, so regionprops above ran no moment einsum. The normalized / Hu moments are
# only needed under calculate_advanced, so the scatter skips them otherwise.
raw, central, normalized, hu = spatial_moments_2d(
labels, advanced=calculate_advanced
)
inertia = inertia_2d(central)
axis_major, axis_minor, eccentricity, orientation = (
axes_eccentricity_orientation(inertia)
)

formfactor = 4.0 * numpy.pi * props["area"] / props["perimeter"] ** 2
denom = [max(x, 1) for x in 4.0 * numpy.pi * props["area"]]
compactness = props["perimeter"] ** 2 / denom
Expand All @@ -659,10 +664,10 @@ def get_sizeshape(
F_CONVEX_AREA: props["area_convex"],
F_EQUIVALENT_DIAMETER: props["equivalent_diameter_area"],
F_PERIMETER: props["perimeter"],
F_MAJOR_AXIS_LENGTH: props["axis_major_length"],
F_MINOR_AXIS_LENGTH: props["axis_minor_length"],
F_ECCENTRICITY: props["eccentricity"],
F_ORIENTATION: props["orientation"] * (180 / numpy.pi),
F_MAJOR_AXIS_LENGTH: axis_major,
F_MINOR_AXIS_LENGTH: axis_minor,
F_ECCENTRICITY: eccentricity,
F_ORIENTATION: orientation * (180 / numpy.pi),
F_CENTER_X: props["centroid-1"],
F_CENTER_Y: props["centroid-0"],
F_MIN_X: props["bbox-1"],
Expand All @@ -686,61 +691,14 @@ def get_sizeshape(
}

if calculate_advanced:
results |= {
F_SPATIAL_MOMENT_0_0: props["moments-0-0"],
F_SPATIAL_MOMENT_0_1: props["moments-0-1"],
F_SPATIAL_MOMENT_0_2: props["moments-0-2"],
F_SPATIAL_MOMENT_0_3: props["moments-0-3"],
F_SPATIAL_MOMENT_1_0: props["moments-1-0"],
F_SPATIAL_MOMENT_1_1: props["moments-1-1"],
F_SPATIAL_MOMENT_1_2: props["moments-1-2"],
F_SPATIAL_MOMENT_1_3: props["moments-1-3"],
F_SPATIAL_MOMENT_2_0: props["moments-2-0"],
F_SPATIAL_MOMENT_2_1: props["moments-2-1"],
F_SPATIAL_MOMENT_2_2: props["moments-2-2"],
F_SPATIAL_MOMENT_2_3: props["moments-2-3"],
F_CENTRAL_MOMENT_0_0: props["moments_central-0-0"],
F_CENTRAL_MOMENT_0_1: props["moments_central-0-1"],
F_CENTRAL_MOMENT_0_2: props["moments_central-0-2"],
F_CENTRAL_MOMENT_0_3: props["moments_central-0-3"],
F_CENTRAL_MOMENT_1_0: props["moments_central-1-0"],
F_CENTRAL_MOMENT_1_1: props["moments_central-1-1"],
F_CENTRAL_MOMENT_1_2: props["moments_central-1-2"],
F_CENTRAL_MOMENT_1_3: props["moments_central-1-3"],
F_CENTRAL_MOMENT_2_0: props["moments_central-2-0"],
F_CENTRAL_MOMENT_2_1: props["moments_central-2-1"],
F_CENTRAL_MOMENT_2_2: props["moments_central-2-2"],
F_CENTRAL_MOMENT_2_3: props["moments_central-2-3"],
F_NORMALIZED_MOMENT_0_0: props["moments_normalized-0-0"],
F_NORMALIZED_MOMENT_0_1: props["moments_normalized-0-1"],
F_NORMALIZED_MOMENT_0_2: props["moments_normalized-0-2"],
F_NORMALIZED_MOMENT_0_3: props["moments_normalized-0-3"],
F_NORMALIZED_MOMENT_1_0: props["moments_normalized-1-0"],
F_NORMALIZED_MOMENT_1_1: props["moments_normalized-1-1"],
F_NORMALIZED_MOMENT_1_2: props["moments_normalized-1-2"],
F_NORMALIZED_MOMENT_1_3: props["moments_normalized-1-3"],
F_NORMALIZED_MOMENT_2_0: props["moments_normalized-2-0"],
F_NORMALIZED_MOMENT_2_1: props["moments_normalized-2-1"],
F_NORMALIZED_MOMENT_2_2: props["moments_normalized-2-2"],
F_NORMALIZED_MOMENT_2_3: props["moments_normalized-2-3"],
F_NORMALIZED_MOMENT_3_0: props["moments_normalized-3-0"],
F_NORMALIZED_MOMENT_3_1: props["moments_normalized-3-1"],
F_NORMALIZED_MOMENT_3_2: props["moments_normalized-3-2"],
F_NORMALIZED_MOMENT_3_3: props["moments_normalized-3-3"],
F_HU_MOMENT_0: props["moments_hu-0"],
F_HU_MOMENT_1: props["moments_hu-1"],
F_HU_MOMENT_2: props["moments_hu-2"],
F_HU_MOMENT_3: props["moments_hu-3"],
F_HU_MOMENT_4: props["moments_hu-4"],
F_HU_MOMENT_5: props["moments_hu-5"],
F_HU_MOMENT_6: props["moments_hu-6"],
F_INERTIA_TENSOR_0_0: props["inertia_tensor-0-0"],
F_INERTIA_TENSOR_0_1: props["inertia_tensor-0-1"],
F_INERTIA_TENSOR_1_0: props["inertia_tensor-1-0"],
F_INERTIA_TENSOR_1_1: props["inertia_tensor-1-1"],
F_INERTIA_TENSOR_EIGENVALUES_0: props["inertia_tensor_eigvals-0"],
F_INERTIA_TENSOR_EIGENVALUES_1: props["inertia_tensor_eigvals-1"],
}
# Spatial / central / normalized / Hu moments and the inertia tensor all come from the
# scatter `central` + the single `inertia_2d` computed above — regionprops ran no
# moment einsum. moment_feature_dict owns the 53 feature names / orders (shared with
# the numba backend); the 6-tuple passes both off-diagonals (equal, symmetric tensor).
it_00, it_off, it_11, eig_0, eig_1 = inertia
results |= moment_feature_dict(
raw, central, normalized, hu, (it_00, it_off, it_off, it_11, eig_0, eig_1)
)

if new_features:
results |= {
Expand Down
Loading
Loading