Python numpy.testing.assert_array_less() Examples

The following are 13 code examples of numpy.testing.assert_array_less(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module numpy.testing , or try the search function .
Example #1
Source File: test_matching.py    From Carnets with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def test_matching_function():
    from astropy.coordinates import ICRS
    from astropy.coordinates.matching import match_coordinates_3d
    # this only uses match_coordinates_3d because that's the actual implementation

    cmatch = ICRS([4, 2.1]*u.degree, [0, 0]*u.degree)
    ccatalog = ICRS([1, 2, 3, 4]*u.degree, [0, 0, 0, 0]*u.degree)

    idx, d2d, d3d = match_coordinates_3d(cmatch, ccatalog)
    npt.assert_array_equal(idx, [3, 1])
    npt.assert_array_almost_equal(d2d.degree, [0, 0.1])
    assert d3d.value[0] == 0

    idx, d2d, d3d = match_coordinates_3d(cmatch, ccatalog, nthneighbor=2)
    assert np.all(idx == 2)
    npt.assert_array_almost_equal(d2d.degree, [1, 0.9])
    npt.assert_array_less(d3d.value, 0.02) 
Example #2
Source File: test_action_head.py    From vel with MIT License 6 votes vote down vote up
def test_sample_diag_gaussian():
    """ Test sampling from a multivariate gaussian distribution with a diagonal covariance matrix """
    head = DiagGaussianActionHead(1, 5)

    array = np.zeros((10000, 5, 2))

    sample = head.sample(torch.from_numpy(array))

    result_array = sample.detach().cpu().numpy()

    nt.assert_array_less(np.abs(result_array.mean(axis=0)), 0.1)
    nt.assert_array_less(result_array.std(axis=0), 1.1)
    nt.assert_array_less(0.9, result_array.std(axis=0))

    array2 = np.zeros((10000, 5, 2))
    array2[:, 0, 0] = 5.0
    array2[:, 0, 1] = np.log(10)

    sample2 = head.sample(torch.from_numpy(array2))

    result_array2 = sample2.detach().cpu().numpy()
    nt.assert_array_less(result_array2.mean(axis=0), np.array([5.3, 0.1, 0.1, 0.1, 0.1]))
    nt.assert_array_less(np.array([4.7, -0.1, -0.1, -0.1, -0.1]), result_array2.mean(axis=0)) 
Example #3
Source File: test_action_head.py    From vel with MIT License 6 votes vote down vote up
def test_sample_categorical():
    """
    Test sampling from a categorical distribution
    """
    head = CategoricalActionHead(1, 5)

    array = np.zeros((10000, 5))

    sample = head.sample(torch.from_numpy(array))

    result_array = sample.detach().cpu().numpy()

    nt.assert_array_less(np.abs(result_array.mean(axis=0)), 2.1)
    nt.assert_array_less(1.9, np.abs(result_array.mean(axis=0)))

    array2 = np.zeros((10000, 5))
    array2[:, 0:4] = -10.0
    array2[:, 4] = 10.0

    sample2 = head.sample(F.log_softmax(torch.from_numpy(array2), dim=1))
    result_array2 = sample2.detach().cpu().numpy()

    nt.assert_array_less(np.abs(result_array2.mean(axis=0)), 4.1)
    nt.assert_array_less(3.9, np.abs(result_array2.mean(axis=0))) 
Example #4
Source File: test_random_panel.py    From vnpy_crypto with MIT License 5 votes vote down vote up
def assert_maxabs(actual, expected, value):
    npt.assert_array_less(em.maxabs(actual, expected, None), value) 
Example #5
Source File: test_common.py    From scikit-optimize with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def check_minimizer_bounds(result, n_calls):
    # no values should be below or above the bounds
    eps = 10e-9  # check for assert_array_less OR equal
    assert_array_less(result.x_iters, np.tile([10+eps, 15+eps], (n_calls, 1)))
    assert_array_less(np.tile([-5-eps, 0-eps], (n_calls, 1)), result.x_iters) 
Example #6
Source File: test_util.py    From lenstronomy with MIT License 5 votes vote down vote up
def test_estimate_theta_E():

    x = np.array([-0.45328229, 0.57461556, 0.53757501, -0.42312438])
    y = np.array([0.69582971, -0.51226356, 0.37577509, -0.40245467])

    approx = util.approx_theta_E(x, y)
    npt.assert_array_less(approx - 1, 0.2) 
Example #7
Source File: test_acceleration.py    From pyunlocbox with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def test_regularized_nonlinear(self):
        """
        Test gradient descent solver with regularized non-linear acceleration,
        solving problems with L2-norm functions.

        """
        dim = 25
        np.random.seed(0)
        x0 = np.random.rand(dim)
        xstar = np.random.rand(dim)
        x0 = xstar + 5. * (x0 - xstar) / np.linalg.norm(x0 - xstar)

        A = np.random.rand(dim, dim)
        step = 1 / np.linalg.norm(np.dot(A.T, A))

        accel = acceleration.regularized_nonlinear(k=5)
        solver = solvers.gradient_descent(step=step, accel=accel)
        param = {'solver': solver, 'rtol': 0,
                 'maxit': 200, 'verbosity': 'NONE'}

        # L2-norm prox and dummy gradient.
        f1 = functions.norm_l2(lambda_=0.5, A=A, y=np.dot(A, xstar))
        f2 = functions.dummy()
        ret = solvers.solve([f1, f2], x0, **param)
        pctdiff = 100 * np.sum((xstar - ret['sol'])**2) / np.sum(xstar**2)
        nptest.assert_array_less(pctdiff, 1.91)

        # Sanity checks
        accel = acceleration.regularized_nonlinear()
        self.assertRaises(ValueError, accel.__init__, 10, ['not', 'good'])
        self.assertRaises(ValueError, accel.__init__, 10, 'nope') 
Example #8
Source File: test_point_mass.py    From harmonica with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def test_g_z_relative_error():
    """
    Test the relative error in computing the g_z component
    """
    # Define a single point mass
    point_mass = (1, -67, -300.7)
    mass = 250
    coordinates_p = (0, -39, -13)
    # Compute the z component
    exact_deriv = point_mass_gravity(
        coordinates_p, point_mass, mass, "g_z", "cartesian"
    )
    # Compute the numerical derivative of potential
    delta = 0.1
    easting = np.zeros(2) + coordinates_p[0]
    northing = np.zeros(2) + coordinates_p[1]
    upward = np.array([coordinates_p[2] - delta, coordinates_p[2] + delta])
    coordinates = (easting, northing, upward)
    potential = point_mass_gravity(
        coordinates, point_mass, mass, "potential", "cartesian"
    )
    # Remember that the ``g_z`` field returns the downward component of the
    # gravitational acceleration. As a consequence, the numerical
    # derivativative is multiplied by -1.
    approximated_deriv = -1e5 * (potential[1] - potential[0]) / (2.0 * delta)

    # Compute the relative error
    relative_error = np.abs((approximated_deriv - exact_deriv) / exact_deriv)

    # Bound value
    distance = distance_cartesian(coordinates_p, point_mass)
    bound_value = 1.5 * (delta / distance) ** 2

    # Compare the results
    npt.assert_array_less(relative_error, bound_value) 
Example #9
Source File: test_point_mass.py    From harmonica with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def test_g_northing_relative_error():
    """
    Test the relative error in computing the g_northing component
    """
    # Define a single point mass
    point_mass = (1, -67, -300.7)
    mass = 250
    coordinates_p = (0, -39, -13)
    # Compute the northing component
    exact_deriv = point_mass_gravity(
        coordinates_p, point_mass, mass, "g_northing", "cartesian"
    )
    # Compute the numerical derivative of potential
    delta = 0.1
    easting = np.zeros(2) + coordinates_p[0]
    northing = np.array([coordinates_p[1] - delta, coordinates_p[1] + delta])
    upward = np.zeros(2) + coordinates_p[2]
    coordinates = (easting, northing, upward)
    potential = point_mass_gravity(
        coordinates, point_mass, mass, "potential", "cartesian"
    )
    approximated_deriv = 1e5 * (potential[1] - potential[0]) / (2.0 * delta)

    # Compute the relative error
    relative_error = np.abs((approximated_deriv - exact_deriv) / exact_deriv)

    # Bound value
    distance = distance_cartesian(coordinates_p, point_mass)
    bound_value = 1.5 * (delta / distance) ** 2

    # Compare the results
    npt.assert_array_less(relative_error, bound_value) 
Example #10
Source File: test_point_mass.py    From harmonica with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def test_g_easting_relative_error():
    """
    Test the relative error in computing the g_easting component
    """
    # Define a single point mass
    point_mass = (20, 54, -500.7)
    mass = 200
    coordinates_p = (-3, 24, -10)
    # Compute the easting component
    exact_deriv = point_mass_gravity(
        coordinates_p, point_mass, mass, "g_easting", "cartesian"
    )
    # Compute the numerical derivative of potential
    delta = 0.1
    easting = np.array([coordinates_p[0] - delta, coordinates_p[0] + delta])
    northing = np.zeros(2) + coordinates_p[1]
    upward = np.zeros(2) + coordinates_p[2]
    coordinates = (easting, northing, upward)
    potential = point_mass_gravity(
        coordinates, point_mass, mass, "potential", "cartesian"
    )
    approximated_deriv = 1e5 * (potential[1] - potential[0]) / (2.0 * delta)

    # Compute the relative error
    relative_error = np.abs((approximated_deriv - exact_deriv) / exact_deriv)

    # Bound value
    distance = distance_cartesian(coordinates_p, point_mass)
    bound_value = 1.5 * (delta / distance) ** 2

    # Compare the results
    npt.assert_array_less(relative_error, bound_value) 
Example #11
Source File: test_arrays.py    From Carnets with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def test_array_precession():
    """
    Ensures that FK5 coordinates as arrays precess their equinoxes
    """
    j2000 = Time('J2000')
    j1975 = Time('J1975')

    fk5 = FK5([1, 1.1]*u.radian, [0.5, 0.6]*u.radian)
    assert fk5.equinox.jyear == j2000.jyear
    fk5_2 = fk5.transform_to(FK5(equinox=j1975))
    assert fk5_2.equinox.jyear == j1975.jyear

    npt.assert_array_less(0.05, np.abs(fk5.ra.degree - fk5_2.ra.degree))
    npt.assert_array_less(0.05, np.abs(fk5.dec.degree - fk5_2.dec.degree)) 
Example #12
Source File: test_kgw_slow.py    From pyscf with Apache License 2.0 4 votes vote down vote up
def setUpClass(cls):
        cls.cell = cell = Cell()
        # Lift some degeneracies
        cell.atom = '''
        C 0.000000000000   0.000000000000   0.000000000000
        C 1.67   1.68   1.69
        '''
        cell.basis = {'C': [[0, (0.8, 1.0)],
                            [1, (1.0, 1.0)]]}
        # cell.basis = 'gth-dzvp'
        cell.pseudo = 'gth-pade'
        cell.a = '''
        0.000000000, 3.370137329, 3.370137329
        3.370137329, 0.000000000, 3.370137329
        3.370137329, 3.370137329, 0.000000000'''
        cell.unit = 'B'
        cell.verbose = 5
        cell.build()

        k = cell.make_kpts([cls.k, 1, 1], scaled_center=cls.k_c)

        # K-points
        cls.model_krhf = model_krhf = KRHF(cell, k).density_fit()
        model_krhf.conv_tol = 1e-14
        model_krhf.kernel()

        ke = numpy.concatenate(model_krhf.mo_energy)
        ke.sort()

        # Make sure no degeneracies are present
        testing.assert_array_less(1e-4, ke[1:] - ke[:-1])

        # TD
        cls.td_model_srhf = td_model_srhf = std.TDRHF(model_krhf)
        td_model_srhf.kernel()

        cls.td_model_krhf = td_model_krhf = ktd.TDRHF(model_krhf)
        td_model_krhf.kernel()

        # adjust_td_phase(td_model_srhf, td_model_krhf)

        # GW
        cls.gw = sgw.GW(td_model_srhf)
        cls.kgw = kgw.GW(td_model_krhf) 
Example #13
Source File: test_krhf_slow_supercell.py    From pyscf with Apache License 2.0 4 votes vote down vote up
def setUpClass(cls):
        cls.cell = cell = Cell()
        # Lift some degeneracies
        cell.atom = '''
        C 0.000000000000   0.000000000000   0.000000000000
        C 1.67   1.68   1.69
        '''
        cell.basis = {'C': [[0, (0.8, 1.0)],
                            [1, (1.0, 1.0)]]}
        # cell.basis = 'gth-dzvp'
        cell.pseudo = 'gth-pade'
        cell.a = '''
        0.000000000, 3.370137329, 3.370137329
        3.370137329, 0.000000000, 3.370137329
        3.370137329, 3.370137329, 0.000000000'''
        cell.unit = 'B'
        cell.verbose = 5
        cell.build()

        k = cell.make_kpts([cls.k, 1, 1], scaled_center=cls.k_c)

        # The Gamma-point reference
        cls.model_rhf = model_rhf = RHF(super_cell(cell, [cls.k, 1, 1]), kpt=k[0]).density_fit()
        model_rhf.conv_tol = 1e-14
        model_rhf.kernel()

        # K-points
        cls.model_krhf = model_krhf = KRHF(cell, k).density_fit()
        model_krhf.conv_tol = 1e-14
        model_krhf.kernel()

        adjust_mf_phase(model_rhf, model_krhf)

        ke = numpy.concatenate(model_krhf.mo_energy)
        ke.sort()

        # Make sure mo energies are the same
        testing.assert_allclose(model_rhf.mo_energy, ke)

        # Make sure no degeneracies are present
        testing.assert_array_less(1e-4, ke[1:] - ke[:-1])

        cls.ov_order = ov_order(model_krhf)

        # The Gamma-point TD
        cls.td_model_rhf = td_model_rhf = td.TDRHF(model_rhf)
        td_model_rhf.kernel()
        cls.ref_m = td_model_rhf.eri.tdhf_full_form()