Skip to content

Commit

Permalink
Merge pull request #82 from opensafely/paper-fixes
Browse files Browse the repository at this point in the history
Paper fixes
  • Loading branch information
ccunningham101 committed Sep 18, 2023
2 parents c184925 + 366fd4a commit 3739d68
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 12 deletions.
2 changes: 1 addition & 1 deletion analysis/report/panel_plots.py
Original file line number Diff line number Diff line change
Expand Up @@ -426,7 +426,7 @@ def get_group_chart(
)
# Save the season table only if there is more than one group
more_than_one_group = panel_group_data.group.nunique() > 1
if (produce_season_table or mark_seasons) and more_than_one_group:
if (produce_season_table and more_than_one_group) or mark_seasons:
season_table = make_season_table(
panel_group_data.set_index("date"),
"group",
Expand Down
2 changes: 1 addition & 1 deletion analysis/report/plot_measures_report.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ def plot_measures(
df_copy, category, column_to_plot, output_dir, filename
)
# TODO: check whether this works for weekly
if season_table and mark_seasons:
if not season_table.empty and mark_seasons:
annotate_seasons(season_table, column_to_plot, ax)

elif frequency == "week":
Expand Down
15 changes: 9 additions & 6 deletions analysis/report/report_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -356,14 +356,17 @@ def make_season_table(df, category, column_to_plot, output_dir, filename=None):
table.numerator = pd.to_numeric(table.numerator, errors="coerce")
table.denominator = pd.to_numeric(table.denominator, errors="coerce")
except ValueError:
return None
if filename:
# If too much is redacted, we cannot make the table
table = pd.DataFrame()
formatted = pd.DataFrame()
# If too much is redacted, we cannot make the table
# But project yaml might expect existance of html
if filename and not table.empty:
try:
formatted = format_season_table(table, column_to_plot, category)
except KeyError:
return None
formatted = reorder_dashes(formatted, level=0)
formatted = reorder_dashes(formatted, level=0)
except:
formatted = pd.DataFrame()
if filename:
formatted.to_html(output_dir / f"{filename}_table.html")
return table

Expand Down
18 changes: 17 additions & 1 deletion analysis/report/top_5_report.py
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,7 @@ def get_proportion_of_events(df, code, date):


def plot_top_codes_over_time(
code_df, top_codes, measure, output_dir, frequency
code_df, top_codes, measure, output_dir, frequency, xtick_frequency=1
):
"""
Plots the top 5 codes over time for each measure.
Expand Down Expand Up @@ -287,6 +287,9 @@ def plot_top_codes_over_time(
)
ax.set_xticks(xticks)
ax.set_xticklabels([x.strftime("%B %Y") for x in xticks])
for index, label in enumerate(ax.xaxis.get_ticklabels()):
if index % xtick_frequency != 0:
label.set_visible(False)
elif frequency == "week":
xticks = pd.date_range(
start=code_proportions["date"].min(),
Expand Down Expand Up @@ -319,6 +322,7 @@ def main():
input_file = args.input_file
output_dir = args.output_dir
frequency = args.frequency
xtick_frequency = args.xtick_frequency
base_fontsize = args.base_fontsize

output_dir.mkdir(parents=True, exist_ok=True)
Expand All @@ -327,6 +331,11 @@ def main():
measure_table = get_measure_tables(input_file)

all_codes = {**MEDICATION_TO_CODELIST, **CLINICAL_TO_CODELIST}
if frequency == "month":
for med, codelist in MEDICATION_TO_CODELIST.items():
all_codes[f"{med}_with_clinical_any"] = codelist
for clin, codelist in CLINICAL_TO_CODELIST.items():
all_codes[f"{clin}_with_medication_any"] = codelist

for key, codelist in all_codes.items():
measure = f"event_code_{key}_rate"
Expand Down Expand Up @@ -404,6 +413,7 @@ def main():
measure=measure,
output_dir=output_dir,
frequency=frequency,
xtick_frequency=xtick_frequency,
)


Expand All @@ -426,6 +436,12 @@ def parse_args():
choices=["month", "week"],
help="The frequency of the data",
)
parser.add_argument(
"--xtick-frequency",
help="Display every nth xtick",
type=int,
default=1,
)
parser.add_argument(
"--base-fontsize",
help="Default text size",
Expand Down
14 changes: 14 additions & 0 deletions analysis/study_definition_report.py
Original file line number Diff line number Diff line change
Expand Up @@ -365,6 +365,13 @@ def generate_expectations_codes(codelist, incidence=0.5):
group_by=["population"],
small_number_suppression=True,
),
Measure(
id=f"event_code_{medication_key}_with_clinical_any_rate",
numerator=f"{medication_key}_with_clinical_any",
denominator="population",
group_by=[f"event_code_{medication_key}"],
small_number_suppression=False,
),
]
)

Expand Down Expand Up @@ -433,6 +440,13 @@ def generate_expectations_codes(codelist, incidence=0.5):
group_by=["population"],
small_number_suppression=True,
),
Measure(
id=f"event_code_{clinical_key}_with_medication_any_rate",
numerator=f"{clinical_key}_with_medication_any",
denominator="population",
group_by=[f"event_code_{clinical_key}"],
small_number_suppression=False,
),
]
)

Expand Down
8 changes: 5 additions & 3 deletions project.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ actions:
needs: [curation_weekly]
outputs:
moderately_sensitive:
measure_csv: output/curation/measure_event_*.csv
measure_csv: output/curation/measure_event_*_rate.csv

curation_diagnostics:
run: cohortextractor:latest generate_cohort
Expand Down Expand Up @@ -267,6 +267,7 @@ actions:
--input-file output/report/results/measure_unrounded.csv
--output-dir output/report/results
--frequency month
--xtick-frequency 2
needs: [join_measures_unrounded]
outputs:
moderately_sensitive:
Expand Down Expand Up @@ -637,15 +638,16 @@ actions:
--output-name "phenoxymethylpenicillin_with_clinical_any_by_subgroup"
--exclude-group "Missing"
--scale "rate"
--xtick-frequency 3
--xtick-frequency 2
--produce-season-table
--base-fontsize 12
--columns 1
needs: [join_measures_rounded]
outputs:
moderately_sensitive:
measure: output/report/results/paper/phenoxymethylpenicillin_with_clinical_any_by_subgroup.png
tables: output/report/results/paper/event_phenoxymethylpenicillin_with_clinical_any_age_band_rate_table.html
table_age: output/report/results/paper/event_phenoxymethylpenicillin_with_clinical_any_age_band_rate_table.html
table_region: output/report/results/paper/event_phenoxymethylpenicillin_with_clinical_any_region_rate_table.html

supplemental_antibiotics_groups:
run: >
Expand Down

0 comments on commit 3739d68

Please sign in to comment.