Skip to content
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

cubic spline interpolator #687

Merged
merged 1 commit into from
Feb 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion autofit/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@
from .example.model import Gaussian
from .text import formatter
from .text import samples_text
from .interpolator import LinearInterpolator
from .interpolator import LinearInterpolator, SplineInterpolator
from .tools import util


Expand Down
12 changes: 12 additions & 0 deletions autofit/interpolator.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import copy
from abc import ABC, abstractmethod
from scipy.interpolate import CubicSpline
from typing import List, Dict, cast

from scipy.stats import stats
Expand Down Expand Up @@ -207,3 +208,14 @@ class LinearInterpolator(AbstractInterpolator):
def _interpolate(x, y, value):
slope, intercept, r, p, std_err = stats.linregress(x, y)
return slope * value + intercept


class SplineInterpolator(AbstractInterpolator):
"""
Interpolate data with a piecewise cubic polynomial which is twice continuously differentiable
"""

@staticmethod
def _interpolate(x, y, value):
f = CubicSpline(x, y)
return f(value)
51 changes: 40 additions & 11 deletions test_autofit/test_interpolator.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,25 +12,54 @@ def test_trivial():
assert result is instance


@pytest.fixture(name="instances")
def make_instances():
return [
af.ModelInstance(
items=dict(
t=1.0, gaussian=af.Gaussian(centre=0.0, normalization=1.0, sigma=-1.0),
)
),
af.ModelInstance(
items=dict(
t=2.0, gaussian=af.Gaussian(centre=1.0, normalization=2.0, sigma=-2.0),
)
),
]


@pytest.fixture(name="time_series")
def make_time_series():
return af.LinearInterpolator(
[
af.ModelInstance(
items=dict(
t=1.0,
gaussian=af.Gaussian(centre=0.0, normalization=1.0, sigma=-1.0),
)
),
def make_time_series(instances):
return af.LinearInterpolator(instances)


def test_spline_interpolator(instances):
interpolator = af.SplineInterpolator(instances)

result = interpolator[interpolator.t == 1.5]

assert result.t == 1.5
assert result.gaussian.centre == 0.5


def test_smooth_spline_interpolator(instances):
interpolator = af.SplineInterpolator(
instances
+ [
af.ModelInstance(
items=dict(
t=2.0,
gaussian=af.Gaussian(centre=1.0, normalization=2.0, sigma=-2.0),
t=3.0,
gaussian=af.Gaussian(centre=4.0, normalization=3.0, sigma=-3.0),
)
),
]
)

result = interpolator[interpolator.t == 1.5]

assert result.t == 1.5
assert result.gaussian.centre < 0.5


@pytest.mark.parametrize(
"t, centre", [(0.0, -1.0), (1.0, 0.0), (1.5, 0.5), (2.0, 1.0), (3.0, 2.0)]
Expand Down