Skip to content

Option to return sparse arrays from sc.get.aggregate #2898

Description

@ivirshup

What kind of feature would you like to request?

Additional function parameters / changed functionality / changed defaults?

Please describe your wishes

@Intron7, found a use case 😆

It could be nice for sc.get.aggregate to be able to return sparse matrices where we don't expect the aggregation to return very dense data.

Previously discussed in:

Usecases include:

This would require both api design choices for what the argument is called, and efficient implementations for both dense and sparse results (python-graphblas could be useful here)

Activity

  1. Intron7 commented on Mar 6, 2024

    @Intron7
    Member

    @ivirshup Do we need the computation itself to be sparse, or do we require the result to be returned in a sparse format? If it's the latter, we could consider developing a decorator that transforms the output into the desired format, as is done in cuML.

  2. ivirshup commented on Mar 6, 2024

    @ivirshup
    MemberAuthor

    The computation should be sparse. Otherwise I'd be fine with the user doing it themselves, but the main value add here would be lower memory overhead/ optimized implementation.

  3. ivirshup commented on Mar 6, 2024

    @ivirshup
    MemberAuthor

    Here's a demo implementation using python-graphblas for max:
    Setup:

    from scipy import sparse
    import numpy as np
    
    N_OBS, N_VAR = 2_000, 10_000
    N_CLASSES = N_VAR - int(N_VAR / 1000)
    rng = np.random.default_rng(0)
    
    X = sparse.random(N_OBS, N_VAR, density=0.01, format="csr", random_state=rng)
    var_labels = np.concatenate([np.arange(N_CLASSES), rng.choice(N_CLASSES, size=N_VAR - N_CLASSES)])

    Implementation:

    import graphblas as gb
    from sklearn.preprocessing import label_binarize
    
    var_labels_mtx = label_binarize(var_labels, classes=np.arange(N_CLASSES), sparse_output=True)
    
    result = gb.io.to_scipy_sparse(
        gb.semiring.max_times(
            gb.io.from_scipy_sparse(X) @ gb.io.from_scipy_sparse(var_labels_mtx)
        ).new()
    )
    Test
    import numpy_groupies as npg
    
    npg_result = npg.aggregate(var_labels, X.toarray(), func="max", axis=1)
    
    np.testing.assert_array_equal(npg_result, result.toarray())
  4. added this to the 1.11.0 milestone on Mar 21, 2024
  5. modified the milestones: 1.11.0, 1.12.0 on Dec 20, 2024
  6. modified the milestones: 1.12.0, 1.13.0 on Nov 10, 2025
  7. modified the milestones: 1.13.0, 1.14.0 on Nov 13, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

Area – API

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions