Skip to content

Commit

Permalink
Fmivalt/slow wave (#4)
Browse files Browse the repository at this point in the history
* test

* test

* test

* test

* test

* test

* test

* test
  • Loading branch information
xmival00 committed Jul 5, 2024
1 parent ecae3ab commit 2c8adb7
Show file tree
Hide file tree
Showing 18 changed files with 2,318 additions and 96 deletions.
10 changes: 0 additions & 10 deletions .github/workflows/test_publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,30 +9,20 @@ jobs:
strategy:
matrix:
include:
- os: ubuntu-20.04
python-version: '3.6.12'
- os: ubuntu-20.04
python-version: '3.7'
- os: ubuntu-20.04
python-version: '3.8'
- os: ubuntu-20.04
python-version: '3.9'
- os: ubuntu-20.04
python-version: '3.10'

- os: macos-latest
python-version: '3.6.12'
- os: macos-latest
python-version: '3.7'
- os: macos-latest
python-version: '3.8'
- os: macos-latest
python-version: '3.9'
- os: macos-latest
python-version: '3.10'

- os: windows-latest
python-version: '3.7'
- os: windows-latest
python-version: '3.8'
- os: windows-latest
Expand Down
5 changes: 3 additions & 2 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Copyright 2024 Bioelectronics Neurophysiology and Engineering Laboratory, Mayo Clinic
Copyright 2024 Bioelectronics Neurophysiology and Engineering Laboratory - Mayo Clinic

Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:

Expand All @@ -8,4 +8,5 @@ Redistribution and use in source and binary forms, with or without modification,

3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS “AS IS” AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS “AS IS” AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

71 changes: 41 additions & 30 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,9 @@
BEhavioral STate Analysis Toolbox (BEST)
""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""

A Python package for behavioral state analysis using EEG.

BEST includes tools automated sleep classification of long-term iEEG data recorded using
implantable neural stimulation and recording devices, removal of DBS artifacts and feature extraction.
A Python package for EEG and behavioral state analysis EEG using multiple tools.
Including tools for automated sleep classification, analysis of long-term and short-term electrical brain signals
recorded both invasively and non-invasively for an acute period of time but also for long-term data spanning years.

The tools were developed in the `Bioelectronics Neurophysiology and Engineering Laboratory <https://www.mayo.edu/research/labs/bioelectronics-neurophysiology-engineering/overview>`_ at Mayo Clinic, Rochester, MN, USA.

Expand All @@ -27,59 +26,71 @@ Installation
pip install best-toolbox
Cite
Acknowledgement
"""""""""""""""""""""""""""
This toolbox was developed during multiple projects we appreciate you acknowledge when using or inspired by this toolbox.
The toolbox as a whole was developed during the following projects:
BEST was developed and originally published for the first time with by (Mivalt 2022, and Sladky 2022).
We apreciate you citing these papers when utilizing this toolbox in further research work.

| F. Mivalt et V. Kremen et al., “Electrical brain stimulation and continuous behavioral state tracking in ambulatory humans,” J. Neural Eng., vol. 19, no. 1, p. 016019, Feb. 2022, doi: 10.1088/1741-2552/ac4bfd.
|
| V. Sladky et al., “Distributed brain co-processor for tracking spikes, seizures and behaviour during electrical brain stimulation,” Brain Commun., vol. 4, no. 3, May 2022, doi: 10.1093/braincomms/fcac115.
|
For individual modules, please see below.
| F. Mivalt et V. Kremen et al., “Electrical brain stimulation and continuous behavioral state tracking in ambulatory humans,” J. Neural Eng., vol. 19, no. 1, p. 016019, Feb. 2022, doi: 10.1088/1741-2552/ac4bfd.
|
| V. Sladky et al., “Distributed brain co-processor for tracking spikes, seizures and behaviour during electrical brain stimulation,” Brain Commun., vol. 4, no. 3, May 2022, doi: 10.1093/braincomms/fcac115.
Please, see the sections below for references to individual submodules.

Sleep classification and feature extraction
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Automated feature extraction and sleep classification was developed during the following projects.

| F. Mivalt et V. Kremen et al., “Electrical brain stimulation and continuous behavioral state tracking in ambulatory humans,” J. Neural Eng., vol. 19, no. 1, p. 016019, Feb. 2022, doi: 10.1088/1741-2552/ac4bfd.
|
| F. Mivalt et V. Sladky et al., “Automated sleep classification with chronic neural implants in freely behaving canines,” J. Neural Eng., vol. 20, no. 4, p. 046025, Aug. 2023, doi: 10.1088/1741-2552/aced21.
| F. Mivalt et V. Kremen et al., “Electrical brain stimulation and continuous behavioral state tracking in ambulatory humans,” J. Neural Eng., vol. 19, no. 1, p. 016019, Feb. 2022, doi: 10.1088/1741-2552/ac4bfd.
|
| F. Mivalt et V. Sladky et al., “Automated sleep classification with chronic neural implants in freely behaving canines,” J. Neural Eng., vol. 20, no. 4, p. 046025, Aug. 2023, doi: 10.1088/1741-2552/aced21.
|
| Gerla, V., Kremen, V., Macas, M., Dudysova, D., Mladek, A., Sos, P., & Lhotska, L. (2019). Iterative expert-in-the-loop classification of sleep PSG recordings using a hierarchical clustering. Journal of Neuroscience Methods, 317(February), 61?70. https://doi.org/10.1016/j.jneumeth.2019.01.013
|
| Kremen, V., Brinkmann, B. H., Van Gompel, J. J., Stead, S. (Matt) M., St Louis, E. K., & Worrell, G. A. (2018). Automated Unsupervised Behavioral State Classification using Intracranial Electrophysiology. Journal of Neural Engineering. https://doi.org/10.1088/1741-2552/aae5ab
|
| Kremen, V., Duque, J. J., Brinkmann, B. H., Berry, B. M., Kucewicz, M. T., Khadjevand, F., G.A. Worrell, G. A. (2017). Behavioral state classification in epileptic brain using intracranial electrophysiology. Journal of Neural Engineering, 14(2), 026001. https://doi.org/10.1088/1741-2552/aa5688
Our work was based on the following references:

| Gerla, V., Kremen, V., Macas, M., Dudysova, D., Mladek, A., Sos, P., & Lhotska, L. (2019). Iterative expert-in-the-loop classification of sleep PSG recordings using a hierarchical clustering. Journal of Neuroscience Methods, 317(February), 61?70. https://doi.org/10.1016/j.jneumeth.2019.01.013
|
| Kremen, V., Brinkmann, B. H., Van Gompel, J. J., Stead, S. (Matt) M., St Louis, E. K., & Worrell, G. A. (2018). Automated Unsupervised Behavioral State Classification using Intracranial Electrophysiology. Journal of Neural Engineering. https://doi.org/10.1088/1741-2552/aae5ab
|
| Kremen, V., Duque, J. J., Brinkmann, B. H., Berry, B. M., Kucewicz, M. T., Khadjevand, F., G.A. Worrell, G. A. (2017). Behavioral state classification in epileptic brain using intracranial electrophysiology. Journal of Neural Engineering, 14(2), 026001. https://doi.org/10.1088/1741-2552/aa5688

Seizure detection
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
| V. Sladky et al., “Distributed brain co-processor for tracking spikes, seizures and behaviour during electrical brain stimulation,” Brain Commun., vol. 4, no. 3, May 2022, doi: 10.1093/braincomms/fcac115.
| V. Sladky et al., “Distributed brain co-processor for tracking spikes, seizures and behaviour during electrical brain stimulation,” Brain Commun., vol. 4, no. 3, May 2022, doi: 10.1093/braincomms/fcac115.
Artificial Signal Generation
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

| F. Mivalt et al., “Deep Generative Networks for Algorithm Development in Implantable Neural Technology,” in 2022 IEEE International Conference on Systems, Man, and Cybernetics (SMC), Oct. 2022, pp. 1736–1741, doi: 10.1109/SMC53654.2022.9945379.
| F. Mivalt et al., “Deep Generative Networks for Algorithm Development in Implantable Neural Technology,” in 2022 IEEE International Conference on Systems, Man, and Cybernetics (SMC), Oct. 2022, pp. 1736–1741, doi: 10.1109/SMC53654.2022.9945379.
Evoked Response Potential Analysis
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
| K. J. Miller et al., “Canonical Response Parameterization: Quantifying the structure of responses to single-pulse intracranial electrical brain stimulation,” PLOS Comput. Biol., vol. 19, no. 5, p. e1011105, May 2023, doi: 10.1371/journal.pcbi.1011105.
| K. J. Miller et al., “Canonical Response Parameterization: Quantifying the structure of responses to single-pulse intracranial electrical brain stimulation,” PLOS Comput. Biol., vol. 19, no. 5, p. e1011105, May 2023, doi: 10.1371/journal.pcbi.1011105.
EEG Slow Wave Detection and Analysis
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Readme to the EEG Slow Detection project available in this repository in this repository: `projects/slow_wave_detection/readme <./projects/slow_wave_detection/readme.rst>`_.

Acknowledgement
"""""""""""""""""""
| XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX


Funding
""""""""""""""""""

BEST was developed under projects supported by

BEST was developed under projects supported by NIH Brain Initiative UH2&3 NS095495 Neurophysiologically-Based Brain State Tracking & Modulation in Focal Epilepsy, NIH U01-NS128612 An Ecosystem of Techmology and Protocols for Adaptive Neuromodulation Research in Humans, DARPA HR0011-20-2-0028 Manipulating and Optimizing Brain Rhythms for Enhancement of Sleep (Morpheus).
- NIH Brain Initiative UH2&3 NS095495 - *Neurophysiologically-Based Brain State Tracking & Modulation in Focal Epilepsy*,
- NIH U01-NS128612 - *An Ecosystem of Techmology and Protocols for Adaptive Neuromodulation Research in Humans*,
- DARPA - HR0011-20-2-0028 *Manipulating and Optimizing Brain Rhythms for Enhancement of Sleep (Morpheus)*.

Filip Mivalt was also partially supported by the grant FEKT-K-22-7649 realized within the project Quality Internal Grants of the Brno University of Technology (KInG BUT), Reg. No. CZ.02.2.69/0.0/0.0/19_073/0016948, which is financed from the OP RDE.


License
""""""""""""""""""

This software is licensed under GNU license. For details see the `LICENSE <https://github.com/bnelair/best-toolbox/blob/master/LICENSE>`_ file in the root directory of this project.
This software is licensed under BSD-3 license. For details see the `LICENSE <https://github.com/bnelair/best-toolbox/blob/master/LICENSE>`_ file in the root directory of this project.


Documentation
Expand Down
Binary file removed _images/favicon-32x32_piespro.png
Binary file not shown.
Binary file removed _images/icon_piespro.png
Binary file not shown.
Binary file removed _images/logo.png
Binary file not shown.
Binary file removed _images/logo_final_noframe_white.png
Binary file not shown.
Binary file removed _images/logo_frame.png
Binary file not shown.
Binary file added _images/slope_vaclav.pdf
Binary file not shown.
Binary file added _images/slope_vaclav.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added _images/slow_waves_example.pdf
Binary file not shown.
Binary file added _images/slow_waves_example.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 2 additions & 1 deletion best/bids/utils.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@

import os

import pandas as pd

from tqdm import tqdm
from mef_tools.io import MefReader

from best.files import get_files

def list_mefd_files(path_bidsdata):
Expand Down
22 changes: 17 additions & 5 deletions best/feature_extraction/SpectralFeatures.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,16 @@ def mean_frequency(args):

f = np.reshape(f, (1, -1))
pwr = np.sum(P, axis=1)
mnfreq = np.dot(P, f.T).squeeze() / pwr
# Ensure pwr does not contain zeros to avoid division by zero
pwr[pwr == 0] = np.nan # Replace zeros with NaNs to handle them later

# Suppress warnings temporarily and handle invalid values
with np.errstate(invalid='ignore', divide='ignore'):
mnfreq = np.dot(P, f.T).squeeze() / pwr

# Handle invalid values (e.g., NaNs or infinities) in the result
mnfreq = np.nan_to_num(mnfreq, nan=0.0, posinf=0.0, neginf=0.0)

return [mnfreq], ['MEAN_DOMINANT_FREQUENCY']


Expand Down Expand Up @@ -159,13 +168,16 @@ def relative_bands(args):
fullpsdx = np.nansum(Pxx[:, (freq >= bands.min()) & (freq <= bands.max())], axis=1)
for band in bands:
subpsdx = Pxx[:, (freq >= band[0]) & (freq <= band[1])]
outp_params.append(
np.nansum(subpsdx, axis=1) / fullpsdx
)
# Suppress warnings temporarily and handle invalid values
with np.errstate(invalid='ignore', divide='ignore'):
ratio = np.nansum(subpsdx, axis=1) / fullpsdx

# Handle invalid values (e.g., NaNs or infinities) in the ratio
ratio = np.nan_to_num(ratio, nan=0.0, posinf=0.0, neginf=0.0)
outp_params.append(ratio)
outp_msg.append('REL_PSD_' + str(band[0]) + '-' + str(band[1]) + 'Hz')
return outp_params, outp_msg


def normalized_entropy(args):
"""
**Spectral entropy (Shannon Entropy)**
Expand Down
Loading

0 comments on commit 2c8adb7

Please sign in to comment.