a b/examples/cinc17/notebooks/cinc17_eval.ipynb
1
{
2
 "cells": [
3
  {
4
   "cell_type": "code",
5
   "execution_count": 1,
6
   "metadata": {},
7
   "outputs": [
8
    {
9
     "name": "stderr",
10
     "output_type": "stream",
11
     "text": [
12
      "Using TensorFlow backend.\n"
13
     ]
14
    }
15
   ],
16
   "source": [
17
    "import collections\n",
18
    "import json\n",
19
    "import keras\n",
20
    "import numpy as np\n",
21
    "import os\n",
22
    "import sys\n",
23
    "sys.path.append(\"../../../ecg\")\n",
24
    "import scipy.stats as sst\n",
25
    "\n",
26
    "import util\n",
27
    "import load"
28
   ]
29
  },
30
  {
31
   "cell_type": "code",
32
   "execution_count": 2,
33
   "metadata": {},
34
   "outputs": [
35
    {
36
     "name": "stderr",
37
     "output_type": "stream",
38
     "text": [
39
      "100%|██████████| 852/852 [00:00<00:00, 931.25it/s]\n"
40
     ]
41
    }
42
   ],
43
   "source": [
44
    "model_path = \"/deep/group/awni/ecg_models/default/1528249597-44/0.412-0.870-015-0.309-0.892.hdf5\"\n",
45
    "data_path = \"../dev.json\"\n",
46
    "\n",
47
    "data = load.load_dataset(data_path)\n",
48
    "preproc = util.load(os.path.dirname(model_path))\n",
49
    "model = keras.models.load_model(model_path)"
50
   ]
51
  },
52
  {
53
   "cell_type": "code",
54
   "execution_count": 31,
55
   "metadata": {},
56
   "outputs": [],
57
   "source": [
58
    "data_path = \"../train.json\"\n",
59
    "with open(\"../train.json\", 'r') as fid:\n",
60
    "    train_labels = [json.loads(l)['labels'] for l in fid]\n",
61
    "counts = collections.Counter(preproc.class_to_int[l[0]] for l in train_labels)\n",
62
    "counts = sorted(counts.most_common(), key=lambda x: x[0])\n",
63
    "counts = zip(*counts)[1]\n",
64
    "smooth = 500\n",
65
    "counts = np.array(counts)[None, None, :]\n",
66
    "total = np.sum(counts) + counts.shape[1]\n",
67
    "prior = (counts + smooth) / float(total)"
68
   ]
69
  },
70
  {
71
   "cell_type": "code",
72
   "execution_count": 3,
73
   "metadata": {},
74
   "outputs": [],
75
   "source": [
76
    "probs = []\n",
77
    "labels = []\n",
78
    "for x, y  in zip(*data):\n",
79
    "    x, y = preproc.process([x], [y])\n",
80
    "    probs.append(model.predict(x))\n",
81
    "    labels.append(y)"
82
   ]
83
  },
84
  {
85
   "cell_type": "code",
86
   "execution_count": 38,
87
   "metadata": {},
88
   "outputs": [],
89
   "source": [
90
    "preds = []\n",
91
    "ground_truth = []\n",
92
    "for p, g in zip(probs, labels):\n",
93
    "    preds.append(sst.mode(np.argmax(p / prior, axis=2).squeeze())[0][0])\n",
94
    "    ground_truth.append(sst.mode(np.argmax(g, axis=2).squeeze())[0][0])"
95
   ]
96
  },
97
  {
98
   "cell_type": "code",
99
   "execution_count": 39,
100
   "metadata": {},
101
   "outputs": [
102
    {
103
     "name": "stdout",
104
     "output_type": "stream",
105
     "text": [
106
      "             precision    recall  f1-score   support\n",
107
      "\n",
108
      "          A      0.859     0.912     0.885        80\n",
109
      "          N      0.914     0.923     0.919       508\n",
110
      "          O      0.803     0.785     0.794       233\n",
111
      "          ~      0.731     0.613     0.667        31\n",
112
      "\n",
113
      "avg / total      0.872     0.873     0.872       852\n",
114
      "\n",
115
      "CINC Average 0.865827\n"
116
     ]
117
    }
118
   ],
119
   "source": [
120
    "import sklearn.metrics as skm\n",
121
    "report = skm.classification_report(\n",
122
    "            ground_truth, preds,\n",
123
    "            target_names=preproc.classes,\n",
124
    "            digits=3)\n",
125
    "scores = skm.precision_recall_fscore_support(\n",
126
    "                    ground_truth,\n",
127
    "                    preds,\n",
128
    "                    average=None)\n",
129
    "print(report)\n",
130
    "print \"CINC Average {:3f}\".format(np.mean(scores[2][:3]))"
131
   ]
132
  },
133
  {
134
   "cell_type": "code",
135
   "execution_count": null,
136
   "metadata": {},
137
   "outputs": [],
138
   "source": []
139
  }
140
 ],
141
 "metadata": {
142
  "kernelspec": {
143
   "display_name": "Python 2",
144
   "language": "python",
145
   "name": "python2"
146
  },
147
  "language_info": {
148
   "codemirror_mode": {
149
    "name": "ipython",
150
    "version": 2
151
   },
152
   "file_extension": ".py",
153
   "mimetype": "text/x-python",
154
   "name": "python",
155
   "nbconvert_exporter": "python",
156
   "pygments_lexer": "ipython2",
157
   "version": "2.7.12"
158
  }
159
 },
160
 "nbformat": 4,
161
 "nbformat_minor": 2
162
}