forked from diffpy/diffpy.srmise
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_dataclusters.py
89 lines (81 loc) · 2.59 KB
/
test_dataclusters.py
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
from copy import copy
import numpy as np
import pytest
from diffpy.srmise.dataclusters import DataClusters
def test___eq__():
actual = DataClusters(np.array([1, 2, 3]), np.array([3, 2, 1]), 1)
expected = DataClusters(np.array([1, 2, 3]), np.array([3, 2, 1]), 1)
assert expected == actual
attributes = vars(actual)
for attr_key, attr_val in attributes.items():
reset = copy(attr_val)
assert expected == actual
if attr_val is not None:
attributes.update({attr_key: attr_val + 1})
else:
attributes.update({attr_key: 1})
try:
assert not expected == actual
except AssertionError:
print(f"not-equal test failed on {attr_key}")
assert not expected == actual
attributes.update({attr_key: reset})
@pytest.mark.parametrize(
"inputs, expected",
[
(
{
"x": np.array([1, 2, 3]),
"y": np.array([3, 2, 1]),
"res": 4,
},
{
"x": np.array([1, 2, 3]),
"y": np.array([3, 2, 1]),
"res": 4,
"data_order": [2, 1, 0],
"clusters": np.array([[0, 0]]),
"current_idx": 2,
"lastpoint_idx": 0,
"INIT": 0,
"READY": 1,
"CLUSTERING": 2,
"DONE": 3,
"lastcluster_idx": None,
"status": 1,
},
),
],
)
def test_DataClusters_constructor(inputs, expected):
actual = DataClusters(x=inputs["x"], y=inputs["y"], res=inputs["res"])
actual_attributes = vars(actual)
for attr_key, actual_attr_val in actual_attributes.items():
if isinstance(actual_attr_val, np.ndarray):
assert np.array_equal(actual_attr_val, expected[attr_key])
else:
assert actual_attr_val == expected[attr_key]
@pytest.mark.parametrize(
"inputs, msg",
[
(
{
"x": np.array([1]),
"y": np.array([3, 2]),
"res": 4,
},
"Sequences x and y must have the same length.",
),
(
{
"x": np.array([1]),
"y": np.array([3]),
"res": -1,
},
"Value of resolution parameter is less than zero. Please rerun specifying a non-negative res",
),
],
)
def test_set_data_order_bad(inputs, msg):
with pytest.raises(ValueError, match=msg):
DataClusters(x=inputs["x"], y=inputs["y"], res=inputs["res"])