Giter VIP home page Giter VIP logo

Comments (6)

raphaelvallat avatar raphaelvallat commented on July 17, 2024 1

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.

apavlo89 avatar apavlo89 commented on July 17, 2024 1

Yay, it now outputs values with hypnogram! Thank you very much Raphael for all your help, you rock!

from yasa.

raphaelvallat avatar raphaelvallat commented on July 17, 2024

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.

apavlo89 avatar apavlo89 commented on July 17, 2024

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.

raphaelvallat avatar raphaelvallat commented on July 17, 2024

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.

apavlo89 avatar apavlo89 commented on July 17, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.