forked from big-data-lab-team/fuzzy-linreg
-
Notifications
You must be signed in to change notification settings - Fork 0
/
calculate_sigmaps.py
198 lines (140 loc) · 5.48 KB
/
calculate_sigmaps.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
import os
import significantdigits as sd
# import torchio as tio
import nibabel as nib
from significantdigits import Error, Method
import numpy as np
import glob
def calculate_fuzzy(folder, save_filepath):
t1s = []
files = os.listdir(f"{folder}")
for subject in os.listdir(f"{folder}/1"):
if 'mat' in subject:continue
if subject.split('.')[0] in already_done: continue
print(subject)
if 'OAS1_0041_MR1_mpr-3_anon_reorient.nii.gz' != subject: continue
for iter in range(1,11):
if 'nii' in subject:
# img = tio.ScalarImage(f"{folder}/{iter}/{subject}")
img = nib.load(f"{folder}/{iter}/{subject}")
print(type(img.get_fdata().squeeze()[0,0,0]))
print(img.get_fdata().shape)
t1s.append(img.get_fdata().squeeze())
mean = np.mean(np.array(t1s), axis=0)
print(mean.shape, type(mean[0,0,0]))
sig = sd.significant_digits(
array=t1s,
reference=mean,
# axis=0,
# error=Error.Relative,
# method=Method.General,
)
print(type(sig[0,0,0]), sig.shape)
np.save(f"{save_filepath}/{subject.split('.')[0]}.npy", sig)
# break
def calculate_archdocker(save_filepath):
t1s = []
for subject in glob.glob(f"g5k_results/Docker/6a92985a9f458557cd62fb3eb0f0cebf/*1.nii.gz"):
print(subject)
if os.path.isfile(f"g5k_results/sigmaps/Docker/{subject.split('/')[-1].split('.')[0]}.npy"): continue
# subject = subject.split('/')[-1].split('.')[0]
# files[subject] = []
for dir in glob.glob(f"g5k_results/Docker/*"):
t1s.append(nib.load(f"{subject}").get_fdata().squeeze())
# files[subject].append(f"{dir}/{subject}")
# for dir in glob.glob(f"g5k_results/GUIX/*"):
# # files[subject].append(f"{dir}/{subject}")
# t1s.append(nib.load(f"{subject}").get_fdata().squeeze())
mean = np.mean(np.array(t1s), axis=0)
print(mean.shape, type(mean[0,0,0]))
sig = sd.significant_digits(
array=t1s,
reference=mean,
)
print(type(sig[0,0,0]), sig.shape)
np.save(f"{save_filepath}/{subject.split('/')[-1].split('.')[0]}.npy", sig)
def calculate_archguix(save_filepath):
t1s = []
for subject in glob.glob(f"g5k_results/Docker/6a92985a9f458557cd62fb3eb0f0cebf/*1.nii.gz"):
print(subject)
if os.path.isfile(f"g5k_results/sigmaps/GUIX/{subject.split('/')[-1].split('.')[0]}.npy"): continue
# subject = subject.split('/')[-1].split('.')[0]
# files[subject] = []
# for dir in glob.glob(f"g5k_results/Docker/*"):
# t1s.append(nib.load(f"{subject}").get_fdata().squeeze())
# # files[subject].append(f"{dir}/{subject}")
for dir in glob.glob(f"g5k_results/GUIX/*"):
# files[subject].append(f"{dir}/{subject}")
t1s.append(nib.load(f"{subject}").get_fdata().squeeze())
mean = np.mean(np.array(t1s), axis=0)
print(mean.shape, type(mean[0,0,0]))
sig = sd.significant_digits(
array=t1s,
reference=mean,
)
print(type(sig[0,0,0]), sig.shape)
np.save(f"{save_filepath}/{subject.split('/')[-1].split('.')[0]}.npy", sig)
def simple_beeswarm(y, nbins=None):
"""
Returns x coordinates for the points in ``y``, so that plotting ``x`` and
``y`` results in a bee swarm plot.
"""
y = np.asarray(y)
if nbins is None:
nbins = len(y) // 6
# Get upper bounds of bins
x = np.zeros(len(y))
ylo = np.min(y)
yhi = np.max(y)
dy = (yhi - ylo) / nbins
ybins = np.linspace(ylo + dy, yhi - dy, nbins - 1)
# Divide indices into bins
i = np.arange(len(y))
ibs = [0] * nbins
ybs = [0] * nbins
nmax = 0
for j, ybin in enumerate(ybins):
f = y <= ybin
ibs[j], ybs[j] = i[f], y[f]
nmax = max(nmax, len(ibs[j]))
f = ~f
i, y = i[f], y[f]
ibs[-1], ybs[-1] = i, y
nmax = max(nmax, len(ibs[-1]))
# Assign x indices
dx = 1 / (nmax // 2)
for i, y in zip(ibs, ybs):
if len(i) > 1:
j = len(i) % 2
i = i[np.argsort(y)]
a = i[j::2]
b = i[j+1::2]
x[a] = (0.5 + j / 3 + np.arange(len(b))) * dx
x[b] = (0.5 + j / 3 + np.arange(len(b))) * -dx
return x
def calculate_swarm():
fuzzy = []
arch = []
for sub in os.listdir('results/sigmaps'):
print(sub)
s = np.load(f"results/sigmaps/{sub}").flatten()
fuzzy.append(s)
fuzzy = np.array(fuzzy).flatten()
print(fuzzy.shape)
for sub in os.listdir('g5k_results/sigmaps'):
print(sub)
s = np.load(f"g5k_results/sigmaps/{sub}").flatten()
arch.append(s)
arch = np.array(arch).flatten()
print(arch.shape)
x1 = simple_beeswarm(fuzzy)
# ax.plot(x, prot['Verrou All'], 'o', alpha=0.5)
x2 = simple_beeswarm(arch)
np.save('swarm_fuzzy.pkl', x1)
np.save('sig_fuzzy.pkl', fuzzy)
np.save('swarm_arch.pkl', x2)
np.save('sig_arch.pkl', arch)
# calculate_swarm()
calculate_archdocker(save_filepath='g5k_results/sigmaps/Docker')
calculate_archguix(save_filepath='g5k_results/sigmaps/GUIX')
# calculate_fuzzy(folder='results/anat-12dofs/mca', save_filepath='results/sigmaps')