1
0
Fork 0

fix: Refactored saving plots, seperate function for checking for existing plots

This commit is contained in:
Benedikt Bastin 2021-03-23 12:01:51 +01:00
parent bbea5ae532
commit 6ee0666039

112
plot.py
View file

@ -285,23 +285,33 @@ if os.path.isdir(archive_folder):
else: else:
os.mkdir(archive_folder) os.mkdir(archive_folder)
def check_recreate_plot(plot_name):
def save_plot(archive_plot_filename, latest_plot_filename): archive_plot_filename = '{}/{}'.format(archive_folder, plot_name)
plt.savefig(archive_plot_filename + '.pdf')
plt.savefig(archive_plot_filename + '.png')
plt.savefig(latest_plot_filename + '.pdf')
plt.savefig(latest_plot_filename + '.png')
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(): def plot_vaccination_bar_graph_total_time():
archive_plot_filename = '{}/vaccination_bar_graph_total_time'.format(archive_folder) plot_name = 'vaccination_bar_graph_total_time'
latest_plot_filename = '{}/vaccination_bar_graph_total_time'.format(site_folder) if not check_recreate_plot(plot_name):
if os.path.isfile(archive_plot_filename + '.pdf') and not force_renew:
print('Plot {} already exists'.format(archive_plot_filename))
return return
fig, ax = plt.subplots(1) fig, ax = plt.subplots(1)
@ -327,7 +337,7 @@ def plot_vaccination_bar_graph_total_time():
ax.set_xlabel('Datum') ax.set_xlabel('Datum')
ax.set_ylabel('Tägliche Impfungen') ax.set_ylabel('Tägliche Impfungen')
save_plot(archive_plot_filename, latest_plot_filename) save_plot(plot_name)
plt.close() plt.close()
@ -335,11 +345,8 @@ plot_vaccination_bar_graph_total_time()
def plot_vaccination_bar_graph_total_time_by_week(): def plot_vaccination_bar_graph_total_time_by_week():
archive_plot_filename = '{}/vaccination_bar_graph_total_time_by_week'.format(archive_folder) plot_name = 'vaccination_bar_graph_total_time_by_week'
latest_plot_filename = '{}/vaccination_bar_graph_total_time_by_week'.format(site_folder) if not check_recreate_plot(plot_name):
if os.path.isfile(archive_plot_filename + '.pdf') and not force_renew:
print('Plot {} already exists'.format(archive_plot_filename))
return return
fig, ax = plt.subplots(1) fig, ax = plt.subplots(1)
@ -404,18 +411,15 @@ def plot_vaccination_bar_graph_total_time_by_week():
ax.set_xlabel('Datum') ax.set_xlabel('Datum')
ax.set_ylabel('Wöchentliche Impfungen') ax.set_ylabel('Wöchentliche Impfungen')
save_plot(archive_plot_filename, latest_plot_filename) save_plot(plot_name)
plt.close() plt.close()
plot_vaccination_bar_graph_total_time_by_week() plot_vaccination_bar_graph_total_time_by_week()
def plot_vaccination_bar_graph_total_time_two_bars(): def plot_vaccination_bar_graph_total_time_two_bars():
archive_plot_filename = '{}/vaccination_bar_graph_total_time_two_bars'.format(archive_folder) plot_name = 'vaccination_bar_graph_total_time_two_bars'
latest_plot_filename = '{}/vaccination_bar_graph_total_time_two_bars'.format(site_folder) if not check_recreate_plot(plot_name):
if os.path.isfile(archive_plot_filename + '.pdf') and not force_renew:
print('Plot {} already exists'.format(archive_plot_filename))
return return
fig, ax = plt.subplots(1) fig, ax = plt.subplots(1)
@ -444,18 +448,15 @@ def plot_vaccination_bar_graph_total_time_two_bars():
ax.set_xlabel('Datum') ax.set_xlabel('Datum')
ax.set_ylabel('Tägliche Impfungen') ax.set_ylabel('Tägliche Impfungen')
save_plot(archive_plot_filename, latest_plot_filename) save_plot(plot_name)
plt.close() plt.close()
plot_vaccination_bar_graph_total_time_two_bars() plot_vaccination_bar_graph_total_time_two_bars()
def plot_vaccination_bar_graph_compare_both_vaccinations(): def plot_vaccination_bar_graph_compare_both_vaccinations():
archive_plot_filename = '{}/vaccination_bar_graph_compare_both_vaccinations'.format(archive_folder) plot_name = 'vaccination_bar_graph_compare_both_vaccinations'
latest_plot_filename = '{}/vaccination_bar_graph_compare_both_vaccinations'.format(site_folder) if not check_recreate_plot(plot_name):
if os.path.isfile(archive_plot_filename + '.pdf') and not force_renew:
print('Plot {} already exists'.format(archive_plot_filename))
return return
fig, ax = plt.subplots(1) fig, ax = plt.subplots(1)
@ -485,17 +486,15 @@ def plot_vaccination_bar_graph_compare_both_vaccinations():
ax.set_xlabel('Datum') ax.set_xlabel('Datum')
ax.set_ylabel('Tägliche Impfungen') ax.set_ylabel('Tägliche Impfungen')
save_plot(archive_plot_filename, latest_plot_filename) save_plot(plot_name)
plt.close() plt.close()
plot_vaccination_bar_graph_compare_both_vaccinations() plot_vaccination_bar_graph_compare_both_vaccinations()
def plot_cumulative_two_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: plot_name = 'cumulative_two_vaccinations'
print('Plot {} already exists'.format(archive_plot_filename)) if not check_recreate_plot(plot_name):
return return
fig, ax = plt.subplots(1) fig, ax = plt.subplots(1)
@ -525,18 +524,16 @@ def plot_cumulative_two_vaccinations():
ax.set_xlabel('Datum') ax.set_xlabel('Datum')
ax.set_ylabel('Kumulative Impfungen') ax.set_ylabel('Kumulative Impfungen')
save_plot(archive_plot_filename, latest_plot_filename) save_plot(plot_name)
plt.close() plt.close()
plot_cumulative_two_vaccinations() plot_cumulative_two_vaccinations()
def plot_cumulative_two_vaccinations_percentage(): 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: plot_name = 'cumulative_two_vaccinations_percentage'
print('Plot {} already exists'.format(archive_plot_filename)) if not check_recreate_plot(plot_name):
return return
fig, ax = plt.subplots(1) fig, ax = plt.subplots(1)
@ -567,7 +564,7 @@ def plot_cumulative_two_vaccinations_percentage():
ax.set_xlabel('Datum') ax.set_xlabel('Datum')
ax.set_ylabel('Kumulative Impfungen') ax.set_ylabel('Kumulative Impfungen')
save_plot(archive_plot_filename, latest_plot_filename) save_plot(plot_name)
plt.close() plt.close()
@ -575,11 +572,9 @@ plot_cumulative_two_vaccinations_percentage()
def plot_people_between_first_and_second(): 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: plot_name = 'people_between_first_and_second'
print('Plot {} already exists'.format(archive_plot_filename)) if not check_recreate_plot(plot_name):
return return
fig, ax = plt.subplots(1) fig, ax = plt.subplots(1)
@ -611,7 +606,7 @@ def plot_people_between_first_and_second():
ax.set_xlabel('Datum') ax.set_xlabel('Datum')
ax.set_ylabel('Personen zwischen Erst- und Zweitimpfung') ax.set_ylabel('Personen zwischen Erst- und Zweitimpfung')
save_plot(archive_plot_filename, latest_plot_filename) save_plot(plot_name)
plt.close() plt.close()
@ -619,11 +614,8 @@ plot_people_between_first_and_second()
def plot_vaccination_rate(): def plot_vaccination_rate():
archive_plot_filename = '{}/vaccination_rate'.format(archive_folder) plot_name = 'vaccination_rate'
latest_plot_filename = '{}/vaccination_rate'.format(site_folder) if not check_recreate_plot(plot_name):
if os.path.isfile(archive_plot_filename + '.pdf') and not force_renew:
print('Plot {} already exists'.format(archive_plot_filename))
return return
fig, ax = plt.subplots(1) fig, ax = plt.subplots(1)
@ -656,18 +648,15 @@ def plot_vaccination_rate():
ax.set_xlabel('Datum') ax.set_xlabel('Datum')
ax.set_ylabel('Impfrate [Impfungen/Tag]') ax.set_ylabel('Impfrate [Impfungen/Tag]')
save_plot(archive_plot_filename, latest_plot_filename) save_plot(plot_name)
plt.close() plt.close()
plot_vaccination_rate() plot_vaccination_rate()
def plot_vaccination_done_days(): def plot_vaccination_done_days():
archive_plot_filename = '{}/vaccination_done_days'.format(archive_folder) plot_name = 'vaccination_done_days'
latest_plot_filename = '{}/vaccination_done_days'.format(site_folder) if not check_recreate_plot(plot_name):
if os.path.isfile(archive_plot_filename + '.pdf') and not force_renew:
print('Plot {} already exists'.format(archive_plot_filename))
return return
fig, ax = plt.subplots(1) fig, ax = plt.subplots(1)
@ -698,18 +687,15 @@ def plot_vaccination_done_days():
ax.set_xlabel('Datum') ax.set_xlabel('Datum')
ax.set_ylabel('Tage, bis 70 % erreicht sind') ax.set_ylabel('Tage, bis 70 % erreicht sind')
save_plot(archive_plot_filename, latest_plot_filename) save_plot(plot_name)
plt.close() plt.close()
plot_vaccination_done_days() plot_vaccination_done_days()
def plot_vaccination_done_dates(): def plot_vaccination_done_dates():
archive_plot_filename = '{}/vaccination_done_dates'.format(archive_folder) plot_name = 'vaccination_done_dates'
latest_plot_filename = '{}/vaccination_done_dates'.format(site_folder) if not check_recreate_plot(plot_name):
if os.path.isfile(archive_plot_filename + '.pdf') and not force_renew:
print('Plot {} already exists'.format(archive_plot_filename))
return return
fig, ax = plt.subplots(1) fig, ax = plt.subplots(1)
@ -750,7 +736,7 @@ def plot_vaccination_done_dates():
ax.set_xlabel('Datum') ax.set_xlabel('Datum')
ax.set_ylabel('Datum, an dem 70 % erreicht sind') ax.set_ylabel('Datum, an dem 70 % erreicht sind')
save_plot(archive_plot_filename, latest_plot_filename) save_plot(plot_name)
plt.close() plt.close()
plot_vaccination_done_dates() plot_vaccination_done_dates()