|
13 | 13 | import pandas as pd |
14 | 14 | from balance.sample_class import Sample |
15 | 15 | from balance.summary_utils import ( |
| 16 | + _append_poststratify_model_diagnostics, |
| 17 | + _append_rake_model_diagnostics, |
16 | 18 | _build_diagnostics, |
17 | 19 | _build_summary, |
18 | 20 | _concat_metric_val_var, |
@@ -756,3 +758,158 @@ def get_params(self, deep=False): |
756 | 758 | covars_asmd_main=covars_asmd, |
757 | 759 | ) |
758 | 760 | assert not out.empty |
| 761 | + |
| 762 | + |
| 763 | +class _BrokenLen: |
| 764 | + def __len__(self) -> int: |
| 765 | + raise RuntimeError("length unavailable") |
| 766 | + |
| 767 | + |
| 768 | +def _minimal_diagnostics_inputs() -> dict[str, Any]: |
| 769 | + covars_df = pd.DataFrame({"a": [1, 2], "b": ["x", "y"]}) |
| 770 | + covars_asmd = pd.DataFrame( |
| 771 | + {"self": [0.1], "unadjusted": [0.2], "unadjusted - self": [0.1]}, |
| 772 | + index=pd.Index(["a"]), |
| 773 | + ) |
| 774 | + |
| 775 | + return { |
| 776 | + "covars_df": covars_df, |
| 777 | + "target_covars_df": covars_df.copy(), |
| 778 | + "weights_summary": pd.DataFrame({"var": ["design_effect"], "val": [1.0]}), |
| 779 | + "covars_asmd": covars_asmd, |
| 780 | + "covars_asmd_main": covars_asmd, |
| 781 | + } |
| 782 | + |
| 783 | + |
| 784 | +def test_build_diagnostics_includes_rake_model_glance() -> None: |
| 785 | + iterations = pd.DataFrame({"conv": [0.5, 0.01]}, index=pd.Index([0, 1])) |
| 786 | + model = { |
| 787 | + "method": "rake", |
| 788 | + "converged": 1, |
| 789 | + "iterations": iterations, |
| 790 | + "variables": ["a", "b"], |
| 791 | + } |
| 792 | + |
| 793 | + out = _build_diagnostics( |
| 794 | + **_minimal_diagnostics_inputs(), |
| 795 | + model_dict=model, |
| 796 | + ) |
| 797 | + |
| 798 | + glance = out[out["metric"] == "model_glance"].set_index("var")["val"] |
| 799 | + assert glance["converged"] == 1 |
| 800 | + assert glance["iterations"] == 2 |
| 801 | + assert glance["final_conv"] == 0.01 |
| 802 | + assert glance["n_variables"] == 2 |
| 803 | + |
| 804 | + |
| 805 | +def test_build_diagnostics_handles_sparse_rake_metadata() -> None: |
| 806 | + model = { |
| 807 | + "method": "rake", |
| 808 | + "iterations": pd.DataFrame({"other": [1.0]}), |
| 809 | + "variables": _BrokenLen(), |
| 810 | + } |
| 811 | + |
| 812 | + out = _build_diagnostics( |
| 813 | + **_minimal_diagnostics_inputs(), |
| 814 | + model_dict=model, |
| 815 | + ) |
| 816 | + |
| 817 | + glance = out[out["metric"] == "model_glance"].set_index("var")["val"] |
| 818 | + assert np.isnan(float(glance["converged"])) |
| 819 | + assert glance["iterations"] == 1 |
| 820 | + assert "final_conv" not in glance.index |
| 821 | + assert np.isnan(float(glance["n_variables"])) |
| 822 | + |
| 823 | + |
| 824 | +def test_build_diagnostics_treats_scalar_strings_as_missing_lengths() -> None: |
| 825 | + for method in ("rake", "poststratify"): |
| 826 | + model = { |
| 827 | + "method": method, |
| 828 | + "variables": "ab", |
| 829 | + "cell_weight_ratio": b"xy", |
| 830 | + } |
| 831 | + |
| 832 | + out = _build_diagnostics( |
| 833 | + **_minimal_diagnostics_inputs(), |
| 834 | + model_dict=model, |
| 835 | + ) |
| 836 | + |
| 837 | + glance = out[out["metric"] == "model_glance"].set_index("var")["val"] |
| 838 | + assert np.isnan(float(glance["n_variables"])) |
| 839 | + if method == "poststratify": |
| 840 | + assert np.isnan(float(glance["n_cells"])) |
| 841 | + |
| 842 | + |
| 843 | +def test_build_diagnostics_includes_poststratify_model_glance() -> None: |
| 844 | + model = { |
| 845 | + "method": "poststratify", |
| 846 | + "variables": ["a"], |
| 847 | + "strict_matching": True, |
| 848 | + "cell_weight_ratio": pd.Series([0.5, 2.0], index=["x", "y"]), |
| 849 | + } |
| 850 | + |
| 851 | + out = _build_diagnostics( |
| 852 | + **_minimal_diagnostics_inputs(), |
| 853 | + model_dict=model, |
| 854 | + ) |
| 855 | + |
| 856 | + glance = out[out["metric"] == "model_glance"].set_index("var")["val"] |
| 857 | + assert glance["n_variables"] == 1 |
| 858 | + assert glance["strict_matching"] == 1 |
| 859 | + assert glance["n_cells"] == 2 |
| 860 | + |
| 861 | + |
| 862 | +def test_build_diagnostics_handles_sparse_poststratify_metadata() -> None: |
| 863 | + model = { |
| 864 | + "method": "poststratify", |
| 865 | + "variables": _BrokenLen(), |
| 866 | + "strict_matching": False, |
| 867 | + "cell_weight_ratio": _BrokenLen(), |
| 868 | + } |
| 869 | + |
| 870 | + out = _build_diagnostics( |
| 871 | + **_minimal_diagnostics_inputs(), |
| 872 | + model_dict=model, |
| 873 | + ) |
| 874 | + |
| 875 | + glance = out[out["metric"] == "model_glance"].set_index("var")["val"] |
| 876 | + assert np.isnan(float(glance["n_variables"])) |
| 877 | + assert glance["strict_matching"] == 0 |
| 878 | + assert np.isnan(float(glance["n_cells"])) |
| 879 | + |
| 880 | + |
| 881 | +def test_rake_model_diagnostics_docstring_example_output() -> None: |
| 882 | + diagnostics = pd.DataFrame(columns=["metric", "val", "var"]) |
| 883 | + model = { |
| 884 | + "method": "rake", |
| 885 | + "converged": 1, |
| 886 | + "iterations": pd.DataFrame({"conv": [0.5, 0.01]}), |
| 887 | + "variables": ["gender", "age_group"], |
| 888 | + } |
| 889 | + |
| 890 | + out = _append_rake_model_diagnostics(diagnostics, model) |
| 891 | + |
| 892 | + assert out.to_dict("records") == [ |
| 893 | + {"metric": "model_glance", "val": 1, "var": "converged"}, |
| 894 | + {"metric": "model_glance", "val": 2, "var": "iterations"}, |
| 895 | + {"metric": "model_glance", "val": 0.01, "var": "final_conv"}, |
| 896 | + {"metric": "model_glance", "val": 2, "var": "n_variables"}, |
| 897 | + ] |
| 898 | + |
| 899 | + |
| 900 | +def test_poststratify_model_diagnostics_docstring_example_output() -> None: |
| 901 | + diagnostics = pd.DataFrame(columns=["metric", "val", "var"]) |
| 902 | + model = { |
| 903 | + "method": "poststratify", |
| 904 | + "variables": ["gender", "age_group"], |
| 905 | + "strict_matching": True, |
| 906 | + "cell_weight_ratio": pd.Series([0.5, 2.0]), |
| 907 | + } |
| 908 | + |
| 909 | + out = _append_poststratify_model_diagnostics(diagnostics, model) |
| 910 | + |
| 911 | + assert out.to_dict("records") == [ |
| 912 | + {"metric": "model_glance", "val": 2, "var": "n_variables"}, |
| 913 | + {"metric": "model_glance", "val": 1, "var": "strict_matching"}, |
| 914 | + {"metric": "model_glance", "val": 2, "var": "n_cells"}, |
| 915 | + ] |
0 commit comments