Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove binsize argument from gridded_skill #395

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 4 additions & 9 deletions modelskill/comparison/_collection.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from pathlib import Path
import tempfile
from typing import (
Any,
Callable,
Dict,
List,
Expand Down Expand Up @@ -617,8 +618,7 @@ def _add_as_col_if_not_in_index(

def gridded_skill(
self,
bins=5,
binsize: float | None = None,
bins: Any = 5,
by: str | Iterable[str] | None = None,
metrics: Iterable[str] | Iterable[Callable] | str | Callable | None = None,
n_min: Optional[int] = None,
Expand All @@ -632,9 +632,6 @@ def gridded_skill(
criteria to bin x and y by, argument bins to pd.cut(), default 5
define different bins for x and y a tuple
e.g.: bins = 5, bins = (5,[2,3,5])
binsize : float, optional
bin size for x and y dimension, overwrites bins
creates bins with reference to round(mean(x)), round(mean(y))
by : str, List[str], optional
group by, by default ["model", "observation"]

Expand Down Expand Up @@ -676,7 +673,7 @@ def gridded_skill(
n (x, y) int32 3 0 0 14 37 17 50 36 72 ... 0 0 15 20 0 0 0 28 76
bias (x, y) float64 -0.02626 nan nan ... nan 0.06785 -0.1143

>>> gs = cc.gridded_skill(binsize=0.5)
>>> gs = cc.gridded_skill()
>>> gs.data.coords
Coordinates:
observation 'alti'
Expand Down Expand Up @@ -705,7 +702,7 @@ def gridded_skill(
metrics = _parse_metric(metrics)

df = cmp._to_long_dataframe()
df = _add_spatial_grid_to_df(df=df, bins=bins, binsize=binsize)
df = _add_spatial_grid_to_df(df=df, bins=bins)

agg_cols = _parse_groupby(by, n_mod=cmp.n_models, n_qnt=cmp.n_quantities)
if "x" not in agg_cols:
Expand Down Expand Up @@ -1123,7 +1120,6 @@ def _load_comparer(folder, f) -> Comparer:
def spatial_skill(
self,
bins=5,
binsize=None,
by=None,
metrics=None,
n_min=None,
Expand All @@ -1134,7 +1130,6 @@ def spatial_skill(
)
return self.gridded_skill(
bins=bins,
binsize=binsize,
by=by,
metrics=metrics,
n_min=n_min,
Expand Down
13 changes: 3 additions & 10 deletions modelskill/comparison/_comparison.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,7 @@ def skill(

def gridded_skill(
self,
bins: int = 5,
binsize: float | None = None,
bins: Any = ...,
by: str | Iterable[str] | None = None,
metrics: Iterable[str] | Iterable[Callable] | str | Callable | None = None,
n_min: int | None = None,
Expand Down Expand Up @@ -1082,7 +1081,6 @@ def score(
def gridded_skill(
self,
bins: int = 5,
binsize: float | None = None,
by: str | Iterable[str] | None = None,
metrics: Iterable[str] | Iterable[Callable] | str | Callable | None = None,
n_min: int | None = None,
Expand All @@ -1096,9 +1094,6 @@ def gridded_skill(
criteria to bin x and y by, argument bins to pd.cut(), default 5
define different bins for x and y a tuple
e.g.: bins = 5, bins = (5,[2,3,5])
binsize : float, optional
bin size for x and y dimension, overwrites bins
creates bins with reference to round(mean(x)), round(mean(y))
by : (str, List[str]), optional
group by column name or by temporal bin via the freq-argument
(using pandas pd.Grouper(freq)),
Expand Down Expand Up @@ -1135,7 +1130,7 @@ def gridded_skill(
n (x, y) int32 3 0 0 14 37 17 50 36 72 ... 0 0 15 20 0 0 0 28 76
bias (x, y) float64 -0.02626 nan nan ... nan 0.06785 -0.1143

>>> gs = cc.gridded_skill(binsize=0.5)
>>> gs = cc.gridded_skill()
>>> gs.data.coords
Coordinates:
observation 'alti'
Expand All @@ -1159,7 +1154,7 @@ def gridded_skill(
raise ValueError("No data to compare")

df = cmp._to_long_dataframe()
df = _add_spatial_grid_to_df(df=df, bins=bins, binsize=binsize)
df = _add_spatial_grid_to_df(df=df, bins=bins)

agg_cols = _parse_groupby(by=by, n_mod=cmp.n_models, n_qnt=1)
if "x" not in agg_cols:
Expand Down Expand Up @@ -1310,7 +1305,6 @@ def load(filename: Union[str, Path]) -> "Comparer":
def spatial_skill(
self,
bins=5,
binsize=None,
by=None,
metrics=None,
n_min=None,
Expand All @@ -1322,7 +1316,6 @@ def spatial_skill(
)
return self.gridded_skill(
bins=bins,
binsize=binsize,
by=by,
metrics=metrics,
n_min=n_min,
Expand Down
31 changes: 7 additions & 24 deletions modelskill/comparison/_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,31 +9,14 @@
IdxOrNameTypes = Union[int, str, List[int], List[str]]


def _add_spatial_grid_to_df(
df: pd.DataFrame, bins, binsize: Optional[float]
) -> pd.DataFrame:
if binsize is None:
# bins from bins
if isinstance(bins, tuple):
bins_x = bins[0]
bins_y = bins[1]
else:
bins_x = bins
bins_y = bins
def _add_spatial_grid_to_df(df: pd.DataFrame, bins) -> pd.DataFrame:
if isinstance(bins, tuple):
bins_x = bins[0]
bins_y = bins[1]
else:
# bins from binsize
x_ptp = df.x.values.ptp() # type: ignore
y_ptp = df.y.values.ptp() # type: ignore
nx = int(np.ceil(x_ptp / binsize))
ny = int(np.ceil(y_ptp / binsize))
x_mean = np.round(df.x.mean())
y_mean = np.round(df.y.mean())
bins_x = np.arange(
x_mean - nx / 2 * binsize, x_mean + (nx / 2 + 1) * binsize, binsize
)
bins_y = np.arange(
y_mean - ny / 2 * binsize, y_mean + (ny / 2 + 1) * binsize, binsize
)
bins_x = bins
bins_y = bins

# cut and get bin centre
df["xBin"] = pd.cut(df.x, bins=bins_x)
df["xBin"] = df["xBin"].apply(lambda x: x.mid)
Expand Down
6 changes: 0 additions & 6 deletions tests/test_trackcompare.py
Original file line number Diff line number Diff line change
Expand Up @@ -336,12 +336,6 @@ def test_gridded_skill_bins(comparer):
assert len(ds.x) == 2
assert len(ds.y) == 3

# binsize (overwrites bins)
ds = comparer.gridded_skill(metrics=["bias"], binsize=2.5, bins=100)
assert len(ds.x) == 4
assert len(ds.y) == 3
assert ds.x[0] == -0.75


# This test doesn't test anything meaningful
# def test_gridded_skill_by(comparer):
Expand Down
Loading