-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhelp_fct.py
103 lines (84 loc) · 3.66 KB
/
help_fct.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
from shared_configuration import N_PARCELS, TR, HCP_DIR, RUNS
from beh_configuration import HCP_BEH_DIR
from configuration import EXPERIMENTS
import numpy as np
import pandas as pd
# Page 49-50: https://www.humanconnectome.org/storage/app/media/documentation/s1200/HCP_S1200_Release_Reference_Manual.pdf
def get_region_info():
"""Return the region name and network assignment for each parcel in dict.
Returns:
* (dict): keys: ('name', 'network', 'hemi')
"""
regions = np.load(f"{HCP_DIR}/regions.npy").T
return dict(name=regions[0].tolist(), network=regions[1],
hemi=['Right']*int(N_PARCELS/2) + ['Left']*int(N_PARCELS/2),)
def load_single_timeseries(subject, experiment, run, remove_mean=True):
"""Load timeseries data for a single subject and single run.
Args:
subject (int): 0-based subject ID to load
experiment (str): Name of experiment
run (int): 0-based run index, across all tasks
remove_mean (bool): If True, subtract the parcel-wise mean (typically the mean BOLD signal is not of interest)
Returns
ts (n_parcel x n_timepoint np.array): Array of BOLD data values
"""
bold_run = EXPERIMENTS[experiment]['runs'][run]
bold_path = f"{HCP_DIR}/subjects/{subject}/timeseries"
bold_file = f"bold{bold_run}_Atlas_MSMAll_Glasser360Cortical.npy"
ts = np.load(f"{bold_path}/{bold_file}")
# print('ts shape:{}'.format(ts.shape))
if remove_mean:
ts -= ts.mean(axis=1, keepdims=True)
return ts
def beh_load_single_timeseries(subject, experiment, run, remove_mean=True):
"""Load timeseries data for a single subject and single run.
Args:
subject (str): subject ID to load
experiment (str): Name of experiment
run (int): (0 or 1)
remove_mean (bool): If True, subtract the parcel-wise mean (typically the mean BOLD signal is not of interest)
Returns
ts (n_parcel x n_timepoint array): Array of BOLD data values
"""
bold_run = RUNS[run]
bold_path = f"{HCP_BEH_DIR}/subjects/{subject}/{experiment}/tfMRI_{experiment}_{bold_run}"
bold_file = "data.npy"
ts = np.load(f"{bold_path}/{bold_file}")
if remove_mean:
ts -= ts.mean(axis=1, keepdims=True)
return ts
def load_evs(subject, experiment, run):
"""Load EVs (explanatory variables) data for one task experiment.
Args:
subject (int): 0-based subject ID to load
experiment (str) : Name of experiment
Returns
evs (list of lists): A list of frames associated with each condition
"""
frames_list = []
task_key = 'tfMRI_'+experiment+'_'+['RL','LR'][run]
for cond in EXPERIMENTS[experiment]['cond']:
ev_file = f"{HCP_DIR}/subjects/{subject}/EVs/{task_key}/{cond}.txt"
ev_array = np.loadtxt(ev_file, ndmin=2, unpack=True) #all (3, 1) shaped for _init_conds
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 trial
frames = [s + np.arange(0, d) for s, d in zip(start, duration)]
frames_list.append(frames)
return frames_list
def average_frames(data, evs, experiment, cond):
"""Averages all frames from any given condition.
Args:
* **data** (:xref:[type]): [description]
* **evs** (:xref:[type]): [description]
* **experiment** (:xref:[type]): [description]
* **cond** (:xref:[type]): [description]
Returns:
* ( np.array): [description]
"""
idx = EXPERIMENTS[experiment]['cond'].index(cond)
# print(evs[idx])
return np.mean(np.concatenate([np.mean(data[:,evs[idx][i]],axis=1,keepdims=True) for i in range(len(evs[idx]))],axis=-1),axis=1)