예제 #1
0
    def test_multi_peak(self):
        x = np.linspace(0, 1, 100)
        y = np.exp(-(x - 0.4)**2 / 0.01) + np.exp(-(x - 0.8)**2 / 0.01)

        bs = Bspline()
        bs.bspline_basis(x_data=x, k=self.n_param, m=2, type_="equidistant")
        peaks = self.PM.multi_peak_matrix(n_param=self.n_param,
                                          y_data=y,
                                          basis=bs.basis)

        self.assertEqual(peaks.shape, (self.n_param - 1, self.n_param))
        self.assertEqual(
            np.count_nonzero(np.count_nonzero(peaks, axis=1) == 0), 3)
예제 #2
0
    def test_multi_extremum_peak_then_valley(self):
        x = np.linspace(0, 1, 100)
        y = np.exp(-(x - 0.4)**2 / 0.01) + -1 * np.exp(-(x - 0.8)**2 / 0.01)

        bs = Bspline()
        bs.bspline_basis(x_data=x, k=self.n_param, m=2, type_="equidistant")
        valley = self.PM.multi_extremum_matrix(n_param=self.n_param,
                                               y_data=y,
                                               basis=bs.basis)

        self.assertEqual(valley.shape, (self.n_param - 1, self.n_param))
        self.assertEqual(
            np.count_nonzero(np.count_nonzero(valley, axis=1) == 0), 3)
예제 #3
0
    def test_valley(self):
        x = np.linspace(0, 1, 100)
        y = -1 * 0.5 * np.exp(-(x - 0.4)**2 / 0.01)

        bs = Bspline()
        bs.bspline_basis(x_data=x, k=self.n_param, m=2, type_="equidistant")
        valley = self.PM.valley_matrix(n_param=self.n_param,
                                       y_data=y,
                                       basis=bs.basis)

        self.assertEqual(valley.shape, (self.n_param - 1, self.n_param))
        self.assertEqual(
            np.count_nonzero(np.count_nonzero(valley, axis=1) == 0), 1)
        self.assertTrue(valley[0, 0], 1)
        self.assertTrue(valley[0, 1], -1)
        self.assertTrue(valley[-1, -1], -1)
        self.assertTrue(valley[-1, -2], 1)
예제 #4
0
    def construct(self):
        self.setup_axes(animate=True)

        BS = Bspline()
        X = np.random.uniform(0,1,10)
        Y = np.sin(2*np.pi*X) + np.random.normal(scale=0.05, size=10)
       
        self.setup_axes()
        dot_collection = VGroup()
        for time, val in enumerate(Y):
            dot = Dot().move_to(self.coords_to_point(X[time], val))
            self.add(dot)
            dot_collection.add(dot)


        B, knots = BS.basismatrix(np.linspace(0,1,1000)).values()
        

        graph = self.get_graph(
            lambda x: float(BS.basisfunction(np.asarray(x), knots, 5, 1)), x_min=-0.2, x_max=1.2)
        graph2 = self.get_graph(
            lambda x: float(BS.basisfunction(np.asarray(x), knots, 6, 2)), x_min=-0.2, x_max=1.2)
        graph3 = self.get_graph(
            lambda x: float(BS.basisfunction(np.asarray(x), knots, 7, 3)), x_min=-0.2, x_max=1.2)
        
        graphs_linear = []
        for i in range(0,B.shape[1]+1):
            graphs_linear.append(self.get_graph(lambda x: float(
                BS.basisfunction(np.asarray(x), knots, i, 1)), x_min=0, x_max=1))

        #self.play(Create(graph))      # animate the creation of the square

        for g in graphs_linear:
            self.play(Create(g))
예제 #5
0
    def construct(self):
        self.setup_axes()
        BS = Bspline()
        x = np.arange(0,10,1)
        data = [1, 2, 2, 4, 4, 2, 4.2, 4, 5.5, 5.8]
        nr_splines = 5

        coef_, B, k = BS.fit(x, data, nr_splines=nr_spline).values()

        basis, knots = Bspline.basismatrix(X=np.linspace(0,10,50), nr_splines=5, l=3, knot_type="e").values()
        
        b1 = [self.get_graph(lambda x: BS.basisfunction(X=x, knots=knots, j=i+3, l=1),
                                                        x_min=0, x_max=10) for i in range(nr_splines-2)]
        b2 = [self.get_graph(lambda x: BS.basisfunction(X=x, knots=knots, j=i+3, l=2), 
                                                        x_min=0, x_max=10) for i in range(nr_splines-1)]
        b3 = [self.get_graph(lambda x: BS.basisfunction(X=x, knots=knots, j=i+3, l=3), 
                                                        x_min=0, x_max=10) for i in range(nr_splines)]
        text01 = Tex("Basic B-splines")
        text02 = Tex("Of different orders")
        self.play(Write(text01))
        self.wait()
        self.play(Transform(text01, text02))
        self.wait()
        
        text_order1 = Tex("Order = 1")
        text_order2 = Tex("Order = 2")
        text_order3 = Tex("Order = 3")

        self.play(Transform(text01, text_order1))
        self.wait()

        for bf1 in b1:
            self.add(bf1)
            self.wait()

        self.play(Transform(text01, text_order2))

        for idx, bf2 in enumerate(b2):
            if idx < len(b1):
                self.play(Transform(b1[idx], bf2))
                self.add(bf2)
                self.remove(b1[idx])
                self.wait()
            else:
                self.add(bf2)
    
        self.play(Transform(text01, text_order3))

        for idx, bf3 in enumerate(b3):
            if idx < len(b2):
                self.play(Transform(b2[idx], bf3))
                self.add(bf3)
                self.remove(b2[idx])
                self.wait()
            else:
                self.add(bf3)
                self.wait()

        data = [1, 2, 2, 4, 4, 2, 4.2, 4, 5.5, 5.8]
        for time, dat in enumerate(data):
            dot = Dot().move_to(self.coords_to_point(time, dat))
            self.add(dot)
            self.wait()
예제 #6
0
 def func2(x):
     BS = Bspline()
     B, k = BS.basismatrix(np.linspace(0,1,100)).values()
     return float(BS.basisfunction(np.asarray(x), k, 5, 3))
예제 #7
0
 def setUp(self):
     self.x = np.random.random(100)
     self.x.sort()
     self.k = 10
     self.m = 2
     self.BS = Bspline()
예제 #8
0
class TestBspline(unittest.TestCase):
    def setUp(self):
        self.x = np.random.random(100)
        self.x.sort()
        self.k = 10
        self.m = 2
        self.BS = Bspline()

    def tearDown(self):
        del self.x
        del self.BS

    def test_bspline_basis_quantile(self):
        self.BS.bspline_basis(x_data=self.x,
                              k=self.k,
                              m=self.m,
                              type_="quantile")
        self.basis = self.BS.basis
        self.assertEqual(self.basis.shape, (100, 10))
        self.assertTrue(
            np.allclose(
                self.BS.knots[3:-3],
                np.quantile(a=self.x, q=np.linspace(0, 1, self.k - self.m))))
        self.assertTrue(self.BS.n_param == self.k)

    def test_bspline_basis_euqidistant(self):
        self.BS.bspline_basis(x_data=self.x,
                              k=self.k,
                              m=self.m,
                              type_="equidistant")
        self.assertEqual(self.BS.basis.shape, (100, 10))
        self.assertTrue(
            np.allclose(
                self.BS.knots[3:-3],
                np.linspace(self.x.min(), self.x.max(), num=self.k - self.m)))
        self.assertTrue(self.BS.n_param == self.k)

    def test_bspline_basis_partition_of_unity(self):
        self.BS.bspline_basis(x_data=self.x,
                              k=self.k,
                              m=self.m,
                              type_="equidistant")
        self.assertTrue(
            np.allclose(self.BS.basis.sum(axis=1), np.ones(len(self.x))))
        self.BS.bspline_basis(x_data=self.x,
                              k=self.k,
                              m=self.m,
                              type_="quantile")
        self.assertTrue(
            np.allclose(self.BS.basis.sum(axis=1), np.ones(len(self.x))))

    def test_bspline_basis_4_splines_per_knot_interval(self):
        # first and last spline interval only has 3 nonzeros because first and last spline is 0 at first and last position
        self.BS.bspline_basis(x_data=self.x,
                              k=self.k,
                              m=self.m,
                              type_="equidistant")
        self.assertTrue(((self.BS.basis > 0).sum(axis=1)[1:-1] == 4).all())
        self.BS.bspline_basis(x_data=self.x,
                              k=self.k,
                              m=self.m,
                              type_="quantile")
        self.assertTrue(((self.BS.basis > 0).sum(axis=1)[1:-1] == 4).all())

    def test_bspline_spp(self):
        self.BS.bspline_basis(x_data=self.x, k=self.k, m=2, type_="quantile")
        y_pred = self.BS.spp(sp=0.5, coef_=np.ones(self.k))
        self.assertAlmostEqual(float(y_pred), 1)