Neurokit2

Neurokit2 automation and integration for biosignal processing and analysis

NeuroKit2 is a community skill for processing physiological signals using the NeuroKit2 Python library, covering ECG analysis, EDA processing, EMG extraction, respiratory signal handling, and heart rate variability computation for biosignal research.

What Is This?

Overview

NeuroKit2 provides tools for analyzing physiological data from biosensor recordings. It covers ECG analysis that detects R-peaks, computes heart rate, and extracts cardiac cycle features from electrocardiogram signals, EDA processing that decomposes electrodermal activity into tonic and phasic components for arousal measurement, EMG extraction that processes electromyography signals to quantify muscle activation patterns and timing, respiratory signal handling that derives breathing rate and depth from respiratory sensor data, and heart rate variability computation that calculates time-domain, frequency-domain, and nonlinear HRV metrics from inter-beat intervals. The skill enables researchers to process biosignals systematically and reproducibly across different recording setups.

Who Should Use This

This skill serves neuroscience researchers analyzing physiological recordings, psychophysiology labs processing multi-channel biosensor data, and biomedical engineers building signal analysis pipelines. It is also well suited for clinical researchers who need standardized feature extraction from wearable or laboratory-grade biosensor hardware.

Why Use It?

Problems It Solves

Physiological signals contain noise and artifacts that require specialized preprocessing before feature extraction. Different biosignal types need distinct processing pipelines with domain-specific algorithms. Computing HRV metrics manually requires implementing multiple statistical and spectral analysis methods. Comparing results across studies needs standardized processing parameters, which NeuroKit2 provides through consistent default settings and well-documented function arguments.

Core Highlights

ECG processor detects heartbeats and extracts cardiac features from raw signals. EDA analyzer decomposes skin conductance into tonic and phasic components. HRV calculator computes comprehensive variability metrics from R-R intervals. Signal cleaner applies artifact removal and filtering across signal types.

How to Use It?

Basic Usage

import neurokit2 as nk
import numpy as np

ecg = nk.ecg_simulate(
  duration=60,
  sampling_rate=500,
  heart_rate=70)

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

rpeaks = info[
  'ECG_R_Peaks']
heart_rate = signals[
  'ECG_Rate']
print(
  f'Mean HR: '
  f'{np.mean(heart_rate)'
  f':.1f} bpm')
print(
  f'R-peaks: '
  f'{len(rpeaks)}')

hrv = nk.hrv(
  signals,
  sampling_rate=500)
print(
  f'RMSSD: '
  f'{hrv["HRV_RMSSD"]'
  f'.values[0]:.2f} ms')
print(
  f'SDNN: '
  f'{hrv["HRV_SDNN"]'
  f'.values[0]:.2f} ms')

Real-World Examples

import neurokit2 as nk
import pandas as pd

class BiosignalPipeline:
  def __init__(
    self,
    sampling_rate:
      int = 500
  ):
    self.sr = sampling_rate

  def process_ecg(
    self,
    signal
  ) -> dict:
    signals, info = (
      nk.ecg_process(
        signal,
        sampling_rate=
          self.sr))
    hrv = nk.hrv(
      signals,
      sampling_rate=
        self.sr)
    return {
      'heart_rate': signals[
        'ECG_Rate'].mean(),
      'rmssd': hrv[
        'HRV_RMSSD']
        .values[0],
      'sdnn': hrv[
        'HRV_SDNN']
        .values[0]}

  def process_eda(
    self,
    signal
  ) -> dict:
    signals, info = (
      nk.eda_process(
        signal,
        sampling_rate=
          self.sr))
    return {
      'scr_peaks': len(
        info[
          'SCR_Peaks']),
      'mean_tonic': signals[
        'EDA_Tonic'].mean()}

  def analyze(
    self,
    ecg_signal,
    eda_signal
  ) -> dict:
    ecg_feats = (
      self.process_ecg(
        ecg_signal))
    eda_feats = (
      self.process_eda(
        eda_signal))
    return {
      **ecg_feats,
      **eda_feats}

Advanced Tips

Use the epoch extraction functions to segment continuous signals around event markers for event-related analysis. For example, you can epoch ECG data around stimulus onset timestamps to examine cardiac responses to specific experimental conditions. Compare multiple cleaning methods on your data since optimal artifact removal varies by recording quality and equipment. Apply nonlinear HRV metrics alongside standard time and frequency measures for more comprehensive autonomic assessment.

When to Use It?

Use Cases

Process ECG recordings to compute heart rate variability metrics for a stress research study. Analyze electrodermal activity to detect arousal responses in an emotion recognition experiment. Build a multi-signal processing pipeline that extracts features from ECG, EDA, and respiratory channels simultaneously.

Related Topics

Physiological signal processing, ECG analysis, heart rate variability, electrodermal activity, biosignal analysis, psychophysiology, and biomedical engineering.

Important Notes

Requirements

NeuroKit2 Python package with NumPy and SciPy dependencies. Physiological signal data sampled at known rates. Matplotlib for signal visualization output.

Usage Recommendations

Do: verify signal quality before automated processing since noisy recordings produce unreliable features. Report processing parameters including filter settings and peak detection methods for reproducibility. Visually inspect detected peaks and segmentation on representative samples to validate automated results before running batch processing across your full dataset.

Don't: apply default parameters without checking whether they suit your recording equipment and protocol. Mix processing parameters across subjects in the same study since this introduces systematic bias. Interpret HRV metrics from recordings shorter than five minutes since frequency-domain measures require minimum duration.

Limitations

Automated peak detection accuracy degrades with high artifact content in recordings. Processing parameters optimized for one recording system may not transfer to different hardware configurations. HRV frequency-domain analysis requires stationary signal segments which movement artifacts can violate.