<jupyter_start><jupyter_text>Interval-related Analysis This example shows how to use Neurokit to analyze longer periods of data (i.e., greater than 10 seconds) such as **resting state data**. If you are looking to perform event-related analysis on epochs, you can refer to this [Neurokit example](https://neurokit2.readthedocs.io/en/latest/examples/eventrelated.html) here.<jupyter_code># Load NeuroKit and other useful packages
import neurokit2 as nk
import pandas as pd
import matplotlib.pyplot as plt
%matplotlib inline
plt.rcParams['figure.figsize'] = [15, 9]  # Bigger images
plt.rcParams['font.size']= 13<jupyter_output><empty_output><jupyter_text>The Dataset First, download the dataset located on the GitHub repository. It contains 5 minutes of physiological signals recorded at a frequency of 100Hz (5 x 60 x 100 = 30000 data points).It contains the following signals : **ECG, PPG, RSP**<jupyter_code># Get data
data = pd.read_csv("https://raw.githubusercontent.com/neuropsychology/NeuroKit/master/data/bio_resting_5min_100hz.csv")<jupyter_output><empty_output><jupyter_text>This is the resting state data from ***1*** **participant** who was asked to close his/her eyes for 8 minutes, trying not to think of anything as well as not to fall asleep.   Process the Signals In this analysis here, we will focus on extracting **ECG** and **RSP** features. To process the respective physiological signals, you can use [ecg_process()](https://neurokit2.readthedocs.io/en/latest/functions.htmlneurokit2.ecg_process) and [rsp_process()](https://neurokit2.readthedocs.io/en/latest/functions.htmlneurokit2.rsp_process). You can then then visualize these signals using [ecg_plot()](https://neurokit2.readthedocs.io/en/latest/functions.htmlneurokit2.ecg_plot) and [rsp_plot()](https://neurokit2.readthedocs.io/en/latest/functions.htmlneurokit2.rsp_plot). For the purposes of these example, we will select just 3000 datapoints (or 30s) to visualize.*Note:* Do remember to specify the correct `sampling_rate` (in this case, to 100Hz) in which the signals were generated, in all the relevant functions.<jupyter_code># Process ecg
ecg_signals, info = nk.ecg_process(data["ECG"], sampling_rate=100)
plot = nk.ecg_plot(ecg_signals[:3000], sampling_rate=100)
# Process rsp
rsp_signals, info = nk.rsp_process(data["RSP"], sampling_rate=100)
plot = nk.rsp_plot(rsp_signals[:3000], sampling_rate=100)<jupyter_output><empty_output><jupyter_text>Extract Features Now that we have the processed signals, we can now perform the analysis using [ecg_intervalrelated()](https://neurokit2.readthedocs.io/en/latest/functions.htmlneurokit2.ecg_intervalrelated) and [rsp_intervalrelated()](https://neurokit2.readthedocs.io/en/latest/functions.htmlneurokit2.rsp_intervalrelated). Simply provide the processed dataframe and these functions will return a dataframe of the features pertaining to the specific signal.These features will be quite different from event-related features (See [Event-related analysis example](https://neurokit2.readthedocs.io/en/latest/examples/eventrelated.html)) as these signals were generated over a longer period of time. Hence, apart from the mean signal rate, variability metrices pertaining to heart rate variability (HRV) and respiratory rate variability (RRV) are also extracted here.<jupyter_code>nk.ecg_intervalrelated(ecg_signals)
nk.rsp_intervalrelated(rsp_signals)<jupyter_output><empty_output><jupyter_text>Optional: Segmenting the Data If you want to segment your data for analysis, such as analyzing two separate portions of your resting state data, you can simply do so by splitting the `ecg_signals` dataframe into **epochs** using [epochs_create()](https://neurokit2.readthedocs.io/en/latest/functions.htmlneurokit2.epochs_create). Using this example dataset, let's say you want to analyze the first half and the second half of the ECG data. This means that each halved data would last for 60 x 2.5s = *150s*.  In this function, we would also specify the onset of events to be at the 0th (for the first half of the data) and the 15000th datapoint (for the second half of the data), since there are 30000 data points in total.<jupyter_code># Half the data
epochs = nk.epochs_create(ecg_signals, events=[0, 15000], sampling_rate=100, epochs_start=0, epochs_end=150)<jupyter_output><empty_output><jupyter_text>This returns a dictionary of 2 processed ECG dataframes, which you can then enter into `ecg_intervalrelated()`.<jupyter_code># Analyze
nk.ecg_intervalrelated(epochs)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.09064780013265532, 'fraction_numerical': 0.023435772717223084, 'mean_word_length': 6.380097879282219, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 25, 'https://': 10, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '6', 'dir': 'jupyter-structured-clean-dedup', 'id': '351052', 'license': 'MIT', 'n_tokens_mistral': 1463, 'n_tokens_neox': 1362, 'n_words': 465, 'path': 'docs/examples/intervalrelated.ipynb', 'repo_name': 'elaineteo2000/NeuroKit', 'size': '4523'}