Python numpy.testing.assert_array_almost_equal() Examples

The following are 30 code examples of numpy.testing.assert_array_almost_equal(). 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_loss_func.py    From astroNN with MIT License 6 votes vote down vote up
def test_binary_crossentropy(self):
        y_pred = tf.constant([[0.5, 0., 1.], [2., 0., -1.]])
        y_pred_2 = tf.constant([[0.5, 2., 1.], [2., 2., -1.]])
        y_true = tf.constant([[1., MAGIC_NUMBER, 1.], [1., MAGIC_NUMBER, 0.]])
        y_pred_sigmoid = tf.nn.sigmoid(y_pred)
        y_pred_2_sigmoid = tf.nn.sigmoid(y_pred_2)

        # Truth with Magic number is wrong
        npt.assert_array_almost_equal(binary_crossentropy(y_true, y_pred_sigmoid).numpy(),
                                      binary_crossentropy(y_true, y_pred, from_logits=True).numpy(), decimal=3)
        # make sure neural network prediction won't matter for magic number term
        npt.assert_array_almost_equal(
            binary_crossentropy(y_true, y_pred_2, from_logits=True).numpy(),
            binary_crossentropy(y_true, y_pred, from_logits=True).numpy()
            , decimal=3)
        npt.assert_array_almost_equal(binary_crossentropy(y_true, y_pred_sigmoid).numpy(),
                                      binary_crossentropy(y_true, y_pred_2_sigmoid).numpy(), decimal=3) 
Example #2
Source File: test_pricing_class.py    From fftoptionlib with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def test_cal_price_fft(self):
        fft_pricer = FourierPricer(self.vanilla_option)
        strike_arr = np.array([5, 10, 30, 36, 50, 60, 100])
        put_call_arr = np.array(['call', 'call', 'put', 'call', 'call', 'put', 'call'])
        exp = np.array(
            [3.09958567e+01, 2.60163625e+01, 8.25753140e-05, 8.12953226e-01,
             8.97449491e-11, 2.37785797e+01, 2.19293560e-85, ]
        )

        volatility = 0.20
        N = 2 ** 15
        d_u = 0.01
        alpha = 1
        fft_pricer.set_log_st_process(BlackScholes(volatility))
        fft_pricer.set_pricing_engine(FFTEngine(N, d_u, alpha, spline_order=3))
        res = fft_pricer.calc_price(strike_arr, put_call_arr, put_label='put')
        npt.assert_array_almost_equal(res, exp, 6) 
Example #3
Source File: test_ros.py    From vnpy_crypto with MIT License 6 votes vote down vote up
def test__do_ros():
    expected = numpy.array([
         3.11279729,   3.60634338,   4.04602788,   4.04602788,
         4.71008116,   6.14010906,   6.97841457,   2.        ,
         4.2       ,   4.62      ,   5.57      ,   5.66      ,
         5.86      ,   6.65      ,   6.78      ,   6.79      ,
         7.5       ,   7.5       ,   7.5       ,   8.63      ,
         8.71      ,   8.99      ,   9.85      ,  10.82      ,
        11.25      ,  11.25      ,  12.2       ,  14.92      ,
        16.77      ,  17.81      ,  19.16      ,  19.19      ,
        19.64      ,  20.18      ,  22.97
    ])

    df = load_basic_data()
    df = ros._do_ros(df, 'conc', 'censored', numpy.log, numpy.exp)
    result = df['final'].values
    npt.assert_array_almost_equal(result, expected) 
Example #4
Source File: test_ros.py    From vnpy_crypto with MIT License 6 votes vote down vote up
def test__impute():
    expected = numpy.array([
         3.11279729,   3.60634338,   4.04602788,   4.04602788,
         4.71008116,   6.14010906,   6.97841457,   2.        ,
         4.2       ,   4.62      ,   5.57      ,   5.66      ,
         5.86      ,   6.65      ,   6.78      ,   6.79      ,
         7.5       ,   7.5       ,   7.5       ,   8.63      ,
         8.71      ,   8.99      ,   9.85      ,  10.82      ,
        11.25      ,  11.25      ,  12.2       ,  14.92      ,
        16.77      ,  17.81      ,  19.16      ,  19.19      ,
        19.64      ,  20.18      ,  22.97
    ])
    df = load_advanced_data()
    df = ros._impute(df, 'conc', 'censored', numpy.log, numpy.exp)
    result = df['final'].values
    npt.assert_array_almost_equal(result, expected) 
Example #5
Source File: test_pricing_class.py    From fftoptionlib with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def test_cal_price_fractional_fft(self):
        fft_pricer = FourierPricer(self.vanilla_option)
        strike_arr = np.array([5, 10, 30, 36, 50, 60, 100])
        put_call_arr = np.array(['call', 'call', 'put', 'call', 'call', 'put', 'call'])
        exp = np.array(
            [3.09958567e+01, 2.60163625e+01, 8.25753140e-05, 8.12953226e-01,
             8.97449491e-11, 2.37785797e+01, 2.19293560e-85, ]
        )
        volatility = 0.20
        N = 2 ** 14
        d_u = 0.01
        d_k = 0.01
        alpha = 1
        fft_pricer.set_log_st_process(BlackScholes(volatility))
        fft_pricer.set_pricing_engine(FractionFFTEngine(N, d_u, d_k, alpha, spline_order=3))
        res = fft_pricer.calc_price(strike_arr, put_call_arr, put_label='put')
        npt.assert_array_almost_equal(res, exp, 6) 
Example #6
Source File: basic_test.py    From osqp-python with Apache License 2.0 6 votes vote down vote up
def test_upper_triangular_P(self):
        res_default = self.model.solve()

        # Get upper triangular P
        P_triu = sparse.triu(self.P, format='csc')

        # Setup and solve with upper triangular part only
        m = osqp.OSQP()
        m.setup(P=P_triu, q=self.q, A=self.A, l=self.l, u=self.u,
                **self.opts)
        res_triu = m.solve()

        # Assert equal
        nptest.assert_array_almost_equal(res_default.x, res_triu.x)
        nptest.assert_array_almost_equal(res_default.y, res_triu.y)
        nptest.assert_array_almost_equal(res_default.info.obj_val, 
                                         res_triu.info.obj_val) 
Example #7
Source File: test_pricing_class.py    From fftoptionlib with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def test_cal_price_cosine_2(self):
        cosine_pricer = FourierPricer(self.vanilla_option)
        strike_arr = np.array([5, 10, 30, 36, 50, 60])
        put_call_arr = np.array(['call', 'call', 'put', 'call', 'call', 'put'])
        exp = np.array([3.09958568e+01, 2.60164423e+01, 9.56077953e-02, 8.81357807e-01,
                        1.41769466e-10, 2.37785797e+01]
                       )

        volatility = 0.20
        N = 2000

        cosine_pricer.set_log_st_process(MertonJump(sigma=volatility,
                                                    jump_rate=0.090913148257155449,
                                                    norm_m=-0.91157356544103341,
                                                    norm_sig=7.3383200797618833e-05))
        cosine_pricer.set_pricing_engine(CosineEngine(N, L=30))
        res = cosine_pricer.calc_price(strike_arr, put_call_arr, put_label='put')
        npt.assert_array_almost_equal(res, exp, 6) 
Example #8
Source File: test_pricing_class.py    From fftoptionlib with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def test_cal_price_cosine_3(self):
        cosine_pricer = FourierPricer(self.vanilla_option)
        strike_arr = np.array([5, 10, 30, 36, 40, 60])
        put_call_arr = np.array(['call', 'call', 'put', 'call', 'call', 'put'])
        exp = np.array([3.09958567e+01, 2.60163625e+01, 1.71886506e-04, 8.75203272e-01,
                        3.55292239e-02, 2.37785797e+01]
                       )

        volatility = 0.20
        N = 2000

        cosine_pricer.set_log_st_process(KouJump(sigma=volatility,
                                                 jump_rate=23.339325557373201,
                                                 exp_pos=59.378410421004197,
                                                 exp_neg=-59.447921334340137,
                                                 prob_pos=-200.08018971817182))
        cosine_pricer.set_pricing_engine(CosineEngine(N, L=30))
        res = cosine_pricer.calc_price(strike_arr, put_call_arr, put_label='put')
        npt.assert_array_almost_equal(res, exp, 6) 
Example #9
Source File: test_pricing_class.py    From fftoptionlib with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def test_cal_price_cosine_5(self):
        cosine_pricer = FourierPricer(self.vanilla_option)
        strike_arr = np.array([5, 10, 30, 36, 40, 60])
        put_call_arr = np.array(['call', 'call', 'put', 'call', 'call', 'put'])
        exp = np.array([22.48662613, 17.50712233, -8.50824394, -7.13267131, -7.22087064,
                        16.09965887]
                       )

        volatility = 0.20
        N = 2000

        cosine_pricer.set_log_st_process(Poisson(
            jump_rate=0.339325557373201,
        ))
        cosine_pricer.set_pricing_engine(CosineEngine(N, L=30))
        res = cosine_pricer.calc_price(strike_arr, put_call_arr, put_label='put')
        npt.assert_array_almost_equal(res, exp, 6) 
Example #10
Source File: test_pooling_helper.py    From iwcs2017-answer-selection with Apache License 2.0 6 votes vote down vote up
def test_attention_softmax(self):
        vector_in = tf.constant([
            [1., 2., 1., 2.0],
            [.3, .2, .9, .3]
        ])
        padding = tf.constant([
            [1., 1., 1., 0.],
            [1., 1., 0., 0.]
        ])

        result = self.sess.run(attention_softmax(vector_in, padding))
        reference_value = np.array([
            [0.21194156, 0.57611692, 0.21194156, 0.],
            [0.52497919, 0.47502081, 0., 0.]
        ])

        npt.assert_array_almost_equal(result, reference_value) 
Example #11
Source File: test_neuralODE.py    From astroNN with MIT License 6 votes vote down vote up
def test_ODEbadprecision(self):  # make sure float32 is not enough for very precise integration
        t = tf.constant(np.linspace(0, 10, 1000), dtype=tf.float32)
        # initial condition
        true_y0 = tf.constant([0., 5.], dtype=tf.float32)

        true_func = lambda y, t: np.sin(5*t)
        ode_func = lambda y, t: tf.cast(tf.stack([5*tf.cos(5*t), -25*tf.sin(5*t)]), tf.float32)
        true_y = odeint(ode_func, true_y0, t, method='dop853', precision=tf.float32)
        self.assertRaises(AssertionError, npt.assert_array_almost_equal, true_y.numpy()[:, 0], true_func(true_y0, t))

        true_y0_pretend_multidims = [[0., 5.]]  # to introduce a mix of list, np array, tensor to make sure no issue
        true_y_pretend_multidims = odeint(ode_func, true_y0_pretend_multidims, t, method='dop853', precision=tf.float32)

        # assert equal pretendinging multidim or not
        np.testing.assert_array_almost_equal(true_y_pretend_multidims[0], true_y)

        true_y0_multidims = tf.constant([[1., 2.], [0., 5.]], dtype=tf.float32)
        t = np.linspace(0, 10, 1000)
        true_y_multidims = odeint(ode_func, true_y0_multidims, t, method='dop853', precision=tf.float32)

        # assert equal in multidim or not
        np.testing.assert_array_almost_equal(true_y_multidims[1], true_y) 
Example #12
Source File: test_numpy_tools.py    From astroNN with MIT License 6 votes vote down vote up
def test_regularizator(self):
        # make sure its the same as tensorflow
        x = np.array([-1., 2., 3., 4.])
        reg = 0.2

        astroNN_x = l1(x, l1=reg)
        astroNN_x_2 = l2(x, l2=reg)

        with tf.device("/cpu:0"), context.eager_mode():
            l1_reg = tf.keras.regularizers.l1(l=reg)
            l2_reg = tf.keras.regularizers.l2(l=reg)
            tf_x = l1_reg(tf.convert_to_tensor(x))
            tf_x_2 = l2_reg(tf.convert_to_tensor(x))

            npt.assert_array_almost_equal(tf_x.numpy(), astroNN_x)
            npt.assert_array_almost_equal(tf_x_2.numpy(), astroNN_x_2) 
Example #13
Source File: test_pricing_class.py    From fftoptionlib with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def test_cal_price_cosine_6(self):
        cosine_pricer = FourierPricer(self.vanilla_option)
        strike_arr = np.array([5, 10, 30, 36, 40, 60])
        put_call_arr = np.array(['call', 'call', 'put', 'call', 'call', 'put'])
        exp = np.array([30.9958673, 26.01656399, 0.15928293, 1.72971868, 0.56756891, 23.82357896])

        volatility = 0.20
        N = 2000

        cosine_pricer.set_log_st_process(CGMY(c=0.1,
                                              g=2,
                                              m=2,
                                              y=1.5))
        cosine_pricer.set_pricing_engine(CosineEngine(N, L=30))
        res = cosine_pricer.calc_price(strike_arr, put_call_arr, put_label='put')
        npt.assert_array_almost_equal(res, exp, 6) 
Example #14
Source File: test_gaia_tools.py    From astroNN with MIT License 6 votes vote down vote up
def test_logsol(self):
        # Test conversion tools related to log solar luminosity
        from astroNN.gaia import fakemag_to_logsol, absmag_to_logsol, logsol_to_absmag, logsol_to_fakemag
        self.assertEqual(logsol_to_fakemag(fakemag_to_logsol(100.)), 100.)
        npt.assert_array_equal(logsol_to_fakemag(fakemag_to_logsol([100., 100.])), [100., 100.])
        npt.assert_array_equal(logsol_to_fakemag(fakemag_to_logsol(np.array([100, 100, 100]))), [100., 100., 100.])
        self.assertEqual(fakemag_to_logsol(MAGIC_NUMBER), MAGIC_NUMBER)
        self.assertEqual(logsol_to_fakemag(fakemag_to_logsol(MAGIC_NUMBER)), MAGIC_NUMBER)
        self.assertEqual(np.any(fakemag_to_logsol([MAGIC_NUMBER, 1000]) == MAGIC_NUMBER), True)

        self.assertEqual(logsol_to_absmag(absmag_to_logsol(99.)), 99.)
        self.assertAlmostEqual(logsol_to_absmag(absmag_to_logsol(-99.)), -99.)
        npt.assert_array_equal(logsol_to_absmag(absmag_to_logsol([99., 99.])), [99., 99.])
        npt.assert_array_almost_equal(logsol_to_absmag(absmag_to_logsol([-99., -99.])), [-99., -99.])
        npt.assert_array_almost_equal(logsol_to_absmag(absmag_to_logsol(np.array([99., 99., 99.]))), [99., 99., 99.])
        self.assertEqual(absmag_to_logsol(MAGIC_NUMBER), MAGIC_NUMBER)
        self.assertEqual(logsol_to_absmag(absmag_to_logsol(MAGIC_NUMBER)), MAGIC_NUMBER)
        self.assertEqual(np.any(absmag_to_logsol([MAGIC_NUMBER, 1000]) == MAGIC_NUMBER), True) 
Example #15
Source File: feasibility_test.py    From osqp-python with Apache License 2.0 6 votes vote down vote up
def test_feasibility_problem(self):

        # Solve problem
        res = self.model.solve()

        # Assert close
        nptest.assert_array_almost_equal(
            res.x,
            np.array([-0.0656074, 1.04194398, 0.4756959, -1.64036689,
                      -0.34180168, -0.81696303, -1.06389178, 0.44944554,
                      -0.44829675, -1.01289944, -0.12513655, 0.02267293,
                      -1.15206474, 1.06817424, 1.18143313, 0.01690332,
                      -0.11373645, -0.48115767,  0.25373436, 0.81369707,
                      0.18883475, 0.47000419, -0.24932451, 0.09298623,
                      1.88381076, 0.77536814, -1.35971433, 0.51511176,
                      0.03317466, 0.90226419]), decimal=3)
        nptest.assert_array_almost_equal(res.y, np.zeros(self.m), decimal=3)
        nptest.assert_array_almost_equal(res.info.obj_val, 0., decimal=3) 
Example #16
Source File: codegen_vectors_test.py    From osqp-python with Apache License 2.0 6 votes vote down vote up
def test_update_bounds(self):
        import vec_emosqp

        # Update upper bound
        l_new = -100. * np.ones(self.m)
        u_new = 1000. * np.ones(self.m)
        vec_emosqp.update_bounds(l_new, u_new)
        x, y, _, _, _ = vec_emosqp.solve()

        # Assert close
        nptest.assert_array_almost_equal(
            x, np.array([-0.12727273, -19.94909091]), decimal=4)
        nptest.assert_array_almost_equal(
            y, np.array([0., 0., 0., -0.8, 0.]), decimal=4)

        # Update upper bound to the original value
        vec_emosqp.update_bounds(self.l, self.u) 
Example #17
Source File: codegen_vectors_test.py    From osqp-python with Apache License 2.0 6 votes vote down vote up
def test_update_u(self):
        import vec_emosqp

        # Update upper bound
        u_new = 1000. * np.ones(self.m)
        vec_emosqp.update_upper_bound(u_new)
        x, y, _, _, _ = vec_emosqp.solve()

        # Assert close
        nptest.assert_array_almost_equal(
            x, np.array([-1.51515152e-01, -3.33282828e+02]), decimal=4)
        nptest.assert_array_almost_equal(
            y, np.array([0., 0., 1.33333333, 0., 0.]), decimal=4)

        # Update upper bound to the original value
        vec_emosqp.update_upper_bound(self.u) 
Example #18
Source File: codegen_matrices_test.py    From osqp-python with Apache License 2.0 6 votes vote down vote up
def test_update_P_allind(self):
        import mat_emosqp

        # Update matrix P
        Px = self.P_new.data
        mat_emosqp.update_P(Px, None, 0)
        x, y, _, _, _ = mat_emosqp.solve()

        # Assert close
        nptest.assert_array_almost_equal(x, np.array([0., 5.]), decimal=5)
        nptest.assert_array_almost_equal(
            y, np.array([0., 0., 3., 0., 0.]), decimal=5)

        # Update matrix P to the original value
        Px_idx = np.arange(self.P.nnz)
        mat_emosqp.update_P(Px, Px_idx, len(Px)) 
Example #19
Source File: codegen_matrices_test.py    From osqp-python with Apache License 2.0 6 votes vote down vote up
def test_update_A(self):
        import mat_emosqp

        # Update matrix A
        Ax = self.A_new.data
        Ax_idx = np.arange(self.A_new.nnz)
        mat_emosqp.update_A(Ax, Ax_idx, len(Ax))

        # Solve problem
        x, y, _, _, _ = mat_emosqp.solve()

        # Assert close
        nptest.assert_array_almost_equal(x,
            np.array([0.15765766, 7.34234234]), decimal=5)
        nptest.assert_array_almost_equal(
            y, np.array([0., 0., 2.36711712, 0., 0.]), decimal=5)

        # Update matrix A to the original value
        Ax = self.A.data
        Ax_idx = np.arange(self.A.nnz)
        mat_emosqp.update_A(Ax, Ax_idx, len(Ax)) 
Example #20
Source File: codegen_matrices_test.py    From osqp-python with Apache License 2.0 6 votes vote down vote up
def test_update_A_allind(self):
        import mat_emosqp

        # Update matrix A
        Ax = self.A_new.data
        mat_emosqp.update_A(Ax, None, 0)
        x, y, _, _, _ = mat_emosqp.solve()

        # Assert close
        nptest.assert_array_almost_equal(x,
            np.array([0.15765766, 7.34234234]), decimal=5)
        nptest.assert_array_almost_equal(
            y, np.array([0., 0., 2.36711712, 0., 0.]), decimal=5)

        # Update matrix A to the original value
        Ax = self.A.data
        Ax_idx = np.arange(self.A.nnz)
        mat_emosqp.update_A(Ax, Ax_idx, len(Ax)) 
Example #21
Source File: codegen_matrices_test.py    From osqp-python with Apache License 2.0 6 votes vote down vote up
def test_update_P_A_indP_indA(self):
        import mat_emosqp

        # Update matrices P and A
        Px = self.P_new.data
        Px_idx = np.arange(self.P_new.nnz)
        Ax = self.A_new.data
        Ax_idx = np.arange(self.A_new.nnz)
        mat_emosqp.update_P_A(Px, Px_idx, len(Px), Ax, Ax_idx, len(Ax))

        # Solve problem
        x, y, _, _, _ = mat_emosqp.solve()

        # Assert close
        nptest.assert_array_almost_equal(x, np.array([4.25, 3.25]), decimal=5)
        nptest.assert_array_almost_equal(
            y, np.array([0., 0., 3.625, 0., 0.]), decimal=5)

        # Update matrices P and A to the original values
        Px = self.P.data
        Ax = self.A.data
        mat_emosqp.update_P_A(Px, None, 0, Ax, None, 0) 
Example #22
Source File: codegen_matrices_test.py    From osqp-python with Apache License 2.0 6 votes vote down vote up
def test_update_P_A_indP(self):
        import mat_emosqp

        # Update matrices P and A
        Px = self.P_new.data
        Px_idx = np.arange(self.P_new.nnz)
        Ax = self.A_new.data
        mat_emosqp.update_P_A(Px, Px_idx, len(Px), Ax, None, 0)
        x, y, _, _, _ = mat_emosqp.solve()

        # Assert close
        nptest.assert_array_almost_equal(x, np.array([4.25, 3.25]), decimal=5)
        nptest.assert_array_almost_equal(
            y, np.array([0., 0., 3.625, 0., 0.]), decimal=5)

        # Update matrices P and A to the original values
        Px = self.P.data
        Ax = self.A.data
        mat_emosqp.update_P_A(Px, None, 0, Ax, None, 0) 
Example #23
Source File: codegen_matrices_test.py    From osqp-python with Apache License 2.0 6 votes vote down vote up
def test_update_P_A_allind(self):
        import mat_emosqp

        # Update matrices P and A
        Px = self.P_new.data
        Ax = self.A_new.data
        mat_emosqp.update_P_A(Px, None, 0, Ax, None, 0)
        x, y, _, _, _ = mat_emosqp.solve()

        # Assert close
        nptest.assert_array_almost_equal(x, np.array([4.25, 3.25]), decimal=5)
        nptest.assert_array_almost_equal(y, np.array([0., 0., 3.625, 0., 0.]), decimal=5)

        # Update matrices P and A to the original values
        Px = self.P.data
        Ax = self.A.data
        mat_emosqp.update_P_A(Px, None, 0, Ax, None, 0) 
Example #24
Source File: unconstrained_test.py    From osqp-python with Apache License 2.0 6 votes vote down vote up
def test_unconstrained_problem(self):

        # Solve problem
        res = self.model.solve()

        # Assert close
        nptest.assert_array_almost_equal(
            res.x, np.array([
                -0.61981415, -0.06174194, 0.83824061, -0.0595013, -0.17810828,
                2.90550031, -1.8901713, -1.91191741, -3.73603446, 1.7530356,
                -1.67018181, 3.42221944, 0.61263403, -0.45838347, -0.13194248,
                2.95744794, 5.2902277, -1.42836238, -8.55123842, -0.79093815,
                0.43418189, -0.69323554, 1.15967924, -0.47821898, 3.6108927,
                0.03404309, 0.16322926, -2.17974795, 0.32458796, -1.97553574]))
        nptest.assert_array_almost_equal(res.y, np.array([]))
        nptest.assert_array_almost_equal(res.info.obj_val, -35.020288603855825) 
Example #25
Source File: polishing_test.py    From osqp-python with Apache License 2.0 6 votes vote down vote up
def test_polish_simple(self):

        # Simple QP problem
        self.P = sparse.diags([11., 0.], format='csc')
        self.q = np.array([3, 4])
        self.A = sparse.csc_matrix([[-1, 0], [0, -1], [-1, -3], [2, 5], [3, 4]])
        self.u = np.array([0, 0, -15, 100, 80])
        self.l = -np.inf * np.ones(len(self.u))
        self.n = self.P.shape[0]
        self.m = self.A.shape[0]
        self.model = osqp.OSQP()
        self.model.setup(P=self.P, q=self.q, A=self.A, l=self.l, u=self.u,
                         **self.opts)

        # Solve problem
        res = self.model.solve()

        # Assert close
        nptest.assert_array_almost_equal(res.x, np.array([0., 5.]))
        nptest.assert_array_almost_equal(res.y, np.array([1.66666667, 0.,
                                                          1.33333333, 0., 0.]))
        nptest.assert_array_almost_equal(res.info.obj_val, 20.) 
Example #26
Source File: test_scripts.py    From pyAFQ with BSD 2-Clause "Simplified" License 6 votes vote down vote up
def test_predict_dki():
    with nbtmp.InTemporaryDirectory() as tmpdir:
        fbval = op.join(tmpdir, 'dki.bval')
        fbvec = op.join(tmpdir, 'dki.bvec')
        fdata = op.join(tmpdir, 'dki.nii.gz')
        make_dki_data(fbval, fbvec, fdata)
        cmd1 = ["pyAFQ_dki", "-d", fdata, "-l", fbval, "-c", fbvec,
                "-o", tmpdir]
        out = runner.run_command(cmd1)
        npt.assert_equal(out[0], 0)

        # Get expected values
        fparams = op.join(tmpdir, "dki_params.nii.gz")
        cmd2 = ["pyAFQ_dki_predict", "-p", fparams, "-l", fbval, "-c", fbvec,
                "-o", tmpdir, '-b', '0']
        out = runner.run_command(cmd2)
        npt.assert_equal(out[0], 0)
        pred = nib.load(op.join(tmpdir, "dki_prediction.nii.gz")).get_fdata()
        data = nib.load(op.join(tmpdir, "dki.nii.gz")).get_fdata()
        npt.assert_array_almost_equal(pred, data) 
Example #27
Source File: test_scripts.py    From pyAFQ with BSD 2-Clause "Simplified" License 6 votes vote down vote up
def test_predict_dti():
    with nbtmp.InTemporaryDirectory() as tmpdir:
        fbval = op.join(tmpdir, 'dti.bval')
        fbvec = op.join(tmpdir, 'dti.bvec')
        fdata = op.join(tmpdir, 'dti.nii.gz')
        make_dti_data(fbval, fbvec, fdata)
        cmd1 = ["pyAFQ_dti", "-d", fdata, "-l", fbval, "-c", fbvec,
                "-o", tmpdir]
        out = runner.run_command(cmd1)
        npt.assert_equal(out[0], 0)
        # Get expected values
        fparams = op.join(tmpdir, "dti_params.nii.gz")
        cmd2 = ["pyAFQ_dti_predict", "-p", fparams, "-l", fbval, "-c", fbvec,
                "-o", tmpdir, '-b', '0']
        out = runner.run_command(cmd2)
        npt.assert_equal(out[0], 0)
        pred = nib.load(op.join(tmpdir, "dti_prediction.nii.gz")).get_fdata()
        data = nib.load(op.join(tmpdir, "dti.nii.gz")).get_fdata()
        npt.assert_array_almost_equal(pred, data) 
Example #28
Source File: basic_test.py    From osqp-python with Apache License 2.0 5 votes vote down vote up
def test_update_l(self):
        # Update lower bound
        l_new = -100 * np.ones(self.m)
        self.model.update(l=l_new)
        res = self.model.solve()

        # Assert close
        nptest.assert_array_almost_equal(res.x, np.array([0., 5.]))
        nptest.assert_array_almost_equal(res.y, np.array([1.66666667, 0.,
                                                          1.33333333, 0., 0.]))
        nptest.assert_array_almost_equal(res.info.obj_val, 20.) 
Example #29
Source File: codegen_vectors_test.py    From osqp-python with Apache License 2.0 5 votes vote down vote up
def test_solve(self):
        # Generate the code
        self.model.codegen('code', python_ext_name='vec_emosqp',
                           force_rewrite=True)
        sh.rmtree('code')
        import vec_emosqp

        # Solve problem
        x, y, _, _, _ = vec_emosqp.solve()

        # Assert close
        nptest.assert_array_almost_equal(x, np.array([0., 5.]), decimal=5)
        nptest.assert_array_almost_equal(
            y, np.array([1.66666667, 0., 1.33333333, 0., 0.]), decimal=5) 
Example #30
Source File: test_pricing_class.py    From fftoptionlib with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def test_cal_price_cosine(self):
        cosine_pricer = FourierPricer(self.vanilla_option)
        strike_arr = np.array([5, 10, 30, 36, 50, 60, 100])
        put_call_arr = np.array(['call', 'call', 'put', 'call', 'call', 'put', 'call'])
        exp = np.array([3.09958567e+01, 2.60163625e+01, 8.25753140e-05, 8.12953226e-01,
                        8.97449491e-11, 2.37785797e+01, 2.19293560e-85, ]
                       )

        volatility = 0.20
        N = 150

        cosine_pricer.set_log_st_process(BlackScholes(volatility))
        cosine_pricer.set_pricing_engine(CosineEngine(N, L=30))
        res = cosine_pricer.calc_price(strike_arr, put_call_arr, put_label='put')
        npt.assert_array_almost_equal(res, exp, 6)