Passed
Pull Request — dev (#32)
by Konstantinos
03:16 queued 01:49
created

test_discretization.discretize_command()   A

Complexity

Conditions 1

Size

Total Lines 8
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 8
nop 0
dl 0
loc 8
rs 10
c 0
b 0
f 0
1
import pytest
2
3
4
@pytest.fixture
5
def define_command():
6
    def _define_engine_command(decorator, command_function):
7
        decorator(command_function)
8
    return _define_engine_command
9
10
11
@pytest.fixture
12
def get_command(somagic):
13
    def _get_command(command_name: str):
14
        return getattr(somagic.command, command_name)
15
    return _get_command
16
17
18
@pytest.fixture
19
def test_discretizer():
20
    from so_magic.data.discretization import Discretizer, BinningAlgorithm
21
22
    alg = BinningAlgorithm.from_built_in('pd.cut')
23
24
    discretizer = Discretizer.from_algorithm(alg)
25
    return discretizer
26
27
28
@pytest.fixture
29
def discretize_command():
30
    def get_discretize_command(discretizer):
31
        def test_discretize_command(data_manager, datapoints, attribute, nb_bins, new_column_name):
32
            output = discretizer.discretize(datapoints, attribute, nb_bins)
33
            data_manager.datapoints.add_column(output['result'], new_column_name)
34
        return test_discretize_command
35
    return get_discretize_command
36
37
38
@pytest.fixture
39
def validate_discretization_operation_behaviour():
40
    def _validate_discretization_operation(cmd, algorithm):
41
        datapoints = cmd.args[0]
42
        target_column = cmd.args[1]
43
        nb_bins = cmd.args[2]
44
        computed_bins = algorithm.output['settings']['used_bins']
45
        assert [_ for _ in computed_bins] == [-0.1, 25.0, 50.0, 75.0, 100.0]
46
47
        input_arguments = algorithm.output['settings']['arguments']
48
        to_check = [len(input_arguments[0]), input_arguments[1]]
49
        assert to_check == [len(datapoints), nb_bins]
50
        assert type(datapoints.column(target_column)) == type(input_arguments[0])
51
        assert list(datapoints.column(target_column)) == list(input_arguments[0])
52
        # assert algorithm.output['settings']['parameters'] == []
53
    return _validate_discretization_operation
54
55
56
@pytest.fixture
57
def discretization_test_data(somagic, test_datapoints):
58
    series = somagic.dataset.datapoints.column('Creative').replace('', 0.0, inplace=False)
59
    assert all(type(x) == float for x in series)
60
61
    somagic.datapoints.add_column(list(series), 'Creative')
62
63
    
64
    assert all(type(x) == float for x in somagic.datapoints.observations['Creative'])
65
66
    return {
67
        'success': [
68
            'Creative'
69
        ],
70
        'fail': [
71
            'Energetic'
72
        ],
73
    }
74
75
76
def test_discretization_operation(somagic, discretization_test_data, define_command, get_command, test_discretizer, discretize_command, validate_discretization_operation_behaviour):
77
    define_command(somagic.commands_decorators.data_manager_command(), discretize_command(test_discretizer))
78
    for attr_name in discretization_test_data['success']:
79
        cmd = get_command('test_discretize_command')
80
        cmd.args = [somagic.datapoints, attr_name, 4, f'binned_{attr_name}']
81
        cmd.execute()
82
83
        validate_discretization_operation_behaviour(cmd, test_discretizer.algorithm)
84
85
    for attr_name in discretization_test_data['fail']:
86
        cmd = get_command('test_discretize_command')
87
        cmd.args = [somagic.datapoints, attr_name, 4, f'binned_{attr_name}']
88
        with pytest.raises(TypeError):
89
            cmd.execute()
90