forked from NIST-MNI/nist_mni_pipelines
-
Notifications
You must be signed in to change notification settings - Fork 0
/
iplScoopFusionSegmentation.py
executable file
·245 lines (196 loc) · 8.3 KB
/
iplScoopFusionSegmentation.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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# @author Vladimir S. FONOV
# @date 12/10/2014
#
# Run fusion segmentation
from __future__ import print_function
import shutil
import os
import sys
import csv
import traceback
import argparse
import json
import tempfile
import re
import copy
import random
# MINC stuff
from ipl.minc_tools import mincTools,mincError
# internal funcions
from ipl.segment import *
# scoop parallel execution
from scoop import futures, shared
def parse_options():
parser = argparse.ArgumentParser(formatter_class=argparse.ArgumentDefaultsHelpFormatter,
description='Build fusion segmentation library')
parser.add_argument('--create',
help="Create new library with parameters in json format",
dest='create')
parser.add_argument('--cv',
help="Run cross-validation using existing library with parameters in json format",
dest='cv')
parser.add_argument('--cv-iter',
help="Run one iteration from cross validation, use -1 to aggregate all CVs",
type=int,
dest='cv_iter')
parser.add_argument('--segment',
help="Apply segmentation using provided library",
dest='segment')
parser.add_argument('--train-ec',
help="Train error correction using specified parametrs",
dest='train_ec')
parser.add_argument('--ext',
action="store_true",
dest="ext",
default=False,
help='Assume external segmentation is done' )
parser.add_argument('--extlib',
dest="extlib",
help='Externally segmented samples, for training : <im1>[,img2..],<auto>,<ground>' )
parser.add_argument('--presegment',
help='Externally segmented sample, will apply error correction on top' )
parser.add_argument('--library',
help="Specify library for error correction",
dest='library')
parser.add_argument('--input',
help="input file, required for application of method",
dest='input')
parser.add_argument('--options',
help="Segmentation options in json format",
dest='options')
parser.add_argument('--output',
help="Output directory/file, required for application of method",
dest='output')
parser.add_argument('--work',
help="Work directory, place to store temporary files",
dest='work')
parser.add_argument('--mask',
help="Input mask",
dest='mask')
parser.add_argument('--debug',
action="store_true",
dest="debug",
default=False,
help='Print debugging information' )
parser.add_argument('--cleanup',
action="store_true",
dest="cleanup",
default=False,
help='Remove most temporary files' )
parser.add_argument('--variant_fuse',
default='fuse',
dest='variant_fuse')
parser.add_argument('--variant_ec',
default='ec',
dest='variant_ec')
parser.add_argument('--variant_reg',
default='ec',
dest='variant_reg')
options = parser.parse_args()
if options.debug:
print(repr(options))
return options
if __name__ == '__main__':
options = parse_options()
if options.create is not None and options.output is not None:
create_parameters={}
try:
with open(options.create,'r') as f:
create_parameters=json.load(f)
except :
print("Error loading configuration:{} {}\n".format(options.create,sys.exc_info()[0]),file=sys.stderr)
traceback.print_exc(file=sys.stderr)
exit( 1)
try:
generate_library(create_parameters, options.output, debug=options.debug,
cleanup=options.cleanup)
except :
print("Error in library generation {}".format(sys.exc_info()[0]),file=sys.stderr)
traceback.print_exc(file=sys.stderr)
exit(1)
elif options.cv is not None and \
options.segment is not None and \
options.output is not None:
cv_parameters={}
try:
with open(options.cv,'r') as f:
cv_parameters=json.load(f)
except :
print("Error loading configuration:{}\n{}".format(options.cv,sys.exc_info()[0]),file=sys.stderr)
traceback.print_exc(file=sys.stderr)
exit(1)
ec_parameters=None
if options.train_ec is not None:
try:
with open(options.train_ec,'r') as f:
ec_parameters=json.load(f)
except :
print("Error loading configuration:{}\n{}".format(options.train_ec,sys.exc_info()[0]),file=sys.stderr)
traceback.print_exc(file=sys.stderr)
exit(1)
library=load_library_info( options.segment )
segmentation_parameters={}
if options.options is not None:
try:
with open(options.options,'r') as f:
segmentation_parameters=json.load(f)
except :
print("Error loading configuration:{}\n{}".format(options.options,sys.exc_info()[0]),file=sys.stderr)
traceback.print_exc(file=sys.stderr)
exit(1)
cv_fusion_segment(cv_parameters,
library,
options.output,
segmentation_parameters,
ec_parameters=ec_parameters,
debug=options.debug,
cleanup=options.cleanup,
ext=options.ext,
extlib=options.extlib,
cv_iter=options.cv_iter)
elif options.segment is not None and options.input is not None:
library=load_library_info(options.segment)
segmentation_parameters={}
if options.options is not None:
try:
with open(options.options,'r') as f:
segmentation_parameters=json.load(f)
except :
print("Error loading configuration:{}\n{}".format(options.options,sys.exc_info()[0]),file=sys.stderr)
traceback.print_exc(file=sys.stderr)
exit(1)
#
fusion_segment(options.input, library,
options.output,
input_mask=options.mask,
parameters=segmentation_parameters,
debug=options.debug,
fuse_variant=options.variant_fuse,
ec_variant=options.variant_ec,
regularize_variant=options.variant_reg,
work_dir=options.work,
cleanup=options.cleanup,
presegment=options.presegment)
elif options.train_ec is not None:
library=load_library_info(options.segment)
ec_parameters={}
segmentation_parameters={}
with open(options.train_ec,'r') as f:
ec_parameters=json.load(f)
if options.options is not None:
with open(options.options,'r') as f:
segmentation_parameters=json.load(f)
train_ec_loo(library,
segmentation_parameters=segmentation_parameters,
ec_parameters=ec_parameters,
debug=options.debug,
cleanup=options.cleanup,
ext=options.ext,
train_list=options.extlib,
fuse_variant=options.variant_fuse,
ec_variant=options.variant_ec,
regularize_variant=options.variant_reg)
# kate: space-indent on; indent-width 4; indent-mode python;replace-tabs on;word-wrap-column 80