{ "cells": [ { "cell_type": "code", "execution_count": 41, "metadata": {}, "outputs": [], "source": [ "import os\n", "import wfdb\n", "import numpy as np\n", "import gradio as gr\n", "from models.inception import *\n", "from scipy.signal import resample\n", "\n", "\n", "def load_data():\n", " cwd = os.getcwd()\n", " sample_data = f\"{cwd}/sample_data/ath_001\"\n", " ecg, meta_data = wfdb.rdsamp(sample_data)\n", " lead_I = ecg[:,0]\n", " sample_frequency = meta_data[\"fs\"]\n", " return lead_I, sample_frequency\n", "\n", "def preprocess_ecg(ecg,fs):\n", " if fs != 500:\n", " ecg = resample(ecg, int(len(ecg)*(500/fs)))\n", " else:\n", " pass\n", " if len(ecg) > 5000:\n", " ecg = ecg[:5000]\n", " else:\n", " pass\n", " return ecg\n", "\n", "def load_model(sample_frequency,recording_time, num_leads):\n", " cwd = os.getcwd()\n", " weights = f\"{cwd}/models/weights/model_weights_leadI.h5\"\n", " model = build_model((sample_frequency * recording_time, num_leads), 1)\n", " model.load_weights(weights)\n", " return model\n", "\n", "\n", "def run(ecg):\n", " SAMPLE_FREQUENCY = 100\n", " TIME = 10\n", " NUM_LEADS = 1\n", " data, fs = load_data()\n", " model = load_model(sample_frequency=SAMPLE_FREQUENCY,recording_time=TIME,num_leads=NUM_LEADS)\n", " predicion = model.predict(data)\n", " return str(predicion)" ] }, { "cell_type": "code", "execution_count": 42, "metadata": {}, "outputs": [], "source": [ "data, fs = load_data()" ] }, { "cell_type": "code", "execution_count": 43, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(5000,)" ] }, "execution_count": 43, "metadata": {}, "output_type": "execute_result" } ], "source": [ "data.shape" ] }, { "cell_type": "code", "execution_count": 49, "metadata": {}, "outputs": [], "source": [ "out = preprocess_ecg(data,250)" ] }, { "cell_type": "code", "execution_count": 50, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(5000,)" ] }, "execution_count": 50, "metadata": {}, "output_type": "execute_result" } ], "source": [ "out[:5000].shape" ] }, { "cell_type": "code", "execution_count": 39, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "0.5" ] }, "execution_count": 39, "metadata": {}, "output_type": "execute_result" } ], "source": [ "500/1000" ] }, { "cell_type": "code", "execution_count": 30, "metadata": {}, "outputs": [], "source": [ "upsamp = preprocess_ecg(data,1000)" ] }, { "cell_type": "code", "execution_count": 31, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([0.])" ] }, "execution_count": 31, "metadata": {}, "output_type": "execute_result" } ], "source": [ "upsamp" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "new_tf", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.9.12" } }, "nbformat": 4, "nbformat_minor": 2 }