-
Notifications
You must be signed in to change notification settings - Fork 0
/
pipeline.py
executable file
·371 lines (189 loc) · 10.3 KB
/
pipeline.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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
"""
Version: 1.5
Summary: Compute traits from a 3D model
Author: suxing liu
Author-email: suxingliu@gmail.com
USAGE:
Default parameter mode: python3 /opt/code/pipeline.py -i /srv/test/test.ply -o /srv/test/result/
User define parameter mode: python3 /opt/code/pipeline.py -i /srv/test/test.ply -o /srv/test/result/ --min_dis 35 --n_slices 1000
INPUT:
3D bean root model
OUTPUT:
Excel file contains traits computation results
PARAMETERS:
("--min_dis", dest = "min_dis", required = False, type = int, default = 35, help = "min distance for watershed segmentation")
("--n_slices", dest = "n_slices", required = False, type = int, default = 1000 , help = 'Number of slices for 3d model.')
"""
import subprocess, os, glob
import sys
import argparse
import pathlib
'''
import numpy as np
import psutil
import concurrent.futures
import multiprocessing
from multiprocessing import Pool
from contextlib import closing
'''
# execute script inside program
def execute_script(cmd_line):
try:
#print(cmd_line)
#os.system(cmd_line)
process = subprocess.getoutput(cmd_line)
print(process)
#process = subprocess.Popen(cmd_line, shell = True, stdout = subprocess.PIPE)
#process.wait()
#print (process.communicate())
except OSError:
print("Failed ...!\n")
# execute pipeline scripts in order
def model_analysis_pipeline(file_path, filename, basename, result_path):
# step 1 python3 /opt/code/model_alignment.py -i ~/example/ -m test.ply -o ~/example/result/
print("Transform point cloud model to its rotation center and align its upright orientation with Z direction...\n")
format_convert = "python3 /opt/code/model_alignment.py -i " + file_path + filename + " -o " + result_path
print(format_convert)
execute_script(format_convert)
# step 2 ./opt/code/compiled/Release/bin/AdTree ~/example/result/test.xyz ~/example/result/ -s
print("Compute structure and skeleton from point cloud model ...\n")
#skeleton_graph = "./compiled/Release/bin/AdTree " + result_path + basename + ".xyz " + result_path + " -s"
skeleton_graph = "/opt/code/compiled/Release/bin/AdTree " + result_path + basename + ".xyz " + result_path + " -s"
print(skeleton_graph)
execute_script(skeleton_graph)
# step 3 python3 /opt/code/extract_slice.py -p ~/example/result/ -f test_branches.obj -o ~/example/result/ -n 1000
print("Generate cross section sequence ...\n")
cross_section_scan = "python3 /opt/code/extract_slice.py -p " + result_path + " -f " + basename + "_branches.obj " + " -o " + result_path + " -n " + str(n_slices)
print(cross_section_scan)
execute_script(cross_section_scan)
# step 4 python3 /opt/code/skeleton_analyze.py -p ~/example/result/ -m1 test_skeleton.ply -m2 test_aligned.ply -o ~/example/result/ --min_dis 12 --visualize_model 0
print("Compute all the traits...\n")
traits_computation = "python3 /opt/code/skeleton_analyze.py -p " + result_path + " -m1 " + basename + "_skeleton.ply " + " -m2 " + basename + "_aligned.ply " + " -o " + result_path + " --min_dis " + str(min_dis) + " --visualize_model " + str(visualize_model)
print(traits_computation)
execute_script(traits_computation)
# step 5 grants read and write access to all result folders
print("Compute all the traits...")
access_grant = "chmod 777 -R " + result_path
print(access_grant + '\n')
execute_script(access_grant)
# parelle processing of folders for local test only
def parallel_folders(subfolder_path):
folder_name = os.path.basename(subfolder_path)
subfolder_path = os.path.join(subfolder_path, '')
m_file = subfolder_path + folder_name + '.' + ext
print("Processing 3d model point cloud file '{}'...\n".format(m_file))
(filename, basename) = get_fname(m_file)
#print("Processing 3d model point cloud file '{}'...\n".format(filename))
#print("Processing 3d model point cloud file basename '{}'...\n".format(basename))
model_analysis_pipeline(subfolder_path, filename, basename, subfolder_path)
# get file information from the file path uisng os for python 2.7
def get_fname(file_full_path):
abs_path = os.path.abspath(file_full_path)
filename= os.path.basename(abs_path)
base_name = os.path.splitext(os.path.basename(filename))[0]
return filename, base_name
# get sub folders from a inout path for local test only
def fast_scandir(dirname):
subfolders= sorted([f.path for f in os.scandir(dirname) if f.is_dir()])
return subfolders
# get file information from the file path using pathon 3
def get_file_info(file_full_path):
p = pathlib.Path(args["input"])
filename = p.name
basename = p.stem
file_path = p.parent.absolute()
file_path = os.path.join(file_path, '')
return file_path, filename, basename
if __name__ == '__main__':
# construct the argument and parse the arguments
ap = argparse.ArgumentParser()
ap.add_argument("-i", "--input", dest = "input", required = True, type = str, help = "full path to 3D model file")
#ap.add_argument("-p", "--path", dest = "path", required = True, type = str, help = "path to 3D model file")
#ap.add_argument("-ft", "--filetype", dest = "filetype", type = str, required = False, default = 'ply', help = "3D model file filetype, default *.ply")
ap.add_argument("-o", "--output_path", dest = "output_path", required = False, type = str, help = "result path")
ap.add_argument("--min_dis", dest = "min_dis", required = False, type = int, default = 35, help = "min distance for watershed segmentation")
ap.add_argument("--n_slices", dest = "n_slices", required = False, type = int, default = 1000 , help = 'Number of slices for 3d model.')
ap.add_argument("--visualize_model", dest = "visualize_model", required = False, type = int, default = 0, help = "Display model or not, deafult as no due to headless display in cluster")
args = vars(ap.parse_args())
# get input file information
if os.path.isfile(args["input"]):
(file_path, filename, basename) = get_file_info(args["input"])
print("Processing 3d model point cloud file '{} {} {}'...\n".format(file_path, filename, basename))
# result path
result_path = args["output_path"] if args["output_path"] is not None else file_path
result_path = os.path.join(result_path, '')
# print out result path
print ("results_folder: {}\n".format(result_path))
# number of slices for cross section
n_slices = args["n_slices"]
min_dis = args["min_dis"]
visualize_model = args["visualize_model"]
# start pipeline
########################################################################################3
model_analysis_pipeline(file_path, filename, basename, result_path)
else:
print("The input file is missing or not readable!\n")
print("Exiting the program...")
sys.exit(0)
'''
# path to model file
file_path = args["path"]
# setting path to model file
file_path = args["path"]
ext = args['filetype'].split(',') if 'filetype' in args else []
patterns = [os.path.join(file_path, f"*.{p}") for p in ext]
model_List = [f for fs in [glob.glob(pattern) for pattern in patterns] for f in fs]
# load input model files
if len(model_List) > 0:
print("Model files in input folder: '{}'\n".format(model_List))
else:
print("3D model file does not exist")
sys.exit()
'''
'''
#loop execute
for model_id, model_file in enumerate(model_List):
print("Processing 3d model point cloud file '{}'...\n".format(model_file))
(filename, basename) = get_fname(model_file)
#print("Processing 3d model point cloud file {} {}\n".format(filename, basename))
model_analysis_pipeline(file_path, filename, basename, result_path)
'''
'''
######################################################################################
# docker version
folder_name = os.path.basename(file_path[:-1])
file_path = os.path.join(file_path, '')
m_file = file_path + folder_name + '.' + ext
print("Processing 3d model point cloud file '{}'...\n".format(m_file))
(filename, basename) = get_fname(m_file)
print("Processing 3d model point cloud file {} {}\n".format(filename, basename))
model_analysis_pipeline(file_path, filename, basename, result_path)
'''
'''
####################################################################################
# local test loop version
subfolders = fast_scandir(file_path)
for subfolder_id, subfolder_path in enumerate(subfolders):
folder_name = os.path.basename(subfolder_path)
subfolder_path = os.path.join(subfolder_path, '')
m_file = subfolder_path + folder_name + '.' + ext
print("Processing 3d model point cloud file '{}'...\n".format(m_file))
(filename, basename) = get_fname(m_file)
#print("Processing 3d model point cloud file '{}'...\n".format(filename))
#print("Processing 3d model point cloud file basename '{}'...\n".format(basename))
model_analysis_pipeline(subfolder_path, filename, basename, subfolder_path)
'''
########################################################################################
# local test parellel version
'''
subfolders = fast_scandir(file_path)
print(len(subfolders))
# get cpu number for parallel processing
agents = psutil.cpu_count() - 4
print("Using {0} cores to perfrom parallel processing... \n".format(int(agents)))
# Create a pool of processes. By default, one is created for each CPU in the machine.
# extract the bouding box for each image in file list
with closing(Pool(processes = agents)) as pool:
result_list = pool.map(parellel_folders, subfolders)
pool.terminate()
'''