Example #1
0
def test_relation_sparse_2d_dataview_digest():
    row = np.array([0, 3, 1, 0])
    col = np.array([0, 3, 1, 2])
    data = np.array([4, 5, 7, 9])
    m = coo_matrix((data, (row, col)), shape=(4, 4))
    view = sparse_2d_dataview(m)
    view1 = sparse_2d_dataview(m)
    assert_equals(_hexdigest(view), _hexdigest(view1))

    row1 = np.array([0, 3, 1, 0])
    col1 = np.array([0, 3, 1, 2])
    data1 = np.array([4, 5, 7, 1])
    m1 = coo_matrix((data1, (row1, col1)), shape=(4, 4))
    view = sparse_2d_dataview(m)
    view1 = sparse_2d_dataview(m1)
    assert_not_equals(_hexdigest(view), _hexdigest(view1))
Example #2
0
def test_relation_sparse_2d_dataview_digest():
    row = np.array([0, 3, 1, 0])
    col = np.array([0, 3, 1, 2])
    data = np.array([4, 5, 7, 9])
    m = coo_matrix((data, (row, col)), shape=(4, 4))
    view = sparse_2d_dataview(m)
    view1 = sparse_2d_dataview(m)
    assert_equals(_hexdigest(view), _hexdigest(view1))

    row1 = np.array([0, 3, 1, 0])
    col1 = np.array([0, 3, 1, 2])
    data1 = np.array([4, 5, 7, 1])
    m1 = coo_matrix((data1, (row1, col1)), shape=(4, 4))
    view = sparse_2d_dataview(m)
    view1 = sparse_2d_dataview(m1)
    assert_not_equals(_hexdigest(view), _hexdigest(view1))
Example #3
0
def test_relation_sparse_2d_dataview_build_from_csc():
    row = np.array([0, 3, 1, 0])
    col = np.array([0, 3, 1, 2])
    data = np.array([4, 5, 7, 9])
    m = coo_matrix((data, (row, col)), shape=(4, 4))
    csc = m.tocsc()
    assert isinstance(csc, csc_matrix)
    view = sparse_2d_dataview(csc)
    assert view.shape() == (4, 4)
Example #4
0
def test_relation_sparse_2d_dataview_build_from_csc():
    row = np.array([0, 3, 1, 0])
    col = np.array([0, 3, 1, 2])
    data = np.array([4, 5, 7, 9])
    m = coo_matrix((data, (row, col)), shape=(4, 4))
    csc = m.tocsc()
    assert isinstance(csc, csc_matrix)
    view = sparse_2d_dataview(csc)
    assert view.shape() == (4, 4)
Example #5
0
def test_relation_sparse_2d_dataview_pickle():
    def sparsify(y, fn=lambda x: x):
        inds = it.product(range(y.shape[0]), range(y.shape[1]))
        ijv = [(i, j, y[i, j]) for i, j in inds if fn(y[i, j])]
        args = (map(op.itemgetter(2),
                    ijv), (map(op.itemgetter(0),
                               ijv), map(op.itemgetter(1), ijv)))
        return coo_matrix(args, shape=y.shape)

    y = np.random.randint(-2, 2, size=(10, 10))
    view = sparse_2d_dataview(sparsify(y))
    bstr = pickle.dumps(view)
    view1 = pickle.loads(bstr)
    assert_equals((view.tocsr() != view1.tocsr()).nnz, 0)

    y = np.random.uniform(size=(4, 3))
    view = sparse_2d_dataview(sparsify(y, fn=lambda x: x >= 0.4 and x <= 0.6))
    bstr = pickle.dumps(view)
    view1 = pickle.loads(bstr)
    assert_almost_equals(np.abs(
        (view.tocsr() - view1.tocsr()).todense()).max(),
                         0.,
                         places=3)
Example #6
0
def test_relation_sparse_2d_dataview_pickle():

    def sparsify(y, fn=lambda x: x):
        inds = it.product(range(y.shape[0]), range(y.shape[1]))
        ijv = [(i, j, y[i, j]) for i, j in inds if fn(y[i, j])]
        args = (
            map(op.itemgetter(2), ijv),
            (map(op.itemgetter(0), ijv), map(op.itemgetter(1), ijv))
        )
        return coo_matrix(args, shape=y.shape)

    y = np.random.randint(-2, 2, size=(10, 10))
    view = sparse_2d_dataview(sparsify(y))
    bstr = pickle.dumps(view)
    view1 = pickle.loads(bstr)
    assert_equals((view.tocsr() != view1.tocsr()).nnz, 0)

    y = np.random.uniform(size=(4, 3))
    view = sparse_2d_dataview(sparsify(y, fn=lambda x: x >= 0.4 and x <= 0.6))
    bstr = pickle.dumps(view)
    view1 = pickle.loads(bstr)
    assert_almost_equals(
        np.abs((view.tocsr() - view1.tocsr()).todense()).max(), 0., places=3)