-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpsy_process_lib.py
More file actions
391 lines (311 loc) · 11.6 KB
/
psy_process_lib.py
File metadata and controls
391 lines (311 loc) · 11.6 KB
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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
This script contains functions for processing fMRI data from the HCP2021 dataset for the
Working Memory task, curated for the NMA-CN 2021 session. The functions are to be used
as individual steps of processing in a script - see corresponding psy_process_runner.py
for such a pipeline.
@author: Pranay S. Yadav
"""
# %% Import libraries
import numpy as np
import pandas as pd
from pathlib import Path
from itertools import product
from nilearn.glm.first_level import make_first_level_design_matrix, run_glm
from nilearn.plotting import plot_design_matrix
from matplotlib import pyplot as plt
# %% Data-specific environment variables
# Voxel data has already been aggregated into ROIs from the Glasser parcellation
N_PARCELS = 360
# The acquisition parameters for all tasks were identical
TR = 0.72 # Time resolution, in seconds
# The parcels are matched across hemispheres with the same order
HEMIS = ["Right", "Left"]
# Each experiment was repeated twice in each subject
RUNS = {"LR": 7, "RL": 8}
N_RUNS = 2
# Task conditions for WM experiment
EXPERIMENTS = {
"WM": {
"cond": [
"0bk_body",
"0bk_faces",
"0bk_places",
"0bk_tools",
"2bk_body",
"2bk_faces",
"2bk_places",
"2bk_tools",
]
},
}
# %% Function definitions
def load_regions(fname):
"""
Load identifier information for all 360 ROIs - name, network, hemisphere for each
Parameters
----------
fname : str or Path object
Full path to file containing region data stored in npy container.
Returns
-------
region_info : pd.DataFrame
Dataframe of shape (360, 3) with identifiers for each ROI.
"""
# Convert to Path object if necessary
if not isinstance(fname, Path):
fname = Path(fname)
# Check if file exists and has correct extension
assert fname.exists(), "File doesn't exist"
assert fname.suffix == ".npy", "File doesn't have .npy extension"
# Load data, convert to dataframe with appropriate labels and return
regions = np.load(fname).T
region_info = pd.DataFrame(
dict(
name=regions[0].tolist(),
network=regions[1],
hemi=["Right"] * int(N_PARCELS / 2) + ["Left"] * int(N_PARCELS / 2),
)
)
return region_info
def load_single_EVs(HCP_DIR, subject, run):
"""
Load explanatory variables with onsets, conditions, accuracies, stimulus identifiers
Parameters
----------
HCP_DIR : str or Path object
Full path to root directory containing dataset.
subject : int
Subject ID to load.
run : str
Run to load, LR or RL.
Returns
-------
df : pd.DataFrame
Dataframe with onsets and metadata for all 80 trials in specified run.
"""
# Prepare path to EV files
f = Path(HCP_DIR) / f"subjects/{subject}/EVs/tfMRI_WM_{run}"
# Read files containing trial onsets for correct and incorrect trials
df_cor = pd.read_csv(
f / "all_bk_cor.txt",
header=None,
sep="\t",
names=["onset", "duration", "modulation"],
)
df_err = pd.read_csv(
f / "all_bk_err.txt",
header=None,
sep="\t",
names=["onset", "duration", "modulation"],
)
df_cor["accuracy"] = "correct"
df_err["accuracy"] = "incorrect"
# Concatenate both, add subject/run identifiers, placeholders for condition/stimulus
df = pd.concat([df_cor, df_err]).sort_values("onset").reset_index(drop=True)
df["condition"] = ""
df["stimulus"] = ""
df["trial_type"] = [f"trial_{x:03}" for x in range(len(df))]
df["subject"] = subject
df["run"] = run
# Iterate over individual files with block onsets and aggregate
df_block_onsets = []
for cond, stim in product(["0bk", "2bk"], ["body", "faces", "places", "tools"]):
dat = pd.read_csv(
f / f"{cond}_{stim}.txt",
header=None,
sep="\t",
names=["onset", "duration", "modulation"],
)
# Add condition and stimulus identifiers
dat["condition"] = cond
dat["stimulus"] = stim
df_block_onsets.append(dat)
# Combine aggregated block onsets
df_block_onsets = pd.concat(df_block_onsets)
df_block_onsets = df_block_onsets.sort_values("onset").reset_index(drop=True)
# Merge stimulus and condition labels based on block onsets
for n, row in df_block_onsets.iterrows():
# Get first 10 trials with onsets occurring after a block onset
idx = df[row["onset"] < df["onset"]].index[:10] # 1 block = 10 trials in exp
# Update identifiers
df.loc[idx, "condition"] = row["condition"]
df.loc[idx, "stimulus"] = row["stimulus"]
return df
def load_single_EVs_legacy(HCP_DIR, subject, run):
"""
Load explanatory variables with condition & stimulus identifiers, with frame numbers
Note: This does not contain onsets in units of time, avoid for GLM, legacy version
based on code from notebook provided in NMA-CN.
Parameters
----------
HCP_DIR : str or Path object
Full path to root directory containing dataset.
subject : int
Subject ID to load.
run : str
Run to load, LR or RL.
Returns
-------
df : pd.DataFrame
Dataframe containing frame-by-frame identifiers for condition & stimulus.
"""
frames_list = []
# Iterate over individual files with block onsets
for cond, stim in product(["0bk", "2bk"], ["body", "faces", "places", "tools"]):
# Load data
ev_file = f"{HCP_DIR}/subjects/{subject}/EVs/tfMRI_WM_{run}/{cond}_{stim}.txt"
ev_array = np.loadtxt(ev_file, ndmin=2, unpack=True)
ev = dict(zip(["onset", "duration", "amplitude"], ev_array))
# Determine when trial starts, rounded down
start = np.floor(ev["onset"] / TR).astype(int)
# Use trial duration to determine how many frames to include for trial
duration = np.ceil(ev["duration"] / TR).astype(int)
# Take the range of frames that correspond to this specific block
frames = [s + np.arange(0, d) for s, d in zip(start, duration)]
frames_list.append(
{"condition": cond, "stimulus": stim, "frame_idx": frames[0]}
)
# Convert messy list of dicts to clean dataframe with identifiers
df = (
pd.DataFrame(frames_list)
.explode("frame_idx")
.sort_values("frame_idx")
.reset_index(drop=True)
)
df["subject"] = subject
df["run"] = run
return df
def load_single_timeseries(HCP_DIR, subject, run, regions, remove_mean=True):
"""
Load timeseries data for a single subject and single run.
Parameters
----------
HCP_DIR : str or Path object
Full path to root directory containing dataset.
subject : int
Subject ID to load.
run : str
Run to load, LR: 7, RL: 8.
regions: pd.DataFrame
Identifiers for all 360 parcels obtained from load_regions()
remove_mean : bool, optional
Subtract parcel-wise mean BOLD signal. The default is True.
Returns
-------
ts : pd.DataFrame
Dataframe of shape (n_frames, n_parcels) containing BOLD values.
Full data per subject per run has size (405, 360)
"""
# Prepare filename from input arguments
bold_run = RUNS[run]
bold_path = f"{HCP_DIR}/subjects/{subject}/timeseries/"
bold_file = f"bold{bold_run}_Atlas_MSMAll_Glasser360Cortical.npy"
# Load data and remove mean if requested
ts = np.load(f"{bold_path}/{bold_file}")
if remove_mean:
ts -= ts.mean(axis=1, keepdims=True)
# Convert to Dataframe, add identifiers and return
ts = pd.DataFrame(ts, index=[regions["name"], regions["network"]]).T
ts["subject"] = subject
ts["run"] = run
ts["frame_idx"] = range(len(ts))
return ts
def extract_task_activity(timeseries, legacy_EVs):
"""
Extract 312 frames containing task activity from full activity matrix, given legacy
frame-by-frame EVs
Parameters
----------
timeseries : pd.DataFrame, shape = (405, 360+3)
Dataframe containing raw BOLD activity with region labels.
legacy_EVs : pd.DataFrame, shape = (312, 3)
Dataframe containing frame-by-frame identifiers for condition & stimulus.
Obtain using load_single_EVs_legacy()
Returns
-------
dat : pd.DataFrame, shape = (312, 360+5)
Dataframe containing raw BOLD activity with region labels & frame-by-frame EVs.
"""
# Index subset of frames based on frame indices for each block in legacy_EVs
dat = timeseries.loc[legacy_EVs["frame_idx"].to_numpy(dtype="int"), :]
# Add condition and stimulus identifiers
dat["condition"] = legacy_EVs["condition"].to_numpy()
dat["stimulus"] = legacy_EVs["stimulus"].to_numpy()
return dat
def construct_design_matrix(EVs, plot=False):
"""
Construct full design matrix for trial-level estimates of first-level betas.
Uses 'spm' HRF model with polynomial (5) order drift and without derivatives.
Parameters
----------
EVs : pd.DataFrame
Dataframe with onsets and metadata for all 80 trials in specified run.
Obtain using load_single_EVs()
Returns
-------
design_matrix : pd.DataFrame
Dataframe containing full design matrix with columns as regressors.
"""
frame_times = frame_times = np.arange(0, TR * 405, TR)
design_matrix = make_first_level_design_matrix(frame_times, EVs, hrf_model="spm")
if plot:
plot_design_matrix(design_matrix)
return design_matrix
def fit_first_level_glm(timeseries, design_matrix):
"""
Fit first-level GLM to get trial-level betas based on regressors from design matrix.
Parameters
----------
timeseries : pd.DataFrame, shape = (405, 360+k)
Dataframe containing raw BOLD activity with region labels.
design_matrix : pd.DataFrame
Dataframe containing full design matrix with columns as regressors.
Returns
-------
betas : pd.DataFrame, shape = (80, 360)
Beta estimates for each trial and parcel.
"""
# Get only numeric part of dataframe corresponding to BOLD activity
dat = timeseries.iloc[:, :360]
# Fit GLM using OLS
labels, results = run_glm(
Y=dat, X=design_matrix, noise_model="ols", n_jobs=-1, verbose=5,
)
# Extract betas for each trial and prepare clean dataframe
betas = pd.DataFrame(
results[0.0].theta[:80, :],
index=design_matrix.columns[:80],
columns=dat.columns,
)
return betas
def compute_betas(timeseries, EVs):
"""
Compute first-level beta estimates from timeseries data based on relevant variables.
Wrapper around construct_design_matrix() and fit_first_level_glm().
Parameters
----------
timeseries : pd.DataFrame, shape = (405, 360+k)
Dataframe containing raw BOLD activity with region labels.
EVs : pd.DataFrame
Dataframe with onsets and metadata for all 80 trials in specified run.
Obtain using load_single_EVs()
Returns
-------
betas : pd.DataFrame, shape = (80, 360 + k)
Beta estimates for each trial and parcel with identifiers.
"""
# Construct design matrix
design_matrix = construct_design_matrix(EVs, plot=False)
# Compute beta estimates
betas = fit_first_level_glm(timeseries, design_matrix)
# Add identifiers and return
betas = (
betas.join(
pd.concat([EVs.set_index("trial_type")], axis=1, keys=["identifiers"])
)
.reset_index()
.rename(columns={"index": "trial_number"})
)
return betas