-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_spatial_clustering.py
117 lines (79 loc) · 3.46 KB
/
test_spatial_clustering.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
## global import ---------------------------------------------------------------
import numpy as np
import pytest
## local import ----------------------------------------------------------------
import spatial_clustering
from spatial_clustering import EntrySpatialPooler
from network import NodeFactory
import config
## test the Entry Temporal Pooler
@pytest.mark.randomize(("size1", int), min_num=2, max_num=10)
@pytest.mark.randomize(("size2", int), min_num=2, max_num=10, ncalls=3)
def test_entry_closest_coinc(size1, size2):
p = EntrySpatialPooler()
input_msg = np.random.randint(255, size=size1)
input_msg.dtype = np.uint8
c = []
for i in range(size2):
r = np.random.randint(255, size=size1)
r.dtype = np.uint8
c.append(r)
min_ = np.sqrt(np.sum(np.power(np.array(c[0], dtype=np.double) -
np.array(input_msg, dtype=np.double),
2)))
k = 0
print 0, min_
for i in range(1, len(c)):
dist = np.sqrt(np.sum(np.power(np.array(c[i], dtype=np.double) -
np.array(input_msg, dtype=np.double),
2)))
print i, dist
if dist < min_:
min_ = dist
k = i
coinc_matrix = c[0]
for i in range(1, len(c)):
coinc_matrix = np.vstack((coinc_matrix, c[i]))
res = p.closest_coincidence(coinc_matrix, input_msg)
assert res[0] == k
assert res[1] == min_
assert res[1].dtype == np.float64
## test the select_active_coinc when len(coincidences) == 0
def select_active_coinc1():
n = make_node((0,0), network.ENTRY, config.usps_net)
s = n.state
input_msg = np.random.randint(255, size=size1)
input_msg.dtype = np.uint8
s.state['input_msg'] = input_msg
s.strategy['trainer'].select_active_coinc(s.state)
assert s.state['coincidences'][0] == input_msg
assert s.state['coincidences'].dtype == np.uint8
assert s.state['TAM'].shape == (1,1)
assert s.state['seen'] == 1
## test the select_active_coinc when there is more than one coincidence
@pytest.mark.randomize(("coinc_size", int), min_num=2, max_num=10)
def select_active_coinc2():
n = make_node((0,0), network.ENTRY, config.usps_net)
s = n.state
input_msg = np.random.randint(255, size=size1)
input_msg.dtype = np.uint8
s.state['input_msg'] = input_msg
s.state['coincidences'] = np.random.randint(255, shape(coinc_size, 4))
s.state['seen'] = np.random.randint(10, size=coinc_size)
s.state['TAM'] = np.random.randint(10, size=(coinc_size, coinc_size))
s.strategy['trainer'].select_active_coinc(s.state)
assert s.state['coincidences'].dtype == np.uint8
## test the closest_coincidence method
@pytest.mark.randomize(("size1", int), min_num=1, max_num=10)
@pytest.mark.randomize(("size2", int), min_num=1, max_num=10, ncalls=3)
def test_wdix_distance2(size1, size2):
array = np.random.randint(0, 100, size=(12000, 4))
distances = spatial_clustering.widx_distance2(array)
print distances
(rows, cols) = array.shape
## assert on the number of non-zero elements in each row
for i in range(rows):
non_zeros_count = 0
for j in range(cols):
if array[i,j] != 0: non_zeros_count += 1
assert non_zeros_count == distances[i]