-
Notifications
You must be signed in to change notification settings - Fork 1
/
test_bones_BioAsset.py
211 lines (161 loc) · 6.34 KB
/
test_bones_BioAsset.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
from imfractal import *
from pylab import *
import sys
import os
import scipy.stats
sys.path.append(os.path.join(os.path.dirname(sys.path[0]), 'imfractal', 'imfractal', "Algorithm"))
import qs3D
from numpy import recfromcsv
def stats_test(_output_filename, input_filename):
_, extension = os.path.splitext(input_filename)
if extension == 'csv':
data = recfromcsv(input_filename, delimiter=',')
first_f = 1
else:
data = np.load(input_filename)
first_f = 0
dims = 15
mfss = np.zeros([len(data), dims])
for i in range(len(data)):
print i
if extension == 'csv':
mfs = np.array(tuple(data[i])[first_f:])#.astype(np.float32)
else:
mfs = data[i]
if len(mfs) < 100:
max_fa = np.max(mfs)
min_fa = np.min(mfs)
std_fa = np.std(mfs)
mean_fa = np.mean(mfs)
median_fa = np.median(mfs)
sum_fa = np.sum(mfs)
variation = scipy.stats.variation(mfs)
var = scipy.stats.tvar(mfs)
skew = scipy.stats.skew(mfs)
kurtosis = scipy.stats.kurtosis(mfs)
arg_max = np.argmax(mfs)
arg_min = np.argmin(mfs)
diff = np.max(mfs) - np.min(mfs)
first_f = mfs[0]
last_f = mfs[-1]
mfss[i] = np.array([max_fa, min_fa,
mean_fa, std_fa,
median_fa, sum_fa,
skew, kurtosis,
variation, var,
arg_max, arg_min, diff,
last_f, first_f])
else:
tmp = np.array([])
for l in range(5):
mmfs = mfs[l*20 : (l+1)*20 - 1]
max_fa = np.max(mmfs)
min_fa = np.min(mmfs)
std_fa = np.std(mmfs)
mean_fa = np.mean(mmfs)
median_fa = np.median(mmfs)
sum_fa = np.sum(mmfs)
variation = scipy.stats.variation(mmfs)
var = scipy.stats.tvar(mmfs)
skew = scipy.stats.skew(mmfs)
kurtosis = scipy.stats.kurtosis(mmfs)
arg_max = np.argmax(mmfs)
arg_min = np.argmin(mmfs)
tmp = np.hstack((tmp,
np.array([max_fa, min_fa,
mean_fa, std_fa,
median_fa, sum_fa,
skew, kurtosis,
variation, var, arg_max, arg_min])))
# for l in range(5):
# skews[l] = scipy.stats.skew(mfs[l*20 : (l+1)*20 - 1])
# kurtosiss[l] = scipy.stats.kurtosis(mfs[l*20 : (l+1)*20 - 1])
#skew = np.mean(skews)
#kurtosis = np.mean(kurtosiss)
mfss[i] = tmp
print mfss[i], i
np.save(data_path + _output_filename, mfss)
print "Saved ", data_path + _output_filename
def do_test(_path, _output_filename):
dims = 21 # should be odd number! to include q = -x , ... q = 0, ..., q = x
if MFS_HOLDER:
dims = 20 # Holder 3D MFS
if LOCAL:
dims = 20
if Stats_MFS:
dims = 10
# BioAsset bone's multifractal spectra database
#slices_str = "slice"
#masks_str = "mask"
params = {
"zero": 1,
"one": 0.75,
"two": 3.7,
"three": 1,
"four": 15,
"five": 0,
"mask_filename": '',
"seven": "no",
"eight": 'S',
"nine": 'M',
"threshold": 200,
"total_pixels":6000,
"adaptive" : False, # adaptive threshold (only for not holder)
"laplacian": APPLY_LAPLACIAN,
"gradient" : APPLY_GRADIENT
}
from os import listdir
from os.path import isfile, join
mask_files = [f for f in listdir(_path) if isfile(join(_path, f)) and "Mask" in f]
slice_files = [f for f in listdir(_path) if isfile(join(_path, f)) and "Slices" in f]
mfss = np.zeros([len(mask_files), dims])
mask_files = sort(mask_files)
slice_files = sort(slice_files)
if len(mask_files) != len(slice_files):
print "The directory should contain the same amount of slices and masks"
exit()
i = 0
for mask_filename in mask_files:
[patient_scan_str, _] = mask_filename.split("Mask")
[first_str, scan_str] = patient_scan_str.split("_120_")
[_, patient_str] = first_str.split("BA")
mask_filename = _path + mask_filename
params["five"] = 1 #fix me
params["mask_filename"] = mask_filename
# obviously we can directly use slice_files[i], but this adds robustness
slice_filename = _path + "BA" + patient_str + "_120_" + scan_str + "Slices.mat"
if slice_filename == _path + slice_files[i]:
print "MASK: ", mask_filename
print "SLICE: ", slice_filename
else:
print "Cannot process test: filename ", _path + slice_files[i], " should be ", slice_filename
exit()
if not(MFS_HOLDER):
aux = CSandbox3D(dims)
aux.setDef(40, 1.02, True, params)
mfss[i] = aux.getFDs(slice_filename)
else:
aux = MFS_3D()
if LOCAL:
#aux = Local_MFS_3D()
aux = Local_MFS_Pyramid_3D()
aux.setDef(1, 20, 3, slice_filename, mask_filename, params)
mfss[i] = aux.getFDs()
#if Stats_MFS:
# aux = Stats_MFS_3D()
# aux.setDef(1, 20, 3, slice_filename, mask_filename, params)
# mfss[i] = aux.getFDs()
#if SLICES_MFS:
# aux = MFS_3D_Slices()
# aux.setDef(1, dims, 3, slice_filename, mask_filename, params)
# ax = 2 # X axis
# mfss[i] = aux.getFDs(ax)
#else:
# aux.setDef(1, dims, 3, slice_filename, mask_filename, params)
# mfss[i] = aux.getFDs()
# in case something goes wrong, save computed mfs up to here
print "Data partially saved to ", data_path + _output_filename + ".npy"
np.save(data_path + _output_filename, mfss)
i += 1
print "Data saved to ", data_path + _output_filename + ".npy"
np.save(data_path + _output_filename, mfss)