{ "cells": [ { "cell_type": "markdown", "id": "85377d78", "metadata": {}, "source": [ "# Alarm data" ] }, { "cell_type": "code", "execution_count": 1, "id": "cb30bfe1-f462-4aad-9660-c120d5904da5", "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "/usr/local/Caskroom/miniconda/base/envs/cstrees/lib/python3.11/site-packages/tqdm/auto.py:21: TqdmWarning: IProgress not found. Please update jupyter and ipywidgets. See https://ipywidgets.readthedocs.io/en/stable/user_install.html\n", " from .autonotebook import tqdm as notebook_tqdm\n" ] } ], "source": [ "import random\n", "import time\n", "\n", "from causallearn.search.ConstraintBased.PC import pc\n", "from causallearn.utils.cit import chisq\n", "\n", "import pandas as pd\n", "import cslearn.scoring as sc\n", "import cslearn.learning as ctl\n", "import cslearn.ldag as ldag\n", "import numpy as np\n", "\n", "%load_ext autoreload\n", "%autoreload 2" ] }, { "cell_type": "markdown", "id": "830d1445", "metadata": {}, "source": [ "## Read data" ] }, { "cell_type": "code", "execution_count": 2, "id": "450c930d-4cbd-4958-b4c9-b6a11d130a27", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
CVPPCWPHISTTPRBPCOHRBPHREKHRSAPAP...ERLOHRERCASHNTPVSACO2VALVVLNGVTUBVMCH
0NORMALNORMALFalseLOWNORMALHIGHHIGHHIGHHIGHNORMAL...FalseHIGHFalseNORMALNORMALNORMALHIGHLOWZERONORMAL
1NORMALNORMALFalseNORMALLOWLOWHIGHHIGHHIGHNORMAL...FalseHIGHFalseNORMALLOWLOWZEROZEROLOWNORMAL
2NORMALHIGHFalseNORMALNORMALHIGHHIGHHIGHHIGHNORMAL...FalseHIGHFalseNORMALLOWLOWZEROZEROLOWNORMAL
3NORMALNORMALFalseLOWLOWHIGHHIGHHIGHHIGHNORMAL...FalseHIGHFalseNORMALNORMALLOWZEROZEROLOWNORMAL
4NORMALNORMALFalseLOWLOWNORMALHIGHHIGHHIGHNORMAL...FalseHIGHFalseNORMALLOWLOWZEROZEROLOWNORMAL
\n", "

5 rows × 37 columns

\n", "
" ], "text/plain": [ " CVP PCWP HIST TPR BP CO HRBP HREK HRSA PAP \\\n", "0 NORMAL NORMAL False LOW NORMAL HIGH HIGH HIGH HIGH NORMAL \n", "1 NORMAL NORMAL False NORMAL LOW LOW HIGH HIGH HIGH NORMAL \n", "2 NORMAL HIGH False NORMAL NORMAL HIGH HIGH HIGH HIGH NORMAL \n", "3 NORMAL NORMAL False LOW LOW HIGH HIGH HIGH HIGH NORMAL \n", "4 NORMAL NORMAL False LOW LOW NORMAL HIGH HIGH HIGH NORMAL \n", "\n", " ... ERLO HR ERCA SHNT PVS ACO2 VALV VLNG VTUB VMCH \n", "0 ... False HIGH False NORMAL NORMAL NORMAL HIGH LOW ZERO NORMAL \n", "1 ... False HIGH False NORMAL LOW LOW ZERO ZERO LOW NORMAL \n", "2 ... False HIGH False NORMAL LOW LOW ZERO ZERO LOW NORMAL \n", "3 ... False HIGH False NORMAL NORMAL LOW ZERO ZERO LOW NORMAL \n", "4 ... False HIGH False NORMAL LOW LOW ZERO ZERO LOW NORMAL \n", "\n", "[5 rows x 37 columns]" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "alarmdf = pd.read_csv('../data/alarm_data.csv')\n", "alarmdf = alarmdf.drop(columns = ['Unnamed: 0'],axis=1)\n", "alarmdf.head()" ] }, { "cell_type": "markdown", "id": "9b0566ca", "metadata": {}, "source": [ "Convert all the outcomes into numeric values." ] }, { "cell_type": "code", "execution_count": 3, "id": "28703f41-03bd-4c40-8419-2d8332e2be10", "metadata": {}, "outputs": [], "source": [ "alarmnp = alarmdf.to_numpy()\n", "alarmnp[:,0] = [0 for i in range(20000)]\n", "\n", "def convertToNumeric(df):\n", " npdf = df.to_numpy()\n", " vars = list(df.columns)\n", " n = len(df)\n", " for v in vars:\n", " j = vars.index(v)\n", " states = list(alarmdf[v].drop_duplicates().to_numpy())\n", " for i in range(n):\n", " npdf[i,j] = states.index(alarmdf[v].iloc[i])\n", " numdf = pd.DataFrame(npdf)\n", " return numdf\n", "\n", "numalarmdf = convertToNumeric(alarmdf)\n", "\n", "cards_row = {0 : 3, 1 : 3, 2 : 2, 3 : 3, 4 : 3, 5 : 3, 6 : 3, 7 : 3, 8 : 3, 9 : 3, 10 : 3, 11 : 2, 12 : 4, 13 : 4, 14 : 4, 15 : 3, 16 : 2, 17 : 2, 18 : 2, 19 : 2, 20 : 2, 21 : 3, 22 : 2, 23 : 2, 24 : 3, 25 : 3, 26 : 2, 27 : 2, 28 : 3, 29 : 2, 30 : 2, 31 : 3, 32 : 3, 33 : 4, 34 : 4, 35 : 4, 36 : 4}\n", "numalarmdf.loc[len(numalarmdf)] = cards_row\n", "target_row = 20000\n", "# Move target row to first element of list.\n", "idx = [target_row] + [i for i in range(len(numalarmdf)) if i != target_row]\n", "numalarmdf.iloc[idx]\n", "numalarmdf_cards = numalarmdf.iloc[idx].reset_index(drop=True)\n", "numalarmdf_cards.columns = alarmdf.columns" ] }, { "cell_type": "markdown", "id": "d072505d", "metadata": {}, "source": [ "## MCMC sampling" ] }, { "cell_type": "markdown", "id": "46d9fa1d", "metadata": {}, "source": [ "We run the PC algorithm to estimate a CPDAG that is used to restrict the possible context variables." ] }, { "cell_type": "code", "execution_count": 4, "id": "ccecf833-6113-47a4-89eb-e6b68104f028", "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "Depth=4, working on node 36: 100%|██████████| 37/37 [00:00<00:00, 1192.31it/s]\n", "Context score tables: 100%|██████████| 37/37 [00:00<00:00, 49.17it/s]\n", "Creating #stagings tables: 100%|██████████| 37/37 [00:00<00:00, 98.00it/s] \n", "Order score tables: 100%|██████████| 37/37 [00:02<00:00, 16.84it/s]\n", "Gibbs order sampler: 100%|██████████| 5000/5000 [00:02<00:00, 2248.31it/s]" ] }, { "name": "stdout", "output_type": "stream", "text": [ "Computation time in seconds: 8.266073226928711\n" ] }, { "name": "stderr", "output_type": "stream", "text": [ "\n" ] } ], "source": [ "np.random.seed(1)\n", "random.seed(1)\n", "start = time.time()\n", "pcgraph = pc(numalarmdf_cards[1:].values, 0.05, \"chisq\", node_names=numalarmdf_cards.columns)\n", "poss_cvars = ctl.causallearn_graph_to_posscvars(pcgraph, labels=numalarmdf_cards.columns)\n", "#print(\"Possible context variables per node:\", poss_cvars)\n", "\n", "score_table, context_scores, context_counts = sc.order_score_tables(numalarmdf_cards,\n", " max_cvars=2,\n", " alpha_tot=1.0,\n", " method=\"BDeu\",\n", " poss_cvars=poss_cvars)\n", "\n", "orders, scores = ctl.gibbs_order_sampler(5000, score_table)\n", "end = time.time()\n", "print('Computation time in seconds:', end - start)" ] }, { "cell_type": "code", "execution_count": 5, "id": "513123d3-02a1-4ebf-a960-dfb5da5639b8", "metadata": {}, "outputs": [], "source": [ "# optimal variable ordering\n", "alarmmaporder = orders[scores.index(max(scores))]\n", "#print(alarmmaporder)" ] }, { "cell_type": "code", "execution_count": 6, "id": "f990832b-2fc2-4efc-8985-cc7f601dc5e8", "metadata": {}, "outputs": [], "source": [ "# get optimal tree for ordering\n", "alarmopttree = ctl._optimal_cstree_given_order(alarmmaporder, context_scores)" ] }, { "cell_type": "markdown", "id": "1fd106fe", "metadata": {}, "source": [ "## LDAG representation" ] }, { "cell_type": "code", "execution_count": 7, "id": "f7f87352", "metadata": {}, "outputs": [], "source": [ "LDAG = alarmopttree.to_LDAG()" ] }, { "cell_type": "code", "execution_count": 8, "id": "ac232beb-90d3-4fc8-aea6-de3816f7d467", "metadata": {}, "outputs": [ { "data": { "image/svg+xml": [ "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "TPR\n", "\n", "TPR\n", "\n", "\n", "\n", "CCHL\n", "\n", "CCHL\n", "\n", "\n", "\n", "TPR->CCHL\n", "\n", "\n", "\n", "\n", "\n", "APL\n", "\n", "APL\n", "\n", "\n", "\n", "TPR->APL\n", "\n", "\n", "\n", "\n", "\n", "BP\n", "\n", "BP\n", "\n", "\n", "\n", "TPR->BP\n", "\n", "\n", "\n", "\n", "\n", "ACO2\n", "\n", "ACO2\n", "\n", "\n", "\n", "CCHL->ACO2\n", "\n", "\n", "[[0], [3]]\n", "\n", "\n", "\n", "HR\n", "\n", "HR\n", "\n", "\n", "\n", "CCHL->HR\n", "\n", "\n", "\n", "\n", "\n", "ECO2\n", "\n", "ECO2\n", "\n", "\n", "\n", "ACO2->ECO2\n", "\n", "\n", "[[2], [3]]\n", "\n", "\n", "\n", "HREK\n", "\n", "HREK\n", "\n", "\n", "\n", "HR->HREK\n", "\n", "\n", "\n", "\n", "\n", "HRSA\n", "\n", "HRSA\n", "\n", "\n", "\n", "HR->HRSA\n", "\n", "\n", "\n", "\n", "\n", "CO\n", "\n", "CO\n", "\n", "\n", "\n", "HR->CO\n", "\n", "\n", "\n", "\n", "\n", "HRBP\n", "\n", "HRBP\n", "\n", "\n", "\n", "HR->HRBP\n", "\n", "\n", "\n", "\n", "\n", "CO->BP\n", "\n", "\n", "\n", "\n", "\n", "INT\n", "\n", "INT\n", "\n", "\n", "\n", "VLNG\n", "\n", "VLNG\n", "\n", "\n", "\n", "INT->VLNG\n", "\n", "\n", "\n", "\n", "\n", "PRSS\n", "\n", "PRSS\n", "\n", "\n", "\n", "INT->PRSS\n", "\n", "\n", "\n", "\n", "\n", "VALV\n", "\n", "VALV\n", "\n", "\n", "\n", "INT->VALV\n", "\n", "\n", "\n", "\n", "\n", "SHNT\n", "\n", "SHNT\n", "\n", "\n", "\n", "INT->SHNT\n", "\n", "\n", "[[1]]\n", "\n", "\n", "\n", "MINV\n", "\n", "MINV\n", "\n", "\n", "\n", "INT->MINV\n", "\n", "\n", "[[3]]\n", "\n", "\n", "\n", "VLNG->ECO2\n", "\n", "\n", "\n", "\n", "\n", "VLNG->VALV\n", "\n", "\n", "\n", "\n", "\n", "VLNG->MINV\n", "\n", "\n", "\n", "\n", "\n", "KINK\n", "\n", "KINK\n", "\n", "\n", "\n", "PRSS->KINK\n", "\n", "\n", "\n", "\n", "\n", "VALV->ACO2\n", "\n", "\n", "\n", "\n", "\n", "PVS\n", "\n", "PVS\n", "\n", "\n", "\n", "VALV->PVS\n", "\n", "\n", "\n", "\n", "\n", "SAO2\n", "\n", "SAO2\n", "\n", "\n", "\n", "SHNT->SAO2\n", "\n", "\n", "[[1]]\n", "\n", "\n", "\n", "PVS->SAO2\n", "\n", "\n", "\n", "\n", "\n", "SAO2->CCHL\n", "\n", "\n", "[[1]]\n", "\n", "\n", "\n", "ANES\n", "\n", "ANES\n", "\n", "\n", "\n", "DISC\n", "\n", "DISC\n", "\n", "\n", "\n", "VTUB\n", "\n", "VTUB\n", "\n", "\n", "\n", "DISC->VTUB\n", "\n", "\n", "[[2], [3]]\n", "\n", "\n", "\n", "VTUB->VLNG\n", "\n", "\n", "\n", "\n", "\n", "VTUB->PRSS\n", "\n", "\n", "\n", "\n", "\n", "PMB\n", "\n", "PMB\n", "\n", "\n", "\n", "PMB->SHNT\n", "\n", "\n", "\n", "\n", "\n", "PAP\n", "\n", "PAP\n", "\n", "\n", "\n", "PMB->PAP\n", "\n", "\n", "\n", "\n", "\n", "VMCH\n", "\n", "VMCH\n", "\n", "\n", "\n", "VMCH->VTUB\n", "\n", "\n", "\n", "\n", "\n", "MVS\n", "\n", "MVS\n", "\n", "\n", "\n", "VMCH->MVS\n", "\n", "\n", "\n", "\n", "\n", "ERCA\n", "\n", "ERCA\n", "\n", "\n", "\n", "ERCA->HREK\n", "\n", "\n", "[[2]]\n", "\n", "\n", "\n", "ERCA->HRSA\n", "\n", "\n", "[[2]]\n", "\n", "\n", "\n", "HYP\n", "\n", "HYP\n", "\n", "\n", "\n", "LVV\n", "\n", "LVV\n", "\n", "\n", "\n", "HYP->LVV\n", "\n", "\n", "[[1]]\n", "\n", "\n", "\n", "STKV\n", "\n", "STKV\n", "\n", "\n", "\n", "HYP->STKV\n", "\n", "\n", "[[1]]\n", "\n", "\n", "\n", "CVP\n", "\n", "CVP\n", "\n", "\n", "\n", "LVV->CVP\n", "\n", "\n", "\n", "\n", "\n", "PCWP\n", "\n", "PCWP\n", "\n", "\n", "\n", "LVV->PCWP\n", "\n", "\n", "\n", "\n", "\n", "STKV->CO\n", "\n", "\n", "\n", "\n", "\n", "LVF\n", "\n", "LVF\n", "\n", "\n", "\n", "LVF->LVV\n", "\n", "\n", "\n", "\n", "\n", "LVF->STKV\n", "\n", "\n", "\n", "\n", "\n", "HIST\n", "\n", "HIST\n", "\n", "\n", "\n", "LVF->HIST\n", "\n", "\n", "\n", "\n", "\n", "FIO2\n", "\n", "FIO2\n", "\n", "\n", "\n", "FIO2->PVS\n", "\n", "\n", "[[2], [3]]\n", "\n", "\n", "\n", "ERLO\n", "\n", "ERLO\n", "\n", "\n", "\n", "ERLO->HRBP\n", "\n", "\n", "\n", "\n", "\n" ], "text/plain": [ ">" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "agraph = LDAG.plot_graphviz()\n", "agraph\n", "#agraph.draw('alarm_CStree_LDAG.png')" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3 (ipykernel)", "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.11.3" } }, "nbformat": 4, "nbformat_minor": 5 }