Skip to content

Commit a900549

Browse files
authored
Merge pull request #11 from openproblems-bio/add_watershed
watershed added
2 parents 778f2b8 + 5885bda commit a900549

File tree

2 files changed

+236
-0
lines changed

2 files changed

+236
-0
lines changed
Lines changed: 179 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,179 @@
1+
name: watershed
2+
label: "watershed Segmentation"
3+
summary: "Output of the segmentation method watershed"
4+
description: "Output of the segmentation method watershed"
5+
links:
6+
documentation: "https://github.com/openproblems-bio/task_ist_preprocessing"
7+
repository: "https://github.com/theislab/txsim"
8+
references:
9+
doi: "10.1109/34.87344"
10+
11+
12+
__merge__: /src/api/comp_method_segmentation.yaml
13+
14+
arguments:
15+
- name: --normalize_func
16+
type: string
17+
default: "gamma"
18+
- name: --normalize_gamma
19+
type: integer
20+
default: 1
21+
- name: --normalize_gain
22+
type: integer
23+
default: 1
24+
- name: --normalize_inv
25+
type: boolean
26+
default: False
27+
- name: --normalize_cutoff
28+
type: double
29+
default: 0.5
30+
- name: --contrast_adjustment_func
31+
type: string
32+
default: "equalize_adapthist"
33+
- name: --contrast_adjustment_kernel_size
34+
type: string
35+
default: "None"
36+
- name: --contrast_adjustment_clip_limit
37+
type: double
38+
default: 0.01
39+
- name: --contrast_adjustment_nbins
40+
type: integer
41+
default: 256
42+
- name: --contrast_adjustment_mask
43+
type: string
44+
default: "None"
45+
- name: --contrast_adjustment_in_range
46+
type: string
47+
default: "image"
48+
- name: --contrast_adjustment_out_range
49+
type: string
50+
default: "dtype"
51+
- name: --blur_func
52+
type: string
53+
default: "gaussian"
54+
- name: --blur_sigma
55+
type: integer
56+
default: 1
57+
- name: --blur_output
58+
type: string
59+
default: "None"
60+
- name: --blur_mode
61+
type: string
62+
default: "nearest"
63+
- name: --blur_cval
64+
type: double
65+
default: 0.0
66+
- name: --blur_preserve_range
67+
type: boolean
68+
default: False
69+
- name: --blur_truncate #keep as double
70+
type: double
71+
default: 4.0
72+
- name: --threshold_func
73+
type: string
74+
default: "local_otsu"
75+
- name: --threshold_nbins
76+
type: integer
77+
default: 256
78+
- name: --threshold_hist
79+
type: string
80+
default: "None"
81+
- name: --threshold_out
82+
type: string
83+
default: "None"
84+
- name: --threshold_mask
85+
type: string
86+
default: "None"
87+
- name: --threshold_shift_x
88+
type: boolean
89+
default: False
90+
- name: --threshold_shift_y
91+
type: boolean
92+
default: False
93+
- name: --threshold_shift_z
94+
type: boolean
95+
default: False
96+
- name: --threshold_footprint
97+
type: string
98+
default: "square"
99+
- name: --threshold_footprint_size
100+
type: integer
101+
default: 50
102+
- name: --distance_transform_func
103+
type: string
104+
default: "distance_transform_edt"
105+
- name: --distance_transform_sampling
106+
type: string
107+
default: "None"
108+
- name: --distance_transform_return_distances
109+
type: boolean
110+
default: True
111+
- name: --distance_transform_return_indices
112+
type: boolean
113+
default: False
114+
- name: --distance_transform_distances
115+
type: string
116+
default: "None"
117+
- name: --distance_transform_indices
118+
type: string
119+
default: "None"
120+
- name: --local_maxima_func
121+
type: string
122+
default: "find_local_maxima"
123+
- name: --local_maxima_min_distance
124+
type: integer
125+
default: 5
126+
- name: --post_processing_func_1
127+
type: string
128+
default: "remove_small_objects"
129+
- name: --post_processing_min_size_1
130+
type: integer
131+
default: 64
132+
- name: --post_processing_connectivity_1
133+
type: integer
134+
default: 1
135+
- name: --post_processing_out_1
136+
type: string
137+
default: "None"
138+
- name: --post_processing_func_2
139+
type: string
140+
default: "remove_small_holes"
141+
- name: --post_processing_area_threshold_2
142+
type: integer
143+
default: 64
144+
- name: --post_processing_connectivity_2
145+
type: integer
146+
default: 1
147+
- name: --post_processing_out_2
148+
type: string
149+
default: "None"
150+
- name: --bg_intensity_filter_bg_factor
151+
type: double
152+
default: 0.3
153+
- name: --bg_intensity_filter_window_size
154+
type: integer
155+
default: 1000
156+
- name: --bg_intensity_filter_bg_size
157+
type: integer
158+
default: 2000
159+
160+
resources:
161+
- type: python_script
162+
path: script.py
163+
164+
engines:
165+
- type: docker
166+
image: openproblems/base_python:1.0.0
167+
setup:
168+
- type: python
169+
pypi: spatialdata
170+
__merge__:
171+
- /src/base/setup_txsim_partial.yaml
172+
- type: native
173+
174+
runners:
175+
- type: executable
176+
- type: nextflow
177+
directives:
178+
label: [ midtime, lowcpu, lowmem ]
179+
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
import txsim as tx
2+
import numpy as np
3+
import os
4+
import yaml
5+
import spatialdata as sd
6+
import anndata as ad
7+
import shutil
8+
import numpy as np
9+
from spatialdata.models import Labels2DModel
10+
import xarray as xr
11+
12+
13+
def convert_to_lower_dtype(arr):
14+
max_val = arr.max()
15+
if max_val <= np.iinfo(np.uint8).max:
16+
new_dtype = np.uint8
17+
elif max_val <= np.iinfo(np.uint16).max:
18+
new_dtype = np.uint16
19+
elif max_val <= np.iinfo(np.uint32).max:
20+
new_dtype = np.uint32
21+
else:
22+
new_dtype = np.uint64
23+
24+
return arr.astype(new_dtype)
25+
26+
## VIASH START
27+
par = {
28+
"input": "../task_ist_preprocessing/resources_test/common/2023_10x_mouse_brain_xenium/dataset.zarr",
29+
"output": "segmentation.zarr"
30+
}
31+
32+
## VIASH END
33+
34+
hyperparameters = par.copy()
35+
36+
hyperparameters = {k:(v if v != "None" else None) for k,v in hyperparameters.items()}
37+
del hyperparameters['input']
38+
del hyperparameters['output']
39+
40+
sdata = sd.read_zarr(par["input"])
41+
image = sdata['morphology_mip']['scale0'].image.compute().to_numpy()
42+
transformation = sdata['morphology_mip']['scale0'].image.transform.copy()
43+
44+
sd_output = sd.SpatialData()
45+
image = sdata['morphology_mip']['scale0'].image.compute().to_numpy()
46+
transformation = sdata['morphology_mip']['scale0'].image.transform.copy()
47+
img_arr = tx.preprocessing.segment_watershed(image[0], hyperparameters)
48+
image = convert_to_lower_dtype(img_arr)
49+
data_array = xr.DataArray(image, name=f'segmentation', dims=('y', 'x'))
50+
parsed_data = Labels2DModel.parse(data_array, transformations=transformation)
51+
sd_output.labels['segmentation'] = parsed_data
52+
53+
print("Writing output", flush=True)
54+
if os.path.exists(par["output"]):
55+
shutil.rmtree(par["output"])
56+
sd_output.write(par["output"])
57+

0 commit comments

Comments
 (0)