|
1
|
|
|
import pytest |
|
2
|
|
|
|
|
3
|
|
|
|
|
4
|
|
|
@pytest.fixture |
|
5
|
|
|
def map_manager(somagic): |
|
6
|
|
|
return somagic.map.manager |
|
7
|
|
|
|
|
8
|
|
|
|
|
9
|
|
|
@pytest.fixture |
|
10
|
|
|
def identical_map_ids(): |
|
11
|
|
|
def assert_map_ids_are_the_same(map_id1, map_id2): |
|
12
|
|
|
assert str(map_id1) == str(map_id2) |
|
13
|
|
|
assert dict(map_id1) == dict(map_id2) |
|
14
|
|
|
return assert_map_ids_are_the_same |
|
15
|
|
|
|
|
16
|
|
|
|
|
17
|
|
|
@pytest.fixture |
|
18
|
|
|
def test_data(test_dataset): |
|
19
|
|
|
from collections import OrderedDict |
|
20
|
|
|
from so_magic.som.manager import MapId |
|
21
|
|
|
map_specs = OrderedDict([ |
|
22
|
|
|
('nb-cols', 4), |
|
23
|
|
|
('nb-rows', 5), |
|
24
|
|
|
('initialization', 'pca'), |
|
25
|
|
|
('maptype', 'toroid'), |
|
26
|
|
|
('gridtype', 'hexagonal'), |
|
27
|
|
|
]) |
|
28
|
|
|
return type('TestData', (object,), { |
|
29
|
|
|
'map_parameters': type('SomModelParameters', (object,), { |
|
30
|
|
|
'args': (test_dataset, map_specs['nb-cols'], map_specs['nb-rows']), |
|
31
|
|
|
'kwargs': {k: map_specs[k] for k in ('initialization', 'maptype', 'gridtype')} |
|
32
|
|
|
}), |
|
33
|
|
|
'get_runtime_map_id': lambda x: MapId.from_self_organizing_map(x), |
|
34
|
|
|
'expected_map_id': MapId(*[test_dataset.name] + list(map_specs.values())), |
|
35
|
|
|
}) |
|
36
|
|
|
|
|
37
|
|
|
|
|
38
|
|
|
def test_map_manager_get_map_method(map_manager, test_data, identical_map_ids): |
|
39
|
|
|
# assert the get_map method returns the same object when invoked with already seen arguments |
|
40
|
|
|
som1 = map_manager.get_map(*test_data.map_parameters.args, **test_data.map_parameters.kwargs) |
|
41
|
|
|
som2 = map_manager.get_map(*test_data.map_parameters.args, **test_data.map_parameters.kwargs) |
|
42
|
|
|
assert id(som1) == id(som2) |
|
43
|
|
|
|
|
44
|
|
|
map_id = test_data.get_runtime_map_id(som1) |
|
45
|
|
|
identical_map_ids(map_id, test_data.expected_map_id) |
|
46
|
|
|
assert som1.get_map_id() == str(map_id) |
|
47
|
|
|
|
|
48
|
|
|
assert som1.nb_clusters == 0 |
|
49
|
|
|
|
|
50
|
|
|
with pytest.raises(TypeError, match="'NoneType' object is not subscriptable"): |
|
51
|
|
|
_ = som1.visual_umatrix |
|
52
|
|
|
|
|
53
|
|
|
# tightly depends on the current implementation that requires to invoke the |
|
54
|
|
|
# 'cluster' method of a SelfOrganisingMap instance to do 'clustering' on the |
|
55
|
|
|
# output of the self-organising map training/learning algorithm |
|
56
|
|
|
som1.cluster(4, random_state=1) |
|
57
|
|
|
assert som1.nb_clusters == 4 |
|
58
|
|
|
|
|
59
|
|
|
umatrix_str_representation = som1.visual_umatrix |
|
60
|
|
|
|
|
61
|
|
|
assert umatrix_str_representation == '3 3 3 3\n2 0 0 0\n2 2 0 0\n1 1 1 1\n1 1 1 1\n' |
|
62
|
|
|
assert umatrix_str_representation == '3 3 3 3\n' \ |
|
63
|
|
|
'2 0 0 0\n' \ |
|
64
|
|
|
'2 2 0 0\n' \ |
|
65
|
|
|
'1 1 1 1\n' \ |
|
66
|
|
|
'1 1 1 1\n' |
|
67
|
|
|
assert umatrix_str_representation == '\ |
|
68
|
|
|
3 3 3 3\n\ |
|
69
|
|
|
2 0 0 0\n\ |
|
70
|
|
|
2 2 0 0\n\ |
|
71
|
|
|
1 1 1 1\n\ |
|
72
|
|
|
1 1 1 1\n\ |
|
73
|
|
|
' |
|
74
|
|
|
|
|
75
|
|
|
assert som1.datapoint_coordinates(0) == (2, 1) |
|
76
|
|
|
|