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

added units flag scatter #134

Merged
merged 10 commits into from
Oct 4, 2022
32 changes: 29 additions & 3 deletions fmskill/comparison.py
Original file line number Diff line number Diff line change
Expand Up @@ -840,6 +840,7 @@ def scatter(
binsize: float = None,
nbins: int = None,
skill_table: bool = False,
units: str = None,
**kwargs,
):
"""Scatter plot showing compared data: observation vs modelled
Expand Down Expand Up @@ -904,6 +905,9 @@ def scatter(
skill_table : bool, optional
calculates the main skills (bias, rmse, si, r2, etc) and adds a box at
the right of the scatter plot, by default False
units : str, optional
add units to plots. By default will use units of dfs0 file. If dataframe is provided,
then a str can be specified. By default None
kwargs

Examples
Expand Down Expand Up @@ -977,15 +981,37 @@ def scatter(
skill_df = self.skill(
metrics=["bias", "rmse", "urmse", "mae", "cc", "si", "r2"], df=df
) # df is filtered to matching subset
stats_with_units=["bias", "rmse", "urmse", "mae"]

lines = []

if units==None:
#Check for units
try:
#Extract first letter of most common dfs0 units in SI;
daniel-caichac-DHI marked this conversation as resolved.
Show resolved Hide resolved
#either (m)eter, (s)econd, (deg)gree, or (m/s). Rest of units can
#always be assigned by user with `units=str` keyword
units_=self._itemInfos[0].unit.display_name
if units_=='meter' or units_=='second':
units_=units_[0]
elif units_=='degree':
units_='°'
elif units_=='meter per sec':
units_='m/s'
except:
#Dimensionless
units_=''
max_str_len = skill_df.df.columns.str.len().max()

for col in skill_df.df.columns:
if col == "model":
continue
lines.append(
f"{col.ljust(max_str_len)} {np.round(skill_df.df[col].values[0],3)}"
if col in stats_with_units:
lines.append(
f"{(col.ljust(max_str_len)).upper()} {np.round(skill_df.df[col].values[0],3)} {units_}"
)
else:
lines.append(
f"{(col.ljust(max_str_len)).upper()} {np.round(skill_df.df[col].values[0],3)}"
daniel-caichac-DHI marked this conversation as resolved.
Show resolved Hide resolved
)

text_ = "\n".join(lines)
Expand Down