-
Notifications
You must be signed in to change notification settings - Fork 7
/
ipl_bison.py
executable file
·177 lines (135 loc) · 6.1 KB
/
ipl_bison.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
#! /usr/bin/env python
# standard libraries
import string
import os
import argparse
import sys
import csv
import json
import math
from ipl.bison import init_clasifierr, train, run_cv, infer, read_csv_dict,load_all_volumes
import numpy as np
def parse_options():
parser = argparse.ArgumentParser(
formatter_class=argparse.ArgumentDefaultsHelpFormatter,
description='Run tissue classifier ')
parser.add_argument('--train',
help="training csv")
parser.add_argument('--infer',
help="inference csv, need to specify trained model")
parser.add_argument('--output',
help="Output prefix")
parser.add_argument('--load',
help="Directiry with pretrained classifier and other files")
parser.add_argument('--prob', action="store_true",
dest="prob",
default=False,
help='Output probabilities' )
parser.add_argument('--method',
choices=['RF-','RF0','RF1','RF2','RF3','NB','SVC','oSVC','LDA','QDA','HGB1','HGB2'],
default='RF1',
help='Classification algorithm')
parser.add_argument('--debug', action="store_true",
dest="debug",
default=False,
help='Print debugging information' )
parser.add_argument('--random', type=int,default=None,
dest="random",
help='Provide random state if needed for shuffling' )
parser.add_argument('--n_cls', type=int,
dest="n_cls",
help='number of non BG classes', default=1 )
parser.add_argument('--n_jobs', type=int,
dest="n_jobs",
help='number jobs for classifier', default=1 )
parser.add_argument('--CV', type=int,
dest="CV",
help='Run cross-validation loop' )
parser.add_argument('--batch', type=int,
dest="batch",
help='Batch size for inference', default=1 )
parser.add_argument('--atlas_pfx',
help='Atlas prefix, if on-line prior resampling is needed', default=1 )
parser.add_argument('--resample', action="store_true",
dest="resample",
default=False,
help='Resample priors on line, need "xfm" column' )
parser.add_argument('--symmetric', action="store_true",
dest="symmetric",
default=False,
help='Produce two outputs in inference mode, one regular, another is flipped' )
parser.add_argument('--inverse_xfm', action="store_true",
dest="inverse_xfm",
default=False,
help='Use invers of the xfm files for resampling (faster for nonlinear xfm)' )
parser.add_argument('--ran_subset', type=float,
dest="ran_subset",
help='Random subset (fraction)', default=1.0 )
parser.add_argument('--subset_seed', type=int,
dest="subset_seed",
help='Seed for RNG to perform random subset', default=1 )
options = parser.parse_args()
return options
if __name__ == "__main__":
options = parse_options()
n_cls = options.n_cls
#modalities = ('t1', 't2', 'pd', 'flair', 'ir','mp2t1', 'mp2uni')
clf = init_clasifierr(options.method, n_jobs=options.n_jobs,
random=options.random)
if options.train is not None and options.output is not None:
train_data = read_csv_dict(options.train)
# recognized headers:
# t1,t2,pd,flair,ir,mp2t1,mp2uni
# pCls<n>,labels,mask
# minimal set: one modality, p<n>, av_modality, labels, mask for training
if 'labels' not in train_data:
print("labels are missing")
exit(1)
elif 'mask' not in train_data: # TODO: train with whole image?
print('mask is missing')
exit(1)
if options.resample:
if 'xfm' not in train_data:
print("Need xfm column")
exit(1)
else:
for i in range(n_cls):
if f'p{i+1}' not in train_data:
print(f'p{i+1} is missing')
exit(1)
print("Loading all volumes")
if options.ran_subset<1.0: print("Using random subset:",options.ran_subset)
_state = np.random.get_state()
np.random.seed(options.subset_seed)
sample_vol=load_all_volumes(train_data, n_cls,
resample=options.resample,
atlas_pfx=options.atlas_pfx,
inverse_xfm=options.inverse_xfm,
n_jobs=options.n_jobs,ran_subset=options.ran_subset)
np.random.set_state(_state)
n_feat = n_cls # p_spatial
print("Classifier:", clf)
if options.CV is not None:
run_cv(options.CV, sample_vol, random=options.random,
method=options.method,output=options.output,
clf=clf, n_cls=n_cls )
else:
train(sample_vol, random=options.random,
method=options.method,
output=options.output,
clf=clf, n_cls=n_cls )
elif options.infer is not None and \
options.output is not None and \
options.load is not None:
input = read_csv_dict(options.infer)
infer(input, n_cls=n_cls,
resample=options.resample, n_jobs=options.n_jobs,
method=options.method, batch=options.batch,
load_pfx=options.load, atlas_pfx=options.atlas_pfx,
inverse_xfm=options.inverse_xfm,
output=options.output, prob=options.prob,
progress=True)
else:
print("Error in arguments, run with --help")
exit(1)
# kate: indent-width 4; replace-tabs on; remove-trailing-space on; hl python; show-tabs on