File size: 1,601 Bytes
ee37d63
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import pandas as pd

from scripts.audit_multi_factor_calculation import (
    build_factor_warnings,
    summarize_factor_series,
)


def test_summarize_factor_series_reports_coverage_and_range():
    summary = summarize_factor_series(pd.Series([-1.0, 0.0, 0.5, 2.0, float("nan")]))

    assert summary["n"] == 5
    assert summary["finite_n"] == 4
    assert summary["nonfinite_count"] == 1
    assert summary["zero_pct"] == 0.25
    assert summary["nonzero_pct"] == 0.75
    assert summary["positive_pct"] == 0.5
    assert summary["negative_pct"] == 0.25
    assert summary["out_of_range_count"] == 1
    assert summary["all_zero"] is False


def test_build_factor_warnings_flags_inert_and_out_of_range_factors():
    warnings = build_factor_warnings(
        {
            "all_zero": {
                "nonfinite_count": 0,
                "out_of_range_count": 0,
                "all_zero": True,
                "nonzero_pct": 0.0,
            },
            "sparse": {
                "nonfinite_count": 0,
                "out_of_range_count": 0,
                "all_zero": False,
                "nonzero_pct": 0.01,
            },
            "bad_range": {
                "nonfinite_count": 0,
                "out_of_range_count": 3,
                "all_zero": False,
                "nonzero_pct": 0.5,
            },
        }
    )

    assert any("all_zero: all values are zero" in warning for warning in warnings)
    assert any("sparse: non-zero coverage" in warning for warning in warnings)
    assert any("bad_range: has 3 values outside" in warning for warning in warnings)