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