-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathoutput.py
More file actions
63 lines (50 loc) · 1.84 KB
/
output.py
File metadata and controls
63 lines (50 loc) · 1.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
import spatialdata as sd
from tifffile import imwrite
import sys
import numpy as np
import pandas as pd
import xarray as xr
from scipy.sparse import coo_matrix
from pathlib import Path
import os
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)
input_path = sys.argv[1]
input_cellspot = sys.argv[2]
output_path = sys.argv[3]
print(input_path)
sdata = sd.read_zarr(input_path)
sd_output = sd.SpatialData()
transformation = sdata['morphology_mip']['scale0'].image.transform.copy()
scs_output = pd.read_csv(input_cellspot, sep = "\t", header = None)
scs_output.rename(columns = {0: 'spot', 1: 'cell'}, inplace = True)
scs_output[['x', 'y']] = scs_output.spot.str.split(':', n=1, expand=True)
scs_output.x = scs_output.x.astype(int)
scs_output.y = scs_output.y.astype(int)
scs_output.cell = scs_output.cell.astype(int)
##converting back to image-like format
sparse_matrix = coo_matrix(
(scs_output['cell'], (scs_output['x'], scs_output['y'])),
shape=(scs_output.x.max() + 1, scs_output.y.max() + 1)
)
labels = sparse_matrix.toarray()
## all non-segmented as -1
labels = np.where(labels == 0, -1, labels)
labels = convert_to_lower_dtype(labels)
labels_array =xr.DataArray(labels, name=f'segmentation', dims=('y', 'x'))
parsed_labels = sd.models.Labels2DModel.parse(labels_array, transformations=transformation)
sd_output.labels['segmentation'] = parsed_labels
print("Writing output", flush=True)
Path(output_path).parent.mkdir(parents=True, exist_ok=True)
if os.path.exists(output_path):
shutil.rmtree(output_path)
sd_output.write(output_path)