Skip to content

Commit

Permalink
test handle_edge_case_percentages
Browse files Browse the repository at this point in the history
As part of this handle descriptions without match top 5
  • Loading branch information
LFISHER7 committed Aug 10, 2023
1 parent 6f6c093 commit 00a927c
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 0 deletions.
9 changes: 9 additions & 0 deletions interactive_templates/templates/v2/analysis/top_5.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,11 +88,20 @@ def calculate_proportion(event_counts):


def add_description(event_counts, code_df, code_column, term_column):
if code_df.empty:
event_counts["Description"] = "-"
return event_counts

code_df = code_df.set_index(code_column).rename(
columns={term_column: "Description"}
)

event_counts = event_counts.set_index(code_column).join(code_df).reset_index()
event_counts.loc[event_counts[code_column] == "Other", "Description"] = "-"

# For codes that did not find a match in code_df set a default value
event_counts["Description"].fillna("-", inplace=True)

return event_counts


Expand Down
17 changes: 17 additions & 0 deletions interactive_templates/templates/v2/tests/test_top_5.py
Original file line number Diff line number Diff line change
Expand Up @@ -139,3 +139,20 @@ def test_add_description(event_counts, code_df):

# Ensure that no rows were lost
assert len(result) == len(event_counts)


@given(df=df_strategy)
def test_handle_edge_case_percentages(df):
df_with_proportions = calculate_proportion(df.copy())
result_df = handle_edge_case_percentages(df_with_proportions.copy())

for _, row in result_df.iterrows():
if (row["Proportion of codes (%)"] == 0) and (row["num"] > 0):
assert (
row["Proportion of codes (%)"] == "<0.001"
), f"Expected '<0.001' but got {row['Proportion of codes (%)']} for num {row['num']}"

if (row["Proportion of codes (%)"] == 100) and (row["num"] < df["num"].sum()):
assert (
row["Proportion of codes (%)"] == ">99.99"
), f"Expected '>99.99' but got {row['Proportion of codes (%)']} for num {row['num']}"

0 comments on commit 00a927c

Please sign in to comment.