|
| 1 | +import numpy as np |
| 2 | +import pandas as pd |
| 3 | +import pytest |
| 4 | + |
| 5 | +from sgkit.cohorts import _cohorts_to_array, _tuple_len |
| 6 | + |
| 7 | + |
| 8 | +def test_tuple_len(): |
| 9 | + assert _tuple_len(tuple()) == 0 |
| 10 | + assert _tuple_len(1) == 1 |
| 11 | + assert _tuple_len("a") == 1 |
| 12 | + assert _tuple_len("ab") == 1 |
| 13 | + assert _tuple_len((1,)) == 1 |
| 14 | + assert _tuple_len(("a",)) == 1 |
| 15 | + assert _tuple_len(("ab",)) == 1 |
| 16 | + assert _tuple_len((1, 2)) == 2 |
| 17 | + assert _tuple_len(("a", "b")) == 2 |
| 18 | + assert _tuple_len(("ab", "cd")) == 2 |
| 19 | + |
| 20 | + |
| 21 | +def test_cohorts_to_array__indexes(): |
| 22 | + with pytest.raises(ValueError, match="Cohort tuples must all be the same length"): |
| 23 | + _cohorts_to_array([(0, 1), (0, 1, 2)]) |
| 24 | + |
| 25 | + np.testing.assert_equal(_cohorts_to_array([]), np.array([])) |
| 26 | + np.testing.assert_equal(_cohorts_to_array([0, 1]), np.array([[0], [1]])) |
| 27 | + np.testing.assert_equal( |
| 28 | + _cohorts_to_array([(0, 1), (2, 1)]), np.array([[0, 1], [2, 1]]) |
| 29 | + ) |
| 30 | + np.testing.assert_equal( |
| 31 | + _cohorts_to_array([(0, 1, 2), (3, 1, 2)]), np.array([[0, 1, 2], [3, 1, 2]]) |
| 32 | + ) |
| 33 | + |
| 34 | + |
| 35 | +def test_cohorts_to_array__ids(): |
| 36 | + with pytest.raises(ValueError, match="Cohort tuples must all be the same length"): |
| 37 | + _cohorts_to_array([("c0", "c1"), ("c0", "c1", "c2")]) |
| 38 | + |
| 39 | + np.testing.assert_equal(_cohorts_to_array([]), np.array([])) |
| 40 | + np.testing.assert_equal( |
| 41 | + _cohorts_to_array(["c0", "c1"], pd.Index(["c0", "c1"])), |
| 42 | + np.array([[0], [1]]), |
| 43 | + ) |
| 44 | + np.testing.assert_equal( |
| 45 | + _cohorts_to_array([("c0", "c1"), ("c2", "c1")], pd.Index(["c0", "c1", "c2"])), |
| 46 | + np.array([[0, 1], [2, 1]]), |
| 47 | + ) |
| 48 | + np.testing.assert_equal( |
| 49 | + _cohorts_to_array( |
| 50 | + [("c0", "c1", "c2"), ("c3", "c1", "c2")], pd.Index(["c0", "c1", "c2", "c3"]) |
| 51 | + ), |
| 52 | + np.array([[0, 1, 2], [3, 1, 2]]), |
| 53 | + ) |
0 commit comments