-
Notifications
You must be signed in to change notification settings - Fork 0
/
unfold_and_rename.py
239 lines (196 loc) · 7.45 KB
/
unfold_and_rename.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
227
228
229
230
231
232
233
234
235
236
237
238
239
"""
Iterate through subjects directories and extract volumes following an unzip for make
data suitable to Matlab SPM12 functions. Creating subjectlist.txt filetext for CAP_TB
script.
Just for Windows environment for now.
Da lanciare 2 volte sui sani e poi sui malati
"""
from asyncio import ALL_COMPLETED
import code
from multiprocessing.connection import wait
import os
import multiprocessing as mp
import concurrent.futures
import pandas as pd
import time
###### Directories
#datadir='/mnt/c/Users/sambo/Desktop/UNIVERSITAS/OneDrive/UNIVERSITAS/MEDICAL BIOINFORMATICS/TIROCINIO_E_TESI/'
#codedir=os.getcwd()
######
def remove_unnecessary():
print(">> Removing unnecessary files ... ")
#datadir= '/mnt/f/Tirocinio_HDD/Data/ADNI/' #remove in .py execution
os.chdir(datadir)
f = open(datadir+'subjectlist.txt')
lines=f.readlines()
for line in lines:
l=line.split('/')
l=l[len(l)-1]
l=l.rstrip()
os.chdir(datadir+l)
print("-> "+datadir+l)
if len(os.listdir()) == 0:
print("empty")
else:
if os.path.exists("mov_warped_func_BACKUP_COPY.txt"):
os.remove('mov_warped_func_BACKUP_COPY.txt')
if os.path.exists("mov_warped_func.nii.gz"):
os.remove('mov_warped_func.nii.gz')
if os.path.isdir('Backup'):
os.chdir(datadir+l+'/Backup')
os.remove('warped_func.nii.gz')
os.chdir(datadir+l)
os.rmdir('Backup')
os.chdir(datadir)
print('> Removing complete.')
def getMovementFile(subj,dir_tmp):
#print("############")
outname='mov_'+subj.split('.')[0]
mcflirt='mcflirt -in '+subj+' -out '+outname+' -plots -spline_final'
print(mcflirt)
os.system(mcflirt)
#renaming to .txt
rename='mv '+outname+'.par'+' '+outname+'.txt'
os.system(rename)
#print("############")
return "OK"
def unzip_and_extractvol(subj):
#print("###########################")
tmpdir=datadir+subj+"/"
os.chdir(tmpdir)
l=os.listdir()[0]
print("------- "+str(os.getcwd())+" ------- ")
##create copy of original
#newname="Copy_"+l
#copy='mkdir Backup && cp '+l+' Backup'
#print(copy)
#os.system(copy)
#Test for integrity:
os.system("if gunzip -t warped_func.nii.gz ;then echo 'file is ok'; else echo 'file is corrupt';pwd >> /home/sbombieri/DATA/LOGFILE.txt; fi ")
print("################# UNZIPPING ")
print(subj)
print(tmpdir)
print("#################")
try:
print(getMovementFile(l,tmpdir))
except:
print("-> ERROR get movement file")
log_output(subj)
##separate from 3D to 4D
output_basename=" volsubj"
fslsplit='fslsplit '+l+output_basename
#print(fslsplit)
try:
os.system(fslsplit)
##unzip for CAP_TB
gunzip='for f in ./vol*; do gunzip $f -f; done'
#print(gunzip)
os.system(gunzip)
print('\n------- Unzipped file in '+subj+' named '+l+' --------\n')
except:
log_output(subj)
os.chdir(datadir)
# from bash to windows format
def refinelist(subjlist):
print("Creating *subjectlist* for inputting correct data in Matlab..\n")
# dir="F:\Tirocinio_HDD\Data\ADNI\\"
dir=datadir
filePath=datadir+"subjectlist.txt"
if os.path.exists(filePath):
os.remove(filePath)
print("Removed previous file")
print("Writing ...")
with open("subjectlist.txt", 'a') as the_file:
with open (subjlist,'r') as file:
for line in file:
#print("# "+line)
the_file.write(windatadir+line)
print("Converted correctly, created file subjectlist in "+filePath)
the_file.close()
file.close()
os.remove(subjlist)
def log_output(subj):
print("------------------>Writing log to :"+datadir+"LOG_ERRORS_UNFOLD_N_RENAME.txt")
f=open(datadir+"LOG_ERRORS_UNFOLD_N_RENAME.txt",'w')
f.write(str("SUB ID -> "+subj+"\n"))
f.close()
def getDirectories():
df = pd.read_csv('Directories.txt',sep='\t',index_col='Index')
global prefix
global datadir
global codedir
global windatadir
prefix = df.loc['PREFIX','value']
datadir = df.loc['Data directory','value']
codedir = df.loc['Code directory','value']
windatadir=df.loc['Windows Data Directory','value'] #se ubuntu usare path di ubuntu
def setTerrain():
prev=os.getcwd()
os.chdir(datadir)
#obj: remove all previous volumes and files, reset to only warped.nii.gz
l=os.listdir()
for f in l:
if 'sub-' in f:
vol="rm "+datadir+f+"/vol*"
mov="rm "+datadir+f+"/mov*"
os.system(vol)
os.system(mov)
print("Default settings are OK.")
os.chdir(prev)
time.sleep(2.5)
if __name__ == '__main__':
print("\n* VERSION 2.2.1 *\n")
#-MAIN-#
getDirectories()
print("## Prefix = "+prefix) #prefix of subject folder
print("## Datadir= "+datadir) #where to save and manage data
print("## Codedir = "+codedir) #where this code is being launched
print("Setting basic things...")
os.system("rm "+datadir+"LOGFILE.txt")
setTerrain()
#os.system("touch "+datadir+"LOGFILE.txt")
#create subjectlist in datadir
os.chdir(datadir)
command=" ls | grep "+prefix+" > subjectlist_tmp.txt"
os.system(command)
#read subjectlist
f=open("subjectlist_tmp.txt","r")
subjs=f.read().split()
f.close()
print("LIST:")
for s in subjs:
print("#"+s)
#another refinement step:
data = pd.read_csv('EMPTY.txt', sep=" ", header=None)
sublist=pd.read_csv("subjectlist_tmp.txt",header=None)
#delete momentarely void folders
for i in range(0,len(data)):
sublist.drop(sublist[sublist[0]==data.loc[i].values[0]].index,inplace=True)
sublist.to_csv(r'subjectlist_tmp.txt', header=None, index=None, sep=' ', mode='w')
#i=0
with concurrent.futures.ProcessPoolExecutor() as executor:
for (subj) in subjs:
#print("Getting movement for "+subj)
#fs = [executor.submit(getMovementFile,subj)]
#concurrent.futures.wait(fs,return_when=ALL_COMPLETED)
#print("## "+str(fs[0].result))
#i+=1
print("Extracting volumes from "+subj)
executor.submit(unzip_and_extractvol,subj)
#unzip_and_extractvol(subj)
print("##----- DONE for subj: "+subj+" -----##")
#with concurrent.futures.ProcessPoolExecutor() as executor:
# for (subj) in subjs:
# print("Extracting volumes from "+subj)
# executor.submit(unzip_and_extractvol,subj)
refinelist("subjectlist_tmp.txt")
# os.system("python3 "+codedir+"correctSubjlist.py")
print('Done unfolding and renamig, now refinig the length of extracted volumes...')
#togli volumi in più
command="python3 "+codedir+"Togli_volumi.py"
os.system(command)
#crea le due liste per lo script matlab
command="python3 "+codedir+"create_lists_healty_patients.py"
os.system(command)
remove_unnecessary()
print('Done!\nNow start Matlab for CAPs generation ;) ')