|
3 | 3 | from polars.testing import assert_frame_equal |
4 | 4 | from rtichoke.calibration.calibration import _apply_heuristics_and_censoring |
5 | 5 |
|
| 6 | + |
6 | 7 | @pytest.fixture |
7 | 8 | def sample_data(): |
8 | | - return pl.DataFrame({ |
9 | | - "real": [1, 0, 2, 1, 2, 0, 1], |
10 | | - "time": [1, 2, 3, 8, 9, 10, 12], |
11 | | - }) |
| 9 | + return pl.DataFrame( |
| 10 | + { |
| 11 | + "real": [1, 0, 2, 1, 2, 0, 1], |
| 12 | + "time": [1, 2, 3, 8, 9, 10, 12], |
| 13 | + } |
| 14 | + ) |
| 15 | + |
12 | 16 |
|
13 | 17 | def test_competing_as_negative_logic(sample_data): |
14 | 18 | # Heuristics that shouldn't change data before horizon |
15 | | - result = _apply_heuristics_and_censoring(sample_data, 15, "adjusted", "adjusted_as_negative") |
| 19 | + result = _apply_heuristics_and_censoring( |
| 20 | + sample_data, 15, "adjusted", "adjusted_as_negative" |
| 21 | + ) |
16 | 22 | # Competing events at times 3 and 9 should become 0. |
17 | | - expected = pl.DataFrame({ |
18 | | - "real": [1, 0, 0, 1, 0, 0, 1], |
19 | | - "time": [1, 2, 3, 8, 9, 10, 12], |
20 | | - }) |
| 23 | + expected = pl.DataFrame( |
| 24 | + { |
| 25 | + "real": [1, 0, 0, 1, 0, 0, 1], |
| 26 | + "time": [1, 2, 3, 8, 9, 10, 12], |
| 27 | + } |
| 28 | + ) |
21 | 29 | assert_frame_equal(result, expected) |
22 | 30 |
|
| 31 | + |
23 | 32 | def test_admin_censoring(sample_data): |
24 | | - result = _apply_heuristics_and_censoring(sample_data, 7, "adjusted", "adjusted_as_negative") |
| 33 | + result = _apply_heuristics_and_censoring( |
| 34 | + sample_data, 7, "adjusted", "adjusted_as_negative" |
| 35 | + ) |
25 | 36 | # Admin censoring for times > 7. Competing event at time=3 becomes 0. |
26 | | - expected = pl.DataFrame({ |
27 | | - "real": [1, 0, 0, 0, 0, 0, 0], |
28 | | - "time": [1, 2, 3, 8, 9, 10, 12], |
29 | | - }) |
| 37 | + expected = pl.DataFrame( |
| 38 | + { |
| 39 | + "real": [1, 0, 0, 0, 0, 0, 0], |
| 40 | + "time": [1, 2, 3, 8, 9, 10, 12], |
| 41 | + } |
| 42 | + ) |
30 | 43 | assert_frame_equal(result, expected) |
31 | 44 |
|
| 45 | + |
32 | 46 | def test_censoring_excluded(sample_data): |
33 | | - result = _apply_heuristics_and_censoring(sample_data, 10, "excluded", "adjusted_as_negative") |
| 47 | + result = _apply_heuristics_and_censoring( |
| 48 | + sample_data, 10, "excluded", "adjusted_as_negative" |
| 49 | + ) |
34 | 50 | # Excludes censored at times 2, 10. Admin censors time > 10. Competing at 3,9 -> 0. |
35 | | - expected = pl.DataFrame({ |
36 | | - "real": [1, 0, 1, 0, 0], |
37 | | - "time": [1, 3, 8, 9, 12], |
38 | | - }) |
| 51 | + expected = pl.DataFrame( |
| 52 | + { |
| 53 | + "real": [1, 0, 1, 0, 0], |
| 54 | + "time": [1, 3, 8, 9, 12], |
| 55 | + } |
| 56 | + ) |
39 | 57 | assert_frame_equal(result.sort("time"), expected.sort("time")) |
40 | 58 |
|
| 59 | + |
41 | 60 | def test_competing_excluded(sample_data): |
42 | 61 | result = _apply_heuristics_and_censoring(sample_data, 10, "adjusted", "excluded") |
43 | 62 | # Excludes competing at 3, 9. Admin censors time > 10. |
44 | | - expected = pl.DataFrame({ |
45 | | - "real": [1, 0, 1, 0, 0], |
46 | | - "time": [1, 2, 8, 10, 12], |
47 | | - }) |
| 63 | + expected = pl.DataFrame( |
| 64 | + { |
| 65 | + "real": [1, 0, 1, 0, 0], |
| 66 | + "time": [1, 2, 8, 10, 12], |
| 67 | + } |
| 68 | + ) |
48 | 69 | assert_frame_equal(result.sort("time"), expected.sort("time")) |
49 | 70 |
|
| 71 | + |
50 | 72 | def test_competing_as_negative(sample_data): |
51 | | - result = _apply_heuristics_and_censoring(sample_data, 10, "adjusted", "adjusted_as_negative") |
| 73 | + result = _apply_heuristics_and_censoring( |
| 74 | + sample_data, 10, "adjusted", "adjusted_as_negative" |
| 75 | + ) |
52 | 76 | # Competing at 3,9 -> 0. Admin censors time > 10. |
53 | | - expected = pl.DataFrame({ |
54 | | - "real": [1, 0, 0, 1, 0, 0, 0], |
55 | | - "time": [1, 2, 3, 8, 9, 10, 12], |
56 | | - }) |
| 77 | + expected = pl.DataFrame( |
| 78 | + { |
| 79 | + "real": [1, 0, 0, 1, 0, 0, 0], |
| 80 | + "time": [1, 2, 3, 8, 9, 10, 12], |
| 81 | + } |
| 82 | + ) |
57 | 83 | assert_frame_equal(result, expected) |
58 | 84 |
|
| 85 | + |
59 | 86 | def test_competing_as_composite(sample_data): |
60 | | - result = _apply_heuristics_and_censoring(sample_data, 10, "adjusted", "adjusted_as_composite") |
| 87 | + result = _apply_heuristics_and_censoring( |
| 88 | + sample_data, 10, "adjusted", "adjusted_as_composite" |
| 89 | + ) |
61 | 90 | # Competing at 3,9 -> 1. Admin censors time > 10. |
62 | | - expected = pl.DataFrame({ |
63 | | - "real": [1, 0, 1, 1, 1, 0, 0], |
64 | | - "time": [1, 2, 3, 8, 9, 10, 12], |
65 | | - }) |
| 91 | + expected = pl.DataFrame( |
| 92 | + { |
| 93 | + "real": [1, 0, 1, 1, 1, 0, 0], |
| 94 | + "time": [1, 2, 3, 8, 9, 10, 12], |
| 95 | + } |
| 96 | + ) |
66 | 97 | assert_frame_equal(result, expected) |
0 commit comments