-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathThreshVoxelStatistics.py
124 lines (108 loc) · 3.7 KB
/
ThreshVoxelStatistics.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
"""
get stats for thresholded maps
"""
import os
import glob
import numpy
import pandas
import argparse
from narps import Narps
from ValueDiagnostics import compare_thresh_unthresh_values
def get_thresh_voxel_stats(basedir):
# load cluster maps
data_dir = os.path.join(
basedir,
'image_diagnostics_zstat')
datafiles = glob.glob(os.path.join(
data_dir, '*'
))
datafiles.sort()
all_data = None
print('found %d data files' % len(datafiles))
for d in datafiles:
df = pandas.read_csv(d)
if all_data is None:
all_data = df
else:
all_data = pandas.concat([all_data, df])
hypnums = all_data.hyp.unique()
results_df = pandas.DataFrame({
'Hyp #': hypnums,
'Minimum sig voxels': None,
'Maximum sig voxels': None,
'Median sig voxels': None,
'N empty images': None})
for hyp in hypnums:
hypdata = all_data.query('hyp == %d' % hyp)
results_df.loc[
results_df['Hyp #'] == hyp,
'Minimum sig voxels'] = hypdata.n_thresh_vox.min()
results_df.loc[
results_df['Hyp #'] == hyp,
'Maximum sig voxels'] = hypdata.n_thresh_vox.max()
results_df.loc[
results_df['Hyp #'] == hyp,
'Median sig voxels'] = hypdata.n_thresh_vox.median()
results_df.loc[
results_df['Hyp #'] == hyp,
'N empty images'] = numpy.sum(hypdata.n_thresh_vox == 0)
results_df.to_csv(os.path.join(
basedir, 'metadata/thresh_voxel_statistics.csv'),
index=False)
all_data.to_csv(os.path.join(
basedir, 'metadata/thresh_voxel_data.csv'
))
print(results_df)
return(None)
# run diagnostics on zstat images
def get_zstat_diagnostics(narps,
verbose=True,
overwrite=False):
for teamID in narps.teams:
collectionID = '%s_%s' % (
narps.teams[teamID].NV_collection_id,
teamID)
if verbose:
print(collectionID)
logfile = os.path.join(
narps.dirs.dirs['logs'],
'zstat_diagnostics.log')
diagnostics_file = os.path.join(
narps.dirs.dirs['image_diagnostics_zstat'],
'%s.csv' % collectionID)
if not os.path.exists(diagnostics_file)\
or overwrite:
image_diagnostics = compare_thresh_unthresh_values(
narps.dirs, collectionID, logfile,
unthresh_dataset='zstat',
thresh_dataset='zstat')
if image_diagnostics is not None:
image_diagnostics.to_csv(diagnostics_file)
if __name__ == "__main__":
# parse arguments
parser = argparse.ArgumentParser(
description='Get stats for thresholded maps')
parser.add_argument('-b', '--basedir',
help='base directory')
parser.add_argument('-t', '--test',
action='store_true',
help='use testing mode (no processing)')
args = parser.parse_args()
# set up base directory
if args.basedir is not None:
basedir = args.basedir
elif 'NARPS_BASEDIR' in os.environ:
basedir = os.environ['NARPS_BASEDIR']
print("using basedir specified in NARPS_BASEDIR")
else:
basedir = '/data'
print("using default basedir:", basedir)
# setup main class
narps = Narps(basedir)
narps.load_data()
# Load full metadata and put into narps structure
narps.metadata = pandas.read_csv(
os.path.join(narps.dirs.dirs['metadata'], 'all_metadata.csv'))
if not args.test:
get_zstat_diagnostics(narps)
get_thresh_voxel_stats(narps.basedir)