Page MenuHomec4science

nearest_neighbor_fitting.py
No OneTemporary

File Metadata

Created
Thu, May 2, 18:00

nearest_neighbor_fitting.py

# Copyright (C) 2018-2020 by the RROMPy authors
#
# This file is part of RROMPy.
#
# RROMPy is free software: you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# RROMPy is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with RROMPy. If not, see <http://www.gnu.org/licenses/>.
#
import numpy as np
from rrompy.utilities.poly_fitting.nearest_neighbor import polyval
from rrompy.parameter import checkParameterList
def test_nearest_neighbor_1d():
directionalWeights = [1.]
nNeighbors = 1
xSupp = checkParameterList(np.arange(-1, 3), 1)
ySupp = np.array([-1., 3., -3., 1.])
xx = np.linspace(-2., 3., 100)
yy = polyval(checkParameterList(xx, 1), ySupp, xSupp, nNeighbors,
directionalWeights)
nn = [np.where(xx < -.5), np.where(np.logical_and(xx > -.5, xx < .5)),
np.where(np.logical_and(xx > .5, xx < 1.5)), np.where(xx > 1.5)]
for j in range(4): assert np.allclose(yy[nn[j]], ySupp[j], atol = 1e-5)
def test_nearest_neighbor_exact_1d():
directionalWeights = [1.]
nNeighbors = 2
xSupp = checkParameterList(np.arange(3), 1)
ySupp = np.array([-1., 3., -3.])
xx = np.linspace(0., 1., 100)
yy = polyval(checkParameterList(xx, 1), ySupp, xSupp, nNeighbors,
directionalWeights)
assert np.allclose(yy, -1. + 4 * xx, atol = 1e-5)
xxL = np.linspace(-10., -.5, 100)
yyL = polyval(checkParameterList(xxL, 1), ySupp, xSupp, nNeighbors,
directionalWeights)
xxR = np.linspace(1.5, 9., 100)
yyR = polyval(checkParameterList(xxR, 1), ySupp, xSupp, nNeighbors,
directionalWeights)
assert np.all(yyL > -1. + 4 * xxL) and np.all(yyR < -1. + 4 * xxR)
def test_nearest_neighbors_bounded_2d():
directionalWeights = [5., 1.]
nNeighbors = 3
xSupp = checkParameterList([[0., 0.], [1., 0.], [0., 1.], [0., 10.]], 2)
ySupp = np.array([1., 2., -1., 1e5])
x1 = np.tile(np.linspace(0., 1., 100), 100)
x2 = np.repeat(np.linspace(0., 1., 100), 100)
xx = np.hstack((x1.reshape(-1, 1), x2.reshape(-1, 1)))
yy = polyval(checkParameterList(xx, 2), ySupp, xSupp, nNeighbors,
directionalWeights)
assert np.all(yy <= 2.) and np.all(yy >= -1.)

Event Timeline