In [1]:
%matplotlib inline
from matplotlib import pylab as pl
import cPickle as pickle
import pandas as pd
import numpy as np
import os
import random

www.ieeg.org follow videos https://www.youtube.com/watch?v=Q0jSTaWQtMs and https://www.youtube.com/watch?v=GvfnSY5tk2Y for setting up an account

search for Dog EEG

found 7 cases but only 3 are annotated I004_A0001_D001, I004_A0002_D002 and I004_A0003_D001


In [2]:
!ls -l ~/Downloads/I004_A0001_D001.csv


-rw-r-----@ 1 udi  staff  472102285 Sep 20 15:13 /Users/udi/Downloads/I004_A0001_D001.csv

Getting annotations

  1. press "Layers..." button at bottom left
  2. Press Annotations at the bottom right of the pop up
  3. Press CSV

In [3]:
!wc -l ~/Downloads/I004_A0001_D001-annotiations.csv


    1441 /Users/udi/Downloads/I004_A0001_D001-annotiations.csv

In [4]:
!head ~/Downloads/I004_A0001_D001-annotiations.csv


Type,Start,End,Description,Channel,ChannelID,AnnotationID,Creator
Seizure,2124151342773,2124151342773,,RIGHT_06,cd2d4443-d625-4193-96d2-efb2b54c8b1f,1521180,Joost Wagenaar
Seizure,2124151342773,2124151342773,,LEFT_08,72ad187e-f0ed-452e-8605-4644e1d2b01e,1521180,Joost Wagenaar
Seizure,2124151342773,2124151342773,,LEFT_01,9bf95d73-3f6e-4c74-bdea-f02745f7799c,1521180,Joost Wagenaar
Seizure,2124151342773,2124151342773,,LEFT_02,09444db8-d008-41ee-a6ac-98b56c540029,1521180,Joost Wagenaar
Seizure,2124151342773,2124151342773,,LEFT_04,1ad7a258-652d-4ade-9638-21eb35ae2331,1521180,Joost Wagenaar
Seizure,2124151342773,2124151342773,,RIGHT_04,649b83c4-1cc4-4f0b-bab3-e058ae8f3f02,1521180,Joost Wagenaar
Seizure,2124151342773,2124151342773,,LEFT_05,61d1a08a-c5b7-40dc-8b2b-6a9dddc56fd5,1521180,Joost Wagenaar
Seizure,2124151342773,2124151342773,,RIGHT_03,4552733e-7b64-4222-87e0-4a438e1e5aea,1521180,Joost Wagenaar
Seizure,2124151342773,2124151342773,,LEFT_07,c3f4023e-6dcd-4167-9f15-4a80fc34f5ae,1521180,Joost Wagenaar

Seizure start time


In [10]:
!cut -d, -f2 ~/Downloads/I004_A0001_D001-annotiations.csv | sort -n -u | wc -l


      91

In [11]:
!cut -d, -f2 ~/Downloads/I004_A0001_D001-annotiations.csv | sort -n -u


Start
44424340820
2124151342773
2154218676757
2170395458984
2180990795898
2191320874023
3701864575195
3717143481445
3721279516601
3727026123047
3732056030273
3733406347656
3738674487304
3741890625000
3790803344726
3791683203125
3792471972656
3793027514648
3793797265625
3794855297851
3795559985351
3796306713867
3797233618164
3797962329101
3798946289062
3799741064453
3800662963867
3801107397461
3801515795898
3802153417968
3802792041015
3803236474609
3804099316406
3804888085937
3805683862304
3806047216797
3806691845703
3807334472656
3807850976562
3808239355468
3809064160156
3809743823242
3810365429687
3810892944336
3811202246093
3812002026367
3812608618164
3812928930664
3813626611328
3813954931640
3817683569336
3819539379882
3821558349609
3823796533203
3842832104492
3845261474609
3846182373047
3847014184570
3847930078125
3848616748047
3849278393554
3850046142578
3850570654297
3851091162109
3852471508789
3852642675781
3853638647461

3854980957031
3855678637695
3856346289062
3856889819336
3857498413086
3858142041015
3858736621093
3859296166992
3859991845703
3860609448242
3861183007812
3861879687500
3862470263672
3863138916015
3863718481445
3864280029297
3865002734375
3865825537109
3876198657226
3888372534179
3893117163086
3908540209961

Getting data

use the Annot. buttons at the bottom to jump to next seizure

First seizure 12:20:24.341709


In [17]:
(((12*60) + 20)*60+24.341709)*1000000


Out[17]:
44424341709.0

In [ ]:
which match exactly the CSV file

In [18]:
44424340820 - _


Out[18]:
-889.0

downloading 1:10:00 data before seizure

end time is seizure start


In [19]:
44424340820/1000000


Out[19]:
44424

width (in seconds)


In [22]:
70*60


Out[22]:
4200

start time is 1:10:00 before that (in seconds)


In [23]:
44424340820/1000000 - 4200


Out[23]:
40224

enter value at the tool bar in the "Start (s)" and "Width (s)" text box. Make sure you erase any previous values you had in does boxes

use the hamburger icon in the toolbar to download CSV


In [2]:
Fs=399.61

In [26]:
!wc -l ~/Downloads/I004_A0001_D001.csv


      17 /Users/udi/Downloads/I004_A0001_D001.csv

it looks like first ine is header and then each channel is in a single line


In [4]:
!head -n 1 ~/Downloads/I004_A0001_D001.csv


Channel Label,Channel ID,Start,Duration,Data...

In [8]:
!head -n 2 ~/Downloads/I004_A0001_D001.csv | tail -n 1 | dd count=1


LEFT_01,9bf95d73-3f6e-4c74-bdea-f02745f7799c,40224000000,4200000000,-94,-79,-88,-96,-99,-89,-97,-121,-122,-104,-81,-70,-57,-49,-54,-49,-41,-27,-22,-27,-28,-22,-34,-52,-54,-32,-22,-25,-34,-57,-63,-63,-62,-47,-34,-38,-34,-30,-24,-21,-17,-25,-40,-42,-54,-61,-75,-81,-77,-81,-82,-84,-90,-96,-99,-105,-116,-119,-110,-97,-96,-79,-74,-69,-51,-29,-11,14,27,29,30,26,27,15,7,5,-6,-9,-10,-27,-43,-50,-46,-46,-46,-49,-69,-93,-108,-103,-84,-71,-57,-51,-57,-73,-73,-47,-20,-28,-34,-26,-20,-25,-27,-38,-56,-73,-86,-90,-83,-86,1+0 records in
1+0 records out
512 bytes transferred in 0.359566 secs (1424 bytes/sec)
tail: stdout: Broken pipe

Number of channels in each line


In [3]:
ieegdata = []
with open('/Users/udi/Downloads/I004_A0001_D001.csv') as fp:
    fp.next()
    for l in fp:
        l = l.split(',')
        N = len(l)-4
        print l[0],l[1],l[2],l[3],N,N/Fs
        ieegdata.append(map(int,l[4:]))
ieegdata = np.array(ieegdata)


LEFT_01 9bf95d73-3f6e-4c74-bdea-f02745f7799c 40224000000 4200000000 1678362 4200.0
LEFT_02 09444db8-d008-41ee-a6ac-98b56c540029 40224000000 4200000000 1678362 4200.0
LEFT_03 236d01ea-9a36-4bbf-a638-20ca31f024e9 40224000000 4200000000 1678362 4200.0
LEFT_04 1ad7a258-652d-4ade-9638-21eb35ae2331 40224000000 4200000000 1678362 4200.0
LEFT_05 61d1a08a-c5b7-40dc-8b2b-6a9dddc56fd5 40224000000 4200000000 1678362 4200.0
LEFT_06 9735ab3a-427b-480c-8e34-3eba10dce254 40224000000 4200000000 1678362 4200.0
LEFT_07 c3f4023e-6dcd-4167-9f15-4a80fc34f5ae 40224000000 4200000000 1678362 4200.0
LEFT_08 72ad187e-f0ed-452e-8605-4644e1d2b01e 40224000000 4200000000 1678362 4200.0
RIGHT_01 9f7bbd04-7548-4256-aadf-8fb2720d0077 40224000000 4200000000 1678362 4200.0
RIGHT_02 9dcdc63c-bf10-4064-adf3-d4c3198af2fe 40224000000 4200000000 1678362 4200.0
RIGHT_03 4552733e-7b64-4222-87e0-4a438e1e5aea 40224000000 4200000000 1678362 4200.0
RIGHT_04 649b83c4-1cc4-4f0b-bab3-e058ae8f3f02 40224000000 4200000000 1678362 4200.0
RIGHT_05 666156b0-9367-4e6c-a459-44243e38f9bc 40224000000 4200000000 1678362 4200.0
RIGHT_06 cd2d4443-d625-4193-96d2-efb2b54c8b1f 40224000000 4200000000 1678362 4200.0
RIGHT_07 54cf3ee5-953b-44a0-a182-6335ad2571cf 40224000000 4200000000 1678362 4200.0
RIGHT_08 1a5094be-0be4-44c9-9aab-82ee0a5b62d1 40224000000 4200000000 1678362 4200.0

In [4]:
ieegdata.shape


Out[4]:
(16, 1678362)

In [5]:
data_type = 'preictal' # preictal interictal test

In [6]:
import scipy.io
import scipy.signal

for target in ['Dog_1', 'Dog_2', 'Dog_3', 'Dog_4', 'Dog_5']:
    segment = 1 # start segment

    all_data = last_sequence = last_data_length_sec = last_Fs = last_channels = last_d_shape = None
    while True:
        fname = '../seizure-data/%s/%s_%s_segment_%04d.mat'%(target,target,data_type,segment)
        try:
            data = scipy.io.loadmat(fname)
        except:
            break
        k = '%s_segment_%d'%(data_type,segment)
#         for k1 in data.keys():
#             if k1 != k:
#                 print data[k1],
#         print
        data_length_sec = data[k]['data_length_sec'][0,0][0,0]
        sequence = data[k]['sequence'][0,0][0,0]
        Fs = float(data[k]['sampling_frequency'][0,0][0,0])
        channels = [t[0] for t in data[k]['channels'][0,0][0]]
        d = data[k]['data'][0,0]

        assert len(channels) == d.shape[0]
        assert int(Fs*data_length_sec + 0.5) == d.shape[1],int(Fs*data_length_sec + 0.5)
        assert last_data_length_sec is None or last_data_length_sec == data_length_sec
        last_data_length_sec = data_length_sec
        assert last_Fs is None or last_Fs == Fs
        last_Fs = Fs
        assert last_channels is None or all(c1==c2 for c1,c2 in zip(last_channels, channels))
        last_channels = channels
        assert last_d_shape is None or last_d_shape == d.shape
        last_d_shape = d.shape

        if sequence == 3:
            print target, channels[0]
            for ieegchan in ieegdata:
                c = scipy.signal.fftconvolve(ieegchan,d[0,::-1],mode='valid')
                score = (c.max()-c.mean())/c.std()
                if score > 10:
                    print target,score
        last_sequence = sequence
        segment += 1
#     print data_length_sec, sequence, Fs, d.shape


Dog_1 NVC1202_32_002_Ecog_c001
Dog_1 NVC1202_32_002_Ecog_c001
Dog_1 NVC1202_32_002_Ecog_c001
Dog_1 NVC1202_32_002_Ecog_c001
Dog_2 NVC0905_22_002_Ecog_c001
Dog_2 NVC0905_22_002_Ecog_c001
Dog_2 NVC0905_22_002_Ecog_c001
Dog_2 NVC0905_22_002_Ecog_c001
Dog_2 NVC0905_22_002_Ecog_c001
Dog_2 NVC0905_22_002_Ecog_c001
Dog_2 NVC0905_22_002_Ecog_c001
Dog_3 NVC0906_22_007_Ecog_c001
Dog_3 NVC0906_22_007_Ecog_c001
Dog_3 NVC0906_22_007_Ecog_c001
Dog_3 NVC0906_22_007_Ecog_c001
Dog_3 NVC0906_22_007_Ecog_c001
Dog_3 NVC0906_22_007_Ecog_c001
Dog_3 NVC0906_22_007_Ecog_c001
Dog_3 NVC0906_22_007_Ecog_c001
Dog_3 NVC0906_22_007_Ecog_c001
Dog_3 NVC0906_22_007_Ecog_c001
Dog_3 NVC0906_22_007_Ecog_c001
Dog_3 NVC0906_22_007_Ecog_c001
Dog_4 NVC1202_26_003_Ecog_c001
Dog_4 NVC1202_26_003_Ecog_c001
Dog_4 NVC1202_26_003_Ecog_c001
Dog_4 NVC1202_26_003_Ecog_c001
Dog_4 NVC1202_26_003_Ecog_c001
Dog_4 NVC1202_26_003_Ecog_c001
Dog_4 NVC1202_26_003_Ecog_c001
Dog_4 NVC1202_26_003_Ecog_c001
Dog_4 NVC1202_26_003_Ecog_c001
Dog_4 NVC1202_26_003_Ecog_c001
Dog_4 NVC1202_26_003_Ecog_c001
Dog_4 NVC1202_26_003_Ecog_c001
Dog_4 NVC1202_26_003_Ecog_c001
Dog_4 NVC1202_26_003_Ecog_c001
Dog_4 NVC1202_26_003_Ecog_c001
Dog_4 NVC1202_26_003_Ecog_c001
Dog_5 NVC0905_22_004_Ecog_c001
Dog_5 NVC0905_22_004_Ecog_c001
Dog_5 NVC0905_22_004_Ecog_c001
Dog_5 NVC0905_22_004_Ecog_c001
Dog_5 NVC0905_22_004_Ecog_c001

In [ ]: