Skip to content

Commit

Permalink
Merge pull request #127 from RTIInternational/126-incorrect-weighted-…
Browse files Browse the repository at this point in the history
…average-calculation-in-compute_zonal_mean

126 incorrect weighted average calculation in compute zonal mean
  • Loading branch information
samlamont authored Mar 12, 2024
2 parents 3d96e9a + 714256c commit afbadc8
Show file tree
Hide file tree
Showing 19 changed files with 115 additions and 267 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@ $ poetry add git+https://github.com/RTIInternational/teehr.git#[BRANCH TAG]

Use Docker
```bash
$ docker build -t teehr:v0.3.9 .
$ docker run -it --rm --volume $HOME:$HOME -p 8888:8888 teehr:v0.3.9 jupyter lab --ip 0.0.0.0 $HOME
$ docker build -t teehr:v0.3.10 .
$ docker run -it --rm --volume $HOME:$HOME -p 8888:8888 teehr:v0.3.10 jupyter lab --ip 0.0.0.0 $HOME
```

## Examples
Expand Down
12 changes: 12 additions & 0 deletions docs/sphinx/changelog/index.rst
Original file line number Diff line number Diff line change
@@ -1,6 +1,18 @@
Release Notes
=============

0.3.10 - 2024-03-07
--------------------

Added
^^^^^
* Added `test_zonal_mean_results.py`

Changed
^^^^^^^
* Fixed the calculation of the zonal mean of pixel values in `compute_zonal_mean()` so it caculates
the weighted average (divides by the sum of weight values).
* Updated grid loading tests and data to reflect the fixed method.

0.3.9 - 2024-02-15
--------------------
Expand Down
26 changes: 25 additions & 1 deletion poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "teehr"
version = "0.3.9"
version = "0.3.10"
description = "Tools for Exploratory Evaluation in Hydrologic Research"
authors = [
"RTI International",
Expand Down Expand Up @@ -52,6 +52,7 @@ flake8-docstrings = "^1.7.0"
nbsphinx = "^0.9.3"
pandoc = "^2.3"
myst-nb = "^1.0.0"
nbstripout = "^0.7.1"

[tool.numpydoc_validation]
checks = [
Expand Down
233 changes: 0 additions & 233 deletions src/teehr/loading/grid_loading_example.ipynb

This file was deleted.

14 changes: 8 additions & 6 deletions src/teehr/loading/nwm/grid_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
def compute_zonal_mean(
da: xr.DataArray, weights_filepath: str
) -> pd.DataFrame:
"""Compute zonal mean of area-weighted pixels for given
"""Compute zonal mean (weighted average) of area-weighted pixels for given
zones and weights."""
# Read weights file
weights_df = pd.read_parquet(
Expand All @@ -28,12 +28,14 @@ def compute_zonal_mean(
cols = weights_df.col.values
# Get the values and apply weights
var_values = arr_2d[rows, cols]
weights_df["value"] = var_values * weights_df.weight.values
# Compute mean
df = weights_df.groupby(by="location_id")["value"].mean().to_frame()
df.reset_index(inplace=True)
weights_df["weighted_value"] = var_values * weights_df.weight.values

return df
# Compute weighted average
df = weights_df.groupby(
by="location_id", as_index=False)[["weighted_value", "weight"]].sum()
df["value"] = df.weighted_value/df.weight

return df[["location_id", "value"]]


@dask.delayed
Expand Down
Loading

0 comments on commit afbadc8

Please sign in to comment.