This repository has been archived by the owner on May 27, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
testweights.py
69 lines (63 loc) · 2.53 KB
/
testweights.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
#!/usr/bin/python
import numpy as np
from scipy import stats
from convolution import convolve, max_gauss_pdf, max_gauss_cdf
__all__ = ['test_weights']
def accuracy(nt_mean, nt_var, nt_count, t_mean, t_var, repetitions):
nt_mean *= repetitions
nt_var *= repetitions
nt_std = np.sqrt(nt_var)
t_mean *= repetitions
t_var *= repetitions
t_std = np.sqrt(t_var)
return convolve(
lambda t: max_gauss_cdf(nt_mean - t_mean, nt_std, nt_count, t),
lambda t: max_gauss_pdf(0, t_std, 1, t),
0
)
def test_weights(responses, type, classifier, matrixshape, repetitions):
responses = np.asarray(responses, dtype = float) \
[:, :classifier.shape[0], :classifier.shape[1]]
# If the weights do not include the last channel or two (or more),
# then the dense classifier matrix created will not be the right
# dimensions. Since this only occurs for the last channels, this
# can be corrected by throwing out the channels that are not in
# the classification matrix, as done by [..., :classifier.shape[1]]
type = np.asarray(type, dtype = bool)
if responses.shape[1] != classifier.shape[0]:
return 'Response window not long enough.'
target = type.nonzero()[0]
nontarget = (~type).nonzero()[0]
target_scores = (responses[target] * classifier). \
sum(axis = 1).sum(axis = 1)
target_mean = target_scores.mean()
target_var = target_scores.var(ddof = 1)
nontarget_scores = (responses[nontarget] * classifier). \
sum(axis = 1).sum(axis = 1)
nontarget_mean = nontarget_scores.mean()
nontarget_var = nontarget_scores.var(ddof = 1)
if target_mean <= nontarget_mean:
return 'These weights are so bad that they actually ' + \
'classify *against* the target.'
if np.isscalar(matrixshape):
matrixshape = [matrixshape]
if np.isscalar(repetitions):
repetitions = [repetitions]
correctness = []
for reps in repetitions:
total_accuracy = 1
for dimension in matrixshape:
total_accuracy *= \
accuracy(
nontarget_mean,
nontarget_var,
dimension - 1,
target_mean,
target_var,
reps
)
correctness.append(total_accuracy)
c_mean = target_mean - nontarget_mean
target_std = np.sqrt(target_var) / c_mean
nontarget_std = np.sqrt(nontarget_var) / c_mean
return (target_std, nontarget_std), correctness