Ejemplo n.º 1
0
    def test_no_aliasing_2(self):
        # B and A take one another's values
        # no copying is necessary since each one is updated.
        orig_a = numpy.zeros((2,2))+.5
        orig_b = numpy.zeros((2,2))-.5
        A = self.shared(orig_a)
        B = self.shared(orig_b)
        C = tensor.dmatrix()

        z = numpy.zeros((2,2))

        data_of_a = data_of(A)
        data_of_b = data_of(B)

        f = pfunc([C], [], updates=[(A,B),(B,A)])
        f(z)
        # correctness
        assert numpy.all(data_of(A) == -.5)
        assert numpy.all(data_of(B) == +.5)

        # shared vars may not be aliased
        assert not numpy.may_share_memory(data_of(A), data_of(B))

        # theano should have been smart enough to not make copies
        assert numpy.may_share_memory(data_of(A), data_of_b)
        assert numpy.may_share_memory(data_of(B), data_of_a)
Ejemplo n.º 2
0
def test_nodes_at_link():
    """Test nodes_at_link shares data with tail and head."""
    assert_array_equal(rmg.nodes_at_link[:, 0], rmg.node_at_link_tail)
    assert_array_equal(rmg.nodes_at_link[:, 1], rmg.node_at_link_head)

    assert_true(np.may_share_memory(rmg.nodes_at_link, rmg.node_at_link_tail))
    assert_true(np.may_share_memory(rmg.nodes_at_link, rmg.node_at_link_head))
    def test_transpose(self):
        s0_transpose = self.s0.transpose()
        s0_diff = s0_transpose.differentials['s']
        assert s0_transpose.shape == (7, 6)
        assert s0_diff.shape == s0_transpose.shape
        assert np.all(s0_transpose.lon == self.s0.lon.transpose())
        assert np.all(s0_diff.d_lon == self.diff.d_lon.transpose())
        assert np.may_share_memory(s0_transpose.distance, self.s0.distance)
        assert np.may_share_memory(s0_diff.d_lon, self.diff.d_lon)

        s1_transpose = self.s1.transpose()
        s1_diff = s1_transpose.differentials['s']
        assert s1_transpose.shape == (7, 6)
        assert s1_diff.shape == s1_transpose.shape
        assert np.all(s1_transpose.lat == self.s1.lat.transpose())
        assert np.all(s1_diff.d_lon == self.diff.d_lon.transpose())
        assert np.may_share_memory(s1_transpose.lat, self.s1.lat)
        assert np.may_share_memory(s1_diff.d_lon, self.diff.d_lon)

        # Only one check on T, since it just calls transpose anyway.
        # Doing it on the CartesianRepresentation just for variety's sake.
        c0_T = self.c0.T
        assert c0_T.shape == (7, 6)
        assert np.all(c0_T.x == self.c0.x.T)
        assert np.may_share_memory(c0_T.y, self.c0.y)
def check_may_share_memory_easy_fuzz(get_max_work, same_steps, min_count):
    # Check that overlap problems with common strides are solved with
    # little work.
    x = np.zeros([17,34,71,97], dtype=np.int16)

    feasible = 0
    infeasible = 0

    pair_iter = iter_random_view_pairs(x, same_steps)

    while min(feasible, infeasible) < min_count:
        a, b = next(pair_iter)

        bounds_overlap = np.may_share_memory(a, b)
        may_share_answer = np.may_share_memory(a, b)
        easy_answer = np.may_share_memory(a, b, max_work=get_max_work(a, b))
        exact_answer = np.may_share_memory(a, b, max_work=MAY_SHARE_EXACT)

        if easy_answer != exact_answer:
            # assert_equal is slow...
            assert_equal(easy_answer, exact_answer)

        if may_share_answer != bounds_overlap:
            assert_equal(may_share_answer, bounds_overlap)

        if bounds_overlap:
            if exact_answer:
                feasible += 1
            else:
                infeasible += 1
def test_non_ndarray_inputs():
    # Regression check for gh-5604

    class MyArray(object):
        def __init__(self, data):
            self.data = data

        @property
        def __array_interface__(self):
            return self.data.__array_interface__

    class MyArray2(object):
        def __init__(self, data):
            self.data = data

        def __array__(self):
            return self.data

    for cls in [MyArray, MyArray2]:
        x = np.arange(5)

        assert_(np.may_share_memory(cls(x[::2]), x[1::2]))
        assert_(not np.shares_memory(cls(x[::2]), x[1::2]))

        assert_(np.shares_memory(cls(x[1::3]), x[::2]))
        assert_(np.may_share_memory(cls(x[1::3]), x[::2]))
Ejemplo n.º 6
0
    def test_dot_mm_partial(self, its=50):
        for it in range(its):
            size = random.choice([0, 1, randint(0, 10), randint(0, 100)])
            a_bm = gen_BandMat(size)
            b_bm = gen_BandMat(size)
            l = random.choice([0, 1, randint(0, 10)])
            u = random.choice([0, 1, randint(0, 10)])
            diag = None if rand_bool() else randn(size)
            diag_value = np.ones((size,)) if diag is None else diag
            a_full = a_bm.full()
            b_full = b_bm.full()

            c_bm = bm.dot_mm_partial(l, u, a_bm, b_bm, diag=diag)
            c_full = fl.band_ec(
                l, u,
                np.dot(np.dot(a_full, np.diag(diag_value)), b_full)
            )
            assert c_bm.l == l
            assert c_bm.u == u
            assert c_bm.size == size
            assert_allclose(c_bm.full(), c_full)
            assert not np.may_share_memory(c_bm.data, a_bm.data)
            assert not np.may_share_memory(c_bm.data, b_bm.data)
            if diag is not None:
                assert not np.may_share_memory(c_bm.data, diag)
Ejemplo n.º 7
0
    def test_BandMat_sub(self, its=100):
        for it in range(its):
            size = random.choice([0, 1, randint(0, 10), randint(0, 100)])
            a_bm = gen_BandMat(size)
            b_bm = gen_BandMat(size)
            a_full = a_bm.full()
            b_full = b_bm.full()

            c_bm = a_bm - b_bm
            c_full = a_full - b_full
            assert_allclose(c_bm.full(), c_full)
            assert not np.may_share_memory(c_bm.data, a_bm.data)
            assert not np.may_share_memory(c_bm.data, b_bm.data)

            c_bm = a_bm - 0
            c_full = a_full - 0
            assert_allclose(c_bm.full(), c_full)
            assert not np.may_share_memory(c_bm.data, a_bm.data)

            c_bm = 0 - a_bm
            c_full = 0 - a_full
            assert_allclose(c_bm.full(), c_full)
            assert not np.may_share_memory(c_bm.data, a_bm.data)

            with self.assertRaises(TypeError):
                a_bm - 1.0
            with self.assertRaises(TypeError):
                1.0 - a_bm
Ejemplo n.º 8
0
def test_no_copy():
    c1 = SkyCoord(np.arange(10.) * u.hourangle, np.arange(20., 30.) * u.deg)
    c2 = SkyCoord(c1, copy=False)
    # Note: c1.ra and c2.ra will *not* share memory, as these are recalculated
    # to be in "preferred" units.  See discussion in #4883.
    assert np.may_share_memory(c1.data.lon, c2.data.lon)
    c3 = SkyCoord(c1, copy=True)
    assert not np.may_share_memory(c1.data.lon, c3.data.lon)
Ejemplo n.º 9
0
def test_nodes_at_link():
    """Test nodes_at_link shares data with tail and head."""
    rmg = RasterModelGrid((4, 5), spacing=1.)
    assert_array_equal(rmg.nodes_at_link[:, 0], rmg.node_at_link_tail)
    assert_array_equal(rmg.nodes_at_link[:, 1], rmg.node_at_link_head)

    assert np.may_share_memory(rmg.nodes_at_link, rmg.node_at_link_tail)
    assert np.may_share_memory(rmg.nodes_at_link, rmg.node_at_link_head)
Ejemplo n.º 10
0
    def test__solve_triangular_banded(self, its=100):
        for it in range(its):
            size = random.choice([0, 1, randint(0, 10), randint(0, 100)])
            b = randn(size)
            chol_bm = gen_chol_factor_BandMat(size, transposed=False)
            chol_data = chol_bm.data
            depth = chol_bm.l + chol_bm.u
            lower = (chol_bm.u == 0)
            if size > 0 and rand_bool() and rand_bool():
                badFrame = randint(size)
                chol_data[0 if lower else depth, badFrame] = 0.0
            else:
                badFrame = None
            transposed = rand_bool()
            overwrite_b = rand_bool()
            chol_full = chol_bm.full()

            b_arg = b.copy()
            if badFrame is not None:
                msg = (
                    'singular matrix: resolution failed at diagonal %d' %
                    badFrame
                )
                msgRe = '^' + re.escape(msg) + '$'
                with self.assertRaisesRegexp(la.LinAlgError, msgRe):
                    bla._solve_triangular_banded(
                        chol_data, b_arg, transposed=transposed, lower=lower,
                        overwrite_b=overwrite_b
                    )
                with self.assertRaisesRegexp(la.LinAlgError, msgRe):
                    sla.solve_triangular(
                        chol_full, b, trans=transposed, lower=lower
                    )
            else:
                x = bla._solve_triangular_banded(
                    chol_data, b_arg, transposed=transposed, lower=lower,
                    overwrite_b=overwrite_b
                )
                if transposed:
                    assert_allclose(bm.dot_mv(chol_bm.T, x), b)
                else:
                    assert_allclose(bm.dot_mv(chol_bm, x), b)
                if size == 0:
                    x_good = np.zeros((size,))
                else:
                    x_good = sla.solve_triangular(
                        chol_full, b, trans=transposed, lower=lower
                    )
                assert_allclose(x, x_good)
                assert not np.may_share_memory(x, chol_data)
                if size > 0:
                    self.assertEquals(
                        np.may_share_memory(x, b_arg),
                        overwrite_b
                    )

            if not overwrite_b:
                assert np.all(b_arg == b)
Ejemplo n.º 11
0
def test_game_function(players, strategies):
    game = gamegen.role_symmetric_game(players, strategies)

    # Test copy
    game2 = rsgame.Game(game)
    assert not np.may_share_memory(game.profiles, game2.profiles)
    assert not np.may_share_memory(game.payoffs, game2.payoffs)
    assert np.all(game.profiles == game2.profiles)
    assert np.all(game.payoffs == game2.payoffs)

    game3 = rsgame.Game(rsgame.BaseGame(game))
    assert game3.is_empty()

    mask = game.profiles > 0

    # Check that min payoffs are actually minimum
    min_payoffs = game.role_repeat(game.min_payoffs())
    assert np.all((game.payoffs >= min_payoffs)[mask]), \
        "not all payoffs less than min payoffs"
    max_payoffs = game.role_repeat(game.max_payoffs())
    assert np.all((game.payoffs <= max_payoffs)[mask]), \
        "not all payoffs greater than max payoffs"

    # Test profile methods
    for prof in game.profiles:
        game.get_payoffs(prof)  # Works
        assert prof in game, "profile from game not in game"

    # Test expected payoff
    mix = game.random_mixtures()[0]

    dev1 = game.deviation_payoffs(mix)
    dev2, dev_jac = game.deviation_payoffs(mix, jacobian=True)
    assert not np.isnan(dev1).any()
    assert not np.isnan(dev_jac).any()
    assert np.allclose(dev1, dev2)

    pay1 = game.get_expected_payoffs(mix)
    pay2 = game.get_expected_payoffs(mix, deviations=dev1)
    pay3, jac1 = game.get_expected_payoffs(mix, jacobian=True)
    pay4, jac2 = game.get_expected_payoffs(
        mix, deviations=(dev1, dev_jac), jacobian=True)
    assert not np.isnan(pay1).any()
    assert (np.allclose(pay1, pay2) and np.allclose(pay1, pay3) and
            np.allclose(pay1, pay4))
    assert not np.isnan(jac1).any()
    assert np.allclose(jac1, jac2)

    # Max social welfare
    welfare, profile = game.get_max_social_welfare()
    assert not np.isnan(welfare)
    assert profile is not None
    for welfare, profile in zip(*game.get_max_social_welfare(True)):
        assert not np.isnan(welfare)
        assert profile is not None

    # Test that various methods can be called
    assert repr(game) is not None
Ejemplo n.º 12
0
def check_may_share_memory_easy_fuzz(get_max_work, same_steps, min_count):
    # Check that overlap problems with common strides are solved with
    # little work.
    x = np.zeros([17,34,71,97], dtype=np.int16)

    rng = np.random.RandomState(1234)

    def random_slice(n, step):
        start = rng.randint(0, n+1, dtype=np.intp)
        stop = rng.randint(start, n+1, dtype=np.intp)
        if rng.randint(0, 2, dtype=np.intp) == 0:
            stop, start = start, stop
            step *= -1
        return slice(start, stop, step)

    feasible = 0
    infeasible = 0

    while min(feasible, infeasible) < min_count:
        steps = tuple(rng.randint(1, 11, dtype=np.intp)
                      if rng.randint(0, 5, dtype=np.intp) == 0 else 1
                      for j in range(x.ndim))
        if same_steps:
            steps2 = steps
        else:
            steps2 = tuple(rng.randint(1, 11, dtype=np.intp)
                           if rng.randint(0, 5, dtype=np.intp) == 0 else 1
                           for j in range(x.ndim))

        t1 = np.arange(x.ndim)
        rng.shuffle(t1)

        t2 = np.arange(x.ndim)
        rng.shuffle(t2)

        s1 = tuple(random_slice(p, s) for p, s in zip(x.shape, steps))
        s2 = tuple(random_slice(p, s) for p, s in zip(x.shape, steps2))
        a = x[s1].transpose(t1)
        b = x[s2].transpose(t2)

        bounds_overlap = np.may_share_memory(a, b)
        may_share_answer = np.may_share_memory(a, b)
        easy_answer = np.may_share_memory(a, b, max_work=get_max_work(a, b))
        exact_answer = np.may_share_memory(a, b, max_work=MAY_SHARE_EXACT)

        if easy_answer != exact_answer:
            # assert_equal is slow...
            assert_equal(easy_answer, exact_answer, err_msg=repr((s1, s2)))

        if may_share_answer != bounds_overlap:
            assert_equal(may_share_answer, bounds_overlap,
                         err_msg=repr((s1, s2)))

        if bounds_overlap:
            if exact_answer:
                feasible += 1
            else:
                infeasible += 1
Ejemplo n.º 13
0
 def assertTableEqual(self, a, b, copy=None):
     assert a.colnames == b.colnames
     nptest.assert_array_equal(a.as_array(), b.as_array())
     assert a.meta == b.meta
     for col, col2 in zip(a.columns.values(), b.columns.values()):
         if copy:
             assert not may_share_memory(col, col2)
         elif copy is False:
             assert may_share_memory(col, col2)
Ejemplo n.º 14
0
def test_nodes_at_link():
    """Test nodes_at_link shares data with tail and head."""
    grid = HexModelGrid(3, 2)

    assert_array_equal(grid.nodes_at_link[:, 0], grid.node_at_link_tail)
    assert_array_equal(grid.nodes_at_link[:, 1], grid.node_at_link_head)

    assert np.may_share_memory(grid.nodes_at_link, grid.node_at_link_tail)
    assert np.may_share_memory(grid.nodes_at_link, grid.node_at_link_head)
 def test_diagonal(self):
     s0_diagonal = self.s0.diagonal()
     s0_diff = s0_diagonal.differentials['s']
     assert s0_diagonal.shape == (6,)
     assert s0_diff.shape == s0_diagonal.shape
     assert np.all(s0_diagonal.lat == self.s0.lat.diagonal())
     assert np.all(s0_diff.d_lon == self.diff.d_lon.diagonal())
     assert np.may_share_memory(s0_diagonal.lat, self.s0.lat)
     assert np.may_share_memory(s0_diff.d_lon, self.diff.d_lon)
 def test_swapaxes(self):
     s1_swapaxes = self.s1.swapaxes(0, 1)
     s1_diff = s1_swapaxes.differentials['s']
     assert s1_swapaxes.shape == (7, 6)
     assert s1_diff.shape == s1_swapaxes.shape
     assert np.all(s1_swapaxes.lat == self.s1.lat.swapaxes(0, 1))
     assert np.all(s1_diff.d_lon == self.diff.d_lon.swapaxes(0, 1))
     assert np.may_share_memory(s1_swapaxes.lat, self.s1.lat)
     assert np.may_share_memory(s1_diff.d_lon, self.diff.d_lon)
Ejemplo n.º 17
0
def _assert_copies(t, t2, deep=True):
    assert t.colnames == t2.colnames
    np.testing.assert_array_equal(t._data, t2._data)
    assert t.meta == t2.meta

    if deep:
        assert not np.may_share_memory(t._data, t2._data)
    else:
        assert np.may_share_memory(t._data, t2._data)
Ejemplo n.º 18
0
    def test_shared_constructor_copies(self):
        # shared constructor makes copy
        # (rule #2)
        orig_a = numpy.zeros((2,2))
        A = self.shared(orig_a)
        assert not numpy.may_share_memory(orig_a, data_of(A))

        # rule #2 reading back from theano-managed memory
        assert not numpy.may_share_memory(A.get_value(borrow=False), data_of(A))
Ejemplo n.º 19
0
 def trim_and_avgerage(self, other):
     """It is assumed that at least two full MLSes are used and sent through the
     system we want to measure. By using m repeated sequences of MLSes we can throw
     away the first full sequence. This first part might be delayed because of
     latency and will contain the startup response of the system which we aren't
     interested in. We then have n=m-1 sequences. Remember that MLS is a repeated
     sequence and it doesn't matter that we have for example latency of for example
     one third of a sequence. This will show up as phase shift which can be
     compensated for.
     
     We now calculate the average of the n sequences. This gives us even more noise
     resistance.
     
     Returns a numpy array of length L.
     """
     # data is [[A_1],
     #          [A_2],
     #          [A_3],
     #          [B_1],
     #          [B_2],
     #          [B_3],
     #          [C_1],
     #          [C_2],
     #          [C_3]]
     
     # throw away first full sequence
     trimmed = other[self.L:]
     self._logger.debug("May share memory: %s" %np.may_share_memory(other, trimmed))
     #print(repr(trimmed))
     # data is [[B_1],
     #          [B_2],
     #          [B_3],
     #          [C_1],
     #          [C_2],
     #          [C_3]]
     
     repeats = len(trimmed)//self.L
     self._logger.debug("repeats (first discared): %i" %repeats)
     
     # reshape so we can average
     reshaped = trimmed.reshape((repeats, self.L))
     # data is [[B_1, B_2, B_3],
     #          [C_1, C_2, C_3]]
     
     self._logger.debug("May share memory: %s" %np.may_share_memory(other, reshaped))
     #print(repr(reshaped))
     
     # the averaging finally creates a new array that has it's own data
     average = np.average(reshaped, axis=0)
     
     average = np.expand_dims(average, axis=1) # up the rank to 2
     # data is [[X_1],
     #          [X_2],
     #          [X_3]]
     
     return average
Ejemplo n.º 20
0
    def test_copy(self):
        mlp0 = MultilayerPerceptron()
        mlp1 = mlp0.copy()

        assert mlp0.params == mlp1.params
        check_mlp_same_est(mlp0, mlp1)
        for l0, l1 in zip(mlp0.layers, mlp1.layers):
            assert not np.may_share_memory(l0.weights, l1.weights)
            assert not np.may_share_memory(l0.activations, l1.activations)
            assert l0.bias == l1.bias
Ejemplo n.º 21
0
def _assert_copies(t, t2, deep=True):
    assert t.colnames == t2.colnames
    np.testing.assert_array_equal(t.as_array(), t2.as_array())
    assert t.meta == t2.meta

    for col, col2 in zip(t.columns.values(), t2.columns.values()):
        if deep:
            assert not np.may_share_memory(col, col2)
        else:
            assert np.may_share_memory(col, col2)
 def test_flatten(self):
     s0_flatten = self.s0.flatten()
     assert s0_flatten.shape == (self.s0.size,)
     assert np.all(s0_flatten.lon == self.s0.lon.flatten())
     # Flatten always copies.
     assert not np.may_share_memory(s0_flatten.distance, self.s0.distance)
     s1_flatten = self.s1.flatten()
     assert s1_flatten.shape == (self.s1.size,)
     assert np.all(s1_flatten.lon == self.s1.lon.flatten())
     assert not np.may_share_memory(s1_flatten.lat, self.s1.lat)
    def test_copy(self):
        s0_copy = self.s0.copy()
        s0_copy_diff = s0_copy.differentials['s']
        assert s0_copy.shape == self.s0.shape
        assert np.all(s0_copy.lon == self.s0.lon)
        assert np.all(s0_copy.lat == self.s0.lat)

        # Check copy was made of internal data.
        assert not np.may_share_memory(s0_copy.distance, self.s0.distance)
        assert not np.may_share_memory(s0_copy_diff.d_lon, self.diff.d_lon)
    def test_broadcast_to(self):
        s0_broadcast = self.s0._apply(np_broadcast_to, (3, 6, 7), subok=True)
        assert type(s0_broadcast) is type(self.s0)
        assert s0_broadcast.shape == (3, 6, 7)
        assert np.all(s0_broadcast.lon == self.s0.lon)
        assert np.all(s0_broadcast.lat == self.s0.lat)
        assert np.all(s0_broadcast.distance == self.s0.distance)
        assert np.may_share_memory(s0_broadcast.lon, self.s0.lon)
        assert np.may_share_memory(s0_broadcast.lat, self.s0.lat)
        assert np.may_share_memory(s0_broadcast.distance, self.s0.distance)
        s1_broadcast = self.s1._apply(np_broadcast_to, shape=(3, 6, 7),
                                      subok=True)
        assert s1_broadcast.shape == (3, 6, 7)
        assert np.all(s1_broadcast.lat == self.s1.lat)
        assert np.all(s1_broadcast.lon == self.s1.lon)
        assert np.all(s1_broadcast.distance == self.s1.distance)
        assert s1_broadcast.distance.shape == (3, 6, 7)
        assert np.may_share_memory(s1_broadcast.lat, self.s1.lat)
        assert np.may_share_memory(s1_broadcast.lon, self.s1.lon)
        assert np.may_share_memory(s1_broadcast.distance, self.s1.distance)

        # A final test that "may_share_memory" equals "does_share_memory"
        # Do this on a copy, to keep self.s0 unchanged.
        sc = self.s0.copy()
        assert not np.may_share_memory(sc.lon, self.s0.lon)
        assert not np.may_share_memory(sc.lat, self.s0.lat)
        sc_broadcast = sc._apply(np_broadcast_to, (3, 6, 7), subok=True)
        assert np.may_share_memory(sc_broadcast.lon, sc.lon)
        # Can only write to copy, not to broadcast version.
        sc.lon[0, 0] = 22. * u.hourangle
        assert np.all(sc_broadcast.lon[:, 0, 0] == 22. * u.hourangle)
Ejemplo n.º 25
0
 def test_build_spec_table_not_copy(self):
     """build_spec_table should not copy tables if not neccessary"""
     xs = np.arange(3)
     # float32 table will force a copy because Orange X is float64
     X = np.ones([4, 3], dtype=np.float32)
     data = build_spec_table(xs, X)
     self.assertFalse(np.may_share_memory(data.X, X))
     # float64 will not force a copy
     X = np.ones([4, 3], dtype=np.float64)
     data = build_spec_table(xs, X)
     self.assertTrue(np.may_share_memory(data.X, X))
 def test_ravel(self):
     s0_ravel = self.s0.ravel()
     assert s0_ravel.shape == (self.s0.size,)
     assert np.all(s0_ravel.lon == self.s0.lon.ravel())
     assert np.may_share_memory(s0_ravel.lon, self.s0.lon)
     assert np.may_share_memory(s0_ravel.lat, self.s0.lat)
     assert np.may_share_memory(s0_ravel.distance, self.s0.distance)
     # Since s1 was broadcast, ravel needs to make a copy.
     s1_ravel = self.s1.ravel()
     assert s1_ravel.shape == (self.s1.size,)
     assert np.all(s1_ravel.lon == self.s1.lon.ravel())
     assert not np.may_share_memory(s1_ravel.lat, self.s1.lat)
Ejemplo n.º 27
0
def test_copy_crop():
    arr = np.arange(45).reshape(9, 5)
    out0 = crop(arr, 1, copy=True)
    assert out0.flags.c_contiguous
    out0[0, 0] = 100
    assert not np.any(arr == 100)
    assert not np.may_share_memory(arr, out0)

    out1 = crop(arr, 1)
    out1[0, 0] = 100
    assert arr[1, 1] == 100
    assert np.may_share_memory(arr, out1)
Ejemplo n.º 28
0
    def test_dot_mv(self, its=100):
        for it in range(its):
            size = random.choice([0, 1, randint(0, 10), randint(0, 100)])
            a_bm = gen_BandMat(size)
            b = randn(size)
            a_full = a_bm.full()

            c = bm.dot_mv(a_bm, b)
            c_good = np.dot(a_full, b)
            assert_allclose(c, c_good)
            assert not np.may_share_memory(c, a_bm.data)
            assert not np.may_share_memory(c, b)
Ejemplo n.º 29
0
    def test_getitem_general(self):
        """Test slicing on different types of fields."""

        ibounds = [True, False]
        ivalue = [True, False]
        ilevel = [True, False]
        itemporal = [True, False]
        irealization = [True, False]
        for ib, iv, il, it, ir in itertools.product(ibounds, ivalue, ilevel, itemporal, irealization):
            field = self.get_field(with_bounds=ib, with_value=iv, with_level=il, with_temporal=it, with_realization=ir)

            if il:
                self.assertEqual(field.shape[2], 2)
            else:
                self.assertEqual(field.shape[2], 1)

            # # try a bad slice
            with self.assertRaises(IndexError):
                field[0]

            # # now good slices

            # # if data is loaded prior to slicing then memory is shared
            field.spatial.geom.point.value
            field_slc = field[:, :, :, :, :]
            self.assertTrue(np.may_share_memory(field.spatial.grid.value, field_slc.spatial.grid.value))
            self.assertTrue(np.may_share_memory(field.spatial.geom.point.value, field_slc.spatial.geom.point.value))

            field_value = field.variables['tmax']._value
            field_slc_value = field_slc.variables['tmax']._value
            try:
                self.assertNumpyAll(field_value, field_slc_value)
            except AttributeError:
                # with no attached value to the field, the private value will be nones
                if iv is None:
                    self.assertIsNone(field_value)
                    self.assertIsNone(field_slc_value)

            if iv == True:
                self.assertTrue(np.may_share_memory(field_value, field_slc_value))
            else:
                self.assertEqual(field_slc_value, None)

            field_slc = field[0, 0, 0, 0, 0]
            self.assertEqual(field_slc.shape, (1, 1, 1, 1, 1))
            if iv:
                self.assertEqual(field_slc.variables['tmax'].value.shape, (1, 1, 1, 1, 1))
                self.assertNumpyAll(field_slc.variables['tmax'].value,
                                    np.ma.array(field.variables['tmax'].value[0, 0, 0, 0, 0]).reshape(1, 1, 1, 1, 1))
            else:
                self.assertEqual(field_slc.variables['tmax']._value, None)
                self.assertEqual(field_slc.variables['tmax']._value, field.variables['tmax']._value)
Ejemplo n.º 30
0
    def test_BandMat_various_divs(self, its=100):
        for it in range(its):
            size = random.choice([0, 1, randint(0, 10), randint(0, 100)])
            mult = randn()
            a_bm = gen_BandMat(size)
            a_full = a_bm.full()

            b_bm = a_bm // mult
            b_full = a_full // mult
            assert b_bm.l == a_bm.l
            assert b_bm.u == a_bm.u
            assert_allclose(b_bm.full(), b_full)
            assert not np.may_share_memory(b_bm.data, a_bm.data)

            b_bm = a_bm / mult
            b_full = a_full / mult
            assert b_bm.l == a_bm.l
            assert b_bm.u == a_bm.u
            assert_allclose(b_bm.full(), b_full)
            assert not np.may_share_memory(b_bm.data, a_bm.data)

            b_bm = a_bm.__floordiv__(mult)
            b_full = a_full.__floordiv__(mult)
            assert b_bm.l == a_bm.l
            assert b_bm.u == a_bm.u
            assert_allclose(b_bm.full(), b_full)
            assert not np.may_share_memory(b_bm.data, a_bm.data)

            # __div__ does not exist in python3
            if sys.version_info[0] < 3:
                b_bm = a_bm.__div__(mult)
                b_full = a_full.__div__(mult)
                assert b_bm.l == a_bm.l
                assert b_bm.u == a_bm.u
                assert_allclose(b_bm.full(), b_full)
                assert not np.may_share_memory(b_bm.data, a_bm.data)

            b_bm = a_bm.__truediv__(mult)
            b_full = a_full.__truediv__(mult)
            assert b_bm.l == a_bm.l
            assert b_bm.u == a_bm.u
            assert_allclose(b_bm.full(), b_full)
            assert not np.may_share_memory(b_bm.data, a_bm.data)

            with self.assertRaises(TypeError):
                a_bm // a_bm
            with self.assertRaises(TypeError):
                a_bm / a_bm
            with self.assertRaises(TypeError):
                1.0 // a_bm
            with self.assertRaises(TypeError):
                1.0 / a_bm
Ejemplo n.º 31
0
 def test_diagonal(self):
     s0_diagonal = self.s0.diagonal()
     assert s0_diagonal.shape == (6, )
     assert np.all(s0_diagonal.data.lat == self.s0.data.lat.diagonal())
     assert np.may_share_memory(s0_diagonal.data.lat, self.s0.data.lat)
Ejemplo n.º 32
0
 def test_reshape(self):
     s0_reshape = self.s0.reshape(2, 3, 7)
     assert s0_reshape.shape == (2, 3, 7)
     assert np.all(s0_reshape.data.lon == self.s0.data.lon.reshape(2, 3, 7))
     assert np.all(s0_reshape.data.lat == self.s0.data.lat.reshape(2, 3, 7))
     assert np.may_share_memory(s0_reshape.data.lon, self.s0.data.lon)
     assert np.may_share_memory(s0_reshape.data.lat, self.s0.data.lat)
     s1_reshape = self.s1.reshape(3, 2, 7)
     assert s1_reshape.shape == (3, 2, 7)
     assert np.all(s1_reshape.data.lat == self.s1.data.lat.reshape(3, 2, 7))
     assert np.may_share_memory(s1_reshape.data.lat, self.s1.data.lat)
     assert np.all(s1_reshape.obstime == self.s1.obstime.reshape(3, 2, 7))
     assert np.may_share_memory(s1_reshape.obstime.jd1, self.s1.obstime.jd1)
     assert np.all(s1_reshape.location == self.s1.location.reshape(3, 2, 7))
     assert np.may_share_memory(s1_reshape.location, self.s1.location)
     assert np.all(
         s1_reshape.temperature == self.s1.temperature.reshape(3, 2, 7))
     assert np.may_share_memory(s1_reshape.temperature, self.s1.temperature)
     assert s1_reshape.pressure == self.s1.pressure
     # For reshape(3, 14), copying is necessary for lon, lat, location, time
     s1_reshape2 = self.s1.reshape(3, 14)
     assert s1_reshape2.shape == (3, 14)
     assert np.all(s1_reshape2.data.lon == self.s1.data.lon.reshape(3, 14))
     assert not np.may_share_memory(s1_reshape2.data.lon, self.s1.data.lon)
     assert np.all(s1_reshape2.obstime == self.s1.obstime.reshape(3, 14))
     assert not np.may_share_memory(s1_reshape2.obstime.jd1,
                                    self.s1.obstime.jd1)
     assert np.all(s1_reshape2.location == self.s1.location.reshape(3, 14))
     assert not np.may_share_memory(s1_reshape2.location, self.s1.location)
     assert np.all(
         s1_reshape2.temperature == self.s1.temperature.reshape(3, 14))
     assert np.may_share_memory(s1_reshape2.temperature,
                                self.s1.temperature)
     assert s1_reshape2.pressure == self.s1.pressure
     s2_reshape = self.s2.reshape(3, 2, 7)
     assert s2_reshape.shape == (3, 2, 7)
     assert np.all(s2_reshape.data.lon == self.s2.data.lon.reshape(3, 2, 7))
     assert np.may_share_memory(s2_reshape.data.lat, self.s2.data.lat)
     assert np.all(s2_reshape.obstime == self.s2.obstime.reshape(3, 2, 7))
     assert np.may_share_memory(s2_reshape.obstime.jd1, self.s2.obstime.jd1)
     assert np.all(
         s2_reshape.obsgeoloc.xyz == self.s2.obsgeoloc.reshape(3, 2, 7).xyz)
     assert np.may_share_memory(s2_reshape.obsgeoloc.x, self.s2.obsgeoloc.x)
     s3_reshape = self.s3.reshape(3, 2, 7)
     assert s3_reshape.shape == (3, 2, 7)
     assert np.all(s3_reshape.obstime == self.s3.obstime.reshape(3, 2, 7))
     assert np.may_share_memory(s3_reshape.obstime.jd1, self.s3.obstime.jd1)
     assert np.all(
         s3_reshape.obsgeoloc.xyz == self.s3.obsgeoloc.reshape(3, 2, 7).xyz)
     assert np.may_share_memory(s3_reshape.obsgeoloc.x, self.s3.obsgeoloc.x)
     sc_reshape = self.sc.reshape(3, 2, 7)
     assert sc_reshape.shape == (3, 2, 7)
     assert np.all(sc_reshape.data.lon == self.sc.data.lon.reshape(3, 2, 7))
     assert np.may_share_memory(sc_reshape.data.lat, self.sc.data.lat)
     assert np.all(sc_reshape.obstime == self.sc.obstime.reshape(3, 2, 7))
     assert np.may_share_memory(sc_reshape.obstime.jd1, self.sc.obstime.jd1)
     assert np.all(
         sc_reshape.obsgeoloc.xyz == self.sc.obsgeoloc.reshape(3, 2, 7).xyz)
     assert np.may_share_memory(sc_reshape.obsgeoloc.x, self.sc.obsgeoloc.x)
     # For reshape(3, 14), the arrays all need to be copied.
     sc_reshape2 = self.sc.reshape(3, 14)
     assert sc_reshape2.shape == (3, 14)
     assert np.all(sc_reshape2.data.lon == self.sc.data.lon.reshape(3, 14))
     assert not np.may_share_memory(sc_reshape2.data.lat, self.sc.data.lat)
     assert np.all(sc_reshape2.obstime == self.sc.obstime.reshape(3, 14))
     assert not np.may_share_memory(sc_reshape2.obstime.jd1,
                                    self.sc.obstime.jd1)
     assert np.all(
         sc_reshape2.obsgeoloc.xyz == self.sc.obsgeoloc.reshape(3, 14).xyz)
     assert not np.may_share_memory(sc_reshape2.obsgeoloc.x,
                                    self.sc.obsgeoloc.x)
Ejemplo n.º 33
0
 def test_squeeze(self):
     s0_squeeze = self.s0.reshape(3, 1, 2, 1, 7).squeeze()
     assert s0_squeeze.shape == (3, 2, 7)
     assert np.all(s0_squeeze.data.lat == self.s0.data.lat.reshape(3, 2, 7))
     assert np.may_share_memory(s0_squeeze.data.lat, self.s0.data.lat)
Ejemplo n.º 34
0
 def test_broadcast_to(self):
     s1_broadcast = np.broadcast_to(self.s1, (20, 6, 7))
     assert s1_broadcast.shape == (20, 6, 7)
     assert np.all(s1_broadcast.data.lon == self.s1.data.lon[np.newaxis])
     assert np.may_share_memory(s1_broadcast.data.lat, self.s1.data.lat)
Ejemplo n.º 35
0
 def test_matrix_memory_sharing(self):
     assert_(np.may_share_memory(self.m, self.m.ravel()))
     assert_(not np.may_share_memory(self.m, self.m.flatten()))
Ejemplo n.º 36
0
    def test_potential_output_aliasing_induced_by_updates(self):

        A = self.shared(np.zeros((2, 2)))
        B = self.shared(np.zeros((2, 2)))
        C = np.zeros((2, 2))
        D = tensor.dmatrix()
        DD = D + 5

        f = pfunc([D], [], updates=[(A, D), (B, D)])
        f(C)

        assert not np.may_share_memory(data_of(A), data_of(B))
        f = pfunc([D], [], updates=[(A, D[:]), (B, D)])
        f(C)
        assert not np.may_share_memory(data_of(A), data_of(B))
        f = pfunc([D], [], updates=[(A, (D + 5)), (B, D[:])])
        f(C)
        assert not np.may_share_memory(data_of(A), data_of(B))

        f = pfunc([D], [], updates=[(A, (D + 5)), (B, D)])
        f(C)
        assert not np.may_share_memory(data_of(A), data_of(B))

        f = pfunc([D], DD, updates=[(A, DD[:1]), (B, DD)])
        R = f(C)
        assert not np.may_share_memory(data_of(A), data_of(B))
        assert not np.may_share_memory(R, data_of(B))
        assert not np.may_share_memory(R, data_of(A))

        f = pfunc([D], DD, updates=[(A, DD[:1]), (B, (DD[:1] * 2))])
        R = f(C)
        assert not np.may_share_memory(data_of(A), data_of(B))
        assert not np.may_share_memory(R, data_of(B))
        assert not np.may_share_memory(R, data_of(A))

        f = pfunc([D], (DD * 4),
                  updates=[(A, (DD[:1] * 3)), (B, (DD[:1] * 2))])
        R = f(C)
        assert not np.may_share_memory(data_of(A), data_of(B))
        assert not np.may_share_memory(R, data_of(B))
        assert not np.may_share_memory(R, data_of(A))

        f = pfunc([D], (DD * 4),
                  updates=[(A, (DD[:1] * 3)), (B, (DD[:1] * 3))])
        R = f(C)
        assert not np.may_share_memory(data_of(A), data_of(B))
        assert not np.may_share_memory(R, data_of(B))
        assert not np.may_share_memory(R, data_of(A))
Ejemplo n.º 37
0
def test_check_array_memmap(copy):
    X = np.ones((4, 4))
    with TempMemmap(X, mmap_mode='r') as X_memmap:
        X_checked = check_array(X_memmap, copy=copy)
        assert np.may_share_memory(X_memmap, X_checked) == (not copy)
        assert X_checked.flags['WRITEABLE'] == copy
Ejemplo n.º 38
0
 def test_ravel(self):
     s0_ravel = self.s0.ravel()
     assert s0_ravel.shape == (self.s0.size, )
     assert np.all(s0_ravel.data.lon == self.s0.data.lon.ravel())
     assert np.may_share_memory(s0_ravel.data.lon, self.s0.data.lon)
     assert np.may_share_memory(s0_ravel.data.lat, self.s0.data.lat)
     # Since s1 lon, lat were broadcast, ravel needs to make a copy.
     s1_ravel = self.s1.ravel()
     assert s1_ravel.shape == (self.s1.size, )
     assert np.all(s1_ravel.data.lon == self.s1.data.lon.ravel())
     assert not np.may_share_memory(s1_ravel.data.lat, self.s1.data.lat)
     assert np.all(s1_ravel.obstime == self.s1.obstime.ravel())
     assert not np.may_share_memory(s1_ravel.obstime.jd1,
                                    self.s1.obstime.jd1)
     assert np.all(s1_ravel.location == self.s1.location.ravel())
     assert not np.may_share_memory(s1_ravel.location, self.s1.location)
     assert np.all(s1_ravel.temperature == self.s1.temperature.ravel())
     assert np.may_share_memory(s1_ravel.temperature, self.s1.temperature)
     assert s1_ravel.pressure == self.s1.pressure
     s2_ravel = self.s2.ravel()
     assert s2_ravel.shape == (self.s2.size, )
     assert np.all(s2_ravel.data.lon == self.s2.data.lon.ravel())
     assert not np.may_share_memory(s2_ravel.data.lat, self.s2.data.lat)
     assert np.all(s2_ravel.obstime == self.s2.obstime.ravel())
     assert not np.may_share_memory(s2_ravel.obstime.jd1,
                                    self.s2.obstime.jd1)
     # CartesianRepresentation do not allow direct comparisons, as this is
     # too tricky to get right in the face of rounding issues.  Here, though,
     # it cannot be an issue, so we compare the xyz quantities.
     assert np.all(s2_ravel.obsgeoloc.xyz == self.s2.obsgeoloc.ravel().xyz)
     assert not np.may_share_memory(s2_ravel.obsgeoloc.x,
                                    self.s2.obsgeoloc.x)
     s3_ravel = self.s3.ravel()
     assert s3_ravel.shape == (42, )  # cannot use .size on frame w/o data.
     assert np.all(s3_ravel.obstime == self.s3.obstime.ravel())
     assert not np.may_share_memory(s3_ravel.obstime.jd1,
                                    self.s3.obstime.jd1)
     assert np.all(s3_ravel.obsgeoloc.xyz == self.s3.obsgeoloc.ravel().xyz)
     assert not np.may_share_memory(s3_ravel.obsgeoloc.x,
                                    self.s3.obsgeoloc.x)
     sc_ravel = self.sc.ravel()
     assert sc_ravel.shape == (self.sc.size, )
     assert np.all(sc_ravel.data.lon == self.sc.data.lon.ravel())
     assert not np.may_share_memory(sc_ravel.data.lat, self.sc.data.lat)
     assert np.all(sc_ravel.obstime == self.sc.obstime.ravel())
     assert not np.may_share_memory(sc_ravel.obstime.jd1,
                                    self.sc.obstime.jd1)
     assert np.all(sc_ravel.obsgeoloc.xyz == self.sc.obsgeoloc.ravel().xyz)
     assert not np.may_share_memory(sc_ravel.obsgeoloc.x,
                                    self.sc.obsgeoloc.x)
Ejemplo n.º 39
0
 def test_getitem0101(self):
     # We on purpose take a slice with only one element, as for the
     # general tests it doesn't matter, but it allows us to check
     # for a few cases that shapes correctly become scalar if we
     # index our size-1 array down to a scalar.  See gh-10113.
     item = (slice(0, 1), slice(0, 1))
     s0_0101 = self.s0[item]
     assert s0_0101.shape == (1, 1)
     assert_array_equal(s0_0101.data.lon, self.s0.data.lon[item])
     assert np.may_share_memory(s0_0101.data.lon, self.s0.data.lon)
     assert np.may_share_memory(s0_0101.data.lat, self.s0.data.lat)
     s0_0101_00 = s0_0101[0, 0]
     assert s0_0101_00.shape == ()
     assert s0_0101_00.data.lon.shape == ()
     assert_array_equal(s0_0101_00.data.lon, self.s0.data.lon[0, 0])
     s1_0101 = self.s1[item]
     assert s1_0101.shape == (1, 1)
     assert_array_equal(s1_0101.data.lon, self.s1.data.lon[item])
     assert np.may_share_memory(s1_0101.data.lat, self.s1.data.lat)
     assert np.all(s1_0101.obstime == self.s1.obstime[item])
     assert np.may_share_memory(s1_0101.obstime.jd1, self.s1.obstime.jd1)
     assert_array_equal(s1_0101.location, self.s1.location[0, 0])
     assert np.may_share_memory(s1_0101.location, self.s1.location)
     assert_array_equal(s1_0101.temperature, self.s1.temperature[item])
     assert np.may_share_memory(s1_0101.temperature, self.s1.temperature)
     # scalar should just be transferred.
     assert s1_0101.pressure is self.s1.pressure
     s1_0101_00 = s1_0101[0, 0]
     assert s1_0101_00.shape == ()
     assert s1_0101_00.obstime.shape == ()
     assert s1_0101_00.obstime == self.s1.obstime[0, 0]
     s2_0101 = self.s2[item]
     assert s2_0101.shape == (1, 1)
     assert np.all(s2_0101.data.lon == self.s2.data.lon[item])
     assert np.may_share_memory(s2_0101.data.lat, self.s2.data.lat)
     assert np.all(s2_0101.obstime == self.s2.obstime[item])
     assert np.may_share_memory(s2_0101.obstime.jd1, self.s2.obstime.jd1)
     assert_array_equal(s2_0101.obsgeoloc.xyz, self.s2.obsgeoloc[item].xyz)
     s3_0101 = self.s3[item]
     assert s3_0101.shape == (1, 1)
     assert s3_0101.obstime.shape == (1, 1)
     assert np.all(s3_0101.obstime == self.s3.obstime[item])
     assert np.may_share_memory(s3_0101.obstime.jd1, self.s3.obstime.jd1)
     assert_array_equal(s3_0101.obsgeoloc.xyz, self.s3.obsgeoloc[item].xyz)
     sc_0101 = self.sc[item]
     assert sc_0101.shape == (1, 1)
     assert_array_equal(sc_0101.data.lon, self.sc.data.lon[item])
     assert np.may_share_memory(sc_0101.data.lat, self.sc.data.lat)
     assert np.all(sc_0101.obstime == self.sc.obstime[item])
     assert np.may_share_memory(sc_0101.obstime.jd1, self.sc.obstime.jd1)
     assert_array_equal(sc_0101.obsgeoloc.xyz, self.sc.obsgeoloc[item].xyz)
Ejemplo n.º 40
0
 def test_array_memory_sharing(self):
     assert_(np.may_share_memory(self.a, self.a.ravel()))
     assert_(not np.may_share_memory(self.a, self.a.flatten()))
Ejemplo n.º 41
0
 def may_share_memory(a, b):
     # This is a method of TensorType, so both a and b should be ndarrays
     if isinstance(a, numpy.ndarray) and isinstance(b, numpy.ndarray):
         return numpy.may_share_memory(a, b)
     else:
         return False