summaryrefslogtreecommitdiff
path: root/src/python/test/test_alpha_complex_3d.py
blob: f7bd4fdad21de9db7f10d81e825511372c3835af (plain)
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
""" This file is part of the Gudhi Library - https://gudhi.inria.fr/ - which is released under MIT.
    See file LICENSE or go to https://gudhi.inria.fr/licensing/ for full license details.
    Author(s):       Vincent Rouvreau

    Copyright (C) 2021 Inria

    Modification(s):
      - YYYY/MM Author: Description of the modification
"""

from gudhi import AlphaComplex3D
import pytest

try:
    # python3
    from itertools import zip_longest
except ImportError:
    # python2
    from itertools import izip_longest as zip_longest



def _empty_alpha(precision):
    alpha_complex = AlphaComplex3D(precision = precision)
    assert alpha_complex.__is_defined() == True

def _one_3d_point_alpha(precision):
    alpha_complex = AlphaComplex3D(points=[[0, 0, 0]], precision = precision)
    assert alpha_complex.__is_defined() == True

def test_empty_alpha():
    for precision in ['fast', 'safe', 'exact']:
        _empty_alpha(precision)
        _one_3d_point_alpha(precision)

def _infinite_alpha(precision):
    point_list = [[0, 0, 0], [1, 0, 0], [0, 1, 0], [1, 1, 0], [0, 0, 1], [1, 0, 1], [0, 1, 1], [1, 1, 1]]
    alpha_complex = AlphaComplex3D(points=point_list, precision = precision)
    assert alpha_complex.__is_defined() == True

    stree = alpha_complex.create_simplex_tree()
    assert stree.__is_persistence_defined() == False

    assert stree.num_simplices() == 51
    assert stree.num_vertices() == len(point_list)

    for filtration in stree.get_filtration():
        if len(filtration[0]) == 1:
            assert filtration[1] == 0.
        if len(filtration[0]) == 4:
            assert filtration[1] == 0.75

    for idx in range(len(point_list)):
        pt_idx = point_list.index(alpha_complex.get_point(idx))
        assert pt_idx >= 0
        assert pt_idx < len(point_list)

    with pytest.raises(IndexError):
        alpha_complex.get_point(len(point_list))

def test_infinite_alpha():
    for precision in ['fast', 'safe', 'exact']:
        _infinite_alpha(precision)

def _filtered_alpha(precision):
    point_list = [[0, 0, 0], [1, 0, 0], [0, 1, 0], [1, 1, 0], [0, 0, 1], [1, 0, 1], [0, 1, 1], [1, 1, 1]]
    filtered_alpha = AlphaComplex3D(points=point_list, precision = precision)

    stree = filtered_alpha.create_simplex_tree(max_alpha_square=0.25)

    assert stree.num_simplices() == 20
    assert stree.num_vertices() == len(point_list)

    for filtration in stree.get_filtration():
        if len(filtration[0]) == 1:
            assert filtration[1] == 0.
        elif len(filtration[0]) == 2:
            assert filtration[1] == 0.25
        else:
            assert False

    for idx in range(len(point_list)):
        pt_idx = point_list.index(filtered_alpha.get_point(idx))
        assert pt_idx >= 0
        assert pt_idx < len(point_list)

    with pytest.raises(IndexError):
        filtered_alpha.get_point(len(point_list))

def test_filtered_alpha():
    for precision in ['fast', 'safe', 'exact']:
        _filtered_alpha(precision)

def _3d_points_on_a_plane(precision):
    alpha = AlphaComplex3D(points = [[1.0, 1.0 , 0.0],
                                     [7.0, 0.0 , 0.0],
                                     [4.0, 6.0 , 0.0],
                                     [9.0, 6.0 , 0.0],
                                     [0.0, 14.0, 0.0],
                                     [2.0, 19.0, 0.0],
                                     [9.0, 17.0, 0.0]], precision = precision)

    with pytest.raises(ValueError):
        stree = alpha.create_simplex_tree()

def test_3d_points_on_a_plane():
    for precision in ['fast', 'safe', 'exact']:
        _3d_points_on_a_plane(precision)

def test_inconsistency_points_and_weights():
    points = [[1.0, 1.0 , 1.0],
              [7.0, 0.0 , 2.0],
              [4.0, 6.0 , 0.0],
              [9.0, 6.0 , 1.0],
              [0.0, 14.0, 2.0],
              [2.0, 19.0, 0.0],
              [9.0, 17.0, 1.0]]
    with pytest.raises(ValueError):
        # 7 points, 8 weights, on purpose
        alpha = AlphaComplex3D(points = points,
                                weights = [1., 2., 3., 4., 5., 6., 7., 8.])

    with pytest.raises(ValueError):
        # 7 points, 6 weights, on purpose
        alpha = AlphaComplex3D(points = points,
                                weights = [1., 2., 3., 4., 5., 6.])

def _weighted_doc_example(precision):
    pts = [[ 1., -1., -1.],
           [-1.,  1., -1.],
           [-1., -1.,  1.],
           [ 1.,  1.,  1.],
           [ 2.,  2.,  2.]]
    wgts = [4., 4., 4., 4., 1.]
    alpha = AlphaComplex3D(points = pts, weights = wgts, precision = precision)
    stree = alpha.create_simplex_tree()

    # Needs to retrieve points as points are shuffled
    get_idx = lambda idx: pts.index(alpha.get_point(idx))
    indices = [get_idx(x) for x in range(len(pts))]

    assert stree.filtration([indices[x] for x in [0, 1, 2, 3]]) == pytest.approx(-1.)
    assert stree.filtration([indices[x] for x in [0, 1, 3, 4]]) == pytest.approx(95.)
    assert stree.filtration([indices[x] for x in [0, 2, 3, 4]]) == pytest.approx(95.)
    assert stree.filtration([indices[x] for x in [1, 2, 3, 4]]) == pytest.approx(95.)

def test_weighted_doc_example():
    for precision in ['fast', 'safe', 'exact']:
        _weighted_doc_example(precision)