File size: 2,396 Bytes
6f423bd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
069d642
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import numpy as np
import pytest

from plotscribe.calibrate import fit_categorical, fit_numeric


def test_linear_with_outlier():
    px = [100, 200, 300, 400, 500]
    vals = [50, 40, 30, 200, 10]  # one OCR error
    cal = fit_numeric("y", px, vals)
    assert cal.kind == "linear"
    assert cal.n_inliers == 4
    assert float(cal.to_value(250)) == pytest.approx(35.0)
    assert any("outlier" in w for w in cal.warnings)


def test_log_axis():
    px = [400, 300, 200, 100]
    cal = fit_numeric("y", px, [1, 10, 100, 1000])
    assert cal.kind == "log"
    assert float(cal.to_value(250)) == pytest.approx(10 ** 1.5)


def test_candidates_pick_consistent_reading():
    px = [0, 100, 200, 300]
    vals = [[0.0], [10.0, 1.0], [20.0, 2.0], [30.0]]
    cal = fit_numeric("x", px, vals)
    assert cal.n_inliers == 4
    assert cal.tick_values == pytest.approx([0, 10, 20, 30])


def test_too_few_labels_is_flagged():
    cal = fit_numeric("y", [10.0], [5.0])
    assert cal.confidence == 0 and cal.warnings


def test_categorical_order():
    cal = fit_categorical("x", [300.0, 100.0, 200.0], ["c", "a", "b"])
    assert cal.categories == ["a", "b", "c"]
    assert cal.nearest_category(190)[0] == 1


def test_rescale_roundtrip():
    cal = fit_numeric("y", [100, 200, 300], [3, 2, 1])
    r = cal.rescaled(2.0)
    assert float(r.to_value(100)) == pytest.approx(float(cal.to_value(200)))
    assert np.isclose(float(cal.to_px(cal.to_value(123.0))), 123.0)


def test_misread_digit_on_every_label_is_flagged():
    # "0" read as "6": 26, 46, 66 fits a line but is off the tick grid.
    cal = fit_numeric("y", [500, 400, 300, 200], [26, 46, 66, 86])
    assert cal.confidence < 0.6
    clean = fit_numeric("y", [500, 400, 300, 200], [20, 40, 60, 80])
    assert clean.confidence == 1.0


def test_lost_decimal_points_are_flagged():
    # "0.1".."0.4" read as 1..4; the correctly read 0.6, 0.7 are 10x off the fit.
    px = [400, 300, 200, 100, -100, -200]
    cal = fit_numeric("y", px, [1, 2, 3, 4, 0.6, 0.7])
    assert cal.n_inliers == 4 and cal.confidence < 0.3


def test_misread_category_names():
    from plotscribe.pipeline import misread_name
    for ok in ["Banana", "Q1 2020", "Isle of Man", "Korea, Rep.", "R&D", "A"]:
        assert not misread_name(ok), ok
    for bad in ["Plum |!", "a Fig", "Mango kK", "iwDat", "Peach)", "category_3"]:
        assert misread_name(bad), bad