Skip to content
Snippets Groups Projects
test.ipynb 6.34 KiB
Newer Older
{
 "cells": [
  {
   "cell_type": "code",
   "metadata": {},
   "outputs": [],
   "source": [
    "from memory_db import InMemoryDatabase\n",
    "from utils import populate_tables\n",
    "import csv\n",
    "import constants\n",
    "import datetime"
   ]
  },
  {
   "cell_type": "code",
   "metadata": {},
   "outputs": [],
   "source": [
    "db = InMemoryDatabase()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[]"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "cursor = db.connection.cursor()\n",
    "query = 'SELECT * FROM test_results;'\n",
    "cursor.execute(query)\n",
    "cursor.fetchall()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {},
   "outputs": [],
   "source": [
    "conn.close()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[]"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "cursor = db.connection.cursor()\n",
    "query = 'SELECT * FROM test_results;'\n",
    "cursor.execute(query)\n",
    "cursor.fetchall()"
   "metadata": {},
   "outputs": [],
   "source": [
    "db.insert_patient('822825', 29, 'f')\n",
    "db.insert_patient('16318', 42, 'm')\n",
    "db.insert_patient('440673', 67, 'f')"
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[('822825', 29, 'f', '2024-01-01 06:12:00', 68.58),\n",
       " ('822825', 29, 'f', '2024-01-09 10:48:00', 70.58),\n",
       " ('822825', 29, 'f', '2024-01-09 14:20:00', 64.15),\n",
       " ('822825', 29, 'f', '2024-01-10 17:29:00', 48.39),\n",
       " ('822825', 29, 'f', '2024-01-17 06:27:00', 58.01),\n",
       " ('822825', 29, 'f', '2024-01-23 17:55:00', 85.93)]"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "db.get_patient_history('822825')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[('16318', 42, 'm', '16318', '2024-01-01 09:47:00', 64.44),\n",
       " ('16318', 42, 'm', '16318', '2024-01-04 16:35:00', 1070423.72)]"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "db.get_patient_history('16318')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[('440673', 67, 'f', '440673', '2024-01-01 10:47:00', 69.18),\n",
       " ('440673', 67, 'f', '440673', '2024-01-01 12:43:00', 83.32),\n",
       " ('440673', 67, 'f', '440673', '2024-01-05 13:04:00', 84.87),\n",
       " ('440673', 67, 'f', '440673', '2024-01-05 15:47:00', 76.07)]"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "db.get_patient_history('440673')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [],
   "source": [
    "db.discharge_patient('16318')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# db.persist_db()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "db.close()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Load the datasets\n",
    "aki_df = pd.read_csv(\"aki.csv\")\n",
    "aki_pred_df = pd.read_csv(\"aki_predicted.csv\")\n",
    "\n",
    "# Assuming 'mrn' and 'date' are the column names in both CSV files\n",
    "# Convert 'date' to datetime for accurate comparison\n",
    "aki_df[\"date\"] = pd.to_datetime(aki_df[\"date\"])\n",
    "aki_pred_df[\"date\"] = pd.to_datetime(aki_pred_df[\"date\"])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Merge the datasets to find true positives\n",
    "tp_df = pd.merge(aki_df, aki_pred_df, how=\"inner\", on=[\"mrn\", \"date\"])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Count true positives, false positives, and false negatives\n",
    "tp = len(tp_df)\n",
    "fp = len(aki_pred_df) - tp\n",
    "fn = len(aki_df) - tp"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Precision: 0.9819587628865979\n",
      "Recall: 1.0\n",
      "F3 Score: 0.9981660990306522\n"
     ]
    }
   ],
   "source": [
    "# Calculate precision, recall, and F3 score\n",
    "precision = tp / (tp + fp) if tp + fp else 0\n",
    "recall = tp / (tp + fn) if tp + fn else 0\n",
    "beta_squared = 3**2\n",
    "f3_score = (\n",
    "    (1 + beta_squared) * (precision * recall) / ((beta_squared * precision) + recall)\n",
    "    if (precision + recall)\n",
    "    else 0\n",
    ")\n",
    "\n",
    "print(f\"Precision: {precision}\")\n",
    "print(f\"Recall: {recall}\")\n",
    "print(f\"F3 Score: {f3_score}\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "venv",
   "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.8.17"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}