Skip to content

Commit

Permalink
plot percentages clin + med over time
Browse files Browse the repository at this point in the history
  • Loading branch information
LFISHER7 committed Mar 28, 2024
1 parent c044c56 commit c5c2c63
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 0 deletions.
56 changes: 56 additions & 0 deletions analysis/report/pcnt_table.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
import argparse
import pandas
import fnmatch
import re
import matplotlib.dates as mdates
import matplotlib.pyplot as plt

from report_utils import ci_95_proportion, ci_to_str

Expand Down Expand Up @@ -38,6 +41,48 @@ def subset_table(measure_table, measures_pattern, date):

return table_subset

def plot_pcnt_over_time(measure_table, output_dir, type):
"""
Plot the percentage over time for each measure.
"""
measure_table['date'] = pandas.to_datetime(measure_table['date'])
measure_table['value'] *= 100


valid_dates = measure_table.dropna(subset=['value'])['date']
start_date = valid_dates.min()
end_date = valid_dates.max()
measure_table = measure_table[(measure_table['date'] >= start_date) & (measure_table['date'] <= end_date)]

plt.figure(figsize=(10, 6))
for name, group in measure_table.groupby('name'):

legend_label = name

if type == "clinical":
match = re.search(r"event_(\w+)_with_clinical_any_pcnt", name)
if match:
legend_label = match.group(1).title()
elif type == "medication":
match = re.search(r"event_(\w+)_with_medication_any_pcnt", name)
if match:
legend_label = match.group(1).title()

plt.plot(group['date'], group['value'], label=legend_label)

plt.xlabel('Date')
plt.ylabel('Percentage')
plt.title(f'Percentage with {type} event')
plt.legend(loc='center left', bbox_to_anchor=(1, 0.5), fontsize='small')
plt.xticks(rotation=90)
plt.grid(True)
plt.ylim(bottom=0)
plt.xlim(start_date, end_date)
plt.gca().xaxis.set_major_locator(mdates.MonthLocator())
plt.gca().xaxis.set_major_formatter(mdates.DateFormatter('%b-%Y'))
plt.tight_layout()
plt.savefig(output_dir / f"pcnt_over_time_{type}.jpg", dpi=300)
plt.close()

def match_paths(files, pattern):
return fnmatch.filter(files, pattern)
Expand Down Expand Up @@ -106,6 +151,17 @@ def main():
table = pandas.DataFrame(joined)
table.to_html(output_dir / "pcnt_with_indication.html", index=True)

filtered_table_med = measure_table[
measure_table['name'].str.contains('with_medication_any_pcnt')
]
plot_pcnt_over_time(filtered_table_med, output_dir, "medication")

filtered_table_clinical = measure_table[
measure_table['name'].str.contains('with_clinical_any_pcnt')
]
plot_pcnt_over_time(filtered_table_clinical, output_dir, "clinical")



if __name__ == "__main__":
main()
2 changes: 2 additions & 0 deletions project.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -710,3 +710,5 @@ actions:
# outputs:
# moderately_sensitive:
# tables: released_outputs/output/report/results/paper/pcnt_with_indication.html
# figure: released_outputs/output/report/results/paper/pcnt_over_time*.jpg

0 comments on commit c5c2c63

Please sign in to comment.