Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
Binary file removed COMPOSER_code.zip
Binary file not shown.
142 changes: 142 additions & 0 deletions demo_code/compute_snoozed_auc.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
# Copyright 2021
# Supreeth P. Shashikumar <spshashikumar@health.ucsd.edu>
# Shamim Nemati <snemati@health.ucsd.edu>
# Revision: 1.1 Date: 08/03/2021

import numpy as np

def compute_confusion_matrix_per_patient(labels, predictions, threshold, seqLength, includeMaskPerPatient, false_positive_prediction_horizon = 44, snooze_length = 6):
# Fill-in confusion matrix.
tp = 0
fp = 0
fn = 0
tn = 0

tp_modified = 0
fp_modified = 0

snooze = 0

for t in range(seqLength):
# Only check labels and predictions when not snoozed.
if snooze == 0:
if includeMaskPerPatient[t]==1:
label = labels[t] == 1
prediction = predictions[t] >= threshold

# Is there a positive label within the horizon?
label_horizon = np.any(labels[t:min(seqLength, t+false_positive_prediction_horizon+1)])

if label_horizon and prediction:
tp += 1
elif not label_horizon and prediction:
fp += 1
elif label and not prediction:
fn += 1
elif not label and not prediction:
tn += 1

if prediction:
snooze = snooze_length

else:
snooze -= 1

return tp, fp, fn, tn


def compute_snoozed_auc(labels, predictions, seqLength, includeMaskPerPatient, false_positive_prediction_horizon=44, snooze_length = 6, num_thresholds = 500):
# Check input for errors.
if len(labels) != len(predictions):
raise Exception('Numbers of labels and predictions must be equal.')
if any(len(X) != len(Y) for X, Y in zip(labels, predictions)):
raise Exception('Numbers of labels and predictions must be equal for all samples.')
if any(not (x == 0 or x == 1) for X in labels for x in X):
raise Exception('Labels must be binary.')
if any(not (0 <= y <= 1) for Y in predictions for y in Y):
raise Exception('Predictions must be between 0 and 1, inclusive.')

# Find prediction thresholds.
#num_cohorts = len(labels)
sorted_predictions = [np.unique(Y) for Y in predictions]

if not num_thresholds:
thresholds = np.unique(np.concatenate(sorted_predictions))[::-1]
else:
percentiles = np.linspace(0, 100, num_thresholds)
thresholds = np.unique(np.percentile(np.concatenate(sorted_predictions), percentiles, interpolation='nearest'))[::-1]

if thresholds[0] != 1:
thresholds = np.insert(thresholds, 0, 1)
if thresholds[-1] == 0:
thresholds = thresholds[:-1]
num_thresholds = len(thresholds)

num_patients = labels.shape[0]

# Populate confusion matrix for each prediction threshold.
sample_tp = np.zeros(num_patients)
sample_fp = np.zeros(num_patients)
sample_fn = np.zeros(num_patients)
sample_tn = np.zeros(num_patients)
sample_indices = -np.ones(num_patients, dtype=np.int)

tp = np.zeros(num_thresholds)
fp = np.zeros(num_thresholds)
fn = np.zeros(num_thresholds)
tn = np.zeros(num_thresholds)

for i in range(num_thresholds):
for j in range(num_patients):
k = np.searchsorted(sorted_predictions[j], thresholds[i])
if k != sample_indices[j]:
a, b, c, d = compute_confusion_matrix_per_patient(labels[j], predictions[j], thresholds[i], seqLength[j], includeMaskPerPatient[j], false_positive_prediction_horizon, snooze_length)
sample_tp[j] = a
sample_fp[j] = b
sample_fn[j] = c
sample_tn[j] = d
sample_indices[j] = k

tp[i] += sample_tp[j]
fp[i] += sample_fp[j]
fn[i] += sample_fn[j]
tn[i] += sample_tn[j]

tpr = np.zeros(num_thresholds)
tnr = np.zeros(num_thresholds)
ppv = np.zeros(num_thresholds)
npv = np.zeros(num_thresholds)

for i in range(num_thresholds):
if tp[i] + fn[i]:
tpr[i] = tp[i] / (tp[i] + fn[i])
else:
tpr[i] = 1
if fp[i] + tn[i]:
tnr[i] = tn[i] / (fp[i] + tn[i])
else:
tpr[i] = 0
if tp[i] + fp[i]:
ppv[i] = tp[i] / (tp[i] + fp[i])
else:
ppv[i] = 1

if fn[i] + tn[i]:
npv[i] = tn[i] / (fn[i] + tn[i])
else:
npv[i] = 0

# Compute AUROC as the area under a piecewise linear function of TPR
# (x-axis) and FPR (y-axis) and AUPRC as the area under a piecewise linear
# function of recall (x-axis) and precision (y-axis).
auroc = 0
auprc = 0
auprc_modified = 0
for i in range(num_thresholds - 1):
auroc += 0.5 * (tpr[i + 1] - tpr[i]) * (tnr[i + 1] + tnr[i])
auprc += (tpr[i + 1] - tpr[i]) * (ppv[i + 1])

# for i in range(num_thresholds):
# print(i, thresholds[i], tp[i], fp[i], fn[i], tn[i])

return auroc, auprc, tpr, tnr, fp, ppv, thresholds, npv
20 changes: 20 additions & 0 deletions demo_code/demo_data/p00018.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
3.524158596992492676e-01,0,0
2.373938113451004028e-01,0,0
1.946674138307571411e-01,1,0
3.095490932464599609e-01,0,0
2.915228009223937988e-01,0,0
2.742238640785217285e-01,0,0
1.463383585214614868e-01,1,0
1.215671002864837646e-01,1,0
1.064075678586959839e-01,1,0
9.595078974962234497e-02,1,0
8.819054067134857178e-02,1,0
6.943967193365097046e-02,1,0
7.014473527669906616e-02,1,0
7.069533318281173706e-02,1,0
7.114680856466293335e-02,1,0
1.151653826236724854e-01,1,0
1.148346960544586182e-01,1,0
1.156225427985191345e-01,1,0
1.163050234317779541e-01,1,0
1.481731981039047241e-01,1,0
21 changes: 21 additions & 0 deletions demo_code/demo_data/p00056.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
8.673850446939468384e-02,1,0
7.706475257873535156e-02,1,0
4.691105708479881287e-02,1,0
1.722965985536575317e-01,0,0
1.190305575728416443e-01,1,0
6.667682528495788574e-02,1,0
4.770652949810028076e-02,1,0
7.717213034629821777e-02,1,0
7.408756017684936523e-02,1,0
1.183828860521316528e-01,1,0
5.475239828228950500e-02,1,0
2.908693067729473114e-02,1,0
2.977270446717739105e-02,1,0
3.379622846841812134e-02,1,0
2.765891142189502716e-02,1,0
3.352526575326919556e-02,1,0
2.683576196432113647e-02,1,0
2.441511675715446472e-02,1,0
2.447645366191864014e-02,1,0
3.150004893541336060e-02,1,0
3.206261992454528809e-02,1,0
24 changes: 24 additions & 0 deletions demo_code/demo_data/p00062.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
2.327396124601364136e-01,0,0
1.799809336662292480e-01,0,0
1.609969884157180786e-01,0,0
1.109594926238059998e-01,0,0
9.771641343832015991e-02,0,0
1.133971586823463440e-01,0,0
1.335569769144058228e-01,0,0
1.050397530198097229e-01,1,0
1.781912744045257568e-01,0,0
3.535856008529663086e-01,0,0
1.905256658792495728e-01,1,0
1.901751309633255005e-01,1,0
2.039649486541748047e-01,1,0
3.655618429183959961e-01,0,0
1.842548698186874390e-01,1,0
3.691251873970031738e-01,0,0
2.257111072540283203e-01,1,0
3.855168223381042480e-01,0,0
7.639527916908264160e-01,1,0
3.671570420265197754e-01,0,0
3.083380460739135742e-01,0,0
3.002890348434448242e-01,0,0
4.024444222450256348e-01,0,0
3.837247490882873535e-01,0,0
26 changes: 26 additions & 0 deletions demo_code/demo_data/p00090.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
2.488320022821426392e-01,0,0
1.979351341724395752e-01,1,0
1.607611626386642456e-01,1,0
1.380303651094436646e-01,1,0
1.223617047071456909e-01,1,0
1.107598841190338135e-01,1,0
7.814987748861312866e-02,1,0
7.159715890884399414e-02,1,0
5.468975380063056946e-02,1,0
5.159991607069969177e-02,1,0
4.954269900918006897e-02,1,0
4.854648187756538391e-02,1,0
4.865250363945960999e-02,1,0
4.899834841489791870e-02,1,0
5.747842416167259216e-02,1,0
5.782412737607955933e-02,1,0
6.699778139591217041e-02,1,0
6.522141396999359131e-02,1,0
6.467941403388977051e-02,1,0
6.444168835878372192e-02,1,0
6.435013562440872192e-02,1,0
6.805702298879623413e-02,1,0
6.775090098381042480e-02,1,0
6.776337325572967529e-02,1,0
6.632813811302185059e-02,1,0
5.466015264391899109e-02,1,0
21 changes: 21 additions & 0 deletions demo_code/demo_data/p00101.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
6.220677495002746582e-01,1,0
5.073778629302978516e-01,1,0
5.705167651176452637e-01,1,0
4.508942365646362305e-01,1,0
3.859788179397583008e-01,1,0
3.305697739124298096e-01,0,0
2.618824243545532227e-01,0,0
6.680250763893127441e-01,1,0
5.181686282157897949e-01,1,0
3.212387263774871826e-01,0,0
3.445956110954284668e-01,0,0
4.637837111949920654e-01,1,0
1.282385736703872681e-01,1,0
3.278776705265045166e-01,0,0
4.151531159877777100e-01,1,0
3.990756273269653320e-01,1,0
4.129233062267303467e-01,1,0
4.006917178630828857e-01,1,0
1.480282992124557495e-01,1,0
1.325468420982360840e-01,1,0
2.799977362155914307e-01,0,0
38 changes: 38 additions & 0 deletions demo_code/demo_data/p00104.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
1.961058080196380615e-01,0,0
1.987523287534713745e-01,0,0
2.982433438301086426e-01,0,0
2.951050996780395508e-01,0,0
2.228471338748931885e-01,0,0
4.143873453140258789e-01,0,0
2.285730093717575073e-01,0,0
3.676817417144775391e-01,0,0
2.382458299398422241e-01,0,0
2.074865549802780151e-01,0,0
2.154600173234939575e-01,0,0
2.412652522325515747e-01,0,0
2.622527778148651123e-01,0,0
2.347321957349777222e-01,1,0
5.215241909027099609e-01,1,0
3.982219398021697998e-01,0,0
1.449419856071472168e-01,1,0
3.473561704158782959e-01,0,0
1.706304848194122314e-01,1,0
3.519901037216186523e-01,0,0
2.256698608398437500e-01,0,0
1.722945570945739746e-01,1,0
2.874557077884674072e-01,0,0
4.335812330245971680e-01,1,0
3.675619959831237793e-01,0,0
3.314910829067230225e-01,0,0
4.179086685180664062e-01,1,0
6.239072680473327637e-01,1,0
6.107838749885559082e-01,1,0
3.462873697280883789e-01,0,0
3.909673392772674561e-01,0,0
2.948751449584960938e-01,0,0
6.219827532768249512e-01,1,0
4.987363815307617188e-01,1,0
6.992628574371337891e-01,1,0
6.030540466308593750e-01,1,0
6.890112161636352539e-01,1,0
4.800139367580413818e-01,1,0
87 changes: 87 additions & 0 deletions demo_code/demo_data/p00111.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
8.455271273851394653e-02,0,0
4.950173199176788330e-01,1,0
4.740326702594757080e-01,0,0
6.111966371536254883e-01,1,0
4.562445580959320068e-01,0,0
2.932156920433044434e-01,0,0
2.848603129386901855e-01,0,0
1.630455851554870605e-01,0,0
2.662973403930664062e-01,0,0
3.305304348468780518e-01,0,0
3.301716446876525879e-01,0,0
2.828376293182373047e-01,0,0
2.870158851146697998e-01,0,0
2.684437632560729980e-01,0,0
2.781126797199249268e-01,0,0
3.532216846942901611e-01,0,0
3.163609504699707031e-01,0,0
3.460981547832489014e-01,0,0
2.954213917255401611e-01,0,0
5.443381071090698242e-01,1,0
5.360348224639892578e-01,1,0
4.693817496299743652e-01,1,0
6.658125519752502441e-01,1,0
6.294396519660949707e-01,1,0
7.849550843238830566e-01,1,0
8.144682645797729492e-01,1,0
7.429504394531250000e-01,1,0
6.779129505157470703e-01,1,0
5.961812734603881836e-01,1,0
7.158362865447998047e-01,1,0
6.835216283798217773e-01,1,0
6.447662115097045898e-01,1,0
6.715291142463684082e-01,1,0
4.188896715641021729e-01,0,0
4.550487995147705078e-01,1,0
5.948814153671264648e-01,1,0
6.547959446907043457e-01,1,0
5.404837727546691895e-01,1,0
6.065972447395324707e-01,1,0
7.805557250976562500e-01,1,0
8.571919202804565430e-01,1,0
6.618452668190002441e-01,1,0
7.575771212577819824e-01,1,0
6.720529794692993164e-01,1,0
5.791990756988525391e-01,1,0
5.727471113204956055e-01,1,0
7.733771204948425293e-01,1,0
6.616895794868469238e-01,1,0
6.000316739082336426e-01,1,0
6.993266940116882324e-01,1,0
6.265998482704162598e-01,1,0
7.655912637710571289e-01,1,0
6.127461791038513184e-01,1,0
5.661484599113464355e-01,1,0
6.401023864746093750e-01,1,0
5.669670701026916504e-01,1,0
6.258417367935180664e-01,1,0
6.392814517021179199e-01,1,0
6.246948242187500000e-01,1,0
6.919012665748596191e-01,1,0
5.874356031417846680e-01,1,0
6.200698614120483398e-01,1,0
5.449547171592712402e-01,1,0
7.387002110481262207e-01,1,0
7.545434236526489258e-01,1,0
7.274111509323120117e-01,1,0
6.821124553680419922e-01,1,0
6.572970151901245117e-01,1,0
6.780682206153869629e-01,1,0
7.428504228591918945e-01,1,0
7.313720583915710449e-01,1,0
7.665268778800964355e-01,1,0
7.065571546554565430e-01,1,0
6.811770200729370117e-01,1,0
7.090263962745666504e-01,1,0
7.107076048851013184e-01,1,0
6.129837632179260254e-01,1,0
7.160031795501708984e-01,1,0
5.195250511169433594e-01,1,0
4.911837577819824219e-01,1,0
6.014863848686218262e-01,1,0
5.876170992851257324e-01,1,0
6.298850178718566895e-01,1,0
7.663446664810180664e-01,1,0
7.325693964958190918e-01,1,0
7.227794528007507324e-01,1,0
6.508201956748962402e-01,1,0
Loading