-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmribrew_rsfmri_tc2fc.py
50 lines (39 loc) · 1.59 KB
/
mribrew_rsfmri_tc2fc.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
import os
import numpy as np
import pandas as pd
from mribrew.data_io import read_mat
from mribrew.atlases import fetch_dk_atlas
import ray
ray.init()
# define the data directory and other constants
data_dir = '/Users/to8050an/Documents/data/BF2/aparcaseg_rs_timecourses'
subfolder = 'aparcaseg'
tc_fname = 'aparcaseg.TC.mat'
labels_fname = 'aparcaseg.labels.mat'
export_dir = '/Users/to8050an/Documents/data/BF2/fc'
@ray.remote
def process_subject(mid):
try:
# read all time courses and labels
tc_f_labels = read_mat(os.path.join(data_dir, mid, subfolder, labels_fname)).flatten()
tc_f = read_mat(os.path.join(data_dir, mid, subfolder, tc_fname))
# select only labels which are present in dk_labels
_, labels = fetch_dk_atlas(lut_idx=True)
# timecourses into df where rows = regions and columns = timepoints
df_tc = pd.DataFrame(tc_f, index=tc_f_labels)
df_tc = df_tc.loc[labels.values()]
# calculate functional connectivity via pearson correlation
df_fc = df_tc.T.corr()
# export the functional connectivity matrix
export_dir_ = os.path.join(export_dir, mid)
os.makedirs(export_dir_, exist_ok=True)
df_fc.to_csv(os.path.join(export_dir_, 'fc_aparcaseg.csv'))
print(f'Successfully processed {mid}')
except Exception as e:
print(f'Error processing {mid}: {e}')
# list of subjects (directory names)
subjects = next(os.walk(data_dir))[1]
# run the processing function for all subjects in parallel
futures = [process_subject.remote(mid) for mid in subjects]
ray.get(futures)
ray.shutdown()