Skip to content

Commit 1eb1bbb

Browse files
committed
Napari support for labeling components
1 parent 0409d17 commit 1eb1bbb

File tree

2 files changed

+21
-2
lines changed

2 files changed

+21
-2
lines changed

flamingo_tools/postprocessing/label_components.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -750,6 +750,7 @@ def label_components_single(
750750
s3_bucket_name: Optional[str] = None,
751751
s3_service_endpoint: Optional[str] = None,
752752
custom_dic: Optional[dict] = None,
753+
use_napari: bool = False,
753754
**_
754755
):
755756
"""Process a single cochlea using one set of parameters or a custom dictionary.
@@ -774,6 +775,7 @@ def label_components_single(
774775
s3_service_endpoint:
775776
custom_dic: Custom dictionary which allows multiple post-processing configurations and combines the
776777
results into final components.
778+
use_napari: Visualize component labels with napari viewer.
777779
"""
778780
if os.path.isdir(out_path):
779781
raise ValueError(f"Output path {out_path} is a directory. Provide a path to a single output file.")
@@ -820,3 +822,17 @@ def label_components_single(
820822
print(f"Custom component(s) have {custom_comp} {cell_type.upper()}s.")
821823

822824
tsv_table.to_csv(out_path, sep="\t", index=False)
825+
826+
if use_napari:
827+
import napari
828+
scale_factor = 20
829+
centroids = list(zip(tsv_table["anchor_x"], tsv_table["anchor_y"], tsv_table["anchor_z"]))
830+
component_labels = list(tsv_table["component_labels"])
831+
array_downscaled = downscaled_centroids(centroids=centroids, scale_factor=scale_factor,
832+
component_labels=component_labels, downsample_mode="components")
833+
image_downscaled = downscaled_centroids(centroids, scale_factor=scale_factor,
834+
downsample_mode="accumulated")
835+
viewer = napari.Viewer()
836+
viewer.add_image(image_downscaled, name='3D Volume')
837+
viewer.add_labels(array_downscaled, name="components")
838+
napari.run()

reproducibility/label_components/repro_label_components.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ def wrapper_label_components(
1717
output_path: str,
1818
table_path: Optional[str] = None,
1919
ddict: Optional[str] = None,
20+
use_napari: bool = False,
2021
s3: bool = False,
2122
**kwargs
2223
):
@@ -25,7 +26,7 @@ def wrapper_label_components(
2526
and the explicit setting of parameters.
2627
"""
2728
if ddict is None:
28-
label_components_single(table_path, out_path=output_path, s3=s3, **kwargs)
29+
label_components_single(table_path, out_path=output_path, use_napari=use_napari, s3=s3, **kwargs)
2930
else:
3031
param_dicts = _load_json_as_list(ddict)
3132
for params in param_dicts:
@@ -41,7 +42,7 @@ def wrapper_label_components(
4142
save_path = os.path.join(output_path, "_".join([cochlea_str, f"{table_str}.tsv"]))
4243
else:
4344
save_path = output_path
44-
label_components_single(table_path=table_path, out_path=save_path, s3=s3,
45+
label_components_single(table_path=table_path, out_path=save_path, s3=s3, use_napari=use_napari,
4546
**params)
4647

4748

@@ -65,6 +66,7 @@ def main():
6566
parser.add_argument("--max_edge_distance", type=float, default=30,
6667
help="Maximal distance in micrometer between points to create edges for connected components.")
6768
parser.add_argument("-c", "--components", type=int, nargs="+", default=[1], help="List of connected components.")
69+
parser.add_argument("--napari", action="store_true", help="Use napari viewer to visualize result.")
6870

6971
# options for S3 bucket
7072
parser.add_argument("--s3", action="store_true", help="Flag for using S3 bucket.")
@@ -88,6 +90,7 @@ def main():
8890
min_component_length=args.min_component_length,
8991
min_size=args.min_size,
9092
force_overwrite=args.force,
93+
use_napari=args.napari,
9194
s3=args.s3,
9295
s3_credentials=args.s3_credentials,
9396
s3_bucket_name=args.s3_bucket_name,

0 commit comments

Comments
 (0)