-
Notifications
You must be signed in to change notification settings - Fork 14
/
skeletons.py
executable file
·78 lines (58 loc) · 2.1 KB
/
skeletons.py
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#! /g/kreshuk/pape/Work/software/conda/miniconda3/envs/cluster-new/bin/python
import os
import json
import numpy as np
import luigi
import z5py
import skeletor.io
from cluster_tools.skeletons import SkeletonWorkflow
def skeletons(path, max_jobs, target):
""" Skeletonize segmentation.
"""
input_key = 'volumes/segmentation/multicut'
output_key = 'skeletons'
config_dir = './configs'
tmp_folder = './tmp_skeletons'
os.makedirs(config_dir, exist_ok=True)
config = SkeletonWorkflow.get_config()
global_config = config['global']
shebang = '#! /g/kreshuk/pape/Work/software/conda/miniconda3/envs/cluster-new/bin/python'
global_config.update({'shebang': shebang})
with open(os.path.join(config_dir, 'global.config'), 'w') as f:
json.dump(global_config, f)
resolution = [40, 4, 4]
size_threshold = 2500
task = SkeletonWorkflow(tmp_folder=tmp_folder, config_dir=config_dir,
max_jobs=max_jobs, target=target,
input_path=path, input_key=input_key,
output_path=path, output_key=output_key,
resolution=resolution, size_threshold=size_threshold)
success = luigi.build([task], local_scheduler=True)
assert success
def view_skeletons(path):
from cremi_tools.viewer.volumina import view
input_key = 'segmentation/multicut'
output_key = 'skeletons'
f = z5py.File(path)
raw_key = 'raw/s0'
ds = f[raw_key]
ds.n_threads = 8
raw = ds[:]
ds = f[input_key]
ds.n_threads = 8
seg = ds[:]
skel_vol = np.zeros_like(seg, dtype='uint32')
ds_skels = f[output_key]
seg_ids = np.unique(seg)
for seg_id in seg_ids:
nodes, _ = skeletor.io.read_n5(ds_skels, seg_id)
if nodes is None:
continue
coords = tuple(np.array([n[i] for n in nodes])
for i in range(3))
skel_vol[coords] = seg_id
view([raw, seg, skel_vol])
if __name__ == '__main__':
path = '/g/kreshuk/data/cremi/example/sampleA.n5'
skeletons(path, 32, 'local')
view_skeletons(path)