-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathextractSubdata.py
157 lines (132 loc) · 7.97 KB
/
extractSubdata.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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Thu Feb 21 09:16:09 2019
@author: Or Duek
In this script I will first take data for each subject for each run.
"""
#%% Specific subject data
data_dir = '/media/Data/FromHPC/output/fmriprep'
subject_id = 1063
def extractInfo (subject_id, data_dir):
# function should recieve subject id. Take subject runs (nifti files) and mat files
# then it will create a seperate Bunsh and contrasts for each subject.
# it needs to return filename, bunch of the run and contrasts for each run
import os
import pandas as pd
from nipype.interfaces.base import Bunch
os.chdir('/media/Data/work')
from readConditionFiles_r_aPTSD import loadmat, readConditions, organizeBlocks
eventsTotal = organizeBlocks(subject_id) # creates array of 8 arrays. One for each run
for i in range(len(eventsTotal)):
print (i)
eventsTotal[i]['condName'] = 'test'
for n in range(1,len(eventsTotal[i])+1):
if eventsTotal[i].condition[n] =='Gain':
if eventsTotal[i].trial_type[n] == 'risk':
eventsTotal[i]['condName'][n] = 'GainRisk'
else:
eventsTotal[i]['condName'][n] = 'GainAmb'
if eventsTotal[i].condition[n] == 'Loss':
if eventsTotal[i].trial_type[n] == 'risk':
eventsTotal[i]['condName'][n] = 'LossRisk'
else:
eventsTotal[i]['condName'][n] = 'LossAmb'
from bids.grabbids import BIDSLayout
layout = BIDSLayout(data_dir)
tasks = ['3','4','5','6'] # number of task (i.e. block. corresponding to file name)
source_epi = layout.get(type="bold", session="1", extensions="nii.gz", subject = subject_id) # get file list of subject
# grab confounds for the specific file
model_spec = []
contrastList =[]
filenames = []
condition = []
for r in range(len(tasks)):
print (source_epi[r])
confounds = pd.read_csv(os.path.join(data_dir,
"sub-%s"%subject_id, "ses-%s"%source_epi[r].session, "func",
"sub-%s_ses-%s_task-%s_desc-confounds_regressors.tsv"%(source_epi[r].subject, source_epi[r].session, tasks[r])),
sep="\t", na_values="n/a")
if eventsTotal[r].condition[r+1]=='Loss':
model_spec.insert(r,
Bunch(conditions = ['LossRisk', 'LossAmb'],
onsets = [list(eventsTotal[r][eventsTotal[r].condName=='LossRisk'].onset),
list(eventsTotal[r][eventsTotal[r].condName=='LossAmb'].onset)],
durations = [ list(eventsTotal[r][eventsTotal[r].condName=='LossRisk'].duration),
list(eventsTotal[r][eventsTotal[r].condName=='LossAmb'].duration)],
regressors=[list(confounds.framewise_displacement.fillna(0)),
list(confounds.a_comp_cor_00),
list(confounds.a_comp_cor_01),
list(confounds.a_comp_cor_02),
list(confounds.a_comp_cor_03),
list(confounds.a_comp_cor_04),
list(confounds.a_comp_cor_05),
],
regressor_names=['FramewiseDisplacement',
'aCompCor0',
'aCompCor1',
'aCompCor2',
'aCompCor3',
'aCompCor4',
'aCompCor5'],
) )
condition_names = ['LossRisk', 'LossAmb']
# GainRisk_cond = ['GainRisk','T', condition_names,[1,0,0,0]]
# GainAmb_cond = ['GainAmb','T',condition_names,[0,1,0,0]]
LossRisk_cond = ['LossRisk','T', condition_names,[1,0]]
LossAmb_cond = ['LossAmb','T',condition_names,[0,1]]
# Gain_all = ['Gain', 'F', [GainRisk_cond, GainAmb_cond]]
Loss_all = ['Loss', 'F', [LossRisk_cond, LossAmb_cond]]
contrasts = [LossRisk_cond, LossAmb_cond, Loss_all]
contrastList.insert(r, list(contrasts))
condition.insert(r, 'Loss')
elif eventsTotal[r].condition[r+1]=='Gain':
print ('Gain')
model_spec.insert(r,
Bunch(conditions = ['GainRisk','GainAmb'],
onsets = [list(eventsTotal[r][eventsTotal[r].condName=='GainRisk'].onset),
list(eventsTotal[r][eventsTotal[r].condName=='GainAmb'].onset)],
durations = [list(eventsTotal[r][eventsTotal[r].condName=='GainRisk'].duration),
list(eventsTotal[r][eventsTotal[r].condName=='GainAmb'].duration)],
regressors=[list(confounds.framewise_displacement.fillna(0)),
list(confounds.a_comp_cor_00),
list(confounds.a_comp_cor_01),
list(confounds.a_comp_cor_02),
list(confounds.a_comp_cor_03),
list(confounds.a_comp_cor_04),
list(confounds.a_comp_cor_05),
],
regressor_names=['FramewiseDisplacement',
'aCompCor0',
'aCompCor1',
'aCompCor2',
'aCompCor3',
'aCompCor4',
'aCompCor5'],
)
)
condition_names = ['GainRisk','GainAmb']
#LossRisk_cond = ['LossRisk','T', condition_names,[0,0,1,0]]
#LossAmb_cond = ['LossAmb','T',condition_names,[0,0,0,1]]
GainRisk_cond = ['GainRisk','T', condition_names,[1,0]]
GainAmb_cond = ['GainAmb','T',condition_names,[0,1]]
Gain_all = ['Gain', 'F', [GainRisk_cond, GainAmb_cond]]
#Loss_all = ['Loss', 'F', [LossRisk_cond, LossAmb_cond]]
contrasts = [GainRisk_cond, GainAmb_cond, Gain_all] #, LossRisk_cond, LossAmb_cond, Loss_all]
#
contrastList.insert(r, list(contrasts))
condition.insert(r, 'Gain')
filenames.insert(r, source_epi[r].filename)
# condition_names = ['GainRisk', 'GainAmb' ,'LossRisk', 'LossAmb']
#
# GainRisk_cond = ['GainRisk','T', condition_names ,[1,0,0,0]]
# GainAmb_cond = ['GainAmb','T', condition_names ,[0,1,0,0]]
# LossRisk_cond = ['LossRisk','T', condition_names,[0,0,1,0]]
# LossAmb_cond = ['LossAmb','T',condition_names,[0,0,0,1]]
# Risk_vs_Amb = ["Risk vs. Amb",'T', condition_names ,[0.5, -0.5, 0.5, -0.5]]
# Gain_vs_Loss = ["Gain vs. Loss",'T', condition_names ,[0.5, 0.5, -0.5, -0.5]]
#
# gain_total = ["Gain", 'F', [GainRisk_cond, GainAmb_cond]]
#loss_total = ["Loss", 'F', [LossAmb_cond, LossRisk_cond]]
# contrasts=[GainRisk_cond, GainAmb_cond, LossRisk_cond, LossAmb_cond, Risk_vs_Amb, Gain_vs_Loss] #, gain_total]#, loss_total]
return (model_spec, contrasts, filenames, condition)