-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: add
Type
to interpolator class and factory method for usa…
…bility (#110) * refactor: make interpolator class composable with interpolationtype for usability * feat: add generateinterpolate method * feat: add bindings and binding tets * fix: remove redundant tests based on vishwa's feedback Co-authored-by: Vishwa Shah <vishwa2710@gmail.com> * fix: address rest of comments * docs: fix interpolator constructor --------- Co-authored-by: Vishwa Shah <vishwa2710@gmail.com>
- Loading branch information
1 parent
1da0c43
commit 9830b0b
Showing
23 changed files
with
458 additions
and
225 deletions.
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
53 changes: 53 additions & 0 deletions
53
bindings/python/src/OpenSpaceToolkitMathematicsPy/CurveFitting/Interpolator.cpp
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
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
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
41 changes: 24 additions & 17 deletions
41
bindings/python/test/curve_fitting/interpolator/test_barycentric_rational.py
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 |
---|---|---|
@@ -1,27 +1,34 @@ | ||
# Apache License 2.0 | ||
|
||
import pytest | ||
|
||
from ostk.mathematics.curve_fitting import Interpolator | ||
from ostk.mathematics.curve_fitting.interpolator import BarycentricRational | ||
|
||
|
||
@pytest.fixture | ||
def interpolator() -> BarycentricRational: | ||
return BarycentricRational( | ||
x=[0.0, 1.0, 2.0, 4.0, 5.0, 6.0], y=[0.0, 3.0, 6.0, 9.0, 17.0, 5.0] | ||
) | ||
|
||
|
||
class TestBarycentricRational: | ||
def test_default_constructor(self): | ||
BarycentricRational( | ||
x=[0.0, 1.0, 2.0, 4.0, 5.0, 6.0], y=[0.0, 3.0, 6.0, 9.0, 17.0, 5.0] | ||
) | ||
|
||
def test_evaluate(self): | ||
spline = BarycentricRational( | ||
x=[0.0, 1.0, 2.0, 4.0, 5.0, 6.0], y=[0.0, 3.0, 6.0, 9.0, 17.0, 5.0] | ||
) | ||
|
||
assert spline.evaluate(0.0) == 0.0 | ||
assert spline.evaluate(1.0) == 3.0 | ||
assert spline.evaluate(2.0) == 6.0 | ||
assert spline.evaluate(4.0) == 9.0 | ||
assert spline.evaluate(5.0) == 17.0 | ||
assert spline.evaluate(6.0) == 5.0 | ||
def test_constructors(self, interpolator: BarycentricRational): | ||
assert interpolator is not None | ||
assert isinstance(interpolator, Interpolator) | ||
assert isinstance(interpolator, BarycentricRational) | ||
|
||
def test_evaluate(self, interpolator: BarycentricRational): | ||
|
||
assert interpolator.evaluate(0.0) == 0.0 | ||
assert interpolator.evaluate(1.0) == 3.0 | ||
assert interpolator.evaluate(2.0) == 6.0 | ||
assert interpolator.evaluate(4.0) == 9.0 | ||
assert interpolator.evaluate(5.0) == 17.0 | ||
assert interpolator.evaluate(6.0) == 5.0 | ||
|
||
assert ( | ||
spline.evaluate(x=[0.0, 1.0, 2.0, 4.0, 5.0, 6.0]) | ||
interpolator.evaluate(x=[0.0, 1.0, 2.0, 4.0, 5.0, 6.0]) | ||
== [0.0, 3.0, 6.0, 9.0, 17.0, 5.0] | ||
).all() |
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
71 changes: 71 additions & 0 deletions
71
bindings/python/test/curve_fitting/interpolator/test_interpolator.py
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,71 @@ | ||
# Apache License 2.0 | ||
|
||
import pytest | ||
|
||
from ostk.mathematics.curve_fitting import Interpolator | ||
|
||
|
||
@pytest.fixture | ||
def interpolation_type() -> Interpolator.Type: | ||
return Interpolator.Type.Linear | ||
|
||
|
||
@pytest.fixture | ||
def interpolator(interpolation_type: Interpolator.Type) -> Interpolator: | ||
class MyInterpolator(Interpolator): | ||
def evaluate(self, x: list[float]) -> list[float]: | ||
return x | ||
|
||
def evaluate(self, x: float) -> float: | ||
return x | ||
|
||
return MyInterpolator(interpolation_type=interpolation_type) | ||
|
||
|
||
class TestInterpolator: | ||
def test_subclass(self, interpolator: Interpolator): | ||
assert interpolator is not None | ||
assert isinstance(interpolator, Interpolator) | ||
|
||
def test_get_interpolation_type( | ||
self, | ||
interpolator: Interpolator, | ||
interpolation_type: Interpolator.Type, | ||
): | ||
assert interpolator.get_interpolation_type() == interpolation_type | ||
|
||
@pytest.mark.parametrize( | ||
"parametrized_interpolation_type, x, y", | ||
[ | ||
( | ||
Interpolator.Type.BarycentricRational, | ||
[0.0, 1.0, 2.0, 4.0, 5.0, 6.0], | ||
[0.0, 3.0, 6.0, 9.0, 17.0, 5.0], | ||
), | ||
( | ||
Interpolator.Type.CubicSpline, | ||
[0.0, 1.0, 2.0, 3.0, 4.0, 5.0], | ||
[0.0, 3.0, 6.0, 9.0, 17.0, 5.0], | ||
), | ||
( | ||
Interpolator.Type.Linear, | ||
[0.0, 1.0, 2.0, 4.0, 5.0, 6.0], | ||
[0.0, 3.0, 6.0, 9.0, 17.0, 5.0], | ||
), | ||
], | ||
) | ||
def test_generate_interpolators( | ||
self, | ||
parametrized_interpolation_type: Interpolator.Type, | ||
x: list[float], | ||
y: list[float], | ||
): | ||
interpolator: Interpolator = Interpolator.generate_interpolator( | ||
interpolation_type=parametrized_interpolation_type, | ||
x=x, | ||
y=y, | ||
) | ||
|
||
assert interpolator is not None | ||
assert isinstance(interpolator, Interpolator) | ||
assert interpolator.get_interpolation_type() == parametrized_interpolation_type |
15 changes: 13 additions & 2 deletions
15
bindings/python/test/curve_fitting/interpolator/test_linear.py
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
Oops, something went wrong.