-
Notifications
You must be signed in to change notification settings - Fork 0
/
#t_map_flywheel.py#
executable file
·229 lines (201 loc) · 6.27 KB
/
#t_map_flywheel.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
import nibabel as nb
import numpy as np
from scipy.ndimage import label as L
from glob import glob
import copy
import commonly as c
def distance_qualify(i,j,l,dis=4):
dist=[]
for x,y in l:
dist.append(((x-i)**2+(y-j)**2)**0.5)
if min(dist)>dis:
return False
else:
return True
def distance(x,y):
return (((x[0]-y[0])**2)+((x[1]-y[1])**2))**0.5
def damper(x,t):
if t<0:
return x
m=0.5*1.5/0.8
return x*m/t
def increase(x,t):
#if t>0:
#return x
#m=0.5*1.5/0.8
#return x*m/t
return 0.5
def cut(x,t):
#if t>0:
#return x
#m=0.5*1.5/0.8
#return x*m/t
return 0.3
def classify_pixel(raw_im,i,j,t_map):
if raw_im[i,j]==0:
if t_map[i+1,j]==0:
return (True,3)
elif t_map[i-1,j]==0:
return (True,3)
elif t_map[i,j+1]==0:
return (True,3)
elif t_map[i,j-1]==0:
return (True,3)
elif t_map[i+1,j+1]==0:
return (True,3)
elif t_map[i+1,j-1]==0:
return (True,3)
elif t_map[i-1,j+1]==0:
return (True,3)
elif t_map[i-1,j-1]==0:
return (True,3)
if raw_im[i,j]==0:
return (True,0)
if raw_im[i,j]!=0:
if raw_im[i+1,j]==0:
return (False,1)
elif raw_im[i-1,j]==0:
return (False,1)
elif raw_im[i,j+1]==0:
return (False,1)
elif raw_im[i,j-1]==0:
return (False,1)
elif raw_im[i+1,j+1]==0:
return (False,1)
elif raw_im[i+1,j-1]==0:
return (False,1)
elif raw_im[i-1,j+1]==0:
return (False,1)
elif raw_im[i-1,j-1]==0:
return (False,1)
else:
return (False,2)
def T_correct_pos(raw,t,soo,file_handl):
raw_im_handl=nb.load(raw[0])
t_map_handl=nb.load(t[0])
raw_im=raw_im_handl.get_data()
t_map=t_map_handl.get_data()
im=np.where(raw_im>=0.5,raw_im,0)
t_map_pos=np.where(t_map>=1.5,t_map,0)
t_clust,n_clust=L(t_map_pos)
print('needs {} corrections'.format(n_clust))
new_im=copy.deepcopy(raw_im)
for c in range(n_clust):
edge_coords=[]
working_clust=np.where(t_clust==c+1)
work_coords=zip(working_clust[0],working_clust[1])
work_dict={}
for iii in list(work_coords):
work_dict[iii]=1
for i in range(len(working_clust[0])):
claas=classify_pixel(im,working_clust[0][i],working_clust[1][i],t_map_pos)
if claas[0]:
del work_dict[(working_clust[0][i],working_clust[1][i])]
elif claas[1]==1:
edge_coords.append((working_clust[0][i],working_clust[1][i]))
#del work_dict[(working_clust[0][i],working_clust[1][i])]
if len(edge_coords)<=2:
continue
for pix in work_dict.keys():
#print('here')
if distance_qualify(pix[0],pix[1],edge_coords):
#Printx
('Qualifiedquit
quitC-x
{}{}'.format(pix[0],pix[1]))
new_im[pix[0],pix[1]]=damper(im[pix[0],pix[1]],t_map_pos[pix[0],pix[1]])
return new_im,raw_im_handl.affine
nb.save(nb.Nifti1Image(new_im,raw_im_handl.affine),outputs_path+'/final_output/rereg_t_map_edit.nii.gz')
def T_correct_neg(raw,t,soo,file_handl,outputs_path):
t_map_handl=nb.load(t[0])
raw_im=raw
t_map=t_map_handl.get_data()
im=np.where(raw_im>=0.5,raw_im,0)
t_map_neg=np.where(t_map<-1.5,t_map,0)
t_clust,n_clust=L(t_map_neg)
print('needs {} corrections'.format(n_clust))
new_im=copy.deepcopy(raw_im)
for c in range(n_clust):
edge_coords=[]
edge_cluster_coords=[]
working_clust=np.where(t_clust==c+1)
if len(working_clust[0])>40:
work_coords=zip(working_clust[0],working_clust[1])
work_dict={}
for iii in list(work_coords):
work_dict[iii]=1
for i in range(len(working_clust[0])):
claas=classify_pixel(im,working_clust[0][i],working_clust[1][i],t_map_neg)
if claas[1]==1:
edge_coords.append((working_clust[0][i],working_clust[1][i]))
if claas[1]==3:
edge_cluster_coords.append((working_clust[0][i],working_clust[1][i]))
if claas[0]:
del work_dict[(working_clust[0][i],working_clust[1][i])]
maxy=0
distancey=0
file_handl.write('edge_coords:{}'.format(len(edge_cluster_coords))+'\n')
if edge_cluster_coords and edge_coords:
edge_mean=np.mean(edge_coords,axis=0)
edge_cluster_mean=np.mean(edge_cluster_coords,axis=0)
file_handl.write(str(distance(edge_mean,edge_cluster_mean))+'\n')
if distance(edge_mean,edge_cluster_mean)<5:
continue
#else:this part is just to mark where the centers are
#print(int(edge_mean[0]),int(edge_mean[1]),int(edge_cluster_mean[0]),int(edge_cluster_mean[1]))
#new_im[int(edge_mean[0]),int(edge_mean[1])]=9000
#new_im[int(edge_cluster_mean[0]),int(edge_cluster_mean[1])]=9000
if len(edge_coords)<=2:
continue
print('shaving lesion areas')
for pix in work_dict.keys():
if distance_qualify(pix[0],pix[1],edge_coords):
new_im[pix[0],pix[1]]=cut(im[pix[0],pix[1]],t_map_neg[pix[0],pix[1]])
else:
work_coords=zip(working_clust[0],working_clust[1])
work_dict={}
for iii in list(work_coords):
work_dict[iii]=1
for i in range(len(working_clust[0])):
claas=classify_pixel(im,working_clust[0][i],working_clust[1][i],t_map_neg)
if claas[1]==1:
edge_coords.append((working_clust[0][i],working_clust[1][i]))
#del work_dict[(working_clust[0][i],working_clust[1][i])]
if len(edge_coords)<=2:
continue
print('increasing dark spots')
for pix in work_dict.keys():
#print('here')
if distance_qualify(pix[0],pix[1],edge_coords):
#print('qualified{}{}'.format(pix[0],pix[1]))
new_im[pix[0],pix[1]]=increase(im[pix[0],pix[1]],t_map_neg[pix[0],pix[1]])
return new_im
nb.save(nb.Nifti1Image(new_im,t_map_handl.affine),outputs_path+'/final_output/rereg_t_map_edit.nii.gz')
def scanner(x):
if 'SKYRA' in x:
return '_SKYRA'
if 'GE' in x:
return '_GE'
if 'PHILIPS' in x:
return '_PHILIPS'
else:
return ''
def run_this(subject,outputs_path,prefix=0):
file_handl=open(outputs_path+'/papers.txt','a')
if not(prefix):
if 'retest' in subject:
subject=c.get_mse(subject)+'retest'+scanner(subject)
else:
subject=c.get_mse(subject)+scanner(subject)
else:
subject=prefix
file_handl.write('t_map'+'\n')
print(subject)
#raw=glob(outputs_path+'/final_output/raw_im.nii.gz')
t=glob(outputs_path+'/final_output/rereg_t_map.nii.gz')
raw=glob(outputs_path+'/final_output/rereg_original_line_fit.nii.gz')
raw_handl=nb.load(raw[0])
#yes,affine=T_correct_pos(raw,t,subject,file_handl)
nb.save(nb.Nifti1Image(T_correct_neg(raw_handl.get_data(),t,subject,file_handl,outputs_path),raw_handl.affine),outputs_path+'/final_output/rereg_t_map_edit.nii.gz')
file_handl.close()
C-x-x,C-c