test_calculate.test_AMD_finite()   A
last analyzed

Complexity

Conditions 2

Size

Total Lines 10
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 9
dl 0
loc 10
rs 9.95
c 0
b 0
f 0
cc 2
nop 0
1
import numpy as np
2
import pytest
3
import amd
4
5
6
def test_AMD(reference_data):
7
    for name in reference_data:
8
        for s in reference_data[name]:
9
            calc_amd = amd.AMD(s["PeriodicSet"], 100)
10
11
            if not np.allclose(calc_amd, s["AMD100"]):
12
                n = s["PeriodicSet"].name
13
                ref_amd = str(s["AMD100"])
14
15
                pytest.fail(
16
                    f"AMD of structure {n} disagrees with reference. "
17
                    f"reference: {ref_amd}, calculated: {calc_amd}"
18
                )
19
20
21
def test_PDD(reference_data):
22
    for name in reference_data:
23
        for s in reference_data[name]:
24
            calc_pdd = amd.PDD(s["PeriodicSet"], 100, lexsort=False)
25
26
            # Not ideal, but it seems different Python versions can make
27
            # floating point changes that reorders the rows of the PDD.
28
            if not np.allclose(calc_pdd, s["PDD100"]):
29
                # if not amd.EMD(s["PDD100"], calc_pdd) < 1e-14:
30
                abs_diffs = np.abs(calc_pdd - s["PDD100"])
31
                diffs = str(np.sort(abs_diffs.flatten())[::-1][:10])
32
                n = s["PeriodicSet"].name
33
                ref_pdd = str(s["PDD100"])
34
35
                pytest.fail(
36
                    f"PDD of structure {n} disagrees with reference; "
37
                    f"reference: {ref_pdd}, calculated: {calc_pdd}; "
38
                    f"Largest elementwise diff between PDDs: {diffs}; "
39
                    f"PDD EMD: {amd.EMD(s['PDD100'], calc_pdd)}"
40
                )
41
42
43
def test_PDD_to_AMD(reference_data):
44
    for name in reference_data:
45
        for s in reference_data[name]:
46
            calc_pdd = amd.PDD(s["PeriodicSet"], 100, lexsort=False)
47
            calc_amd = amd.AMD(s["PeriodicSet"], 100)
48
            amd_from_pdd = amd.PDD_to_AMD(calc_pdd)
49
50
            if not np.allclose(calc_amd, amd_from_pdd):
51
                n = s["PeriodicSet"].name
52
                pytest.fail(
53
                    f"Directly calculated AMD of structure {n} disagrees "
54
                    "with AMD calculated from PDD."
55
                )
56
57
58
def test_AMD_finite():
59
    trapezium = np.array([[0, 0], [1, 1], [3, 1], [4, 0]])
60
    kite = np.array([[0, 0], [1, 1], [1, -1], [4, 0]])
61
    trap_amd = amd.AMD_finite(trapezium)
62
    kite_amd = amd.AMD_finite(kite)
63
    dist = np.linalg.norm(trap_amd - kite_amd)
64
65
    if not abs(dist - 0.6180339887498952) < 1e-16:
66
        pytest.fail(
67
            "AMDs of finite sets trapezium and kite are different than expected."
68
        )
69
70
71
def test_PDD_finite():
72
    trapezium = np.array([[0, 0], [1, 1], [3, 1], [4, 0]])
73
    kite = np.array([[0, 0], [1, 1], [1, -1], [4, 0]])
74
    trap_pdd = amd.PDD_finite(trapezium)
75
    kite_pdd = amd.PDD_finite(kite)
76
    dist = amd.EMD(trap_pdd, kite_pdd)
77
78
    if not abs(dist - 0.874032045) < 1e-8:
79
        pytest.fail(
80
            "PDDs of finite sets trapezium and kite are different than " "expected."
81
        )
82