From 6ee0666039a00247b38c5fd0811d54e52c38daec Mon Sep 17 00:00:00 2001 From: Benedikt Bastin Date: Tue, 23 Mar 2021 12:01:51 +0100 Subject: [PATCH] fix: Refactored saving plots, seperate function for checking for existing plots --- plot.py | 112 +++++++++++++++++++++++++------------------------------- 1 file changed, 49 insertions(+), 63 deletions(-) diff --git a/plot.py b/plot.py index 977ba1a..7c7ec4a 100644 --- a/plot.py +++ b/plot.py @@ -285,23 +285,33 @@ if os.path.isdir(archive_folder): else: os.mkdir(archive_folder) +def check_recreate_plot(plot_name): -def save_plot(archive_plot_filename, latest_plot_filename): - plt.savefig(archive_plot_filename + '.pdf') - plt.savefig(archive_plot_filename + '.png') - plt.savefig(latest_plot_filename + '.pdf') - plt.savefig(latest_plot_filename + '.png') + archive_plot_filename = '{}/{}'.format(archive_folder, plot_name) - print('Created plot {} as pdf and png'.format(archive_plot_filename)) + if os.path.isfile(archive_plot_filename + '.pdf') and not force_renew: + print('Plot {} already exists'.format(plot_name)) + return False + + return True + +def save_plot(plot_name): + + folders = [archive_folder, site_folder] + file_formats = ['pdf', 'png'] + file_template = '{folder}/{plot_name}.{format}' + + for folder in folders: + for format in file_formats: + plt.savefig(file_template.format(folder=folder, plot_name=plot_name, format=format)) + + print('Created plot {} as {}'.format(plot_name, file_formats)) def plot_vaccination_bar_graph_total_time(): - archive_plot_filename = '{}/vaccination_bar_graph_total_time'.format(archive_folder) - latest_plot_filename = '{}/vaccination_bar_graph_total_time'.format(site_folder) - - if os.path.isfile(archive_plot_filename + '.pdf') and not force_renew: - print('Plot {} already exists'.format(archive_plot_filename)) + plot_name = 'vaccination_bar_graph_total_time' + if not check_recreate_plot(plot_name): return fig, ax = plt.subplots(1) @@ -327,7 +337,7 @@ def plot_vaccination_bar_graph_total_time(): ax.set_xlabel('Datum') ax.set_ylabel('Tägliche Impfungen') - save_plot(archive_plot_filename, latest_plot_filename) + save_plot(plot_name) plt.close() @@ -335,11 +345,8 @@ plot_vaccination_bar_graph_total_time() def plot_vaccination_bar_graph_total_time_by_week(): - archive_plot_filename = '{}/vaccination_bar_graph_total_time_by_week'.format(archive_folder) - latest_plot_filename = '{}/vaccination_bar_graph_total_time_by_week'.format(site_folder) - - if os.path.isfile(archive_plot_filename + '.pdf') and not force_renew: - print('Plot {} already exists'.format(archive_plot_filename)) + plot_name = 'vaccination_bar_graph_total_time_by_week' + if not check_recreate_plot(plot_name): return fig, ax = plt.subplots(1) @@ -404,18 +411,15 @@ def plot_vaccination_bar_graph_total_time_by_week(): ax.set_xlabel('Datum') ax.set_ylabel('Wöchentliche Impfungen') - save_plot(archive_plot_filename, latest_plot_filename) + save_plot(plot_name) plt.close() plot_vaccination_bar_graph_total_time_by_week() def plot_vaccination_bar_graph_total_time_two_bars(): - archive_plot_filename = '{}/vaccination_bar_graph_total_time_two_bars'.format(archive_folder) - latest_plot_filename = '{}/vaccination_bar_graph_total_time_two_bars'.format(site_folder) - - if os.path.isfile(archive_plot_filename + '.pdf') and not force_renew: - print('Plot {} already exists'.format(archive_plot_filename)) + plot_name = 'vaccination_bar_graph_total_time_two_bars' + if not check_recreate_plot(plot_name): return fig, ax = plt.subplots(1) @@ -444,18 +448,15 @@ def plot_vaccination_bar_graph_total_time_two_bars(): ax.set_xlabel('Datum') ax.set_ylabel('Tägliche Impfungen') - save_plot(archive_plot_filename, latest_plot_filename) + save_plot(plot_name) plt.close() plot_vaccination_bar_graph_total_time_two_bars() def plot_vaccination_bar_graph_compare_both_vaccinations(): - archive_plot_filename = '{}/vaccination_bar_graph_compare_both_vaccinations'.format(archive_folder) - latest_plot_filename = '{}/vaccination_bar_graph_compare_both_vaccinations'.format(site_folder) - - if os.path.isfile(archive_plot_filename + '.pdf') and not force_renew: - print('Plot {} already exists'.format(archive_plot_filename)) + plot_name = 'vaccination_bar_graph_compare_both_vaccinations' + if not check_recreate_plot(plot_name): return fig, ax = plt.subplots(1) @@ -485,17 +486,15 @@ def plot_vaccination_bar_graph_compare_both_vaccinations(): ax.set_xlabel('Datum') ax.set_ylabel('Tägliche Impfungen') - save_plot(archive_plot_filename, latest_plot_filename) + save_plot(plot_name) plt.close() plot_vaccination_bar_graph_compare_both_vaccinations() def plot_cumulative_two_vaccinations(): - archive_plot_filename = '{}/cumulative_two_vaccinations'.format(archive_folder) - latest_plot_filename = '{}/cumulative_two_vaccinations'.format(site_folder) - if os.path.isfile(archive_plot_filename + '.pdf') and not force_renew: - print('Plot {} already exists'.format(archive_plot_filename)) + plot_name = 'cumulative_two_vaccinations' + if not check_recreate_plot(plot_name): return fig, ax = plt.subplots(1) @@ -525,18 +524,16 @@ def plot_cumulative_two_vaccinations(): ax.set_xlabel('Datum') ax.set_ylabel('Kumulative Impfungen') - save_plot(archive_plot_filename, latest_plot_filename) + save_plot(plot_name) plt.close() plot_cumulative_two_vaccinations() def plot_cumulative_two_vaccinations_percentage(): - archive_plot_filename = '{}/cumulative_two_vaccinations_percentage'.format(archive_folder) - latest_plot_filename = '{}/cumulative_two_vaccinations_percentage'.format(site_folder) - if os.path.isfile(archive_plot_filename + '.pdf') and not force_renew: - print('Plot {} already exists'.format(archive_plot_filename)) + plot_name = 'cumulative_two_vaccinations_percentage' + if not check_recreate_plot(plot_name): return fig, ax = plt.subplots(1) @@ -567,7 +564,7 @@ def plot_cumulative_two_vaccinations_percentage(): ax.set_xlabel('Datum') ax.set_ylabel('Kumulative Impfungen') - save_plot(archive_plot_filename, latest_plot_filename) + save_plot(plot_name) plt.close() @@ -575,11 +572,9 @@ plot_cumulative_two_vaccinations_percentage() def plot_people_between_first_and_second(): - archive_plot_filename = '{}/people_between_first_and_second'.format(archive_folder) - latest_plot_filename = '{}/people_between_first_and_second'.format(site_folder) - if os.path.isfile(archive_plot_filename + '.pdf') and not force_renew: - print('Plot {} already exists'.format(archive_plot_filename)) + plot_name = 'people_between_first_and_second' + if not check_recreate_plot(plot_name): return fig, ax = plt.subplots(1) @@ -611,7 +606,7 @@ def plot_people_between_first_and_second(): ax.set_xlabel('Datum') ax.set_ylabel('Personen zwischen Erst- und Zweitimpfung') - save_plot(archive_plot_filename, latest_plot_filename) + save_plot(plot_name) plt.close() @@ -619,11 +614,8 @@ plot_people_between_first_and_second() def plot_vaccination_rate(): - archive_plot_filename = '{}/vaccination_rate'.format(archive_folder) - latest_plot_filename = '{}/vaccination_rate'.format(site_folder) - - if os.path.isfile(archive_plot_filename + '.pdf') and not force_renew: - print('Plot {} already exists'.format(archive_plot_filename)) + plot_name = 'vaccination_rate' + if not check_recreate_plot(plot_name): return fig, ax = plt.subplots(1) @@ -656,18 +648,15 @@ def plot_vaccination_rate(): ax.set_xlabel('Datum') ax.set_ylabel('Impfrate [Impfungen/Tag]') - save_plot(archive_plot_filename, latest_plot_filename) + save_plot(plot_name) plt.close() plot_vaccination_rate() def plot_vaccination_done_days(): - archive_plot_filename = '{}/vaccination_done_days'.format(archive_folder) - latest_plot_filename = '{}/vaccination_done_days'.format(site_folder) - - if os.path.isfile(archive_plot_filename + '.pdf') and not force_renew: - print('Plot {} already exists'.format(archive_plot_filename)) + plot_name = 'vaccination_done_days' + if not check_recreate_plot(plot_name): return fig, ax = plt.subplots(1) @@ -698,18 +687,15 @@ def plot_vaccination_done_days(): ax.set_xlabel('Datum') ax.set_ylabel('Tage, bis 70 % erreicht sind') - save_plot(archive_plot_filename, latest_plot_filename) + save_plot(plot_name) plt.close() plot_vaccination_done_days() def plot_vaccination_done_dates(): - archive_plot_filename = '{}/vaccination_done_dates'.format(archive_folder) - latest_plot_filename = '{}/vaccination_done_dates'.format(site_folder) - - if os.path.isfile(archive_plot_filename + '.pdf') and not force_renew: - print('Plot {} already exists'.format(archive_plot_filename)) + plot_name = 'vaccination_done_dates' + if not check_recreate_plot(plot_name): return fig, ax = plt.subplots(1) @@ -750,7 +736,7 @@ def plot_vaccination_done_dates(): ax.set_xlabel('Datum') ax.set_ylabel('Datum, an dem 70 % erreicht sind') - save_plot(archive_plot_filename, latest_plot_filename) + save_plot(plot_name) plt.close() plot_vaccination_done_dates()