<jupyter_start><jupyter_text>Detection of Cardiac Arrhythmia from ECG SequenceIn the posted [1.2 version](https://cit.mak.ac.ug/staff/jquinn/software/lsanomaly.html) several demonstrations were included in the distribution. This notebook recreates the ECG demonstration.This uses both the static (`predict_proba`) and sequence (`predict_sequence`) detection methods on ECG data. The specific data can be downloaded [here](http://www.physionet.org/cgi-bin/ATM) selecting Select MIT-BIH Arrhythmia Database ( _mitdb_ ), and export as a CSV.The data was initially prepared from the CSV by applying a median filter with a kernel size of 301:```X = np.loadtxt(    "data/MIT_physionet_sample.csv", skiprows=2, usecols=(1, 2), delimiter=",") Apply a median filter with a window size of 301X[:, 0] = X[:, 0] - scipy.signal.medfilt(X[:, 0], kernel_size=301)X[:, 1] = X[:, 1] - scipy.signal.medfilt(X[:, 1], kernel_size=301)```For efficiency, the data used here has the median filter applied and is in `/notebooks/filtered_ecg.json`.<jupyter_code>import os
import numpy as np
from pathlib import Path

cwd = os.getcwd()
os.chdir(Path(cwd).parents[1])

from lsanomaly import LSAnomaly
import lsanomaly.notebooks.dynamic as demo<jupyter_output><empty_output><jupyter_text>Data Prep<jupyter_code>X_train, X_test = demo.data_prep(
    data_file=os.path.join(
        os.getcwd(), "lsanomaly", "notebooks", "filtered_ecg.json"
    )
)<jupyter_output><empty_output><jupyter_text>Anomaly Models<jupyter_code>anomaly_model = LSAnomaly(rho=1, sigma=0.5)
anomaly_model.fit(X_train)<jupyter_output><empty_output><jupyter_text>Static Detection<jupyter_code>y_pred_static = anomaly_model.predict_proba(X_test)<jupyter_output><empty_output><jupyter_text>Dynamic DetectionBecause the dynamic model uses an HMM, a state transition matrix $A$ and an initial probability vector $\pi$ are required.We set$$\begin{equation*}A = \begin{pmatrix}0.999 & 0.001 \\0.01 & 0.99 \\\end{pmatrix}\end{equation*}$$and$$\pi = (0.5, .05)$$<jupyter_code>A = np.array([[0.999, 0.001], [0.01, 0.99]])
pi = np.array([0.5, 0.5])
y_pred_dynamic = anomaly_model.predict_sequence(
    X_test, A, pi, inference="smoothing"
)<jupyter_output><empty_output><jupyter_text>RESULTS<jupyter_code>demo.plot_results(X_test, y_pred_static, y_pred_dynamic);<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.13253012048192772, 'fraction_numerical': 0.02710843373493976, 'mean_word_length': 6.404458598726115, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 25, 'https://': 1, 'lorem ipsum': 0, 'www.': 1, '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': '78806', 'license': 'MIT', 'n_tokens_mistral': 892, 'n_tokens_neox': 818, 'n_words': 194, 'path': 'lsanomaly/notebooks/static_dynamic.ipynb', 'repo_name': 'lsanomaly/lsanomaly', 'size': '2324'}