forked from chrisadamsonmcri/CCSegThickness
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCCSegThicknessToCSV
executable file
·150 lines (117 loc) · 5.77 KB
/
CCSegThicknessToCSV
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
#!/usr/bin/python
import os
import getopt
import sys
import errno
import h5py
import csv
import numpy
import CCSegUtils
import CCSegSubjectSelection
def criticalError(errorString):
print "Error: " + str(errorString)
quit()
def usage():
print "Usage: " + sys.argv[0] + " [options] <input directory> <output directory>"
print
print "Prints thickness profiles to <output directory>/all_thickness_profiles.csv"
print "Prints registered thickness profiles to <output directory>/all_registered_thickness_profiles.csv"
print "Each row is the subject followed by the nodal thicknesses"
print
print "Prints stats of the thickness profiles according to the parcellations in <output directory>/parcellation_stats/"
print "For each parcellation scheme: witelson, hoferfrahm, emsell, the following files are produced:"
print "\t<output directory>/parcellation_stats/<scheme>_stats_area.csv: the areas of each region"
print "\tWithin the following files, the stats refer to the thicknesses of the nodes within each region"
print "\t<output directory>/parcellation_stats/<scheme>_stats_max.csv: the maximum thickness within each region"
print "\t<output directory>/parcellation_stats/<scheme>_stats_min.csv: the minimum thickness within each region"
print "\t<output directory>/parcellation_stats/<scheme>_stats_median.csv: the median thickness within each region"
print "\t<output directory>/parcellation_stats/<scheme>_stats_std.csv: the standard deviation of the thicknesses within each region"
print "\t<output directory>/parcellation_stats/<scheme>_stats_var.csv: the variance of the thicknesses within each region"
def main():
longOptStrings = []
longOptStrings.extend(CCSegSubjectSelection.getOptions())
longOptStrings.extend(['headers'])
opts, args = getopt.getopt(sys.argv[1:], "h", longOptStrings)
if len(args) != 2:
print "The number of arguments must be 2"
usage()
exit()
numpy.set_printoptions(precision = 3, formatter = {'all':lambda x: "%.3f" % x})
inputDirectory = args[0]
outputDirectory = args[1]
writeHeaders = False
for o, a in opts:
if o == '--headers':
writeHeaders = True
if o == '-h':
usage()
quit()
#for o, a in opts:
if not os.path.isdir(inputDirectory):
criticalError("The input directory specified was not found")
actualInputFiles = CCSegSubjectSelection.selectSubjectsFromOpts(inputDirectory, opts = opts)
if len(actualInputFiles) == 0:
criticalError("No subjects to process")
allThicknessFID = open(os.path.join(outputDirectory, 'all_thickness_profiles.csv'), 'w')
allRegisteredThicknessFID = open(os.path.join(outputDirectory, 'all_registered_thickness_profiles.csv'), 'w')
allBendingAnglesFID = open(os.path.join(outputDirectory, 'all_bending_angles.csv'), 'w')
parcellationStatsDirectory = os.path.join(outputDirectory, 'parcellation_stats')
try:
os.makedirs(parcellationStatsDirectory)
except OSError as exc: # Python >2.5
if exc.errno == errno.EEXIST and os.path.isdir(parcellationStatsDirectory):
pass
else:
raise Exception
regionalStats = ['area', 'min', 'max', 'mean', 'median', 'std', 'var']
witelsonStatsFIDS = dict.fromkeys(regionalStats)
hoferFrahmStatsFIDS = dict.fromkeys(regionalStats)
emsellStatsFIDS = dict.fromkeys(regionalStats)
for curStat in regionalStats:
witelsonStatsFIDS[curStat] = open(os.path.join(parcellationStatsDirectory, 'witelson_stats_' + curStat + '.csv'), 'w')
hoferFrahmStatsFIDS[curStat] = open(os.path.join(parcellationStatsDirectory, 'hoferfrahm_stats_' + curStat + '.csv'), 'w')
emsellStatsFIDS[curStat] = open(os.path.join(parcellationStatsDirectory, 'emsell_stats_' + curStat + '.csv'), 'w')
headersWritten = False
for curFile in sorted(actualInputFiles):
outputBase = os.path.join(outputDirectory, curFile)
if os.path.isfile(outputBase + "_thickness.hdf5"):
FID = h5py.File(outputBase + "_thickness.hdf5", 'r')
T = numpy.array(FID['thicknessProfile'])
TR = numpy.ravel(numpy.array(FID['registeredThicknessProfileCov']))
S = numpy.array(FID['validStreamlines'])
B = numpy.array(FID['bendingAngle'])
T[numpy.where(S == 0)] = numpy.nan
if headersWritten == False and writeHeaders == True:
headersWritten = True
allThicknessFID.write('Subject,')
allRegisteredThicknessFID.write('Subject,')
allThicknessFID.write(",".join(['Node' + ("%03g" % x) for x in range(1, T.size + 1)]) + "\n")
allRegisteredThicknessFID.write(",".join(['Node' + ("%03g" % x) for x in range(1, T.size + 1)]) + "\n")
allBendingAnglesFID.write('Subject,BendingAngle\n')
allThicknessFID.write(curFile + ",")
allRegisteredThicknessFID.write(curFile + ",")
allBendingAnglesFID.write(curFile + ",")
#curThicknessProfile = numpy.array(FID['thicknessProfile'])
#print ','.join([str(x) for x in TR.tolist()]) + "\n"
allThicknessFID.write(','.join([str(x) for x in T.tolist()]) + "\n")
allRegisteredThicknessFID.write(','.join([str(x) for x in TR.tolist()]) + "\n")
allBendingAnglesFID.write(('%f\n' % (B)))
for curStat in regionalStats:
witelsonStatsFIDS[curStat].write(curFile + ',')
hoferFrahmStatsFIDS[curStat].write(curFile + ',')
emsellStatsFIDS[curStat].write(curFile + ',')
witelsonStatsFIDS[curStat].write(','.join([str(x) for x in numpy.array(FID['/witelsonStats/' + curStat]).tolist()]) + "\n")
hoferFrahmStatsFIDS[curStat].write(','.join([str(x) for x in numpy.array(FID['/hoferFrahmStats/' + curStat]).tolist()]) + "\n")
emsellStatsFIDS[curStat].write(','.join([str(x) for x in numpy.array(FID['/emsellStats/' + curStat]).tolist()]) + "\n")
FID.close()
pass
pass
allThicknessFID.close()
allRegisteredThicknessFID.close()
allBendingAnglesFID.close()
for curStat in regionalStats:
witelsonStatsFIDS[curStat].close()
hoferFrahmStatsFIDS[curStat].close()
emsellStatsFIDS[curStat].close()
if __name__ == "__main__":
main()