Example #1
0
def test_large_concat_stack():
    data = np.array([1], dtype=np.uint8)
    coords = np.array([[255]], dtype=np.uint8)

    xs = COO(coords, data, shape=(256,), has_duplicates=False, sorted=True)
    x = xs.todense()

    assert_eq(np.stack([x, x]),
              sparse.stack([xs, xs]))

    assert_eq(np.concatenate((x, x)),
              sparse.concatenate((xs, xs)))
Example #2
0
def test_concatenate():
    xx = sparse.random((2, 3, 4), density=0.5)
    x = xx.todense()
    yy = sparse.random((5, 3, 4), density=0.5)
    y = yy.todense()
    zz = sparse.random((4, 3, 4), density=0.5)
    z = zz.todense()

    assert_eq(np.concatenate([x, y, z], axis=0),
              sparse.concatenate([xx, yy, zz], axis=0))

    xx = sparse.random((5, 3, 1), density=0.5)
    x = xx.todense()
    yy = sparse.random((5, 3, 3), density=0.5)
    y = yy.todense()
    zz = sparse.random((5, 3, 2), density=0.5)
    z = zz.todense()

    assert_eq(np.concatenate([x, y, z], axis=2),
              sparse.concatenate([xx, yy, zz], axis=2))

    assert_eq(np.concatenate([x, y, z], axis=-1),
              sparse.concatenate([xx, yy, zz], axis=-1))
Example #3
0
def test_concatenate():
    x = random_x((2, 3, 4))
    xx = COO.from_numpy(x)
    y = random_x((5, 3, 4))
    yy = COO.from_numpy(y)
    z = random_x((4, 3, 4))
    zz = COO.from_numpy(z)

    assert_eq(np.concatenate([x, y, z], axis=0),
              sparse.concatenate([xx, yy, zz], axis=0))

    x = random_x((5, 3, 1))
    xx = COO.from_numpy(x)
    y = random_x((5, 3, 3))
    yy = COO.from_numpy(y)
    z = random_x((5, 3, 2))
    zz = COO.from_numpy(z)

    assert_eq(np.concatenate([x, y, z], axis=2),
              sparse.concatenate([xx, yy, zz], axis=2))

    assert_eq(np.concatenate([x, y, z], axis=-1),
              sparse.concatenate([xx, yy, zz], axis=-1))
Example #4
0
def test_concatenate_noarrays():
    with pytest.raises(ValueError):
        sparse.concatenate([])
Example #5
0
    def test_inconsistent_fv(self):
        xs = sparse.random((3, 4), density=0.5, fill_value=1)
        ys = sparse.random((3, 4), density=0.5, fill_value=2)

        with pytest.raises(ValueError):
            sparse.concatenate([xs, ys])
Example #6
0
def concatenate(arrays, axis=0, array_mode=None, array_default_mode='numpy',
                array_homo_mode=None):
    """Join a sequence of arrays along an existing axis.

    Args:

      arrays (Seq[array]): Input arrays

      axis (int): The axis along which the arrays will be joined. If
        axis is None, arrays are flattened before use. Default is 0.

      array_mode (str, NoneType): See :func:`validate_array_args`.

      array_default_mode (str, NoneType): See :func:`validate_array_args`.

      array_homo_mode (str, NoneType): See :func:`validate_array_args`.

    Returns:

    The concatenated array.

    Examples:

    >>> a = np.random.random((3, 4))
    >>> b = np.random.random((3, 4))
    >>> c1 = np.concatenate((a,b))
    >>> c2 = concatenate((a,b))
    >>> isinstance(c2, np.ndarray)
    True
    >>> np.all(c1 == c2)
    True

    >>> c2 = concatenate((a,b), array_mode='scipy-sparse')
    >>> scipy.sparse.issparse(c2)
    True
    >>> c3 = concatenate((a,b), array_mode='pydata-sparse')
    >>> import sparse
    >>> isinstance(c3, sparse.SparseArray)
    True
    >>> np.all(c1 == c2.todense())
    True
    >>> np.all(c1 == c3.todense())
    True

    >>> c1 = np.concatenate((a,b), axis=1)
    >>> c2 = concatenate((a,b), array_mode='scipy-sparse', axis=1)
    >>> scipy.sparse.issparse(c2)
    True
    >>> np.all(c1 == c2.todense())
    True
    """
    arrays = _utils.validate_array_args(*arrays,
                                      mode=array_mode,
                                      default_mode=array_default_mode,
                                      homo_mode=array_homo_mode)
    if all(scipy.sparse.issparse(a) for a in arrays):
        axis = axis+2 if axis < 0 else axis
        if axis == 0:
            c = scipy.sparse.vstack(arrays)
        else:
            assert axis == 1
            c = scipy.sparse.hstack(arrays)
    elif (_config.has_package('sparse') and
          all(_utils.is_pydata_sparse(a) for a in arrays)):
        import sparse
        c = sparse.concatenate(arrays, axis)
    else:
        c = np.concatenate(arrays, axis)
    return c