[b46260]: / DataExtraction.ipynb

Download this file

143 lines (142 with data), 3.8 kB

{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import wfdb\n",
    "import pandas as pd\n",
    "import numpy as np\n",
    "import os"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Retrieve ECG values and annotations using WFDB"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {},
   "outputs": [],
   "source": [
    "def read_ecg_data(file):\n",
    "    record = wfdb.rdrecord(os.path.join('picsdb/1.0.0/',file))\n",
    "    annotation = wfdb.rdann(os.path.join('picsdb/1.0.0/',file), 'atr')\n",
    "    return(pd.DataFrame(record.p_signal,columns = ['ecg']),pd.DataFrame(annotation.sample,columns = ['time']))    "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Generate CSV storing the retrieved data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "metadata": {},
   "outputs": [],
   "source": [
    "def generate_csv(infant_file):    \n",
    "    ecg, timestamp = read_ecg_data(infant_file)\n",
    "    ecg.loc[timestamp[\"time\"], 'brady'] = 1\n",
    "    ecg['brady'] = ecg['brady'].fillna(0)\n",
    "    ecg.to_csv((infant_file+\".csv\"))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 63,
   "metadata": {},
   "outputs": [],
   "source": [
    "infants = [\"infant2_ecg\",\"infant3_ecg\",\"infant4_ecg\",\"infant6_ecg\",\"infant7_ecg\",\"infant8_ecg\",\"infant9_ecg\",\"infant10_ecg\"]\n",
    "\n",
    "#Generating csv for all the infants\n",
    "for i in infants:\n",
    "    generate_csv(i)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Retrieve ECG values, bradycardia labels and qrsc peak annotations"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [],
   "source": [
    "def read_ecg_hr_data(file):\n",
    "    record = wfdb.rdrecord(os.path.join('picsdb/1.0.0/',file))\n",
    "    brady_annotation = wfdb.rdann(os.path.join('picsdb/1.0.0/',file), 'atr')\n",
    "    rpeak_annotation = wfdb.rdann(os.path.join('picsdb/1.0.0/',file), 'qrsc')\n",
    "    return(pd.DataFrame(record.p_signal,columns = ['ecg']),pd.DataFrame(brady_annotation.sample,columns = ['time']), pd.DataFrame(rpeak_annotation.sample,columns = ['time']))    "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Generate the CSV file of the retrieved data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [],
   "source": [
    "def generate_csv_2(infant_file):    \n",
    "    ecg, timestamp, rpeak = read_ecg_hr_data(infant_file)\n",
    "    ecg.loc[timestamp[\"time\"], 'brady'] = 1\n",
    "    ecg['brady'] = ecg['brady'].fillna(0)\n",
    "    ecg.loc[rpeak[\"time\"], 'rpeak'] = 1\n",
    "    ecg['rpeak'] = ecg['rpeak'].fillna(0)\n",
    "    ecg.to_csv((infant_file+\"_hr.csv\"))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "metadata": {},
   "outputs": [],
   "source": [
    "infants2 = [\"infant2_ecg\",\"infant3_ecg\",\"infant4_ecg\",\"infant5_ecg\",\"infant6_ecg\",\"infant7_ecg\",\"infant8_ecg\",\"infant9_ecg\",\"infant10_ecg\"]\n",
    "for i in infants2:\n",
    "    generate_csv_2(i)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python [conda env:physio]",
   "language": "python",
   "name": "conda-env-physio-py"
  },
  "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.7.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}