-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtksurferCapture.py
316 lines (244 loc) · 9.8 KB
/
tksurferCapture.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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
#!/usr/bin/env python3
import shutil
import os
from os.path import join, isfile, basename, dirname
from pathlib import Path
import re
from nilearn import datasets
from nilearn import plotting
import nipype.interfaces.freesurfer as fs # fsl
import pandas as pd
import argparse
import textwrap
import sys
from plumbum import local
pd.set_option('max_rows', 5000)
# def labelValueSwap(labelLoc:str, newNum:int) -> None:
# '''Replace zeros with newNum'''
# #print labelLoc
# # read lines from labelLoc
# with open(labelLoc, 'r') as f:
# lines = f.read()
# newLines = lines.replace('0.0000000000', str(newNum))
# newName = os.path.dirname(labelLoc)+'/' +\
# os.path.basename(labelLoc)[:-6] + \
# '_new.label'
# #print newName
# with open(newName, 'w') as newF:
# newF.write(newLines)
def tclWrite(thicknessAsc:str, location:str) -> None:
'''Write tcl script that saves screenshots from the thicknessAsc file'''
directory = Path(location).parent
toWrite = f'''set curv {thicknessAsc}
set tiff_directory {directory}
read_binary_curv
curv_to_val
set overlayflag 1
#set fthresh .001
#set fmid .2
sclv_set_current_threshold_from_percentile .92 .93 .99
set colscalebarflag 1
set curvflag 0
UpdateAndRedraw
redraw
make_lateral_view
save_tiff lateral.tif
rotate_brain_x 90
redraw
save_tiff inferior.tif
make_lateral_view
rotate_brain_y 180
redraw
save_tiff medial.tif
# will cause FS to exit
exit 0'''
with open(location, 'w') as f:
f.write(toWrite)
def ctedit(ctab:str, labelName:str, newNum:int) -> None:
'''Change color for the given label in the color tab file'''
# read color table file
with open(ctab, 'r') as f:
lines = f.readlines()
newLines = []
# for each line in the color table
for line in lines:
if labelName in line:
newLine = re.sub(r'(\d+)\s+(\d+)\s+(\d+)\s+(\d+)',
f'60 60 60 {int(newNum)}',
line)
newLines.append(newLine)
else:
newLines.append(line)
# wirte the new color file
with open(ctab, 'w') as f:
for line in newLines:
f.write(line)
def makeBrainPic(freesurfer_dir:str):
'''Save brain images into files'''
freesurfer_dir = Paht(freesurfer_dir)
for side in ['lh','rh']:
tcl_script = freesurfer_dir / 'tmp' / side + '_tksurfer.tcl'
#annotFile = os.path.join(freesurfer_dir,
#'mri/aparc+aseg.mgz')
# print freesurfer_dir
#print os.path.dirname(freesurfer_dir)
# shots = fs.SurfaceSnapshots(
# subject_id=os.path.basename(freesurfer_dir),
# subjects_dir=os.path.dirname(freesurfer_dir),
# hemi=side,
# tcl_script=tcl_script,
# #annot_file = annotFile,
# #overlay_range = (-4,4),
# #screenshot_stem = freesurfer_dir+'_'+side,
# six_images = True,
# surface="inflated")
command = f'tksurfer {freesurfer_dir.name} {side} inflated \
-tcl {tcl_script}'
print(command)
os.environ["FREESURFER_HOME"] = local.path(os.getenv('FREESURFER_HOME'))
os.environ["SUBJECTS_DIR"] = freesurfer_dir.parent
# print(shots.cmdline)
#print "export SUBJECTS_DIR={0}".format(os.path.dirname(freesurfer_dir))
# os.popen(shots.cmdline).read()
os.popen(command).read()
# createdList = [side+'_'+x+'.tif' for x in ["inferior", "lateral", "medial"]]
# for img in createdList:
# #print img
# if 'baseline' in freesurfer_dir:
# folderName = os.path.dirname(freesurfer_dir).split('/baseline')[0]
# else:
# folderName = os.path.dirname(freesurfer_dir)
# shutil.move(img.split('_')[1],
# os.path.join('/ccnc/mri_team/',
# os.path.basename(folderName) + '_' + os.path.basename(img)))
def mergeLabel(freesurfer_dir:str, labelNames:list) -> None:
'''Merge labels into single file using Freesurfer mri_mergelabels'''
freesurfer_dir = Path(freesurfer_dir)
out_dir = freesurfer_dir / 'tmp'
in_label = ' '.join(
[f'-i {out_dir}/{side}.{x}_new.label' for x in labelNames]
)
for side in 'lh', 'rh':
out_label = f'{out_dir}/{side}_all.label'
command = f'mri_mergelabels {in_label} -o {out_label} 2>/dev/null'
os.popen(command).read()
def cleanMean(meanCSV:str, indCSV:str) -> pd.DataFrame:
'''Merge mean csv with individual csv file'''
# read mean CSV
meanDf = pd.read_csv(meanCSV, index_col=0)
meanDf = meanDf.groupby(['roi', 'side']).mean().reset_index()
df = pd.read_csv(indCSV, index_col=0)
df['roi'] = df.subroi.str[3:]
df['side'] = df.subroi.str[:2]
mergedDf = pd.merge(meanDf, df,
on=['roi', 'side'],
how='inner')
# individual subject thickness - mean thickness
mergedDf['mean_sub_indv'] = mergedDf.thickavg_y - mergedDf.thickavg_x
return mergedDf
#def rescale(npArray, new_min = 70, new_max = 100):
#values = npArray.tolist()
#print values
#output = []
#old_min, old_max = min(values), max(values)
#for v in values:
#new_v = (new_max - new_min) / (old_max - old_min) * (v - old_min) + new_min
#output.append(new_v)
#return output
def makeAsc(freesurferLoc:str, output_dir:str) -> None:
'''Converts thickness and white matter surface map to a ascii file'''
freesurferLoc = Path(freesurferLoc)
output_dir = Path(output_dir)
for side in 'lh', 'rh':
thickSurf = freesurferLoc / 'surf' / f'{side}.thickness'
whiteSurf = freesurferLoc / 'surf' / f'{side}.white'
# output = freesurferLoc / 'tmp' / f'{side}.thickness.asc'
output = output_dir / f'{side}.thickness.asc'
command = f'mris_convert -c {thickSurf} {whiteSurf} {output}'
out = os.popen(command).read()
def main(fsDir:str, csv:str, mean_csv_loc:str):
'''Make brain slice screenshots using tksurfer'''
fsDir = Path(fsDir)
# mean_csv_loc = '/ccnc_bin/meanThickness/detailed_mean_2017_06_16.csv'
# get mean df + individual df
mergedDf = cleanMean(mean_csv_loc, csv)
#standard_ctab = '/ccnc_bin/meanThickness/standard_ctab.txt'
fs_home = local.path(os.getenv('FREESURFER_HOME'))
standard_ctab = f'{fs_home}/FreeSurferColorLUT.txt'
subject_ctab = fsDir / 'ctab.txt'
shutil.copyfile(standard_ctab, subject_ctab)
# converts surface files into ascii files
makeAsc(fsDir, fsDir / 'tmp')
# for each side
for side in 'lh', 'rh':
mergedDfSide = mergedDf.groupby('side').get_group(side)
# load thickness ascii file
ascii_file = fsDir / 'tmp' / f'{side}.thickness.asc'
ascii_file_new = fsDir / 'tmp' / f'new.{side}.thickness.asc'
ascii_df = pd.read_csv(ascii_file, sep='\s+')
ascii_df.columns = ['vertexNum', 'x','y','z','value']
ascii_df.set_index('vertexNum', inplace=True)
ascii_df['value'] = 0
# for each labels
for index, row in mergedDfSide.iterrows():
# get locations
roi_name = f'{row.side}.{row.roi}.label'
roi_loc = fsDir / 'tmp' / roi_name
# get label df
df = pd.read_csv(roi_loc, skiprows=[0,1], sep='\s+')
df.columns = ['vertexNum', 'x', 'y', 'z', 'value']
# here replacing value
df['value'] = row.mean_sub_indv
df.set_index('vertexNum', inplace=True)
#update asciiDf
ascii_df.update(df)
df.roi = row.roi
#labelValueSwap(roiLoc, row[1].mean_sub_indv)
#asciiDf.index = asciiDf.index.apply(lambda x: x.zfill(15))
ascii_df.to_csv(ascii_file_new,sep=' ', header=False)
tcl_loc = fsDir / 'tmp' / f'{side}_tksurfer.tcl'
tclWrite(asciiFileNew, tcl_loc)
#mergeLabel(fsDir, mergedDf.roi.unique())
makeBrainPic(fsDir)
#if __name__=='__main__':
#main(sys.argv[1], sys.argv[2])
##main('/Volumes/promise/CCNC_MRI_3T/NOR/NOR103_SHS/baseline/FREESURFER',
##'/Volumes/promise/CCNC_MRI_3T/NOR/NOR103_SHS/baseline/FREESURFER/tmp/thick_kev_detailed.csv')
if __name__ == '__main__':
parser = argparse.ArgumentParser(
formatter_class=argparse.ArgumentDefaultsHelpFormatter,
description=textwrap.dedent('''\
{codeName} :
========================================
eg) {codeName} --fsDir {inputLoc}
{codeName} --fsDir {inputLoc} --csv ~/thick_kev_detailed.csv
'''.format(codeName=basename(__file__),
inputLoc = 'subjectLoc')))
parser.add_argument(
'-f', '--fsDir',
help='Freesurfer directory location',
default=os.getcwd())
parser.add_argument(
'-m', '--mean_csv',
help='Location of mean csv file',
default=os.getcwd())
parser.add_argument(
'-c', '--csv',
help='Output csv file from freesurfer_summary.py',
default=join(os.getcwd(), 'tmp/thick_kev_detailed_new.csv'))
args = parser.parse_args()
os.environ["FREESURFER_HOME"] = local.path(os.getenv('FREESURFER_HOME'))
os.environ["SUBJECTS_DIR"] = Path(args.fsDir).parent
if Path(args.mean_csv).is_file():
pass
# else:
# fsaverage_dir = Path(local.path(os.getenv('FREESURFER_HOME'))) \
# / 'subjects/fsaverage'
# makeAsc(fsaverage_dir, Path(args.mean_csv).parent)
# ascii_file = fsDir / 'tmp' / f'{side}.thickness.asc'
# for
if Path(args.csv).is_file():
main(args.fsDir, args.csv, args.mean_csv)
else:
print(args.csv)
sys.exit('CSV is missing - Please learn freesurfer_summary.py')