diff --git a/drees.py b/drees.py index aa3bbe9..3785ded 100644 --- a/drees.py +++ b/drees.py @@ -3,11 +3,12 @@ import json import logging import os import re +from collections import namedtuple from datetime import datetime as dt from enum import Enum from functools import partial from multiprocessing import Pool -from typing import Any, Dict, List, Optional, OrderedDict, Tuple +from typing import Any, Dict, List, Optional, OrderedDict, Tuple, Union import numpy as np import requests @@ -33,7 +34,8 @@ 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/" +MAIN_URL = "/home/romain/code/covid-plotter/" # cycler could be better, but for ages plots it's ok AGE_COLORS = { @@ -79,6 +81,10 @@ class AgeGroup(DreesEnum): VERY_OLD = (4, "[80;+]") +VaccineMean = namedtuple("VaccineMean", ["age", "field", "percent"]) +AgeMean = namedtuple("AgeMean", ["age", "field", "percent"]) + + def get_data( file_path: Optional[str] = None, extension: Optional[str] = "json", @@ -283,11 +289,12 @@ def save_and_close_fig( logging.info(f"{output_path} plotted") -def analyse(np_data: np.ndarray, np_date: np.ndarray) -> None: +def analyse(np_data: np.ndarray) -> List[Union[VaccineMean, AgeMean]]: """ analyse data """ logging.info("analysing data...") + lst_analyse_data: List[Union[VaccineMean, AgeMean]] = list() np_percent_vac, _ = get_vaccine_percent(np_data) logging.info("--- field by age vaccine mean percent ---") @@ -297,6 +304,9 @@ def analyse(np_data: np.ndarray, np_date: np.ndarray) -> None: np.nanmean(np_percent_vac[:, age_group.value, field.value]) * 100, 2 ) print(f"{field.name} - {age_group.label} - vac : {mean_vac_percent}%") + lst_analyse_data.append( + VaccineMean(age_group.label, field.label, mean_vac_percent) + ) logging.info("--- age by field and vac status mean percent ---") for field in Field: @@ -310,6 +320,9 @@ def analyse(np_data: np.ndarray, np_date: np.ndarray) -> None: np.nanmean(np_percent_age[:, age_group.value]), 2 ) print(f"age: {age_group.label} - field: {field.name} = {percent_age_mean}%") + lst_analyse_data.append( + AgeMean(age_group.label, field.label, percent_age_mean) + ) percent_age_vac_mean = np.round( np.nanmean(np_percent_age_vac[:, age_group.value]), 2 @@ -324,6 +337,7 @@ def analyse(np_data: np.ndarray, np_date: np.ndarray) -> None: print( f"age: {age_group.label} - status: unvac - field: {field.name} = {percent_age_unvac_mean}%" ) + return lst_analyse_data def plot_bar_age_percent_vac_status_by_field( @@ -523,7 +537,9 @@ def move_tmp_plots() -> None: logging.info("files moved") -def generate_html_page(np_date: np.ndarray) -> None: +def generate_html_page( + np_date: np.ndarray, lst_analyse_data: List[Union[VaccineMean, AgeMean]] +) -> None: logging.info("generating html page with plots...") os.makedirs(BUILD_REPOSITORY, exist_ok=True) env = Environment( @@ -540,6 +556,8 @@ def generate_html_page(np_date: np.ndarray) -> None: "static": os.path.join(MAIN_URL, STATIC_REPOSITORY), "src": DATA_URL, "period": f"{date_start} - {date_end}", + "vaccine_mean": [x for x in lst_analyse_data if type(x) == VaccineMean], + "age_mean": [x for x in lst_analyse_data if type(x) == AgeMean], } ) with open(os.path.join(BUILD_REPOSITORY, "index.html"), "w") as f: @@ -593,7 +611,7 @@ if __name__ == "__main__": dic_data_grouped: Dict[dt, Any] = group_by_age_date(dic_data) np_data, np_date = get_np_data(dic_data_grouped) - analyse(np_data, np_date) + lst_analyse_data = analyse(np_data) if not args.no_plot: os.makedirs(OUTPUT_REPOSITORY, exist_ok=True) @@ -612,4 +630,4 @@ if __name__ == "__main__": move_tmp_plots() if args.to_html: - generate_html_page(np_date) + generate_html_page(np_date, lst_analyse_data) diff --git a/static/css/index.css b/static/css/index.css index 55e4ff5..44b6ebe 100644 --- a/static/css/index.css +++ b/static/css/index.css @@ -18,14 +18,52 @@ flex: 0 1 33% } +.table-container { + display: flex; + flex-direction: row; + justify-content: center; + align-items: center; + flex-wrap: wrap; +} + +.table-item { + flex: 0 1 33% +} + .img-bars { width: 1500px; height: 500px; border: solid } + +table, th, td { + border: 1px solid black; +} + +table { + border-collapse: collapse; + margin: auto; + width: 60%; +} img { width: 100%; height: 100%; object-fit: contain; +} + +hr { + width: 100%; + margin-top: 40px; + border: 0; + height: 1px; + background: #333; + background-image: -webkit-linear-gradient(left, #ccc, #333, #ccc); + background-image: -moz-linear-gradient(left, #ccc, #333, #ccc); + background-image: -ms-linear-gradient(left, #ccc, #333, #ccc); + background-image: -o-linear-gradient(left, #ccc, #333, #ccc); +} + +h1, h3, h5 { + text-decoration: underline; } \ No newline at end of file diff --git a/templates/index.template.html b/templates/index.template.html index a735fd5..233d194 100644 --- a/templates/index.template.html +++ b/templates/index.template.html @@ -1,24 +1,76 @@ - Thegux covid-19 + Covid-19 DREES dataset - +
-

Thegux covid-19

+

Covid-19 DREES dataset

source : Data DRESS
période temporelle : {{ period }}
pas de temps : journalier
-

Hospitalisations/Soins critiques/Décés par status vaccinal groupés et tranches d'âges

+
contact: admin@thegux.fr
+
+

Données globales

+
Répartition des tranches d'âges par catégorie (moyennée sur la période temporelle)
+
+ {% for field in fields %} +
+ + + + + + + + + {% for mean in age_mean %} + {% if field.label == mean.field %} + + + + + {% endif %} + {% endfor %} +
{{ field.label }}
Age%
{{ mean.age }}{{ mean.percent }}
+
+ {% endfor %} +
+
Pourcentage de vaccinés par catégorie (moyenné sur la période temporelle)
+
+ {% for field in fields %} +
+ + + + + + + + + {% for mean in vaccine_mean %} + {% if field.label == mean.field %} + + + + + {% endif %} + {% endfor %} +
{{ field.label }}
Age%
{{ mean.age }}{{ mean.percent }}
+
+ {% endfor %} +
+
+

Hospitalisations/Soins critiques/Décés par status vaccinal et tranches d'âges groupées

{% for field in fields %} {% endfor %} -

Hospitalisations/Soins critiques/Décés par tranches d'âges groupés et status vaccinal

+

Hospitalisations/Soins critiques/Décés par tranches d'âges et status vaccinal groupé

{% for age in ages %} {% for field in fields %}