-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmidnight_flywheel.py
executable file
·208 lines (192 loc) · 7.16 KB
/
midnight_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
from shutil import copyfile
from scipy.stats import norm
from avgim import avgim
import nibabel as nb
import numpy as np
import os
import sys
from glob import glob
import crop_zoom_to_roi_original as crop
import commonly as c
import General_reg_longi as GRL
from subprocess import Popen, PIPE
import time
from multiprocessing import Pool
from collections import defaultdict
#import cord_seg_clean as cord
import random
import logging as log
#used to individually correct from the gray matter algorithm most recent June 2019
def alter_ims(ima,mask,naming):
if 'control' in ima:
prefix=os.path.basename(ima)[0:2]
else:
try:
prefix=c.get_mse(os.path.basename(ima))
except:
prefix=os.path.basename(ima).split("_")[0]
tmp=nb.load(mask)
roi=tmp.get_data()
tmp1=np.where(roi>.45,roi,0)
b_roi=np.where(tmp1<=.45,tmp1,1)
raw=nb.load(ima)
im=raw.get_data()
gray=np.multiply(b_roi,im)
nb.save(nb.Nifti1Image(gray,raw.affine),naming+'gray.nii.gz')
#info=np.concatenate([gray[np.where(gray>0.1)],gray[np.where(gray<0.1)]])
#avg=sum(info)/len(info)
info=gray[np.where(gray>0.1)]
avg=np.mean(info)
final=np.where(b_roi<0.1,im,avg-35)
nb.save(nb.Nifti1Image(final,raw.affine),naming+prefix+'altered.nii.gz')
return naming+prefix+'altered.nii.gz'
def individual_correct(i,cord,target,naming,subject,outputs_path,file_handl):
try:
os.mkdir(os.path.join(outputs_path, 'working'))
except:
pass
working=os.path.join(outputs_path, 'working/')
mse=subject
#cord_image=glob('/data/henry11/PBR/subjects/'+mse+'/nii/*only_cord*C2_3*psir*PSIR*')+glob('/data/henry7/PBR/subjects/'+mse+'/nii/*only_cord*C2_3*psir*PSIR*')
#cord_image=glob('/data/henry6/PBR/H_cohort/*only_cord*'+mse+'*C2_3.nii.gz')
try:
pth=[alter_ims(cord,i,working)]
file_handl.write(str((cord,i)))
except:
#input('I errored 1')
raise ValueError('could not alter')
return ' '
#pth=[working+'altered.nii.gz']
try:
os.mkdir(os.path.join(outputs_path, 'registrations2'))
except:
pass
try:
os.mkdir(os.path.join(outputs_path, 'registrations2/warped'))
except:
pass
output_path=os.path.join(outputs_path, 'registrations2/')
static_path=target
filt=None
pth2=[i]
#print(pth)
#print(pth2)
filt2=None
return (pth,output_path,static_path,mse,filt,pth2,filt2)
#return GRL.SimpleRegister(pth,output_path,static_path,mse,lamb=filt,pth2=pth2,lamb2=filt2).Syn()
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(static,outputs_path,cycle_size,subj, sess, protocol, prefix=0):
file_handl=open(os.path.join(outputs_path, 'prints.txt'),'a')
errors=[]
if not(prefix):
if 'retest' in static:
subject=c.get_mse(static)+'retest'+scanner(static)
else:
subject=c.get_mse(static)+scanner(static)
else:
subject=prefix
log.info('#########{}######{}'.format(subject,subject))
file_handl.write('#########{}######{}\n'.format(subject,subject))
registrations=sorted(glob(os.path.join(outputs_path, 'registrations1/warped/*.nii.gz')))
try:
registrations.remove(os.path.join(outputs_path, 'registrations1/warped/synslice_avggmsegs.nii.gz'))
registrations.remove(os.path.join(outputs_path, 'registrations1/warped/cor_synslice_avggmsegs.nii.gz'))
except:
pass
cord_registrations=sorted(glob(os.path.join(outputs_path, 'registrations1/warped1/*.nii.gz')))
try:
cord_registrations.remove(os.path.join(outputs_path, 'registrations1/warped1/synslice_avggmsegs.nii.gz'))
except:
pass
t_map=os.path.join(outputs_path, 'quality_assurance/t_map.nii.gz')
original_line_fit=os.path.join(outputs_path, 'quality_assurance/original_line_fit.nii.gz')
target=static
olf=nb.load(original_line_fit).get_data()
poop1=nb.load(target)
poop1_dat=poop1.get_data()
poop2_dat=nb.load(t_map).get_data()
x,y=poop1_dat.shape
new=np.zeros(poop1_dat.shape)
mean_white_interm=poop1_dat[np.where(olf<0.5)]
mean_white=np.mean(mean_white_interm[np.where(mean_white_interm!=0)])
file_handl.write('mean_white:{}\n'.format(mean_white))
for q in range(x):
for w in range(y):
if poop2_dat[q,w]<=-1.5:
new[q,w]=random.randint(int(mean_white-(mean_white*.03)),int(mean_white+(mean_white*.03)))
#or pass? what happens with 0's?
else:
new[q,w]=poop1_dat[q,w]
tmp=np.where(olf>=0.5)
for i in range(len(tmp[0])):
new[tmp[0][i],tmp[1][i]]=poop1_dat[tmp[0][i],tmp[1][i]]
nb.save(nb.Nifti1Image(new,poop1.affine),os.path.join(outputs_path,'quality_assurance/cor_raw_im.nii.gz'))
target=os.path.join(outputs_path,'quality_assurance/cor_raw_im.nii.gz')
output=[]
data_dict={}
for dicting in range(max((len(registrations),len(cord_registrations)))):
try:
poop=int(os.path.basename(registrations[dicting])[0])
log.info(poop)
data_dict[os.path.basename(registrations[dicting]).split('_')[0]]=[registrations[dicting]]
continue
except:
pass
ide=c.get_mse(os.path.basename(registrations[dicting]))
data_dict[ide]=[registrations[dicting]]
for dicting in range(len(cord_registrations)):
try:
poop=int(os.path.basename(cord_registrations[dicting])[0])
data_dict[os.path.basename(cord_registrations[dicting]).split('_')[0]].append(cord_registrations[dicting])
continue
except:
pass
ide=c.get_mse(os.path.basename(cord_registrations[dicting]))
try:
data_dict[ide].append(cord_registrations[dicting])
except:
pass
argus=[]
for corrrecting in range(len(registrations)):
try:
idee=c.get_mse(os.path.basename(registrations[corrrecting]))
except:
pass
try:
poop=int(os.path.basename(registrations[corrrecting])[0])
idee=os.path.basename(registrations[corrrecting]).split('_')[0]
except:
pass
try:
first=data_dict[idee][0]
#print('first:{}'.format(first))
except:
continue
try:
cord=data_dict[idee][1]
#print('cord:{}'.format(cord))
except:
continue
try:
import pdb
#pdb.set_trace()
argus.append(individual_correct(first,cord,target,'test',subject,outputs_path,file_handl))
#output.append(individual_correct(first,cord,target,'test',subject,outputs_path))
except:
errors.append(os.path.basename(cord).split('.')[0])
if len(errors)>10:
return errors
#try:
#print(argus)
GRL.Syn(argus,file_handl,cycle_size, subj, sess, protocol)
file_handl.close()
#except:
#return ['error with registrations']