-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRandomSplit.py
257 lines (171 loc) · 6.52 KB
/
RandomSplit.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
#
# Nathan Lay
# AI Resource at National Cancer Institute
# National Institutes of Health
# January 2023
#
# THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
# IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
# OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
# IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
# NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
# THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#
import numpy as np
def RandomSplit(W, training_size, tries=10):
assert W.ndim == 2
N = W.shape[1]
if training_size < 1:
training_size = int(training_size*N)
assert training_size >= 0 and training_size <= N
assert np.all(W.max(axis=0) > 0) # Make sure all instances count for something
if training_size == 0:
return np.zeros(N, dtype=int), 0.0
if training_size == N:
return np.ones(N, dtype=int), 0.0
# Remove rows with no counts over any instance
D = W.sum(axis=1)
W = W[D > 0, :]
D = D[D > 0]
K = W.shape[0]
assert K > 1 and N >= K
D = 1.0/D
Z = np.eye(K) - 1.0/K
# This is the same as D*W... just in numpy weirdness
W = W*D[..., None]
# This is ZDW
W = Z @ W
U, S, Vh = np.linalg.svd(W)
Q = Vh[(K-1):, :].T
bestRes = -1.0
bestX = None
for _ in range(tries):
x = np.random.randn(Q.shape[1])
x = np.inner(Q, x)
ind = np.argsort(x)
ind = ind[::-1]
x = np.zeros(N, dtype=int)
x[ind[:training_size]] = 1
res = np.linalg.norm(np.inner(W, x))
if res < bestRes or bestRes < 0.0:
bestRes = res
bestX = x
return bestX, bestRes
def BalancedCrossValidation(W, F, tries=10, aggregator=np.max):
assert W.ndim == 2
N = W.shape[1]
assert F > 1 and F <= N
assert np.all(W.max(axis=0) > 0) # Make sure all instances count for something
# Remove rows with no counts over any instance
D = W.sum(axis=1)
W = W[D > 0, :]
D = D[D > 0]
K = W.shape[0]
assert K > 1 and N >= K
D = 1.0/D
Z = np.eye(K) - 1.0/K
# This is the same as D*W... just in numpy weirdness
W = W*D[..., None]
# This is ZDW
W = Z @ W
U, S, Vh = np.linalg.svd(W)
Q = Vh[(K-1):, :].T
bestRes = None
bestFolds = None
for _ in range(tries):
x = np.random.randn(Q.shape[1])
x = np.inner(Q, x)
ind = np.argsort(x)
ind = ind[::-1]
res = []
folds = []
for f in range(F):
val_begin = N*f//F
val_end = N*(f+1)//F
x = np.ones(N, dtype=int)
x[ind[val_begin:val_end]] = 0 # Mask out validation set
folds.append(x)
res.append(np.linalg.norm(np.inner(W, x)))
if bestRes is None or aggregator(res) < aggregator(bestRes):
bestRes = res
bestFolds = folds
return bestFolds, bestRes
def MakeRandomSplit(W, training_size, testing_size, column_map, tries=10):
assert W.ndim == 2
N = W.shape[1]
assert N <= len(column_map)
if training_size < 1:
training_size = int(training_size*N)
if testing_size < 1:
testing_size = int(testing_size*N)
assert training_size >= 0 and testing_size >= 0 and training_size + testing_size <= N
validation_size = N - (training_size + testing_size)
xtest, restest = RandomSplit(W, testing_size, tries=tries)
xtrainval = 1-xtest
testing_list = []
for i in np.argwhere(xtest):
cases = column_map[int(i)] # One column could represent a single patient with multiple scans!
if not isinstance(cases, list):
cases = [ cases ]
testing_list += cases
Wtrainval = W[:, np.argwhere(xtrainval).squeeze(-1)]
column_map_trainval = []
for i in np.argwhere(xtrainval):
column_map_trainval.append(column_map[int(i)])
xtrain, restrain = RandomSplit(Wtrainval, training_size, tries=tries)
validation_list = []
training_list = []
for i in range(len(xtrain)):
cases = column_map_trainval[i]
if not isinstance(cases, list):
cases = [ cases ]
if xtrain[i]:
training_list += cases
else:
validation_list += cases
return training_list, testing_list, validation_list, restrain, restest
def MakeBalancedCrossValidation(W, F, column_map, testing_size=0, tries=10, aggregator=np.max):
assert W.ndim == 2
N = W.shape[1]
assert N <= len(column_map)
if testing_size < 1:
testing_size = int(testing_size*N)
assert testing_size >= 0 and testing_size < N
testing_list = []
restest = 0.0
if testing_size > 0:
xtest, restest = RandomSplit(W, testing_size, tries=tries)
xcv = 1-xtest
for i in np.argwhere(xtest):
cases = column_map[int(i)] # One column could represent a single patient with multiple scans!
if not isinstance(cases, list):
cases = [ cases ]
testing_list += cases
Wcv = W[:, np.argwhere(xcv).squeeze(-1)]
column_map_cv = []
for i in np.argwhere(xcv):
column_map_cv.append(column_map[int(i)])
else:
Wcv = W
column_map_cv = column_map
folds, res = BalancedCrossValidation(Wcv, F, tries=tries, aggregator=aggregator)
training_lists = []
validation_lists = []
for fold in folds:
training_list = []
validation_list = []
for i in range(len(fold)):
cases = column_map_cv[i]
if not isinstance(cases, list):
cases = [ cases ]
if fold[i]:
training_list += cases
else:
validation_list += cases
training_lists.append(training_list)
validation_lists.append(validation_list)
return training_lists, validation_lists, testing_list, res, restest