Exemplo n.º 1
0
 def test_starmodel_predict_single_point(self):
     self.descr_str = (("s(1)", "peak", self.n_params + 10, (1, 100),
                        "equidistant"), )
     M = StarModel(description=self.descr_str)
     M.fit(X=self.x, y=self.y, plot_=False)
     y_pred = M.predict_single_point(x_sp=self.x[10])
     self.assertEqual(type(y_pred), np.float64)
Exemplo n.º 2
0
 def setUp(self):
     self.x = np.linspace(0, 1, 100).reshape(100, 1)
     self.y = np.exp(-(self.x - 0.4)**2 / 0.01)
     self.n_params = 25
     self.descr_str = (("s(1)", "smooth", self.n_params, (1, 100),
                        "equidistant"), )
     self.M = StarModel(description=self.descr_str)
Exemplo n.º 3
0
 def test_starmodel_set_params_after_gcv_2d(self):
     descr_str = (
         ("s(1)", "smooth", 25, (1, 100), "equidistant"),
         ("s(2)", "smooth", 25, (1, 100), "quantile"),
         ("t(1,2)", "smooth", (5, 5), (1, 100), "quantile"),
     )
     x = np.random.random(200).reshape(100, 2)
     x.sort(axis=0)
     M = StarModel(description=descr_str)
     d = dict()
     d["s(1)_constraint"] = "CHANGED"
     d["s(1)_smoothness"] = "CHANGED"
     d["s(2)_constraint"] = "CHANGED"
     d["s(2)_smoothness"] = "CHANGED"
     d["t(1,2)_constraint"] = "CHANGED"
     d["t(1,2)_smoothness"] = "CHANGED"
     M.set_params_after_gcv(params=d)
     self.assertEqual(M.description_dict["s(1)"]["lam"]["smoothness"],
                      "CHANGED")
     self.assertEqual(M.description_dict["s(1)"]["lam"]["constraint"],
                      "CHANGED")
     self.assertEqual(M.description_dict["s(2)"]["lam"]["smoothness"],
                      "CHANGED")
     self.assertEqual(M.description_dict["s(2)"]["lam"]["constraint"],
                      "CHANGED")
     self.assertEqual(M.description_dict["t(1,2)"]["lam"]["smoothness"],
                      "CHANGED")
     self.assertEqual(M.description_dict["t(1,2)"]["lam"]["constraint"],
                      "CHANGED")
Exemplo n.º 4
0
 def test_starmodel_predict(self):
     self.descr_str = (("s(1)", "peak", self.n_params + 10, (1, 100),
                        "equidistant"), )
     M = StarModel(description=self.descr_str)
     M.fit(X=self.x, y=self.y, plot_=False)
     x_pred = np.linspace(0, 1, 7)
     y_pred = M.predict(X_pred=x_pred)
     self.assertEqual(type(y_pred), np.ndarray)
     self.assertEqual(y_pred.shape[0], x_pred.shape[0])
Exemplo n.º 5
0
 def test_starmodel_fit_2d(self):
     descr_str = (
         ("s(1)", "smooth", 25, (1, 100), "equidistant"),
         ("s(2)", "smooth", 25, (1, 100), "quantile"),
         ("t(1,2)", "smooth", (5, 5), (1, 100), "quantile"),
     )
     x = np.random.random(200).reshape(100, 2)
     x.sort(axis=0)
     y = np.sin(x[:, 0]) + x[:, 0] * x[:, 1]
     M = StarModel(description=descr_str)
     M = M.fit(X=x, y=y, plot_=False)
     self.assertEqual(len(M.coef_), M.basis.shape[1])
Exemplo n.º 6
0
 def test_starmodel_calc_GCV_score_2d(self):
     descr_str = (
         ("s(1)", "smooth", 25, (1, 100), "equidistant"),
         ("s(2)", "smooth", 25, (1, 100), "quantile"),
     )
     x = np.random.random(200).reshape(100, 2)
     x.sort(axis=0)
     y = np.sin(x[:, 0]) + x[:, 0] * x[:, 1]
     M = StarModel(description=descr_str)
     M = M.fit(X=x, y=y, plot_=False)
     gcv = M.calc_GCV_score(y=y)
     self.assertEqual(type(gcv), np.float64)
Exemplo n.º 7
0
 def test_starmodel_get_params_2d(self):
     descr_str = (
         ("s(1)", "smooth", 25, (1, 100), "equidistant"),
         ("s(2)", "smooth", 25, (1, 100), "quantile"),
     )
     x = np.random.random(200).reshape(100, 2)
     x.sort(axis=0)
     M = StarModel(description=descr_str)
     d = M.get_params()
     self.assertTrue(type(d) == dict)
     self.assertTrue(len(d.keys()) == 2)
     self.assertTrue(type(d["s(1)"]) == dict)
     self.assertTrue(len(d["s(1)"].keys()) == 4)
Exemplo n.º 8
0
 def test_starmodel_calc_hat_matrix_2d(self):
     descr_str = (
         ("s(1)", "smooth", 25, (1, 100), "equidistant"),
         ("s(2)", "smooth", 25, (1, 100), "quantile"),
     )
     x = np.random.random(200).reshape(100, 2)
     x.sort(axis=0)
     y = np.sin(x[:, 0]) + x[:, 0] * x[:, 1]
     M = StarModel(description=descr_str)
     M = M.fit(X=x, y=y, plot_=False)
     H = M.calc_hat_matrix()
     self.assertEqual(H.shape, (x.shape[0], x.shape[0]))
     self.assertTrue(np.allclose(H, H.T))
Exemplo n.º 9
0
 def constraint_pen_mat_2d(self, c1="smooth", c2="smooth", y=None):
     if y is None: y = self.y
     descr_str = (
         ("s(1)", c1, 25, (1, 100), "equidistant"),
         ("s(2)", c2, 25, (1, 100), "quantile"),
     )
     x = np.random.random(200).reshape(100, 2)
     x.sort(axis=0)
     M = StarModel(description=descr_str)
     M.calc_LS_fit(X=x, y=y)
     M.create_constraint_penalty_matrix(beta_test=M.coef_)
     self.assertEqual(M.constraint_penalty_matrix.shape,
                      (2 * descr_str[0][2], 2 * descr_str[1][2]))
     del M
Exemplo n.º 10
0
 def test_starmodel_set_params_2d(self):
     descr_str = (
         ("s(1)", "smooth", 25, (1, 100), "equidistant"),
         ("s(2)", "smooth", 25, (1, 100), "quantile"),
         ("t(1,2)", "smooth", (5, 5), (1, 100), "equidistant"),
     )
     x = np.random.random(200).reshape(100, 2)
     x.sort(axis=0)
     M = StarModel(description=descr_str)
     M.create_basis(X=x)
     d = M.get_params()
     d["s(1)"]["constraint"] = "CHANGED"
     d["s(1)"]["knot_type"] = "CHANGED"
     d["s(1)"]["lam"] = {"smoothness": "CHANGED", "constraint": "CHANGED"}
     d["s(1)"]["n_param"] = "CHANGED"
     d["s(2)"]["constraint"] = "CHANGED"
     d["s(2)"]["knot_type"] = "CHANGED"
     d["s(2)"]["lam"] = {"smoothness": "CHANGED", "constraint": "CHANGED"}
     d["s(2)"]["n_param"] = "CHANGED"
     d["t(1,2)"]["constraint"] = "CHANGED"
     d["t(1,2)"]["knot_type"] = "CHANGED"
     d["t(1,2)"]["lam"] = {"smoothness": "CHANGED", "constraint": "CHANGED"}
     d["t(1,2)"]["n_param"] = "CHANGED"
     M.set_params(params=d)
     self.assertEqual(M.description_dict["s(1)"]["constraint"], "CHANGED")
     self.assertEqual(M.description_dict["s(1)"]["knot_type"], "CHANGED")
     self.assertEqual(M.description_dict["s(1)"]["lam"], {
         "smoothness": "CHANGED",
         "constraint": "CHANGED"
     })
     self.assertEqual(M.description_dict["s(1)"]["n_param"], "CHANGED")
     self.assertEqual(M.description_dict["s(2)"]["constraint"], "CHANGED")
     self.assertEqual(M.description_dict["s(2)"]["knot_type"], "CHANGED")
     self.assertEqual(M.description_dict["s(2)"]["lam"], {
         "smoothness": "CHANGED",
         "constraint": "CHANGED"
     })
     self.assertEqual(M.description_dict["s(2)"]["n_param"], "CHANGED")
     self.assertEqual(M.description_dict["t(1,2)"]["constraint"], "CHANGED")
     self.assertEqual(M.description_dict["t(1,2)"]["knot_type"], "CHANGED")
     self.assertEqual(M.description_dict["t(1,2)"]["lam"], {
         "smoothness": "CHANGED",
         "constraint": "CHANGED"
     })
     self.assertEqual(M.description_dict["t(1,2)"]["n_param"], "CHANGED")
Exemplo n.º 11
0
    def test_starmodel_generate_GCV_parameter_list_2d(self):
        descr_str = (
            ("s(1)", "smooth", 25, (1, 100), "equidistant"),
            ("s(2)", "smooth", 25, (1, 100), "quantile"),
        )
        x = np.random.random(200).reshape(100, 2)
        x.sort(axis=0)
        M = StarModel(description=descr_str)
        n_grid, p_min = 5, 1e-4
        grid = M.generate_GCV_parameter_list(n_grid=n_grid, p_min=p_min)
        i, c = 0, np.inf
        for _, g in enumerate(grid):
            i += 1
            c1, c2 = g["s(1)_constraint"], g["s(1)_smoothness"]
            cc = np.min([c1, c2])
            c = np.min([c, cc])

        self.assertEqual(i, n_grid**4)
        self.assertEqual(c, p_min)
Exemplo n.º 12
0
 def test_check_constraint_full_model(self):
     descr = ( ("s(1)", "smooth", self.n_param, (1, 100), "quantile"), )
     M = StarModel(description=descr)
     M.fit(X=self.x.reshape(-1, 1), y=self.y, plot_=False)
     v = check_constraint_full_model(model=M)
     self.assertEqual(v.sum(), self.n_param-2)