-
Notifications
You must be signed in to change notification settings - Fork 422
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #119 from ahaberlie/master
Added generic 1d nearest neighbor helper function
- Loading branch information
Showing
3 changed files
with
100 additions
and
4 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
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,17 @@ | ||
# Copyright (c) 2008-2015 MetPy Developers. | ||
# Distributed under the terms of the BSD 3-Clause License. | ||
# SPDX-License-Identifier: BSD-3-Clause | ||
|
||
import numpy as np | ||
from numpy.testing import assert_array_equal | ||
|
||
from metpy.calc.tools import resample_nn_1d | ||
|
||
|
||
def test_resample_nn(): | ||
'Test 1d nearest neighbor functionality.' | ||
a = np.arange(5.) | ||
b = np.array([2, 3.8]) | ||
truth = np.array([2, 4]) | ||
|
||
assert_array_equal(truth, resample_nn_1d(a, b)) |
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,30 @@ | ||
# Copyright (c) 2008-2015 MetPy Developers. | ||
# Distributed under the terms of the BSD 3-Clause License. | ||
# SPDX-License-Identifier: BSD-3-Clause | ||
|
||
import numpy as np | ||
|
||
|
||
def resample_nn_1d(a, centers): | ||
"""Helper function that returns one-dimensional nearest-neighbor | ||
indexes based on user-specified centers. | ||
Parameters | ||
---------- | ||
a : array-like | ||
1-dimensional array of numeric values from which to | ||
extract indexes of nearest-neighbors | ||
centers : array-like | ||
1-dimensional array of numeric values representing a subset of values to approximate | ||
Returns | ||
------- | ||
An array of indexes representing values closest to given array values | ||
""" | ||
ix = [] | ||
for center in centers: | ||
index = (np.abs(a - center)).argmin() | ||
if index not in ix: | ||
ix.append(index) | ||
return ix |