-
Notifications
You must be signed in to change notification settings - Fork 2k
Expand file tree
/
Copy pathCoxPHMojoModelTest.java
More file actions
201 lines (175 loc) · 7.74 KB
/
CoxPHMojoModelTest.java
File metadata and controls
201 lines (175 loc) · 7.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
package hex.genmodel.algos.coxph;
import org.junit.Test;
import java.nio.file.Paths;
import hex.genmodel.MojoModel;
import hex.genmodel.easy.RowData;
import hex.genmodel.easy.EasyPredictModelWrapper;
import static org.junit.Assert.*;
public class CoxPHMojoModelTest {
@Test
public void testFeatureValue() {
final double[] row = {0, 1, 2, 3, 4, 5, 6};
final CoxPHMojoModel mojo = new CoxPHMojoModel(null, null, null);
mojo._strata_len = 0;
assertEquals(1.0, mojo.featureValue(row, 1), 0);
assertEquals(4.0, mojo.featureValue(row, 4), 0);
mojo._strata_len = 2;
assertEquals(3.0, mojo.featureValue(row, 1), 0);
assertEquals(6.0, mojo.featureValue(row, 4), 0);
}
@Test
public void testForOneCategory() {
final double[] row = {0, 1, 2, 3, 4, 5, 6};
final CoxPHMojoModel mojo = new CoxPHMojoModel(null, null, null);
mojo._cat_offsets = new int[]{0, 2, 8, 11, 15};
mojo._coef = new double[]{0.1, 0.2, 0.3, 0.4, 0.5, 0.6};
mojo._strata_len = 0;
assertEquals(0.4, mojo.forOneCategory(row, 1, 0), 0);
mojo._strata_len = 2;
assertEquals(0.6, mojo.forOneCategory(row, 1, 0), 0);
}
/*
Test backward compatibility of CoxPH mojo model trained in 3.32.1.3.
Only using all features (both categorical and numeric).
*/
@Test
public void testCoxPHBackwardCompatibilityAll332() throws Exception {
String mojofile = String.valueOf(
Paths.get(
CoxPHMojoModelTest.class.getClassLoader().getResource("hex/genmodel/algos/coxph/CoxPH_bc_all_3_32.zip").toURI()
).toFile()
);
EasyPredictModelWrapper.Config config = new EasyPredictModelWrapper.Config()
.setModel(MojoModel.load(mojofile));
EasyPredictModelWrapper model = new EasyPredictModelWrapper(config);
String [][] inputs = new String[][] {
{"0", "50", "1", "-17.1553730321697", "0.123203285420945","0",
"0", "1", "-2.3004572363122033", "-23.840464872142775", "c0.l2", "c1.l2"},
{"0", "6", "1", "3.83572895277207", "0.254620123203285", "0",
"0", "2", "33.01605679101838", "-12.944002705270874" ,"c0.l1","c1.l2"},
{"0", "1", "0", "6.29705681040383", "0.265571526351814", "0",
"0", "3", "-22.54601829241052", "77.61631885563669", "c0.l2", "c1.l0"}
};
double [] expected = {0.0902431, 0.422815, 0.663896};
double[] preds = new double[inputs.length];
for (int i = 0; i < inputs.length; i++) {
RowData row = new RowData();
row.put("start", inputs[i][0]);
row.put("stop", inputs[i][1]);
row.put("event", inputs[i][2]);
row.put("age", inputs[i][3]);
row.put("year", inputs[i][4]);
row.put("surgery", inputs[i][5]);
row.put("transplant", inputs[i][6]);
row.put("id", inputs[i][7]);
row.put("C1", inputs[i][8]);
row.put("C2", inputs[i][9]);
row.put("C3", inputs[i][10]);
row.put("C4", inputs[i][11]);
preds[i] = model.predictCoxPH(row).value;
}
assertArrayEquals(expected, preds, 0.000001);
}
/*
Test backward compatibility of CoxPH mojo model trained in 3.42.0.4.
Only using all features (both categorical and numeric).
*/
@Test
public void testCoxPHBackwardCompatibilityAll342() throws Exception {
String mojofile = String.valueOf(
Paths.get(
CoxPHMojoModelTest.class.getClassLoader().getResource("hex/genmodel/algos/coxph/CoxPH_bc_all_3_42.zip").toURI()
).toFile()
);
EasyPredictModelWrapper.Config config = new EasyPredictModelWrapper.Config()
.setModel(MojoModel.load(mojofile));
EasyPredictModelWrapper model = new EasyPredictModelWrapper(config);
String [][] inputs = new String[][] {
{"0", "50", "1", "-17.1553730321697", "0.123203285420945","0",
"0", "1", "-2.3004572363122033", "-23.840464872142775", "c0.l2", "c1.l2"},
{"0", "6", "1", "3.83572895277207", "0.254620123203285", "0",
"0", "2", "33.01605679101838", "-12.944002705270874" ,"c0.l1","c1.l2"},
{"0", "1", "0", "6.29705681040383", "0.265571526351814", "0",
"0", "3", "-22.54601829241052", "77.61631885563669", "c0.l2", "c1.l0"}
};
double [] expected = {0.0902431, 0.422815, 0.663896};
double[] preds = new double[inputs.length];
for (int i = 0; i < inputs.length; i++) {
RowData row = new RowData();
row.put("start", inputs[i][0]);
row.put("stop", inputs[i][1]);
row.put("event", inputs[i][2]);
row.put("age", inputs[i][3]);
row.put("year", inputs[i][4]);
row.put("surgery", inputs[i][5]);
row.put("transplant", inputs[i][6]);
row.put("id", inputs[i][7]);
row.put("C1", inputs[i][8]);
row.put("C2", inputs[i][9]);
row.put("C3", inputs[i][10]);
row.put("C4", inputs[i][11]);
preds[i] = model.predictCoxPH(row).value;
}
assertArrayEquals(expected, preds, 0.000001);
}
/*
Test backward compatibility of CoxPH mojo model trained in 3.32.1.3.
Only using categorical features.
*/
@Test
public void testCoxPHBackwardCompatibilityCatOnly332() throws Exception {
String mojofile = String.valueOf(
Paths.get(
CoxPHMojoModelTest.class.getClassLoader().getResource("hex/genmodel/algos/coxph/CoxPH_bc_catOnly_3_32.zip").toURI()
).toFile()
);
EasyPredictModelWrapper.Config config = new EasyPredictModelWrapper.Config()
.setModel(MojoModel.load(mojofile));
EasyPredictModelWrapper model = new EasyPredictModelWrapper(config);
String [][] inputs = new String[][] {
{"0", "0"},
{"0", "1"},
{"1", "0"},
{"1", "1"},
};
double [] expected = {0.0628001, 0.221134, -0.686394, -0.528061};
double[] preds = new double[inputs.length];
for (int i = 0; i < inputs.length; i++) {
RowData row = new RowData();
row.put("surgery", inputs[i][0]);
row.put("transplant", inputs[i][1]);
preds[i] = model.predictCoxPH(row).value;
}
assertArrayEquals(expected, preds, 0.000001);
}
/*
Test backward compatibility of CoxPH mojo model trained in 3.42.0.4.
Only using categorical features.
*/
@Test
public void testCoxPHBackwardCompatibilityCatOnly342() throws Exception {
String mojofile = String.valueOf(
Paths.get(
CoxPHMojoModelTest.class.getClassLoader().getResource("hex/genmodel/algos/coxph/CoxPH_bc_catOnly_3_42.zip").toURI()
).toFile()
);
EasyPredictModelWrapper.Config config = new EasyPredictModelWrapper.Config()
.setModel(MojoModel.load(mojofile));
EasyPredictModelWrapper model = new EasyPredictModelWrapper(config);
String [][] inputs = new String[][] {
{"0", "0"},
{"0", "1"},
{"1", "0"},
{"1", "1"},
};
double [] expected = {0.0628001, 0.221134, -0.686394, -0.528061};
double[] preds = new double[inputs.length];
for (int i = 0; i < inputs.length; i++) {
RowData row = new RowData();
row.put("surgery", inputs[i][0]);
row.put("transplant", inputs[i][1]);
preds[i] = model.predictCoxPH(row).value;
}
assertArrayEquals(expected, preds, 0.000001);
}
}