Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_eog_intervalrelated():
eog = nk.data('eog_200hz')['vEOG']
eog_signals, info = nk.eog_process(eog, sampling_rate=200)
columns = ['EOG_Peaks_N', 'EOG_Rate_Mean']
# Test with signal dataframe
features = nk.eog_intervalrelated(eog_signals)
assert all(elem in np.array(features.columns.values, dtype=str) for elem
in columns)
assert features.shape[0] == 1 # Number of rows
# Test with dict
epochs = nk.epochs_create(eog_signals, events=[5000, 10000, 15000], epochs_start=-0.1, epochs_end=1.9)
epochs_dict = nk.eog_intervalrelated(epochs)
assert all(elem in columns for elem
def test_eog_findpeaks():
eog_signal = nk.data('eog_100hz')
eog_cleaned = nk.eog_clean(eog_signal, sampling_rate=100)
# Test with Neurokit
nk_peaks = nk.eog_findpeaks(eog_cleaned, sampling_rate=100, method="neurokit", threshold=0.33, show=False)
assert nk_peaks.size == 19
# Test with MNE
mne_peaks = nk.eog_findpeaks(eog_cleaned, method="mne")
assert mne_peaks.size == 44
# Test with brainstorm
brainstorm_peaks = nk.eog_findpeaks(eog_cleaned, method="brainstorm")
assert brainstorm_peaks.size == 28
blinker_peaks = nk.eog_findpeaks(eog_cleaned, method="blinker", sampling_rate=100)
assert blinker_peaks.size == 14
def test_rsp_intervalrelated():
data = nk.data("bio_resting_5min_100hz")
df, info = nk.rsp_process(data["RSP"], sampling_rate=100)
# Test with signal dataframe
features_df = nk.rsp_intervalrelated(df)
assert features_df.shape[0] == 1 # Number of rows
# Test with dict
epochs = nk.epochs_create(df, events=[0, 15000], sampling_rate=100, epochs_end=150)
features_dict = nk.rsp_intervalrelated(epochs)
assert features_dict.shape[0] == 2 # Number of rows
def test_data():
dataset = "bio_eventrelated_100hz"
data = nk.data(dataset)
assert len(data.columns) == 4
assert data.size == 15000 * 4
assert all(elem in ["ECG", "EDA", "Photosensor", "RSP"] for elem in np.array(data.columns.values, dtype=str))
dataset2 = "bio_eventrelated_100hz.csv"
data2 = nk.data(dataset2)
assert len(data.columns) == len(data2.columns)
assert data2.size == data.size
assert all(elem in np.array(data.columns.values, dtype=str) for elem in np.array(data2.columns.values, dtype=str))
data = nk.data("bio_eventrelated_100hz")
df, info = nk.bio_process(
ecg=data["ECG"], rsp=data["RSP"], eda=data["EDA"], keep=data["Photosensor"], sampling_rate=100
)
events = nk.events_find(
data["Photosensor"], threshold_keep="below", event_conditions=["Negative", "Neutral", "Neutral", "Negative"]
)
epochs = nk.epochs_create(df, events, sampling_rate=100, epochs_start=-0.1, epochs_end=1.9)
event_related = nk.bio_analyze(epochs)
assert len(event_related) == len(epochs)
labels = [int(i) for i in event_related["Label"]]
assert labels == list(np.arange(1, len(epochs) + 1))
# Example with interval-related analysis
data = nk.data("bio_resting_8min_100hz")
df, info = nk.bio_process(ecg=data["ECG"], rsp=data["RSP"], eda=data["EDA"], sampling_rate=100)
interval_related = nk.bio_analyze(df)
assert len(interval_related) == 1
def test_bio_analyze():
# Example with event-related analysis
data = nk.data("bio_eventrelated_100hz")
df, info = nk.bio_process(
ecg=data["ECG"], rsp=data["RSP"], eda=data["EDA"], keep=data["Photosensor"], sampling_rate=100
)
events = nk.events_find(
data["Photosensor"], threshold_keep="below", event_conditions=["Negative", "Neutral", "Neutral", "Negative"]
)
epochs = nk.epochs_create(df, events, sampling_rate=100, epochs_start=-0.1, epochs_end=1.9)
event_related = nk.bio_analyze(epochs)
assert len(event_related) == len(epochs)
labels = [int(i) for i in event_related["Label"]]
assert labels == list(np.arange(1, len(epochs) + 1))
# Example with interval-related analysis
data = nk.data("bio_resting_8min_100hz")
df, info = nk.bio_process(ecg=data["ECG"], rsp=data["RSP"], eda=data["EDA"], sampling_rate=100)
matplotlib.use('Agg')
from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
import matplotlib.cm
import neurokit2 as nk
# =============================================================================
# Quick Example
# =============================================================================
# Download example data
data = nk.data("bio_eventrelated_100hz")
# Preprocess the data (filter, find peaks, etc.)
processed_data, info = nk.bio_process(ecg=data["ECG"], rsp=data["RSP"], eda=data["EDA"], sampling_rate=100)
# Compute relevant features
results = nk.bio_analyze(processed_data, sampling_rate=100)
# =============================================================================
# Simulate physiological signals
# =============================================================================
# Generate synthetic signals
ecg = nk.ecg_simulate(duration=10, heart_rate=70)
ppg = nk.ppg_simulate(duration=10, heart_rate=70)
rsp = nk.rsp_simulate(duration=10, respiratory_rate=15)
eda = nk.eda_simulate(duration=10, scr_number=3)
# Compute HRV indices
hrv = nk.hrv(peaks, sampling_rate=100, show=True)
hrv
# Save plot
fig = plt.gcf()
fig.set_size_inches(10*1.5, 6*1.5, forward=True)
fig.savefig("README_hrv.png", dpi=300, h_pad=3)
# =============================================================================
# ECG Delineation
# =============================================================================
# Download data
ecg_signal = nk.data(dataset="ecg_3000hz")['ECG']
# Extract R-peaks locations
_, rpeaks = nk.ecg_peaks(ecg_signal, sampling_rate=3000)
# Delineate
signal, waves = nk.ecg_delineate(ecg_signal, rpeaks, sampling_rate=3000, method="dwt", show=True, show_type='all')
# Save plot
fig = plt.gcf()
fig.set_size_inches(10*1.5, 6*1.5, forward=True)
fig.savefig("README_delineate.png", dpi=300, h_pad=3)
# =============================================================================
# Complexity
# =============================================================================