Page Menu
Home
c4science
Search
Configure Global Search
Log In
Files
F85597258
nearest_neighbor_fitting.py
No One
Temporary
Actions
Download File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Award Token
Subscribers
None
File Metadata
Details
File Info
Storage
Attached
Created
Mon, Sep 30, 05:17
Size
2 KB
Mime Type
text/x-python
Expires
Wed, Oct 2, 05:17 (1 d, 22 h)
Engine
blob
Format
Raw Data
Handle
21175503
Attached To
R6746 RationalROMPy
nearest_neighbor_fitting.py
View Options
# Copyright (C) 2018 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
Log In to Comment