/
test_clinical_kernel.py
292 lines (225 loc) · 9.91 KB
/
test_clinical_kernel.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
import numpy
from numpy.testing import assert_array_almost_equal
import pandas
import pytest
from sklearn.base import clone
from sklearn.metrics.pairwise import pairwise_kernels
from sksurv.kernels import ClinicalKernelTransform, clinical_kernel
def _get_expected_matrix(with_ordinal=True, with_nominal=True, with_continuous=True):
mat_age = numpy.array([[1., 0.9625, 0.925, 0.575, 0.],
[0.9625, 1., 0.9625, 0.6125, 0.0375],
[0.925, 0.9625, 1., 0.6500, 0.075],
[0.575, 0.6125, 0.6500, 1., 0.425],
[0., 0.0375, 0.075, 0.425, 1.]])
mat_node_size = numpy.array([[1., 2/3, 2/3, 1/3, 2/3],
[2/3, 1., 1/3, 0., 1.],
[2/3, 1/3, 1., 2/3, 1/3],
[1/3, 0., 2/3, 1., 0.],
[2/3, 1., 1/3, 0., 1.]])
mat_node_spread = numpy.array([[1., 0., 1., 0.5, 0.],
[0., 1., 0., 0.5, 1.],
[1., 0., 1., 0.5, 0.],
[0.5, 0.5, 0.5, 1., 0.5],
[0., 1., 0., 0.5, 1.]])
mat_metastasis = numpy.array([[1, 0, 1, 1, 0],
[0, 1, 0, 0, 1],
[1, 0, 1, 1, 0],
[1, 0, 1, 1, 0],
[0, 1, 0, 0, 1]], dtype=float)
included = []
if with_continuous:
included.append(mat_age)
if with_ordinal:
included.append(mat_node_size)
included.append(mat_node_spread)
if with_nominal:
included.append(mat_metastasis)
expected = included[0]
for i in range(1, len(included)):
expected += included[i]
expected /= len(included)
return expected
@pytest.fixture
def make_data():
data = {'age': [20, 23, 26, 54, 100],
'lymph node size': [2, 1, 3, 4, 1],
'lymph node spread': ['distant', 'none', 'distant', 'close', 'none'],
'metastasis': ['yes', 'no', 'yes', 'yes', 'no']}
def _make_data(with_ordinal=True, with_nominal=True, with_continuous=True):
data_s = {}
if with_continuous:
data_s['age'] = data['age']
if with_ordinal:
data_s['lymph node size'] = pandas.Categorical(data['lymph node size'],
categories=[1, 2, 3, 4],
ordered=True)
data_s['lymph node spread'] = pandas.Categorical(data['lymph node spread'],
categories=['none', 'close', 'distant'],
ordered=True)
if with_nominal:
data_s['metastasis'] = pandas.Categorical(data['metastasis'],
categories=['no', 'yes'],
ordered=False)
expected = _get_expected_matrix(
with_ordinal=with_ordinal,
with_nominal=with_nominal,
with_continuous=with_continuous)
return pandas.DataFrame(data_s), expected
return _make_data
class TestClinicalKernel(object):
@staticmethod
def test_clinical_kernel_1(make_data):
data, expected = make_data()
mat = clinical_kernel(data)
assert_array_almost_equal(expected, mat, 4)
@staticmethod
def test_clinical_kernel_no_ordinal(make_data):
data, expected = make_data(with_ordinal=False)
mat = clinical_kernel(data)
assert_array_almost_equal(expected, mat, 4)
@staticmethod
def test_clinical_kernel_no_nominal(make_data):
data, expected = make_data(with_nominal=False)
mat = clinical_kernel(data)
assert_array_almost_equal(expected, mat, 4)
@staticmethod
def test_clinical_kernel_no_continuous(make_data):
data, expected = make_data(with_continuous=False)
mat = clinical_kernel(data)
assert_array_almost_equal(expected, mat, 4)
@staticmethod
def test_clinical_kernel_only_nominal(make_data):
data, expected = make_data(with_continuous=False, with_ordinal=False)
mat = clinical_kernel(data)
assert_array_almost_equal(expected, mat, 4)
@staticmethod
def test_clinical_kernel_x_and_y(make_data):
data, m = make_data()
mat = clinical_kernel(data.iloc[:3, :], data.iloc[3:, :])
expected = m[:3:, 3:]
assert_array_almost_equal(expected, mat, 4)
@staticmethod
def test_fit_error_ndim():
t = ClinicalKernelTransform()
with pytest.raises(ValueError, match="expected 2d array, but got 1"):
t.fit(numpy.random.randn(31))
with pytest.raises(ValueError, match="expected 2d array, but got 3"):
t.fit(numpy.random.randn(31, 20, 2))
@staticmethod
def test_kernel_transform(make_data):
data, expected = make_data()
t = ClinicalKernelTransform()
t.fit(data)
mat = t.transform(t.X_fit_)
assert_array_almost_equal(expected, mat, 4)
@staticmethod
def test_kernel_transform_x_and_y(make_data):
data, m = make_data()
t = ClinicalKernelTransform(fit_once=True)
t.prepare(data)
x_num = t.X_fit_.copy()
t.fit(x_num[:3, :])
mat = t.transform(x_num[3:, :])
expected = m[:3, 3:].T
assert_array_almost_equal(expected, mat, 4)
@staticmethod
def test_kernel_transform_feature_mismatch(make_data):
data, _ = make_data()
t = ClinicalKernelTransform()
t.fit(data)
with pytest.raises(ValueError, match='expected array with 4 features, but got 17'):
t.transform(numpy.zeros((2, 17), dtype=float))
@staticmethod
def test_pairwise(make_data):
data, expected = make_data()
t = ClinicalKernelTransform()
t.fit(data)
mat = pairwise_kernels(t.X_fit_, t.X_fit_,
metric=t.pairwise_kernel, n_jobs=1)
assert_array_almost_equal(expected, mat, 4)
@staticmethod
def test_pairwise_x_and_y(make_data):
data, m = make_data()
t = ClinicalKernelTransform()
t.fit(data)
mat = pairwise_kernels(t.X_fit_[:3, :], t.X_fit_[3:, :],
metric=t.pairwise_kernel, n_jobs=1)
expected = m[:3:, 3:]
assert_array_almost_equal(expected, mat, 4)
@staticmethod
def test_pairwise_x_and_y_error_shape(make_data):
data, _ = make_data()
t = ClinicalKernelTransform()
t.fit(data)
with pytest.raises(ValueError, match="X and Y have different number of features"):
t.pairwise_kernel(data.iloc[0, :], data.iloc[1, :2])
@staticmethod
def test_pairwise_no_nominal(make_data):
data, expected = make_data(with_nominal=False)
t = ClinicalKernelTransform()
t.fit(data)
mat = pairwise_kernels(t.X_fit_[:3, :], t.X_fit_[3:, :],
metric=t.pairwise_kernel, n_jobs=1)
assert_array_almost_equal(expected[:3:, 3:], mat, 4)
@staticmethod
def test_call_function(make_data):
data, expected = make_data()
t = ClinicalKernelTransform(fit_once=True)
t.prepare(data)
mat = t(t.X_fit_, t.X_fit_)
assert_array_almost_equal(expected, mat, 4)
@staticmethod
def test_call_function_x_and_y(make_data):
data, m = make_data()
t = ClinicalKernelTransform(fit_once=True)
t.prepare(data)
mat = t(t.X_fit_[:3, :], t.X_fit_[3:, :])
expected = m[:3:, 3:]
assert_array_almost_equal(expected, mat, 4)
@staticmethod
def test_pairwise_feature_mismatch(make_data):
data, _ = make_data()
t = ClinicalKernelTransform()
t.fit(data)
with pytest.raises(ValueError, match=r'Incompatible dimension for X and Y matrices: '
r'X.shape\[1\] == 4 while Y.shape\[1\] == 17'):
pairwise_kernels(t.X_fit_, numpy.zeros((2, 17), dtype=float),
metric=t.pairwise_kernel, n_jobs=1)
@staticmethod
def test_prepare(make_data):
data, expected = make_data()
t = ClinicalKernelTransform(fit_once=True)
t.prepare(data)
copy = clone(t).fit(t.X_fit_)
mat = copy.transform(t.X_fit_[:4, :])
assert_array_almost_equal(expected[:4, :], mat, 4)
@staticmethod
def test_prepare_error_fit_once(make_data):
data = make_data()
t = ClinicalKernelTransform(fit_once=False)
with pytest.raises(ValueError, match="prepare can only be used if fit_once parameter is set to True"):
t.prepare(data)
@staticmethod
def test_prepare_error_type():
t = ClinicalKernelTransform(fit_once=True)
with pytest.raises(TypeError, match='X must be a pandas DataFrame'):
t.prepare([[0, 1], [1, 2], [4, 3], [6, 5]])
@staticmethod
def test_prepare_error_dtype():
t = ClinicalKernelTransform(fit_once=True)
data = pandas.DataFrame({"age": [12, 61, 18, 21, 57, 17],
"date": numpy.array(
["2016-01-01", "1954-06-30", "1999-03-01", "2005-02-25", "2112-12-31",
"1731-09-16"], dtype='datetime64')})
with pytest.raises(TypeError, match=r'unsupported dtype: dtype\(.+\)'):
t.prepare(data)
@staticmethod
def test_feature_mismatch(make_data):
data, _ = make_data()
x = data.iloc[:, :2]
y = data.iloc[:, 2:]
with pytest.raises(ValueError, match='columns do not match'):
clinical_kernel(x, y)
y = numpy.zeros((10, 17))
with pytest.raises(ValueError, match='x and y have different number of features'):
clinical_kernel(x, y)