Comments (6)
My mistake, I meant that you ahve to use hypno = hypno[::30]
. For more details on arrays indexing in Numpy, please see: https://numpy.org/doc/stable/reference/arrays.indexing.html
from yasa.
Yay, it now outputs values with hypnogram! Thank you very much Raphael for all your help, you rock!
from yasa.
Hi @apavlo89,
PS: when pasting code into GitHub, make sure to use the code block marker which makes it far more readable, e.g.
import yasa
The entropy notebooks example was designed to output epoch-level values. This is useful if you want to do sleep stage classification for example. If you have a hypnogram in 30-sec epochs, you can simply add the hypnogram as a column of df_feat
and then use a groupby operation to calculate the aggregated entropy features per sleep stage, e.g.
df_feat['Stage'] = hypno
df_feat.groupby('Stage').mean()
Hope this helps,
Thanks
Raphael
from yasa.
Thank you very much for the help, it now works for hypnograms that have 1 value per 30s epoch. Unfortunately, for the hypnograms I scored with Visbrain (which are a lot :/), Visbrain does this weird thing where it produces 30 values per 30s epoch. I've never understood why that is the case, to be honest as every software I know scores 1 value per 30 s epoch but I digress. In addition, choosing the newest method of creating hypnograms in Visbrain does not create a hypnogram that is recognized by YASA e.g.,
Wake 511.9921875
N1 1655.98828125
N2 2227.9921875
N3 4304.98828125
Wake 4364.98828125
N3 4394.98828125
N2 5177.98828125
REM 5478.99609375
N2 5869.9921875
N3 6201.99609375
Wake 6261.99609375
N3 6983.98828125
Wake 7043.98828125
N3 7585.9921875
N2 7796.98828125
Wake 7826.98828125
N2 7856.98828125
N1 7946.98828125
N2 8368.9921875
N3 8880.99609375
REM 8910.99609375
N2 9211.9921875
Wake 9271.9921875
REM 9331.9921875
N2 9336.99609375
REM 10054.9921875
N2 10324.9921875
N1 10385.98828125
Wake 10415.98828125
N1 10535.98828125
Wake 10565.98828125
REM 10800.99609375
So neither of the two ways of saving hypnograms in Visbrain work with the entropy analysis, but the '30 values per 30 s hypnogram' works with the other YASA analyses types when I use hypno_up = yasa.hypno_upsample_to_data(hypno=hypno_int, sf_hypno=(1), data=data, sf_data=sf)
For the '30 values per 30s hypnogram' this is the code I use in YASA but doesn't work as it gives ValueError: Length of values does not match length of index
#P6
# Load data as a eeglab file
raw = mne.io.read_raw_eeglab('D:/test/6_EEG_LR_THREEHRS.set', eog=(), preload=True, uint16_codec=None, verbose=None)
# Load data
data = raw._data * 1e6
sf = raw.info['sfreq']
chan = raw.ch_names
#ch_names = chan
times = np.arange(data.size) / sf
#times = np.arange(data.shape[1]) / sf
print(data.shape, chan, times)
data = data[0, :]
#print(data.shape, np.round(data[0:5], 3))
hypno = np.loadtxt('D:/test/6_HYPNO_LR_THREEHRS_LETTER.txt', dtype=str)
hypno
hypno_int = yasa.hypno_str_to_int(hypno)
hypno_int
hypno_up = yasa.hypno_upsample_to_data(hypno=hypno_int, sf_hypno=(1), data=data, sf_data=sf)
#print(np.unique(hypno_up))
# print(hypno_up.size == data.shape[1]) # Does the hypnogram have the same number of samples as data?
# print(hypno_up.size, 'samples:', hypno_up)
# Convert the EEG data to 30-sec data
times, data_win = yasa.sliding_window(data, sf, window=30)
# Convert times to minutes
times /= 60
data_win.shape
from numpy import apply_along_axis as apply
df_feat = {
# Entropy
'perm_entropy': apply(ent.perm_entropy, axis=1, arr=data_win, normalize=True),
'svd_entropy': apply(ent.svd_entropy, 1, data_win, normalize=True),
'spec_entropy': apply(ent.spectral_entropy, 1, data_win, sf=sf, nperseg=data_win.shape[1], normalize=True),
'sample_entropy': apply(ent.sample_entropy, 1, data_win),
# Fractal dimension
'dfa': apply(ent.detrended_fluctuation, 1, data_win),
'petrosian': apply(ent.petrosian_fd, 1, data_win),
'katz': apply(ent.katz_fd, 1, data_win),
'higuchi': apply(ent.higuchi_fd, 1, data_win),
}
df_feat = pd.DataFrame(df_feat)
df_feat.head()
# def lziv(x):
# """Binarize the EEG signal and calculate the Lempel-Ziv complexity.
# """
# return ent.lziv_complexity(x > x.mean(), normalize=True)
# df_feat['lziv'] = apply(lziv, 1, data_win) # Slow
df_feat['Stage'] = hypno
df_feat.groupby('Stage').mean()
df_feat.to_csv('D:/test/P6_nonlinear.csv')
Any idea how I can work around this? Is there a way to convert 30 values per 30s hypnograms into 1 value per 30s or anything else that can be done? Thank you
from yasa.
Hi @apavlo89,
As explained on the Gitter chat of Visbrain, the old format of Visbrain export one value per second. You can downsample this to get one value or 30 seconds using: hypno = hypno[hypno::30]
.
Lmk if you solved the issue!
Raphael
from yasa.
Hello Raphael,
When I use hypno=hypno[hypno::30]
it gives me:
TypeError: only integer scalar arrays can be converted to a scalar index
At first, I thought it was giving me this error because my hypnogram has strings/letter for sleep stages but even when changing the sleep stages to numbers in the txt hypnogram I still get the same error. Here is the code I used:
import yasa
import mne
import numpy as np
import pandas as pd
import entropy as ent
import matplotlib.pyplot as plt
import seaborn as sns
sns.set(font_scale=1.2)
#P6
#AF7
# Load data as a eeglab file
raw = mne.io.read_raw_eeglab('D:/test/6_EEG_LR_THREEHRS.set', eog=(), preload=True, uint16_codec=None, verbose=None)
# Load data
data = raw._data * 1e6
sf = raw.info['sfreq']
chan = raw.ch_names
#ch_names = chan
times = np.arange(data.size) / sf
#times = np.arange(data.shape[1]) / sf
print(data.shape, chan, times)
data = data[0, :]
#print(data.shape, np.round(data[0:5], 3))
hypno = np.loadtxt('D:/test/6_HYPNO_LR_THREEHRS_NUMBER.txt', dtype=int)
hypno
hypno = hypno[hypno::30]
from yasa.
Related Issues (20)
- sw_detection, How to improve its accuracy? HOT 1
- Support for MEG spindle detection HOT 1
- Epoch Fragmentation when predicting for short epochs (yasa in mice) HOT 3
- Error while using topoplot() function HOT 4
- I want to know unit of pandpower HOT 1
- extract fractal noise HOT 3
- AssertionError: hypno and include must have same dtype HOT 1
- Spindles and Slow waves summary error HOT 6
- Slow wave detection reproducibility question HOT 2
- Question about Power Spectral Analysis HOT 3
- Invalid REM latency HOT 1
- Add Latency to persistent sleep in sleep_statistics HOT 1
- plot_average plots all channels, not only indicated ones HOT 3
- InconsistentVersionWarning because LabelEncoder from version 0.24.2 of scikit-learn HOT 2
- Possibility of detect the sleep stages on less than 5min of data HOT 4
- Hypno plot errors HOT 1
- Switch to modern python packaging
- Important conversion problem - urgent help needed :) HOT 2
- Finalize evaluation module
- Please help HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from yasa.