Variety of standard model evaluation metrics.
Project description
OWPHydroTools :: Metrics
This subpackage implements common evaluation metrics used in hydrological model validation and forecast verification. See the Metrics Documentation for a complete list and description of the currently available metrics. To request more metrics, submit an issue through the OWPHydroTools Issue Tracker on GitHub.
Installation
In accordance with the python community, we support and advise the usage of virtual
environments in any workflow using python. In the following installation guide, we
use python's built-in venv
module to create a virtual environment in which the
tool will be installed. Note this is just personal preference, any python virtual
environment manager should work just fine (conda
, pipenv
, etc. ).
# Create and activate python environment, requires python >= 3.8
$ python3 -m venv venv
$ source venv/bin/activate
$ python3 -m pip install --upgrade pip
# Install metrics
$ python3 -m pip install hydrotools.metrics
Usage
The following example demonstrates how one might use hydrotools.metrics
to compute a Threat Score, also called the Critical Success Index, by comparing a persistence forecast to USGS streamflow observations. This example also requires the hydrotools.nwis_client
package.
Code
from hydrotools.metrics import metrics
from hydrotools.nwis_client.iv import IVDataService
import pandas as pd
# Get observed data
service = IVDataService()
observed = service.get(
sites='01646500',
startDT='2020-01-01',
endDT='2021-01-01'
)
# Preprocess data
observed = observed[['value_time', 'value']]
observed = observed.drop_duplicates(['value_time'])
observed = observed.set_index('value_time')
# Simulate a 10-day persistence forecast
issue_frequency = pd.Timedelta('6H')
forecast_length = pd.Timedelta('10D')
forecasts = observed.resample(issue_frequency).nearest()
forecasts = forecasts.rename(columns={"value": "sim"})
# Find observed maximum during forecast period
values = []
for idx, sim in forecasts.itertuples():
obs = observed.loc[idx:idx+forecast_length, "value"].max()
values.append(obs)
forecasts["obs"] = values
# Apply flood criteria using a "Hit Window" approach
# A flood is observed or simluated if any value within the
# forecast_length meets or exceeds the flood_criteria
#
# Apply flood_criteria to forecasts
flood_criteria = 19200.0
forecasts['simulated_flood'] = (forecasts['sim'] >= flood_criteria)
forecasts['observed_flood'] = (forecasts['obs'] >= flood_criteria)
# Compute contingency table
contingency_table = metrics.compute_contingency_table(
forecasts['observed_flood'],
forecasts['simulated_flood']
)
print('Contingency Table Components')
print('============================')
print(contingency_table)
# Compute threat score/critical success index
TS = metrics.threat_score(contingency_table)
print('Threat Score: {:.2f}'.format(TS))
Output
Contingency Table Components
============================
true_positive 148
false_positive 0
false_negative 194
true_negative 1123
dtype: int64
Threat Score: 0.43
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file hydrotools_metrics-1.3.5.tar.gz
.
File metadata
- Download URL: hydrotools_metrics-1.3.5.tar.gz
- Upload date:
- Size: 19.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.10.12
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 4d89d3a577a463a59dbdb84bf18ec038d9a89f1cba1030dc70650685e41186ce |
|
MD5 | 3d224db408704ba6a30842546410a348 |
|
BLAKE2b-256 | 26bd672ab755537459f99c73b247ba55838b5d0fea36872d6f4868c8b1ad2a66 |
File details
Details for the file hydrotools.metrics-1.3.5-py3-none-any.whl
.
File metadata
- Download URL: hydrotools.metrics-1.3.5-py3-none-any.whl
- Upload date:
- Size: 11.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.10.12
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 84f8f3939abd7997a2d58d62a0d1e73b27e077cfd1e079ff06238b56aca8dcea |
|
MD5 | ad5a3a22ae1ee5323adf09c9f5486cda |
|
BLAKE2b-256 | 3a88586e84f522901abf8ba81ba39c11fe47ca650c89eb346b4ac08b4cc2f810 |