Predicting Mortality of ICU Patients: The PhysioNet/Computing in Cardiology Challenge 2012 1.0.0

File: <base>/sources/mehmet.kayaalp_at_gmail.com/entry7/physionet2012 (12,528 bytes)
#!/usr/bin/perl
require 5.008;
use strict;
use warnings;

my %measure;
my %evaluated;
my %stat;
my @marginal;
my @icu;
my $threshold = -1.868;

&load_measures;
&load_parameters;
my ($rid, $icu) = &read_case;
&inference;

sub inference{
	my @counts;
	my @ln;
	my $N = $marginal[0] + $marginal[1];
	
	while(my($measure, $trend) = each %evaluated){
		next unless defined $measure{$measure};
		my $href = $stat{$measure};
		if(defined $href->{$trend}){
			push @counts, $href->{$trend};
		}else{
			my @values = @marginal;
			foreach my $trend (keys %$href){ #take out from marginals, significant pattern counts 
				$values[$_] -= $stat{$measure}{$trend}[$_] for (0..1);
			}
			push @counts, \@values;		
		}		
	}
	foreach my $s (0..1){
		$ln[$s] = log($marginal[$s]) - log $N;
		my $i = 1;
		foreach my $c_aref (@counts){
			$ln[$s] += log($c_aref->[$s] + 1) - log($c_aref->[0] + $c_aref->[1] + 2);
			$i++;
		}
		$ln[$s] += log($icu[$icu][$s]); $i++;
		$ln[$s] /= $i;
	}
	my $lnP_mortality = $ln[1]-log(exp($ln[0]) + exp($ln[1]));
	my $prediction = $lnP_mortality > $threshold ? 1 : 0;
	print "$rid,$prediction,", &calibrate402(exp($lnP_mortality)), "\n";
	#print "$rid,$prediction,", exp($lnP_mortality), "\n";
}
sub calibrate402{
	my $p = shift;
	if($p >= 0.173){
		$p = -155.02 * $p * $p + 72.053 * $p - 7.3446;
	}else{
		$p = 71.367 * $p * $p - 12.394 * $p + 0.4959;
	}
	return 0.001 if $p < 0.001;
	return 0.70 if $p > 0.70;
	return $p;
}
sub load_parameters{
	open my $ifh, $0 or die "Cannot read $0!\n$!";
	while(<$ifh>){
		last if /^#BeginParameters/;
	}
	while(<$ifh>){
		last if /^#EndParameters/;
		chomp;
		s/^#//;
		my ($head, @tail) = split /\t/, $_;
		if($head eq 'marginals'){
			@marginal = @tail;
		}else{
			my $trend = shift @tail;
			$stat{$head}{$trend} = \@tail;
		}
	}
	close $ifh;
	
	$icu[1][1] = 0.140;
	$icu[2][1] = 0.049;
	$icu[3][1] = 0.186;
	$icu[4][1] = 0.145;
	$icu[$_][0] = 1 - $icu[$_][1] for (1..4);
}
sub read_case{
	my %static;
	$static{$_} = 1 for ('Age', 'BMI', 'Gender', 'Height','Weight','ICUType');
	my %observed;
	my $rid;
	my $icu;
	while(<STDIN>){
		next unless /^\d\d:/;
		chomp;
		my ($time,$measure,$value) = split /\,/, $_;
		$rid = $value and next if $measure eq 'RecordID';
		$icu = $value and next if $measure eq 'ICUType';
		next unless (defined $measure{$measure} or $measure =~ /^.eight$/);
		next unless $value =~ /\d/;
		next if $value == -1;
		$time =~ s/://;
		$time += 0; #make it numeric
		next if $time > 4800;
		my $aref;
		if($static{$measure}){
			$observed{$measure} = [0,0] unless defined $observed{$measure};
			$aref = $observed{$measure};
		}else{
			my $day = $time < 2400 ? 1 : 2;
			$observed{$measure}{$day} = [0,0] unless defined $observed{$measure}{$day};
			$aref = $observed{$measure}{$day};
		}
		$aref->[0] += $value;
		$aref->[1] ++;
	}
	#my %evaluated;
	if(defined $observed{Height} and defined $observed{Weight}){
		my $height = $observed{Height}[0] / (100* $observed{Height}[1]);
		my $weight = $observed{Weight}[0] / ($observed{Weight}[1]);
		$observed{BMI} = [$weight / ($height * $height),1];
	}
	foreach my $measure (keys %measure){
		my $aref;
		if($static{$measure}){
			$aref = $observed{$measure};
			if($aref){
				$evaluated{$measure} = &evaluate($measure, $aref);
			}else{
				$evaluated{$measure} .= 'U';
			}
		}else{
			for my $day (1..2){
				$aref = $observed{$measure}{$day};
				if($aref){
					$evaluated{$measure} .= &evaluate($measure, $aref);
				}else{
					$evaluated{$measure} .= 'U';
				}
			}
		}
	}
	return $rid, $icu;
}
sub evaluate{
	my $measure = shift;
	my $aref = shift;
	
	my $mean = $aref->[0] / $aref->[1];
	if($mean <= $measure{$measure}{low}){
		return 'L';
	}elsif($mean > $measure{$measure}{high}){
		return 'H';
	}else{
		return 'N';
	}
}
sub load_measures{
	open my $ifh, $0 or die "Cannot read $0!\n$!";
	while(<$ifh>){
		last if /^#BeginRanges/;
	}
	while(<$ifh>){
		last if /^#EndRanges/;
		chomp;
		s/^#//;
		my ($measure, $low, $high) = split /\s*\t\s*/, $_;
		#$measure =~ s/\s*:$//;
		$measure{$measure}{low} = $low;
		$measure{$measure}{high} = $high;
	}
	close $ifh;
}

#BeginRanges
#ALP            	 96.80	 96.80	0.26	[-,+]         	[172/1122, 150/568]
#ALT            	 52.00	512.60	0.23	[-,+,+]       	[200/1156, 100/475, 28/90]
#AST            	 77.50	 77.50	0.24	[-,+]         	[193/1166, 136/559]
#Age            	 81.20	 99.60	0.24	[-,+,-]       	[396/3288, 157/655, 1/57]
#Albumin        	  2.76	  2.76	0.25	[+,-]         	[157/619, 146/996]
#BMI            	 20.55	 20.55	0.16	[+,-,U+]      	[28/141, 233/1964, 293/1895]
#BUN            	 42.40	 42.40	0.30	[-,+]         	[383/3382, 167/554]
#Bilirubin      	  1.00	  1.00	0.27	[-,+]         	[176/1156, 151/562]
#Cholesterol    	202.30	218.90	0.08	[+,-,+]       	[42/257, 0/24, 4/24]
#Creatinine     	  1.74	  1.74	0.27	[-,+]         	[364/3257, 186/679]
#DiasABP        	 54.53	 54.53	0.17	[+,-]         	[143/842, 266/1957]
#FiO2           	  0.34	  0.60	0.20	[+,-,+]       	[8/38, 319/2106, 117/573]
#GCS            	  8.28	  8.28	0.32	[+,-]         	[225/703, 324/3233]
#Glucose        	167.80	167.80	0.24	[-,+]         	[403/3284, 144/603]
#HCO3           	 18.62	 30.17	0.30	[+,-,+]       	[134/361, 380/3361, 35/202]
#HCT            	 28.60	 30.02	0.17	[-,+,-]       	[159/1161, 97/585, 294/2190]
#HR             	 59.90	104.15	0.22	[+,-,+]       	[15/78, 423/3367, 111/492]
#K              	  3.40	  4.80	0.19	[+,-,+]       	[32/175, 443/3371, 72/358]
#Lactate        	  2.65	  2.65	0.26	[-,+]         	[239/1627, 146/556]
#MAP            	 66.54	 91.09	0.19	[+,-,+]       	[46/157, 286/2155, 74/480]
#MechVent       	  0.72	  0.72	0.20	[-,+]         	[32/704, 363/1825]
#Mg             	  2.18	  2.18	0.18	[-,+]         	[381/2970, 166/927]
#NIDiasABP      	 48.04	 48.04	0.19	[+,-]         	[138/727, 349/2756]
#NIMAP          	 65.90	 65.90	0.21	[+,-]         	[135/645, 352/2836]
#NISysABP       	 99.15	 99.15	0.21	[+,-]         	[119/531, 369/2962]
#Na             	132.70	143.88	0.25	[+,-,+]       	[54/207, 406/3365, 88/353]
#PaCO2          	 32.50	 50.50	0.28	[+,-,+]       	[109/315, 321/2488, 44/220]
#PaO2           	102.50	102.50	0.21	[+,-]         	[146/695, 327/2328]
#Platelets      	 99.68	389.78	0.23	[+,-,+]       	[112/425, 406/3304, 32/203]
#RespRate       	 26.11	 26.11	0.16	[-,+,U+]      	[73/1036, 14/65, 467/2899]
#SaO2           	 96.10	 98.90	0.20	[+,-,+]       	[93/459, 154/1229, 18/104]
#SysABP         	105.18	105.18	0.22	[+,-]         	[126/566, 283/2233]
#Temp           	 36.26	 37.99	0.24	[+,-,+]       	[99/382, 416/3384, 34/170]
#TroponinI      	  2.93	  5.56	0.10	[+,-,+]       	[32/110, 4/21, 21/74]
#TroponinT      	  0.05	  2.21	0.23	[-,+,-]       	[33/201, 130/558, 19/104]
#Urine          	 64.46	 64.46	0.30	[+,-]         	[230/766, 307/3117]
#WBC            	  4.02	 18.72	0.23	[+,-,+]       	[24/95, 420/3386, 106/446]
#pH             	  7.35	  7.46	0.22	[+,-,+]       	[110/512, 309/2306, 55/222]
#EndRanges

#BeginParameters
#marginals	3446	554
#ALP	HH	167	62
#ALP	HL	30	10
#ALP	HU	190	64
#ALP	LH	18	5
#ALP	LL	246	53
#ALP	LU	542	80
#ALP	UH	35	16
#ALP	UL	140	32
#ALP	UU	2078	232
#ALT	HH	39	22
#ALT	LL	213	51
#ALT	LN	13	8
#ALT	LU	591	114
#ALT	NL	22	2
#ALT	NN	165	47
#ALT	NU	149	31
#ALT	UL	135	33
#ALT	UN	38	12
#ALT	UU	2053	226
#AST	HH	187	77
#AST	HL	52	6
#AST	HU	159	33
#AST	LH	19	13
#AST	LL	213	39
#AST	LU	586	113
#AST	UH	42	14
#AST	UL	138	34
#AST	UU	2050	225
#Age	H	56	1
#Age	L	2892	396
#Age	N	498	157
#Albumin	HH	109	16
#Albumin	HL	37	11
#Albumin	HU	567	92
#Albumin	LL	107	36
#Albumin	LU	237	85
#Albumin	UH	148	33
#Albumin	UL	97	27
#Albumin	UU	2134	251
#BMI	H	1731	233
#BMI	L	113	28
#BMI	U	1602	293
#BUN	HH	293	140
#BUN	HL	91	20
#BUN	LH	75	24
#BUN	LL	2824	345
#BUN	LU	66	13
#BUN	UL	27	4
#BUN	UU	60	4
#Bilirubin	HH	204	82
#Bilirubin	HL	25	7
#Bilirubin	HU	146	41
#Bilirubin	LH	18	9
#Bilirubin	LL	230	37
#Bilirubin	LU	573	99
#Bilirubin	UH	40	17
#Bilirubin	UL	155	35
#Bilirubin	UU	2055	227
#Cholesterol	HU	18	4
#Cholesterol	LU	183	34
#Cholesterol	NU	22	0
#Cholesterol	UL	28	8
#Cholesterol	UU	3187	508
#Creatinine	HH	405	159
#Creatinine	HL	72	13
#Creatinine	LH	83	26
#Creatinine	LL	2728	330
#Creatinine	LU	65	13
#Creatinine	UL	27	5
#Creatinine	UU	60	4
#DiasABP	HH	1225	200
#DiasABP	HL	265	34
#DiasABP	HU	136	3
#DiasABP	LH	180	48
#DiasABP	LL	403	90
#DiasABP	LU	58	5
#DiasABP	UH	88	15
#DiasABP	UL	35	14
#DiasABP	UU	1056	145
#FiO2	HH	145	57
#FiO2	HN	323	83
#FiO2	HU	174	7
#FiO2	NH	110	18
#FiO2	NL	31	7
#FiO2	NN	904	221
#FiO2	NU	425	15
#FiO2	UH	61	16
#FiO2	UN	83	15
#FiO2	UU	1173	110
#GCS	HH	2536	254
#GCS	HL	122	51
#GCS	HU	15	2
#GCS	LH	388	56
#GCS	LL	316	181
#GCS	UU	59	5
#Glucose	HH	164	52
#Glucose	HL	450	109
#Glucose	HU	25	3
#Glucose	LH	184	58
#Glucose	LL	2218	300
#Glucose	LU	157	13
#Glucose	UH	16	0
#Glucose	UL	126	12
#Glucose	UU	106	7
#HCO3	HH	102	26
#HCO3	HN	54	12
#HCO3	LL	141	91
#HCO3	LN	146	48
#HCO3	NH	72	5
#HCO3	NL	92	27
#HCO3	NN	2628	318
#HCO3	NU	78	12
#HCO3	UN	44	6
#HCO3	UU	71	5
#HCT	HH	1417	209
#HCT	HL	328	51
#HCT	HN	252	32
#HCT	HU	51	10
#HCT	LH	166	46
#HCT	LL	602	98
#HCT	LN	138	23
#HCT	NH	95	18
#HCT	NL	206	33
#HCT	NN	80	16
#HCT	UH	24	6
#HCT	UU	60	4
#HR	HH	233	78
#HR	HN	211	49
#HR	LL	40	6
#HR	LN	67	13
#HR	NH	168	34
#HR	NL	48	7
#HR	NN	2601	359
#HR	UU	58	5
#K	HH	97	26
#K	HN	245	48
#K	HU	22	2
#K	LL	58	12
#K	LN	139	24
#K	NH	117	32
#K	NL	189	39
#K	NN	2235	341
#K	NU	124	12
#K	UH	12	4
#K	UN	103	5
#K	UU	89	7
#Lactate	HH	59	56
#Lactate	HL	193	37
#Lactate	HU	224	43
#Lactate	LH	24	13
#Lactate	LL	383	86
#Lactate	LU	730	109
#Lactate	UH	22	10
#Lactate	UL	163	31
#Lactate	UU	1648	169
#MAP	HH	197	35
#MAP	HN	115	28
#MAP	HU	52	2
#MAP	LL	44	15
#MAP	LN	76	25
#MAP	LU	16	1
#MAP	NH	198	28
#MAP	NL	107	26
#MAP	NN	1336	212
#MAP	NU	122	3
#MAP	UH	36	7
#MAP	UL	11	10
#MAP	UN	75	13
#MAP	UU	1060	148
#MechVent	HH	1462	363
#MechVent	HU	568	10
#MechVent	UH	104	22
#MechVent	UU	1312	159
#Mg	HH	426	103
#Mg	HL	315	31
#Mg	HU	40	7
#Mg	LH	407	94
#Mg	LL	1867	284
#Mg	LU	138	16
#Mg	UH	34	7
#Mg	UL	123	5
#Mg	UU	96	7
#NIDiasABP	HH	1489	182
#NIDiasABP	HL	153	41
#NIDiasABP	HU	488	127
#NIDiasABP	LH	177	22
#NIDiasABP	LL	212	53
#NIDiasABP	LU	136	50
#NIDiasABP	UH	212	6
#NIDiasABP	UL	129	6
#NIDiasABP	UU	450	67
#NIMAP	HH	1508	181
#NIMAP	HL	130	38
#NIMAP	HU	492	127
#NIMAP	LH	224	30
#NIMAP	LL	163	48
#NIMAP	LU	134	51
#NIMAP	UH	249	6
#NIMAP	UL	94	6
#NIMAP	UU	452	67
#NISysABP	HH	1629	197
#NISysABP	HL	115	37
#NISysABP	HU	485	124
#NISysABP	LH	182	32
#NISysABP	LL	120	34
#NISysABP	LU	133	51
#NISysABP	UH	283	8
#NISysABP	UL	58	5
#NISysABP	UU	441	66
#Na	HH	167	59
#Na	HN	127	21
#Na	LL	83	34
#Na	LN	76	26
#Na	NH	168	41
#Na	NL	111	20
#Na	NN	2475	324
#Na	NU	93	11
#Na	UN	55	7
#Na	UU	69	6
#PaCO2	HH	84	16
#PaCO2	HN	52	21
#PaCO2	HU	42	9
#PaCO2	LL	74	50
#PaCO2	LN	74	31
#PaCO2	LU	64	18
#PaCO2	NH	49	14
#PaCO2	NL	127	42
#PaCO2	NN	1279	200
#PaCO2	NU	567	38
#PaCO2	UL	20	10
#PaCO2	UN	107	21
#PaCO2	UU	897	80
#PaO2	HH	856	192
#PaO2	HL	576	89
#PaO2	HU	499	35
#PaO2	LH	113	32
#PaO2	LL	197	64
#PaO2	LU	172	27
#PaO2	UH	90	17
#PaO2	UL	47	17
#PaO2	UU	896	81
#Platelets	HH	119	21
#Platelets	HN	77	15
#Platelets	LL	215	76
#Platelets	LN	32	15
#Platelets	NH	26	4
#Platelets	NL	193	49
#Platelets	NN	2578	337
#Platelets	NU	79	15
#Platelets	UN	46	11
#Platelets	UU	64	4
#RespRate	HH	33	6
#RespRate	HL	36	7
#RespRate	LH	23	8
#RespRate	LL	907	64
#RespRate	UU	2432	467
#SaO2	HN	22	6
#SaO2	HU	60	8
#SaO2	LL	80	20
#SaO2	LN	67	18
#SaO2	LU	118	24
#SaO2	NH	20	7
#SaO2	NL	173	26
#SaO2	NN	350	47
#SaO2	NU	398	40
#SaO2	UH	15	9
#SaO2	UL	69	24
#SaO2	UN	136	33
#SaO2	UU	1919	289
#SysABP	HH	1488	224
#SysABP	HL	146	29
#SysABP	HU	136	3
#SysABP	LH	230	48
#SysABP	LL	209	71
#SysABP	LU	58	5
#SysABP	UH	101	15
#SysABP	UL	22	14
#SysABP	UU	1056	145
#Temp	HH	68	12
#Temp	HN	110	22
#Temp	LL	110	52
#Temp	LN	313	59
#Temp	NH	134	34
#Temp	NL	145	46
#Temp	NN	2473	312
#Temp	NU	13	6
#Temp	UU	59	5
#TroponinI	HH	18	4
#TroponinI	HU	26	13
#TroponinI	LL	19	7
#TroponinI	LU	52	21
#TroponinI	UU	3298	497
#TroponinT	HH	35	14
#TroponinT	HU	39	3
#TroponinT	LL	30	11
#TroponinT	LU	142	22
#TroponinT	NN	142	46
#TroponinT	NU	227	66
#TroponinT	UL	16	2
#TroponinT	UN	18	5
#TroponinT	UU	2765	372
#Urine	HH	2236	215
#Urine	HL	445	99
#Urine	HU	35	3
#Urine	LH	255	49
#Urine	LL	344	164
#Urine	LU	19	5
#Urine	UU	100	17
#WBC	HH	190	69
#WBC	HN	228	38
#WBC	LL	43	18
#WBC	LN	41	7
#WBC	NH	103	35
#WBC	NL	34	6
#WBC	NN	2590	343
#WBC	NU	75	17
#WBC	UN	51	9
#WBC	UU	69	4
#pH	HH	33	21
#pH	HN	73	18
#pH	HU	55	8
#pH	LH	20	10
#pH	LL	216	71
#pH	LN	321	65
#pH	LU	141	20
#pH	NH	126	24
#pH	NL	83	32
#pH	NN	985	135
#pH	NU	365	34
#pH	UH	23	7
#pH	UL	26	11
#pH	UN	93	16
#pH	UU	880	80
#EndParameters