Overview of xskillscore
mainxarray. For probabilistic forecasts, it is intended to be used in conjunction with properscoring.repository·main·Indexed 42 days ago
https://github.com/xarray-contrib/xskillscoreA Python package for verifying deterministic and probabilistic forecasts, built for the xarray ecosystem. It provides a Contingency object for calculating binary and multi-category metrics (such as Brier score, Heidke Skill Score, and Equitable Threat Score), comparative tests like the sign test and halfwidth CI test, and resampling utilities for bootstrapping via xskillscore.core.resampling.
xarray. For probabilistic forecasts, it is intended to be used in conjunction with properscoring.Contingency-based metrics in xskillscore require the construction of a Contingency object. This object builds a contingency table from forecasts and observations, and you can then call specific methods to retrieve metrics based on that table.
Contingency object.Contingency.table.Contingency instance.Available for binary outcomes:
Contingency.bias_scoreContingency.correct_negativesContingency.equit_threat_scoreContingency.false_alarm_rateContingency.false_alarm_ratioContingency.false_alarmsContingency.hit_rateContingency.hitsContingency.missesContingency.odds_ratioContingency.odds_ratio_skill_scoreContingency.success_ratioContingency.threat_scoreAvailable for multi-class outcomes:
Contingency.accuracyContingency.gerrity_scoreContingency.heidke_scoreContingency.peirce_scorerocYou can run various types of tests to ensure your changes are correct:
pytest.# Run all tests
pytest xskillscore
# Run doctests in docstrings (ignoring the tests directory)
pytest --doctest-modules xskillscore --ignore xskillscore/tests
# Test notebooks
cd docs
nbstripout source/*.ipynb
make -j4 htmlpytest xskillscore
pytest --doctest-modules xskillscore --ignore xskillscore/tests
cd docs
nbstripout source/*.ipynb
make -j4 htmlYou can install the latest stable release of xskillscore using either pip or conda from the conda-forge channel.
# Using pip
pip install xskillscore
# Using conda
conda install -c conda-forge xskillscoreYou can install xskillscore using conda, pip, or directly from the GitHub repository.
$ conda install -c conda-forge xskillscore
$ pip install xskillscore
$ pip install git+https://github.com/xarray-contrib/xskillscoreDocumentation is written in reStructuredText. To build it locally or lint files, use the following commands:
Building HTML documentation:
make html inside the docs directory.Linting reStructuredText:
Run doc8 from the project root to lint .rst files.
Adding new API functions to documentation:
If you add new functions, run sphinx-autogen -o api api.rst from the docs/source directory and update api.rst.
# Build documentation
conda env update -f ci/doc.yml
conda activate xskillscore-docs
cd docs
make html
# Linting
cd ..
doc8 *.rst
# Autogenerate API docs
sphinx-autogen -o api api.rstconda env update -f ci/doc.yml
conda activate xskillscore-docs
cd docs
make html
cd ..
doc8 *.rst
sphinx-autogen -o api api.rstxskillscore uses asv (Airspeed Velocity) to monitor performance regressions. Benchmarks are located in the asv_bench/ directory.
To run a continuous benchmark comparing your branch against the upstream main branch:
cd asv_bench/
asv continuous -f 1.1 upstream/main HEADFiltering Benchmarks:
Use the -b flag with a regular expression to run specific benchmarks or groups.
asv continuous ... -b ^deterministic (runs benchmarks in deterministic.py).asv continuous ... -b deterministic.Compute_small.time_xskillscore_metric_small.asv continuous -f 1.1 upstream/main HEAD -b ^deterministic
asv continuous -f 1.1 upstream/main HEAD -b deterministic.Compute_small.time_xskillscore_metric_smallTo contribute to xskillscore, follow these steps to set up your local environment using mamba and conda:
ci/dev.yml to update your conda environment.pre-commit.# Clone and setup upstream
git clone git@github.com:YOUR_GITHUB_USERNAME/xskillscore.git
cd xskillscore
git remote add upstream git@github.com:xarray-contrib/xskillscore.git
# Create a feature branch
git checkout -b your-bugfix-feature-branch-name main
# Install dependencies
mamba env update -f ci/dev.yml
conda activate xskillscore-dev
# Editable install
pip install --no-deps -e .
# Run linting/formatting hooks
pre-commit run --all-filesgit clone git@github.com:YOUR_GITHUB_USERNAME/xskillscore.git
cd xskillscore
git remote add upstream git@github.com:xarray-contrib/xskillscore.git
# now, to fix a bug or add feature create your own branch off "main":
git checkout -b your-bugfix-feature-branch-name main
mamba env update -f ci/dev.yml
conda activate xskillscore-dev
pip install --no-deps -e .
pre-commit run --all-filesTo install the latest pre-release versions directly from the main branch of the GitHub repository, use the following command:
pip install git+https://github.com/xarray-contrib/xskillscore@main --upgradexskillscore is built on top of xarray.apply_ufunc, which provides vectorization. This makes xskillscore significantly faster than pandas.groupby.apply when dealing with large datasets or when evaluating predictions across many fields/dimensions.
Performance Tip: Instead of using df.groupby([...]).apply(...), convert your DataFrame to an xarray.Dataset using .set_index([...]).to_xarray() and then use the xskillscore accessor. This approach leverages vectorized operations to speed up the calculation across the specified dimensions.
While xskillscore is designed for multidimensional arrays, it can be used effectively on tabular data (like pandas.DataFrame) by converting the DataFrame to an xarray.Dataset. This is particularly useful when you need to evaluate predictions across different fields or dimensions.
To use xskillscore with tabular data:
pandas.DataFrame with target (y) and prediction (yhat) columns..set_index(['dim1', 'dim2']))..to_xarray()..xs accessor to call metrics, specifying the dimension to reduce (the original index) and using skipna=True to handle the nan values created by the conversion process.import pandas as pd
import xarray as xr
import xskillscore as xs
# 1. Prepare DataFrame
df = pd.DataFrame({
'group': ['A', 'A', 'B', 'B'],
'y': [10, 12, 20, 22],
'yhat': [11, 11, 19, 23]
})
# 2. Set index and convert to xarray
# This creates dimensions for 'group' and an index dimension
ds = df.set_index(['group']).to_xarray()
# 3. Apply metric via .xs accessor
# We reduce the 'index' dimension to get a result per 'group'
out = ds.xs.mse('y', 'yhat', dim='index', skipna=True)
# 4. Convert back to pandas if needed
print(out.to_series())Use xskillscore.effective_sample_size to estimate the effective sample size of a dataset. This is typically used to account for autocorrelation in time series data when calculating statistical significance or uncertainty.
import xskillscore
# Usage:
xskillscore.effective_sample_size(observed, forecast, ...)