neurokit2

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

NeuroKit2

NeuroKit2

Overview

概述

NeuroKit2 is a comprehensive Python toolkit for processing and analyzing physiological signals (biosignals). Use this skill to process cardiovascular, neural, autonomic, respiratory, and muscular signals for psychophysiology research, clinical applications, and human-computer interaction studies.
NeuroKit2是一款全面的Python工具包,用于处理和分析生理信号(生物信号)。可使用该工具处理心血管、神经、自主神经、呼吸和肌肉信号,适用于心理生理学研究、临床应用以及人机交互研究。

When to Use This Skill

适用场景

Apply this skill when working with:
  • Cardiac signals: ECG, PPG, heart rate variability (HRV), pulse analysis
  • Brain signals: EEG frequency bands, microstates, complexity, source localization
  • Autonomic signals: Electrodermal activity (EDA/GSR), skin conductance responses (SCR)
  • Respiratory signals: Breathing rate, respiratory variability (RRV), volume per time
  • Muscular signals: EMG amplitude, muscle activation detection
  • Eye tracking: EOG, blink detection and analysis
  • Multi-modal integration: Processing multiple physiological signals simultaneously
  • Complexity analysis: Entropy measures, fractal dimensions, nonlinear dynamics
在处理以下内容时可应用本工具:
  • 心脏信号:ECG、PPG、心率变异性(HRV)、脉搏分析
  • 大脑信号:EEG频段、微状态、复杂度、源定位
  • 自主神经信号:皮肤电活动(EDA/GSR)、皮肤电反应(SCR)
  • 呼吸信号:呼吸频率、呼吸变异性(RRV)、单位时间通气量
  • 肌肉信号:EMG振幅、肌肉激活检测
  • 眼动追踪:EOG、眨眼检测与分析
  • 多模态整合:同时处理多种生理信号
  • 复杂度分析:熵测量、分形维数、非线性动力学

Core Capabilities

核心功能

1. Cardiac Signal Processing (ECG/PPG)

1. 心脏信号处理(ECG/PPG)

Process electrocardiogram and photoplethysmography signals for cardiovascular analysis. See
references/ecg_cardiac.md
for detailed workflows.
Primary workflows:
  • ECG processing pipeline: cleaning → R-peak detection → delineation → quality assessment
  • HRV analysis across time, frequency, and nonlinear domains
  • PPG pulse analysis and quality assessment
  • ECG-derived respiration extraction
Key functions:
python
import neurokit2 as nk
处理心电图和光电容积脉搏波信号,用于心血管分析。详细工作流程请参考
references/ecg_cardiac.md
主要工作流程:
  • ECG处理流水线:清洗 → R波峰检测 → 波形划分 → 质量评估
  • 时域、频域和非线性域的HRV分析
  • PPG脉搏分析与质量评估
  • 基于ECG的呼吸信号提取
关键函数:
python
import neurokit2 as nk

Complete ECG processing pipeline

完整ECG处理流水线

signals, info = nk.ecg_process(ecg_signal, sampling_rate=1000)
signals, info = nk.ecg_process(ecg_signal, sampling_rate=1000)

Analyze ECG data (event-related or interval-related)

分析ECG数据(事件相关或区间相关)

analysis = nk.ecg_analyze(signals, sampling_rate=1000)
analysis = nk.ecg_analyze(signals, sampling_rate=1000)

Comprehensive HRV analysis

全面HRV分析

hrv = nk.hrv(peaks, sampling_rate=1000) # Time, frequency, nonlinear domains
undefined
hrv = nk.hrv(peaks, sampling_rate=1000) # 时域、频域、非线性域
undefined

2. Heart Rate Variability Analysis

2. 心率变异性分析

Compute comprehensive HRV metrics from cardiac signals. See
references/hrv.md
for all indices and domain-specific analysis.
Supported domains:
  • Time domain: SDNN, RMSSD, pNN50, SDSD, and derived metrics
  • Frequency domain: ULF, VLF, LF, HF, VHF power and ratios
  • Nonlinear domain: Poincaré plot (SD1/SD2), entropy measures, fractal dimensions
  • Specialized: Respiratory sinus arrhythmia (RSA), recurrence quantification analysis (RQA)
Key functions:
python
undefined
从心脏信号中计算全面的HRV指标。所有指标和特定域分析请参考
references/hrv.md
支持的分析域:
  • 时域:SDNN、RMSSD、pNN50、SDSD及衍生指标
  • 频域:ULF、VLF、LF、HF、VHF功率及比值
  • 非线性域:庞加莱图(SD1/SD2)、熵测量、分形维数
  • 专项分析:呼吸性窦性心律不齐(RSA)、递归量化分析(RQA)
关键函数:
python
undefined

All HRV indices at once

一次性获取所有HRV指标

hrv_indices = nk.hrv(peaks, sampling_rate=1000)
hrv_indices = nk.hrv(peaks, sampling_rate=1000)

Domain-specific analysis

特定域分析

hrv_time = nk.hrv_time(peaks) hrv_freq = nk.hrv_frequency(peaks, sampling_rate=1000) hrv_nonlinear = nk.hrv_nonlinear(peaks, sampling_rate=1000) hrv_rsa = nk.hrv_rsa(peaks, rsp_signal, sampling_rate=1000)
undefined
hrv_time = nk.hrv_time(peaks) hrv_freq = nk.hrv_frequency(peaks, sampling_rate=1000) hrv_nonlinear = nk.hrv_nonlinear(peaks, sampling_rate=1000) hrv_rsa = nk.hrv_rsa(peaks, rsp_signal, sampling_rate=1000)
undefined

3. Brain Signal Analysis (EEG)

3. 大脑信号分析(EEG)

Analyze electroencephalography signals for frequency power, complexity, and microstate patterns. See
references/eeg.md
for detailed workflows and MNE integration.
Primary capabilities:
  • Frequency band power analysis (Delta, Theta, Alpha, Beta, Gamma)
  • Channel quality assessment and re-referencing
  • Source localization (sLORETA, MNE)
  • Microstate segmentation and transition dynamics
  • Global field power and dissimilarity measures
Key functions:
python
undefined
分析脑电图信号的频率功率、复杂度和微状态模式。详细工作流程及MNE集成请参考
references/eeg.md
主要功能:
  • 频段功率分析(Delta、Theta、Alpha、Beta、Gamma)
  • 通道质量评估与重新参考
  • 源定位(sLORETA、MNE)
  • 微状态分割与转移动力学
  • 全局场功率与相异度测量
关键函数:
python
undefined

Power analysis across frequency bands

跨频段功率分析

power = nk.eeg_power(eeg_data, sampling_rate=250, channels=['Fz', 'Cz', 'Pz'])
power = nk.eeg_power(eeg_data, sampling_rate=250, channels=['Fz', 'Cz', 'Pz'])

Microstate analysis

微状态分析

microstates = nk.microstates_segment(eeg_data, n_microstates=4, method='kmod') static = nk.microstates_static(microstates) dynamic = nk.microstates_dynamic(microstates)
undefined
microstates = nk.microstates_segment(eeg_data, n_microstates=4, method='kmod') static = nk.microstates_static(microstates) dynamic = nk.microstates_dynamic(microstates)
undefined

4. Electrodermal Activity (EDA)

4. 皮肤电活动(EDA)

Process skin conductance signals for autonomic nervous system assessment. See
references/eda.md
for detailed workflows.
Primary workflows:
  • Signal decomposition into tonic and phasic components
  • Skin conductance response (SCR) detection and analysis
  • Sympathetic nervous system index calculation
  • Autocorrelation and changepoint detection
Key functions:
python
undefined
处理皮肤电导信号,用于自主神经系统评估。详细工作流程请参考
references/eda.md
主要工作流程:
  • 将信号分解为紧张性和相位性成分
  • 皮肤电反应(SCR)检测与分析
  • 交感神经系统指数计算
  • 自相关与变化点检测
关键函数:
python
undefined

Complete EDA processing

完整EDA处理

signals, info = nk.eda_process(eda_signal, sampling_rate=100)
signals, info = nk.eda_process(eda_signal, sampling_rate=100)

Analyze EDA data

分析EDA数据

analysis = nk.eda_analyze(signals, sampling_rate=100)
analysis = nk.eda_analyze(signals, sampling_rate=100)

Sympathetic nervous system activity

交感神经系统活动

sympathetic = nk.eda_sympathetic(signals, sampling_rate=100)
undefined
sympathetic = nk.eda_sympathetic(signals, sampling_rate=100)
undefined

5. Respiratory Signal Processing (RSP)

5. 呼吸信号处理(RSP)

Analyze breathing patterns and respiratory variability. See
references/rsp.md
for detailed workflows.
Primary capabilities:
  • Respiratory rate calculation and variability analysis
  • Breathing amplitude and symmetry assessment
  • Respiratory volume per time (fMRI applications)
  • Respiratory amplitude variability (RAV)
Key functions:
python
undefined
分析呼吸模式与呼吸变异性。详细工作流程请参考
references/rsp.md
主要功能:
  • 呼吸频率计算与变异性分析
  • 呼吸振幅与对称性评估
  • 单位时间呼吸容积(fMRI应用)
  • 呼吸振幅变异性(RAV)
关键函数:
python
undefined

Complete RSP processing

完整RSP处理

signals, info = nk.rsp_process(rsp_signal, sampling_rate=100)
signals, info = nk.rsp_process(rsp_signal, sampling_rate=100)

Respiratory rate variability

呼吸频率变异性

rrv = nk.rsp_rrv(signals, sampling_rate=100)
rrv = nk.rsp_rrv(signals, sampling_rate=100)

Respiratory volume per time

单位时间呼吸容积

rvt = nk.rsp_rvt(signals, sampling_rate=100)
undefined
rvt = nk.rsp_rvt(signals, sampling_rate=100)
undefined

6. Electromyography (EMG)

6. 肌电图(EMG)

Process muscle activity signals for activation detection and amplitude analysis. See
references/emg.md
for workflows.
Key functions:
python
undefined
处理肌肉活动信号,用于激活检测与振幅分析。工作流程请参考
references/emg.md
关键函数:
python
undefined

Complete EMG processing

完整EMG处理

signals, info = nk.emg_process(emg_signal, sampling_rate=1000)
signals, info = nk.emg_process(emg_signal, sampling_rate=1000)

Muscle activation detection

肌肉激活检测

activation = nk.emg_activation(signals, sampling_rate=1000, method='threshold')
undefined
activation = nk.emg_activation(signals, sampling_rate=1000, method='threshold')
undefined

7. Electrooculography (EOG)

7. 眼电图(EOG)

Analyze eye movement and blink patterns. See
references/eog.md
for workflows.
Key functions:
python
undefined
分析眼球运动与眨眼模式。工作流程请参考
references/eog.md
关键函数:
python
undefined

Complete EOG processing

完整EOG处理

signals, info = nk.eog_process(eog_signal, sampling_rate=500)
signals, info = nk.eog_process(eog_signal, sampling_rate=500)

Extract blink features

提取眨眼特征

features = nk.eog_features(signals, sampling_rate=500)
undefined
features = nk.eog_features(signals, sampling_rate=500)
undefined

8. General Signal Processing

8. 通用信号处理

Apply filtering, decomposition, and transformation operations to any signal. See
references/signal_processing.md
for comprehensive utilities.
Key operations:
  • Filtering (lowpass, highpass, bandpass, bandstop)
  • Decomposition (EMD, SSA, wavelet)
  • Peak detection and correction
  • Power spectral density estimation
  • Signal interpolation and resampling
  • Autocorrelation and synchrony analysis
Key functions:
python
undefined
对任意信号应用滤波、分解与变换操作。全面工具请参考
references/signal_processing.md
关键操作:
  • 滤波(低通、高通、带通、带阻)
  • 分解(EMD、SSA、小波)
  • 波峰检测与校正
  • 功率谱密度估计
  • 信号插值与重采样
  • 自相关与同步分析
关键函数:
python
undefined

Filtering

滤波

filtered = nk.signal_filter(signal, sampling_rate=1000, lowcut=0.5, highcut=40)
filtered = nk.signal_filter(signal, sampling_rate=1000, lowcut=0.5, highcut=40)

Peak detection

波峰检测

peaks = nk.signal_findpeaks(signal)
peaks = nk.signal_findpeaks(signal)

Power spectral density

功率谱密度

psd = nk.signal_psd(signal, sampling_rate=1000)
undefined
psd = nk.signal_psd(signal, sampling_rate=1000)
undefined

9. Complexity and Entropy Analysis

9. 复杂度与熵分析

Compute nonlinear dynamics, fractal dimensions, and information-theoretic measures. See
references/complexity.md
for all available metrics.
Available measures:
  • Entropy: Shannon, approximate, sample, permutation, spectral, fuzzy, multiscale
  • Fractal dimensions: Katz, Higuchi, Petrosian, Sevcik, correlation dimension
  • Nonlinear dynamics: Lyapunov exponents, Lempel-Ziv complexity, recurrence quantification
  • DFA: Detrended fluctuation analysis, multifractal DFA
  • Information theory: Fisher information, mutual information
Key functions:
python
undefined
计算非线性动力学、分形维数与信息论测量指标。所有可用指标请参考
references/complexity.md
可用测量指标:
  • :香农熵、近似熵、样本熵、排列熵、谱熵、模糊熵、多尺度熵
  • 分形维数:Katz维数、Higuchi维数、Petrosian维数、Sevcik维数、关联维数
  • 非线性动力学:李雅普诺夫指数、Lempel-Ziv复杂度、递归量化分析
  • DFA:去趋势波动分析、多重分形DFA
  • 信息论:Fisher信息、互信息
关键函数:
python
undefined

Multiple complexity metrics at once

一次性获取多个复杂度指标

complexity_indices = nk.complexity(signal, sampling_rate=1000)
complexity_indices = nk.complexity(signal, sampling_rate=1000)

Specific measures

特定测量指标

apen = nk.entropy_approximate(signal) dfa = nk.fractal_dfa(signal) lyap = nk.complexity_lyapunov(signal, sampling_rate=1000)
undefined
apen = nk.entropy_approximate(signal) dfa = nk.fractal_dfa(signal) lyap = nk.complexity_lyapunov(signal, sampling_rate=1000)
undefined

10. Event-Related Analysis

10. 事件相关分析

Create epochs around stimulus events and analyze physiological responses. See
references/epochs_events.md
for workflows.
Primary capabilities:
  • Epoch creation from event markers
  • Event-related averaging and visualization
  • Baseline correction options
  • Grand average computation with confidence intervals
Key functions:
python
undefined
围绕刺激事件创建时间窗并分析生理反应。工作流程请参考
references/epochs_events.md
主要功能:
  • 从事件标记创建时间窗
  • 事件相关平均与可视化
  • 基线校正选项
  • 带置信区间的总平均计算
关键函数:
python
undefined

Find events in signal

在信号中查找事件

events = nk.events_find(trigger_signal, threshold=0.5)
events = nk.events_find(trigger_signal, threshold=0.5)

Create epochs around events

围绕事件创建时间窗

epochs = nk.epochs_create(signals, events, sampling_rate=1000, epochs_start=-0.5, epochs_end=2.0)
epochs = nk.epochs_create(signals, events, sampling_rate=1000, epochs_start=-0.5, epochs_end=2.0)

Average across epochs

跨时间窗平均

grand_average = nk.epochs_average(epochs)
undefined
grand_average = nk.epochs_average(epochs)
undefined

11. Multi-Signal Integration

11. 多信号整合

Process multiple physiological signals simultaneously with unified output. See
references/bio_module.md
for integration workflows.
Key functions:
python
undefined
同时处理多个生理信号并生成统一输出。整合工作流程请参考
references/bio_module.md
关键函数:
python
undefined

Process multiple signals at once

同时处理多个信号

bio_signals, bio_info = nk.bio_process( ecg=ecg_signal, rsp=rsp_signal, eda=eda_signal, emg=emg_signal, sampling_rate=1000 )
bio_signals, bio_info = nk.bio_process( ecg=ecg_signal, rsp=rsp_signal, eda=eda_signal, emg=emg_signal, sampling_rate=1000 )

Analyze all processed signals

分析所有处理后的信号

bio_analysis = nk.bio_analyze(bio_signals, sampling_rate=1000)
undefined
bio_analysis = nk.bio_analyze(bio_signals, sampling_rate=1000)
undefined

Analysis Modes

分析模式

NeuroKit2 automatically selects between two analysis modes based on data duration:
Event-related analysis (< 10 seconds):
  • Analyzes stimulus-locked responses
  • Epoch-based segmentation
  • Suitable for experimental paradigms with discrete trials
Interval-related analysis (≥ 10 seconds):
  • Characterizes physiological patterns over extended periods
  • Resting state or continuous activities
  • Suitable for baseline measurements and long-term monitoring
Most
*_analyze()
functions automatically choose the appropriate mode.
NeuroKit2会根据数据时长自动选择两种分析模式之一:
事件相关分析(<10秒):
  • 分析刺激锁定反应
  • 基于时间窗的分割
  • 适用于带有离散试次的实验范式
区间相关分析(≥10秒):
  • 表征较长时间内的生理模式
  • 静息状态或连续活动
  • 适用于基线测量与长期监测
大多数
*_analyze()
函数会自动选择合适的模式。

Installation

安装

bash
uv pip install neurokit2
For development version:
bash
uv pip install https://github.com/neuropsychology/NeuroKit/zipball/dev
bash
uv pip install neurokit2
开发版本安装:
bash
uv pip install https://github.com/neuropsychology/NeuroKit/zipball/dev

Common Workflows

常见工作流程

Quick Start: ECG Analysis

快速入门:ECG分析

python
import neurokit2 as nk
python
import neurokit2 as nk

Load example data

加载示例数据

ecg = nk.ecg_simulate(duration=60, sampling_rate=1000)
ecg = nk.ecg_simulate(duration=60, sampling_rate=1000)

Process ECG

处理ECG

signals, info = nk.ecg_process(ecg, sampling_rate=1000)
signals, info = nk.ecg_process(ecg, sampling_rate=1000)

Analyze HRV

分析HRV

hrv = nk.hrv(info['ECG_R_Peaks'], sampling_rate=1000)
hrv = nk.hrv(info['ECG_R_Peaks'], sampling_rate=1000)

Visualize

可视化

nk.ecg_plot(signals, info)
undefined
nk.ecg_plot(signals, info)
undefined

Multi-Modal Analysis

多模态分析

python
undefined
python
undefined

Process multiple signals

处理多个信号

bio_signals, bio_info = nk.bio_process( ecg=ecg_signal, rsp=rsp_signal, eda=eda_signal, sampling_rate=1000 )
bio_signals, bio_info = nk.bio_process( ecg=ecg_signal, rsp=rsp_signal, eda=eda_signal, sampling_rate=1000 )

Analyze all signals

分析所有信号

results = nk.bio_analyze(bio_signals, sampling_rate=1000)
undefined
results = nk.bio_analyze(bio_signals, sampling_rate=1000)
undefined

Event-Related Potential

事件相关电位

python
undefined
python
undefined

Find events

查找事件

events = nk.events_find(trigger_channel, threshold=0.5)
events = nk.events_find(trigger_channel, threshold=0.5)

Create epochs

创建时间窗

epochs = nk.epochs_create(processed_signals, events, sampling_rate=1000, epochs_start=-0.5, epochs_end=2.0)
epochs = nk.epochs_create(processed_signals, events, sampling_rate=1000, epochs_start=-0.5, epochs_end=2.0)

Event-related analysis for each signal type

针对每种信号类型进行事件相关分析

ecg_epochs = nk.ecg_eventrelated(epochs) eda_epochs = nk.eda_eventrelated(epochs)
undefined
ecg_epochs = nk.ecg_eventrelated(epochs) eda_epochs = nk.eda_eventrelated(epochs)
undefined

References

参考资料

This skill includes comprehensive reference documentation organized by signal type and analysis method:
  • ecg_cardiac.md: ECG/PPG processing, R-peak detection, delineation, quality assessment
  • hrv.md: Heart rate variability indices across all domains
  • eeg.md: EEG analysis, frequency bands, microstates, source localization
  • eda.md: Electrodermal activity processing and SCR analysis
  • rsp.md: Respiratory signal processing and variability
  • ppg.md: Photoplethysmography signal analysis
  • emg.md: Electromyography processing and activation detection
  • eog.md: Electrooculography and blink analysis
  • signal_processing.md: General signal utilities and transformations
  • complexity.md: Entropy, fractal, and nonlinear measures
  • epochs_events.md: Event-related analysis and epoch creation
  • bio_module.md: Multi-signal integration workflows
Load specific reference files as needed using the Read tool to access detailed function documentation and parameters.
本工具包含按信号类型和分析方法组织的全面参考文档:
  • ecg_cardiac.md:ECG/PPG处理、R波峰检测、波形划分、质量评估
  • hrv.md:全分析域的心率变异性指标
  • eeg.md:EEG分析、频段、微状态、源定位
  • eda.md:皮肤电活动处理与SCR分析
  • rsp.md:呼吸信号处理与变异性分析
  • ppg.md:光电容积脉搏波信号分析
  • emg.md:肌电图处理与激活检测
  • eog.md:眼电图与眨眼分析
  • signal_processing.md:通用信号工具与变换
  • complexity.md:熵、分形与非线性测量
  • epochs_events.md:事件相关分析与时间窗创建
  • bio_module.md:多信号整合工作流程
可根据需要使用读取工具加载特定参考文件,以获取详细的函数文档和参数说明。

Additional Resources

额外资源