|
1
|
|
|
import pytest |
|
2
|
|
|
from so_magic.utils import MapOnLinearSpace |
|
3
|
|
|
from so_magic.utils.linear_mapping import LinearScale |
|
4
|
|
|
|
|
5
|
|
|
@pytest.mark.parametrize('lower_bound, upper_bound, wrong_index', [ |
|
6
|
|
|
(20, 100, 2), |
|
7
|
|
|
pytest.param(20, 20, 2, marks=pytest.mark.xfail(raises=ValueError, reason="lower_bound < upper_bound should be true; The lower bound of a linear scale is restricted to be strictly smaller than the upper bound")), |
|
8
|
|
|
pytest.param(80, 20, 2, marks=pytest.mark.xfail(raises=ValueError, reason="lower_bound < upper_bound should be true; The lower bound of a linear scale is restricted to be strictly smaller than the upper bound")), |
|
9
|
|
|
]) |
|
10
|
|
|
def test_linear_scale(lower_bound, upper_bound, wrong_index): |
|
11
|
|
|
scale = LinearScale(lower_bound, upper_bound) |
|
12
|
|
|
assert scale[0] == lower_bound |
|
13
|
|
|
assert scale[1] == upper_bound |
|
14
|
|
|
assert len(scale) == 2 |
|
15
|
|
|
with pytest.raises(IndexError) as e: |
|
16
|
|
|
_ = scale[wrong_index] |
|
17
|
|
|
assert f'You can only request the 0 (lower bound) or 1 (upper bound) indexed item on the linear scale. You requested {wrong_index}' == str(e) |
|
18
|
|
|
|
|
19
|
|
|
@pytest.mark.parametrize('from_scale, target_scale, reverse, input_value, output_value', [ |
|
20
|
|
|
([0, 400], [0, 100], True, 50, 87.5), |
|
21
|
|
|
([0, 400], [0, 100], True, 100, 75), |
|
22
|
|
|
([0, 400], [0, 100], True, 25, 93.75), |
|
23
|
|
|
([0, 400], [0, 100], True, 10, 97.5), |
|
24
|
|
|
([0, 400], [0, 100], True, 400, 0), |
|
25
|
|
|
([0, 400], [0, 100], True, 0, 100), |
|
26
|
|
|
([0, 400], [0, 100], False, 50, 12.5), |
|
27
|
|
|
([0, 400], [0, 100], False, 100, 25), |
|
28
|
|
|
([0, 400], [0, 100], False, 25, 6.25), |
|
29
|
|
|
([0, 400], [0, 100], False, 10, 2.5), |
|
30
|
|
|
([0, 400], [0, 100], False, 400, 100), |
|
31
|
|
|
([0, 400], [0, 100], False, 0, 0), |
|
32
|
|
|
]) |
|
33
|
|
|
def test_linear_transformation(from_scale, target_scale, reverse, input_value, output_value): |
|
34
|
|
|
linear_transformer = MapOnLinearSpace.universal_constructor(from_scale, target_scale, reverse=reverse) |
|
35
|
|
|
assert all(hasattr(linear_transformer, x) for x in ('from_scale', 'target_scale', 'reverse')) |
|
36
|
|
|
assert hasattr(linear_transformer, 'from_scale') |
|
37
|
|
|
assert linear_transformer.transform(input_value) == output_value |
|
38
|
|
|
|
|
39
|
|
|
linear_transformer.reverse = not linear_transformer.reverse |
|
40
|
|
|
assert linear_transformer.transform(input_value) == target_scale[1] - output_value |
|
41
|
|
|
|
|
42
|
|
|
linear_transformer.reverse = False |
|
43
|
|
|
linear_transformer.from_scale = [0, 10] |
|
44
|
|
|
linear_transformer.target_scale = [0, 100] |
|
45
|
|
|
assert linear_transformer.transform(7) == 70 |
|
46
|
|
|
|