-
Notifications
You must be signed in to change notification settings - Fork 1
/
load_data.py
413 lines (321 loc) · 12.8 KB
/
load_data.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
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
"""
dataset loader.
@author Caleb Geniesse
"""
from __future__ import print_function, division
from __future__ import unicode_literals
import os
import re
import glob
from collections import defaultdict
import logging
logger = logging.getLogger(__name__)
logging.basicConfig(level=logging.DEBUG)
import numpy as np
import pandas as pd
import scipy as sp
import scipy.stats
from sklearn.datasets.base import Bunch
#from sklearn.preprocessing import LabelEncoder
#from nilearn.input_data import NiftiLabelsMasker
#from nilearn.image import load_img
#from nilearn.signal import clean
#from nilearn.datasets import fetch_atlas_msdl
class ResourceConfig(object):
# walk back, until we find base directory
base_dir = os.path.dirname(os.path.abspath(__file__))
while len(base_dir) and not os.path.exists(os.path.join(base_dir, 'data')):
base_dir = os.path.dirname(base_dir)
# define some paths
data_dir = os.path.join(base_dir, 'data/myconnectome/base/')
data_scrubbed_dir = os.path.join(data_dir, 'combined_data_scrubbed')
data_tmask_dir = os.path.join(data_dir, 'rsfmri/tmasks')
data_behavior_dir = os.path.join(data_dir, 'behavior')
data_parcel_dir = os.path.join(data_dir, 'parcellation')
# init _config
_config = ResourceConfig()
def fetch_data(**kwargs):
""" Fetch my connectome data.
Just print command to download data for now... too long to run in-script.
"""
url = "http://web.stanford.edu/group/poldracklab/myconnectome-data/base/combined_data_scrubbed"
cmd = "wget -N -r -l inf --no-remove-listing -nH --cut-dirs=3 {}".format(url)
print("Run the following in a new Jupyter cell to fetch data:\n")
print("%%bash")
print("mkdir -p data")
print("cd data")
print("{}".format(cmd))
# import subprocess as sp
# o = sp.check_output(cmd, shell=True)
#
return True
##############################################################################
### mask helpers
##############################################################################
def get_RSN_rmask(atlas, n=None, minor=False, ignore=['Zero', 'na'], **kwargs):
""" Return region mask based on RSNs
Inputs
------
:atlas = Bunch
:n = int, return rmask for first n networks
:minor = bool, return rmask for minor networks (major by default)
"""
rmask = atlas.data.copy()
# get list of RSNs sorted by size
RSNs = rmask[~rmask.network.isin(ignore)].groupby('network')
RSNs = RSNs.network.count().sort_values(ascending=minor)
RSNs = RSNs.index.tolist()[:n]
# define rmask based on RSNs
rmask = rmask.assign(data_id = rmask.index)
rmask = rmask.assign(rmask = rmask.network.isin(RSNs))
rmask = rmask.loc[rmask.rmask, ['data_id', 'region', 'network', 'rmask']]
#rmask = rmask.assign(group = rmask.network.map(RSNs.index))
return rmask
def get_session_tmask(meta, session=None, **kwargs):
""" Return temporal mask for subject(s)
Inputs
------
:data = data to mask
:session = subcode (load all by default, assumes data for all sessions)
"""
def glob_tmask(subcode=None):
subcode = 'sub???' if subcode is None else subcode.split('.txt')[0]
glob_str = os.path.join(_config.data_tmask_dir, subcode + '.txt')
found = sorted(glob.glob(glob_str))
return found
def load_tmask(filename):
tmask = pd.read_csv(filename, header=None, names=['tmask'], dtype=bool)
tmask = tmask.assign(session=os.path.basename(filename).split('.txt')[0])
tmask = tmask.assign(tr_id=tmask.index)
return tmask
# glob tmask paths (subject specific)
tmask_paths = sorted(__ for _ in np.ravel(session) for __ in glob_tmask(_))
# load tmasks
tmask = pd.concat(map(load_tmask, tmask_paths), ignore_index=True, sort=False)
# joint to meta
tmask = meta.copy().reset_index(drop=False).join(
tmask.set_index(['session', 'tr_id']),
how='inner', on=['session', 'tr_id'],
)
# assign data_id, return
tmask = tmask.fillna(False).assign(data_id = tmask.index)
tmask = tmask.loc[tmask.tmask, ['data_id', 'session', 'tr_id', 'tmask']]
return tmask
##############################################################################
### meta data helper functions
##############################################################################
def load_atlas(atlas_file=None):
""" Load parcellation / atlas data.
"""
if atlas_file is None:
atlas_file = os.path.join(_config.data_parcel_dir, "parcel_data.txt")
# load parcellation into DataFrame
df_parcel = pd.read_table(atlas_file, header=None)
# relabel columns
df_parcel = df_parcel.rename(
columns={
0:'target',
1:'hemisphere',
2:'x',
3:'y',
4:'z',
5:'region',
6:'subregion',
7:'network'
})
# region_coords: (x, y, z)
df_coords = df_parcel[['x', 'y', 'z']].copy()
# regions: string list of region labels
df_regions = df_parcel[['region']].copy()
# networks: names of the networks
df_networks = df_parcel[['network']].copy()
# Bunch
atlas = Bunch(
data=df_parcel,
regions=df_regions,
region_coords=df_coords,
networks=df_networks,
)
return atlas
def clean_meta(df, columns=None, zscore_meta=False, **kwargs):
""" Clean meta DataFrame, zscore.
"""
# copy input df
df_clean = df.copy()
# select columns
if columns is not None:
df_clean = df_clean[columns]
# replace non-numerics
df_clean = df_clean.replace('.', 0.0)
df_numeric = df_clean.astype(str).applymap(str.isnumeric)
df_clean[df_numeric==False] = np.nan
df_clean = df_clean.fillna(0.0)
# only keep numeric data, convert to float
df_clean = df_clean.astype(float)
# z-score values
if zscore_meta is True:
good_rows = df_clean.any(axis=1) & df_clean.std(axis=1).gt(0)
good_cols = df_clean.any(axis=0) & df_clean.std(axis=0).gt(0)
df_nonzero = df_clean.loc[good_rows, good_cols]
df_clean.loc[good_rows, good_cols] = scipy.stats.zscore(df_nonzero, axis=0)
# fill nans
df_clean = df_clean.fillna(0.0)
df_clean = df_clean.loc[:, df_clean.any(axis=0)]
# return cleaned
return df_clean
def combine_sessions(sessions, **kwargs):
""" Merge session data sets in single data set.
"""
# make a copy of the sessions, just to be safe
sessions_ = list(sessions)
# define dataset based on first session
dataset_ = None
# append data from other sessions
for i, session_ in enumerate(sessions_):
print("[+] session: {}, file: {}".format(
i, session_.meta.reset_index(drop=False).session[0])
)
if dataset_ is None:
dataset_ = Bunch(**dict(session_))
else:
dataset_.data = dataset_.data.append(session_.data, ignore_index=True, sort=False)
dataset_.meta = dataset_.meta.append(session_.meta, ignore_index=False, sort=False)
dataset_.tmask = dataset_.tmask.append(session_.tmask, ignore_index=False, sort=False)
# clean
if kwargs.get('clean_meta'):
dataset_.meta = clean_meta(dataset_.meta, **kwargs).reset_index(drop=False)
# set X, y
dataset_.X = dataset_.data.values.reshape(-1, dataset_.data.shape[-1])
dataset_.y = dataset_.meta.values.reshape(-1, dataset_.meta.shape[-1])
# cache sessions
dataset_.sessions = list(sessions)
return dataset_
##############################################################################
### main loaders
##############################################################################
def load_scrubbed(**kwargs):
""" Loads scrubbed data
"""
logger = logging.getLogger(__name__)
logger.info('load_scrubbed(**{})'.format(kwargs))
# random seed
if kwargs.get('seed') is not None:
np.random.seed(kwargs.get('seed'))
# data paths
_config = ResourceConfig()
glob_str = os.path.join(_config.data_scrubbed_dir, "sub???.txt")
data_paths = sorted(glob.glob(glob_str))
# shuffle data paths
shuffle = int(kwargs.get('shuffle', 0))
for _ in range(shuffle):
logging.debug("Shuffle paths...")
np.random.shuffle(data_paths)
# get tmasks, meta
tmask_paths = [os.path.join(
_config.data_tmask_dir, os.path.basename(_)
) for _ in data_paths]
# meta paths
glob_str = os.path.join(
_config.data_behavior_dir, 'trackingdata_goodscans.txt')
meta_paths = sorted(glob.glob(glob_str))
# hacky, but just print command to fetch data, if not already
if len(data_paths) < 1:
fetch_data()
return None
# how many sessions to load?
n_sessions = kwargs.get('n_sessions', -1)
n_sessions_split = kwargs.get('n_sessions_split')
if n_sessions == -1:
n_sessions = len(data_paths)
# zscoring
zscore = kwargs.get('zscore')
if zscore is True:
kwargs.update(
zscore_data=kwargs.get('zscore_data', zscore),
zscore_meta=kwargs.get('zscore_meta', zscore),
)
# check sizes
logger.debug('found {} data files'.format(len(data_paths)))
logger.debug('found {} tmask files'.format(len(tmask_paths)))
logger.debug('found {} meta files'.format(len(meta_paths)))
logger.debug('using {} sessions'.format(n_sessions))
# load data ?
logger.info("Loading data...")
dataset = []
for i, data_path in enumerate(data_paths):
if i >= n_sessions:
break
print("[+] session: {}, file: {}".format(
i, os.path.basename(data_path))
, end=" | ")
# load dataframe
df_data = pd.read_csv(data_path, header=None, delim_whitespace=True)
# load meta as tr_id (for now...)
df_meta = df_data.assign(tr_id = df_data.index.values)[['tr_id']]
# parse session, session_id from file
session = os.path.basename(data_path).split('.txt')[0]
session_id = int(''.join([__ for __ in session if __.isdigit()]))
df_meta = df_meta.assign(session=session, session_id=session_id)
# join with other meta files
df_meta = df_meta.join(
pd.concat(pd.read_table(_,index_col='subcode') for _ in meta_paths)
, how='left', on='session'
)
# load atlas
atlas = load_atlas()
# load tmask (subject specific)
df_tmask = get_session_tmask(df_meta, session=session, **dict(kwargs.get('tmask_kwds', {})))
if kwargs.get('apply_tmask'):
df_data = df_data.loc[df_tmask.data_id, :]
df_meta = df_meta.loc[df_tmask.data_id, :]
print("keeping: {} (time points)".format(df_data.shape[0]), end=' | ')
# load rmask (region specific)
df_rmask = get_RSN_rmask(atlas, **dict(kwargs.get('rmask_kwds', {})))
if kwargs.get('apply_rmask'):
df_data = df_data.loc[:, df_rmask.data_id]
print("keeping: {} (regions)".format(df_data.shape[-1]), end=' | ')
# clean data, meta
#df_data = clean_data(data=df_data, meta=df_meta, **kwargs)
#df_meta = clean_meta(df_meta, **kwargs)
# z score (?)
if kwargs.get('zscore_data'):
print("zscore: {} zscore_data: {}"
.format(zscore, kwargs.get('zscore_data'))
, end='\t')
df_data.loc[:] = scipy.stats.zscore(df_data.values, axis=0)
#masker = NiftiLabelsMasker(
# labels_img=atlas_paths[0],
# memory="nilearn_cache"
# )
#masker = masker.fit()
# low pas filter
#cleaned_ = clean(df_data.values,
# low_pass=0.09, high_pass=0.008
# )
#df_data.iloc[:, :] = cleaned_
# reset meta index
df_meta = df_meta.set_index(['day_of_week', 'session', 'session_id', 'tr_id'])
# save masker, x
dataset.append(Bunch(
data=df_data.copy(),#.fillna(0),
meta=df_meta.copy(), #.fillna(0),
#masker=masker,
X=df_data.values.copy(),
y=df_meta.values.copy(),
# masks
tmask=df_tmask.copy(),
rmask=df_rmask.copy(),
# atlas
atlas=atlas,
))
print()
# return dataset as Bunch
if kwargs.get('merge'):
# merge and return
dataset = combine_sessions(dataset, **kwargs)
elif kwargs.get('clean_meta'):
for i, session in enumerate(dataset):
meta = clean_meta(session.meta, **kwargs).reset_index(drop=False)
dataset[i].meta = meta.copy()
dataset[i].y = meta.set_index(['day_of_week', 'session']).values.copy()
return dataset