refactor(code): Move finding table validities into model module
All findings tables can use the validities functionality to add strength of evidence (internal/external) to themselves. Generalized the function to work for any main findings csv (to dataframe) table not just institional findings.
This commit is contained in:
parent
5e405e05d7
commit
953720ce54
2 changed files with 55 additions and 22 deletions
51
src/model/strength_of_findings.py
Normal file
51
src/model/strength_of_findings.py
Normal file
|
|
@ -0,0 +1,51 @@
|
|||
import math
|
||||
|
||||
from pandas import DataFrame
|
||||
|
||||
from src import prep_data
|
||||
|
||||
|
||||
def _binned_strength(strength: float) -> str:
|
||||
if strength < 3.0:
|
||||
return r"\-"
|
||||
if strength < 6.0:
|
||||
return r"\+"
|
||||
return r"\++"
|
||||
|
||||
|
||||
def _combined_validities(
|
||||
apply_to: DataFrame, by_intervention: DataFrame, column: str = "internal_validity"
|
||||
):
|
||||
if not isinstance(apply_to, str):
|
||||
return
|
||||
combined = 0.0
|
||||
for study in apply_to.split(";"):
|
||||
new = by_intervention.loc[by_intervention["citation"] == study, column]
|
||||
if len(new) > 0 and not math.isnan(new.iat[0]):
|
||||
combined += new.iat[0]
|
||||
if combined:
|
||||
return _binned_strength(combined)
|
||||
return r"\-"
|
||||
|
||||
|
||||
def add_validities(
|
||||
findings_df: DataFrame, studies_by_intervention: DataFrame
|
||||
) -> DataFrame:
|
||||
valid_subset = (
|
||||
prep_data.calculate_validities(studies_by_intervention)[
|
||||
["internal_validity", "external_validity", "citation"]
|
||||
]
|
||||
.fillna(1.0)
|
||||
.drop_duplicates(subset=["citation"])
|
||||
.sort_values("internal_validity")
|
||||
)
|
||||
|
||||
def apply_internal(df):
|
||||
return _combined_validities(df, valid_subset, "internal_validity")
|
||||
|
||||
def apply_external(df):
|
||||
return _combined_validities(df, valid_subset, "external_validity")
|
||||
|
||||
findings_df["internal_validity"] = findings_df["studies"].apply(apply_internal)
|
||||
findings_df["external_validity"] = findings_df["studies"].apply(apply_external)
|
||||
return findings_df
|
||||
Loading…
Add table
Add a link
Reference in a new issue