Skip to content

Commit

Permalink
Merge pull request #10 from openproblems-bio/segm_m_binning
Browse files Browse the repository at this point in the history
binning method
  • Loading branch information
LouisK92 authored Dec 3, 2024
2 parents 7f6c798 + 98fa8ee commit 9e32a2d
Show file tree
Hide file tree
Showing 2 changed files with 94 additions and 0 deletions.
36 changes: 36 additions & 0 deletions src/methods_segmentation/binning/config.vsh.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
name: binning
label: "Binning Segmentation"
summary: "Segment the spatial data into equidistant square bins"
description: "The binning method for segmentation serves as a baseline of a poor segmentation"
links:
documentation: "https://github.com/openproblems-bio/task_ist_preprocessing"
repository: "https://github.com/openproblems-bio/task_ist_preprocessing"
references:
doi: "10.1101/2023.02.13.528102"

__merge__: /src/api/comp_method_segmentation.yaml

arguments:
- name: --bin_size
type: integer
default: 30

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 ]
58 changes: 58 additions & 0 deletions src/methods_segmentation/binning/script.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
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_binning(image[0], hyperparameters['bin_size']) ### TOdo find the optimal bin_size
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"])

0 comments on commit 9e32a2d

Please sign in to comment.