|
8 | 8 | from scipy.signal import butter, lfilter, filtfilt, hilbert
|
9 | 9 | # from ripple2nwb.neural_processing import NeuralDataProcessor
|
10 | 10 | # from prepype import NeuralDataProcessor
|
| 11 | +from ripple2nwb.neural_processing import get_bipolar_referenced_electrodes |
11 | 12 | from prepype.neural_processing import NeuralDataProcessor, downsample, downsample_NWB
|
12 | 13 | from utils_jgm.machine_compatibility_utils import MachineCompatibilityUtils
|
13 | 14 | from utils_jgm.toolbox import resample
|
@@ -55,6 +56,9 @@ def __init__(self, nwb_dir, patient):
|
55 | 56 | self.bad_electrodes = []
|
56 | 57 | self.good_electrodes = list(np.arange(256))
|
57 | 58 |
|
| 59 | + self._bipolar_to_elec_map = None |
| 60 | + self._good_channels = None |
| 61 | + |
58 | 62 | self.high_gamma_min = 70
|
59 | 63 | self.high_gamma_max = 199
|
60 | 64 |
|
@@ -109,6 +113,10 @@ def __init__(self, nwb_dir, patient):
|
109 | 113 |
|
110 | 114 | self.good_electrodes = [x for x in self.good_electrodes if x not in self.bad_electrodes]
|
111 | 115 |
|
| 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 | + |
112 | 120 | self.config = None
|
113 | 121 |
|
114 | 122 |
|
@@ -197,12 +205,21 @@ def make_data(self,
|
197 | 205 | processor.edwards_high_gamma()
|
198 | 206 | print('High gamma extraction done.')
|
199 | 207 |
|
200 |
| - nwbfile_electrodes = processor.nwb_file.processing['ecephys'].\ |
| 208 | + if not BPR: |
| 209 | + nwbfile_electrodes = processor.nwb_file.processing['ecephys'].\ |
201 | 210 | 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]})'].\ |
204 | 212 | data[()][:, self.good_electrodes]
|
205 | 213 |
|
| 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 | + |
206 | 223 | print(f"Number of good electrodes in {file}: {nwbfile_electrodes.shape[1]}")
|
207 | 224 |
|
208 | 225 | # Begin building the WAVE files for wav2vec training
|
@@ -236,6 +253,7 @@ def make_data(self,
|
236 | 253 | concatenated_speaking_segments = np.concatenate(all_speaking_segments,
|
237 | 254 | axis=0)
|
238 | 255 |
|
| 256 | + |
239 | 257 | # Training data: speaking segments only
|
240 | 258 | if create_training_data and chopped_sentence_dir:
|
241 | 259 | num_full_chunks = len(concatenated_speaking_segments) // chunk_length
|
@@ -401,12 +419,44 @@ def make_data(self,
|
401 | 419 | print('In distribution block. TFRecords created.')
|
402 | 420 |
|
403 | 421 | 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\ |
406 | 424 | in the wav2vec training data: {e}")
|
407 | 425 |
|
408 | 426 | io.close()
|
409 | 427 |
|
| 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 | + |
410 | 460 |
|
411 | 461 | '''
|
412 | 462 | JGM is the author of the following functions. Light modifications made.
|
|
0 commit comments