-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcheck_estimable.py
178 lines (148 loc) · 5.42 KB
/
check_estimable.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
from os.path import join as pjoin
import os
import sys
# import glob as gb
# import shutil as sh
import nibabel as nib
import fnmatch
import re
from tempfile import mkdtemp
#----
# Define the default size of the image
IMG_FILE_SIZE = 614728
PAT = r'(.*)(\d{12})(.*)' # pattern to match a PSC in a filename (with path)
#------------------------------------------------------------------------------#
# deal - badly - with the onlines arguments
#------------------------------------------------------------------------------#
def usage(msg):
print('\n Usage: "python check_estimable.py in_dir out_dir [*.nii] " %s'
% msg)
print('\n in_dir : a directory that contains all the contrast images')
print(' (contrasts can be in several sub-directories)')
print('\n out_dir : a directory that contains the resulting txt files \n')
quit()
if len(sys.argv) < 2:
usage(' ')
# get directory - check it exists
basedir = sys.argv[1]
if not os.path.exists(basedir):
msg = 'directory %s does not exist' % basedir
usage(msg)
# if a second argument is provided:
if len(sys.argv) > 2:
savedir = sys.argv[2]
if not os.path.exists(savedir):
usage(str('save directory %s doesnt exist' % savedir))
else:
savedir = mkdtemp()
# if a third argument is provided:
if len(sys.argv) > 3:
PSC = sys.argv[3]
else:
PSC = '*.nii'
print('searching %s images in directory %s ' % (PSC,basedir))
#------------------------------------------------------------------------------#
# find files with a given pattern
def find_files(directory, pattern):
"""find files in directory and subdirectory with a given pattern
Parameters
----------
directory: string
The directory where to find files recursively
Returns
-------
pattern: string
A pattern to match
Examples
--------
find_files('/home/user/code','*.py')
"""
for root, dirs, files in os.walk(directory):
for base_name in files:
if fnmatch.fnmatch(base_name, pattern):
filename = pjoin(root, base_name)
yield filename
class LoadImgError(Exception):
""" exception if load image fails """
#------------------------------------------------------------------------------#
def find_files_with_string_header(lfiles, searchstring='unestimable',
pscpatt = PAT):
""" Find the files with the string in the header
Parameters
----------
lfiles: list
list of files names
searchstring : str
the string to search in the header
Returns
-------
dictionary, list_with_string: list
lists of filenames
"""
fdic = { 'all': [],
'psc': [],
'bad_psc': [],
'too_small': [],
'cannot_load': [],
'nonestim': [],
'estim': []
}
r = re.compile(pscpatt)
for filename in lfiles:
fdic['all'].append(filename)
# check if filename has a PSC
#-----------------------------------------------------------------
m = r.match(filename)
if m:
fdic['psc'].append(m.groups()[1])
else:
fdic['bad_psc'].append(filename)
continue
# check if filename has a reasonable size
#-----------------------------------------------------------------
if os.lstat(filename).st_size < IMG_FILE_SIZE:
fdic['too_small'].append(filename)
continue
# check if filename can be loaded by nibabel and header readable
#-----------------------------------------------------------------
try:
img = nib.load(filename)
except:
print('Nibabel Could not load file : %s ' % filename)
fdic['cannot_load'].append(filename)
continue
try:
imghdr = img.get_header()
except:
print('Nibabel Could not get header of file : %s ' % filename)
fdic['cannot_load'].append(filename)
continue
# check if the filename has string ("unestimable") in the descrip
#-----------------------------------------------------------------
if imghdr['descrip'].tostring().find(searchstring) >= 0:
print(imghdr['descrip'].tostring())
print('%s',filename)
fdic['nonestim'].append(filename)
else:
fdic['estim'].append(filename)
return(fdic)
#------------------------------------------------------------------------------#
# save the txt files in savedir, use basedir to provide a name
def save_results(savedir, basedir, lfiles, suffix):
fname = (basedir[1:] + suffix).replace(os.path.sep,'_')
fid = open(pjoin(savedir,fname), 'w')
fid.writelines("%s\n" % item for item in lfiles)
fid.close()
print('Saved text files: \n\t%s\n in directory %s\n' % (fname, savedir))
#------------------------------------------------------------------------------#
# do it:
#------------------------------------------------------------------------------#
# print(' find the files ...')
lfiles = find_files(basedir, PSC)
# print('find those that are not estimable')
res = find_files_with_string_header(lfiles)
for k in res:
if res[k]: # not empty
print('number of %s is : %d' % (k,len(res[k])))
save_results(savedir, basedir, res[k], '_' + k + '.txt')
#------------------------------------------------------------------------------#