From 6089b64665e1d8453a683dbff55edb78e60937af Mon Sep 17 00:00:00 2001 From: Marty Oehme Date: Mon, 29 Jul 2024 09:48:25 +0200 Subject: [PATCH] chore(script): Refactor study strength bin calc Move calculation into validity modelling to have one source of truth which all in-text representations will use. --- manuscript/article.qmd | 29 +++++++++-------------------- src/model/validity.py | 13 +++++++++++-- 2 files changed, 20 insertions(+), 22 deletions(-) diff --git a/manuscript/article.qmd b/manuscript/article.qmd index 6699ba5..d9b30ef 100644 --- a/manuscript/article.qmd +++ b/manuscript/article.qmd @@ -220,29 +220,18 @@ to better identify areas of strong analytical lenses or areas of more limited an ::: {#tbl-findings-institutional} ```{python} -#| label: tbl-findings-institutional +# | label: tbl-findings-institutional from src.model import validity +from src.model.validity import strength_for # Careful: ruff org imports will remove -study_strength_bins = { - 0.0: r"\-", - 5.0: r"\+", - 10.0: r"\++", -} - - -def strength_for(val): - return list(study_strength_bins.keys())[ - list(study_strength_bins.values()).index(val) - ] - - -findings_institutional = pd.read_csv(f"{g.SUPPLEMENTARY_DATA}/findings-institutional.csv") +findings_institutional = pd.read_csv( + f"{g.SUPPLEMENTARY_DATA}/findings-institutional.csv" +) +fd_df = validity.add_to_findings(findings_institutional, df_by_intervention) outp = Markdown( tabulate( - validity.add_to_findings( - findings_institutional, df_by_intervention, study_strength_bins - )[ + fd_df[ [ "area of policy", "internal_validity", @@ -262,8 +251,8 @@ outp = Markdown( tablefmt="grid", ) ) -del findings_institutional -outp +del findings_institutional, fd_df +outp # type: ignore[ReportUnusedExpression] ``` Note: Each main finding is presented with an internal strength of evidence and an external strength of evidence which describe the combined validities of the evidence base for the respective finding. diff --git a/src/model/validity.py b/src/model/validity.py index 2bdace3..30a7343 100644 --- a/src/model/validity.py +++ b/src/model/validity.py @@ -26,13 +26,22 @@ METHOD_RANKINGS = { 5.0: ["RCT", "randomi(?:s|z)ed.control.trial"], } -VALIDITY_STRENGTH_BINS = { +STRENGTH_BINS: dict[float, str] = { 0.0: r"\-", 5.0: r"\+", 10.0: r"\++", } +def strength_for(val: str): + """Returns the numeric value for a specific strength bin str representation. + + Does a reverse lookup of a strength bin for its representation as a string, + such as looking for '++' and finding 10.0 as its required validity strength. + """ + return list(STRENGTH_BINS.keys())[list(STRENGTH_BINS.values()).index(val)] + + def calculate( df: DataFrame, repr_col: str = "representativeness", @@ -94,7 +103,7 @@ def calculate( def add_to_findings( findings_df: DataFrame, studies_by_intervention: DataFrame, - strength_bins: dict[float, str] | None = None, + strength_bins: dict[float, str] | None = STRENGTH_BINS, ) -> DataFrame: """Returns summary of findings with validities added.