def test_intercept_flag(rows=10, columns=9):
        inout = get_random_array(rows, columns)
        x = inout[0]
        y = inout[1]

        ntX = HomogenNumericTable(x)
        ntY = HomogenNumericTable(y)

        ridge_training_algorithm = ridge_training.Batch()
        ridge_training_algorithm.input.set(ridge_training.data, ntX)
        ridge_training_algorithm.input.set(ridge_training.dependentVariables, ntY)

        # set parameter
        alpha = 1.0
        alpha_nt = HomogenNumericTable(np.array([alpha], ndmin=2))
        ridge_training_algorithm.parameter.ridgeParameters = alpha_nt

        result = ridge_training_algorithm.compute()

        model = result.get(ridge_training.model)
        beta_coeff = model.getBeta()
        np_beta = getNumpyArray(beta_coeff)
        daal_intercept = np_beta[0,0]

        regression = ScikitRidgeRegression(alpha=1.0, fit_intercept=True)
        regression.fit(x, y)

        scikit_intercept = regression.intercept_
        assert_array_almost_equal(scikit_intercept, [daal_intercept])
Example #2
0
def check_chisquare(f_obs, f_exp, ddof, axis, expected_chi2):
    # Use this only for arrays that have no masked values.
    f_obs = np.asarray(f_obs)
    if axis is None:
        num_obs = f_obs.size
    else:
        if axis == 'no':
            use_axis = 0
        else:
            use_axis = axis
        b = np.broadcast(f_obs, f_exp)
        num_obs = b.shape[use_axis]

    if axis == 'no':
        chi2, p = mstats.chisquare(f_obs, f_exp=f_exp, ddof=ddof)
    else:
        chi2, p = mstats.chisquare(f_obs, f_exp=f_exp, ddof=ddof, axis=axis)
    assert_array_equal(chi2, expected_chi2)

    ddof = np.asarray(ddof)
    expected_p = stats.chisqprob(expected_chi2, num_obs - 1 - ddof)
    assert_array_equal(p, expected_p)

    # Also compare to stats.chisquare
    if axis == 'no':
        stats_chisq, stats_p = stats.chisquare(f_obs, f_exp=f_exp, ddof=ddof)
    else:
        stats_chisq, stats_p = stats.chisquare(f_obs,
                                               f_exp=f_exp,
                                               ddof=ddof,
                                               axis=axis)
    assert_array_almost_equal(chi2, stats_chisq)
    assert_array_almost_equal(p, stats_p)
    def get_daal_prediction(x=np.arange(10).reshape(10,1), y=np.arange(10).reshape(10,1)):

        ntX = HomogenNumericTable(x)
        ntY = HomogenNumericTable(y)

        ridge_training_algorithm = ridge_training.Batch()
        ridge_training_algorithm.input.set(ridge_training.data, ntX)
        ridge_training_algorithm.input.set(ridge_training.dependentVariables, ntY)

        # set parameter
        alpha = 0.0
        alpha_nt = HomogenNumericTable(np.array([alpha], ndmin=2))
        ridge_training_algorithm.parameter.ridgeParameters = alpha_nt

        result = ridge_training_algorithm.compute()
        model = result.get(ridge_training.model)

        ridge_prediction_algorithm = ridge_prediction.Batch()
        ridge_prediction_algorithm.input.setModel(ridge_prediction.model, model)
        ridge_prediction_algorithm.input.setTable(ridge_prediction.data, ntX)
        result = ridge_prediction_algorithm.compute()

        np_predicted = getNumpyArray(result.get(ridge_prediction.prediction))
        # assert the same as the initial dependent variable
        assert_array_almost_equal(y, np_predicted)
        return np_predicted
Example #4
0
    def test_svd_daal_vs_sklearn(rows=1000, columns=1000):
        indata = get_random_array(rows, columns)
        daal_input = HomogenNumericTable(indata)
        algorithm = svd.Batch()
        algorithm.input.set(svd.data, daal_input)

        start_sklearn = time.time()
        _U, s, _Vh = np.linalg.svd(indata, full_matrices=False)
        end_sklearn = time.time()

        start_daal = time.time()
        result = algorithm.compute()
        end_daal = time.time()

        if os.getenv("CHECKPERFORMANCE") is not None:
            assert (end_daal - start_daal <= end_sklearn - start_sklearn)

        sigma = getNumpyArray(result.get(svd.singularValues))
        _rows, cols = sigma.shape
        d_sigma = sigma.reshape(cols, )

        assert_array_almost_equal(d_sigma, s)

        print("SVD for matrix[{}][{}]".format(rows, columns))
        print("+ Sklearn SVD: {}".format(end_sklearn - start_sklearn))
        print("+ Sklearn Daal: {}".format(end_daal - start_daal))
def check_chisquare(f_obs, f_exp, ddof, axis, expected_chi2):
    # Use this only for arrays that have no masked values.
    f_obs = np.asarray(f_obs)
    if axis is None:
        num_obs = f_obs.size
    else:
        if axis == 'no':
            use_axis = 0
        else:
            use_axis = axis
        b = np.broadcast(f_obs, f_exp)
        num_obs = b.shape[use_axis]

    if axis == 'no':
        chi2, p = mstats.chisquare(f_obs, f_exp=f_exp, ddof=ddof)
    else:
        chi2, p = mstats.chisquare(f_obs, f_exp=f_exp, ddof=ddof, axis=axis)
    assert_array_equal(chi2, expected_chi2)

    ddof = np.asarray(ddof)
    expected_p = stats.chisqprob(expected_chi2, num_obs - 1 - ddof)
    assert_array_equal(p, expected_p)

    # Also compare to stats.chisquare
    if axis == 'no':
        stats_chisq, stats_p = stats.chisquare(f_obs, f_exp=f_exp, ddof=ddof)
    else:
        stats_chisq, stats_p = stats.chisquare(f_obs, f_exp=f_exp, ddof=ddof,
                                               axis=axis)
    assert_array_almost_equal(chi2, stats_chisq)
    assert_array_almost_equal(p, stats_p)
Example #6
0
    def test_kalman(self):
        # V0 = 12
        h = np.atleast_2d(1)  # voltimeter measure the voltage itself
        q = 1e-9  # variance of process noise as the car operates
        r = 0.05 ** 2  # variance of measurement error
        b = 0  # no system input
        u = 0  # no system input
        filt = Kalman(R=r, A=1, Q=q, H=h, B=b)

        # Generate random voltages and watch the filter operate.
        n = 50

        # truth = np.random.randn(n) * np.sqrt(q) + V0
        truth = np.array([12.0000146639, 12.0000065349, 12.0000032619, 11.9999884321, 11.9999738705, 11.9999717554, 12.000012208, 12.0000269596, 12.0000235391, 12.0000420788, 11.9999763588, 12.0000272251, 11.9999278487, 11.9999775611, 12.0000280208, 11.9999788802, 11.9999531533, 11.9999963402, 11.9999318055, 11.9999537065, 12.0000011098, 11.9999750291, 12.000012755, 11.9999355349, 11.9999960117, 12.0000033276, 12.0000100317, 11.9999962376, 12.0000527124, 12.000019087, 12.0000273276, 11.9999461331, 12.0000527385, 11.9999990413, 11.9999850764, 12.0000031025, 11.9999868222, 11.9999830042, 11.9999881952, 12.0000064122, 12.0000343455, 11.9999779108, 12.0000134648, 12.0000329604, 12.0000536949, 11.999990415, 11.9999935299, 11.9999850717, 12.0000048284, 12.000060395])  # @IgnorePep8
        # print(', '.join(['{0}'.format(y) for y in truth]))
        # noise = np.random.randn(n) * np.sqrt(r)
        noise = np.array([0.0621366489443, 0.0247330994998, 0.109271521369, -0.0567657373852, -0.0315244553453, -0.0025701462034, 0.00181288937042, 0.109022849782, 0.0241169949603, -0.0543790728855, 0.0380117839575, 0.0582078684346, -0.0734606815139, -0.0665977120387, 0.0270631615171, 0.0369150024436, 0.0788445252483, 0.0111006834355, 0.0169567513801, 0.057081645789, -0.0293106512335, 0.0120282068494, 0.0211769283138, -0.0703228625119, -0.0222740803259, 0.0636500853308, 0.0302268639638, 0.0823316446844, 0.0504871636818, -0.0295402185417, 0.0780534199429, -0.0275453184976, -0.0613567940452, -0.0416930336494, -0.0972202720552, 0.0367572628693, 0.0150242119742, -0.0269063072041, 0.0654834111234, 0.0281867030757, -0.0978716452136, -0.0209143055692, -0.0236015624104, 0.0215804707908, -0.0525669442916, -0.00999304544556, -0.120029281573, -0.0255283929591, -0.00426938028504, -0.0487170747831])  # @IgnorePep8
        # print('noise')
        # print(', '.join(['{0}'.format(y) for y in noise]))
        z = truth + noise  # measurement
        x = np.zeros(n)

        for i, zi in enumerate(z):
            x[i] = filt(zi, u)  # perform a Kalman filter iteration

        print(', '.join(['{0}'.format(y) for y in x]))
        assert_array_almost_equal(x, [12.0621513128, 12.0434454699, 12.065388589, 12.0348470726, 12.0215675093, 12.0175398445, 12.0152948689, 12.0270143185, 12.0266950063, 12.0185917138, 12.0203550503, 12.0235117738, 12.0160466547, 12.0101417447, 12.0112717378, 12.0128731715, 12.0167512202, 12.0164370857, 12.0164608484, 12.0184896738, 12.0162133963, 12.0160220138, 12.0162467099, 12.0126366982, 12.0112399981, 12.01325607, 12.0138850472, 12.0163296762, 12.0175094638, 12.0159415984, 12.0179463317, 12.0165228489, 12.0141641309, 12.0125210028, 12.0093846209, 12.0101451843, 12.0102767167, 12.0092975878, 12.0107382282, 12.01117469, 12.0085152867, 12.0078138971, 12.0070834443, 12.0074137537, 12.0060816948, 12.0057319401, 12.0030552687, 12.0024592874, 12.0023220236, 12.0013021198])  # @IgnorePep8
 def test_case(self):
     model = clone(estimator)
     model.fit(**fit_data)
     for method in methods:
         try:
             code = sklearn2code(model, method, javascript)
         except ExpressionTypeNotSupportedError:
             continue
         js = execjs.get('Node')
         context = js.compile(code)
         exported_pred = []
         try:
             for _, row in export_predict_data['X'].iterrows():
                 val = context.eval('%s(%s)' % (method, ', '.join(
                     [str(x) if x == x else 'NaN' for x in row])))
                 exported_pred.append(val)
             exported_pred = np.array(exported_pred)
             pred = DataFrame(getattr(model, method)(**predict_data))
             assert_array_almost_equal(np.ravel(pred),
                                       np.ravel(exported_pred), 3)
         except:
             #                 print(code)
             import clipboard
             clipboard.copy(code)
             raise
Example #8
0
    def test_kurtosis(self):
        # Set flags for axis = 0 and fisher=0 (Pearson's definition of kurtosis
        # for compatibility with Matlab)
        y = mstats.kurtosis(self.testmathworks, 0, fisher=0, bias=1)
        assert_almost_equal(y, 2.1658856802973, 10)
        # Note that MATLAB has confusing docs for the following case
        #  kurtosis(x,0) gives an unbiased estimate of Pearson's skewness
        #  kurtosis(x)  gives a biased estimate of Fisher's skewness (Pearson-3)
        #  The MATLAB docs imply that both should give Fisher's
        y = mstats.kurtosis(self.testmathworks, fisher=0, bias=0)
        assert_almost_equal(y, 3.663542721189047, 10)
        y = mstats.kurtosis(self.testcase, 0, 0)
        assert_almost_equal(y, 1.64)

        # test that kurtosis works on multidimensional masked arrays
        correct_2d = ma.array(
            np.array([-1.5, -3.0, -1.47247052385, 0.0, -1.26979517952]),
            mask=np.array([False, False, False, True, False], dtype=np.bool),
        )
        assert_array_almost_equal(mstats.kurtosis(self.testcase_2d, 1), correct_2d)
        for i, row in enumerate(self.testcase_2d):
            assert_almost_equal(mstats.kurtosis(row), correct_2d[i])

        correct_2d_bias_corrected = ma.array(
            np.array([-1.5, -3.0, -1.88988209538, 0.0, -0.5234638463918877]),
            mask=np.array([False, False, False, True, False], dtype=np.bool),
        )
        assert_array_almost_equal(mstats.kurtosis(self.testcase_2d, 1, bias=False), correct_2d_bias_corrected)
        for i, row in enumerate(self.testcase_2d):
            assert_almost_equal(mstats.kurtosis(row, bias=False), correct_2d_bias_corrected[i])

        # Check consistency between stats and mstats implementations
        assert_array_almost_equal_nulp(mstats.kurtosis(self.testcase_2d[2, :]), stats.kurtosis(self.testcase_2d[2, :]))
Example #9
0
    def test_linear_regression_simple():

        # calculate beta coefficients
        x = np.array([0., 2., 3.]).reshape(3, 1)

        nt_x = nt_y = HomogenNumericTable(x)

        lr_alg = linear_training.Batch(method=linear_training.qrDense)
        lr_alg.input.set(linear_training.data, nt_x)
        lr_alg.input.set(linear_training.dependentVariables, nt_y)
        result = lr_alg.compute()
        model = result.get(linear_training.model)
        beta_coeff = model.getBeta()
        np_beta_coeff = getNumpyArray(beta_coeff)

        res_beta_coeff = np.array([0, 1]).reshape(1, 2)

        assert_almost_equal(res_beta_coeff, np_beta_coeff)

        # predict
        lr_alg_predict = linear_prediction.Batch()
        lr_alg_predict.input.setModel(linear_prediction.model, model)
        lr_alg_predict.input.setTable(linear_prediction.data, nt_x)
        result = lr_alg_predict.compute()
        np_predict = getNumpyArray(result.get(linear_prediction.prediction))
        assert_array_almost_equal(x, np_predict)
Example #10
0
def test_chisquare_masked_arrays():
    # The other tests were taken from the tests for stats.chisquare, so
    # they don't test the function with masked arrays.  Here masked arrays
    # are tested.
    obs = np.array([[8, 8, 16, 32, -1], [-1, -1, 3, 4, 5]]).T
    mask = np.array([[0, 0, 0, 0, 1], [1, 1, 0, 0, 0]]).T
    mobs = ma.masked_array(obs, mask)
    expected_chisq = np.array([24.0, 0.5])

    chisq, p = mstats.chisquare(mobs)
    assert_array_equal(chisq, expected_chisq)
    assert_array_almost_equal(
        p, stats.chisqprob(expected_chisq,
                           mobs.count(axis=0) - 1))

    chisq, p = mstats.chisquare(mobs.T, axis=1)
    assert_array_equal(chisq, expected_chisq)
    assert_array_almost_equal(
        p, stats.chisqprob(expected_chisq,
                           mobs.T.count(axis=1) - 1))

    # When axis=None, the two values should have type np.float64.
    chisq, p = mstats.chisquare([1, 2, 3], axis=None)
    assert_(isinstance(chisq, np.float64))
    assert_(isinstance(p, np.float64))
    assert_equal(chisq, 1.0)
    assert_almost_equal(p, stats.chisqprob(1.0, 2))
Example #11
0
    def test_tile_array(self):
        a = np.array([[1, 2], [3, 4], [5, 6]])

        a_tiled = hm.linalg.tile_array(a, 2)

        a_tiled_expected = np.array([[1, 2, 0, 0], [3, 4, 0, 0], [5, 6, 0, 0],
                                     [0, 0, 1, 2], [0, 0, 3, 4], [0, 0, 5, 6]])
        assert_array_almost_equal(a_tiled.toarray(), a_tiled_expected)
Example #12
0
def test_golden_section():
    data = mlclass.ex4()
    x = add_bias(data['x'])
    y = data['y']
    theta = array((.01, .01, .01))
    p = -grad(x, y, theta, 0.)
    c = lambda alpha: nan_guard(cost(x, y, theta + alpha*p, 0.))
    assert_array_almost_equal((0.000739624, 0.685489), golden_section((-1.61803, 0, 1, 880.894, 0.778512, 651.614), c))
Example #13
0
 def test_zmap(self):
     # This is not in R, so tested by using:
     #    (testcase[i]-mean(testcase,axis=0)) / sqrt(var(testcase)*3/4)
     y = mstats.zmap(self.testcase, self.testcase)
     desired_unmaskedvals = ([-1.3416407864999, -0.44721359549996,
                              0.44721359549996, 1.3416407864999])
     assert_array_almost_equal(desired_unmaskedvals,
                               y.data[y.mask == False], decimal=12)
Example #14
0
 def test_zmap(self):
     # This is not in R, so tested by using:
     #    (testcase[i]-mean(testcase,axis=0)) / sqrt(var(testcase)*3/4)
     y = mstats.zmap(self.testcase, self.testcase)
     desired_unmaskedvals = ([-1.3416407864999, -0.44721359549996,
                              0.44721359549996, 1.3416407864999])
     assert_array_almost_equal(desired_unmaskedvals,
                               y.data[y.mask == False], decimal=12)
Example #15
0
def test_line_search():
    data = mlclass.ex4()
    x = add_bias(data['x'])
    y = data['y']
    theta = array((.01, .01, .01))
    p = -grad(x, y, theta, 0.)
    c = lambda alpha: cost(x, y, theta + alpha*p, 0.)
    assert_array_almost_equal((0.000739624, 0.685489), line_search(0., 1., c))
Example #16
0
    def test_create_interpolation_least_squares_domain_repetitive_extrapolation(
            self):
        ""
        n = 16
        kh = 1.0
        max_conv_factor = 0.3
        a = hm.linalg.helmholtz_1d_operator(kh, n)
        location = np.arange(n)
        level = hm.setup.hierarchy.create_finest_level(a)
        # Generate relaxed test matrix.
        x = _get_test_matrix(a, n, 10, num_examples=8)
        coarsener = hm.setup.coarsening_uniform.UniformCoarsener(
            level, x, 4, repetitive=True)
        r, aggregate_size, num_components = coarsener.get_optimal_coarsening(
            max_conv_factor)[:3]
        assert aggregate_size == 6
        assert num_components == 2

        p = hm.setup.interpolation.create_interpolation_least_squares_domain(
            x,
            a,
            r,
            location,
            n,
            aggregate_size=aggregate_size,
            num_components=num_components,
            repetitive=True)

        num_test_examples = 5
        x_test = x[:, -num_test_examples:]
        error_a = np.mean(
            norm(a.dot(x_test - p.dot(r.dot(x_test))), axis=0) /
            norm(x_test, axis=0))
        assert p[0].nnz == 4
        assert error_a == pytest.approx(0.492, 1e-2)
        assert p.shape == (16, 6)

        # To print p:
        #print(','.join(np.array2string(y, separator=",", formatter={'float_kind':lambda x: "%.2f" % x}) for y in np.array(p.todense())))
        assert_array_almost_equal(p.todense(), [
            [-0.26, 0.17, 0.00, 0.00, -0.20, 0.14],
            [0.07, 0.56, 0.00, 0.00, -0.08, 0.20],
            [0.27, 0.11, 0.00, 0.00, 0.16, 0.02],
            [0.34, -0.25, 0.01, -0.02, 0.00, 0.00],
            [-0.03, -0.40, -0.13, -0.05, 0.00, 0.00],
            [-0.30, -0.16, -0.11, -0.01, 0.00, 0.00],
            [-0.20, 0.14, -0.26, 0.17, 0.00, 0.00],
            [-0.08, 0.20, 0.07, 0.56, 0.00, 0.00],
            [0.16, 0.02, 0.27, 0.11, 0.00, 0.00],
            [0.00, 0.00, 0.34, -0.25, 0.01, -0.02],
            [0.00, 0.00, -0.22, -0.26, -0.39, 0.12],
            [0.00, 0.00, -0.38, 0.04, -0.04, 0.55],
            [0.00, 0.00, 0.16, 0.02, 0.27, 0.11],
            [0.01, -0.02, 0.00, 0.00, 0.34, -0.25],
            [-0.13, -0.05, 0.00, 0.00, -0.03, -0.40],
            [-0.11, -0.01, 0.00, 0.00, -0.30, -0.16],
        ],
                                  decimal=2)
Example #17
0
    def test_2D(self):
        a = ma.array(((1, 2, 3, 4), (1, 2, 3, 4), (1, 2, 3, 4)), mask=((0, 0, 0, 0), (1, 0, 0, 1), (0, 1, 1, 0)))
        actual = mstats.hmean(a)
        desired = ma.array((1, 2, 3, 4))
        assert_array_almost_equal(actual, desired, decimal=14)

        actual1 = mstats.hmean(a, axis=-1)
        desired = (4.0 / (1 / 1.0 + 1 / 2.0 + 1 / 3.0 + 1 / 4.0), 2.0 / (1 / 2.0 + 1 / 3.0), 2.0 / (1 / 1.0 + 1 / 4.0))
        assert_array_almost_equal(actual1, desired, decimal=14)
Example #18
0
 def test_savitzky_golay(self):
     t = np.linspace(-4, 4, 100)
     # noise = np.random.normal(0, 0.05, t.shape)
     noise = np.array([-0.0100738410948, 0.0434144660993, -0.00378151879776, 0.0113356386249, 0.00435077505515, -0.0477602072563, 0.0373494077292, -0.0133865881648, -0.058259106898, 0.0661995082581, -0.0302004598725, 0.0251073570423, 0.0504480752083, 0.00591197638959, -0.0936100824835, -0.0475801028574, 0.164260739963, 0.000309393717298, 0.0145265378376, 0.0451974209359, -0.0296464887607, 0.00113264461646, 0.0203243951278, 0.0250086489045, 0.0286663554031, 0.0162585459204, -0.0285237485127, 0.0841528003829, -0.0449728982279, 0.0549796633406, -0.0968901623365, 0.0306727572312, -0.06950957371, 0.00598396964893, 0.0583904237732, 0.0271856795988, 0.0503237614252, 0.179978222833, 0.0909965842309, -0.037723727536, 0.0766788834896, -0.135152061379, -0.0275445486266, 0.00437867535899, -0.0272885999582, -0.0167170993005, 0.0522077667657, -0.044618733434, -0.0323362657615, 0.0122726298648, 0.0311304227245, -0.106361849439, 0.0526578516171, 0.0600674983294, 0.0745276212563, -0.0488453979797, -0.000833273665862, 0.0232768529385, 0.0138869047145, -0.0581069396022, 0.0321001340163, -0.023975724861, -0.0281919023447, 0.0341550408589, -0.0128104984676, -0.0214928514613, -0.0930630211028, -0.086094936458, 0.0169055851217, -0.0106085935018, 0.0114499301052, 0.120823119533, 0.0401375561218, 0.0979979215035, -0.015930504967, 0.0730359376807, -0.0369380623571, 0.0628171477772, -0.0271592027456, -0.0805267873905, -0.047314352792, -0.00693573205681, -0.0276294819462, -0.00896110691771, 0.0248482543453, 0.0877066047642, -0.0325533337143, -0.0638923240986, 0.0512523528302, 0.0720952473332, 0.00555629231342, 0.0562912258368, 0.0518574742355, -0.0599288505845, 0.0129657160415, -0.0025435076237, -0.0136966498082, 0.0260013064028, 0.0315561663839, -0.0293194006299])  # @IgnorePep8
     # print(', '.join(['{0}'.format(y) for y in noise]))
     y = np.exp(-t**2) + noise
     ysg = SavitzkyGolay(n=20, degree=2).smooth(y)
     # print(', '.join(['{0}'.format(y) for y in ysg]))
     assert_array_almost_equal(ysg,[0.08136940307, 0.0628995512206, 0.0459066440291, 0.0303906814954, 0.0163516636196, 0.00378959040169, -0.00729553815838, -0.0169037220606, -0.0250349613049, -0.0316892558914, -0.03686660582, -0.0405670110908, -0.0427904717036, -0.0435369876586, -0.0428065589558, -0.0405991855951, -0.0369148675765, -0.0317536049001, -0.0251153975657, -0.0170002455736, -0.00740814892354, 0.00138883298422, 0.00903982318392, 0.0156981288342, 0.0262571138525, 0.038712235996, 0.0497401536595, 0.0708145615737, 0.0930139468092, 0.115399487253, 0.145859174356, 0.181612636791, 0.217133614374, 0.257717904903, 0.300137304571, 0.345750694351, 0.392370647909, 0.447698806473, 0.498669071812, 0.552242388661, 0.602481962016, 0.649961345551, 0.695325299394, 0.735374047294, 0.773010811455, 0.805919165272, 0.834865852047, 0.854796400219, 0.86842923705, 0.871717227059, 0.870973756546, 0.854091989437, 0.838398655214, 0.810673923439, 0.783634317013, 0.749908334726, 0.714315301461, 0.67175727743, 0.634614314389, 0.593943523363, 0.54497174327, 0.497539947485, 0.442464744684, 0.39039262486, 0.339641566641, 0.287962531039, 0.243889096129, 0.20650960823, 0.165038163827, 0.127423305227, 0.098217909507, 0.0723906956301, 0.0457840790019, 0.0325691800506, 0.0207065223098, 0.0137400799931, 0.00547944733578, -0.000268557810876, -0.00301934919753, -0.00213643706217, -0.0101846106787, -0.0170002602175, -0.0225833856784, -0.0269339870616, -0.030052064367, -0.0319376175946, -0.0325906467444, -0.0320111518164, -0.0301991328106, -0.0271545897271, -0.0228775225657, -0.0173679313266, -0.0106258160097, -0.00265117661497, 0.00655598685753, 0.0169956744078, 0.0286678860359, 0.0415726217417, 0.0557098815254, 0.0710796653868])  # @IgnorePep8
Example #19
0
    def test_gram_schmidt(self):
        a = np.random.random((10, 4))

        q = hm.linalg.gram_schmidt(a)

        q_expected = _gram_schmidt_explicit(a)
        assert_array_almost_equal(
            hm.linalg.normalize_signs(q, axis=1),
            hm.linalg.normalize_signs(q_expected, axis=1))
def test_ctm_adjoint():
    scen, control = small_scenario()
    state = CTMSimulator().simulate(scen, control)
    hx = CTMAdjoint(scen).hx(state, control)
    hu = CTMAdjoint(scen).hu(state, control)
    assert_array_equal(hx, np.tril(hx))
    u = np.ones(control.flatten().shape) * 0.01
    adjoint = CTMAdjoint(scen)
    assert_array_almost_equal(adjoint.grad(u), adjoint.grad_fd(u), 3)
Example #21
0
def test_bracket():
    data = mlclass.ex4()
    x = add_bias(data['x'])
    y = data['y']
    theta = array((.01, .01, .01))
    p = -grad(x, y, theta, 0.)
    c = lambda alpha: nan_guard(cost(x, y, theta + alpha*p, 0.))

    assert_array_almost_equal((-1.61803, 0, 1, 880.894, 0.778512, 651.614), bracket(0., 1., c), decimal=3)
 def test_min_sample_size_axis(self):
     data = [[40.0, 0.0, 40.0, 0.0], 
            [40.0, 0.0, 40.0, 0.0]]
      
     self.assertAlmostEqual(2236.5791393864079, 
                            min_sample_size(data, 0.95, 0.05))
     
     x = 4051.185794406982
     assert_array_almost_equal([x, x],
                               min_sample_size(data, .95, .05, 1))
Example #23
0
    def test_zscore_multicolumns():

        input_ = np.random.rand(10, 3)
        sc_zscore = stats.zscore(input_, axis=0, ddof=1)

        da_input = HomogenNumericTable(input_)
        da_zscore = z_score(da_input)
        np_da_zscore = getNumpyArray(da_zscore)

        assert_array_almost_equal(sc_zscore, np_da_zscore)
Example #24
0
    def test_2D(self):
        a = ma.array(((1, 2, 3, 4), (1, 2, 3, 4), (1, 2, 3, 4)),
                     mask=((0, 0, 0, 0), (1, 0, 0, 1), (0, 1, 1, 0)))
        actual = mstats.hmean(a)
        desired = ma.array((1, 2, 3, 4))
        assert_array_almost_equal(actual, desired, decimal=14)

        actual1 = mstats.hmean(a, axis=-1)
        desired = (4. / (1 / 1. + 1 / 2. + 1 / 3. + 1 / 4.),
                   2. / (1 / 2. + 1 / 3.), 2. / (1 / 1. + 1 / 4.))
        assert_array_almost_equal(actual1, desired, decimal=14)
Example #25
0
    def test_scaled_norm_of_matrix_3d(self):
        num_functions = 4
        x = 2 * np.random.random((10, 2, num_functions)) - 1

        actual = hm.linalg.scaled_norm_of_matrix(x)

        y = x.reshape(20, num_functions)
        factor = 1 / np.prod(x.shape[:-1])**0.5
        expected = factor * np.array(
            [norm(y[:, i]) for i in range(y.shape[1])])
        assert_array_almost_equal(actual, expected, 10)
Example #26
0
    def test_gauss_seidel_sweep_same_as_loop_implementation(self):
        n = 16
        kh = 0.5
        a = hm.linalg.helmholtz_1d_operator(kh, n)
        relaxer = hm.solve.relax.GsRelaxer(a)

        x = np.random.random((n, 5))
        b = np.zeros_like(x)
        for i in range(10):
            y = gs_relax_with_loop(kh, x)
            x = relaxer.step(x, b)
            assert_array_almost_equal(x - x.mean(), y - y.mean())
def test_penalize_nothing_var():
    adj1, _ = dumb_admm_system()
    old_ju1 = adj1.ju
    alpha = 0.0
    def new_ju1(x, u):
        ju = old_ju1(x, u)
        ju[1] = alpha * 2 * (u[1] - 3.0)
        return ju
    adj1.ju = new_ju1
    assert_array_almost_equal(AcceleratedGradient().solve(adj1, numpy.zeros(2)), [1.0, 0.0], 3)
    alpha = 1.0
    assert_array_almost_equal(AcceleratedGradient().solve(adj1, numpy.zeros(2)), [1.0, 3.0], 3)
    def test_ridge_regression_against_scikit(rows=10, columns=9):
        '''
        Test prediction daal against scikit
        :param rows:
        :param columns:
        '''
        inout = get_random_array(rows, columns)
        x = inout[0]
        y = inout[1]
        daal_predicted = get_daal_prediction(x, y)
        scik_predicted = get_scikit_prediction(x, y)

        assert_array_almost_equal(daal_predicted, scik_predicted)
Example #29
0
def test_td_target():
    trials = 10
    time_steps = 100

    rewards = np.random.normal(size=(trials, time_steps, 1))
    values = np.random.normal(size=(trials, time_steps, 1))
    gamma = .9

    expected_result = rewards.copy()
    expected_result[:, :-1, :] += gamma * values[:, 1:, :]

    assert_array_almost_equal(expected_result,
                              td_target(gamma, rewards, values))
Example #30
0
    def test_spaceGroupQuery(self):

        sg = self.stru2.sg
        assert sg.number is 225
        assert sg.num_sym_equiv is 192
        assert sg.num_primitive_sym_equiv is 48
        assert sg.short_name=='Fm-3m'
        assert sg.alt_name=='F M 3 M'
        assert sg.point_group_name=='PGm3barm'
        assert sg.crystal_system=='CUBIC'
        assert sg.pdb_name=='F 4/m -3 2/m'
        assert_array_almost_equal(sg.symop_list[1].R, numpy.identity(3))
        assert_array_almost_equal(sg.symop_list[1].t, numpy.array([ 0. ,  0.5,  0.5]))
    def test_spaceGroupQuery(self):

        sg = self.stru2.sg
        assert sg.number is 225
        assert sg.num_sym_equiv is 192
        assert sg.num_primitive_sym_equiv is 48
        assert sg.short_name=='Fm-3m'
        assert sg.alt_name=='F M 3 M'
        assert sg.point_group_name=='PGm3barm'
        assert sg.crystal_system=='CUBIC'
        assert sg.pdb_name=='F 4/m -3 2/m'
        assert_array_almost_equal(sg.symop_list[1].R, numpy.identity(3))
        assert_array_almost_equal(sg.symop_list[1].t, numpy.array([ 0. ,  0.5,  0.5]))
Example #32
0
def test_discount():
    trials = 10
    time_steps = 100

    rewards = np.random.normal(size=(trials, time_steps))
    gamma = .9

    expected_result = np.empty(shape=(trials, time_steps))

    for i in range(time_steps):
        discount_vec = gamma**np.arange(time_steps - i)
        expected_result[:, i] = np.dot(rewards[:, i:], discount_vec)

    assert_array_almost_equal(expected_result, discount(gamma, rewards))
Example #33
0
    def test_pairwise_cos_similarity(self):
        # Compare with explicit summation.
        x = 2 * np.random.random((10, 4)) - 1
        y = 2 * np.random.random((10, 3)) - 1
        x[:, 0] = 0

        actual = hm.linalg.pairwise_cos_similarity(x, y)
        expected = np.array([[
            sum(x[:, i] * y[:, j]) / np.clip(
                (sum(x[:, i]**2) * sum(y[:, j]**2))**0.5, 1e-15, None)
            for j in range(y.shape[1])
        ] for i in range(x.shape[1])])

        assert_array_almost_equal(actual, expected, 10)
    def test_confidence_interval_axis(self):
        data = [[8.0, 7.0, 5.0, 9.0, 9.5, 11.3, 5.2, 8.5],
                [8.0, 7.0, 5.0, 9.0, 9.5, 11.3, 5.2, 8.5]]

        assert_array_almost_equal([1.67722628,  1.67722628],
                                  half_confidence_interval_size(data, .95, 
                                                                axis=1))

        assert_array_almost_equal([ 0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.],
                                  half_confidence_interval_size(data, .95, 
                                                                axis=0))
        
        self.assertAlmostEqual(1.06902922476,
                               half_confidence_interval_size(data, .95))
Example #35
0
    def test_helmholtz_1d_operator(self):
        kh = 1.5
        n = 8
        a = hm.linalg.helmholtz_1d_operator(kh, n)

        a_expected = \
            [[0.25, 1., 0., 0., 0., 0., 0., 1.],
             [1., 0.25, 1., 0., 0., 0., 0., 0.],
             [0., 1., 0.25, 1., 0., 0., 0., 0.],
             [0., 0., 1., 0.25, 1., 0., 0., 0.],
             [0., 0., 0., 1., 0.25, 1., 0., 0.],
             [0., 0., 0., 0., 1., 0.25, 1., 0.],
             [0., 0., 0., 0., 0., 1., 0.25, 1.],
             [1., 0., 0., 0., 0., 0., 1., 0.25]]
        assert_array_almost_equal(a.toarray(), a_expected)
Example #36
0
    def test_create_uniform_coarsening_domain_indivisible_size(self):
        n = 17
        kh = 0.5
        aggregate_size = 4
        a = hm.linalg.helmholtz_1d_operator(kh, n)
        # Generate relaxed test matrix.
        x = _get_test_matrix(a, n, 10)

        # Generate coarse variables (R) on the non-repetitive domain.
        coarsener = hm.setup.coarsening_uniform.FixedAggSizeUniformCoarsener(x, aggregate_size, repetitive=True)
        result = coarsener.all_candidate_coarsening()

        # For a cycle index of 1, the max coarsening ratio is 0.7, and aggregate_size * 0.7 = 2.8, so we can have at
        # most # 2 PCs per aggregate.
        assert len(result) == 2
        r_values = np.array([item[1][0] for item in result])
        mean_energy_error = np.array([item[1][1] for item in result])

        assert_array_almost_equal(mean_energy_error, [0.477069, 0.175552], decimal=5)
        assert r_values[0].shape == (5, 17)
        r = r_values[1]
        assert r.shape == (10, 17)
        # To print r:
        # print(','.join(np.array2string(y, separator=",", formatter={'float_kind':lambda x: "%.2f" % x})
        #       for y in np.array(r.todense())))
        assert_array_almost_equal(
            r.todense(), [
                [-0.27, -0.53, -0.62, -0.52, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00,
                 0.00],
                [0.70, 0.38, -0.12, -0.59, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00,
                 0.00],
                [0.00, 0.00, 0.00, 0.00, -0.27, -0.53, -0.62, -0.52, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00,
                 0.00],
                [0.00, 0.00, 0.00, 0.00, 0.70, 0.38, -0.12, -0.59, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00,
                 0.00],
                [0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, -0.27, -0.53, -0.62, -0.52, 0.00, 0.00, 0.00, 0.00,
                 0.00],
                [0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.70, 0.38, -0.12, -0.59, 0.00, 0.00, 0.00, 0.00,
                 0.00],
                [0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, -0.27, -0.53, -0.62, -0.52,
                 0.00],
                [0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.70, 0.38, -0.12, -0.59,
                 0.00],
                [0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, -0.27, -0.53, -0.62,
                 -0.52],
                [0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.70, 0.38, -0.12, -0.59]
            ], decimal=2)
Example #37
0
    def test_linear_regression_daal_vs_sklearn(rows=10,
                                               columns=9,
                                               verbose=False):
        inout = get_random_array(rows, columns)
        x = inout[0]
        y = inout[1]

        start_sklearn = time.time()
        lin_solver_sklearn = h2o4gpu.LinearRegression(verbose=True,
                                                      backend='sklearn')
        lin_solver_sklearn.fit(x, y)
        sklearn_predicted = lin_solver_sklearn.predict(x)
        end_sklearn = time.time()

        print(("Sklearn prediction: ", sklearn_predicted) if verbose else "",
              end="")

        start_daal = time.time()
        lin_solver_daal = h2o4gpu.LinearRegression(
            fit_intercept=True,
            verbose=True,
            backend='daal',
            method=LinearMethod.normal_equation)

        lin_solver_daal.fit(x, y)
        daal_predicted = lin_solver_daal.predict(x)
        end_daal = time.time()

        print(("Daal prediction: ", daal_predicted) if verbose else "", end="")

        print("Prediction calculated:")
        print("+ Sklearn: {}".format(end_sklearn - start_sklearn))
        print("+ Daal:    {}".format(end_daal - start_daal))

        assert_array_almost_equal(daal_predicted, sklearn_predicted, decimal=4)
        assert_array_almost_equal(daal_predicted, y, decimal=4)

        if os.getenv("CHECKPERFORMANCE") is not None:
            assert end_daal - start_daal <= end_sklearn - start_sklearn

        sklearn_score = lin_solver_sklearn.score(x, y)
        daal_score = lin_solver_daal.score(x, y)
        print("Score calculated: ")
        print("+ Sklearn: {}".format(sklearn_score))
        print("+ Daal:    {}".format(daal_score))

        assert daal_score == sklearn_score
Example #38
0
def check_equal(cdfmapping, mapping):
    assert_array_equal(cdfmapping.img.shape, mapping.img.shape)
    assert_array_equal(cdfmapping.lats.shape, mapping.lats.shape)

    assert_array_equal(cdfmapping.img, mapping.img)
    assert_array_equal(cdfmapping.lats, mapping.lats)
    assert_array_equal(cdfmapping.lons, mapping.lons)
    assert_array_equal(cdfmapping.latsCenter, mapping.latsCenter)
    assert_array_equal(cdfmapping.lonsCenter, mapping.lonsCenter)
    
    assert_equal(cdfmapping.boundingBox, mapping.boundingBox)
    
    # elevation is stored as float32, so there is some loss of accuracy
    assert_array_almost_equal(cdfmapping.elevation, mapping.elevation, decimal=5)
    
    assert_equal(cdfmapping.photoTime, mapping.photoTime)
    assert_equal(cdfmapping.cameraPosGCRS, mapping.cameraPosGCRS)
Example #39
0
    def test_sparse_circulant(self):
        vals = np.array([1, 2, 3, 5, 4])
        offsets = np.array([-2, -1, 0, 2, 1])
        n = 8

        a = hm.linalg.sparse_circulant(vals, offsets, n)
        a_expected = \
            [[3., 4., 5., 0., 0., 0., 1., 2.],
             [2., 3., 4., 5., 0., 0., 0., 1.],
             [1., 2., 3., 4., 5., 0., 0., 0.],
             [0., 1., 2., 3., 4., 5., 0., 0.],
             [0., 0., 1., 2., 3., 4., 5., 0.],
             [0., 0., 0., 1., 2., 3., 4., 5.],
             [5., 0., 0., 0., 1., 2., 3., 4.],
             [4., 5., 0., 0., 0., 1., 2., 3.]]

        assert_array_almost_equal(a.toarray(), a_expected)
Example #40
0
    def test_gauss_seidel_sweep_reduces_energy_for_spd(self):
        n = 16
        kh = 0
        # Make a positive definite (= -Laplacian).
        a = -hm.linalg.helmholtz_1d_operator(kh, n)
        relaxer = hm.solve.relax.GsRelaxer(a)

        x = np.random.random((n, 1))
        b = np.zeros_like(x)
        energy = x.T.dot(a.dot(x))[0, 0]
        for i in range(10):
            y = gs_relax_with_loop(kh, x)
            x = relaxer.step(x, b)
            assert_array_almost_equal(x - x.mean(), y - y.mean())
            energy_new = x.T.dot(a.dot(x))[0, 0]
            assert energy_new < energy
            energy = energy_new
 def test_case(self):
     model = clone(estimator)
     model.fit(**fit_data)
     for method in methods:
         pred = getattr(model, method)(**predict_data)
         code = sklearn2code(model, method, numpy_flat)
         try:
             module = exec_module('test_module', code)
             exported_pred = getattr(module,
                                     method)(**export_predict_data['X'])
             if isinstance(exported_pred, tuple):
                 exported_pred = DataFrame(dict(enumerate(exported_pred)))
             assert_array_almost_equal(pred, exported_pred, 3)
         except:
             #                 print(code)
             #                 import clipboard
             #                 clipboard.copy(code)
             raise
Example #42
0
    def test_svd_simple_check():
        indata = np.array([[1, 3, 4], [5, 6, 9], [1, 2, 3], [7, 6, 8]])
        dataSource = HomogenNumericTable(indata)

        algorithm = svd.Batch()
        algorithm.input.set(svd.data, dataSource)
        result = algorithm.compute()

        sigma = getNumpyArray(result.get(svd.singularValues))
        U = getNumpyArray(result.get(svd.leftSingularMatrix))
        V = getNumpyArray(result.get(svd.rightSingularMatrix))

        # create diagonal matrix of Singular values
        _rows, cols = sigma.shape
        d_sigma = sigma.reshape(cols, )
        outdata = np.dot(U, np.dot(np.diag(d_sigma), V))

        assert_array_almost_equal(outdata, indata)
Example #43
0
    def test_ritz(self):
        n = 16
        kh = 0
        a = hm.linalg.helmholtz_1d_operator(kh, n)
        x = np.random.random((n, 4))
        action = lambda x: a.dot(x)

        y, lam_y = hm.linalg.ritz(x, action)

        lam_y_expected = np.array(
            [_rq(y[:, k], action) for k in range(y.shape[1])])
        assert_array_almost_equal(lam_y, lam_y_expected)

        assert y.shape == x.shape
        r = action(y)
        r = np.array([r[:, k] - lam_y[k] * y[:, k] for k in range(y.shape[1])])

        assert norm(r.dot(x)) < 1e-14
Example #44
0
    def test_tile_csr_matrix(self):
        n = 4
        kh = 0.5
        a = hm.linalg.helmholtz_1d_operator(kh, n)

        a_expected = \
            [[-1.75, 1., 0., 1.],
             [1., -1.75, 1., 0.],
             [0., 1., -1.75, 1.],
             [1., 0., 1., -1.75]]
        assert_array_almost_equal(a.toarray(), a_expected)

        for growth_factor in range(2, 5):
            a_tiled = hm.linalg.tile_csr_matrix(a, growth_factor)
            a_tiled_expected = hm.linalg.helmholtz_1d_operator(
                kh, growth_factor * n).tocsr()
            assert_array_almost_equal(a_tiled.toarray(),
                                      a_tiled_expected.toarray())
 def test_all(self):
     for i, node in enumerate(self.nodes):
         assert_array_almost_equal(AcceleratedGradient(50).solve(node.f, numpy.ones(2) * 10), [i] * 2)
     graph = Graph(self.nodes, self.edges)
     problem = MultiAgentProblem(graph, 100)
     problem.solve()
     for edge in self.edges:
         assert_array_almost_equal(edge.x_values(edge.l_node), edge.x_values(edge.r_node) * -1.0, 2)
     for i, node in enumerate(self.nodes):
         x = node.x
         if i is 0:
             self.assertAlmostEqual(x[0], 0.0, 2)
         else:
             self.assertAlmostEqual(x[0], i - 0.5, 2)
         if i is self.n - 1:
             self.assertAlmostEqual(x[1], i, 2)
         else:
             self.assertAlmostEqual(x[1], i + 0.5, 2)
Example #46
0
    def test_tile_csr_matrix_level2_operator(self):
        a = np.array([[-0.16, 0.05, 0.18, 0.35, 0., 0., 0.18, -0.21],
                      [0.05, -1.22, -0.21, -0.42, 0., 0., 0.35, -0.42],
                      [0.18, -0.21, -0.16, 0.05, 0.18, 0.35, 0., 0.],
                      [0.35, -0.42, 0.05, -1.22, -0.21, -0.42, 0., 0.],
                      [0., 0., 0.18, -0.21, -0.16, 0.05, 0.18, 0.35],
                      [0., 0., 0.35, -0.42, 0.05, -1.22, -0.21, -0.42],
                      [0.18, 0.35, 0., 0., 0.18, -0.21, -0.16, 0.05],
                      [-0.21, -0.42, 0., 0., 0.35, -0.42, 0.05, -1.22]])

        a_tiled = hm.linalg.tile_csr_matrix(scipy.sparse.csr_matrix(a), 2)

        # The tiled operator should have blocks of of 2 6-point stencils that are periodic on 16 points.
        wrapped_around = np.zeros_like(a)
        wrapped_around[-2:, :2] = a[-2:, :2]
        wrapped_around[:2, -2:] = a[:2, -2:]
        a_tiled_expected = np.block([[a - wrapped_around, wrapped_around],
                                     [wrapped_around, a - wrapped_around]])
        assert_array_almost_equal(a_tiled.toarray(), a_tiled_expected)
def test_admm_on_dumb():
    adj1, adj2 = dumb_admm_system()
    xymap1 = []
    xymap2 = []
    uymap1 = [0, 1]
    uymap2 = [0, 1]
    yzmap1 = [0, 1]
    yzmap2 = [1, 0]
    data1 = AdjointAdmmData(adj1, xymap1, uymap1, yzmap1, numpy.zeros(2), rho=.5)
    data2 = AdjointAdmmData(adj2, xymap2, uymap2, yzmap2, numpy.zeros(2), rho=.5)

    sp1 = AdjointAdmmSubProblem(data1, solver=AcceleratedGradient())
    sp2 = AdjointAdmmSubProblem(data2, solver=AcceleratedGradient())

    def objective(problem):
        return (problem.z[0] - 1) ** 2 + (problem.z[1] - 2) ** 2 + sum(
            [sp.data.admm_cost() for sp in problem.sub_problems])

    problem = Admm([sp2, sp1], n_iters=40, objective=objective)
    problem.solve()
    assert_array_almost_equal(problem.z, [1.0, 2.0])
Example #48
0
 def test_dataset_to_instances(self, default_rank = [1, 2, 3, 4, 5]):
     #create the final numpy array manually and see if the conversion happens ok
     newdata = []
     default_rank = Ranking(default_rank).inverse().integers()
     for i in range(1, 101):
         for rank_strings in range(5):
             vector = sorted([(str(k),k+rank_strings) for k in range(20)])
             vector = np.array([float(v) for _, v in vector])
             #newdata.append([vector, int(rank_strings)+1, i])
             newdata.append([vector, default_rank[rank_strings], i])
     newdata = np.array(newdata)
     newmetadata = {'input_size' : 20, #size of feature vector
                    'scores' :  set([1, 2, 3, 4, 5])}
     
     data, metadata = dataset_to_instances(self.train_filename).raw_source()
     for i in range(np.shape(data)[0]):
         vector1, rank1, query1 = data[i]
         vector2, rank2, query2 = newdata[i]
         assert_array_almost_equal(vector1, vector2, err_msg="MLPython: error in the conversion of batch data")
         assert_equal(rank1, rank2, err_msg="MLPython: error in the conversion of rank_strings value")
         assert_equal(query1, query2)
     assert_equal(metadata, newmetadata, err_msg="MLPython: error in the conversion of metadata")
def test_chisquare_masked_arrays():
    # The other tests were taken from the tests for stats.chisquare, so
    # they don't test the function with masked arrays.  Here masked arrays
    # are tested.
    obs = np.array([[8, 8, 16, 32, -1], [-1, -1, 3, 4, 5]]).T
    mask = np.array([[0, 0, 0, 0, 1], [1, 1, 0, 0, 0]]).T
    mobs = ma.masked_array(obs, mask)
    expected_chisq = np.array([24.0, 0.5])

    chisq, p = mstats.chisquare(mobs)
    assert_array_equal(chisq, expected_chisq)
    assert_array_almost_equal(p, stats.chisqprob(expected_chisq, mobs.count(axis=0) - 1))

    chisq, p = mstats.chisquare(mobs.T, axis=1)
    assert_array_equal(chisq, expected_chisq)
    assert_array_almost_equal(p, stats.chisqprob(expected_chisq, mobs.T.count(axis=1) - 1))

    # When axis=None, the two values should have type np.float64.
    chisq, p = mstats.chisquare([1,2,3], axis=None)
    assert_(isinstance(chisq, np.float64))
    assert_(isinstance(p, np.float64))
    assert_equal(chisq, 1.0)
    assert_almost_equal(p, stats.chisqprob(1.0, 2))
Example #50
0
def test_both():
    t = 3
    nl = 2
    nr = 2
    zl = numpy.zeros(t)
    zr = numpy.zeros(t)
    p = numpy.zeros(t)
    beta = .5
    cl = LeftAdmm(zl, p, beta, t, nl).no_control()
    cr = RightAdmm(zl, p, beta, t, nl).no_control()
    for i in range(100):
        l = LeftAdmm(zl, p, beta, t, nl)
        cl = l.optimal_control()
        xl = l.fs(cl)[nl - 1, :]
        cr = RightAdmm(zr, p, beta, t, nr).optimal_control()
        xr = cr[[0] + range(nr, len(cr))]
        v = -p + beta / 2.0 * (xl - xr)
        zl = 1.0 / beta * (-p - v) + xl
        zr = 1.0 / beta * (-p - v) - xr
        p = -v
    fsl = LeftAdmm(zl, p, beta, t, nl).solve()
    fsr = RightAdmm(zr, p, beta, t, nl).solve()
    assert_array_almost_equal(fsl[-1, :], fsr[0, :], 7)
    assert_array_almost_equal(numpy.concatenate((fsl[:-1, :], fsr), 0), numpy.eye(3) / 3.0, 3)
Example #51
0
    def test_2D(self):
        a = ma.array(((1, 2, 3, 4), (1, 2, 3, 4), (1, 2, 3, 4)), mask=((0, 0, 0, 0), (1, 0, 0, 1), (0, 1, 1, 0)))
        actual = mstats.gmean(a)
        desired = np.array((1, 2, 3, 4))
        assert_array_almost_equal(actual, desired, decimal=14)

        desired1 = mstats.gmean(a, axis=0)
        assert_array_almost_equal(actual, desired1, decimal=14)

        actual = mstats.gmean(a, -1)
        desired = ma.array((np.power(1 * 2 * 3 * 4, 1.0 / 4.0), np.power(2 * 3, 1.0 / 2.0), np.power(1 * 4, 1.0 / 2.0)))
        assert_array_almost_equal(actual, desired, decimal=14)
Example #52
0
    def test_vs_nonmasked(self):
        x = np.array((-2, -1, 0, 1, 2, 3) * 4) ** 2
        assert_array_almost_equal(mstats.normaltest(x), stats.normaltest(x))
        assert_array_almost_equal(mstats.skewtest(x), stats.skewtest(x))
        assert_array_almost_equal(mstats.kurtosistest(x), stats.kurtosistest(x))

        funcs = [stats.normaltest, stats.skewtest, stats.kurtosistest]
        mfuncs = [mstats.normaltest, mstats.skewtest, mstats.kurtosistest]
        x = [1, 2, 3, 4]
        for func, mfunc in zip(funcs, mfuncs):
            assert_raises(ValueError, func, x)
            assert_raises(ValueError, mfunc, x)
Example #53
0
    def test_hampelfilter(self):

        Y = np.array([4979, 5002, 5004, 5000, 4987, 4989, 5008, 5013, 5000, 4996, 5026, 5000, 5003, 4988, 4991, 5017, 5013, 5004, 4990, 4997, 5010, 5007, 5001, 4970, 5002, 4986, 5032, 5003, 4994, 5001, 4988, 4998, 5002, 4996, 4988, 5009, 5003, 4986, 4996, 4994, 4997, 5003, 5012, 5002, 5001, 5006, 5006, 4986, 4998, 4978, 4995, 4999, 4987, 5015, 5009, 5016, 4995, 5007, 5013, 4996, 5018, 4992, 4989, 4994, 4991, 4995, 5018, 5007, 5008, 4990, 5010, 4986, 5009, 4999, 5008, 5000, 5001, 5000, 5004, 5032, 5009, 5005, 5014, 5000, 4992, 4994, 4991, 4993, 5000, 5005, 4993, 5003, 4995, 4980, 4995, 4999, 5007, 5000, 4988, 4982, 5030, 4997, 4993, 5009, 4993, 5020, 5010, 4997, 4987, 4999, 5006, 4984, 5000, 4987, 4986, 5006, 5002, 5001, 5003, 4996, 4994, 5002, 4989, 4998, 4996, 5007, 4993, 4998, 5001, 4989, 5012, 5001, 5010, 4996, 5004, 5008, 5002, 5024, 4997, 5001, 5010, 5023, 4996, 5018, 5002, 4990, 5009, 4997, 5016, 5000, 5013, 5008, 4990, 5011, 4996, 5010, 5004, 5023, 4999, 5004, 4996, 5011, 4998, 5000, 4988, 5000, 4993, 4998, 5000, 5006, 5010, 4988, 5000, 4997, 4995, 4996, 5005, 5003, 5009, 5009, 4997, 5010, 5014, 4990, 5013, 5015, 5011, 4987, 5004, 4999, 5030, 5000, 5000, 4999, 5013, 5012, 5001, 5012, 4995, 4997, 4995, 4985, 4989, 5007, 5010, 4997, 5011, 4995, 5000, 5011, 4998, 4991, 5021, 5003, 4985, 4991, 4995, 5018, 4979, 5003, 5011, 5009, 5001, 4998, 5008, 4994, 5016, 4992, 5008, 4993, 4983, 5010, 5023, 4975, 4987, 4994, 5002, 4984, 5005, 5006, 5005, 5000, 4996, 5000, 4999, 5004, 4994, 5000, 4981, 5001, 4994, 4993, 5000, 5004, 5001, 4992, 5002, 5006, 4997, 4987, 5001, 5008, 5011, 4995, 5006, 4976, 4999, 4989, 5008, 4996, 4997, 5006, 5005, 5007, 4990, 5010, 4996, 4998, 5000, 5014, 5002, 5001, 4996, 4997, 5000, 5003, 5000, 4986, 5013, 5001, 4990, 4990, 5010, 5000, 5021, 5000, 5006, 4995, 5012, 5019, 5000, 5006, 4999, 5007, 5014, 5000, 4994, 4991, 5010, 4985, 4988, 5007, 5005, 5000, 5005, 4997, 4995, 4987, 5000, 5000, 5009, 5000, 4999, 5011, 5018, 4994, 5000, 4995, 5000, 5008, 4994, 4992, 5000, 4998, 4992, 5006, 4996, 5000, 5018, 5000, 5022, 5002, 5007, 5000, 5012, 5014, 5010, 4997, 4996, 5014, 4995, 4991, 5006, 4992, 5008, 5004, 4995, 5016, 4974, 5005, 5000, 4985, 5002, 5000, 5009, 4991, 4995, 5004, 4987, 5014, 4986, 5008, 4985, 4995, 5010, 5000, 5001, 5013, 4988, 5001, 5002, 4986, 4985, 5001, 4995, 5004, 4998, 4998, 4992, 5003, 5000, 4991, 4998, 5002, 5011, 4986, 5013, 4997, 5000, 4997, 5003, 5014, 4998, 5002, 5006, 5015, 4992, 4992, 4995, 4998, 4994, 5007, 4977, 5004, 5005, 4990, 4991, 5001, 5010, 4991, 4990, 5006, 5004, 5004, 4997, 5001, 4992, 5000, 5000, 4996, 4987, 4993, 5001, 5006, 4999, 4993, 4999, 5001, 5001, 5014, 4985, 5010, 5006, 4989, 5002, 5011, 4999, 5010, 5005, 5002, 5010, 4990, 4994, 5000, 4985, 5010, 5000, 4999, 5000, 4977, 5007, 5005, 4990, 5000, 4985, 5003, 5009, 5012, 5012, 5000, 5018, 4997, 5002, 4992, 4999, 4997, 5005, 5009, 4989, 5005, 4988, 5001, 5000, 5018, 4999, 5005, 4997, 5015, 5001, 4988, 4983, 5013, 4992, 5002, 5012, 4999, 5013, 5000, 4988, 4989, 4996, 5000, 5002, 4999, 5021, 4991, 5009, 5005, 4994, 5000, 4994, 4985, 4999, 4980, 5013, 5014, 5000, 4996, 5008, 5028, 5002, 5004, 4999, 5012, 4992, 5002, 4987, 5004, 5009, 5004, 5013, 4989, 5027, 4992, 5016, 5007, 5003, 5013, 5003, 4983, 5000, 4987, 5001, 4998, 4994, 4998, 4997, 5023, 4971, 5001, 4990, 5025, 5003, 4996, 5006, 5005, 4995, 4994, 5023, 5003, 5005, 4991, 5001, 5028, 4996, 5001, 5005, 5001, 5001, 5028, 4983, 5000, 5005, 5001, 4999, 5003, 4982, 5000, 5012, 5011, 5008, 5000, 4998, 4994, 4988, 5013, 4998, 5017, 4991, 5007, 5016, 5007, 4997, 4974, 4997, 5006, 5003, 5010, 5003, 4997, 4997, 4994, 4998, 4987, 5004, 4974, 5009, 5006, 5009, 4984, 5010, 5023, 5000, 5008, 4992, 5008, 4993, 5010, 5012, 4989, 5002, 4993, 5007, 4985, 4990, 4988, 4997, 5000, 4994, 5004, 4981, 5000, 4999, 5003, 4988, 5006, 5019, 5003, 4992, 4977, 4998, 4987, 4995, 4987, 5000, 4986, 4998, 4996, 5008, 5011, 5009, 5000, 4999, 5010, 4999, 5009, 5010, 4992, 4998, 4994, 5009, 4998, 5013, 5011, 4997, 5016, 4995, 5003, 4986, 5010, 4995, 4986, 5025, 4990, 5010, 4990, 4996, 5000, 5002, 4994, 5002, 5000, 5020, 4995, 5003, 5006, 5000, 4997, 4996, 5010, 4992, 5013, 4998, 4988, 4999, 5004, 5004, 5002, 5002, 5001, 4986, 5011, 5012, 4988, 5000, 5000, 5012, 5004, 5004, 4984, 5012, 4998, 5005, 5005, 5005, 4995, 4971, 5006, 5000, 5008, 5015, 4999, 4999, 4996, 5000, 4992, 5019, 5020, 4999, 4991, 5020, 5000, 5002, 5000, 5003, 4985, 4990, 5011, 5012, 4997, 4989, 4999, 5006, 4997, 4992, 4999, 4985, 4999, 5012, 5003, 4989, 5010, 5010, 5012, 4990, 4986, 5014, 5000, 4994, 5017, 4995, 5005, 5003, 5000, 5000, 4988, 5018, 5007, 5009, 5002, 5004, 4999, 5026, 4987, 4986, 5000, 4994, 5012, 5022, 4996, 5002, 4986, 4990, 4989, 5030, 4997, 5002, 5004, 4999, 5000, 4992, 4977, 4997, 4998, 5000, 4987, 5032, 4998, 5014, 4978, 5001, 4988, 5006, 5003, 5005, 5021, 5005, 5000, 5003, 4990, 4991, 4990, 5016, 5006, 4981, 5002, 4998, 4989, 4995, 5007, 5004, 5019, 5001, 5004, 5000, 4996, 4988, 5022, 5003, 5009, 4998, 5003, 4991, 5000, 4988, 5004, 5002, 4991, 4998, 4996, 4991, 5013, 5007, 5010, 5004, 5006, 5007, 4993, 5000, 5002, 4992, 4999, 4992, 5001, 4989, 4992, 5015, 5011, 5008, 5004, 4990, 4995, 4993, 5004, 4995, 4982, 5008, 5000, 4993, 5000, 5000, 4991, 5005, 5008, 5003, 4994, 4997, 5012, 4995, 5009, 5020, 5010, 5019, 4979, 5013, 4990, 5002, 5000, 5015, 4989, 5009, 4998, 5004, 5010, 4988, 4994, 5004, 4985, 4998, 4991, 4985, 4994, 4986, 5010, 4990, 5002, 5005, 5008, 5004, 4988, 4980, 5005, 4994, 5004, 4998, 4990, 5010, 5003, 5011, 4992, 5009, 5003, 5013, 4996, 4997, 4989, 4992, 5002, 4992, 4996, 4999, 5000, 5009, 4990, 4992, 5020, 4981, 5011, 5000, 5008, 4997, 4994, 4982, 5014, 5013, 4996, 5002, 5011, 5003, 5000, 5015, 4999, 5001, 5002, 5018, 4984, 5022, 5009, 5006, 5009, 5000, 4974, 4991, 5020, 5000, 4987, 5003, 4987, 5003, 5002, 5016, 4997, 5022, 5005, 4998, 4999, 5001, 5000, 5008, 5000, 5001, 4994, 4991, 4997, 4990, 4991, 4995, 5008, 5007, 4994, 5000, 5007, 4988, 5014, 5003, 4998, 4995, 5000, 5000, 5004], dtype=float)  # @IgnorePep8
        # randint = np.random.randint
        # Y = 5000 + np.int_(10*np.random.randn(1000))
        # outliers = randint(0, 1000, size=(10,))
        outliers = np.array([470, 795, 399, 614, 864,  37, 815, 112,  69,  16])
        # noise = randint(1000, size=(10,))
        noise = np.array([932, 286, 736, 427, 858,  63, 243, 264, 668, 844])
        # print(', '.join(['{0:d}'.format(y) for y in Y]))
        Y[outliers] = Y[outliers] + noise
        YY, _res = HampelFilter(fulloutput=True)(Y)
        assert_array_almost_equal(YY[outliers], [5002, 5000, 5003, 5008, 5000,
                                                 4997, 4998, 4999, 5009, 4997])
        assert_array_almost_equal(np.int_(YY), [5001, 5002, 5004, 5000, 4987, 4989, 5008, 5013, 5000, 4996, 5000, 5000, 5003, 4988, 4991, 5017, 4997, 5004, 4990, 4997, 5010, 5007, 5001, 4970, 5002, 4986, 5032, 5003, 4994, 5001, 4988, 4998, 5002, 4996, 4988, 5009, 5003, 4997, 4996, 4994, 4997, 5003, 5012, 5002, 5001, 5006, 5006, 4986, 4998, 4978, 4995, 4999, 4987, 5015, 5009, 5016, 4995, 5007, 5013, 4996, 5018, 4992, 4989, 4994, 4991, 4995, 5018, 5007, 5008, 5009, 5010, 5008, 5009, 4999, 5000, 5000, 5001, 5000, 5004, 5005, 5009, 5005, 5014, 5000, 4992, 4994, 4991, 4993, 5000, 5005, 4993, 5003, 4995, 4980, 4995, 4999, 5007, 5000, 4988, 4982, 5030, 4997, 4993, 5009, 4993, 5020, 5010, 4997, 4987, 4999, 5006, 4984, 4999, 4987, 4986, 5006, 5002, 5001, 5003, 4996, 4994, 5002, 4989, 4998, 4996, 5007, 4993, 4998, 5001, 4989, 5012, 5001, 5010, 4996, 5004, 5008, 5002, 5004, 4997, 5001, 5010, 5023, 4996, 5018, 5002, 4990, 5009, 4997, 5016, 5000, 5013, 5008, 4990, 5011, 4996, 5010, 5004, 5023, 4999, 5004, 4996, 5011, 4998, 5000, 4998, 5000, 4993, 4998, 5000, 5006, 5010, 4988, 5000, 4997, 4995, 4996, 5005, 5003, 5009, 5009, 4997, 5010, 5014, 5011, 5013, 5015, 5011, 4987, 5004, 4999, 5000, 5000, 5000, 4999, 5001, 5012, 5001, 5012, 4995, 4997, 4995, 4985, 4989, 5007, 5010, 4997, 5011, 4995, 5000, 5011, 4998, 4991, 5021, 5003, 4985, 4991, 4995, 5018, 4979, 5003, 5011, 5009, 5001, 4998, 5008, 4994, 5016, 4992, 5008, 4993, 4983, 5010, 5023, 4975, 4987, 4994, 5002, 5002, 5005, 5006, 5005, 5000, 4996, 5000, 4999, 5004, 5000, 5000, 4981, 5001, 4994, 4993, 5000, 5004, 5001, 4992, 5002, 5006, 4997, 4987, 5001, 5008, 5011, 4995, 5006, 4976, 4999, 4989, 5008, 4996, 4997, 5006, 5005, 5007, 4990, 5010, 4996, 4998, 5000, 5000, 5002, 5001, 4996, 4997, 5000, 5003, 5000, 5000, 5013, 5001, 4990, 4990, 5010, 5000, 5021, 5000, 5006, 4995, 5012, 5019, 5000, 5006, 4999, 5007, 5014, 5000, 4994, 4991, 5010, 4985, 4988, 5007, 5005, 5000, 5005, 4997, 4995, 4987, 5000, 5000, 5000, 5000, 4999, 5011, 5018, 4994, 5000, 4995, 5000, 5008, 4994, 4992, 5000, 4998, 4992, 5006, 4996, 5000, 5018, 5000, 5002, 5002, 5007, 5000, 5012, 5014, 5010, 4997, 4996, 5014, 4995, 4991, 5006, 4992, 5008, 5004, 4995, 5016, 5000, 5005, 5000, 4985, 5002, 5000, 5009, 4991, 4995, 5004, 4987, 5014, 4986, 5008, 4985, 4995, 5010, 5000, 5001, 5001, 5001, 5001, 5002, 4986, 5001, 5001, 4995, 5004, 4998, 4998, 4992, 5003, 5000, 4991, 4998, 5002, 5011, 5000, 5013, 4997, 5000, 5003, 5003, 5014, 4998, 5002, 5006, 5015, 4992, 4992, 4995, 4998, 4994, 5007, 4977, 5004, 5005, 4990, 4991, 4991, 4991, 4991, 5004, 5006, 5004, 5004, 4997, 5001, 4992, 5000, 5000, 4996, 4987, 4993, 5001, 5006, 4999, 4993, 4999, 5001, 5001, 5014, 4985, 5010, 5006, 4989, 5002, 5011, 4999, 5010, 5005, 5002, 5010, 4990, 4994, 5000, 4985, 5000, 5000, 4999, 5000, 5000, 5007, 5005, 4990, 5000, 4985, 5003, 5009, 5012, 5012, 5000, 5002, 4997, 5002, 4992, 4999, 4997, 5005, 5009, 4989, 5005, 4988, 5001, 5000, 5000, 4999, 5005, 4997, 5015, 5001, 4988, 4983, 5013, 4992, 5002, 5012, 4999, 5013, 5000, 4988, 4989, 4996, 5000, 5002, 4999, 5002, 4991, 5009, 5005, 4994, 5000, 4994, 4985, 4999, 4980, 5013, 5014, 5000, 4996, 5008, 5002, 5002, 5004, 4999, 5012, 4992, 5002, 4987, 5004, 5009, 5004, 5013, 4989, 5027, 4992, 5016, 5007, 5003, 5013, 5003, 5001, 5000, 4987, 5001, 4998, 4994, 4998, 4997, 4997, 4971, 5001, 4990, 5001, 5003, 4996, 5006, 5005, 4995, 5005, 5023, 5003, 5005, 4991, 5001, 5001, 4996, 5001, 5005, 5001, 5001, 5001, 5001, 5000, 5005, 5001, 4999, 5003, 5001, 5000, 5012, 5011, 5008, 5000, 4998, 4994, 4988, 5013, 4998, 5017, 4991, 5007, 5016, 5007, 4997, 5003, 4997, 5006, 5003, 5010, 5003, 4997, 4997, 4994, 4998, 4987, 5004, 5004, 5009, 5006, 5009, 5009, 5010, 5023, 5000, 5008, 5008, 5008, 4993, 5010, 5012, 4989, 5002, 4993, 4990, 4985, 4990, 4988, 4997, 5000, 4994, 5004, 5000, 5000, 4999, 5003, 5003, 5006, 5019, 5003, 4992, 4977, 4998, 4987, 4995, 4987, 5000, 4986, 4998, 4996, 5008, 5011, 5009, 5000, 5009, 5010, 4999, 5009, 5010, 4992, 4998, 4994, 5009, 4998, 5013, 5011, 4997, 5016, 4995, 5003, 4986, 5010, 4995, 4986, 5025, 4990, 5010, 4990, 4996, 5000, 5002, 4994, 5002, 5000, 5002, 4995, 5003, 5006, 5000, 4997, 4996, 4998, 4992, 5013, 4998, 4988, 4999, 5004, 5004, 5002, 5002, 5001, 4986, 5011, 5012, 4988, 5000, 5000, 5012, 5004, 5004, 4984, 5004, 5005, 5005, 5005, 5005, 5005, 5005, 5006, 5000, 5008, 5015, 4999, 4999, 4996, 5000, 4992, 5019, 5020, 4999, 4991, 5000, 5000, 5002, 5000, 5003, 4985, 4990, 5011, 5012, 4997, 4989, 4999, 4997, 4997, 4992, 4999, 4985, 4999, 5012, 5003, 5010, 5010, 5010, 5012, 5010, 4986, 5014, 5000, 4994, 5017, 4995, 5005, 5003, 5000, 5000, 4988, 5018, 5007, 5009, 5002, 5004, 4999, 5000, 4987, 4986, 5000, 4994, 5012, 5022, 4996, 5002, 4986, 4990, 4989, 4997, 4997, 5002, 5004, 4999, 5000, 4992, 4998, 4997, 4998, 5000, 5000, 5032, 4998, 5014, 4978, 5001, 5003, 5006, 5003, 5005, 5005, 5005, 5000, 5003, 4990, 4991, 4990, 5016, 5006, 4981, 4998, 4998, 4989, 4995, 5007, 5004, 5004, 5001, 5004, 5000, 4996, 4988, 5000, 5003, 5009, 4998, 5003, 4991, 5000, 4988, 5004, 5002, 4991, 4998, 4996, 4991, 5013, 5007, 5010, 5004, 5006, 5007, 4993, 5000, 5002, 4992, 4999, 4992, 5001, 4989, 4992, 5015, 5011, 5008, 5004, 4990, 4995, 4993, 5004, 5000, 4982, 5008, 5000, 4993, 5000, 5000, 4991, 5005, 5008, 5003, 4994, 4997, 5012, 4995, 5009, 5020, 5010, 5019, 4979, 5013, 4990, 5002, 5000, 5015, 4989, 5009, 4998, 5004, 5010, 4988, 4994, 5004, 4985, 4998, 4991, 4985, 4994, 4986, 5010, 4990, 5002, 5005, 5008, 5004, 4988, 5004, 5005, 4994, 5004, 4998, 4990, 5010, 5003, 5011, 4992, 5009, 5003, 5013, 4996, 4997, 4989, 4992, 5002, 4992, 4996, 4999, 5000, 5009, 4990, 4992, 5020, 4981, 5011, 5000, 5008, 4997, 4994, 4982, 5014, 5013, 4996, 5002, 5011, 5003, 5000, 5002, 4999, 5001, 5002, 5018, 4984, 5022, 5009, 5006, 5009, 5000, 4974, 4991, 5020, 5000, 4987, 5003, 5002, 5003, 5002, 5016, 4997, 5002, 5005, 4998, 4999, 5001, 5000, 5000, 5000, 5001, 4994, 4991, 4997, 4990, 4991, 4995, 5008, 5007, 4994, 5000, 5007, 4988, 5014, 5003, 4998, 4995, 5000, 5000, 5004])  # @IgnorePep8

        YY1, _res1 = HampelFilter(dx=1, t=3, adaptive=0.1, fulloutput=True)(Y)
        assert_array_almost_equal(np.int_(YY1), [4999, 5002, 5004, 5000, 4987, 4989, 5008, 5013, 5000, 4996, 5026, 5000, 5003, 4988, 4991, 5017, 5003, 5004, 4990, 4997, 5010, 5007, 5001, 4970, 5002, 4986, 5032, 5003, 4994, 5001, 4988, 4998, 5002, 4996, 4988, 5009, 5003, 5001, 4996, 4994, 4997, 5003, 5012, 5002, 5001, 5006, 5006, 4986, 4998, 4978, 4995, 4999, 4987, 5015, 5009, 5016, 4995, 5007, 5013, 4996, 5018, 4992, 4989, 4994, 4991, 4995, 5018, 5007, 5008, 5006, 5010, 4986, 5009, 4999, 5008, 5000, 5001, 5000, 5004, 5004, 5009, 5005, 5014, 5000, 4992, 4994, 4991, 4993, 5000, 5005, 4993, 5003, 4995, 4980, 4995, 4999, 5007, 5000, 4988, 4982, 5030, 4997, 4993, 5009, 4993, 5020, 5010, 4997, 4987, 4999, 5006, 4984, 4998, 4987, 4986, 5006, 5002, 5001, 5003, 4996, 4994, 5002, 4989, 4998, 4996, 5007, 4993, 4998, 5001, 4989, 5012, 5001, 5010, 4996, 5004, 5008, 5002, 5024, 4997, 5001, 5010, 5023, 4996, 5018, 5002, 4990, 5009, 4997, 5016, 5000, 5013, 5008, 4990, 5011, 4996, 5010, 5004, 5023, 4999, 5004, 4996, 5011, 4998, 5000, 4988, 5000, 4993, 4998, 5000, 5006, 5010, 4988, 5000, 4997, 4995, 4996, 5005, 5003, 5009, 5009, 4997, 5010, 5014, 5009, 5013, 5015, 5011, 5005, 5004, 4999, 5002, 5000, 5000, 4999, 5013, 5012, 5001, 5012, 4995, 4997, 4995, 4985, 4989, 5007, 5010, 4997, 5011, 4995, 5000, 5011, 4998, 4991, 5021, 5003, 4985, 4991, 4995, 5018, 4979, 5003, 5011, 5009, 5001, 4998, 5008, 4994, 5016, 4992, 5008, 4993, 4983, 5010, 5023, 4975, 4987, 4994, 5002, 4984, 5005, 5006, 5005, 5000, 4996, 5000, 4999, 5004, 4994, 5000, 4981, 5001, 4994, 4993, 5000, 5004, 5001, 4992, 5002, 5006, 4997, 4987, 5001, 5008, 5011, 4995, 5006, 4976, 4999, 4989, 5008, 4996, 4997, 5006, 5005, 5007, 4990, 5010, 4996, 4998, 5000, 5000, 5002, 5001, 4996, 4997, 5000, 5003, 5000, 4986, 5013, 5001, 4990, 4990, 5010, 5000, 5021, 5000, 5006, 4995, 5012, 5019, 5000, 5006, 4999, 5007, 5014, 5000, 4994, 4991, 5010, 4985, 4988, 5007, 5005, 5000, 5005, 4997, 4995, 4999, 5000, 5000, 5009, 5000, 4999, 5011, 5018, 4994, 5000, 4995, 5000, 5008, 4994, 4992, 5000, 4998, 4992, 5006, 4996, 5000, 5018, 5000, 5022, 5002, 5007, 5000, 5012, 5014, 5010, 4997, 4996, 5014, 4995, 4991, 5006, 4992, 5008, 5004, 4995, 5016, 5000, 5005, 5000, 4985, 5002, 5000, 5009, 4991, 4995, 5004, 4987, 5014, 4986, 5008, 4985, 4995, 5010, 5000, 5001, 5013, 4988, 5001, 5002, 4986, 4985, 5001, 4995, 5004, 4998, 4998, 4992, 5003, 5000, 4991, 4998, 5002, 5011, 4986, 5013, 4997, 5000, 5002, 5003, 5014, 4998, 5002, 5006, 5015, 4992, 4992, 4995, 4998, 4994, 5007, 4977, 5004, 5005, 4990, 4991, 5001, 5010, 4991, 4990, 5006, 5004, 5004, 4997, 5001, 4992, 5000, 5000, 4996, 4987, 4993, 5001, 5006, 4999, 4993, 4999, 5001, 5001, 5014, 4985, 5010, 5006, 4989, 5002, 5011, 4999, 5010, 5005, 5002, 5010, 4990, 4994, 5000, 4985, 5010, 5000, 4999, 5000, 4977, 5007, 5005, 4990, 5000, 4985, 5003, 5009, 5012, 5012, 5000, 5003, 4997, 5002, 4992, 4999, 4997, 5005, 5009, 4989, 5005, 4988, 5001, 5000, 5000, 4999, 5005, 4997, 5015, 5001, 4988, 4983, 5013, 4992, 5002, 5012, 4999, 5013, 5000, 4988, 4989, 4996, 5000, 5002, 4999, 5021, 4991, 5009, 5005, 4994, 5000, 4994, 4985, 4999, 4980, 5013, 5014, 5000, 4996, 5008, 5028, 5002, 5004, 4999, 5012, 4992, 5002, 4987, 5004, 5009, 5004, 5013, 4989, 5027, 4992, 5016, 5007, 5003, 5013, 5003, 4999, 5000, 4987, 5001, 4998, 4994, 4998, 4997, 4998, 4998, 5001, 4990, 5025, 5003, 4996, 5006, 5005, 4995, 4994, 5023, 5003, 5005, 4991, 5001, 5001, 4996, 5001, 5005, 5001, 5001, 5000, 5000, 5000, 5005, 5001, 4999, 5003, 4982, 5000, 5012, 5011, 5008, 5000, 4998, 4994, 4988, 5013, 4998, 5017, 4991, 5007, 5016, 5007, 4997, 4974, 4997, 5006, 5003, 5010, 5003, 4997, 4997, 4994, 4998, 4987, 5004, 5001, 5009, 5006, 5009, 4984, 5010, 5023, 5000, 5008, 5007, 5008, 4993, 5010, 5012, 4989, 5002, 4993, 5007, 4985, 4990, 4988, 4997, 5000, 4994, 5004, 4981, 5000, 4999, 5003, 4988, 5006, 5019, 5003, 4992, 4977, 4998, 4987, 4995, 4987, 5000, 4986, 4998, 4996, 5008, 5011, 5009, 5000, 4999, 5010, 4999, 5009, 5010, 4992, 4998, 4994, 5009, 4998, 5013, 5011, 4997, 5016, 4995, 5003, 4986, 5010, 4995, 4986, 5025, 4990, 5010, 4990, 4996, 5000, 5002, 4994, 5002, 5000, 5001, 4995, 5003, 5006, 5000, 4997, 4996, 5010, 4992, 5013, 4998, 4988, 4999, 5004, 5004, 5002, 5002, 5001, 4986, 5011, 5012, 4988, 5000, 5000, 5012, 5004, 5004, 4984, 5012, 4998, 5005, 5005, 5005, 4995, 5004, 5006, 5000, 5008, 5015, 4999, 4999, 4996, 5000, 4992, 5019, 5020, 4999, 4991, 5020, 5000, 5002, 5000, 5003, 4985, 4990, 5011, 5012, 4997, 4989, 4999, 5006, 4997, 4992, 4999, 4985, 4999, 5012, 5003, 4989, 5010, 5010, 5012, 4990, 4986, 5014, 5000, 4994, 5017, 4995, 5005, 5003, 5000, 5000, 4988, 5018, 5007, 5009, 5002, 5004, 4999, 5026, 4987, 4986, 5000, 4994, 5012, 5022, 4996, 5002, 4986, 4990, 4989, 4997, 4997, 5002, 5004, 4999, 5000, 4992, 4999, 4997, 4998, 5000, 5002, 5032, 4998, 5014, 4978, 5001, 4988, 5006, 5003, 5005, 5002, 5005, 5000, 5003, 4990, 4991, 4990, 5016, 5006, 4981, 4999, 4998, 4989, 4995, 5007, 5004, 5019, 5001, 5004, 5000, 4996, 4988, 5001, 5003, 5009, 4998, 5003, 4991, 5000, 4988, 5004, 5002, 4991, 4998, 4996, 4991, 5013, 5007, 5010, 5004, 5006, 5007, 4993, 5000, 5002, 4992, 4999, 4992, 5001, 4989, 4992, 5015, 5011, 5008, 5004, 4990, 4995, 4993, 5004, 5000, 4982, 5008, 5000, 4993, 5000, 5000, 4991, 5005, 5008, 5003, 4994, 4997, 5012, 4995, 5009, 5020, 5010, 5019, 4979, 5013, 4990, 5002, 5000, 5015, 4989, 5009, 4998, 5004, 5010, 4988, 4994, 5004, 4985, 4998, 4991, 4985, 4994, 4986, 5010, 4990, 5002, 5005, 5008, 5004, 4988, 4980, 5005, 4994, 5004, 4998, 4990, 5010, 5003, 5011, 4992, 5009, 5003, 5013, 4996, 4997, 4989, 4992, 5002, 4992, 4996, 4999, 5000, 5009, 4990, 4992, 5020, 4981, 5011, 5000, 5008, 4997, 4994, 4982, 5014, 5013, 4996, 5002, 5011, 5003, 5000, 5015, 4999, 5001, 5002, 5018, 4984, 5022, 5009, 5006, 5009, 5000, 4974, 4991, 5020, 5000, 4987, 5003, 4987, 5003, 5002, 5016, 4997, 5022, 5005, 4998, 4999, 5001, 5000, 4999, 5000, 5001, 4994, 4991, 4997, 4990, 4991, 4995, 5008, 5007, 4994, 5000, 5007, 4988, 5014, 5003, 4998, 4995, 5000, 5000, 5004])  # @IgnorePep8

        YY2, _res2 = HampelFilter(dx=3, t=0, fulloutput=True)(Y)  # Y0 =
        assert_array_almost_equal(np.int_(YY2), [5001, 5000, 4994, 5000, 5002, 5000, 5000, 5000, 5000, 5003, 5000, 5000, 5000, 5003, 5003, 5003, 4997, 5004, 5007, 5004, 5001, 5001, 5001, 5002, 5002, 5001, 5001, 5001, 4998, 5001, 4998, 4996, 4998, 4998, 5002, 5002, 4996, 4997, 5003, 5003, 5002, 5001, 5002, 5003, 5003, 5002, 5001, 4998, 4998, 4995, 4995, 4998, 4999, 4999, 5007, 5009, 5009, 5009, 5007, 4996, 4996, 4994, 4994, 4994, 4994, 4995, 5007, 5008, 5008, 5009, 5008, 5008, 5008, 5001, 5000, 5001, 5001, 5004, 5004, 5005, 5005, 5005, 5005, 5000, 4994, 4994, 4994, 4993, 4994, 4995, 4995, 4995, 4995, 4995, 4999, 4995, 4995, 4999, 4999, 4997, 4997, 4993, 4997, 5009, 4997, 4997, 4999, 4999, 4999, 4999, 4997, 4987, 4999, 5002, 5001, 5002, 5001, 5001, 5002, 5001, 4998, 4996, 4996, 4996, 4998, 4998, 4998, 4998, 5001, 5001, 5001, 5001, 5004, 5004, 5004, 5004, 5002, 5004, 5008, 5002, 5010, 5002, 5002, 5009, 5002, 5002, 5002, 5002, 5008, 5008, 5008, 5008, 5008, 5008, 5008, 5004, 5004, 5004, 5004, 5004, 5000, 4999, 5000, 4998, 4998, 4998, 5000, 5000, 5000, 5000, 5000, 5000, 4997, 4997, 4997, 5000, 5003, 5003, 5005, 5009, 5009, 5009, 5010, 5011, 5011, 5011, 5004, 5011, 5004, 5000, 5000, 5000, 5000, 5001, 5001, 5001, 5001, 5001, 4997, 4995, 4995, 4995, 4997, 4997, 4997, 5000, 5007, 5000, 4998, 5000, 5000, 5000, 4998, 4995, 4995, 4995, 4995, 4995, 5003, 5003, 5003, 5003, 5003, 5008, 5001, 5001, 4998, 4994, 4994, 5008, 4993, 4993, 4993, 4994, 4994, 4994, 4994, 5002, 5002, 5002, 5000, 5000, 5000, 5000, 5000, 4999, 5000, 4999, 4994, 4994, 5000, 5000, 5000, 5000, 5001, 5001, 5001, 5001, 5001, 5002, 5001, 5001, 5001, 5001, 4999, 4999, 4996, 4997, 4997, 4999, 5005, 5005, 5005, 5005, 5005, 5000, 5000, 5000, 5001, 5000, 5000, 5000, 5001, 5000, 5000, 5000, 5000, 5000, 5000, 5000, 5000, 5001, 5000, 5000, 5000, 5006, 5006, 5006, 5006, 5006, 5006, 5007, 5006, 5000, 5000, 5000, 5000, 4994, 4994, 4994, 5000, 5005, 5000, 5000, 5000, 5000, 5000, 5000, 5000, 5000, 5000, 5000, 5000, 5000, 5000, 5000, 5000, 5000, 4995, 5000, 4998, 4998, 4998, 4996, 4998, 5000, 5000, 5000, 5002, 5002, 5002, 5007, 5007, 5010, 5007, 5007, 5010, 5010, 4997, 4997, 4996, 4996, 5004, 4995, 5004, 5004, 5004, 5004, 5000, 5000, 5000, 5000, 5000, 5000, 5000, 5000, 5000, 4995, 4995, 4995, 4995, 4995, 5000, 5000, 5001, 5000, 5001, 5001, 5001, 5001, 5001, 4995, 5001, 4998, 4998, 4998, 4998, 4998, 4998, 4998, 4998, 5000, 5000, 5000, 4998, 5000, 5002, 5003, 5003, 5003, 5002, 5003, 5006, 5003, 5002, 4998, 4998, 4995, 4995, 4994, 4995, 4998, 4998, 4994, 5001, 5001, 5001, 4991, 4991, 5001, 5004, 5004, 5001, 5001, 5001, 5000, 5000, 4997, 4996, 4996, 5000, 4999, 4996, 4999, 4999, 5001, 5001, 4999, 5001, 5001, 5001, 5002, 5006, 5002, 5006, 5005, 5002, 5005, 5005, 5002, 5002, 5000, 5000, 5000, 4999, 5000, 5000, 5000, 5000, 5000, 5000, 5000, 5000, 5003, 5003, 5003, 5003, 5009, 5009, 5009, 5002, 5000, 4999, 4999, 4999, 4999, 4999, 4999, 5001, 5001, 5001, 5000, 5001, 5000, 5001, 5001, 5001, 4999, 5001, 4997, 5001, 5001, 4999, 5002, 5002, 5000, 5000, 4999, 4999, 5000, 4999, 4999, 4999, 5000, 5002, 5002, 5000, 5000, 4994, 4999, 4994, 4994, 4999, 4999, 4999, 5000, 5008, 5008, 5004, 5002, 5004, 5004, 5002, 5002, 5002, 5002, 5004, 5004, 5004, 5004, 5004, 5009, 5007, 5007, 5007, 5007, 5003, 5003, 5003, 5001, 5000, 4998, 4998, 4998, 4998, 4998, 4998, 4997, 4998, 5001, 5001, 5001, 5003, 5003, 5003, 5003, 5003, 5005, 5003, 5001, 5003, 5003, 5001, 5001, 5001, 5001, 5001, 5001, 5001, 5001, 5001, 5001, 5001, 5000, 5000, 5001, 5001, 5003, 5003, 5000, 5000, 5000, 5000, 4998, 4998, 4998, 4998, 5007, 5007, 5007, 5007, 4997, 5006, 5003, 5003, 5003, 5003, 5003, 5003, 4998, 4997, 4997, 4997, 4997, 4998, 5004, 5004, 5006, 5009, 5009, 5008, 5009, 5008, 5008, 5008, 5008, 5008, 5008, 5002, 5002, 5002, 4993, 4990, 4993, 4993, 4994, 4994, 4994, 4997, 4999, 5000, 4999, 5000, 5000, 5003, 5003, 5003, 4998, 4998, 4995, 4992, 4992, 4987, 4995, 4995, 4996, 4998, 5000, 5000, 5000, 5008, 5008, 5009, 5009, 5000, 4999, 4999, 4999, 4998, 4998, 4998, 4998, 5009, 5009, 5003, 5003, 5003, 4997, 4995, 4995, 4995, 4995, 4995, 4995, 4996, 5000, 4996, 5000, 5000, 5000, 5000, 5002, 5002, 5002, 5000, 5000, 5000, 5000, 5000, 4998, 4997, 4998, 4999, 4999, 5002, 5002, 5002, 5002, 5002, 5002, 5002, 5001, 5000, 5000, 5004, 5004, 5000, 5004, 5004, 5004, 5004, 5005, 5005, 5005, 5005, 5005, 5005, 5005, 5000, 5000, 5000, 5000, 4999, 4999, 4999, 4999, 4999, 5000, 5000, 5002, 5000, 5000, 5000, 5000, 5000, 5002, 5000, 4997, 4997, 4999, 4999, 4997, 4997, 4997, 4999, 4999, 4999, 4999, 4999, 5003, 5010, 5010, 5003, 5010, 5010, 5000, 5000, 4995, 5000, 5003, 5000, 5000, 5000, 5000, 5003, 5003, 5002, 5004, 5004, 5007, 5004, 5002, 5000, 4999, 4999, 5000, 4996, 5000, 5000, 4996, 4996, 4996, 4996, 4997, 4997, 4999, 5000, 5000, 4999, 4999, 4998, 4998, 4998, 4998, 4998, 5000, 5000, 5001, 5001, 5001, 5001, 5003, 5003, 5005, 5005, 5005, 5003, 5003, 5000, 5000, 5000, 4991, 4991, 4998, 4998, 4998, 4998, 4998, 5004, 5001, 5004, 5004, 5004, 5001, 5001, 5001, 5003, 5000, 5003, 5003, 5003, 5000, 5000, 5000, 5000, 4998, 4998, 4996, 4998, 4998, 4998, 5004, 5006, 5007, 5007, 5006, 5004, 5002, 5000, 4999, 4999, 4999, 4992, 4992, 4999, 5001, 5004, 5004, 5004, 5004, 5004, 5004, 4995, 4995, 5000, 5000, 5000, 5000, 5000, 5000, 5000, 5000, 5000, 5000, 5003, 5003, 5003, 5003, 5009, 5010, 5010, 5010, 5010, 5010, 5002, 5002, 5000, 5002, 5000, 5002, 5004, 5004, 4998, 5004, 4998, 4998, 4994, 4991, 4994, 4991, 4991, 4991, 4991, 4994, 5002, 5004, 5004, 5002, 5004, 5004, 5004, 4998, 4994, 4998, 5003, 5003, 5003, 5003, 5003, 5009, 5003, 5003, 4997, 4997, 4997, 4996, 4996, 4996, 4996, 4999, 4999, 4996, 4999, 4999, 5000, 5000, 5000, 5000, 5000, 4997, 5000, 5000, 4997, 4997, 5002, 5003, 5003, 5003, 5002, 5002, 5002, 5002, 5001, 5002, 5002, 5006, 5009, 5009, 5006, 5006, 5006, 5000, 5000, 5000, 4991, 5000, 5002, 5002, 5002, 5003, 5003, 5003, 5002, 5001, 5000, 5001, 5000, 5000, 5000, 5000, 5000, 4997, 4994, 4994, 4994, 4995, 4995, 4995, 5000, 5000, 5007, 5003, 5000, 5000, 5000, 5000, 5000, 5000, 5000, 5000])  # @IgnorePep8
Example #54
0
def test_pass_info():
    beta_op.beta = 1.0
    beta_op.factor = 1.02
    beta_op.factor = 1.000
    beta_op.beta_admm_factor = 1.000
    n, t = 10, 10
    scen_full = Scenario.blank(n, t)
    scen_full.ic.mainline[0] = 0.5
    admm = CtmAdjointAdmm.from_full_scenario(scen_full, 5, beta=30.0, n_iters=400)
    d1 = admm.l_scen.simulate(admm.l_scen.no_control()).density
    d2 = admm.r_scen.simulate(admm.r_scen.no_control()).density
    sln = admm.solve()
    d3 = sln.l.fs(sln.l.construct_control(sln.cl)).density
    d4 = sln.r.fs(sln.r.construct_control(sln.cr)).density

    subplot(2, 2, 1)
    pcolor(d1, cmap="binary", vmin=0.0, vmax=1.0)
    xlabel("Time")
    ylabel("Space")
    title("Initial Upstream Subnetwork")
    colorbar()
    subplot(2, 2, 2)
    pcolor(d2, cmap="binary", vmin=0.0, vmax=1.0)
    xlabel("Time")
    ylabel("Space")
    title("Initial Downstream Subnetwork")
    colorbar()
    subplot(2, 2, 3)
    pcolor(d3, cmap="binary", vmin=0.0, vmax=1.0)
    xlabel("Time")
    ylabel("Space")
    title("Final Upstream Subnetwork")
    colorbar()
    subplot(2, 2, 4)
    pcolor(d4, cmap="binary", vmin=0.0, vmax=1.0)
    xlabel("Time")
    ylabel("Space")
    title("Final Downstream Subnetwork")
    colorbar()
    savefig("init-final.pdf")

    sln.density_summary()
    cl = sln.l.construct_control(sln.cl)
    cr = sln.r.construct_control(sln.cr)
    assert_array_almost_equal(cl[0][-2:, :], cr[0][:2, :])
    assert_array_almost_equal(cl[1], [0, 0, 0.5, 0], 2)
    assert_array_almost_equal(cr[1], [0, 0.5, 0, 0], 2)
    assert_almost_equals(sln.history.x[-1], 0.0, 2)
    assert_almost_equals(sln.history.y[-1], 0.0, 2)
Example #55
0
def test_fs_controlled():
    beta_op.beta = 1.0
    scen, control = small_scenario()
    ca = CTMAdjoint(scen)
    for _ in range(5):
        c = rand(scen.N * scen.T, 1).flatten()
        assert_array_almost_equal(ca.grad(c), ca.grad_fd(c))
        # fs = CTMSimulator().simulate(scen, control)
    ca = ControlledCtmAdjoint(True, scen)
    control = ca.construct_control(ca.no_control())
    fs = ca.fs(control)
    # assert_array_equal(fs.density, [[.9, 1.0, .9, .1, 0, 0, 0, 0, 0, 0, 0], [0, 1.0, .9, .9, .1, 0, 0, 0, 0, 0, 0]])
    assert_array_almost_equal(fs.density, [[0.9, 1.0, 0.9, 0.1, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0]])
    control[1][:] += 2.0
    fs = ca.fs(control)
    assert_array_almost_equal(
        fs.density, np.array([[0.9, 1.8, 1.8, 1.8, 1.8, 1.8, 1.8], [2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 1.0]])
    )
Example #56
0
def test_two_length():
    beta_op.beta = 50.0
    # beta_op.beta = 0.8
    beta_op.factor = 1.02
    beta_op.beta = 100.0
    beta_op.factor = 1.02
    beta_op.factor = 1.000
    beta = 100.0
    beta_op.beta_admm_factor = 1.000
    beta_op.inner_iters = 100
    n, t = 2, 2
    scen_full = Scenario.blank(n, t)
    scen_full.ic.mainline[1] = 0.5
    sln = CtmAdjointAdmm.from_full_scenario(scen_full, 1, beta=beta, n_iters=20).solve()
    sln.density_summary()
    cl = sln.l.construct_control(sln.cl)
    cr = sln.r.construct_control(sln.cr)
    assert_array_almost_equal(cl[0][-2:, :], cr[0][:2, :])
    assert_array_almost_equal(cl[1], [0, 0, 0.5, 0], 2)
    assert_array_almost_equal(cr[1], [0, 0.5, 0, 0], 2)
    assert_almost_equals(sln.history.x[-1], 0.0, 2)
    assert_almost_equals(sln.history.y[-1], 0.0, 2)
Example #57
0
def test_plotting_positions():
    # Regression test for #1256
    pos = mstats.plotting_positions(np.arange(3), 0, 0)
    assert_array_almost_equal(pos.data, np.array([0.25, 0.5, 0.75]))
Example #58
0
def test_triinterp():
    # Test points within triangles of masked triangulation.
    x, y = np.meshgrid(np.arange(4), np.arange(4))
    x = x.ravel()
    y = y.ravel()
    z = 1.23*x - 4.79*y
    triangles = [[0, 1, 4], [1, 5, 4], [1, 2, 5], [2, 6, 5], [2, 3, 6],
                 [3, 7, 6], [4, 5, 8], [5, 9, 8], [5, 6, 9], [6, 10, 9],
                 [6, 7, 10], [7, 11, 10], [8, 9, 12], [9, 13, 12], [9, 10, 13],
                 [10, 14, 13], [10, 11, 14], [11, 15, 14]]
    mask = np.zeros(len(triangles))
    mask[8:10] = 1
    triang = mtri.Triangulation(x, y, triangles, mask)
    linear_interp = mtri.LinearTriInterpolator(triang, z)
    cubic_min_E = mtri.CubicTriInterpolator(triang, z)
    cubic_geom = mtri.CubicTriInterpolator(triang, z, kind='geom')

    xs = np.linspace(0.25, 2.75, 6)
    ys = [0.25, 0.75, 2.25, 2.75]
    xs, ys = np.meshgrid(xs, ys)  # Testing arrays with array.ndim = 2
    for interp in (linear_interp, cubic_min_E, cubic_geom):
        zs = interp(xs, ys)
        assert_array_almost_equal(zs, (1.23*xs - 4.79*ys))

    # Test points outside triangulation.
    xs = [-0.25, 1.25, 1.75, 3.25]
    ys = xs
    xs, ys = np.meshgrid(xs, ys)
    for interp in (linear_interp, cubic_min_E, cubic_geom):
        zs = linear_interp(xs, ys)
        assert_array_equal(zs.mask, [[True]*4]*4)

    # Test mixed configuration (outside / inside).
    xs = np.linspace(0.25, 1.75, 6)
    ys = [0.25, 0.75, 1.25, 1.75]
    xs, ys = np.meshgrid(xs, ys)
    for interp in (linear_interp, cubic_min_E, cubic_geom):
        zs = interp(xs, ys)
        matest.assert_array_almost_equal(zs, (1.23*xs - 4.79*ys))
        mask = (xs >= 1) * (xs <= 2) * (ys >= 1) * (ys <= 2)
        assert_array_equal(zs.mask, mask)

    # 2nd order patch test: on a grid with an 'arbitrary shaped' triangle,
    # patch test shall be exact for quadratic functions and cubic
    # interpolator if *kind* = user
    (a, b, c) = (1.23, -4.79, 0.6)

    def quad(x, y):
        return a*(x-0.5)**2 + b*(y-0.5)**2 + c*x*y

    def gradient_quad(x, y):
        return (2*a*(x-0.5) + c*y, 2*b*(y-0.5) + c*x)

    x = np.array([0.2, 0.33367, 0.669, 0., 1., 1., 0.])
    y = np.array([0.3, 0.80755, 0.4335, 0., 0., 1., 1.])
    triangles = np.array([[0, 1, 2], [3, 0, 4], [4, 0, 2], [4, 2, 5],
                          [1, 5, 2], [6, 5, 1], [6, 1, 0], [6, 0, 3]])
    triang = mtri.Triangulation(x, y, triangles)
    z = quad(x, y)
    dz = gradient_quad(x, y)
    # test points for 2nd order patch test
    xs = np.linspace(0., 1., 5)
    ys = np.linspace(0., 1., 5)
    xs, ys = np.meshgrid(xs, ys)
    cubic_user = mtri.CubicTriInterpolator(triang, z, kind='user', dz=dz)
    interp_zs = cubic_user(xs, ys)
    assert_array_almost_equal(interp_zs, quad(xs, ys))
    (interp_dzsdx, interp_dzsdy) = cubic_user.gradient(x, y)
    (dzsdx, dzsdy) = gradient_quad(x, y)
    assert_array_almost_equal(interp_dzsdx, dzsdx)
    assert_array_almost_equal(interp_dzsdy, dzsdy)

    # Cubic improvement: cubic interpolation shall perform better than linear
    # on a sufficiently dense mesh for a quadratic function.
    n = 11
    x, y = np.meshgrid(np.linspace(0., 1., n+1), np.linspace(0., 1., n+1))
    x = x.ravel()
    y = y.ravel()
    z = quad(x, y)
    triang = mtri.Triangulation(x, y, triangles=meshgrid_triangles(n+1))
    xs, ys = np.meshgrid(np.linspace(0.1, 0.9, 5), np.linspace(0.1, 0.9, 5))
    xs = xs.ravel()
    ys = ys.ravel()
    linear_interp = mtri.LinearTriInterpolator(triang, z)
    cubic_min_E = mtri.CubicTriInterpolator(triang, z)
    cubic_geom = mtri.CubicTriInterpolator(triang, z, kind='geom')
    zs = quad(xs, ys)
    diff_lin = np.abs(linear_interp(xs, ys) - zs)
    for interp in (cubic_min_E, cubic_geom):
        diff_cubic = np.abs(interp(xs, ys) - zs)
        assert np.max(diff_lin) >= 10 * np.max(diff_cubic)
        assert (np.dot(diff_lin, diff_lin) >=
                100 * np.dot(diff_cubic, diff_cubic))
def test_simplest():
    scen, control = smallest_possible()
    u = control.flatten()
    adjoint = CTMAdjoint(scen)
    assert_array_almost_equal(adjoint.grad(u), adjoint.grad_fd(u))
Example #60
0
def test_triinterp_transformations():
    # 1) Testing that the interpolation scheme is invariant by rotation of the
    # whole figure.
    # Note: This test is non-trivial for a CubicTriInterpolator with
    # kind='min_E'. It does fail for a non-isotropic stiffness matrix E of
    # :class:`_ReducedHCT_Element` (tested with E=np.diag([1., 1., 1.])), and
    # provides a good test for :meth:`get_Kff_and_Ff`of the same class.
    #
    # 2) Also testing that the interpolation scheme is invariant by expansion
    # of the whole figure along one axis.
    n_angles = 20
    n_radii = 10
    min_radius = 0.15

    def z(x, y):
        r1 = np.hypot(0.5 - x, 0.5 - y)
        theta1 = np.arctan2(0.5 - x, 0.5 - y)
        r2 = np.hypot(-x - 0.2, -y - 0.2)
        theta2 = np.arctan2(-x - 0.2, -y - 0.2)
        z = -(2*(np.exp((r1/10)**2)-1)*30. * np.cos(7.*theta1) +
              (np.exp((r2/10)**2)-1)*30. * np.cos(11.*theta2) +
              0.7*(x**2 + y**2))
        return (np.max(z)-z)/(np.max(z)-np.min(z))

    # First create the x and y coordinates of the points.
    radii = np.linspace(min_radius, 0.95, n_radii)
    angles = np.linspace(0 + n_angles, 2*np.pi + n_angles,
                         n_angles, endpoint=False)
    angles = np.repeat(angles[..., np.newaxis], n_radii, axis=1)
    angles[:, 1::2] += np.pi/n_angles
    x0 = (radii*np.cos(angles)).flatten()
    y0 = (radii*np.sin(angles)).flatten()
    triang0 = mtri.Triangulation(x0, y0)  # Delaunay triangulation
    z0 = z(x0, y0)

    # Then create the test points
    xs0 = np.linspace(-1., 1., 23)
    ys0 = np.linspace(-1., 1., 23)
    xs0, ys0 = np.meshgrid(xs0, ys0)
    xs0 = xs0.ravel()
    ys0 = ys0.ravel()

    interp_z0 = {}
    for i_angle in range(2):
        # Rotating everything
        theta = 2*np.pi / n_angles * i_angle
        x = np.cos(theta)*x0 + np.sin(theta)*y0
        y = -np.sin(theta)*x0 + np.cos(theta)*y0
        xs = np.cos(theta)*xs0 + np.sin(theta)*ys0
        ys = -np.sin(theta)*xs0 + np.cos(theta)*ys0
        triang = mtri.Triangulation(x, y, triang0.triangles)
        linear_interp = mtri.LinearTriInterpolator(triang, z0)
        cubic_min_E = mtri.CubicTriInterpolator(triang, z0)
        cubic_geom = mtri.CubicTriInterpolator(triang, z0, kind='geom')
        dic_interp = {'lin': linear_interp,
                      'min_E': cubic_min_E,
                      'geom': cubic_geom}
        # Testing that the interpolation is invariant by rotation...
        for interp_key in ['lin', 'min_E', 'geom']:
            interp = dic_interp[interp_key]
            if i_angle == 0:
                interp_z0[interp_key] = interp(xs0, ys0)  # storage
            else:
                interpz = interp(xs, ys)
                matest.assert_array_almost_equal(interpz,
                                                 interp_z0[interp_key])

    scale_factor = 987654.3210
    for scaled_axis in ('x', 'y'):
        # Scaling everything (expansion along scaled_axis)
        if scaled_axis == 'x':
            x = scale_factor * x0
            y = y0
            xs = scale_factor * xs0
            ys = ys0
        else:
            x = x0
            y = scale_factor * y0
            xs = xs0
            ys = scale_factor * ys0
        triang = mtri.Triangulation(x, y, triang0.triangles)
        linear_interp = mtri.LinearTriInterpolator(triang, z0)
        cubic_min_E = mtri.CubicTriInterpolator(triang, z0)
        cubic_geom = mtri.CubicTriInterpolator(triang, z0, kind='geom')
        dic_interp = {'lin': linear_interp,
                      'min_E': cubic_min_E,
                      'geom': cubic_geom}
        # Test that the interpolation is invariant by expansion along 1 axis...
        for interp_key in ['lin', 'min_E', 'geom']:
            interpz = dic_interp[interp_key](xs, ys)
            matest.assert_array_almost_equal(interpz, interp_z0[interp_key])