From 361fb719f0ce5415b0cedaee6f799e3a6ed95a52 Mon Sep 17 00:00:00 2001 From: rowanc1 Date: Thu, 20 Feb 2014 10:06:10 -0800 Subject: [PATCH] Fix and test interpolation. Issue #43 --- SimPEG/Tests/test_interpolation.py | 16 ++++++++++++++++ SimPEG/Utils/interputils.py | 26 ++++++++++---------------- 2 files changed, 26 insertions(+), 16 deletions(-) diff --git a/SimPEG/Tests/test_interpolation.py b/SimPEG/Tests/test_interpolation.py index 018f9091..e2634cb7 100644 --- a/SimPEG/Tests/test_interpolation.py +++ b/SimPEG/Tests/test_interpolation.py @@ -2,6 +2,9 @@ import numpy as np import unittest from TestUtils import OrderTest from SimPEG.Utils import mkvc +from SimPEG import Mesh +import unittest + MESHTYPES = ['uniformTensorMesh', 'randomTensorMesh'] TOLERANCES = [0.9, 0.5] @@ -50,6 +53,19 @@ class TestInterpolation1D(OrderTest): self.name = 'Interpolation 1D: N' self.orderTest() +class TestOutliersInterp1D(unittest.TestCase): + + def setUp(self): + pass + + def test_outliers(self): + M = Mesh.TensorMesh([4]) + Q = M.getInterpolationMat(np.array([[0],[0.126],[0.127]]),'CC',zerosOutside=True) + x = np.arange(4)+1 + self.assertTrue(np.all(Q*x == [1,1.004,1.008])) + Q = M.getInterpolationMat(np.array([[-1],[0.126],[0.127]]),'CC',zerosOutside=True) + self.assertTrue(np.all(Q*x == [0,1.004,1.008])) + class TestInterpolation2d(OrderTest): name = "Interpolation 2D" LOCS = np.random.rand(50,2)*0.6+0.2 diff --git a/SimPEG/Utils/interputils.py b/SimPEG/Utils/interputils.py index 04aed35d..3f9b7e71 100644 --- a/SimPEG/Utils/interputils.py +++ b/SimPEG/Utils/interputils.py @@ -178,19 +178,13 @@ def _interpmat3D(locs, x, y, z): if __name__ == '__main__': from SimPEG import * import matplotlib.pyplot as plt - - M = Mesh.TensorMesh([4]) - M.vectorCCx - Q = M.getInterpolationMat(np.array([[0],[0.126],[0.127]]),'CC',zerosOutside=True) - print Q.todense() - - # locs = np.random.rand(50)*0.8+0.1 - # x = np.linspace(0,1,7) - # dense = np.linspace(0,1,200) - # fun = lambda x: np.cos(2*np.pi*x) - # Q = Utils.interpmat(locs, x) - # plt.plot(x, fun(x), 'bs-') - # plt.plot(dense, fun(dense), 'y:') - # plt.plot(locs, Q*fun(x), 'mo') - # plt.plot(locs, fun(locs), 'rx') - # plt.show() + locs = np.random.rand(50)*0.8+0.1 + x = np.linspace(0,1,7) + dense = np.linspace(0,1,200) + fun = lambda x: np.cos(2*np.pi*x) + Q = Utils.interpmat(locs, x) + plt.plot(x, fun(x), 'bs-') + plt.plot(dense, fun(dense), 'y:') + plt.plot(locs, Q*fun(x), 'mo') + plt.plot(locs, fun(locs), 'rx') + plt.show()