-
Notifications
You must be signed in to change notification settings - Fork 7
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
[WIP] Enh/58 interpolation methods #60
Merged
neuromusic
merged 3 commits into
AllenInstitute:feature/more_interpolation
from
mochic:enh/58_interpolation-methods
Mar 16, 2018
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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,86 @@ | ||
import numpy as np | ||
from sklearn.gaussian_process import GaussianProcessRegressor | ||
|
||
|
||
def _sinc_interpolator(x, s, u): | ||
"""sinc interpolator | ||
|
||
from: | ||
https://gist.github.com/endolith/1297227 | ||
|
||
Parameters | ||
---------- | ||
x : `np.ndarray` | ||
sampled values | ||
s : `np.ndarray` | ||
sampled points | ||
u : `np.ndarray` | ||
unsampled points | ||
|
||
Returns | ||
------- | ||
np.ndarray | ||
interpolated values | ||
|
||
Notes | ||
----- | ||
- `x` is typically the dependent variable and `s` is typically the | ||
independent variable | ||
""" | ||
if len(x) != len(s): | ||
raise ValueError('x and s must be the same length') | ||
|
||
# Find the period | ||
T = s[1] - s[0] | ||
|
||
sincM = np.tile(u, (len(s), 1)) - np.tile(s[:, np.newaxis], (1, len(u))) | ||
|
||
return np.dot(x, np.sinc(sincM / T)) | ||
|
||
|
||
def kriging_interpolator_factory(x, y, **kwargs): | ||
"""1-dimensional kriging | ||
|
||
from: | ||
https://stackoverflow.com/questions/24978052/interpolation-over-regular-grid-in-python | ||
|
||
Parameters | ||
---------- | ||
x : `np.ndarray` | ||
independent variable used to generate interpolator | ||
y : `np.ndarray` | ||
dependent variable used to generator interpolator | ||
**kwargs | ||
keyword arguments supplied to `GaussianProcessRegressor` | ||
|
||
Returns | ||
------- | ||
function | ||
interpolator function | ||
|
||
Notes | ||
----- | ||
- assumes `x` and `y` are of shape: (n, ), with an equal number of elements | ||
""" | ||
process = GaussianProcessRegressor(**kwargs) | ||
process.fit(x.reshape(1, -1), y.reshape(1, -1)) | ||
|
||
return lambda u: process.predict(u.reshape(1, -1)).reshape(-1) | ||
|
||
|
||
def sinc_interpolator_factory(x, y): | ||
"""sinc interpolator factory | ||
|
||
Parameters | ||
---------- | ||
x : `np.ndarray` | ||
independent variable used to generate interpolator | ||
y : `np.ndarray` | ||
dependent variable used to generator interpolator | ||
|
||
Returns | ||
------- | ||
function | ||
interpolator function | ||
""" | ||
return lambda u: _sinc_interpolator(y, x, u) |
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,30 +1,9 @@ | ||
import numpy as np | ||
import xarray as xr | ||
from scipy import interpolate | ||
|
||
def events_to_xr_dim(events,dim='event'): | ||
|
||
def events_to_xr_dim(events, dim='event'): | ||
# builds the event dataframe into coords for xarray | ||
coords = events.to_dict(orient='list') | ||
coords = {k:(dim,v) for k,v in coords.items()} | ||
coords = {k: (dim, v, ) for k, v in coords.items()} | ||
# define a DataArray that will describe the event dimension | ||
return xr.DataArray( | ||
events.index, | ||
name=dim, | ||
dims=[dim], | ||
coords=coords, | ||
) | ||
|
||
def create_interpolator(t,y): | ||
""" creates a cubic spline interpolator | ||
|
||
Parameters | ||
--------- | ||
y : array-like of floats | ||
|
||
Returns | ||
--------- | ||
interpolator function that accepts a list of times | ||
|
||
""" | ||
interpolator = interpolate.InterpolatedUnivariateSpline(t, y) | ||
return interpolator | ||
return xr.DataArray(events.index, name=dim, dims=[dim], coords=coords) |
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 |
---|---|---|
|
@@ -3,3 +3,4 @@ scipy | |
pandas | ||
xarray | ||
scikit-learn | ||
six |
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,64 @@ | ||
import pytest | ||
import numpy as np | ||
import numpy.testing as npt | ||
|
||
from neuroglia import interpolator | ||
|
||
|
||
# values from justin kiggins | ||
TIME_PRECISE = np.arange(0, 0.2, 0.001) | ||
TIME_TARGET = TIME_PRECISE[::10] # real time | ||
TIME_OBSERVED = TIME_TARGET + 0.002 * np.random.randn(*TIME_TARGET.shape) # unevenly sampled time | ||
make_neuron = lambda t: np.sin(100 * t) | ||
|
||
|
||
@pytest.mark.parametrize("x, y, u, expected", [ | ||
( | ||
np.linspace(-np.pi, np.pi, 201), | ||
np.sin(np.linspace(-np.pi, np.pi, 201)), | ||
np.linspace(-np.pi, np.pi, 201), | ||
np.sin(np.linspace(-np.pi, np.pi, 201)), | ||
), | ||
( | ||
np.linspace(0, 5, 100), | ||
np.linspace(0, 5, 100), | ||
np.linspace(0, 5, 100), | ||
np.linspace(0, 5, 100), | ||
), | ||
( | ||
TIME_OBSERVED, | ||
make_neuron(TIME_OBSERVED), | ||
TIME_OBSERVED, | ||
make_neuron(TIME_OBSERVED), | ||
), | ||
]) | ||
def test_kriging_interpolator_factory(x, y, u, expected): | ||
"""Bad test... | ||
""" | ||
npt.assert_allclose( | ||
interpolator.kriging_interpolator_factory(x, y)(u), | ||
expected, | ||
atol=0.05 | ||
) | ||
|
||
|
||
@pytest.mark.parametrize("x, y, u, expected", [ | ||
( | ||
np.linspace(-np.pi, np.pi, 201), | ||
np.sin(np.linspace(-np.pi, np.pi, 201)), | ||
np.linspace(-np.pi, np.pi, 201), | ||
np.sin(np.linspace(-np.pi, np.pi, 201)), | ||
), | ||
( | ||
np.linspace(0, 5, 100), | ||
np.linspace(0, 5, 100), | ||
np.linspace(0, 5, 100), | ||
np.linspace(0, 5, 100), | ||
), | ||
]) | ||
def test_sinc_interpolator_factory(x, y, u, expected): | ||
npt.assert_allclose( | ||
interpolator.sinc_interpolator_factory(x, y)(u), | ||
expected, | ||
atol=0.05 | ||
) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
the logic here risks breaking compatability with scikit-learn infrastructure. see http://scikit-learn.org/0.16/developers/index.html#instantiation