Passed
Push — mpeta ( 12cb7e...e55e57 )
by Konstantinos
01:52
created

test_som_manager   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 68
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 38
dl 0
loc 68
rs 10
c 0
b 0
f 0
wmc 5

4 Functions

Rating   Name   Duplication   Size   Complexity  
A test_map_manager_get_map_method() 0 45 2
A identical_map_ids() 0 6 1
A map_manager() 0 3 1
A map_id_class() 0 4 1
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 map_id_class():
11
    from so_magic.som.manager import MapId
12
    return MapId
13
14
15
@pytest.fixture
16
def identical_map_ids():
17
    def assert_map_ids_are_the_same(map_id1, map_id2):
18
        assert str(map_id1) == str(map_id2)
19
        assert dict(map_id1) == dict(map_id2)
20
    return assert_map_ids_are_the_same
21
22
23
def test_map_manager_get_map_method(map_manager, test_dataset, map_id_class, identical_map_ids):
24
    # assert the get_map method returns the same object when invoked with already seen arguments
25
    som1 = map_manager.get_map(test_dataset, 4, 5, initialization='pca', maptype='toroid', gridtype='hexagonal')
26
    som2 = map_manager.get_map(test_dataset, 4, 5, initialization='pca', maptype='toroid', gridtype='hexagonal')
27
    assert id(som1) == id(som2)
28
29
    map_id = map_id_class.from_self_organizing_map(som1)
30
    identical_map_ids(map_id, map_id_class(
31
        test_dataset.name,
32
        4,
33
        5,
34
        'pca',
35
        'toroid',
36
        'hexagonal'
37
    ))
38
39
    assert som1.get_map_id() == str(map_id)
40
    assert som1.nb_clusters == 0
41
42
    with pytest.raises(TypeError, match="'NoneType' object is not subscriptable"):
43
        _ = som1.visual_umatrix
44
45
    # tightly depends on the current implementation that requires to invoke the
46
    # 'cluster' method of a SelfOrganisingMap instance to do 'clustering' on the
47
    # output of the self-organising map training/learning algorithm
48
    som1.cluster(4, random_state=1)
49
    assert som1.nb_clusters == 4
50
51
    umatrix_str_representation = som1.visual_umatrix
52
53
    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'
54
    assert umatrix_str_representation == '3 3 3 3\n' \
55
                                         '2 0 0 0\n' \
56
                                         '2 2 0 0\n' \
57
                                         '1 1 1 1\n' \
58
                                         '1 1 1 1\n'
59
    assert umatrix_str_representation == '\
60
3 3 3 3\n\
61
2 0 0 0\n\
62
2 2 0 0\n\
63
1 1 1 1\n\
64
1 1 1 1\n\
65
'
66
67
    assert som1.datapoint_coordinates(0) == (2, 1)
68