From 10dce6ad65545a9c3a794c20bc5ba040dad69a83 Mon Sep 17 00:00:00 2001 From: Olivier Burggraaff Date: Tue, 20 Feb 2024 17:15:38 +0100 Subject: [PATCH] Improve function signatures --- fpcup/io.py | 9 ++++++--- fpcup/plotting.py | 19 ++++++++++++------- 2 files changed, 18 insertions(+), 10 deletions(-) diff --git a/fpcup/io.py b/fpcup/io.py index 03b6f5c..ee70782 100644 --- a/fpcup/io.py +++ b/fpcup/io.py @@ -20,7 +20,8 @@ _SAMPLE_LENGTH = 10 -def save_ensemble_results(results: Iterable[Result], savefolder: PathOrStr, progressbar=True, leave_progressbar=True) -> None: +def save_ensemble_results(results: Iterable[Result], savefolder: PathOrStr, *, + progressbar=True, leave_progressbar=True) -> None: """ Save all Result DataFrames in `results` to files in a given `savefolder`. """ @@ -35,7 +36,8 @@ def save_ensemble_results(results: Iterable[Result], savefolder: PathOrStr, prog for run in tqdm(results, total=n, desc="Saving output files", unit="files", disable=not progressbar, leave=leave_progressbar): run.to_file(savefolder) -def load_ensemble_summary_from_folder(folder: PathOrStr, *, crs=CRS_AMERSFOORT, sample=False, save_if_generated=True, progressbar=True, leave_progressbar=True) -> Summary: +def load_ensemble_summary_from_folder(folder: PathOrStr, *, + crs=CRS_AMERSFOORT, sample=False, save_if_generated=True, progressbar=True, leave_progressbar=True) -> Summary: """ For a given folder, try to load the ensemble summary file. If it is not available, load all individual summary files and combine them. @@ -69,7 +71,8 @@ def load_ensemble_summary_from_folder(folder: PathOrStr, *, crs=CRS_AMERSFOORT, return summary -def load_ensemble_results_from_folder(folder: PathOrStr, *, extension=".wout", sample=False, progressbar=True, leave_progressbar=True) -> list[Result]: +def load_ensemble_results_from_folder(folder: PathOrStr, *, + extension=".wout", sample=False, progressbar=True, leave_progressbar=True) -> list[Result]: """ Load all the output files in a given folder. The individual Result DataFrames will be assigned a run_id from their filenames. diff --git a/fpcup/plotting.py b/fpcup/plotting.py index b8a4fba..833c8b3 100644 --- a/fpcup/plotting.py +++ b/fpcup/plotting.py @@ -29,7 +29,7 @@ # return "abc" # _capitalise_ticks = mticker.StrMethodFormatter("abc{x:}") -def plot_outline(ax: plt.Axes, province: str="All", **kwargs): +def plot_outline(ax: plt.Axes, province: str="All", **kwargs) -> None: """ Plot an outline of the Netherlands ("All") or a specific province (e.g. "Zuid-Holland"). """ @@ -47,7 +47,8 @@ def column_to_title(column: str) -> str: """ return column.capitalize().replace("_", " ") -def brp_histogram(data: gpd.GeoDataFrame, column: str, figsize=(3, 5), usexticks=True, xlabel: Optional[str]="Crop", title: Optional[str]=None, top5=True, saveto: Optional[PathOrStr]=None, **kwargs) -> None: +def brp_histogram(data: gpd.GeoDataFrame, column: str, *, + figsize=(3, 5), usexticks=True, xlabel: Optional[str]="Crop", title: Optional[str]=None, top5=True, saveto: Optional[PathOrStr]=None, **kwargs) -> None: """ Make a bar plot showing the distribution of plots/crops in BRP data. """ @@ -92,7 +93,8 @@ def brp_histogram(data: gpd.GeoDataFrame, column: str, figsize=(3, 5), usexticks plt.show() plt.close() -def brp_map(data: gpd.GeoDataFrame, column: str, province: Optional[str]="All", figsize=(10, 10), title: Optional[str]=None, rasterized=True, colour_dict: Optional[StringDict]=None, saveto: Optional[PathOrStr]=None, **kwargs) -> None: +def brp_map(data: gpd.GeoDataFrame, column: str, *, + province: Optional[str]="All", figsize=(10, 10), title: Optional[str]=None, rasterized=True, colour_dict: Optional[StringDict]=None, saveto: Optional[PathOrStr]=None, **kwargs) -> None: """ Create a map of BRP polygons in the given column. If `province` is provided, only data within that province will be plotted, with the corresponding outline. @@ -131,7 +133,8 @@ def brp_map(data: gpd.GeoDataFrame, column: str, province: Optional[str]="All", plt.show() plt.close() -def brp_crop_map_split(data: gpd.GeoDataFrame, column: str="crop_species", crops: Iterable[str]=brp_crops_colours.keys(), figsize=(14, 3.5), shape=(1, 5), title: Optional[str]=None, rasterized=True, saveto: Optional[PathOrStr]=None, **kwargs) -> None: +def brp_crop_map_split(data: gpd.GeoDataFrame, column: str="crop_species", *, + crops: Iterable[str]=brp_crops_colours.keys(), figsize=(14, 3.5), shape=(1, 5), title: Optional[str]=None, rasterized=True, saveto: Optional[PathOrStr]=None, **kwargs) -> None: """ Create a map of BRP polygons, with one panel per crop species. Shape is (nrows, ncols). @@ -165,7 +168,8 @@ def replace_year_in_datetime(date: dt.date, newyear: int=2000) -> dt.date: """ return date.replace(year=newyear) -def plot_wofost_ensemble_results(outputs: Iterable[pd.DataFrame], keys: Iterable[str]=None, title: Optional[str]=None, saveto: Optional[PathOrStr]=None, replace_years=True, progressbar=True, leave_progressbar=False) -> None: +def plot_wofost_ensemble_results(outputs: Iterable[pd.DataFrame], keys: Iterable[str]=None, *, + title: Optional[str]=None, saveto: Optional[PathOrStr]=None, replace_years=True, progressbar=True, leave_progressbar=False) -> None: """ Plot WOFOST ensemble results. """ @@ -206,7 +210,7 @@ def plot_wofost_ensemble_results(outputs: Iterable[pd.DataFrame], keys: Iterable plt.close() -def _numerical_or_date_bins(column: pd.Series): +def _numerical_or_date_bins(column: pd.Series) -> int | pd.DatetimeIndex: """ Generate bins for a column based on its data type. """ @@ -215,7 +219,8 @@ def _numerical_or_date_bins(column: pd.Series): else: return rcParams["hist.bins"] -def plot_wofost_ensemble_summary(summary: Summary, *, keys: Iterable[str]=None, title: Optional[str]=None, province: Optional[str]="All", saveto: Optional[PathOrStr]=None) -> None: +def plot_wofost_ensemble_summary(summary: Summary, keys: Iterable[str]=None, *, + title: Optional[str]=None, province: Optional[str]="All", saveto: Optional[PathOrStr]=None) -> None: """ Plot WOFOST ensemble results. """