예제 #1
0
 def test_tol(self):
     """
     Tests that the optimization of OP parameters can be achieved
     to different tolerancesself.
     Tolerances don't directly translate, so we expand it slightly for
     the test (by a factor prop. to e**(len(ps)+1)).
     """
     eps = np.finfo(float).eps
     hightol_result = orthogonal_polynomial_constants(
         self.xs, degree=self.default_degree, tol=10 ** -16
     )
     for pow in np.linspace(np.log(eps * 1000.0), -5, 3):
         tol = np.exp(pow)
         with self.subTest(tol=tol):
             ret = orthogonal_polynomial_constants(
                 self.xs, degree=self.default_degree, tol=tol
             )
             self.assertTrue(not len(ret[0]))  # first item is empty
             self.assertTrue(len(ret) == self.default_degree)
             for ix, ps in enumerate(ret):
                 if ps:
                     test_tol = tol * np.exp(len(ps) + 1)
                     a = np.array(list(ps), dtype=float)
                     b = np.array(list(hightol_result[ix]), dtype=float)
                     # print( (abs(a)-abs(b)) / ((abs(a)+abs(b))/2)  - test_tol)
                     self.assertTrue(np.allclose(a, b, atol=test_tol))
예제 #2
0
 def test_xs(self):
     """Tests operation on different x arrays."""
     for xs in [self.xs, self.xs[1:], self.xs[2:-2]]:
         with self.subTest(xs=xs):
             ret = orthogonal_polynomial_constants(xs, degree=self.default_degree)
             self.assertTrue(not len(ret[0]))  # first item is empty
             self.assertTrue(len(ret) == self.default_degree)
예제 #3
0
    def test_degree(self):
        """Tests generation of different degree polynomial parameters."""

        max_degree = 4
        expected = orthogonal_polynomial_constants(self.xs, degree=max_degree)
        for degree in range(1, max_degree):
            with self.subTest(degree=degree):
                ret = orthogonal_polynomial_constants(self.xs, degree=degree)
                self.assertTrue(not len(ret[0]))  # first item is empty
                self.assertTrue(len(ret) == degree)
                # the parameter values should be independent of the degree.
                allclose = all([
                    np.allclose(
                        np.array(expected[idx], dtype=float),
                        np.array(tpl, dtype=float),
                    ) for idx, tpl in enumerate(ret)
                ])
                self.assertTrue(allclose)
예제 #4
0
 def test_against_original(self):
     """Check that the constants line up with Hugh's paper."""
     ret = orthogonal_polynomial_constants(self.xs, degree=self.default_degree)
     print(ret, self.expect)
     for out, expect in zip(ret, self.expect):
         with self.subTest(out=out, expect=expect):
             if out:
                 self.assertTrue(
                     np.allclose(
                         np.array(out, dtype="float"),
                         np.array(expect, dtype="float"),
                     )
                 )
             else:
                 self.assertEqual(out, ())  # first one should be empty tuple
예제 #5
0
 def test_params(self):
     # the first three all have the same result - defaulting to ONeill 2016
     # the two following use a full REE set for the OP basis function definition
     # the last illustrates that custom parameters could be used
     degree = self.default_degree
     for params in [
             None,
             "ONeill2016",
             "O'Neill (2016)",
             "full",
             "Full",
             orthogonal_polynomial_constants(
                 get_ionic_radii(REE(), charge=3, coordination=8),  # xs
                 degree=self.default_degree,
             ),
     ]:
         with self.subTest(params=params):
             ret = calc_lambdas(self.df, params=params, degree=degree)
             self.assertTrue(ret.columns.size == self.default_degree)
예제 #6
0
 def test_function_params(self):
     params = orthogonal_polynomial_constants(self.xs, degree=len(self.lambdas))
     ret = get_lambda_poly_func(self.lambdas, params=params)
     self.assertTrue(callable(ret))