<jupyter_start><jupyter_text>Introduction to visualizing data in the eeghdf files<jupyter_code># I have copied the stacklineplot from my python-edf/examples code to help with display. Maybe I will put this as a helper or put it out as a utility package to make it easier to install.

from __future__ import print_function, division, unicode_literals
%matplotlib inline
# %matplotlib notebook

import matplotlib
import matplotlib.pyplot as plt
#import seaborn
import pandas as pd
import numpy as np
import h5py
from pprint import pprint

import stacklineplot
# matplotlib.rcParams['figure.figsize'] = (18.0, 12.0)
matplotlib.rcParams['figure.figsize'] = (12.0, 8.0)

hdf = h5py.File('../data/spasms.eeghdf','r')
pprint(list(hdf.items()))
pprint(list(hdf['patient'].attrs.items()))
rec = hdf['record-0']
pprint(list(rec.items()))
pprint(list(rec.attrs.items()))
years_old = rec.attrs['patient_age_days']/365
pprint("age in years: %s" % years_old)
signals = rec['signals']
labels = rec['signal_labels']
electrode_labels = [str(s,'ascii') for s in labels]
numbered_electrode_labels = ["%d:%s" % (ii, str(labels[ii], 'ascii')) for ii in range(len(labels))]<jupyter_output><empty_output><jupyter_text>Simple visualization of EEG (electrodecrement seizure pattern)<jupyter_code># plot 10s epochs (multiples in DE)
ch0, ch1 = (0,19)
DE = 2 # how many 10s epochs to display
epoch = 53; ptepoch = 10*int(rec.attrs['sample_frequency'])
dp = int(0.5*ptepoch)
# stacklineplot.stackplot(signals[ch0:ch1,epoch*ptepoch+dp:(epoch+DE)*ptepoch+dp],seconds=DE*10.0, ylabels=electrode_labels[ch0:ch1], yscale=0.3)
print("epoch:", epoch)


# search identified spasms at 1836, 1871, 1901, 1939
stacklineplot.show_epoch_centered(signals, 1836,
                        epoch_width_sec=15,
                        chstart=0, chstop=19, fs=rec.attrs['sample_frequency'],
                        ylabels=electrode_labels, yscale=3.0)


annot = rec['edf_annotations']
#print(list(annot.items()))
#annot['texts'][:]
signals.shape
antext = [s.decode('utf-8') for s in annot['texts'][:]]
starts100ns = [xx for xx in annot['starts_100ns'][:]]
len(starts100ns), len(antext)
import pandas as pd
df = pd.DataFrame(data=antext, columns=['text'])
df['starts100ns'] = starts100ns
df['starts_sec'] = df['starts100ns']/10**7
df # look at the annotations
df[df.text.str.contains('sz',case=False)]
df[df.text.str.contains('seizure',case=False)] # find the seizure
df[df.text.str.contains('spasm',case=False)] # find the seizure
list(annot.items())<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.14759273875295975, 'fraction_numerical': 0.03591160220994475, 'mean_word_length': 5.778074866310161, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '2', 'dir': 'jupyter-structured-clean-dedup', 'id': '507840', 'license': 'BSD-3-Clause', 'n_tokens_mistral': 977, 'n_tokens_neox': 904, 'n_words': 212, 'path': 'notebooks/vizSpasm.ipynb', 'repo_name': 'aragorn/eeg-hdfstorage', 'size': '2534'}