Skip to content

Commit b8d8748

Browse files
feat: bipolar, almost done
1 parent 006c460 commit b8d8748

File tree

1 file changed

+55
-5
lines changed

1 file changed

+55
-5
lines changed

packages/ecog2vec/ecog2vec/data_generator.py

+55-5
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
from scipy.signal import butter, lfilter, filtfilt, hilbert
99
# from ripple2nwb.neural_processing import NeuralDataProcessor
1010
# from prepype import NeuralDataProcessor
11+
from ripple2nwb.neural_processing import get_bipolar_referenced_electrodes
1112
from prepype.neural_processing import NeuralDataProcessor, downsample, downsample_NWB
1213
from utils_jgm.machine_compatibility_utils import MachineCompatibilityUtils
1314
from utils_jgm.toolbox import resample
@@ -55,6 +56,9 @@ def __init__(self, nwb_dir, patient):
5556
self.bad_electrodes = []
5657
self.good_electrodes = list(np.arange(256))
5758

59+
self._bipolar_to_elec_map = None
60+
self._good_channels = None
61+
5862
self.high_gamma_min = 70
5963
self.high_gamma_max = 199
6064

@@ -109,6 +113,10 @@ def __init__(self, nwb_dir, patient):
109113

110114
self.good_electrodes = [x for x in self.good_electrodes if x not in self.bad_electrodes]
111115

116+
self.grid_step = 1
117+
self.elec_layout = np.arange(np.prod(
118+
self.grid_size)-1, -1, -1).reshape(self.grid_size).T[::self.grid_step, ::self.grid_step]
119+
112120
self.config = None
113121

114122

@@ -197,12 +205,21 @@ def make_data(self,
197205
processor.edwards_high_gamma()
198206
print('High gamma extraction done.')
199207

200-
nwbfile_electrodes = processor.nwb_file.processing['ecephys'].\
208+
if not BPR:
209+
nwbfile_electrodes = processor.nwb_file.processing['ecephys'].\
201210
data_interfaces['LFP'].\
202-
electrical_series[f'high gamma \
203-
({list(self.config["referencing"])[0]})'].\
211+
electrical_series[f'high gamma ({list(self.config["referencing"])[0]})'].\
204212
data[()][:, self.good_electrodes]
205213

214+
elif BPR:
215+
nwbfile_electrodes = processor.nwb_file.processing['ecephys'].\
216+
data_interfaces['LFP'].\
217+
electrical_series[f'high gamma ({list(self.config["referencing"])[0]})'].\
218+
data[()][:, self.good_channels()]
219+
220+
else:
221+
raise ValueError("Only CAR or BPR are supported.")
222+
206223
print(f"Number of good electrodes in {file}: {nwbfile_electrodes.shape[1]}")
207224

208225
# Begin building the WAVE files for wav2vec training
@@ -236,6 +253,7 @@ def make_data(self,
236253
concatenated_speaking_segments = np.concatenate(all_speaking_segments,
237254
axis=0)
238255

256+
239257
# Training data: speaking segments only
240258
if create_training_data and chopped_sentence_dir:
241259
num_full_chunks = len(concatenated_speaking_segments) // chunk_length
@@ -401,12 +419,44 @@ def make_data(self,
401419
print('In distribution block. TFRecords created.')
402420

403421
except Exception as e:
404-
print(f"An error occured \
405-
and block {path} is not inluded \
422+
print(f"An error occured\
423+
and block {path} is not inluded\
406424
in the wav2vec training data: {e}")
407425

408426
io.close()
409427

428+
def bipolar_to_elec_map(self):
429+
# print('WARNING!!!! MAKING UP bipolar_to_elec_map!!!')
430+
elec_map = []
431+
layout = self.elec_layout # for short
432+
for i in range(layout.shape[0]):
433+
for j in range(layout.shape[1]):
434+
if j < layout.shape[1]-1:
435+
elec_map.append((layout[i, j], layout[i, j+1]))
436+
if i < layout.shape[0]-1:
437+
elec_map.append((layout[i, j], layout[i+1, j]))
438+
return np.array(elec_map)
439+
440+
def good_channels(self):
441+
'''
442+
Pseudo-channels, constructed (on the fly) from the physical electrodes.
443+
For now at least, we won't USE_FIELD_POTENTIALS if we want to
444+
REFERENCE_BIPOLAR.
445+
NB!!: The *order* of these channels matters--it determines the order of
446+
the input data, and therefore is required by the functions that plot
447+
electrode_contributions in plotters.py! And the order of these channels
448+
will be determined by the *elec_layout*.
449+
'''
450+
451+
# NB: this means that the electrodes are *not* in numerical order ('e1'
452+
# does not correspond to the 0th entry in all_electrodes): as you can
453+
# check, flattening the elec_layout does not yield an ordered list.
454+
all_electrodes = self.elec_layout.flatten().tolist()
455+
return [
456+
ch for ch, elec_pair in enumerate(self.bipolar_to_elec_map())
457+
if all([e in self.good_electrodes for e in elec_pair])
458+
]
459+
410460

411461
'''
412462
JGM is the author of the following functions. Light modifications made.

0 commit comments

Comments
 (0)