Skip to content

Commit

Permalink
Improve function signatures
Browse files Browse the repository at this point in the history
  • Loading branch information
burggraaff committed Feb 20, 2024
1 parent 199d470 commit 10dce6a
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 10 deletions.
9 changes: 6 additions & 3 deletions fpcup/io.py
Original file line number Diff line number Diff line change
Expand Up @@ -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`.
"""
Expand All @@ -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.
Expand Down Expand Up @@ -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.
Expand Down
19 changes: 12 additions & 7 deletions fpcup/plotting.py
Original file line number Diff line number Diff line change
Expand Up @@ -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").
"""
Expand All @@ -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.
"""
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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).
Expand Down Expand Up @@ -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.
"""
Expand Down Expand Up @@ -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.
"""
Expand All @@ -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.
"""
Expand Down

0 comments on commit 10dce6a

Please sign in to comment.