-
Notifications
You must be signed in to change notification settings - Fork 0
/
dataframe_constructor_monopolar.py
265 lines (191 loc) · 7.82 KB
/
dataframe_constructor_monopolar.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
from os.path import *
import sys
sys.path.append('/Users/m/PTSA_NEW_GIT')
from ptsa.data.readers import TalReader,TalStimOnlyReader
mount_point = '/Volumes/rhino_root'
from brain_plot_utils import *
import pandas as pd
import numpy as np
# def extract_electrode_positions(tal_path, electrode_types=['D', 'G', 'S']):
# from ptsa.data.readers import TalReader
# tal_reader = TalReader(filename=tal_path)
# tal_structs = tal_reader.read()
#
# lh_selector = np.array(map(lambda loc: loc.upper().startswith('L'), tal_structs.tagName))
# rh_selector = np.array(map(lambda loc: loc.upper().startswith('R'), tal_structs.tagName))
#
# electrode_types_lower = map(lambda t: t.lower(), electrode_types)
#
# electrode_type_selector = np.array(map(lambda eType: eType.lower() in electrode_types_lower, tal_structs.eType))
#
# lh_data = tal_structs[['avgSurf','eType']]
# rh_data = tal_structs[['avgSurf','eType']]
#
# lh_data = lh_data[lh_selector & electrode_type_selector]
# rh_data = rh_data[rh_selector & electrode_type_selector]
#
# return lh_data,rh_data
#
# def append_to_combined(combined,*arrays):
# tmp_array = None
# for array in arrays:
#
# if array is None: continue
#
# if tmp_array is None:
# if len(array):
# tmp_array = array
# elif array is not None and len(array):
# tmp_array = np.vstack(tmp_array,array)
#
# if combined is None and tmp_array is not None and len(tmp_array):
# combined = tmp_array
# elif tmp_array is not None and len(tmp_array):
# try:
# combined = np.vstack((combined,tmp_array))
# except:
# print combined
# print tmp_array
# return combined
def get_elec_data_coords_array(hemi_data):
dtype_avgSurf = [('x_snap', '<f8'), ('y_snap', '<f8'),('z_snap', '<f8')]
if hemi_data is not None and len(hemi_data):
tmp_array = np.array([((hemi_data.avgSurf.x_snap,hemi_data.avgSurf.y_snap,hemi_data.avgSurf.z_snap),hemi_data.eType[0],hemi_data.tagName[0])],
dtype=[('avgSurf', dtype_avgSurf),('eType','|S256'),('tagName','|S256')])
return tmp_array
else:
return None
def get_tal_structs_row(subject,anode_tag,cathode_tag):
# '/Users/m/data/eeg/R1111M/tal/R1111M_talLocs_database_bipol.mat'
# tal_path = join(mount_point,'data/eeg/',subject,'tal',subject+'_talLocs_database_bipol.mat')
tal_path = str(join(mount_point,'data/eeg/',subject,'tal',subject+'_talLocs_database_monopol.mat'))
tal_reader = TalReader(filename=tal_path,struct_name='talStruct')
tal_structs = tal_reader.read()
# sel = tal_structs[np.where(tal_structs.tagName == anode_tag+'-'+cathode_tag)]
sel = tal_structs[np.where((tal_structs.tagName == anode_tag)|(tal_structs.tagName == cathode_tag))]
if not len(sel):
sel = tal_structs[np.where((tal_structs.tagName == anode_tag)|(tal_structs.tagName == cathode_tag))]
if not len(sel):
tal_path = str(join(mount_point,'data/eeg/',subject,'tal',subject+'_talLocs_database_stimOnly.mat'))
tal_reader = TalStimOnlyReader(filename=tal_path)
tal_structs = tal_reader.read()
sel = tal_structs[np.where((tal_structs.tagName == anode_tag)|(tal_structs.tagName == cathode_tag))]
if not len(sel):
sel = tal_structs[np.where((tal_structs.tagName == anode_tag)|(tal_structs.tagName == cathode_tag))]
return sel
def construct_elec_dataframe(df):
tal_structs = None
subject = ''
lh_selector = None
rh_selector = None
lh_data_combined = None
rh_data_combined = None
x = np.zeros(shape=(len(tdf['Subject'])),dtype=np.float)
y = np.zeros(shape=(len(tdf['Subject'])),dtype=np.float)
z = np.zeros(shape=(len(tdf['Subject'])),dtype=np.float)
eType = np.zeros(shape=(len(tdf['Subject'])),dtype='|S256')
monopol_elec_data = []
for index, row in df.iterrows():
if subject != row['Subject']:
subject = row['Subject']
print subject
# # '/Users/m/data/eeg/R1111M/tal/R1111M_talLocs_database_bipol.mat'
# tal_path = join(mount_point,'data/eeg/',subject,'tal',subject+'_talLocs_database_bipol.mat')
# tal_reader = TalReader(filename=tal_path)
# tal_structs = tal_reader.read()
#
# sel = tal_structs[np.where(tal_structs.tagName == row['stimAnodeTag']+'-'+row['stimCathodeTag'])]
# if not len(sel):
# sel = tal_structs[np.where(tal_structs.tagName == row['stimCathodeTag']+'-'+row['stimAnodeTag'])]
sel = get_tal_structs_row(subject=subject,anode_tag=row['stimAnodeTag'],cathode_tag=row['stimCathodeTag'])
for s in sel:
monopol_elec_data.append([subject,s.tagName,s.avgSurf.x,s.avgSurf.y,s.avgSurf.z,s.eType])
# try:
# x[index] = sel[0].avgSurf.x_snap
# y[index] = sel[0].avgSurf.y_snap
# z[index] = sel[0].avgSurf.z_snap
# eType[index] = sel[0].eType
#
# # df.x[index] = sel[0].avgSurf.x_snap
# # df.y[index] = sel[0].avgSurf.y_snap
# # df.z[index] = sel[0].avgSurf.z_snap
# # df.eType[index] = sel[0].eType
# except IndexError:
# print row
#
# except AttributeError:
# print row
#
# df['x']=x
# df['y']=y
# df['z']=z
# df['eType']=eType
monopolar_df = pd.DataFrame(monopol_elec_data,columns=['subject','tagName','x','y','z','eType'])
monopolar_df = monopolar_df.drop_duplicates(cols=['subject','tagName'])
return monopolar_df
def extend_elec_dataframe(df):
tal_structs = None
subject = ''
lh_selector = None
rh_selector = None
lh_data_combined = None
rh_data_combined = None
x = np.zeros(shape=(len(tdf['Subject'])),dtype=np.float)
y = np.zeros(shape=(len(tdf['Subject'])),dtype=np.float)
z = np.zeros(shape=(len(tdf['Subject'])),dtype=np.float)
eType = np.zeros(shape=(len(tdf['Subject'])),dtype='|S256')
monopol_elec_data = []
for count, (index, row) in enumerate(df.iterrows()):
# print count, index, row
if subject != row['Subject']:
subject = row['Subject']
print subject
sel = get_tal_structs_row(subject=subject,anode_tag=row['stimAnodeTag'],cathode_tag=row['stimCathodeTag'])
for s in sel:
monopol_elec_data.append([subject,s.tagName,s.avgSurf.x,s.avgSurf.y,s.avgSurf.z,s.eType])
if len(sel):
x[count] = sel[0].avgSurf.x
y[count] = sel[0].avgSurf.y
z[count] = sel[0].avgSurf.z
eType[count] = sel[0].eType
else:
x[count] = np.nan
y[count] = np.nan
z[count] = np.nan
eType[count] = ''
df['x'] = x
df['y'] = y
df['z'] = z
df['eType'] = eType
return df
# # EXTENSION FO DATAFRAME
# ttest_table_df_filename = 'ttest_table_params.csv'
#
# tdf = pd.read_csv(ttest_table_df_filename)
#
# elec_coord_tdf = extend_elec_dataframe(tdf)
#
# print elec_coord_tdf
#
# # EXTENSION FO DATAFRAME
# # ORIGINAL CODE
# ttest_table_df_filename = 'ttest_table_params.csv'
#
# tdf = pd.read_csv(ttest_table_df_filename)
#
# elec_coord_tdf = construct_elec_dataframe(tdf)
#
# elec_coord_tdf.to_csv('coords_'+ttest_table_df_filename)
#
#
# print new_tdf
#
# new_tdf.to_csv('ttest_table_params_with_location.csv')
ttest_table_df_filename = 'ps_aggregator_significance_table_05_26'
tdf = pd.read_excel(ttest_table_df_filename+'.xlsx')
tdf.to_csv(ttest_table_df_filename+'.csv')
# elec_coord_tdf = construct_elec_dataframe(tdf)
#
# elec_coord_tdf.to_csv('coords_'+ttest_table_df_filename+'.csv')
# print new_tdf
# new_tdf.to_csv('ttest_table_params_with_location_latest.csv')