Skip to content

Commit 17bae38

Browse files
committed
Fixed mismatch of pixel distances and physical units
1 parent d26d848 commit 17bae38

File tree

2 files changed

+9
-4
lines changed

2 files changed

+9
-4
lines changed

flamingo_tools/segmentation/postprocessing.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -113,12 +113,12 @@ def neighbors_in_radius(table: pd.DataFrame, radius: float = 15) -> np.ndarray:
113113
#
114114

115115

116-
# FIXME: this computes the distance in pixels, but the MoBIE table contains it in physical units (=nm)
117-
# This is inconsistent.
118-
def _compute_table(segmentation):
116+
def _compute_table(segmentation, resolution):
119117
props = measure.regionprops(segmentation)
120118
label_ids = np.array([prop.label for prop in props])
121119
coordinates = np.array([prop.centroid for prop in props])
120+
# transform pixel distance to physical units
121+
coordinates = coordinates * resolution
122122
sizes = np.array([prop.area for prop in props])
123123
table = pd.DataFrame({
124124
"label_id": label_ids,
@@ -137,6 +137,7 @@ def filter_segmentation(
137137
threshold: float,
138138
min_size: int = 1000,
139139
table: Optional[pd.DataFrame] = None,
140+
resolution: float = 0.38,
140141
output_key: str = "segmentation_postprocessed",
141142
**spatial_statistics_kwargs,
142143
) -> Tuple[int, int]:
@@ -152,6 +153,7 @@ def filter_segmentation(
152153
threshold: Distance in micrometer to check for neighbors
153154
min_size: Minimal number of pixels for filtering small instances
154155
table: Dataframe of segmentation table
156+
resolution: Resolution of segmentation in micrometer
155157
output_key: Output key for postprocessed segmentation
156158
spatial_statistics_kwargs: Arguments for spatial statistics function
157159
@@ -162,7 +164,7 @@ def filter_segmentation(
162164
# Compute the table on the fly.
163165
# NOTE: this currently doesn't work for large segmentations.
164166
if table is None:
165-
table = _compute_table(segmentation)
167+
table = _compute_table(segmentation, resolution=resolution)
166168
n_ids = len(table)
167169

168170
# First apply the size filter.

scripts/prediction/postprocess_seg.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ def main():
2323
help="The key / internal path of the segmentation.")
2424
parser.add_argument("--output_key", type=str, default="segmentation_postprocessed",
2525
help="The key / internal path of the output.")
26+
parser.add_argument('-r', "--resolution", type=float, default=0.38,
27+
help="Resolution of segmentation in micrometer.")
2628

2729
parser.add_argument("--s3_input", type=str, default=None, help="Input file path on S3 bucket.")
2830
parser.add_argument("--s3_credentials", type=str, default=None,
@@ -102,6 +104,7 @@ def create_spatial_statistics_dict(functions, keyword, options, threshold):
102104
spatial_statistics=spatial_statistics,
103105
threshold=threshold,
104106
min_size=args.min_size, table=tsv_table,
107+
resolution=args.resolution,
105108
output_key=args.output_key, **spatial_statistics_kwargs)
106109

107110
print(f"Number of pre-filtered objects: {n_pre}\nNumber of post-filtered objects: {n_post}")

0 commit comments

Comments
 (0)