Hilbert Detector#

[1]:
# first let's load in all our packages
import matplotlib
import matplotlib.pyplot as plt
import numpy as np
import sys
import os
import re
import pandas as pd


from mne_bids import (read_raw_bids, BIDSPath,
                      get_entity_vals, get_datatypes,
                      make_report)
from mne_bids.stats import count_events

import mne
from mne import make_ad_hoc_cov

basepath = os.path.join(os.getcwd(), "../..")
sys.path.append(basepath)
from mne_hfo import HilbertDetector
[2]:
%%capture
# this may change depending on where you store the data
root = "C:/Users/patri/Dropbox/fedele_hfo_data"
subjects = get_entity_vals(root, 'subject')
sessions = get_entity_vals(root, 'session')
subjectID = subjects[0]
sessionID = sessions[0]
bids_path = BIDSPath(subject=subjectID, session=sessionID,
                     datatype='ieeg',
                     suffix='ieeg',
                     extension='.vhdr', root=root)

# get first matching dataset
fpath = bids_path.match()[0]
# load dataset into mne Raw object
extra_params = dict(preload=True)
raw = read_raw_bids(fpath, extra_params)
[3]:
def convert_to_bipolar(raw, drop_originals=True):
    original_ch_names = raw.ch_names
    ch_names_sorted = sorted(original_ch_names)
    ch_pairs = []
    for first, second in zip(ch_names_sorted, ch_names_sorted[1:]):
        firstName = re.sub(r'[0-9]+', '', first)
        secondName = re.sub(r'[0-9]+', '', second)
        if firstName == secondName:
            ch_pairs.append((first,second))
    for ch_pair in ch_pairs:
        raw = mne.set_bipolar_reference(raw, ch_pair[0], ch_pair[1], drop_refs=False)
    if drop_originals:
        raw = raw.drop_channels(original_ch_names)
    return raw
[4]:
%%capture
raw = convert_to_bipolar(raw)

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-4-f6e3da9a949b> in <module>
      2 info = raw.info
      3 sfreq = info['sfreq']
----> 4 data = raw.get_data(start=0, stop=30*sfreq)
      5 raw = mne.io.RawArray(data, info=info)

<decorator-gen-170> in get_data(self, picks, start, stop, reject_by_annotation, return_times, verbose)

~\.virtualenvs\mne-hfo-tjBZjdVA\lib\site-packages\mne\io\base.py in get_data(self, picks, start, stop, reject_by_annotation, return_times, verbose)
    823         stop = min(self.n_times if stop is None else stop, self.n_times)
    824         if len(self.annotations) == 0 or reject_by_annotation is None:
--> 825             data, times = self[picks, start:stop]
    826             return (data, times) if return_times else data
    827         _check_option('reject_by_annotation', reject_by_annotation.lower(),

~\.virtualenvs\mne-hfo-tjBZjdVA\lib\site-packages\mne\io\base.py in __getitem__(self, item)
    770         sel, start, stop = self._parse_get_set_params(item)
    771         if self.preload:
--> 772             data = self._data[sel, start:stop]
    773         else:
    774             data = self._read_segment(start=start, stop=stop, sel=sel,

TypeError: slice indices must be integers or None or have an __index__ method
[7]:
info = raw.info
sfreq = info['sfreq']
data = raw.get_data(start=0, stop=int(30*sfreq))
raw = mne.io.RawArray(data, info=info)
Creating RawArray with float64 data, n_channels=43, n_times=60000
    Range : 0 ... 59999 =      0.000 ...    30.000 secs
Ready.
[8]:
kwargs = {
    'band_method': 'log',
    'filter_band': (80, 250), # (l_freq, h_freq)
    'threshold': 3, # Number of st. deviations
    'hfo_name': "ripple",
    'n_jobs': 1,
}
hil_detector = HilbertDetector(**kwargs)
[9]:
raw.n_times
[9]:
60000
[10]:
# perform the fits
hil_detector = hil_detector.fit(raw)
  0%|          | 0/43 [00:00<?, ?it/s]
freq_span: 61
  2%|▏         | 1/43 [00:00<00:21,  2.00it/s]
freq_span: 61
  5%|▍         | 2/43 [00:00<00:20,  2.00it/s]
freq_span: 61
  7%|▋         | 3/43 [00:01<00:19,  2.02it/s]
freq_span: 61
  9%|▉         | 4/43 [00:01<00:19,  2.03it/s]
freq_span: 61
 12%|█▏        | 5/43 [00:02<00:18,  2.07it/s]
freq_span: 61
 14%|█▍        | 6/43 [00:02<00:17,  2.09it/s]
freq_span: 61
 16%|█▋        | 7/43 [00:03<00:16,  2.12it/s]
freq_span: 61
 19%|█▊        | 8/43 [00:03<00:16,  2.13it/s]
freq_span: 61
 21%|██        | 9/43 [00:04<00:15,  2.16it/s]
freq_span: 61
 23%|██▎       | 10/43 [00:04<00:15,  2.19it/s]
freq_span: 61
 26%|██▌       | 11/43 [00:05<00:14,  2.20it/s]
freq_span: 61
 28%|██▊       | 12/43 [00:05<00:14,  2.18it/s]
freq_span: 61
 30%|███       | 13/43 [00:06<00:13,  2.15it/s]
freq_span: 61
 33%|███▎      | 14/43 [00:06<00:13,  2.15it/s]
freq_span: 61
 35%|███▍      | 15/43 [00:07<00:13,  2.15it/s]
freq_span: 61
 37%|███▋      | 16/43 [00:07<00:12,  2.17it/s]
freq_span: 61
 40%|███▉      | 17/43 [00:07<00:11,  2.18it/s]
freq_span: 61
 42%|████▏     | 18/43 [00:08<00:11,  2.21it/s]
freq_span: 61
 44%|████▍     | 19/43 [00:08<00:10,  2.24it/s]
freq_span: 61
 47%|████▋     | 20/43 [00:09<00:10,  2.24it/s]
freq_span: 61
 49%|████▉     | 21/43 [00:09<00:09,  2.23it/s]
freq_span: 61
 51%|█████     | 22/43 [00:10<00:09,  2.22it/s]
freq_span: 61
 53%|█████▎    | 23/43 [00:10<00:08,  2.25it/s]
freq_span: 61
 56%|█████▌    | 24/43 [00:11<00:08,  2.25it/s]
freq_span: 61
 58%|█████▊    | 25/43 [00:11<00:07,  2.25it/s]
freq_span: 61
 60%|██████    | 26/43 [00:11<00:07,  2.24it/s]
freq_span: 61
 63%|██████▎   | 27/43 [00:12<00:07,  2.25it/s]
freq_span: 61
 65%|██████▌   | 28/43 [00:12<00:06,  2.24it/s]
freq_span: 61
 67%|██████▋   | 29/43 [00:13<00:06,  2.22it/s]
freq_span: 61
 70%|██████▉   | 30/43 [00:13<00:05,  2.24it/s]
freq_span: 61
 72%|███████▏  | 31/43 [00:14<00:05,  2.26it/s]
freq_span: 61
 74%|███████▍  | 32/43 [00:14<00:04,  2.26it/s]
freq_span: 61
 77%|███████▋  | 33/43 [00:15<00:04,  2.25it/s]
freq_span: 61
 79%|███████▉  | 34/43 [00:15<00:03,  2.25it/s]
freq_span: 61
 81%|████████▏ | 35/43 [00:15<00:03,  2.25it/s]
freq_span: 61
 84%|████████▎ | 36/43 [00:16<00:03,  2.27it/s]
freq_span: 61
 86%|████████▌ | 37/43 [00:16<00:02,  2.25it/s]
freq_span: 61
 88%|████████▊ | 38/43 [00:17<00:02,  2.28it/s]
freq_span: 61
 91%|█████████ | 39/43 [00:17<00:01,  2.28it/s]
freq_span: 61
 93%|█████████▎| 40/43 [00:18<00:01,  2.29it/s]
freq_span: 61
 95%|█████████▌| 41/43 [00:18<00:00,  2.26it/s]
freq_span: 61
 98%|█████████▊| 42/43 [00:19<00:00,  2.26it/s]
freq_span: 61
100%|██████████| 43/43 [00:19<00:00,  2.21it/s]
  0%|          | 0/43 [00:00<?, ?it/s]
100%|██████████| 61/61 [00:00<00:00, 815.07HFO-first-phase/s]

100%|██████████| 61/61 [00:00<00:00, 820.47HFO-first-phase/s]
  5%|▍         | 2/43 [00:00<00:03, 12.98it/s]
100%|██████████| 61/61 [00:00<00:00, 848.13HFO-first-phase/s]

100%|██████████| 61/61 [00:00<00:00, 864.18HFO-first-phase/s]
  9%|▉         | 4/43 [00:00<00:02, 13.22it/s]
100%|██████████| 61/61 [00:00<00:00, 830.25HFO-first-phase/s]

100%|██████████| 61/61 [00:00<00:00, 871.24HFO-first-phase/s]
 14%|█▍        | 6/43 [00:00<00:02, 13.35it/s]
100%|██████████| 61/61 [00:00<00:00, 847.72HFO-first-phase/s]

100%|██████████| 61/61 [00:00<00:00, 847.76HFO-first-phase/s]
 19%|█▊        | 8/43 [00:00<00:02, 13.42it/s]
100%|██████████| 61/61 [00:00<00:00, 847.64HFO-first-phase/s]

100%|██████████| 61/61 [00:00<00:00, 871.45HFO-first-phase/s]
 23%|██▎       | 10/43 [00:00<00:02, 13.46it/s]
100%|██████████| 61/61 [00:00<00:00, 871.42HFO-first-phase/s]

100%|██████████| 61/61 [00:00<00:00, 859.87HFO-first-phase/s]
 28%|██▊       | 12/43 [00:00<00:02, 13.56it/s]
100%|██████████| 61/61 [00:00<00:00, 865.06HFO-first-phase/s]

100%|██████████| 61/61 [00:00<00:00, 859.05HFO-first-phase/s]
 33%|███▎      | 14/43 [00:01<00:02, 13.56it/s]
100%|██████████| 61/61 [00:00<00:00, 803.96HFO-first-phase/s]

100%|██████████| 61/61 [00:00<00:00, 825.24HFO-first-phase/s]
 37%|███▋      | 16/43 [00:01<00:02, 13.34it/s]
100%|██████████| 61/61 [00:00<00:00, 848.21HFO-first-phase/s]

100%|██████████| 61/61 [00:00<00:00, 897.48HFO-first-phase/s]
 42%|████▏     | 18/43 [00:01<00:01, 13.47it/s]
100%|██████████| 61/61 [00:00<00:00, 884.18HFO-first-phase/s]

100%|██████████| 61/61 [00:00<00:00, 896.74HFO-first-phase/s]
 47%|████▋     | 20/43 [00:01<00:01, 13.66it/s]
100%|██████████| 61/61 [00:00<00:00, 884.58HFO-first-phase/s]

100%|██████████| 61/61 [00:00<00:00, 896.35HFO-first-phase/s]
 51%|█████     | 22/43 [00:01<00:01, 13.82it/s]
100%|██████████| 61/61 [00:00<00:00, 909.52HFO-first-phase/s]

100%|██████████| 61/61 [00:00<00:00, 896.39HFO-first-phase/s]
 56%|█████▌    | 24/43 [00:01<00:01, 13.99it/s]
100%|██████████| 61/61 [00:00<00:00, 896.36HFO-first-phase/s]

100%|██████████| 61/61 [00:00<00:00, 909.36HFO-first-phase/s]
 60%|██████    | 26/43 [00:01<00:01, 14.07it/s]
100%|██████████| 61/61 [00:00<00:00, 896.99HFO-first-phase/s]

100%|██████████| 61/61 [00:00<00:00, 916.46HFO-first-phase/s]
 65%|██████▌   | 28/43 [00:02<00:01, 14.16it/s]
100%|██████████| 61/61 [00:00<00:00, 883.76HFO-first-phase/s]

100%|██████████| 61/61 [00:00<00:00, 883.72HFO-first-phase/s]
 70%|██████▉   | 30/43 [00:02<00:00, 14.11it/s]
100%|██████████| 61/61 [00:00<00:00, 872.18HFO-first-phase/s]

100%|██████████| 61/61 [00:00<00:00, 871.87HFO-first-phase/s]
 74%|███████▍  | 32/43 [00:02<00:00, 14.02it/s]
100%|██████████| 61/61 [00:00<00:00, 871.21HFO-first-phase/s]

100%|██████████| 61/61 [00:00<00:00, 884.17HFO-first-phase/s]
 79%|███████▉  | 34/43 [00:02<00:00, 14.00it/s]
100%|██████████| 61/61 [00:00<00:00, 870.93HFO-first-phase/s]

100%|██████████| 61/61 [00:00<00:00, 895.79HFO-first-phase/s]
 84%|████████▎ | 36/43 [00:02<00:00, 13.99it/s]
100%|██████████| 61/61 [00:00<00:00, 831.16HFO-first-phase/s]

100%|██████████| 61/61 [00:00<00:00, 848.07HFO-first-phase/s]
 88%|████████▊ | 38/43 [00:02<00:00, 13.80it/s]
100%|██████████| 61/61 [00:00<00:00, 839.46HFO-first-phase/s]

100%|██████████| 61/61 [00:00<00:00, 854.21HFO-first-phase/s]
 93%|█████████▎| 40/43 [00:02<00:00, 13.67it/s]
100%|██████████| 61/61 [00:00<00:00, 874.26HFO-first-phase/s]

100%|██████████| 61/61 [00:00<00:00, 860.82HFO-first-phase/s]
 98%|█████████▊| 42/43 [00:03<00:00, 13.72it/s]
100%|██████████| 61/61 [00:00<00:00, 919.07HFO-first-phase/s]
100%|██████████| 43/43 [00:03<00:00, 13.74it/s]
[12]:
hil_detector.df_
[12]:
onset duration label channels sample
0 10.4095 0.6320 ripple AHR1-AHR2 20819.0
1 14.2345 0.2995 ripple AHR1-AHR2 28469.0
2 16.0610 0.3110 ripple AHR1-AHR2 32122.0
3 23.8995 0.4905 ripple AHR1-AHR2 47799.0
4 10.4225 0.5130 ripple AHR2-AHR3 20845.0
... ... ... ... ... ...
69 16.0450 0.5025 ripple PHR4-PHR5 32090.0
70 23.9560 0.5325 ripple PHR4-PHR5 47912.0
71 6.4010 0.1840 ripple PHR5-PHR6 12802.0
72 1.4250 0.1630 ripple PHR7-PHR8 2850.0
73 28.2535 0.3480 ripple PHR7-PHR8 56507.0

74 rows × 5 columns

[ ]: