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.
This commit is contained in:
Marty Oehme 2024-07-29 09:48:25 +02:00
parent 07d3efbcd7
commit 6089b64665
Signed by: Marty
GPG key ID: EDBF2ED917B2EF6A
2 changed files with 20 additions and 22 deletions

View file

@ -222,27 +222,16 @@ to better identify areas of strong analytical lenses or areas of more limited an
```{python} ```{python}
# | label: tbl-findings-institutional # | label: tbl-findings-institutional
from src.model import validity from src.model import validity
from src.model.validity import strength_for # Careful: ruff org imports will remove
study_strength_bins = { findings_institutional = pd.read_csv(
0.0: r"\-", f"{g.SUPPLEMENTARY_DATA}/findings-institutional.csv"
5.0: r"\+", )
10.0: r"\++", fd_df = validity.add_to_findings(findings_institutional, df_by_intervention)
}
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")
outp = Markdown( outp = Markdown(
tabulate( tabulate(
validity.add_to_findings( fd_df[
findings_institutional, df_by_intervention, study_strength_bins
)[
[ [
"area of policy", "area of policy",
"internal_validity", "internal_validity",
@ -262,8 +251,8 @@ outp = Markdown(
tablefmt="grid", tablefmt="grid",
) )
) )
del findings_institutional del findings_institutional, fd_df
outp 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. 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.

View file

@ -26,13 +26,22 @@ METHOD_RANKINGS = {
5.0: ["RCT", "randomi(?:s|z)ed.control.trial"], 5.0: ["RCT", "randomi(?:s|z)ed.control.trial"],
} }
VALIDITY_STRENGTH_BINS = { STRENGTH_BINS: dict[float, str] = {
0.0: r"\-", 0.0: r"\-",
5.0: r"\+", 5.0: r"\+",
10.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( def calculate(
df: DataFrame, df: DataFrame,
repr_col: str = "representativeness", repr_col: str = "representativeness",
@ -94,7 +103,7 @@ def calculate(
def add_to_findings( def add_to_findings(
findings_df: DataFrame, findings_df: DataFrame,
studies_by_intervention: DataFrame, studies_by_intervention: DataFrame,
strength_bins: dict[float, str] | None = None, strength_bins: dict[float, str] | None = STRENGTH_BINS,
) -> DataFrame: ) -> DataFrame:
"""Returns summary of findings with validities added. """Returns summary of findings with validities added.