-
Notifications
You must be signed in to change notification settings - Fork 32
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
DRAFT: Weighted Average #833
Draft
philipc2
wants to merge
23
commits into
main
Choose a base branch
from
weighted-mean
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
23 commits
Select commit
Hold shift + click to select a range
e496054
boilerplate
philipc2 23c7a7b
update boilerplate
philipc2 3094cfc
fix boilerplate
philipc2 af8b865
Merge branch 'main' into weighted-mean
philipc2 23a6422
add quad hexagon to tests
philipc2 bd3248c
Merge branch 'weighted-mean' of https://github.com/UXARRAY/uxarray in…
philipc2 98c1c90
Merge branch 'main' into weighted-mean
philipc2 1ecb633
Merge branch 'main' into weighted-mean
philipc2 ae4a13e
Merge branch 'main' into weighted-mean
philipc2 e8e01a5
Merge branch 'main' into weighted-mean
philipc2 5dbaf5d
write tests, work on api design
philipc2 4c3290b
asv benchmark
philipc2 7e5ceca
Merge branch 'main' into weighted-mean
philipc2 6553163
update asv benchmark
philipc2 659cfe9
Merge branch 'weighted-mean' of https://github.com/UXARRAY/uxarray in…
philipc2 70bf239
Merge branch 'main' into weighted-mean
philipc2 0032870
Committing weighted mean modifications from rtam/weighted mean to uxa…
rytam2 8a2ecb3
Merge branch 'main' into weighted-mean
philipc2 02dc504
merge main
philipc2 f9a4d19
some cleanup
philipc2 efce996
fix tests
philipc2 87e6543
Merge branch 'main' into weighted-mean
philipc2 7999c43
Merge branch 'main' into weighted-mean
philipc2 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
{ | ||
"cells": [ | ||
{ | ||
"cell_type": "markdown", | ||
"source": [ | ||
"# Weighted Average" | ||
], | ||
"metadata": { | ||
"collapsed": false | ||
}, | ||
"id": "4850ba8becab8b0d" | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"outputs": [], | ||
"source": [], | ||
"metadata": { | ||
"collapsed": false | ||
}, | ||
"id": "ee8c405203e6faf" | ||
} | ||
], | ||
"metadata": { | ||
"kernelspec": { | ||
"display_name": "Python 3", | ||
"language": "python", | ||
"name": "python3" | ||
}, | ||
"language_info": { | ||
"codemirror_mode": { | ||
"name": "ipython", | ||
"version": 2 | ||
}, | ||
"file_extension": ".py", | ||
"mimetype": "text/x-python", | ||
"name": "python", | ||
"nbconvert_exporter": "python", | ||
"pygments_lexer": "ipython2", | ||
"version": "2.7.6" | ||
} | ||
}, | ||
"nbformat": 4, | ||
"nbformat_minor": 5 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
import uxarray as ux | ||
import os | ||
|
||
import numpy.testing as nt | ||
import numpy as np | ||
|
||
import pytest | ||
|
||
from pathlib import Path | ||
|
||
current_path = Path(os.path.dirname(os.path.realpath(__file__))) | ||
|
||
|
||
csne30_grid_path = current_path / 'meshfiles' / "ugrid" / "outCSne30" / "outCSne30.ug" | ||
csne30_data_path = current_path / 'meshfiles' / "ugrid" / "outCSne30" / "outCSne30_vortex.nc" | ||
|
||
quad_hex_grid_path = current_path / 'meshfiles' / "ugrid" / "quad-hexagon" / "grid.nc" | ||
quad_hex_data_path_face_centered = current_path / 'meshfiles' / "ugrid" / "quad-hexagon" / "data.nc" | ||
quad_hex_data_path_edge_centered = current_path / 'meshfiles' / "ugrid" / "quad-hexagon" / "random-edge-data.nc" | ||
|
||
|
||
def test_quad_hex_face_centered(): | ||
"""Compares the weighted average computation for the quad hexagon grid | ||
using a face centered data variable to the expected value computed by | ||
hand.""" | ||
uxds = ux.open_dataset(quad_hex_grid_path, quad_hex_data_path_face_centered) | ||
|
||
# expected weighted average computed by hand | ||
expected_weighted_mean = 297.55 | ||
|
||
# compute the weighted mean | ||
result = uxds['t2m'].weighted_mean() | ||
|
||
# ensure values are within 3 decimal points of each other | ||
nt.assert_almost_equal(result.values, expected_weighted_mean, decimal=3) | ||
|
||
def test_quad_hex_edge_centered(): | ||
"""Compares the weighted average computation for the quad hexagon grid | ||
using an edge centered data variable to the expected value computed by | ||
hand.""" | ||
uxds = ux.open_dataset(quad_hex_grid_path, quad_hex_data_path_edge_centered) | ||
|
||
# expected weighted average computed by hand | ||
# expected_weighted_mean = 297.55 | ||
expected_weighted_mean = (uxds['random_data_edge'].values * uxds.uxgrid.edge_node_distances).sum() / uxds.uxgrid.edge_node_distances.sum() | ||
|
||
# compute the weighted mean | ||
result = uxds['random_data_edge'].weighted_mean() | ||
|
||
nt.assert_equal(result, expected_weighted_mean) | ||
|
||
|
||
def test_csne30_equal_area(): | ||
"""Compute the weighted average with a grid that has equal-area faces and | ||
compare the result to the regular mean.""" | ||
uxds = ux.open_dataset(csne30_grid_path, csne30_data_path) | ||
face_areas = uxds.uxgrid.face_areas | ||
|
||
# set the area of each face to be one | ||
uxds.uxgrid._ds['face_areas'].data = np.ones(uxds.uxgrid.n_face) | ||
|
||
|
||
weighted_mean = uxds['psi'].weighted_mean() | ||
unweighted_mean = uxds['psi'].mean() | ||
|
||
# with equal area, both should be equal | ||
nt.assert_equal(weighted_mean, unweighted_mean) |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@rytam2
Can you write a test case for this using Dask?