Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cleanup the get_regionprops_measurements() function in feature_wrapper.py #59

Closed
enricotagliavini opened this issue Apr 18, 2023 · 3 comments · Fixed by #61
Closed

Cleanup the get_regionprops_measurements() function in feature_wrapper.py #59

enricotagliavini opened this issue Apr 18, 2023 · 3 comments · Fixed by #61

Comments

@enricotagliavini
Copy link
Contributor

This function has a lot of branching and it start being hard to read. Let's make it nicer and easier to read.

@jluethi
Copy link
Contributor

jluethi commented Apr 18, 2023

We could split it up into 3 functions:

1 for calculating positions:

# Always include xy positions
coordinate_measurements = {
"x_pos_pix": labeled_obj["centroid"][-1],
"y_pos_pix": labeled_obj["centroid"][-2],
}
label_info.update(coordinate_measurements)
if not is_2D:
coordinate_measurements_3D = {
"z_pos_pix_scaled": labeled_obj["centroid"][-3],
"z_pos_pix_img": labeled_obj["centroid"][-3]
/ spacing_anisotropy_scalar(spacing)
}
label_info.update(coordinate_measurements_3D)

1 for the morphology calculations:

if measure_morphology:
morphology_measurements = {
"is_touching_border_xy": is_touching_border_xy(
labeled_obj, img_shape=img.shape
),
"imgdim_x": img.shape[-1],
"imgdim_y": img.shape[-2],
"area_bbox": labeled_obj["area_bbox"],
"area_convhull": labeled_obj["area_convex"],
"equivDiam": labeled_obj["equivalent_diameter_area"],
"extent": labeled_obj["extent"],
"solidity": labeled_obj["solidity"],
}
# Sometimes, major & minor axis calculations fail with a
# ValueError: math domain error
try:
morphology_measurements["majorAxisLength"] = labeled_obj[
"major_axis_length"
]
morphology_measurements["minorAxisLength"] = labeled_obj[
"minor_axis_length"
]
morphology_measurements["minmajAxisRatio"] = minor_major_axis_ratio(
labeled_obj
)
morphology_measurements[
"aspectRatio_equivalentDiameter"
] = aspect_ratio(labeled_obj)
except ValueError:
morphology_measurements["majorAxisLength"] = np.NaN
morphology_measurements["minorAxisLength"] = np.NaN
morphology_measurements["minmajAxisRatio"] = np.NaN
morphology_measurements["aspectRatio_equivalentDiameter"] = np.NaN
if is_2D:
spacing_2d = spacing_to2d(spacing)
morphology_2D_only = {
"area_pix": labeled_obj["area"],
"perimeter": labeled_obj["perimeter"],
"concavity": convex_hull_area_resid(labeled_obj),
"asymmetry": convex_hull_centroid_dif(labeled_obj, spacing_2d),
"eccentricity": labeled_obj["eccentricity"],
"circularity": circularity(labeled_obj),
"concavity_count": concavity_count(
labeled_obj, min_area_fraction=min_area_fraction
),
"disconnected_components": disconnected_component(
labeled_obj.image
),
}
morphology_measurements.update(morphology_2D_only)
else:
morphology_3d_only = {
"imgdim_z": img.shape[-3],
"is_touching_border_z": is_touching_border_z(
labeled_obj, img_shape=img.shape
),
"volume_pix": labeled_obj["area"],
"surface_area": labeled_obj["surface_area_marchingcube"],
}
morphology_measurements.update(morphology_3d_only)

and 1 for the intensity calculations:

if img is not None:
intensity_measurements = {
"mean_intensity": labeled_obj["mean_intensity"],
"max_intensity": labeled_obj["max_intensity"],
"min_intensity": labeled_obj["min_intensity"],
"percentile25": labeled_obj["fixed_percentiles"][0],
"percentile50": labeled_obj["fixed_percentiles"][1],
"percentile75": labeled_obj["fixed_percentiles"][2],
"percentile90": labeled_obj["fixed_percentiles"][3],
"percentile95": labeled_obj["fixed_percentiles"][4],
"percentile99": labeled_obj["fixed_percentiles"][5],
"stdev": labeled_obj["stdv"],
"skew": labeled_obj["skewness"],
"kurtosis": labeled_obj["kurtos"],
"x_pos_weighted_pix": labeled_obj["weighted_centroid"][-1],
"y_pos_weighted_pix": labeled_obj["weighted_centroid"][-2],
"x_massDisp_pix": labeled_obj["weighted_centroid"][-1]
- labeled_obj["centroid"][-1],
"y_massDisp_pix": labeled_obj["weighted_centroid"][-2]
- labeled_obj["centroid"][-2],
}
# add 3D-specific intensity measurements
if not is_2D:
intensity_3D_only = {
"z_pos_weighted_pix": labeled_obj["weighted_centroid"][-3],
"z_massDisp_pix": labeled_obj["weighted_centroid"][-3]
- labeled_obj["centroid"][-3],
}
intensity_measurements.update(intensity_3D_only)
# channel prefix addition is optional
if channel_prefix is not None:
intensity_measurements_pref = {
channel_prefix + "." + str(key): val
for key, val in intensity_measurements.items()
}
else:
intensity_measurements_pref = intensity_measurements

Each function then returns one dictionary with updates and the main function just calls them & updates the dictionaries.

@jluethi
Copy link
Contributor

jluethi commented Apr 18, 2023

Each of those functions could just take the labeled_obj and a few args (e.g. min_area_fraction, channel_prefix, the img.shape for morphology)

@jluethi
Copy link
Contributor

jluethi commented Apr 18, 2023

Do you want to tackle this @enricotagliavini or shall I have a go at it (as described above) while working on the Fractal task part?

@jluethi jluethi linked a pull request Apr 18, 2023 that will close this issue
@enricotagliavini enricotagliavini changed the title Cleanup the get_regionprops_measurements() funziona in feature_wrapper.py Cleanup the get_regionprops_measurements() function in feature_wrapper.py Apr 18, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants