NeuroKit2 Documentation

repository·master·Indexed 25 days ago

https://github.com/neuropsychology/neurokit

A Python toolbox for neurophysiological signal processing. It provides high-level and mid-level APIs for cleaning, processing, and analyzing biosignals such as ECG, EDA, RSP, and EMG. The library includes utilities for ECG R-peak detection benchmarking, EEG complexity analysis (including delay and embedding dimension optimization), and access to various physiological databases like MIT-BIH and GUDB.

Tokens
30.8K
Snippets
79
Records
211
Agent score
81%

What's inside neurokit2

  1. Overview of Texas Resting-state EEG dataset

    master

    The Texas Resting-state EEG dataset contains EEG recordings from 22 subjects. Each subject has 72 EEG channels recorded during an 8-minute resting task. The task consists of interleaved 1-minute intervals of 'eyes open' (4 mins total) and 'eyes closed' (4 mins total) conditions. Participants were instructed to remain relaxed, alert, and awake throughout the recording.

    Dataset Specifications:

    • Subjects: 22
    • Channels: 72 EEG channels
    • Task Duration: 8 minutes per subject (4 mins eyes open, 4 mins eyes closed)
    • Task Structure: 1-minute interleaved intervals
    • Note: Due to a technical error, one participant has only 4 minutes of recording time.
  2. Analyze M/EEG Microstates with neurokit2

    master

    The neurokit2.microstates module provides a suite of functions for analyzing M/EEG microstates. The workflow typically involves segmenting the signal, classifying microstates, and computing dynamic properties.

    Key functions include:

    • microstates_segment(): Segments the signal into microstate components.
    • microstates_classify(): Classifies the segments into specific microstate types.
    • microstates_plot(): Visualizes the microstate analysis results.
    • microstates_clean(): Cleans the microstate data.
    • microstates_dynamic(): Computes dynamic properties of the microstates.
    • microstates_findnumber(): Determines the optimal number of microstates.
    • microstates_peaks(): Identifies microstate peaks.
    • microstates_static(): Computes static microstate properties.
  3. How NeuroKit2 API design works

    master

    NeuroKit2 follows a consistent naming convention based on the signal type and the desired goal. This allows users to switch between different signal types easily while using the same functional logic.

    • Consistency: Functions follow the pattern signaltype_functiongoal(). For example, to clean an ECG signal, you use ecg_clean(). Common goals include *_clean(), *_findpeaks(), *_process(), and *_plot().
    • Accessibility: High-level "master" functions like *_process() handle the entire pipeline (cleaning, preprocessing, and processing) using sensible defaults.
    • Flexibility: Mid-level functions like *_clean() or *_rate() allow advanced users to build custom pipelines with granular control over parameters.
  4. Implement control flow with if/else and indentation

    master

    Control flow allows code to execute differently based on conditions.

    Crucial Rule: Indentation Python uses indentation (usually 4 spaces or one TAB) to define code blocks. A colon : marks the start of a block (like an if statement), and the subsequent lines must be indented. Incorrect indentation will cause an error.

    If/Else Syntax:

    x = 5
    if x < 3:
        print("lower")
    else:
        print("higher")
  5. Understand Python functions

    master

    Functions are reusable blocks of code that typically take an input and return a transformed output. In Python, you call a function by its name followed by parentheses. Common built-in functions include str() (converts to string), int() (converts to integer), and print() (outputs to console). The range(n) function is frequently used in loops to create a sequence of integers from 0 to n-1.

    x = 3
    x = str(x)
    print(x)
    
    # Using range in a loop
    for i in range(3):
        print(i)
  6. Access elements using indexing

    master

    Dictionary Indexing

    Access elements in a dictionary using their key inside square brackets:

    mydict = {"A": 1, "B": 2, "C": 3}
    x = mydict["B"]
    print(x)  # Output: 2

    List Indexing (Zero-based)

    In Python, indexing starts at 0. The first element is at index 0, the second element is at index 1, and so on.

    mylist = [1, 2, 3]
    x = mylist[1]  # Accesses the 2nd element
    print(x)       # Output: 2
    mydict = {"A": 1, "B": 2, "C": 3}
    x = mydict["B"]
    print(x)
  7. Understand Event-related vs Interval-related physiological analysis

    master

    Physiological data analysis in NeuroKit2 typically falls into two categories:

    1. Event-related analysis: Refers to physiological changes occurring immediately in response to a specific event (e.g., stimulus presentation). This is epoch-based, where short chunks of signal (epochs) are time-segmented and locked to a stimulus. Use bio_analyze() to compute rate changes, peak characteristics, and phase characteristics.

    2. Interval-related analysis: Refers to physiological characteristics occurring over longer periods (seconds to days) without specific time-locked events (e.g., resting state, watching a movie). Use bio_analyze() to compute rate characteristics (like variability metrics) and peak characteristics over the entire duration.

  8. Perform conditional indexing and masking with NumPy

    master

    You can create a boolean mask by applying a condition to a NumPy array. This mask can then be used to subset the array or modify specific elements that meet the condition.

    import numpy as np
    
    myvector = np.array([1, 2, 3, 2, 1])
    
    # Create a boolean mask
    mask = myvector <= 2
    
    # Subset the array using the mask
    subset = myvector[mask]
    
    # Modify elements in-place using a condition
    myvector[myvector <= 2] = 6
  9. Use NumPy arrays and vectorized operations

    master

    While Python lists can hold multiple types, NumPy arrays (often called vectors when one-dimensional) are optimized containers that hold a single data type. A key advantage of arrays is vectorization: mathematical operations applied to an array are automatically propagated to every element, avoiding the need for manual loops.

    import numpy as np
    
    # Convert a list to a vector
    mylist = [1, 2, 3]
    myvector = np.array(mylist)
    
    # Vectorized operation: adds 1 to every element
    myvector = myvector + 1