Exemplo n.º 1
0
def test_invalid_shape_error():
    s = sparse.random((3, 4), density=0.5, format='coo')

    with pytest.raises(ValueError):
        sparse.as_coo(s, shape=(2, 3))

    with pytest.raises(ValueError):
        COO(s, shape=(2, 3))
Exemplo n.º 2
0
def test_invalid_attrs_error(s):
    with pytest.raises(ValueError):
        sparse.as_coo(s, shape=(2, 3))

    with pytest.raises(ValueError):
        COO(s, shape=(2, 3))

    with pytest.raises(ValueError):
        sparse.as_coo(s, fill_value=0.0)

    with pytest.raises(ValueError):
        COO(s, fill_value=0.0)
Exemplo n.º 3
0
def test_invalid_attrs_error():
    s = sparse.random((3, 4), density=0.5, format="coo")

    with pytest.raises(ValueError):
        sparse.as_coo(s, shape=(2, 3))

    with pytest.raises(ValueError):
        COO(s, shape=(2, 3))

    with pytest.raises(ValueError):
        sparse.as_coo(s, fill_value=0.0)

    with pytest.raises(ValueError):
        COO(s, fill_value=0.0)
Exemplo n.º 4
0
def as_pydata_coo(a, shape=None, fill_value=None):
    """Convert a to pydata coo array

    Examples:

    >>> import sparse
    >>> a = np.random.random((3, 4, 5))
    >>> a2 = as_pydata_coo(a)
    >>> isinstance(a2, sparse.COO)
    True
    >>> np.testing.assert_array_equal(a, a2.todense())

    >>> a2 = as_pydata_coo(sparse.as_coo(a))
    >>> isinstance(a2, sparse.COO)
    True
    >>> np.testing.assert_array_equal(a, a2.todense())

    >>> import tensorflow as tf
    >>> indices = [[0,0], [1,1]]
    >>> values = [1.0, 2.0]
    >>> a = tf.SparseTensorValue(indices=indices, values=values,
    ...                          dense_shape=(2,2))
    >>> a2 = as_pydata_coo(a)
    >>> isinstance(a2, sparse.COO)
    True
    >>> np.testing.assert_array_equal(a2.todense(),
    ...                               np.array([[1.0, 0], [0, 2.0]]))
    """
    _config.assert_has_package('sparse')
    import sparse
    if not (scipy.sparse.issparse(a) or isinstance(a, np.ndarray) or
            is_pydata_sparse(a)):
        a = as_scipy_coo(a)

    return sparse.as_coo(a, shape, fill_value)
Exemplo n.º 5
0
def test_as_coo(format):
    x = format(sparse.random((3, 4), density=0.5, format="coo").todense())

    s1 = sparse.as_coo(x)
    s2 = COO(x)

    assert_eq(x, s1)
    assert_eq(x, s2)
Exemplo n.º 6
0
 def test_empty(self):
     x = np.array([])
     assert_eq(np.roll(x, 1), sparse.roll(sparse.as_coo(x), 1))
Exemplo n.º 7
0
    K = A.shape[1] if X is None else X.shape[0]
    nC = Y.shape[1]
    W = None
    if args.weighted:
        W = utils.calc_class_weights(Y[..., idx_train, :])

    # ************************************************************
    # calculate node features
    # ************************************************************
    vals = []
    X = X.astype(np.float32)
    EYE = scipy.sparse.eye(K, dtype=np.float32, format='coo')
    A = A.astype(np.float32)

    # normalized x
    rowsum = sparse.as_coo(X).sum(axis=-1, keepdims=True)
    #rowsum.data[rowsum.data==0] = 1e-10
    rowsum.data = 1.0 / rowsum.data
    vals.append((sparse.as_coo(X) * rowsum).to_scipy_sparse())
    nodes = scipy.sparse.hstack(vals)
    nodes = nodes.toarray()

    # ************************************************************
    # calculate edge features
    # ************************************************************
    EYE = scipy.sparse.eye(K, dtype=np.float32, format='coo')

    curvs_used = ['both', 'ollivier', 'forman']
    for i, curv in enumerate(curvs_used):
        print("curv: ", curv)
        if curv == 'both':
Exemplo n.º 8
0
    W = None
    if args.weighted:
        W = utils.calc_class_weights(Y[..., idx_train, :])
    # ************************************************************
    # calculate edge features
    # ************************************************************
    EYE = scipy.sparse.eye(K, dtype=np.float32, format='coo')
    edge_feat_list = [ollivier_curv_vals, forman_curv_vals]

    vals = []
    for mat in edge_feat_list:
        vals.append((mat + mat.transpose() + EYE > 0).astype(np.float32))
        if args.encode_edge_direction:
            vals.append((mat + EYE > 0).astype(np.float32))
            vals.append((mat.transpose() + EYE > 0).astype(np.float32))
    vals = [sparse.as_coo(x) for x in vals]
    vals = sparse.stack(vals, axis=0)
    vals = vals.todense()

    vals = aml_graph.normalize_adj(vals,
                                   args.edge_norm,
                                   assume_symmetric_input=False)
    temp_vals = vals
    vals = [vals]

    ret = np.concatenate(vals, 1)

    edges = np.transpose(ret, [1, 2, 0])

    for j in range(args.num_trials):