/
test_util.py
381 lines (288 loc) · 13.4 KB
/
test_util.py
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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
from collections import OrderedDict
import numpy
from numpy.testing import assert_array_equal
import pandas
import pandas.util.testing as tm
import pytest
from sksurv.util import Surv, safe_concat
class TestUtil(object):
@staticmethod
def test_concat_numeric():
rnd = numpy.random.RandomState(14)
a = pandas.Series(rnd.randn(100), name="col_A")
b = pandas.Series(rnd.randn(100), name="col_B")
expected_df = pandas.DataFrame.from_dict(OrderedDict(
[(a.name, a), (b.name, b)]
))
actual_df = safe_concat((a, b), axis=1)
tm.assert_frame_equal(actual_df, expected_df)
@staticmethod
def test_concat_numeric_categorical():
rnd = numpy.random.RandomState(14)
a = pandas.Series(rnd.randn(100), name="col_A")
b = pandas.Series(pandas.Categorical.from_codes(
rnd.binomial(4, 0.6, 100), ["C1", "C2", "C3", "C4", "C5"]), name="col_B")
expected_df = pandas.DataFrame.from_dict(OrderedDict(
[(a.name, a), (b.name, b)]
))
actual_df = safe_concat((a, b), axis=1)
tm.assert_frame_equal(actual_df, expected_df)
@staticmethod
def test_concat_categorical():
rnd = numpy.random.RandomState(14)
a = pandas.DataFrame.from_dict(OrderedDict([
("col_A", pandas.Series(pandas.Categorical.from_codes(
rnd.binomial(2, 0.6, 100), ["C1", "C2", "C3"]), name="col_A")),
("col_B", rnd.randn(100))]))
b = pandas.DataFrame.from_dict(OrderedDict([
("col_A", pandas.Series(pandas.Categorical.from_codes(
rnd.binomial(2, 0.2, 100), ["C1", "C2", "C3"]), name="col_A")),
("col_B", rnd.randn(100))]))
expected_series = pandas.DataFrame.from_dict(OrderedDict([
("col_A", pandas.Series(pandas.Categorical.from_codes(
numpy.r_[a.col_A.cat.codes.values, b.col_A.cat.codes.values],
["C1", "C2", "C3"]
))),
("col_B", numpy.r_[a.col_B.values, b.col_B.values])
]))
expected_series.index = pandas.Index(a.index.tolist() + b.index.tolist())
actual_series = safe_concat((a, b), axis=0)
tm.assert_frame_equal(actual_series, expected_series)
@staticmethod
def test_concat_categorical_mismatch():
rnd = numpy.random.RandomState(14)
a = pandas.DataFrame.from_dict(OrderedDict([
("col_A", pandas.Series(pandas.Categorical.from_codes(
rnd.binomial(2, 0.6, 100), ["C1", "C2", "C3"]), name="col_A")),
("col_B", rnd.randn(100))]))
b = pandas.DataFrame.from_dict(OrderedDict([
("col_A", pandas.Series(pandas.Categorical.from_codes(
rnd.binomial(3, 0.6, 100), ["C1", "C2", "C3", "C4"]), name="col_A")),
("col_B", rnd.randn(100))]))
with pytest.raises(ValueError, match="categories for column col_A do not match"):
safe_concat((a, b), axis=0)
@staticmethod
def test_concat_dataframe_numeric_categorical():
rnd = numpy.random.RandomState(14)
numeric_df = pandas.DataFrame.from_dict(OrderedDict(
[("col_A", rnd.randn(100)), ("col_B", rnd.randn(100))]
))
cat_series = pandas.Series(pandas.Categorical.from_codes(
rnd.binomial(4, 0.6, 100), ["C1", "C2", "C3", "C4", "C5"]), name="col_C")
expected_df = numeric_df.copy()
expected_df["col_C"] = cat_series
actual_df = safe_concat((numeric_df, cat_series), axis=1)
tm.assert_frame_equal(actual_df, expected_df)
@staticmethod
def test_concat_duplicate_columns():
rnd = numpy.random.RandomState(14)
numeric_df = pandas.DataFrame.from_dict(OrderedDict([
("col_N", rnd.randn(100)), ("col_B", rnd.randn(100)),
("col_A", pandas.Series(pandas.Categorical.from_codes(
rnd.binomial(4, 0.2, 100), ["C1", "C2", "C3", "C4", "C5"]), name="col_A")),
]))
cat_df = pandas.DataFrame.from_dict(OrderedDict([
("col_A", pandas.Series(pandas.Categorical.from_codes(
rnd.binomial(4, 0.6, 100), ["C1", "C2", "C3", "C4", "C5"]), name="col_A")),
("col_C", pandas.Series(pandas.Categorical.from_codes(
rnd.binomial(1, 0.6, 100), ["Yes", "No"]), name="col_C")),
]))
with pytest.raises(ValueError, match="duplicate columns col_A"):
safe_concat((numeric_df, cat_df), axis=1)
@pytest.fixture
def surv_arrays():
event = numpy.random.binomial(1, 0.5, size=100)
time = numpy.exp(numpy.random.randn(100))
return event, time
@pytest.fixture
def surv_data_frame():
df = pandas.DataFrame({'event': numpy.random.binomial(1, 0.5, size=100),
'time': numpy.exp(numpy.random.randn(100))})
return df
class TestSurv(object):
@staticmethod
def test_from_list(surv_arrays):
event, time = surv_arrays
expected = numpy.empty(dtype=[('event', bool), ('time', float)], shape=100)
expected['event'] = event.astype(bool)
expected['time'] = time
y = Surv.from_arrays(list(event.astype(bool)), list(time))
assert_array_equal(y, expected)
@staticmethod
def test_from_array_bool(surv_arrays):
event, time = surv_arrays
expected = numpy.empty(dtype=[('event', bool), ('time', float)], shape=100)
expected['event'] = event.astype(bool)
expected['time'] = time
y = Surv.from_arrays(event.astype(bool), time)
assert_array_equal(y, expected)
@staticmethod
def test_from_array_with_names(surv_arrays):
event, time = surv_arrays
expected = numpy.empty(dtype=[('death', bool), ('survival_time', float)], shape=100)
expected['death'] = event.astype(bool)
expected['survival_time'] = time
y = Surv.from_arrays(event.astype(bool), time, name_time='survival_time', name_event='death')
assert_array_equal(y, expected)
@staticmethod
def test_from_array_with_one_name_1(surv_arrays):
event, time = surv_arrays
expected = numpy.empty(dtype=[('death', bool), ('time', float)], shape=100)
expected['death'] = event.astype(bool)
expected['time'] = time
y = Surv.from_arrays(event.astype(bool), time, name_event='death')
assert_array_equal(y, expected)
@staticmethod
def test_from_array_with_one_name_2(surv_arrays):
event, time = surv_arrays
expected = numpy.empty(dtype=[('event', bool), ('survival_time', float)], shape=100)
expected['event'] = event.astype(bool)
expected['survival_time'] = time
y = Surv.from_arrays(event.astype(bool), time, name_time='survival_time')
assert_array_equal(y, expected)
@staticmethod
def test_from_array_int_event(surv_arrays):
event, time = surv_arrays
expected = numpy.empty(dtype=[('event', bool), ('time', float)], shape=100)
expected['event'] = event.astype(bool)
expected['time'] = time
y = Surv.from_arrays(event, time)
assert_array_equal(y, expected)
@staticmethod
def test_from_array_int_time(surv_arrays):
event, time = surv_arrays
time += 1
time *= time
expected = numpy.empty(dtype=[('event', bool), ('time', float)], shape=100)
expected['event'] = event.astype(bool)
expected['time'] = time.astype(int)
y = Surv.from_arrays(event.astype(bool), time.astype(int))
assert_array_equal(y, expected)
@staticmethod
def test_from_array_float(surv_arrays):
event, time = surv_arrays
expected = numpy.empty(dtype=[('event', bool), ('time', float)], shape=100)
expected['event'] = event.astype(bool)
expected['time'] = time
y = Surv.from_arrays(event.astype(float), time)
assert_array_equal(y, expected)
@staticmethod
def test_from_array_shape_mismatch(surv_arrays):
event, time = surv_arrays
msg = "Found input variables with inconsistent numbers of samples"
with pytest.raises(ValueError, match=msg):
Surv.from_arrays(event[1:], time)
with pytest.raises(ValueError, match=msg):
Surv.from_arrays(event, time[1:])
@staticmethod
def test_from_array_event_value_wrong_1(surv_arrays):
event, time = surv_arrays
event += 1
with pytest.raises(ValueError,
match="non-boolean event indicator must contain 0 and 1 only"):
Surv.from_arrays(event, time)
@staticmethod
def test_from_array_event_value_wrong_2(surv_arrays):
event, time = surv_arrays
event -= 1
with pytest.raises(ValueError,
match="non-boolean event indicator must contain 0 and 1 only"):
Surv.from_arrays(event, time)
@staticmethod
def test_from_array_event_value_wrong_3(surv_arrays):
event, time = surv_arrays
event[event == 0] = 3
with pytest.raises(ValueError,
match="non-boolean event indicator must contain 0 and 1 only"):
Surv.from_arrays(event, time)
@staticmethod
def test_from_array_event_value_wrong_4(surv_arrays):
event, time = surv_arrays
event[1] = 3
with pytest.raises(ValueError,
match="event indicator must be binary"):
Surv.from_arrays(event, time)
@staticmethod
def test_from_array_event_value_wrong_5(surv_arrays):
event, time = surv_arrays
event = numpy.arange(event.shape[0])
with pytest.raises(ValueError,
match="event indicator must be binary"):
Surv.from_arrays(event, time)
@staticmethod
def test_from_array_names_match(surv_arrays):
event, time = surv_arrays
with pytest.raises(ValueError,
match="name_time must be different from name_event"):
Surv.from_arrays(event, time,
name_event='time_and_event', name_time='time_and_event')
@staticmethod
def test_from_dataframe_bool(surv_data_frame):
data = surv_data_frame
data['event'] = data['event'].astype(bool)
expected = numpy.empty(dtype=[('event', bool), ('time', float)], shape=100)
expected['event'] = data['event']
expected['time'] = data['time']
y = Surv.from_dataframe('event', 'time', data)
assert_array_equal(y, expected)
@staticmethod
def test_from_dataframe_int(surv_data_frame):
data = surv_data_frame
expected = numpy.empty(dtype=[('event', bool), ('time', float)], shape=100)
expected['event'] = data['event'].astype(bool)
expected['time'] = data['time']
y = Surv.from_dataframe('event', 'time', data)
assert_array_equal(y, expected)
@staticmethod
def test_from_dataframe_float(surv_data_frame):
data = surv_data_frame
data['event'] = data['event'].astype(float)
expected = numpy.empty(dtype=[('event', bool), ('time', float)], shape=100)
expected['event'] = data['event'].astype(bool)
expected['time'] = data['time']
y = Surv.from_dataframe('event', 'time', data)
assert_array_equal(y, expected)
@staticmethod
def test_from_dataframe_no_str_columns(surv_data_frame):
data = surv_data_frame
data['event'] = data['event'].astype(bool)
expected = numpy.empty(dtype=[('0', bool), ('1', float)], shape=100)
expected['0'] = data['event']
expected['1'] = data['time']
y = Surv.from_dataframe(0, 1, data.rename(columns={'event': 0, 'time': 1}))
assert_array_equal(y, expected)
@staticmethod
def test_from_dataframe_column_names(surv_data_frame):
data = surv_data_frame.rename(columns={'event': 'death', 'time': 'time_to_death'})
data['death'] = data['death'].astype(bool)
expected = numpy.empty(dtype=[('death', bool), ('time_to_death', float)], shape=100)
expected['death'] = data['death']
expected['time_to_death'] = data['time_to_death']
y = Surv.from_dataframe('death', 'time_to_death', data)
assert_array_equal(y, expected)
@staticmethod
def test_from_dataframe_no_such_column(surv_data_frame, pandas_version_under_0p24):
data = surv_data_frame
data['event'] = data['event'].astype(bool)
expected = numpy.empty(dtype=[('event', bool), ('time', float)], shape=100)
expected['event'] = data['event']
expected['time'] = data['time']
if pandas_version_under_0p24:
match = r'the label \[unknown\] is not in the \[columns\]'
else:
match = 'unknown'
with pytest.raises(KeyError,
match=match):
Surv.from_dataframe('unknown', 'time', data)
with pytest.raises(KeyError,
match=match):
Surv.from_dataframe('event', 'unknown', data)
@staticmethod
def test_from_dataframe_wrong_class(surv_data_frame):
data = surv_data_frame
with pytest.raises(TypeError,
match=r"exepected pandas.DataFrame, but got <class 'dict'>"):
Surv.from_dataframe('event', 'time', data.to_dict())
with pytest.raises(TypeError,
match=r"exepected pandas.DataFrame, but got <class 'numpy.ndarray'>"):
Surv.from_dataframe('event', 'time', data.values)