forked from erhanbas/navigator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
navigator.py
153 lines (125 loc) · 6.27 KB
/
navigator.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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
# -*- coding: utf-8 -*-
"""Command line interface for navigator."""
import util
import numpy as np
import cropper
import os
import sys, getopt
import stat
import h5py
import shutil
def sample_spherical(npoints, ndim=3):
vec = np.random.randn(ndim, npoints)
vec /= np.linalg.norm(vec, axis=0)
return vec
def fixKinksinAnnotation():
input_folder = '/groups/mousebrainmicro/mousebrainmicro/users/base/AnnotationData/h5repo'
swcfiles = [os.path.join(input_folder, fold, files) for fold in os.listdir(input_folder) if
os.path.isdir(os.path.join(input_folder, fold)) for files in
os.listdir(os.path.join(input_folder, fold)) if
files.endswith("-carved.swc")]
swcfiles.sort()
for swc_file in swcfiles[1]:
path, filename = os.path.split(swc_file)
output_h5_file = os.path.join(path, filename.split('.')[0] + '.h5')
input_swc = swc_file
# output_h5_file = os.path.join(path,filename.split('.')[0][:-1]+'.h5')
# input_swc = os.path.join(path,filename.split('.')[0][:-1]+'.swc')
with h5py.File(output_h5_file, "r+") as f:
try:
del f['reconstruction']
except Exception:
pass
um, edges, R, offset, scale, header = util.readSWC(swcfile=input_swc, scale=1)
dset_swc = f.create_dataset("reconstruction", (um.shape[0], 7), dtype='f')
for iter, xyz_ in enumerate(um):
xyz_ = np.ceil(xyz_ - np.sqrt(np.finfo(float).eps))
dset_swc[iter, :] = np.array(
[edges[iter, 0].__int__(), 1, xyz_[0], xyz_[1], xyz_[2], 1.0, edges[iter, 1].__int__()])
def main(argv):
""" creates cropped volume and JW structure (for visualization) based on input render folder and swc file
USAGE: 'navigator.py -i <data_folder> -s <swc_file> -o <output_folder>'
-i <data_folder>: input data folder. Folders should follow octree format, e.g. <data_folder>/1/5/6
-s <swc_file>: input swc_file or folder. for *swc files 7 column conventional reconstruction format.
-o <output_folder>: folder to create h5 and JW files
-h <number_of_level>: [OPTIONAL] sets how many chunks around trace will be used
-j <output_octree>: [OPTIONAL] creates an octree formated folder at target location. "-j" without argument
will create target output at <output_folder>/JW location
NOTES:
oct in [1...8]
grid in [0...(2**depth-1)]
we keep mouselight data in <root>/<neuron-id>/consensus/<tag>_consensus.swc format, e.g.:
/groups/mousebrainmicro/mousebrainmicro/shared_tracing/Finished_Neurons/2018-08-01/G-002/consensus/2018-08-01_G-002_consensus.swc
it is suggested to copy all consensus files for that sample into a single folder manually or with a script than pass input folder with "-f" argument.
For example:
cd /groups/mousebrainmicro/mousebrainmicro/shared_tracing/Finished_Neurons/2018-08-01
find . -name "*consensus*.swc" -exec cp {} /groups/mousebrainmicro/home/base/CODE/MOUSELIGHT/navigator/data/swc_recons/2018-08-01 \;
"""
# @@TODO: multi swc data dump
# @@TODO: fix octree dilation amount. make it user specified
# data_fold='/nrs/mouselight/SAMPLES/2018-08-01-raw-rerender'
# ## input_swc_file='/groups/mousebrainmicro/mousebrainmicro/users/base/AnnotationData/h5repo/2017-09-25_G-001_consensus/2017-09-25_G-001_consensus-proofed.swc'
# input_swc_file='/groups/mousebrainmicro/home/base/CODE/MOUSELIGHT/navigator/data/swc_recons/2018-08-01'
# output_folder='/groups/mousebrainmicro/mousebrainmicro/users/base/AnnotationData/h5repo/2018-08-01'
# octree_folder = os.path.join(output_folder, 'JW')
number_of_level = 3
try:
opts, args = getopt.getopt(argv,"hi:s:o:j:",["data_fold=","input_swc_file=","output_folder=","octree_folder="])
except getopt.GetoptError:
print('navigator.py -i <data_folder> -s <swc_file> -o <output_folder> -h <OPT:number_of_level> -j <OPT:octree_folder>')
sys.exit(2)
for opt, arg in opts:
print('opt:', opt,'arg:', arg)
if opt == '-h':
print('navigator.py -i <data_folder> -s <swc_file> -o <output_folder>')
sys.exit()
elif opt in ("-i", "--data_fold"):
print(arg)
data_fold = arg
elif opt in ("-s", "--input_swc_file"):
input_swc_file = arg
elif opt in ("-o", "--output_folder"):
output_folder = arg
octree_folder = os.path.join(output_folder,'JW')
elif opt in ("-h", "--number_of_level"):
number_of_level = arg
elif opt in ("-j", "--octree_folder"):
try:
octree_folder
except NameError:
print("Using output folder as JW folder")
if octree_folder:
octree_folder = arg
print('SWCFILE :', input_swc_file)
print('DATAFOLDER :', data_fold)
print('OUTPUT :', output_folder)
print('NUMBEROFLEVEL :', number_of_level)
print('OCTREEFOLDER :', octree_folder)
rootfolder, swc_name = os.path.split(input_swc_file)
#swc_name, _ = swc_name_w_ext.split(os.extsep)
if not os.path.exists(output_folder):
os.makedirs(output_folder)
output_swc_name = '{}-carved.swc'.format(swc_name)
output_h5_name = '{}-carved.h5'.format(swc_name)
JW_output_folder = os.path.join(output_folder,'JW')
if not os.path.exists(JW_output_folder):
scale = 1 / 1000 # for voxel, use 1, for scope use 1/1000 to cast to nm
cropper.crop_from_render(data_fold, input_swc_file, output_folder, output_swc_name, output_h5_name, scale)
# shutil.rmtree(JW_output_folder)
if not os.path.exists(JW_output_folder):
os.makedirs(JW_output_folder)
os.chmod(JW_output_folder, 0o770)
output_h5_file = os.path.join(output_folder, output_h5_name)
converter = util.Convert2JW(output_h5_file, JW_output_folder, number_of_oct_level=None)
converter.convert2JW()
converter.mergeJW(number_of_level=converter.number_of_oct_level)
converter.create_transform_file()
print('DONE')
if __name__ == "__main__":
main(sys.argv[1:])
# [21118.5, 4641.6, 3385.0]
#
#
#
#
#