Skip to content

Commit 5487edc

Browse files
cellpose added
1 parent bfe6af5 commit 5487edc

2 files changed

Lines changed: 149 additions & 0 deletions

File tree

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
name: cellpose
2+
3+
__merge__: /src/api/comp_method_segmentation.yaml
4+
5+
arguments:
6+
- name: --batch_size
7+
type: integer
8+
default: 8
9+
- name: --model_type
10+
type: string
11+
default: "cyto"
12+
- name: --channel_axis
13+
type: string
14+
default: "None"
15+
- name: --z_axis
16+
type: string
17+
default: "None"
18+
- name: --normalize
19+
type: boolean
20+
default: True
21+
- name: --invert
22+
type: boolean
23+
default: False
24+
- name: --rescale
25+
type: string
26+
default: "None"
27+
- name: --diameter
28+
type: double
29+
default: 30.0
30+
- name: --do_3D
31+
type: boolean
32+
default: False
33+
- name: --anisotropy
34+
type: string
35+
default: "None"
36+
# - name: --net_avg
37+
# type: boolean
38+
# default: False
39+
- name: --augment
40+
type: boolean
41+
default: False
42+
#- name: --tile
43+
# type: boolean
44+
# default: True
45+
- name: --tile_overlap
46+
type: double
47+
default: 0.1
48+
- name: --resample
49+
type: boolean
50+
default: True
51+
- name: --interp
52+
type: boolean
53+
default: True
54+
- name: --flow_threshold
55+
type: double
56+
default: 0.4
57+
- name: --cellprob_threshold
58+
type: double
59+
default: 0.0
60+
- name: --min_size
61+
type: integer
62+
default: 15
63+
- name: --stitch_threshold
64+
type: double
65+
default: 0.0
66+
67+
resources:
68+
- type: python_script
69+
path: script.py
70+
71+
engines:
72+
- type: docker
73+
image: openproblems/base_python:1.0.0
74+
setup:
75+
- type: python
76+
pypi: spatialdata
77+
__merge__:
78+
- /src/base/setup_txsim_partial.yaml
79+
- type: native
80+
81+
runners:
82+
- type: executable
83+
- type: nextflow
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
import txsim as tx
2+
#from pathlib import Path
3+
#import tifffile
4+
#import squidpy as sq
5+
#from scipy import ndimage
6+
#import skimage.io
7+
#import skimage.measure
8+
#import skimage.segmentation
9+
import numpy as np
10+
#import argparse
11+
import os
12+
import yaml
13+
import spatialdata as sd
14+
import anndata as ad
15+
import shutil
16+
import numpy as np
17+
from spatialdata.models import Labels2DModel
18+
import xarray as xr
19+
import datatree as dt
20+
21+
22+
def convert_to_lower_dtype(arr):
23+
max_val = arr.max()
24+
if max_val <= np.iinfo(np.uint8).max:
25+
new_dtype = np.uint8
26+
elif max_val <= np.iinfo(np.uint16).max:
27+
new_dtype = np.uint16
28+
elif max_val <= np.iinfo(np.uint32).max:
29+
new_dtype = np.uint32
30+
else:
31+
new_dtype = np.uint64
32+
33+
return arr.astype(new_dtype)
34+
35+
## VIASH START
36+
par = {
37+
"input": "../task_ist_preprocessing/resources_test/common/2023_10x_mouse_brain_xenium/dataset.zarr",
38+
"output": "segmentation.zarr"
39+
}
40+
41+
## VIASH END
42+
43+
hyperparameters = par.copy()
44+
45+
hyperparameters = {k:(v if v != "None" else None) for k,v in hyperparameters.items()}
46+
del hyperparameters['input']
47+
del hyperparameters['output']
48+
49+
sdata = sd.read_zarr(par["input"])
50+
image = sdata['morphology_mip']['scale0'].image.compute().to_numpy()
51+
transformation = sdata['morphology_mip']['scale0'].image.transform.copy()
52+
53+
sd_output = sd.SpatialData()
54+
image = sdata['morphology_mip']['scale0'].image.compute().to_numpy()
55+
transformation = sdata['morphology_mip']['scale0'].image.transform.copy()
56+
img_arr = tx.preprocessing.segment_cellpose(image[0], hyperparameters)
57+
image = convert_to_lower_dtype(img_arr)
58+
data_array = xr.DataArray(image, name=f'segmentation', dims=('y', 'x'))
59+
parsed_data = Labels2DModel.parse(data_array, transformations=transformation)
60+
sd_output.labels['segmentation'] = parsed_data
61+
62+
print("Writing output", flush=True)
63+
if os.path.exists(par["output"]):
64+
shutil.rmtree(par["output"])
65+
sd_output.write(par["output"])
66+

0 commit comments

Comments
 (0)