Пример #1
0
    def test_testPut2(self):
        # Test of put
        d = arange(5)
        x = array(d, mask=[0, 0, 0, 0, 0])
        z = array([10, 40], mask=[1, 0])
        assert_(x[2] is not masked)
        assert_(x[3] is not masked)
        x[2:4] = z
        assert_(x[2] is masked)
        assert_(x[3] is not masked)
        assert_(eq(x, [0, 1, 10, 40, 4]))

        d = arange(5)
        x = array(d, mask=[0, 0, 0, 0, 0])
        y = x[2:4]
        z = array([10, 40], mask=[1, 0])
        assert_(x[2] is not masked)
        assert_(x[3] is not masked)
        y[:] = z
        assert_(y[0] is masked)
        assert_(y[1] is not masked)
        assert_(eq(y, [10, 40]))
        assert_(x[2] is masked)
        assert_(x[3] is not masked)
        assert_(eq(x, [0, 1, 10, 40, 4]))
Пример #2
0
    def test_testAverage2(self):
        # More tests of average.
        w1 = [0, 1, 1, 1, 1, 0]
        w2 = [[0, 1, 1, 1, 1, 0], [1, 0, 0, 0, 0, 1]]
        x = arange(6)
        assert_(allclose(average(x, axis=0), 2.5))
        assert_(allclose(average(x, axis=0, weights=w1), 2.5))
        y = array([arange(6), 2.0 * arange(6)])
        assert_(allclose(average(y, None),
                                 np.add.reduce(np.arange(6)) * 3. / 12.))
        assert_(allclose(average(y, axis=0), np.arange(6) * 3. / 2.))
        assert_(allclose(average(y, axis=1),
                                 [average(x, axis=0), average(x, axis=0)*2.0]))
        assert_(allclose(average(y, None, weights=w2), 20. / 6.))
        assert_(allclose(average(y, axis=0, weights=w2),
                                 [0., 1., 2., 3., 4., 10.]))
        assert_(allclose(average(y, axis=1),
                                 [average(x, axis=0), average(x, axis=0)*2.0]))
        m1 = zeros(6)
        m2 = [0, 0, 1, 1, 0, 0]
        m3 = [[0, 0, 1, 1, 0, 0], [0, 1, 1, 1, 1, 0]]
        m4 = ones(6)
        m5 = [0, 1, 1, 1, 1, 1]
        assert_(allclose(average(masked_array(x, m1), axis=0), 2.5))
        assert_(allclose(average(masked_array(x, m2), axis=0), 2.5))
        assert_(average(masked_array(x, m4), axis=0) is masked)
        assert_equal(average(masked_array(x, m5), axis=0), 0.0)
        assert_equal(count(average(masked_array(x, m4), axis=0)), 0)
        z = masked_array(y, m3)
        assert_(allclose(average(z, None), 20. / 6.))
        assert_(allclose(average(z, axis=0),
                                 [0., 1., 99., 99., 4.0, 7.5]))
        assert_(allclose(average(z, axis=1), [2.5, 5.0]))
        assert_(allclose(average(z, axis=0, weights=w2),
                                 [0., 1., 99., 99., 4.0, 10.0]))

        a = arange(6)
        b = arange(6) * 3
        r1, w1 = average([[a, b], [b, a]], axis=1, returned=1)
        assert_equal(shape(r1), shape(w1))
        assert_equal(r1.shape, w1.shape)
        r2, w2 = average(ones((2, 2, 3)), axis=0, weights=[3, 1], returned=1)
        assert_equal(shape(w2), shape(r2))
        r2, w2 = average(ones((2, 2, 3)), returned=1)
        assert_equal(shape(w2), shape(r2))
        r2, w2 = average(ones((2, 2, 3)), weights=ones((2, 2, 3)), returned=1)
        assert_(shape(w2) == shape(r2))
        a2d = array([[1, 2], [0, 4]], float)
        a2dm = masked_array(a2d, [[0, 0], [1, 0]])
        a2da = average(a2d, axis=0)
        assert_(eq(a2da, [0.5, 3.0]))
        a2dma = average(a2dm, axis=0)
        assert_(eq(a2dma, [1.0, 3.0]))
        a2dma = average(a2dm, axis=None)
        assert_(eq(a2dma, 7. / 3.))
        a2dma = average(a2dm, axis=1)
        assert_(eq(a2dma, [1.5, 4.0]))
Пример #3
0
 def test_testMinMax2(self):
     # Test of minimum, maximum.
     assert_(eq(minimum([1, 2, 3], [4, 0, 9]), [1, 0, 3]))
     assert_(eq(maximum([1, 2, 3], [4, 0, 9]), [4, 2, 9]))
     x = arange(5)
     y = arange(5) - 2
     x[3] = masked
     y[0] = masked
     assert_(eq(minimum(x, y), where(less(x, y), x, y)))
     assert_(eq(maximum(x, y), where(greater(x, y), x, y)))
     assert_(minimum.reduce(x) == 0)
     assert_(maximum.reduce(x) == 4)
Пример #4
0
def self_training(X, y, X_unLabeled, clf, th):
    clf.fit(X=X, y=y)
    index_unlabeled = ma.arange(0, len(X_unLabeled), 1)
    y_unlabeled = np.zeros(len(X_unLabeled))
    train_is_failed = False

    while True:
        probs = clf.predict_proba(X=X_unLabeled[~ma.getmaskarray(index_unlabeled)])
        index_greater_equal = np.greater_equal([max(d) for d in probs], [th]*len(probs))
        index_labelable = index_unlabeled.data[~ma.getmaskarray(index_unlabeled)][index_greater_equal]

        if not len(index_labelable) > 0:
            if not len(index_unlabeled.data[ma.getmaskarray(index_unlabeled)]) > 0:
                train_is_failed = True
            break

        index_unlabeled[index_labelable] = ma.masked

        if index_unlabeled.all() is ma.masked:
            break

        y_unlabeled[index_labelable] = [np.argmax(p) for p in probs[index_greater_equal]]

        X_labelable = X_unLabeled[index_unlabeled.mask]
        y_labelable = y_unlabeled[index_unlabeled.mask]

        clf.fit(X=np.append(X, X_labelable, axis=0),
                y=np.append(y, y_labelable))

    if train_is_failed:
        y_unlabeled = []
    else:
        y_unlabeled = ma.array(data=y_unlabeled, mask=index_unlabeled.mask)

    return clf, y_unlabeled
def solve_equation(k, m, x0, xk, n, h, t, w, x, solver, method_name, draw_flag):
    solver(k, m, x0, xk, n, h, t, w, x)
    xx = arange(x0, xk, 0.1)
    f_y = []
    for i in x:
        f_y.append(calculate_f_x_1(i, k, m))
    mean_square_error = calculate_mean_square_error(x, f_y, w)
    max_error = calculate_max_norm(x, f_y, w)
    mean_format = "{:.4f}"
    max_format = "{:.4f}"
    if mean_square_error < 0.0001:
        mean_format = "{:.3e}"
    elif mean_square_error > 100:
        mean_format = "{:.0f}"
    if max_error < 0.0001:
        max_format = "{:.3e}"
    elif max_error > 100:
        max_format = "{:.0f}"
    print(";".join((str(n), mean_format.format(mean_square_error), max_format.format(max_error))))
    if draw_flag:
        add_to_plot(xx, calculate_f_x_1(xx, k, m), "Given function")
        add_to_plot(x, w, method_name + " differential")
        file_name = method_name + "_" + str(n)
        title = (
            method_name
            + "  n = "
            + str(n)
            + "\n mean square error = "
            + mean_format.format(mean_square_error)
            + "\n max error = "
            + max_format.format(max_error)
        )
        draw_plot(title, file_name)
Пример #6
0
 def test_testMasked(self):
     # Test of masked element
     xx = arange(6)
     xx[1] = masked
     self.assertTrue(str(masked) == '--')
     self.assertTrue(xx[1] is masked)
     self.assertEqual(filled(xx[1], 0), 0)
Пример #7
0
def plotBestFit(weights):
    import matplotlib.pyplot as plt
    dataMat, labelMat = loadDataSet()
    dataArr = array(dataMat)
    n = shape(dataArr)[0]
    xcord1 = []
    ycord1 = []
    xcord2 = []
    ycord2 = []
    for i in range(n):
        if int(labelMat[i]) == 1:
            xcord1.append(dataArr[i, 1])
            ycord1.append(dataArr[i, 2])
        else:
            xcord2.append(dataArr[i, 1])
            ycord2.append(dataArr[i, 2])
    fig = plt.figure()
    ax = fig.add_subplot(111)
    ax.scatter(xcord1, ycord1, s=30, c='red', marker='s')
    ax.scatter(xcord2, ycord2, s=30, c='green')
    x = arange(-3.0, 3.0, 0.1)
    y = (-weights[0] - weights[1] * x) / weights[2]
    ax.plot(x, y)
    plt.xlabel('X')
    plt.ylabel('Y')
    plt.show()
Пример #8
0
 def test_masked_1d_single(self):
     data = ma.arange(11)
     data[3:7] = ma.masked
     actual = PERCENTILE.aggregate(data, axis=0, percent=50)
     expected = 7
     self.assertTupleEqual(actual.shape, ())
     self.assertEqual(actual, expected)
Пример #9
0
def brute_force(f, a, b, segments):
    x0 = a
    x1 = b
    dx = (x1 - x0) / segments

    fs = map(lambda _x: (_x, f(_x)), arange(x0, x1, dx))
    return min(fs, key=lambda xf: abs(xf[1]))
Пример #10
0
    def test_pearsonr(self):
        # Tests some computations of Pearson's r
        x = ma.arange(10)
        with warnings.catch_warnings():
            # The tests in this context are edge cases, with perfect
            # correlation or anticorrelation, or totally masked data.
            # None of these should trigger a RuntimeWarning.
            warnings.simplefilter("error", RuntimeWarning)

            assert_almost_equal(mstats.pearsonr(x, x)[0], 1.0)
            assert_almost_equal(mstats.pearsonr(x, x[::-1])[0], -1.0)

            x = ma.array(x, mask=True)
            pr = mstats.pearsonr(x, x)
            assert_(pr[0] is masked)
            assert_(pr[1] is masked)

        x1 = ma.array([-1.0, 0.0, 1.0])
        y1 = ma.array([0, 0, 3])
        r, p = mstats.pearsonr(x1, y1)
        assert_almost_equal(r, np.sqrt(3)/2)
        assert_almost_equal(p, 1.0/3)

        # (x2, y2) have the same unmasked data as (x1, y1).
        mask = [False, False, False, True]
        x2 = ma.array([-1.0, 0.0, 1.0, 99.0], mask=mask)
        y2 = ma.array([0, 0, 3, -1], mask=mask)
        r, p = mstats.pearsonr(x2, y2)
        assert_almost_equal(r, np.sqrt(3)/2)
        assert_almost_equal(p, 1.0/3)
Пример #11
0
def grid_linefit(grid, timevals=None, timeslice=slice(None, None, None)):
    """A compressed spatiotemporal grid is provided. A line fit is performed 
    along the time axis for each spatial cell. Two grids are returned,
    each of which is 2d, with the same spatial shape as the input.
    The pixels of one grid contains the slope, the other contains the 
    r squared value of the line fit for that spatial cell.
    A vector of time values may be provided. If not supplied, one 
    will be generated."""
    if timevals == None:
        timevals = ma.arange(grid.shape[0])
    X = sm.add_constant(timevals, prepend=True)

    outshape = (grid.shape[1],)

    rsq_map = ma.zeros(outshape)
    slope_map = ma.zeros(outshape)

    for i in range(outshape[0]):
        if (i % 1000) == 0:
            print "%d of %d (%f)" % (i, outshape[0], (i * 100.0) / outshape[0])
        if (type(grid) == "numpy.ma.core.MaskedArray") and grid[0, :].mask[i]:
            rsq_map[i] = ma.masked
            slope_map[i] = ma.masked
        else:
            m, rsq = linefit(grid, i, X, timeslice)
            rsq_map[i] = rsq
            slope_map[i] = m

    return (slope_map, rsq_map)
Пример #12
0
 def test_testMasked(self):
     # Test of masked element
     xx = arange(6)
     xx[1] = masked
     assert_(str(masked) == '--')
     assert_(xx[1] is masked)
     assert_equal(filled(xx[1], 0), 0)
Пример #13
0
    def test_multi(self):
        for dtype in [np.int, np.float]:
            data = ma.arange(12, dtype=dtype)
            data[::2] = ma.masked
            self._check(data.reshape(3, 4))

            data = ma.arange(12, dtype=dtype)
            data[1::2] = ma.masked
            self._check(data.reshape(3, 4))

            data = ma.arange(12, dtype=dtype).reshape(3, 4)
            data[::2] = ma.masked
            self._check(data)

            data = ma.arange(12, dtype=dtype).reshape(3, 4)
            data[1::2] = ma.masked
            self._check(data)
Пример #14
0
 def test_masked_1d_multi(self):
     data = ma.arange(11)
     data[3:9] = ma.masked
     percent = np.array([25, 50, 75])
     actual = PERCENTILE.aggregate(data, axis=0, percent=percent)
     expected = [1, 2, 9]
     self.assertTupleEqual(actual.shape, percent.shape)
     self.assertArrayEqual(actual, expected)
Пример #15
0
 def test_scalar_mask(self):
     # Testing the bug raised in https://github.com/SciTools/iris/pull/123#issuecomment-9309872
     # (the fix workaround for the np.append bug failed for scalar masks) 
     cube = tests.stock.realistic_4d_w_missing_data()
     cube.data = ma.arange(np.product(cube.shape), dtype=np.float32).reshape(cube.shape)
     cube.coord('grid_longitude').circular = True
     # There's no result to test, just make sure we don't cause an exception with the scalar mask.
     _ = iris.analysis.interpolate.linear(cube, [('grid_longitude', 0), ('grid_latitude', 0)])
Пример #16
0
    def test_trim(self):
        a = ma.arange(10)
        assert_equal(mstats.trim(a), [0,1,2,3,4,5,6,7,8,9])
        a = ma.arange(10)
        assert_equal(mstats.trim(a,(2,8)), [None,None,2,3,4,5,6,7,8,None])
        a = ma.arange(10)
        assert_equal(mstats.trim(a,limits=(2,8),inclusive=(False,False)),
                     [None,None,None,3,4,5,6,7,None,None])
        a = ma.arange(10)
        assert_equal(mstats.trim(a,limits=(0.1,0.2),relative=True),
                     [None,1,2,3,4,5,6,7,None,None])

        a = ma.arange(12)
        a[[0,-1]] = a[5] = masked
        assert_equal(mstats.trim(a,(2,8)),
                     [None,None,2,3,4,None,6,7,8,None,None,None])

        x = ma.arange(100).reshape(10,10)
        trimx = mstats.trim(x,(0.1,0.2),relative=True,axis=None)
        assert_equal(trimx._mask.ravel(),[1]*10+[0]*70+[1]*20)
        trimx = mstats.trim(x,(0.1,0.2),relative=True,axis=0)
        assert_equal(trimx._mask.ravel(),[1]*10+[0]*70+[1]*20)
        trimx = mstats.trim(x,(0.1,0.2),relative=True,axis=-1)
        assert_equal(trimx._mask.T.ravel(),[1]*10+[0]*70+[1]*20)

        x = ma.arange(110).reshape(11,10)
        x[1] = masked
        trimx = mstats.trim(x,(0.1,0.2),relative=True,axis=None)
        assert_equal(trimx._mask.ravel(),[1]*20+[0]*70+[1]*20)
        trimx = mstats.trim(x,(0.1,0.2),relative=True,axis=0)
        assert_equal(trimx._mask.ravel(),[1]*20+[0]*70+[1]*20)
        trimx = mstats.trim(x.T,(0.1,0.2),relative=True,axis=-1)
        assert_equal(trimx.T._mask.ravel(),[1]*20+[0]*70+[1]*20)
Пример #17
0
def test_hdmedian():
    # 1-D array
    x = ma.arange(11)
    assert_allclose(ms.hdmedian(x), 5, rtol=1e-14)
    x.mask = ma.make_mask(x)
    x.mask[:7] = False
    assert_allclose(ms.hdmedian(x), 3, rtol=1e-14)

    # Check that `var` keyword returns a value.  TODO: check whether returned
    # value is actually correct.
    assert_(ms.hdmedian(x, var=True).size == 2)

    # 2-D array
    x2 = ma.arange(22).reshape((11, 2))
    assert_allclose(ms.hdmedian(x2, axis=0), [10, 11])
    x2.mask = ma.make_mask(x2)
    x2.mask[:7, :] = False
    assert_allclose(ms.hdmedian(x2, axis=0), [6, 7])
Пример #18
0
 def test_testPickle(self):
     # Test of pickling
     x = arange(12)
     x[4:10:2] = masked
     x = x.reshape(4, 3)
     for proto in range(2, pickle.HIGHEST_PROTOCOL + 1):
         s = pickle.dumps(x, protocol=proto)
         y = pickle.loads(s)
         assert_(eq(x, y))
Пример #19
0
def recursive_brute_force(f, a, b, segments=10, recursions=10, epsilon=1e-5):
    x0 = a
    x1 = b
    dx = (x1 - x0) / segments

    for _ in arange(0, recursions, 1):
        fs = map(lambda _x: (_x, f(_x)), arange(x0, x1, dx))
        x, _f = min(fs, key=lambda xf: abs(xf[1]))
        print(x, _f)

        if abs(_f) < epsilon:
            return x, _f

        x0 = x - dx
        x1 = x + dx
        dx = (x1 - x0) / segments

    return x, _f
Пример #20
0
 def setUp(self):
     self.data = ma.arange(12).reshape(3, 4)
     self.data.mask = [[0, 0, 0, 1],
                       [0, 0, 1, 1],
                       [0, 1, 1, 1]]
     # --> fractions of masked-points in columns = [0, 1/3, 2/3, 1]
     self.array = as_lazy_data(self.data)
     self.axis = 0
     self.expected_masked = ma.mean(self.data, axis=self.axis)
Пример #21
0
 def test_testPickle(self):
     # Test of pickling
     import pickle
     x = arange(12)
     x[4:10:2] = masked
     x = x.reshape(4, 3)
     s = pickle.dumps(x)
     y = pickle.loads(s)
     assert_(eq(x, y))
Пример #22
0
 def test_minmax(self):
     a = arange(1, 13).reshape(3, 4)
     amask = masked_where(a < 5, a)
     assert_equal(amask.max(), a.max())
     assert_equal(amask.min(), 5)
     assert_((amask.max(0) == a.max(0)).all())
     assert_((amask.min(0) == [5, 6, 7, 8]).all())
     assert_(amask.max(1)[0].mask)
     assert_(amask.min(1)[0].mask)
Пример #23
0
    def test_flat(self):
        for dtype in [np.int, np.float]:
            data = ma.arange(12, dtype=dtype)
            data[::2] = ma.masked
            self._check(data)

            data.mask = ma.nomask
            data[1::2] = ma.masked
            self._check(data)
Пример #24
0
 def test_masked_1d_multi(self):
     data = ma.arange(11)
     weights = np.ones(data.shape)
     data[3:9] = ma.masked
     percent = np.array([25, 50, 75])
     actual = WPERCENTILE.aggregate(data, axis=0, percent=percent,
                                    weights=weights)
     expected = [0.75, 2, 9.25]
     self.assertTupleEqual(actual.shape, percent.shape)
     self.assertArrayAlmostEqual(actual, expected)
 def test_pearsonr(self):
     "Tests some computations of Pearson's r"
     x = ma.arange(10)
     assert_almost_equal(mstats.pearsonr(x,x)[0], 1.0)
     assert_almost_equal(mstats.pearsonr(x,x[::-1])[0], -1.0)
     #
     x = ma.array(x, mask=True)
     pr = mstats.pearsonr(x,x)
     assert(pr[0] is masked)
     assert(pr[1] is masked)
Пример #26
0
 def test_masked_2d_single(self):
     shape = (2, 11)
     data = ma.arange(np.prod(shape)).reshape(shape)
     data[0, ::2] = ma.masked
     data[1, 1::2] = ma.masked
     actual = PERCENTILE.aggregate(data, axis=0, percent=50)
     self.assertTupleEqual(actual.shape, shape[-1:])
     expected = np.empty(shape[-1:])
     expected[1::2] = data[0, 1::2]
     expected[::2] = data[1, ::2]
     self.assertArrayEqual(actual, expected)
Пример #27
0
 def test_masked_2d_multi(self):
     shape = (3, 10)
     data = ma.arange(np.prod(shape)).reshape(shape)
     data[1] = ma.masked
     percent = np.array([10, 50, 70, 80])
     actual = PERCENTILE.aggregate(data, axis=0, percent=percent)
     self.assertTupleEqual(actual.shape, (shape[-1], percent.size))
     expected = np.tile(np.arange(shape[-1]), percent.size)
     expected = expected.reshape(percent.size, shape[-1]).T
     expected = expected + (percent / 10 * 2)
     self.assertArrayAlmostEqual(actual, expected)
Пример #28
0
 def test_trim_old(self):
     x = ma.arange(100)
     assert_equal(mstats.trimboth(x).count(), 60)
     assert_equal(mstats.trimtail(x,tail='r').count(), 80)
     x[50:70] = masked
     trimx = mstats.trimboth(x)
     assert_equal(trimx.count(), 48)
     assert_equal(trimx._mask, [1]*16 + [0]*34 + [1]*20 + [0]*14 + [1]*16)
     x._mask = nomask
     x.shape = (10,10)
     assert_equal(mstats.trimboth(x).count(), 60)
     assert_equal(mstats.trimtail(x).count(), 80)
    def test_forward_fill(self):
        x = ma.arange(20)
        x[(x%5 != 0)] = masked
        # Test forward_fill w/o gaps, starting unmasked
        test = forward_fill(x)
        assert_equal(test, [ 0, 0, 0, 0, 0, 5, 5, 5, 5, 5,
                            10,10,10,10,10,15,15,15,15,15])
        # Test forward_fill w/ gaps, starting unmasked
        test = forward_fill(x, 3)
        assert_equal(test, x)
        assert_equal(test._mask, x._mask)
        # Test forward_fill w/ gaps, starting unmasked
        x[[3,4]] = (3,4)
        test = forward_fill(x, 3)
        assert_equal(test, [ 0, 0, 0, 3, 4, 5, 5, 5, 5, 5,
                            10,10,10,10,10,15,15,15,15,15,])
        assert_equal(test._mask,[0,0,0,0,0,0,1,1,1,1,
                                 0,1,1,1,1,0,1,1,1,1,])
        # Test forward_fill w/o gaps, starting masked
        x[[0,3,4]] = masked
        test = forward_fill(x)
        assert_equal(test, [ 0, 0, 0, 0, 0, 5, 5, 5, 5, 5,
                            10,10,10,10,10,15,15,15,15,15])
        assert_equal(test._mask, [1,1,1,1,1,0,0,0,0,0,
                                  0,0,0,0,0,0,0,0,0,0,])
        # Test forward_fill w/ gaps, starting masked
        test = forward_fill(x,3)
        assert_equal(test, [ 0, 0, 0, 0, 0, 5, 5, 5, 5, 5,
                            10,10,10,10,10,15,15,15,15,15])
        assert_equal(test._mask, [1,1,1,1,1,0,1,1,1,1,
                                  0,1,1,1,1,0,1,1,1,1,])

        # Test forward_fill w/o gaps, starting masked, ending unmasked
        x[:].mask = False
        x[0] = masked
        test = forward_fill(x)
        assert_equal(test, ma.arange(20))
        assert_equal(test._mask, [1,0,0,0,0,0,0,0,0,0,
                                  0,0,0,0,0,0,0,0,0,0,])
Пример #30
0
 def test_realised_dtype_none(self):
     shape = (2, 3, 4)
     size = np.prod(shape)
     mask_array = ma.arange(size).reshape(shape)
     dtype = mask_array.dtype
     lazy_array = as_lazy_data(mask_array)
     dm = DataManager(lazy_array, realised_dtype=dtype)
     self.assertIsNone(dm._realised_dtype)
     self.assertEqual(dm.dtype, dtype)
     dm.data = mask_array
     self.assertIs(dm.data, mask_array)
     self.assertIsNone(dm._realised_dtype)
     self.assertEqual(dm.dtype, dtype)
Пример #31
0
    def test_testCopySize(self):
        # Tests of some subtle points of copying and sizing.
        with suppress_warnings() as sup:
            sup.filter(
                np.ma.core.MaskedArrayFutureWarning,
                "setting an item on a masked array which has a "
                "shared mask will not copy")

            n = [0, 0, 1, 0, 0]
            m = make_mask(n)
            m2 = make_mask(m)
            self.assertTrue(m is m2)
            m3 = make_mask(m, copy=1)
            self.assertTrue(m is not m3)

            x1 = np.arange(5)
            y1 = array(x1, mask=m)
            self.assertTrue(y1._data is not x1)
            self.assertTrue(allequal(x1, y1._data))
            self.assertTrue(y1.mask is m)

            y1a = array(y1, copy=0)
            self.assertTrue(y1a.mask is y1.mask)

            y2 = array(x1, mask=m, copy=0)
            self.assertTrue(y2.mask is m)
            self.assertTrue(y2[2] is masked)
            y2[2] = 9
            self.assertTrue(y2[2] is not masked)
            self.assertTrue(y2.mask is not m)
            self.assertTrue(allequal(y2.mask, 0))

            y3 = array(x1 * 1.0, mask=m)
            self.assertTrue(filled(y3).dtype is (x1 * 1.0).dtype)

            x4 = arange(4)
            x4[2] = masked
            y4 = resize(x4, (8, ))
            self.assertTrue(eq(concatenate([x4, x4]), y4))
            self.assertTrue(eq(getmask(y4), [0, 0, 1, 0, 0, 0, 1, 0]))
            y5 = repeat(x4, (2, 2, 2, 2), axis=0)
            self.assertTrue(eq(y5, [0, 0, 1, 1, 2, 2, 3, 3]))
            y6 = repeat(x4, 2, axis=0)
            self.assertTrue(eq(y5, y6))
Пример #32
0
    def test_testPut(self):
        # Test of put
        d = arange(5)
        n = [0, 0, 0, 1, 1]
        m = make_mask(n)
        x = array(d, mask=m)
        self.assertTrue(x[3] is masked)
        self.assertTrue(x[4] is masked)
        x[[1, 4]] = [10, 40]
        self.assertTrue(x.mask is not m)
        self.assertTrue(x[3] is masked)
        self.assertTrue(x[4] is not masked)
        self.assertTrue(eq(x, [0, 10, 2, -1, 40]))

        x = array(d, mask=m)
        x.put([0, 1, 2], [-1, 100, 200])
        self.assertTrue(eq(x, [-1, 100, 200, 0, 0]))
        self.assertTrue(x[3] is masked)
        self.assertTrue(x[4] is masked)
Пример #33
0
 def test_testTakeTransposeInnerOuter(self):
     # Test of take, transpose, inner, outer products
     x = arange(24)
     y = np.arange(24)
     x[5:6] = masked
     x = x.reshape(2, 3, 4)
     y = y.reshape(2, 3, 4)
     assert_(eq(np.transpose(y, (2, 0, 1)), transpose(x, (2, 0, 1))))
     assert_(eq(np.take(y, (2, 0, 1), 1), take(x, (2, 0, 1), 1)))
     assert_(eq(np.inner(filled(x, 0), filled(y, 0)),
                inner(x, y)))
     assert_(eq(np.outer(filled(x, 0), filled(y, 0)),
                outer(x, y)))
     y = array(['abc', 1, 'def', 2, 3], object)
     y[2] = masked
     t = take(y, [0, 3, 4])
     assert_(t[0] == 'abc')
     assert_(t[1] == 2)
     assert_(t[2] == 3)
Пример #34
0
 def test_known_with_masked(self):
     dims = self.Blocks[0].dims
     data = ma.ones(dims) * sp.arange(1, dims[-1] + 1)
     data[0:-1:2, 0, 0, :] = 2 * sp.arange(1, dims[-1] + 1)
     for Data in self.Blocks:
         Data.data = data
     self.Blocks[0].data[2, 0, 0, 43] = ma.masked
     self.Blocks[0].data[7, 0, 0, 43] = ma.masked
     self.Blocks[1].data[4, 0, 0, 43] = ma.masked
     self.Blocks[1].data[3, 0, 0, 43] = ma.masked
     self.Blocks[0].data[:, 0, 0, 103] = ma.masked
     self.Blocks[1].data[:, 0, 0, 103] = ma.masked
     self.Blocks[0].data[:, 0, 0, 554] = ma.masked
     self.Blocks[1].data[1:, 0, 0, 554] = ma.masked
     var = tools.calc_time_var_file(self.Blocks, 0, 0)
     expected = ma.arange(1, dims[-1] + 1)**2 / 4.0
     expected[103] = ma.masked
     expected[554] = ma.masked
     self.assertTrue(sp.allclose(var.filled(-1), expected.filled(-1)))
def test_GridToMeshESMFRegridder_curvilinear_round_trip(tmp_path):
    """Test save/load round tripping for `GridToMeshESMFRegridder`."""
    original_rg, src = _make_grid_to_mesh_regridder(grid_dims=2)
    filename = tmp_path / "regridder.nc"
    save_regridder(original_rg, filename)
    loaded_rg = load_regridder(str(filename))

    assert original_rg.grid_x == loaded_rg.grid_x
    assert original_rg.grid_y == loaded_rg.grid_y

    # Demonstrate regridding still gives the same results.
    src_data = ma.arange(np.product(src.data.shape)).reshape(src.data.shape)
    src_data[0, 0] = ma.masked
    src.data = src_data
    # TODO: make this a cube comparison when mesh comparison becomes available.
    original_result = original_rg(src).data
    loaded_result = loaded_rg(src).data
    assert np.array_equal(original_result, loaded_result)
    assert np.array_equal(original_result.mask, loaded_result.mask)
def ReadFile(id):
    x_axis = []
    y_axis = []

    w = 6
    bound = id + 1 * w
    for i in range(id, bound, 1):
        col = []
        f = open(exp_dir + "/results/timestamps/PRJ_{}.txt".format(i), "r")
        read = f.readlines()
        for r in read:
            value = double(r.strip("\n"))  # timestamp.
            col.append(value)
        # calculate the proportional values of samples
        coly = 1. * arange(len(col)) / (len(col) - 1)
        x_axis.append(col)
        y_axis.append(coly)

    return x_axis, y_axis
Пример #37
0
    def histo_plot(self, yaxis, labels):
        _ = self
        xaxis = arange(0.0, len(labels), 1.0)
        # xaxis = [1.0 * x for x in range(0, len(labels))]
        fig = plt.figure()
        fig.suptitle('Corpora Pattern Count', fontsize=14, fontweight='bold')

        ax = fig.add_subplot(111)
        fig.subplots_adjust(top=0.92, left=0.05, right=0.98, bottom=0.08)
        ax.set_xticklabels(labels)
        plt.xticks(xaxis)
        # axes = plt.gca()
        # axes.set_ylim([COUNT_MIN, COUNT_MAX])
        ax.set_xlabel('patterns')
        ax.set_ylabel('count')
        plt.xticks(rotation=90)
        for xoff, (ydata, ycolor) in enumerate(zip(yaxis, list("rgbmyc"))):
            ax.bar(xaxis + 0.2 * xoff, ydata, width=0.2, color=ycolor)
        show()
Пример #38
0
 def test_masked_2d_multi_unequal(self):
     shape = (3, 10)
     data = ma.arange(np.prod(shape)).reshape(shape)
     weights = np.ones(shape)
     weights[0] = 3
     data[1] = ma.masked
     percent = np.array([30, 50, 75, 80])
     actual, weight_total = WPERCENTILE.aggregate(data,
                                                  axis=0,
                                                  percent=percent,
                                                  weights=weights,
                                                  returned=True)
     self.assertTupleEqual(actual.shape, (shape[-1], percent.size))
     expected = np.tile(np.arange(shape[-1]), percent.size)
     expected = expected.reshape(percent.size, shape[-1]).T
     expected[:, 1:] = 2.0 * (
         (0.875 - percent[1:] / 100.0) * data[0, np.newaxis].T +
         (percent[1:] / 100.0 - 0.375) * data[-1, np.newaxis].T)
     self.assertArrayAlmostEqual(actual, expected)
     self.assertTupleEqual(weight_total.shape, (shape[-1], ))
     self.assertArrayEqual(weight_total, np.repeat(4, shape[-1]))
Пример #39
0
    def test_testPut(self):
        # Test of put
        d = arange(5)
        n = [0, 0, 0, 1, 1]
        m = make_mask(n)
        m2 = m.copy()
        x = array(d, mask=m)
        assert_(x[3] is masked)
        assert_(x[4] is masked)
        x[[1, 4]] = [10, 40]
        assert_(x._mask is m)
        assert_(x[3] is masked)
        assert_(x[4] is not masked)
        assert_(eq(x, [0, 10, 2, -1, 40]))

        x = array(d, mask=m2, copy=True)
        x.put([0, 1, 2], [-1, 100, 200])
        assert_(x._mask is not m2)
        assert_(x[3] is masked)
        assert_(x[4] is masked)
        assert_(eq(x, [-1, 100, 200, 0, 0]))
Пример #40
0
def _interpolate(the_test):
    """
        Interpolate the road points using cubic splines and ensure we handle 4F tuples for compatibility
    """
    old_x_vals = [t[0] for t in the_test]
    old_y_vals = [t[1] for t in the_test]

    # This is an approximation based on whatever input is given
    test_road_lenght = LineString([(t[0], t[1]) for t in the_test]).length
    num_nodes = int(test_road_lenght / interpolation_distance)
    if num_nodes < min_num_nodes:
        num_nodes = min_num_nodes

    assert len(
        old_x_vals) >= 2, "You need at leas two road points to define a road"
    assert len(
        old_y_vals) >= 2, "You need at leas two road points to define a road"

    if len(old_x_vals) == 2:
        # With two points the only option is a straight segment
        k = 1
    elif len(old_x_vals) == 3:
        # With three points we use an arc, using linear interpolation will result in invalid road tests
        k = 2
    else:
        # Otheriwse, use cubic splines
        k = 3

    pos_tck, pos_u = splprep([old_x_vals, old_y_vals], s=smoothness, k=k)

    step_size = 1 / num_nodes
    unew = arange(0, 1 + step_size, step_size)

    new_x_vals, new_y_vals = splev(unew, pos_tck)

    # Return the 4-tuple with default z and defatul road width
    return list(
        zip([round(v, rounding_precision) for v in new_x_vals],
            [round(v, rounding_precision) for v in new_y_vals],
            [-28.0 for v in new_x_vals], [8.0 for v in new_x_vals]))
Пример #41
0
def show(model, x_tr, x_te, y_tr, y_te, verb=0, plt=True, bs=200, eps=None, plt_title=None, num=None):
    acc = list()
    val_acc = list()
    test_acc = list()
    loss = list()
    epochs = 10 if not eps else eps
    best_scores = [-1, -1, -1, -1]
    best_epoch = None
    loss_now = 1000000
    go = True
    for _ in range(epochs):
        history = model.fit(x_tr, y_tr, epochs=1, batch_size=bs, validation_split=0.05, verbose=verb)
        acc += history.history['accuracy']
        val_acc += history.history['val_accuracy']
        scores = model.evaluate(x_te, y_te, verbose=0)
        test_acc += [scores[3]]
        loss += history.history['val_loss']
        if num is not None and _ % num == 0:
            f1_score = 2 * scores[1] * scores[2] / (scores[1] + scores[2]) if scores[1] + scores[2] != 0 else 0
            print("Accuracy: {}    F1 score: {}    Loss: {}".format(scores[3], f1_score, scores[0]))
        if loss_now > history.history['val_loss'][0] and go:
            loss_now = history.history['val_loss'][0]
            if best_scores[3] < scores[3]:
                best_scores = scores
                best_epoch = _ + 1
        else:
            go = False

    if plt:
        figure(figsize=(8, 5))
        grid(True)
        xticks(arange(1, epochs + 1, 1))
        yticks(arange(0, 1.05, 0.05))
        plot_title(plt_title)
        plot(arange(1, epochs + 1, 1), acc, label='Learning')
        plot(arange(1, epochs + 1, 1), val_acc, label='Validation')
        plot(arange(1, epochs + 1, 1), test_acc, label='Test')
        plot(arange(1, epochs + 1, 1), loss, label='Validation loss')
        xlabel('Epoch')
        ylabel('Accuracy')
        legend()
        # plot_show()
        savefig("plots/" + plt_title + ".png")
        close("all")
    del model

    return best_scores, best_epoch
Пример #42
0
def newton_czybyszew(nodes_amount):
    gd = graph(title='Interpolacja Newtona\'a')
    f1 = gcurve(graph=gd, color=color.cyan)
    dots = gdots(graph=gd, color=color.red)
    f2 = gcurve(graph=gd, color=color.green)

    # wyliczanie wartości funkcji podstawowej w tym przypadku cos(2*x) * exp(-0.2 * x)
    field_end = 8.05
    for x in arange(0, field_end, 0.1):
        y = origin_newton_function(x)
        f1.plot(x, y)

    # tablica do przechowywania wylosowanych punktów
    initial_points = []
    for x in czybyszew_points(nodes_amount, 0, field_end):
        y = origin_newton_function(x)
        dots.plot(x, y)
        initial_points.append({'x': x, 'y': y})

    # wyliczanie i rysowanie interpolacji
    for point in newton_interpolation(initial_points, field_end):
        f2.plot(point['x'], point['y'])
Пример #43
0
    def test_testCopySize(self):
        # Tests of some subtle points of copying and sizing.
        n = [0, 0, 1, 0, 0]
        m = make_mask(n)
        m2 = make_mask(m)
        self.assertTrue(m is m2)
        m3 = make_mask(m, copy=1)
        self.assertTrue(m is not m3)

        x1 = np.arange(5)
        y1 = array(x1, mask=m)
        self.assertTrue(y1._data is not x1)
        self.assertTrue(allequal(x1, y1._data))
        self.assertTrue(y1.mask is m)

        y1a = array(y1, copy=0)
        self.assertTrue(y1a.mask is y1.mask)

        y2 = array(x1, mask=m, copy=0)
        self.assertTrue(y2.mask is m)
        self.assertTrue(y2[2] is masked)
        y2[2] = 9
        self.assertTrue(y2[2] is not masked)
        self.assertTrue(y2.mask is not m)
        self.assertTrue(allequal(y2.mask, 0))

        y3 = array(x1 * 1.0, mask=m)
        self.assertTrue(filled(y3).dtype is (x1 * 1.0).dtype)

        x4 = arange(4)
        x4[2] = masked
        y4 = resize(x4, (8, ))
        self.assertTrue(eq(concatenate([x4, x4]), y4))
        self.assertTrue(eq(getmask(y4), [0, 0, 1, 0, 0, 0, 1, 0]))
        y5 = repeat(x4, (2, 2, 2, 2), axis=0)
        self.assertTrue(eq(y5, [0, 0, 1, 1, 2, 2, 3, 3]))
        y6 = repeat(x4, 2, axis=0)
        self.assertTrue(eq(y5, y6))
Пример #44
0
 def _interpolate_nodes(
     old_x_vals: List[float], old_y_vals: List[float],
     old_width_vals: List[float], num_nodes: int
 ) -> Tuple[List[float], List[float], List[float], List[float]]:
     assert len(old_x_vals) == len(old_y_vals) == len(old_width_vals), \
         "The lists for the interpolation must have the same length."
     k = 1 if len(old_x_vals) <= 3 else 3
     pos_tck, pos_u = splprep([old_x_vals, old_y_vals],
                              s=self.add_roads_to_scenario.smoothness,
                              k=k)
     step_size = 1 / num_nodes
     unew = arange(0, 1 + step_size, step_size)
     new_x_vals, new_y_vals = splev(unew, pos_tck)
     z_vals = repeat(0.01, len(unew))
     width_tck, width_u = splprep(
         [pos_u, old_width_vals],
         s=self.add_roads_to_scenario.smoothness,
         k=k)
     _, new_width_vals = splev(unew, width_tck)
     # Reduce floating point rounding errors otherwise these may cause problems with calculating parallel_offset
     return [round(v, _interpolate_nodes.rounding_precision) for v in new_x_vals], \
            [round(v, _interpolate_nodes.rounding_precision) for v in new_y_vals], \
            z_vals, new_width_vals
Пример #45
0
def plotBestFit(w, b, dataMat, labelMat):
    m = dataMat.shape[0]
    xcord1 = []
    ycord1 = []
    xcord2 = []
    ycord2 = []
    for i in range(m):
        if int(labelMat[i]) == 1:
            xcord1.append(dataMat[i, 0])
            ycord1.append(dataMat[i, 1])
        else:
            xcord2.append(dataMat[i, 0])
            ycord2.append(dataMat[i, 1])
    fig = plt.figure()
    ax = fig.add_subplot(111)
    ax.scatter(xcord1, ycord1, s=30, c='red', marker='x')
    ax.scatter(xcord2, ycord2, s=30, c='green', marker='o')
    x = arange(-3.0, 3.0, 0.1)
    y = (-w[0] * x - b) / w[1]
    ax.plot(x, y)
    plt.xlabel('X')
    plt.ylabel('Y')
    plt.show()
Пример #46
0
def get_recall_data(statistics, available_classes, alg_name):
    x_complex = []
    add_empty(x_complex, available_classes)
    for statistic in statistics:
        for index, clazz in enumerate(available_classes):
            for single_recall in statistic.class_recall:
                if single_recall.class_name == clazz:
                    x_complex[index].is_nullable = False
                    x_complex[index].items.append(single_recall.recall)
    for item in x_complex:
        if item.is_nullable:
            x_complex.remove(item)

    plt.figure(num=None, figsize=(14, 7), dpi=80, facecolor='w', edgecolor='k')
    plt.rcParams.update({'font.size': 20})
    plt.subplot(111)
    x_range = list(arange(1, len(x_complex[0].items) + 1, 1.0))
    x = np.array(x_range)
    plt.bar(x - 0.3,
            x_complex[0].items,
            width=0.6,
            fill=True,
            label=x_complex[0].class_name,
            align='center')
    plt.bar(x + 0.3,
            x_complex[1].items,
            width=0.6,
            fill=True,
            label=x_complex[1].class_name,
            align='center')
    plt.xlabel('Numer klatki', fontsize=20)
    plt.ylabel('Pokrycie', fontsize=20)
    plt.title('Pokrycie w poszczególnych klatkach - ' + alg_name, fontsize=20)
    plt.xticks(np.arange(0, len(x), 10))
    # handles, labels = plt.get_legend_handles_labels()
    plt.legend(prop={'size': 20})
    plt.show()
Пример #47
0
def gurwitz_crit():
    w = initPamsLab2.calc_w()

    matrix = formating_matrix(w)
    print(matrix)


    for i in range(len(matrix), 0, -1):
        opr = det(matrix[:i, :i])
        print("det of", i, "matrix: ", opr)
        if opr < 0:
            print("ne ust")
            break

    a = 0

    inits = initPamsLab2.init_pams()

    for i in arange(1.3936, 1.3937, 0.00001):
        inits[1] = i

        w = initPamsLab2.finish_chain(inits)
        matrix = formating_matrix(w)
        opr = det(matrix[:len(matrix) - 1, :len(matrix) - 1])

        print("det", opr, "Koc:", i) if - 2 <= opr <= 2 else "no"

    listA = [inits[1] - 5, inits[1], inits[1] + 5]

    for i in range(3):
        initis = initPamsLab2.init_pams()
        initis[1] = listA[i]
        print(initis[1])
        w = initPamsLab2.finish_chain(initis)
        newToolBox.newToolBox.all_of_them(w)
        w1 = initPamsLab2.finish_for_nyquist(initis)
        NyquistBode.NyquistBode.all_of_them(w1)
def test_GridToMeshESMFRegridder_bilinear_round_trip(tmp_path):
    """Test save/load round tripping for `GridToMeshESMFRegridder`."""
    original_rg, src = _make_grid_to_mesh_regridder(method="bilinear")
    filename = tmp_path / "regridder.nc"
    save_regridder(original_rg, filename)
    loaded_rg = load_regridder(str(filename))

    assert original_rg.location == loaded_rg.location
    assert original_rg.method == loaded_rg.method
    assert original_rg.mdtol == loaded_rg.mdtol
    assert original_rg.grid_x == loaded_rg.grid_x
    assert original_rg.grid_y == loaded_rg.grid_y
    # TODO: uncomment when iris mesh comparison becomes available.
    # assert original_rg.mesh == loaded_rg.mesh

    # Compare the weight matrices.
    original_matrix = original_rg.regridder.weight_matrix
    loaded_matrix = loaded_rg.regridder.weight_matrix
    # Ensure the original and loaded weight matrix have identical type.
    assert type(original_matrix) is type(loaded_matrix)  # noqa E721
    assert np.array_equal(original_matrix.todense(), loaded_matrix.todense())

    # Demonstrate regridding still gives the same results.
    src_data = ma.arange(np.product(src.data.shape)).reshape(src.data.shape)
    src_data[0, 0] = ma.masked
    src.data = src_data
    # TODO: make this a cube comparison when mesh comparison becomes available.
    original_result = original_rg(src).data
    loaded_result = loaded_rg(src).data
    assert np.array_equal(original_result, loaded_result)
    assert np.array_equal(original_result.mask, loaded_result.mask)

    # Ensure version data is equal.
    assert original_rg.regridder.esmf_version == loaded_rg.regridder.esmf_version
    assert (original_rg.regridder.esmf_regrid_version ==
            loaded_rg.regridder.esmf_regrid_version)
Пример #49
0
    def get_godoghraph(self):
        w_den = tf(self.w.den[0][0], [1])

        print(w_den)

        array = []
        x = []
        y = []

        j = sqrt(-1)
        length_of_array = 2
        for q in arange(0, length_of_array, 0.01):
            array.append(w_den(q * j))

        for i in array:
            x.append(re(i))
            y.append(im(i))

        plt.plot(x, y, "r")
        plt.title('Godograph')
        plt.ylabel('jY')
        plt.xlabel('X')
        plt.grid(True)
        plt.show()
Пример #50
0
    def test_testPut(self):
        # Test of put
        with suppress_warnings() as sup:
            sup.filter(
                np.ma.core.MaskedArrayFutureWarning,
                "setting an item on a masked array which has a "
                "shared mask will not copy")
            d = arange(5)
            n = [0, 0, 0, 1, 1]
            m = make_mask(n)
            x = array(d, mask=m)
            self.assertTrue(x[3] is masked)
            self.assertTrue(x[4] is masked)
            x[[1, 4]] = [10, 40]
            self.assertTrue(x.mask is not m)
            self.assertTrue(x[3] is masked)
            self.assertTrue(x[4] is not masked)
            self.assertTrue(eq(x, [0, 10, 2, -1, 40]))

            x = array(d, mask=m)
            x.put([0, 1, 2], [-1, 100, 200])
            self.assertTrue(eq(x, [-1, 100, 200, 0, 0]))
            self.assertTrue(x[3] is masked)
            self.assertTrue(x[4] is masked)
Пример #51
0
    def test_trim(self):
        a = ma.arange(10)
        assert_equal(mstats.trim(a), [0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
        a = ma.arange(10)
        assert_equal(mstats.trim(a, (2, 8)),
                     [None, None, 2, 3, 4, 5, 6, 7, 8, None])
        a = ma.arange(10)
        assert_equal(mstats.trim(a, limits=(2, 8), inclusive=(False, False)),
                     [None, None, None, 3, 4, 5, 6, 7, None, None])
        a = ma.arange(10)
        assert_equal(mstats.trim(a, limits=(0.1, 0.2), relative=True),
                     [None, 1, 2, 3, 4, 5, 6, 7, None, None])

        a = ma.arange(12)
        a[[0, -1]] = a[5] = masked
        assert_equal(mstats.trim(a, (2, 8)),
                     [None, None, 2, 3, 4, None, 6, 7, 8, None, None, None])

        x = ma.arange(100).reshape(10, 10)
        expected = [1] * 10 + [0] * 70 + [1] * 20
        trimx = mstats.trim(x, (0.1, 0.2), relative=True, axis=None)
        assert_equal(trimx._mask.ravel(), expected)
        trimx = mstats.trim(x, (0.1, 0.2), relative=True, axis=0)
        assert_equal(trimx._mask.ravel(), expected)
        trimx = mstats.trim(x, (0.1, 0.2), relative=True, axis=-1)
        assert_equal(trimx._mask.T.ravel(), expected)

        # same as above, but with an extra masked row inserted
        x = ma.arange(110).reshape(11, 10)
        x[1] = masked
        expected = [1] * 20 + [0] * 70 + [1] * 20
        trimx = mstats.trim(x, (0.1, 0.2), relative=True, axis=None)
        assert_equal(trimx._mask.ravel(), expected)
        trimx = mstats.trim(x, (0.1, 0.2), relative=True, axis=0)
        assert_equal(trimx._mask.ravel(), expected)
        trimx = mstats.trim(x.T, (0.1, 0.2), relative=True, axis=-1)
        assert_equal(trimx.T._mask.ravel(), expected)
Пример #52
0
import numpy.ma as ma

a = ma.arange(6).reshape((2, 3))
a[1, :] = ma.masked
a
a.count()
a.count(axis=0)
a.count(axis=1)
Пример #53
0
    def test_testOddFeatures(self):
        # Test of other odd features
        x = arange(20)
        x = x.reshape(4, 5)
        x.flat[5] = 12
        assert_(x[1, 0] == 12)
        z = x + 10j * x
        assert_(eq(z.real, x))
        assert_(eq(z.imag, 10 * x))
        assert_(eq((z * conjugate(z)).real, 101 * x * x))
        z.imag[...] = 0.0

        x = arange(10)
        x[3] = masked
        assert_(str(x[3]) == str(masked))
        c = x >= 8
        assert_(count(where(c, masked, masked)) == 0)
        assert_(shape(where(c, masked, masked)) == c.shape)
        z = where(c, x, masked)
        assert_(z.dtype is x.dtype)
        assert_(z[3] is masked)
        assert_(z[4] is masked)
        assert_(z[7] is masked)
        assert_(z[8] is not masked)
        assert_(z[9] is not masked)
        assert_(eq(x, z))
        z = where(c, masked, x)
        assert_(z.dtype is x.dtype)
        assert_(z[3] is masked)
        assert_(z[4] is not masked)
        assert_(z[7] is not masked)
        assert_(z[8] is masked)
        assert_(z[9] is masked)
        z = masked_where(c, x)
        assert_(z.dtype is x.dtype)
        assert_(z[3] is masked)
        assert_(z[4] is not masked)
        assert_(z[7] is not masked)
        assert_(z[8] is masked)
        assert_(z[9] is masked)
        assert_(eq(x, z))
        x = array([1., 2., 3., 4., 5.])
        c = array([1, 1, 1, 0, 0])
        x[2] = masked
        z = where(c, x, -x)
        assert_(eq(z, [1., 2., 0., -4., -5]))
        c[0] = masked
        z = where(c, x, -x)
        assert_(eq(z, [1., 2., 0., -4., -5]))
        assert_(z[0] is masked)
        assert_(z[1] is not masked)
        assert_(z[2] is masked)
        assert_(eq(masked_where(greater(x, 2), x), masked_greater(x, 2)))
        assert_(eq(masked_where(greater_equal(x, 2), x),
                   masked_greater_equal(x, 2)))
        assert_(eq(masked_where(less(x, 2), x), masked_less(x, 2)))
        assert_(eq(masked_where(less_equal(x, 2), x), masked_less_equal(x, 2)))
        assert_(eq(masked_where(not_equal(x, 2), x), masked_not_equal(x, 2)))
        assert_(eq(masked_where(equal(x, 2), x), masked_equal(x, 2)))
        assert_(eq(masked_where(not_equal(x, 2), x), masked_not_equal(x, 2)))
        assert_(eq(masked_inside(list(range(5)), 1, 3), [0, 199, 199, 199, 4]))
        assert_(eq(masked_outside(list(range(5)), 1, 3), [199, 1, 2, 3, 199]))
        assert_(eq(masked_inside(array(list(range(5)),
                                       mask=[1, 0, 0, 0, 0]), 1, 3).mask,
                   [1, 1, 1, 1, 0]))
        assert_(eq(masked_outside(array(list(range(5)),
                                        mask=[0, 1, 0, 0, 0]), 1, 3).mask,
                   [1, 1, 0, 0, 1]))
        assert_(eq(masked_equal(array(list(range(5)),
                                      mask=[1, 0, 0, 0, 0]), 2).mask,
                   [1, 0, 1, 0, 0]))
        assert_(eq(masked_not_equal(array([2, 2, 1, 2, 1],
                                          mask=[1, 0, 0, 0, 0]), 2).mask,
                   [1, 0, 1, 0, 1]))
        assert_(eq(masked_where([1, 1, 0, 0, 0], [1, 2, 3, 4, 5]),
                   [99, 99, 3, 4, 5]))
        atest = ones((10, 10, 10), dtype=np.float32)
        btest = zeros(atest.shape, MaskType)
        ctest = masked_where(btest, atest)
        assert_(eq(atest, ctest))
        z = choose(c, (-x, x))
        assert_(eq(z, [1., 2., 0., -4., -5]))
        assert_(z[0] is masked)
        assert_(z[1] is not masked)
        assert_(z[2] is masked)
        x = arange(6)
        x[5] = masked
        y = arange(6) * 10
        y[2] = masked
        c = array([1, 1, 1, 0, 0, 0], mask=[1, 0, 0, 0, 0, 0])
        cm = c.filled(1)
        z = where(c, x, y)
        zm = where(cm, x, y)
        assert_(eq(z, zm))
        assert_(getmask(zm) is nomask)
        assert_(eq(zm, [0, 1, 2, 30, 40, 50]))
        z = where(c, masked, 1)
        assert_(eq(z, [99, 99, 99, 1, 1, 1]))
        z = where(c, 1, masked)
        assert_(eq(z, [99, 1, 1, 99, 99, 99]))
Пример #54
0
    def plot(self, **kwargs) -> Figure:
        """
        Plot a grid of the different components of the Compound Distribution.

        :param kwargs: kwargs for plot methods
        """
        ppf_prior_01 = self.prior().ppf().at(0.01)
        ppf_prior_99 = self.prior().ppf().at(0.99)
        ppf_posterior_01 = self.posterior().ppf().at(0.99)
        ppf_posterior_99 = self.posterior().ppf().at(0.99)
        x_norm_min = int(min(ppf_prior_01, ppf_posterior_01)) - 1
        x_norm_max = int(max(ppf_prior_99, ppf_posterior_99)) + 1
        x_param = arange(x_norm_min, x_norm_max + 0.001, 0.001)
        ff = FigureFormatter(n_rows=2, n_cols=3)
        (ax_prior, ax_data, ax_posterior, ax_prior_predictive, ax_likelihood,
         ax_posterior_predictive) = ff.axes.flat
        # plot prior and posterior parameters
        self.prior().plot(x=x_param, ax=ax_prior.axes, **kwargs)
        self.posterior().plot(x=x_param, ax=ax_posterior.axes, **kwargs)
        y_max_params = max(ax_prior.get_y_max(), ax_posterior.get_y_max())
        ax_prior.set_y_lim(0, y_max_params)
        ax_posterior.set_y_lim(0, y_max_params)
        ax_prior.set_title_text('prior').add_legend()
        ax_posterior.set_title_text('posterior').add_legend()
        # plot prior and posterior predictives
        ppf_prior_pred_01 = self.prior_predictive().ppf().at(0.01)
        ppf_prior_pred_99 = self.prior_predictive().ppf().at(0.99)
        ppf_posterior_pred_01 = self.posterior_predictive().ppf().at(0.01)
        ppf_posterior_pred_99 = self.posterior_predictive().ppf().at(0.99)
        x_pred_min = int(min(ppf_prior_pred_01, ppf_posterior_pred_01)) - 1
        x_pred_max = int(max(ppf_prior_pred_99, ppf_posterior_pred_99)) + 1
        x_pred = arange(x_pred_min, x_pred_max + 0.001, 0.001)
        self.prior_predictive().plot(x=x_pred,
                                     kind='line',
                                     ax=ax_prior_predictive.axes,
                                     **kwargs)
        self.posterior_predictive().plot(x=x_pred,
                                         kind='line',
                                         ax=ax_posterior_predictive.axes,
                                         **kwargs)
        y_max_pred = max(ax_prior_predictive.get_y_max(),
                         ax_posterior_predictive.get_y_max())
        ax_prior_predictive.set_y_lim(0, y_max_pred)
        ax_posterior_predictive.set_y_lim(0, y_max_pred)
        ax_prior_predictive.set_title_text('prior predictive').add_legend()
        ax_posterior_predictive.set_title_text(
            'posterior predictive').add_legend()
        # plot data
        observations = self.likelihood().rvs(num_samples=self._n)
        observations.plot.bar(ax=ax_data.axes, **kwargs)
        ax_data.set_text(title='data', x_label='i', y_label='$X_i$')
        y_abs_max = max([abs(y_lim) for y_lim in ax_data.get_y_lim()])
        ax_data.set_y_lim(-y_abs_max, y_abs_max)
        # plot likelihood
        x_like_min = int(self.likelihood().ppf().at(0.01)) - 1
        x_like_max = int(self.likelihood().ppf().at(0.99)) + 1
        x_exponential = arange(x_like_min, x_like_max + 0.001, 0.001)
        self.likelihood().plot(x=x_exponential, ax=ax_likelihood.axes)
        ax_likelihood.set_title_text('likelihood')
        ax_likelihood.add_legend()
        return ff.figure
Пример #55
0
    def test_testInplace(self):
        # Test of inplace operations and rich comparisons
        y = arange(10)

        x = arange(10)
        xm = arange(10)
        xm[2] = masked
        x += 1
        assert_(eq(x, y + 1))
        xm += 1
        assert_(eq(x, y + 1))

        x = arange(10)
        xm = arange(10)
        xm[2] = masked
        x -= 1
        assert_(eq(x, y - 1))
        xm -= 1
        assert_(eq(xm, y - 1))

        x = arange(10) * 1.0
        xm = arange(10) * 1.0
        xm[2] = masked
        x *= 2.0
        assert_(eq(x, y * 2))
        xm *= 2.0
        assert_(eq(xm, y * 2))

        x = arange(10) * 2
        xm = arange(10)
        xm[2] = masked
        x //= 2
        assert_(eq(x, y))
        xm //= 2
        assert_(eq(x, y))

        x = arange(10) * 1.0
        xm = arange(10) * 1.0
        xm[2] = masked
        x /= 2.0
        assert_(eq(x, y / 2.0))
        xm /= arange(10)
        assert_(eq(xm, ones((10,))))

        x = arange(10).astype(np.float32)
        xm = arange(10)
        xm[2] = masked
        x += 1.
        assert_(eq(x, y + 1.))
Пример #56
0
 def test_multi(self):
     for dtype in [np.int, np.float]:
         data = ma.arange(12, dtype=dtype)
         data[::2] = ma.masked
         self._check(data.reshape(3, 4))
Пример #57
0
 def test_flat(self):
     for dtype in [np.int]:
         data = ma.arange(12, dtype=dtype)
         data[::2] = ma.masked
         self._check(data)
Пример #58
0
 def test_no_mask_multi(self):
     for dtype in [np.int, np.float]:
         data = ma.arange(12, dtype=dtype).reshape(3, 4)
         self._check(data)
Пример #59
0
 def test_no_mask_flat(self):
     for dtype in [np.int, np.float]:
         data = ma.arange(12, dtype=dtype)
         self._check(data)
Пример #60
0
import numpy.ma as ma

a = ma.arange(3)
a[1] = ma.masked
b = ma.arange(2, 5)
a
b
ma.concatenate([a, b])