-
Notifications
You must be signed in to change notification settings - Fork 1
add area modules #8
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
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,177 @@ | ||
| """Volume, size, and shape features for 3D objects.""" | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| from collections.abc import Sequence | ||
| from importlib import import_module | ||
| from typing import Protocol | ||
|
|
||
| import numpy as np | ||
|
|
||
| from zedprofiler.exceptions import ZedProfilerError | ||
|
|
||
|
|
||
| class SupportsImageSetLoader(Protocol): | ||
| """Minimal image-set loader interface required by this module.""" | ||
|
|
||
| # voxel size in z,y,x space | ||
| anisotropy_spacing: tuple[float, float, float] | ||
|
|
||
|
|
||
| class SupportsObjectLoader(Protocol): | ||
| """Minimal object loader interface required by this module.""" | ||
|
|
||
| # label image the image which contains the labeled objects, | ||
| # where each object is represented by a unique integer label | ||
| # (0 is typically reserved for background) | ||
| label_image: np.ndarray | ||
| object_ids: Sequence[int] | ||
|
|
||
|
|
||
| def _empty_feature_result() -> dict[str, list[float]]: | ||
| """Return deterministic empty output schema for area/size/shape features.""" | ||
| return { | ||
| "object_id": [], | ||
| "Volume": [], | ||
| "CenterX": [], | ||
| "CenterY": [], | ||
| "CenterZ": [], | ||
| "BboxVolume": [], | ||
| "MinX": [], | ||
| "MaxX": [], | ||
| "MinY": [], | ||
| "MaxY": [], | ||
| "MinZ": [], | ||
| "MaxZ": [], | ||
| "Extent": [], | ||
| "EulerNumber": [], | ||
| "EquivalentDiameter": [], | ||
| "SurfaceArea": [], | ||
| } | ||
|
|
||
|
|
||
| def compute( | ||
| image_set_loader: SupportsImageSetLoader | None = None, | ||
| object_loader: SupportsObjectLoader | None = None, | ||
| ) -> dict[str, list[float]]: | ||
| """Compute volume/size/shape features for one object loader. | ||
|
|
||
| This supports two invocation modes: | ||
| - no arguments: returns an empty deterministic schema so dispatchers can | ||
| call the function without crashing. | ||
| - both loaders provided: executes feature extraction. | ||
| """ | ||
| if image_set_loader is None and object_loader is None: | ||
| return _empty_feature_result() | ||
| if image_set_loader is None or object_loader is None: | ||
| raise ZedProfilerError( | ||
| "volumesizeshape.compute requires both image_set_loader and " | ||
| "object_loader for execution." | ||
| ) | ||
|
|
||
| return measure_3D_volume_size_shape( | ||
| image_set_loader=image_set_loader, | ||
| object_loader=object_loader, | ||
| ) | ||
|
|
||
|
|
||
| def _get_skimage_measure() -> object: | ||
| """Return `skimage.measure` or raise a user-facing dependency error.""" | ||
| try: | ||
| return import_module("skimage.measure") | ||
| except ImportError as exc: | ||
| raise ZedProfilerError( | ||
| "volumesizeshape requires scikit-image for area/size/shape computation." | ||
| ) from exc | ||
|
|
||
|
|
||
| def calculate_surface_area( | ||
| label_object: np.ndarray, | ||
| props: dict[str, np.ndarray], | ||
| spacing: tuple[float, float, float], | ||
| ) -> float: | ||
| """Calculate surface area for one labeled object using marching cubes.""" | ||
| measure = _get_skimage_measure() | ||
|
|
||
| volume = label_object[ | ||
| max(props["bbox-0"][0], 0) : min(props["bbox-3"][0], label_object.shape[0]), | ||
| max(props["bbox-1"][0], 0) : min(props["bbox-4"][0], label_object.shape[1]), | ||
| max(props["bbox-2"][0], 0) : min(props["bbox-5"][0], label_object.shape[2]), | ||
| ] | ||
| volume_truths = volume > 0 | ||
| verts, faces, _normals, _values = measure.marching_cubes( | ||
| volume_truths, | ||
| method="lewiner", | ||
| spacing=spacing, | ||
| level=0, | ||
| ) | ||
| return measure.mesh_surface_area(verts, faces) | ||
|
|
||
|
|
||
| def measure_3D_volume_size_shape( | ||
| image_set_loader: SupportsImageSetLoader, | ||
| object_loader: SupportsObjectLoader, | ||
| ) -> dict[str, list[float]]: | ||
| """Measure volume/size/shape features for each non-zero label object.""" | ||
| measure = _get_skimage_measure() | ||
|
|
||
| label_object = object_loader.label_image | ||
| spacing = image_set_loader.anisotropy_spacing | ||
| unique_objects = object_loader.object_ids | ||
|
|
||
| features_to_record = _empty_feature_result() | ||
|
|
||
| desired_properties = [ | ||
| "area", # for 3D it is volume but skimage uses "area" naming for the property | ||
| "bbox", | ||
| "centroid", | ||
| "bbox_area", | ||
| "extent", | ||
| "euler_number", | ||
| "equivalent_diameter", | ||
| ] | ||
| for label in unique_objects: | ||
| # avoid the 0 index which is the background and not an object, | ||
| if label == 0: | ||
| continue | ||
| subset_lab_object = label_object.copy() | ||
| # subset here means zeroing out all other objects except the | ||
| # one we want to measure, so that we can use | ||
| # skimage's regionprops_table to compute | ||
| # features for that object | ||
| subset_lab_object[subset_lab_object != label] = 0 | ||
| props = measure.regionprops_table( | ||
| subset_lab_object, | ||
| properties=desired_properties, | ||
| ) | ||
|
|
||
| features_to_record["object_id"].append(label) | ||
| features_to_record["Volume"].append(props["area"].item()) | ||
| features_to_record["CenterX"].append(props["centroid-2"].item()) | ||
| features_to_record["CenterY"].append(props["centroid-1"].item()) | ||
| features_to_record["CenterZ"].append(props["centroid-0"].item()) | ||
| features_to_record["BboxVolume"].append(props["bbox_area"].item()) | ||
| features_to_record["MinX"].append(props["bbox-2"].item()) | ||
| features_to_record["MaxX"].append(props["bbox-5"].item()) | ||
| features_to_record["MinY"].append(props["bbox-1"].item()) | ||
| features_to_record["MaxY"].append(props["bbox-4"].item()) | ||
| features_to_record["MinZ"].append(props["bbox-0"].item()) | ||
| features_to_record["MaxZ"].append(props["bbox-3"].item()) | ||
| features_to_record["Extent"].append(props["extent"].item()) | ||
| features_to_record["EulerNumber"].append(props["euler_number"].item()) | ||
| features_to_record["EquivalentDiameter"].append( | ||
| props["equivalent_diameter"].item() | ||
| ) | ||
|
|
||
| try: | ||
| features_to_record["SurfaceArea"].append( | ||
| calculate_surface_area( | ||
| label_object=label_object, | ||
| props=props, | ||
| spacing=spacing, | ||
| ) | ||
| ) | ||
| except (RuntimeError, ValueError): | ||
| features_to_record["SurfaceArea"].append(np.nan) | ||
|
|
||
| return features_to_record |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.