Skip to content
Merged
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
78 changes: 11 additions & 67 deletions flamingo_tools/segmentation/postprocessing.py
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,7 @@ def erode_subset(
Returns:
The dataframe containing elements left after the erosion.
"""
print("initial length", len(table))
print(f"Initial length: {len(table)}")
n_neighbors = 100
for i in range(iterations):
table = table[table[keyword] < threshold]
Expand Down Expand Up @@ -406,10 +406,12 @@ def components_sgn(
min_cells = 20000
threshold = threshold_erode if threshold_erode is not None else 40

print(f"Using threshold of {threshold} micrometer for eroding segmentation with keyword {keyword}.")

new_subset = erode_subset(table.copy(), iterations=iterations,
threshold=threshold, min_cells=min_cells, keyword=keyword)
if iterations != 0:
print(f"Using threshold of {threshold} micrometer for eroding segmentation with keyword {keyword}.")
new_subset = erode_subset(table.copy(), iterations=iterations,
threshold=threshold, min_cells=min_cells, keyword=keyword)
else:
new_subset = table.copy()

# create graph from coordinates of eroded subset
centroids_subset = list(zip(new_subset["anchor_x"], new_subset["anchor_y"], new_subset["anchor_z"]))
Expand Down Expand Up @@ -486,41 +488,10 @@ def label_components_sgn(
table.sort_values("label_id")

component_labels = [0 for _ in range(len(table))]
table.loc[:, "component_labels"] = component_labels
# be aware of 'label_id' of dataframe starting at 1
for lab, comp in enumerate(components):
for comp_index in comp:
component_labels[comp_index - 1] = lab + 1

return component_labels


def postprocess_sgn_seg(
table: pd.DataFrame,
min_size: int = 1000,
threshold_erode: Optional[float] = None,
min_component_length: int = 50,
max_edge_distance: float = 30,
iterations_erode: Optional[int] = None,
) -> pd.DataFrame:
"""Postprocessing SGN segmentation of cochlea.
Args:
table: Dataframe of segmentation table.
min_size: Minimal number of pixels for filtering small instances.
threshold_erode: Threshold of column value after erosion step with spatial statistics.
min_component_length: Minimal length for filtering out connected components.
max_edge_distance: Maximal distance in micrometer between points to create edges for connected components.
iterations_erode: Number of iterations for erosion, normally determined automatically.
Returns:
Dataframe with component labels.
"""

comp_labels = label_components_sgn(table, min_size=min_size, threshold_erode=threshold_erode,
min_component_length=min_component_length,
max_edge_distance=max_edge_distance, iterations_erode=iterations_erode)

table.loc[:, "component_labels"] = comp_labels
table.loc[table["label_id"].isin(comp), "component_labels"] = lab + 1

return table

Expand Down Expand Up @@ -583,37 +554,10 @@ def label_components_ihc(
length_components, components = zip(*sorted(zip(length_components, components), reverse=True))

component_labels = [0 for _ in range(len(table))]
table.loc[:, "component_labels"] = component_labels
# be aware of 'label_id' of dataframe starting at 1
for lab, comp in enumerate(components):
for comp_index in comp:
component_labels[comp_index - 1] = lab + 1

return component_labels


def postprocess_ihc_seg(
table: pd.DataFrame,
min_size: int = 1000,
min_component_length: int = 50,
max_edge_distance: float = 30,
) -> pd.DataFrame:
"""Postprocessing IHC segmentation of cochlea.
Args:
table: Dataframe of segmentation table.
min_size: Minimal number of pixels for filtering small instances.
min_component_length: Minimal length for filtering out connected components.
max_edge_distance: Maximal distance in micrometer between points to create edges for connected components.
Returns:
Dataframe with component labels.
"""

comp_labels = label_components_ihc(table, min_size=min_size,
min_component_length=min_component_length,
max_edge_distance=max_edge_distance)

table.loc[:, "component_labels"] = comp_labels
table.loc[table["label_id"].isin(comp), "component_labels"] = lab + 1

return table

Expand Down
10 changes: 5 additions & 5 deletions reproducibility/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,14 @@ The extraction of blocks from a 3D volume is required for the creation of annota
Usage:
```
python repro_block_extraction.py --input <JSON-file> --output <out-dir>
```
```

## Post-processing of SGN segmentation
## Labeling components in the segmentation

The post-processing of the SGN segmentation may involve the erosion of the segmentation to exclude artifacts, the variation of the minimal number of nodes within a component, or the minimal distance between nodes to consider them the same component.
The labeling of the SGN segmentation may involve the erosion of the segmentation to exclude artifacts, the variation of the minimal number of nodes within a component, or the minimal distance between nodes to consider them the same component.

Usage:
```
python repro_postprocess_sgn_v1.py --input <JSON-file> --output <out-dir>
```
python repro_label_components.py --input <JSON-file> --output <out-dir>
```

3 changes: 3 additions & 0 deletions reproducibility/block_extraction/ChReef_MLR144R.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
"GFP",
"SGN_v2"
],
"segmentation_channel": "SGN_v2",
"type": "sgn",
"n_blocks": 6,
"crop_centers": [
[
1329,
Expand Down
3 changes: 3 additions & 0 deletions reproducibility/block_extraction/ChReef_MLR145R.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
"GFP",
"SGN_v2"
],
"segmentation_channel": "SGN_v2",
"type": "sgn",
"n_blocks": 6,
"crop_centers": [
[
789,
Expand Down
53 changes: 53 additions & 0 deletions reproducibility/block_extraction/ChReef_MLR155L.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
[
{
"cochlea": "M_LR_000155_L",
"image_channel": [
"PV",
"GFP",
"SGN_v2"
],
"segmentation_channel": "SGN_v2",
"type": "sgn",
"n_blocks": 6,
"crop_centers": [
[
1725,
713,
482
],
[
1395,
810,
389
],
[
1070,
681,
454
],
[
1057,
677,
785
],
[
1121,
1002,
769
],
[
803,
1057,
690
]
],
"halo_size": [
256,
256,
50
],
"component_list": [
1
]
}
]
3 changes: 3 additions & 0 deletions reproducibility/block_extraction/ChReef_MLR155R.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
"GFP",
"SGN_v2"
],
"segmentation_channel": "SGN_v2",
"type": "sgn",
"n_blocks": 6,
"crop_centers": [
[
1634,
Expand Down
26 changes: 26 additions & 0 deletions reproducibility/label_components/IHC_v4c_fig2.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
[
{
"cochlea": "M_LR_000226_L",
"image_channel": "VGlut3",
"cell_type": "ihc",
"unet_version": "v4c"
},
{
"cochlea": "M_LR_000226_R",
"image_channel": "VGlut3",
"cell_type": "ihc",
"unet_version": "v4c"
},
{
"cochlea": "M_LR_000227_L",
"image_channel": "VGlut3",
"cell_type": "ihc",
"unet_version": "v4c"
},
{
"cochlea": "M_LR_000227_R",
"image_channel": "VGlut3",
"cell_type": "ihc",
"unet_version": "v4c"
}
]
77 changes: 77 additions & 0 deletions reproducibility/label_components/SGN_v2_ChReef.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
[
{
"cochlea": "M_LR_000143_L",
"image_channel": "PV",
"cell_type": "sgn",
"max_edge_distance": 70,
"unet_version": "v2"
},
{
"cochlea": "M_LR_000144_L",
"image_channel": "PV",
"cell_type": "sgn",
"max_edge_distance": 50,
"unet_version": "v2"
},
{
"cochlea": "M_LR_000145_L",
"image_channel": "PV",
"cell_type": "sgn",
"unet_version": "v2"
},
{
"cochlea": "M_LR_000153_L",
"image_channel": "PV",
"cell_type": "sgn",
"unet_version": "v2"
},
{
"cochlea": "M_LR_000155_L",
"image_channel": "PV",
"cell_type": "sgn",
"unet_version": "v2"
},
{
"cochlea": "M_LR_000189_L",
"image_channel": "PV",
"cell_type": "sgn",
"unet_version": "v2"
},
{
"cochlea": "M_LR_000143_R",
"image_channel": "PV",
"cell_type": "sgn",
"max_edge_distance": 50,
"unet_version": "v2"
},
{
"cochlea": "M_LR_000144_R",
"image_channel": "PV",
"cell_type": "sgn",
"unet_version": "v2"
},
{
"cochlea": "M_LR_000145_R",
"image_channel": "PV",
"cell_type": "sgn",
"unet_version": "v2"
},
{
"cochlea": "M_LR_000153_R",
"image_channel": "PV",
"cell_type": "sgn",
"unet_version": "v2"
},
{
"cochlea": "M_LR_000155_R",
"image_channel": "PV",
"cell_type": "sgn",
"unet_version": "v2"
},
{
"cochlea": "M_LR_000189_R",
"image_channel": "PV",
"cell_type": "sgn",
"unet_version": "v2"
}
]
Loading
Loading