diff --git a/drees.py b/drees.py index 53bba28..7354659 100644 --- a/drees.py +++ b/drees.py @@ -104,7 +104,7 @@ def get_enum_field(value): return field.value -def group_by_age_date(data: Dict[str, Any], fields: List[str]) -> Dict[dt, Any]: +def group_by_age_date(data: Dict[str, Any]) -> Dict[dt, Any]: """ group the original dictionnary into a more readable one 'date': { @@ -131,8 +131,10 @@ def group_by_age_date(data: Dict[str, Any], fields: List[str]) -> Dict[dt, Any]: dic_data_grouped[date][age] = OrderedDict() if vac_status not in dic_data_grouped[date][age]: dic_data_grouped[date][age][vac_status] = OrderedDict() - for field in fields: - dic_data_grouped[date][age][vac_status][field] = row_fields[field] + for field in Field: + dic_data_grouped[date][age][vac_status][field.label] = row_fields[ + field.label + ] logging.info("data restructured") return dic_data_grouped @@ -209,8 +211,8 @@ def plot_cumulative_field( np_cumulate_unvac: np.ndarray = np.cumsum( np_data_unvac[:, age_group.value, field.value], axis=0 ) - plt.plot(np_date, np_cumulate_vac, label=f"{age_group.label} vaccinate") - plt.plot(np_date, np_cumulate_unvac, label=f"{age_group.label} unvaccinate") + plt.plot(np_date, np_cumulate_vac, label=f"{age_group.label} vax") + plt.plot(np_date, np_cumulate_unvac, label=f"{age_group.label} no vax") plt.title(f"nombre de {field.label} cumulé par age") plt.xlabel("date") @@ -266,6 +268,7 @@ def plot_bar_data_by_age_field( a limit days period is set to have an readable plot """ np_percent_vac, np_percent_unvac = get_vaccine_percent(np_data) + # adjust the fig size to display correctly bars and labels fig, ax = get_plot_fig(figsize=(22, 8)) for idx_date in range(len(np_date)): @@ -275,15 +278,15 @@ def plot_bar_data_by_age_field( unvac_percent = np.round( np_percent_unvac[idx_date, age_group.value, field.value] * 100 ) - bar_vac = ax.bar(idx_date, vac_percent, color="b", label="vac") - bar_unvac = ax.bar( - idx_date, unvac_percent, bottom=vac_percent, color="r", label="novac" - ) + bar_vac = ax.bar(idx_date, vac_percent, color="b", label="vax") + ax.bar(idx_date, unvac_percent, bottom=vac_percent, color="r", label="no vax") ax.bar_label(bar_vac, label_type="edge", color="black", fontsize="8") - ax.set_ylim(top=105) + ax.set_ylim(top=105) # to display 100% label ax.set_ylabel("%") ax.set_title(f"{age_group.label} - {field.label}") + + # avoid displaying all dates ax.set( xticks=range(len(np_date)), xticklabels=[ @@ -291,7 +294,7 @@ def plot_bar_data_by_age_field( for idx, d in enumerate(np_date.astype(dt)) ], ) - plt.legend(["vaccinate", "unvaccinate"], loc=0, frameon=True) + plt.legend(["vax", "no vax"], loc=0, frameon=True) save_and_close_fig( fig, @@ -351,9 +354,7 @@ if __name__ == "__main__": dic_data: Dict[str, Any] = get_data( file_path=os.path.join(DATA_REPOSITORY, "dress.json"), refresh=args.refresh ) - dic_data_grouped: Dict[dt, Any] = group_by_age_date( - dic_data, [x.label for x in Field] - ) + dic_data_grouped: Dict[dt, Any] = group_by_age_date(dic_data) np_data, np_date = get_np_data(dic_data_grouped) diff --git a/mypy.ini b/mypy.ini new file mode 100644 index 0000000..e6d02cc --- /dev/null +++ b/mypy.ini @@ -0,0 +1,4 @@ +[mypy] +python_version = 3.8 +ignore_missing_imports = True +disable_error_code = attr-defined \ No newline at end of file diff --git a/requirements.txt b/requirements.txt index fa28a2f..2bf0bf6 100644 --- a/requirements.txt +++ b/requirements.txt @@ -5,16 +5,22 @@ chardet==4.0.0 click==8.0.1 cycler==0.10.0 idna==2.10 +isort==5.10.1 kiwisolver==1.3.1 matplotlib==3.4.2 +mypy==0.920 mypy-extensions==0.4.3 numpy==1.20.3 pathspec==0.8.1 Pillow==8.2.0 pyparsing==2.4.7 python-dateutil==2.8.1 +pytz==2021.3 regex==2021.4.4 requests==2.25.1 six==1.16.0 toml==0.10.2 +tomli==2.0.0 +types-requests==2.26.2 +typing-extensions==4.0.1 urllib3==1.26.4