create tmp plots and move them after generation (does not affect html plots)

This commit is contained in:
rmanach 2021-12-29 11:14:51 +01:00
parent 1b581603c7
commit fae3ca91ae

View File

@ -2,6 +2,7 @@ import argparse
import json import json
import logging import logging
import os import os
import re
from datetime import datetime as dt from datetime import datetime as dt
from enum import Enum from enum import Enum
from functools import partial from functools import partial
@ -21,10 +22,17 @@ DATE_FORMAT = "%Y-%m-%d"
DATA_URL = "https://data.drees.solidarites-sante.gouv.fr/api/records/1.0/search/?dataset=covid-19-resultats-par-age-issus-des-appariements-entre-si-vic-si-dep-et-vac-si&q=&rows=-1&facet=date&facet=vac_statut&facet=age" DATA_URL = "https://data.drees.solidarites-sante.gouv.fr/api/records/1.0/search/?dataset=covid-19-resultats-par-age-issus-des-appariements-entre-si-vic-si-dep-et-vac-si&q=&rows=-1&facet=date&facet=vac_statut&facet=age"
DATA_REPOSITORY = "data" DATA_REPOSITORY = "data"
STATIC_REPOSITORY = "static" STATIC_REPOSITORY = "static"
OUTPUT_REPOSITORY = os.path.join(STATIC_REPOSITORY, "plots") OUTPUT_REPOSITORY = os.path.join(STATIC_REPOSITORY, "plots")
BUILD_REPOSITORY = "build" BUILD_REPOSITORY = "build"
TMP_SUFFIX = ".tmp"
FORMAT_SUFFIX = ".png"
OUTPUT_SUFFIX = f"{TMP_SUFFIX}{FORMAT_SUFFIX}"
TMP_FILE_REGEX = re.compile(r"^.*{}$".format(OUTPUT_SUFFIX.replace(".", "\.")))
MAIN_URL = "https://covid.thegux.fr/" MAIN_URL = "https://covid.thegux.fr/"
# cycler could be better, but for ages plots it's ok # cycler could be better, but for ages plots it's ok
@ -270,7 +278,7 @@ def save_and_close_fig(
plt.legend() plt.legend()
if is_tight: if is_tight:
plt.tight_layout() plt.tight_layout()
plt.savefig(output_path) plt.savefig(f"{output_path}{OUTPUT_SUFFIX}")
plt.close(fig) plt.close(fig)
logging.info(f"{output_path} plotted") logging.info(f"{output_path} plotted")
@ -499,6 +507,16 @@ def get_age_field_args() -> List[Tuple[AgeGroup, Field]]:
return pool_args return pool_args
def move_tmp_plots() -> None:
"""
move .tmp.png plots into .png after generation
"""
for filename in os.listdir(OUTPUT_REPOSITORY):
file_path = os.path.join(OUTPUT_REPOSITORY, filename)
if re.match(TMP_FILE_REGEX, filename):
os.rename(file_path, file_path.replace(OUTPUT_SUFFIX, FORMAT_SUFFIX))
def generate_html_page(np_date: np.ndarray) -> None: def generate_html_page(np_date: np.ndarray) -> None:
logging.info("generating html page with plots...") logging.info("generating html page with plots...")
os.makedirs(BUILD_REPOSITORY, exist_ok=True) os.makedirs(BUILD_REPOSITORY, exist_ok=True)
@ -515,7 +533,7 @@ def generate_html_page(np_date: np.ndarray) -> None:
"status": VacStatus, "status": VacStatus,
"static": os.path.join(MAIN_URL, STATIC_REPOSITORY), "static": os.path.join(MAIN_URL, STATIC_REPOSITORY),
"src": DATA_URL, "src": DATA_URL,
"period": f"{date_start} -> {date_end}" "period": f"{date_start} -> {date_end}",
} }
) )
with open(os.path.join(BUILD_REPOSITORY, "index.html"), "w") as f: with open(os.path.join(BUILD_REPOSITORY, "index.html"), "w") as f:
@ -585,5 +603,7 @@ if __name__ == "__main__":
plot_cumulative_field(np_data, np_date, field) plot_cumulative_field(np_data, np_date, field)
plot_bar_age_percent_by_field(np_data, np_date, field) plot_bar_age_percent_by_field(np_data, np_date, field)
move_tmp_plots()
if args.to_html: if args.to_html:
generate_html_page(np_date) generate_html_page(np_date)