diff --git a/drees.py b/drees.py index 6933209..d4c45bd 100644 --- a/drees.py +++ b/drees.py @@ -167,6 +167,9 @@ def get_np_data(dic_data_grouped: Dict[dt, Any]) -> Tuple[np.ndarray, np.ndarray idx_field = get_enum_field(field) np_data[idx_date, idx_age, idx_vac, idx_field] = value logging.info("date and data generated") + date_start = np_date[0] + date_end = np_date[len(np_date) - 1] + logging.info(f"range period : {date_start} - {date_end}") return np_data, np_date @@ -185,7 +188,7 @@ def get_vaccine_percent(np_data: np.ndarray) -> Tuple[np.ndarray, np.ndarray]: return np_percent_vac, np_percent_unvac -def get_percent_age_by_date_field( +def get_percent_age_by_date_field_vac_splited( np_data: np.ndarray, field: Field ) -> Tuple[np.ndarray, np.ndarray]: """ @@ -213,10 +216,32 @@ def get_percent_age_by_date_field( return np_percent_age_vac, np_percent_age_unvac +def get_percent_age_by_date_field(np_data: np.ndarray, field: Field) -> np.ndarray: + """ + get numpy percent age grouped by date and field + """ + np_percent_age = np.empty((len(np_data), len(AgeGroup))) + for idx_date in range(len(np_data)): + sum_effectif = np.nansum( + np.nansum(np_data[idx_date, :, :, field.value], axis=1) + ) + for age_group in AgeGroup: + np_percent_age[idx_date, age_group.value] = np.round( + ( + np.sum(np_data[idx_date, age_group.value, :, field.value], axis=0) + / sum_effectif + ) + * 100, + 2, + ) + return np_percent_age + + def get_plot_fig( grid: Optional[bool] = True, date_format: Optional[str] = DATE_FORMAT, figsize: Optional[Tuple[int, int]] = None, + locator: Optional[Any] = md.MonthLocator(), ) -> plt.figure: """ return pyplot fig, ax to plot data over range period with date formatting @@ -224,7 +249,7 @@ def get_plot_fig( fig, ax = plt.subplots(figsize=figsize) ax.grid(grid) date_formatter = md.DateFormatter(date_format) - ax.xaxis.set_major_locator(md.AutoDateLocator()) + ax.xaxis.set_major_locator(locator) ax.xaxis.set_major_formatter(date_formatter) fig.autofmt_xdate() return fig, ax @@ -253,7 +278,7 @@ def analyse(np_data: np.ndarray, np_date: np.ndarray) -> None: logging.info("analysing data...") np_percent_vac, _ = get_vaccine_percent(np_data) - logging.info("--- vaccine mean percent ---") + logging.info("--- field by age vaccine mean percent ---") for age_group in AgeGroup: for field in Field: mean_vac_percent = np.round( @@ -261,11 +286,43 @@ def analyse(np_data: np.ndarray, np_date: np.ndarray) -> None: ) print(f"{field.label} - {age_group.label} - vac : {mean_vac_percent}%") + logging.info("--- age by field and vac status mean percent ---") + for field in Field: + np_percent_age = get_percent_age_by_date_field(np_data, field) + ( + np_percent_age_vac, + np_percent_age_unvac, + ) = get_percent_age_by_date_field_vac_splited(np_data, field) + for age_group in AgeGroup: + percent_age_mean = np.round( + np.nanmean(np_percent_age[:, age_group.value]), 2 + ) + print( + f"age: {age_group.label} - field: {field.label} = {percent_age_mean}%" + ) + + percent_age_vac_mean = np.round( + np.nanmean(np_percent_age_vac[:, age_group.value]), 2 + ) + print( + f"age: {age_group.label} - status: vac - field: {field.label} = {percent_age_vac_mean}%" + ) + + percent_age_unvac_mean = np.round( + np.nanmean(np_percent_age_unvac[:, age_group.value]), 2 + ) + print( + f"age: {age_group.label} - status: unvac - field: {field.label} = {percent_age_unvac_mean}%" + ) + def plot_bar_age_percent_vac_status_by_field( - np_data_vac_status: np.ndarray, field: Field, is_vac: Optional[bool] = True + np_data_vac_status: np.ndarray, + np_date: np.ndarray, + field: Field, + is_vac: Optional[bool] = True, ) -> None: - fig, ax = get_plot_fig(figsize=(22, 8)) + fig, ax = get_plot_fig(figsize=(22, 8), locator=md.WeekdayLocator()) bottom = np_data_vac_status[:, 0] title = "vac" if is_vac else "no vac" for age_group in AgeGroup: @@ -287,11 +344,11 @@ def plot_bar_age_percent_vac_status_by_field( color=AGE_COLORS[age_group.value], ) - ax.set_ylabel("%") - ax.set_title(f"{field.label} - {title}") - plt.legend( - [age_group.label for age_group in AgeGroup], loc="upper right", frameon=True - ) + ax.set_ylabel("%") + ax.set_title(f"{field.label} - {title}") + plt.legend( + [age_group.label for age_group in AgeGroup], loc="upper right", frameon=True + ) save_and_close_fig( fig, os.path.join(OUTPUT_REPOSITORY, f"age_percent_{title}_{field.label}"), @@ -299,15 +356,20 @@ def plot_bar_age_percent_vac_status_by_field( ) -def plot_bar_age_percent_by_field(np_data: np.ndarray, field: Field) -> None: +def plot_bar_age_percent_by_field( + np_data: np.ndarray, np_date: np.ndarray, field: Field +) -> None: """ plot percent vaccinated field group by age bar diagram """ - np_percent_age_vac, np_percent_age_unvac = get_percent_age_by_date_field( - np_data, field + ( + np_percent_age_vac, + np_percent_age_unvac, + ) = get_percent_age_by_date_field_vac_splited(np_data, field) + plot_bar_age_percent_vac_status_by_field(np_percent_age_vac, np_date, field) + plot_bar_age_percent_vac_status_by_field( + np_percent_age_unvac, np_date, field, is_vac=False ) - plot_bar_age_percent_vac_status_by_field(np_percent_age_vac, field) - plot_bar_age_percent_vac_status_by_field(np_percent_age_unvac, field, is_vac=False) def plot_cumulative_field( @@ -366,7 +428,6 @@ def plot_bar_data_by_age_field( """ display a bar graph by field and age over the data period bars display vaccine status percent - 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 @@ -451,6 +512,13 @@ if __name__ == "__main__": default=False, help="redownload data for updates", ) + parser.add_argument( + "-np", + "--no-plot", + action="store_true", + default=False, + help="redownload data for updates", + ) args = parser.parse_args() @@ -460,19 +528,19 @@ if __name__ == "__main__": file_path=os.path.join(DATA_REPOSITORY, "dress.json"), refresh=args.refresh ) 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) - plot_fields_args = get_age_vac_args() - f_fields = partial(plot_fields_by_age_vac, np_data, np_date) - plot_vac_percent_age_args = get_age_field_args() - f_bars = partial(plot_bar_data_by_age_field, np_data, np_date) - with Pool(2) as pool: - pool.starmap(f_fields, plot_fields_args) - pool.starmap(f_bars, plot_vac_percent_age_args) + if not args.no_plot: + plot_fields_args = get_age_vac_args() + f_fields = partial(plot_fields_by_age_vac, np_data, np_date) + plot_vac_percent_age_args = get_age_field_args() + f_bars = partial(plot_bar_data_by_age_field, np_data, np_date) + with Pool(2) as pool: + pool.starmap(f_fields, plot_fields_args) + pool.starmap(f_bars, plot_vac_percent_age_args) - for field in Field: - plot_cumulative_field(np_data, np_date, field) - plot_bar_age_percent_by_field(np_data, field) + for field in Field: + plot_cumulative_field(np_data, np_date, field) + plot_bar_age_percent_by_field(np_data, np_date, field)