Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

watershed added #11

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
179 changes: 179 additions & 0 deletions src/methods_segmentation/watershed/config.vsh.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,179 @@
name: watershed
label: "watershed Segmentation"
summary: "Output of the segmantation methot watershed"
description: "Output of the segmantation methot watershed"
links:
documentation: "https://github.com/theislab/txsim"
repository: "https://github.com/BennyStrobes/Watershed"
references:
doi: "10.1109/34.87344"


__merge__: /src/api/comp_method_segmentation.yaml

arguments:
- name: --normalize_func
type: string
default: "gamma"
- name: --normalize_gamma
type: integer
default: 1
- name: --normalize_gain
type: integer
default: 1
- name: --normalize_inv
type: boolean
default: False
- name: --normalize_cutoff
type: double
default: 0.5
- name: --contrast_adjustment_func
type: string
default: "equalize_adapthist"
- name: --contrast_adjustment_kernel_size
type: string
default: "None"
- name: --contrast_adjustment_clip_limit
type: double
default: 0.01
- name: --contrast_adjustment_nbins
type: integer
default: 256
- name: --contrast_adjustment_mask
type: string
default: "None"
- name: --contrast_adjustment_in_range
type: string
default: "image"
- name: --contrast_adjustment_out_range
type: string
default: "dtype"
- name: --blur_func
type: string
default: "gaussian"
- name: --blur_sigma
type: integer
default: 1
- name: --blur_output
type: string
default: "None"
- name: --blur_mode
type: string
default: "nearest"
- name: --blur_cval
type: double
default: 0.0
- name: --blur_preserve_range
type: boolean
default: False
- name: --blur_truncate #keep as double
type: double
default: 4.0
- name: --threshold_func
type: string
default: "local_otsu"
- name: --threshold_nbins
type: integer
default: 256
- name: --threshold_hist
type: string
default: "None"
- name: --threshold_out
type: string
default: "None"
- name: --threshold_mask
type: string
default: "None"
- name: --threshold_shift_x
type: boolean
default: False
- name: --threshold_shift_y
type: boolean
default: False
- name: --threshold_shift_z
type: boolean
default: False
- name: --threshold_footprint
type: string
default: "square"
- name: --threshold_footprint_size
type: integer
default: 50
- name: --distance_transform_func
type: string
default: "distance_transform_edt"
- name: --distance_transform_sampling
type: string
default: "None"
- name: --distance_transform_return_distances
type: boolean
default: True
- name: --distance_transform_return_indices
type: boolean
default: False
- name: --distance_transform_distances
type: string
default: "None"
- name: --distance_transform_indices
type: string
default: "None"
- name: --local_maxima_func
type: string
default: "find_local_maxima"
- name: --local_maxima_min_distance
type: integer
default: 5
- name: --post_processing_func_1
type: string
default: "remove_small_objects"
- name: --post_processing_min_size_1
type: integer
default: 64
- name: --post_processing_connectivity_1
type: integer
default: 1
- name: --post_processing_out_1
type: string
default: "None"
- name: --post_processing_func_2
type: string
default: "remove_small_holes"
- name: --post_processing_area_threshold_2
type: integer
default: 64
- name: --post_processing_connectivity_2
type: integer
default: 1
- name: --post_processing_out_2
type: string
default: "None"
- name: --bg_intensity_filter_bg_factor
type: double
default: 0.3
- name: --bg_intensity_filter_window_size
type: integer
default: 1000
- name: --bg_intensity_filter_bg_size
type: integer
default: 2000

resources:
- type: python_script
path: script.py

engines:
- type: docker
image: openproblems/base_python:1.0.0
setup:
- type: python
pypi: spatialdata
__merge__:
- /src/base/setup_txsim_partial.yaml
- type: native

runners:
- type: executable
- type: nextflow
directives:
label: [ midtime, lowcpu, lowmem ]

57 changes: 57 additions & 0 deletions src/methods_segmentation/watershed/script.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import txsim as tx
import numpy as np
import os
import yaml
import spatialdata as sd
import anndata as ad
import shutil
import numpy as np
from spatialdata.models import Labels2DModel
import xarray as xr


def convert_to_lower_dtype(arr):
max_val = arr.max()
if max_val <= np.iinfo(np.uint8).max:
new_dtype = np.uint8
elif max_val <= np.iinfo(np.uint16).max:
new_dtype = np.uint16
elif max_val <= np.iinfo(np.uint32).max:
new_dtype = np.uint32
else:
new_dtype = np.uint64

return arr.astype(new_dtype)

## VIASH START
par = {
"input": "../task_ist_preprocessing/resources_test/common/2023_10x_mouse_brain_xenium/dataset.zarr",
"output": "segmentation.zarr"
}

## VIASH END

hyperparameters = par.copy()

hyperparameters = {k:(v if v != "None" else None) for k,v in hyperparameters.items()}
del hyperparameters['input']
del hyperparameters['output']

sdata = sd.read_zarr(par["input"])
image = sdata['morphology_mip']['scale0'].image.compute().to_numpy()
transformation = sdata['morphology_mip']['scale0'].image.transform.copy()

sd_output = sd.SpatialData()
image = sdata['morphology_mip']['scale0'].image.compute().to_numpy()
transformation = sdata['morphology_mip']['scale0'].image.transform.copy()
img_arr = tx.preprocessing.segment_watershed(image[0], hyperparameters)
image = convert_to_lower_dtype(img_arr)
data_array = xr.DataArray(image, name=f'segmentation', dims=('y', 'x'))
parsed_data = Labels2DModel.parse(data_array, transformations=transformation)
sd_output.labels['segmentation'] = parsed_data

print("Writing output", flush=True)
if os.path.exists(par["output"]):
shutil.rmtree(par["output"])
sd_output.write(par["output"])