-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathtest_conversions.py
127 lines (98 loc) · 3.95 KB
/
test_conversions.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
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
# pylint: disable=import-error, wrong-import-position, wrong-import-order, invalid-name
"""Implicit conversion test suite"""
from common import *
from jpype import _jclass
from trustyai.model import feature
from trustyai.model.domain import feature_domain
from org.kie.trustyai.explainability.model import Type
def test_list_python_to_java():
"""Test Python to Java List conversion"""
python_list = [2, 4, 3, 5, 1]
minimum = _jclass.JClass('java.util.Collections').min(python_list)
assert minimum == 1
def test_list_java_to_python():
"""Test Java to Python List conversion"""
python_list = [2, 4, 3, 5, 1]
java_list = _jclass.JClass('java.util.Arrays').asList(python_list)
assert 15 == sum(java_list)
def test_numeric_domain_tuple():
"""Test create numeric domain from tuple"""
domain = (0, 1000)
jdomain = feature_domain(domain)
assert jdomain.getLowerBound() == 0
assert jdomain.getUpperBound() == 1000
domain = (0.0, 1000.0)
jdomain = feature_domain(domain)
assert jdomain.getLowerBound() == 0.0
assert jdomain.getUpperBound() == 1000.0
def test_categorical_numeric_domain_list():
"""Test create numeric domain from list"""
domain = [0, 1000]
jdomain = feature_domain(domain)
assert jdomain.getCategories().size() == 2
assert jdomain.getCategories().containsAll(domain)
domain = [0.0, 1000.0]
jdomain = feature_domain(domain)
assert jdomain.getCategories().size() == 2
assert jdomain.getCategories().containsAll(domain)
def test_categorical_object_domain_list():
"""Test create object domain from list"""
domain = [True, False]
jdomain = feature_domain(domain)
assert str(jdomain.getClass().getSimpleName()) == "ObjectFeatureDomain"
assert jdomain.getCategories().size() == 2
assert jdomain.getCategories().containsAll(domain)
def test_categorical_object_domain_list_2():
"""Test create object domain from list"""
domain = [b"test", b"test2"]
jdomain = feature_domain(domain)
assert str(jdomain.getClass().getSimpleName()) == "ObjectFeatureDomain"
assert jdomain.getCategories().size() == 2
assert jdomain.getCategories().containsAll(domain)
def test_empty_domain():
"""Test empty domain"""
domain = feature_domain(None)
assert domain.isEmpty() is True
def test_categorical_domain_tuple():
"""Test create categorical domain from tuple and list"""
domain = ["foo", "bar", "baz"]
jdomain = feature_domain(domain)
assert jdomain.getCategories().size() == 3
assert jdomain.getCategories().containsAll(list(domain))
domain = ["foo", "bar", "baz"]
jdomain = feature_domain(domain)
assert jdomain.getCategories().size() == 3
assert jdomain.getCategories().containsAll(domain)
def test_feature_function():
"""Test helper method to create features"""
f1 = feature(name="f-1", value=1.0, dtype="number")
assert f1.name == "f-1"
assert f1.value.as_number() == 1.0
assert f1.type == Type.NUMBER
f2 = feature(name="f-2", value=True, dtype="bool")
assert f2.name == "f-2"
assert f2.value.as_obj() == True
assert f2.type == Type.BOOLEAN
f3 = feature(name="f-3", value="foo", dtype="categorical")
assert f3.name == "f-3"
assert f3.value.as_string() == "foo"
assert f3.type == Type.CATEGORICAL
f4 = feature(name="f-4", value=5, dtype="categorical")
assert f4.name == "f-4"
assert f4.value.as_number() == 5
assert f4.type == Type.CATEGORICAL
def test_feature_domains():
"""Test domains"""
f1 = feature(name="f-1", value=1.0, dtype="number")
assert f1.name == "f-1"
assert f1.value.as_number() == 1.0
assert f1.type == Type.NUMBER
assert f1.domain is None
assert f1.is_constrained
f2 = feature(name="f-2", value=2.0, dtype="number", domain=(0.0, 10.0))
assert f2.name == "f-2"
assert f2.value.as_number() == 2.0
assert f2.type == Type.NUMBER
assert f2.domain
print(f2.domain)
assert not f2.is_constrained