All Projects → metagraph-dev → grblas

metagraph-dev / grblas

Licence: Apache-2.0 license
Python wrapper around GraphBLAS

Programming Languages

python
139335 projects - #7 most used programming language
Jupyter Notebook
11667 projects
shell
77523 projects

Projects that are alternatives of or similar to grblas

Networkx
Network Analysis in Python
Stars: ✭ 10,057 (+45613.64%)
Mutual labels:  graph-algorithms, graph-theory, complex-networks, graph-analysis
jgrapht
Master repository for the JGraphT project
Stars: ✭ 2,259 (+10168.18%)
Mutual labels:  graph-algorithms, graph-theory, graph-datastructures
kaliningraph
🕸️ Graphs, finite fields and discrete dynamical systems in Kotlin
Stars: ✭ 62 (+181.82%)
Mutual labels:  graph-algorithms, linear-algebra, graph-theory
graphs
Graph algorithms written in Go
Stars: ✭ 60 (+172.73%)
Mutual labels:  graph-algorithms, graph-theory
eigen
Owl's OCaml Interface to Eigen3 C++ Library
Stars: ✭ 30 (+36.36%)
Mutual labels:  linear-algebra, sparse-matrix
Grafatko
An app for creating and visualizing graphs and graph-related algorithms.
Stars: ✭ 22 (+0%)
Mutual labels:  graph-algorithms, graph-theory
D3graphtheory
💥 Interactive and colorful 🎨 graph theory tutorials made using d3.js ⚡️
Stars: ✭ 1,364 (+6100%)
Mutual labels:  graph-algorithms, graph-theory
lynxkite
The complete graph data science platform
Stars: ✭ 120 (+445.45%)
Mutual labels:  graph-algorithms, complex-networks
jsgraph
Deprecated: Use the @encapsule/arccore package that includes the graph library
Stars: ✭ 42 (+90.91%)
Mutual labels:  graph-algorithms, graph-theory
gardenia
GARDENIA: Graph Analytics Repository for Designing Efficient Next-generation Accelerators
Stars: ✭ 22 (+0%)
Mutual labels:  graph-algorithms, sparse-matrix
qdldl
A free LDL factorisation routine
Stars: ✭ 54 (+145.45%)
Mutual labels:  linear-algebra, sparse-data
Pygraphblas
GraphBLAS for Python
Stars: ✭ 252 (+1045.45%)
Mutual labels:  graph-algorithms, linear-algebra
Libgrape Lite
🍇 A C++ library for parallel graph processing 🍇
Stars: ✭ 169 (+668.18%)
Mutual labels:  graph-algorithms, graph-theory
rustgraphblas
rust-library to wrap GraphBLAS.h
Stars: ✭ 23 (+4.55%)
Mutual labels:  graph-algorithms, graphblas
Erdos
modular and modern graph-theory algorithms framework in Java
Stars: ✭ 104 (+372.73%)
Mutual labels:  graph-algorithms, graph-theory
brainGraph
Graph theory analysis of brain MRI data
Stars: ✭ 136 (+518.18%)
Mutual labels:  graph-theory, complex-networks
Differentia.js
No longer being supported or maintained. A Graph Theory & Data Structure Library for JavaScript.
Stars: ✭ 13 (-40.91%)
Mutual labels:  graph-algorithms, graph-theory
Modal logic
Final Year Masters Project: modal logic solver tableaux
Stars: ✭ 16 (-27.27%)
Mutual labels:  graph-algorithms, graph-theory
networkx-guide
We here are very big fans of NetworkX as a graph library and its comprehensive set of graph algorithms. For many though, working with NetworkX involves a steep learning curve. This guide is designed as an aid for beginners and experienced users to find specific tips and explore the world of complex networks.
Stars: ✭ 28 (+27.27%)
Mutual labels:  graph-algorithms, graph-theory
sympiler
Sympiler is a Code Generator for Transforming Sparse Matrix Codes
Stars: ✭ 32 (+45.45%)
Mutual labels:  linear-algebra, sparse-matrix

Python-graphblas

conda-forge pypi License Tests Docs Coverage Binder

Python wrapper around GraphBLAS

To install, conda install -c conda-forge python-graphblas or pip install python-graphblas. This will also install the SuiteSparse graphblas compiled C library.

Currently works with SuiteSparse:GraphBLAS, but the goal is to make it work with all implementations of the GraphBLAS spec.

The approach taken with this library is to follow the C-API specification as closely as possible while making improvements allowed with the Python syntax. Because the spec always passes in the output object to be written to, we follow the same, which is very different from the way Python normally operates. In fact, many who are familiar with other Python data libraries (numpy, pandas, etc) will find it strange to not create new objects for every call.

At the highest level, the goal is to separate output, mask, and accumulator on the left side of the assignment operator = and put the computation on the right side. Unfortunately, that approach doesn't always work very well with how Python handles assignment, so instead we (ab)use the left-shift << notation to give the same flavor of assignment. This opens up all kinds of nice possibilities.

This is an example of how the mapping works:

// C call
GrB_Matrix_mxm(M, mask, GrB_PLUS_INT64, GrB_MIN_PLUS_INT64, A, B, NULL)
# Python call
M(mask.V, accum=binary.plus) << A.mxm(B, semiring.min_plus)

The expression on the right A.mxm(B) creates a delayed object which does no computation. Once it is used in the << expression with M, the whole thing is translated into the equivalent GraphBLAS call.

Delayed objects also have a .new() method which can be used to force computation and return a new object. This is convenient and often appropriate, but will create many unnecessary objects if used in a loop. It also loses the ability to perform accumulation with existing results. For best performance, following the standard GraphBLAS approach of (1) creating the object outside the loop and (2) using the object repeatedly within each loop is a much better approach, even if it doesn't feel very Pythonic.

Descriptor flags are set on the appropriate elements to keep logic close to what it affects. Here is the same call with descriptor bits set. ttcsr indicates transpose the first and second matrices, complement the structure of the mask, and do a replacement on the output.

// C call
GrB_Matrix_mxm(M, mask, GrB_PLUS_INT64, GrB_MIN_PLUS_INT64, A, B, desc.ttcsr)
# Python call
M(~mask.S, accum=binary.plus, replace=True) << A.T.mxm(B.T, semiring.min_plus)

The objects receiving the flag operations (A.T, ~mask, etc) are also delayed objects. They hold on to the state but do no computation, allowing the correct descriptor bits to be set in a single GraphBLAS call.

If no mask or accumulator is used, the call looks like this:

M << A.mxm(B, semiring.min_plus)

The use of << to indicate updating is actually just syntactic sugar for a real .update() method. The above expression could be written as:

M.update(A.mxm(B, semiring.min_plus))

Operations

M(mask, accum) << A.mxm(B, semiring)        # mxm
w(mask, accum) << A.mxv(v, semiring)        # mxv
w(mask, accum) << v.vxm(B, semiring)        # vxm
M(mask, accum) << A.ewise_add(B, binaryop)  # eWiseAdd
M(mask, accum) << A.ewise_mult(B, binaryop) # eWiseMult
M(mask, accum) << A.kronecker(B, binaryop)  # kronecker
M(mask, accum) << A.T                       # transpose

Extract

M(mask, accum) << A[rows, cols]             # rows and cols are a list or a slice
w(mask, accum) << A[rows, col_index]        # extract column
w(mask, accum) << A[row_index, cols]        # extract row
s = A[row_index, col_index].value           # extract single element

Assign

M(mask, accum)[rows, cols] << A             # rows and cols are a list or a slice
M(mask, accum)[rows, col_index] << v        # assign column
M(mask, accum)[row_index, cols] << v        # assign row
M(mask, accum)[rows, cols] << s             # assign scalar to many elements
M[row_index, col_index] << s                # assign scalar to single element
                                            # (mask and accum not allowed)
del M[row_index, col_index]                 # remove single element

Apply

M(mask, accum) << A.apply(unaryop)
M(mask, accum) << A.apply(binaryop, left=s)   # bind-first
M(mask, accum) << A.apply(binaryop, right=s)  # bind-second

Reduce

v(mask, accum) << A.reduce_rowwise(op)      # reduce row-wise
v(mask, accum) << A.reduce_columnwise(op)   # reduce column-wise
s(accum) << A.reduce_scalar(op)
s(accum) << v.reduce(op)

Creating new Vectors / Matrices

A = Matrix.new(dtype, num_rows, num_cols)   # new_type
B = A.dup()                                 # dup
A = Matrix.from_values([row_indices], [col_indices], [values])  # build

New from delayed

Delayed objects can be used to create a new object using .new() method

C = A.mxm(B, semiring).new()

Properties

size = v.size                               # size
nrows = M.nrows                             # nrows
ncols = M.ncols                             # ncols
nvals = M.nvals                             # nvals
rindices, cindices, vals = M.to_values()    # extractTuples

Initialization

There is a mechanism to initialize graphblas with a context prior to use. This allows for setting the backend to use as well as the blocking/non-blocking mode. If the context is not initialized, a default initialization will be performed automatically.

import graphblas as gb
# Context initialization must happen before any other imports
gb.init('suitesparse', blocking=True)

# Now we can import other items from graphblas
from graphblas import binary, semiring
from graphblas import Matrix, Vector, Scalar

Performant User Defined Functions

Python-graphblas requires numba which enables compiling user-defined Python functions to native C for use in GraphBLAS.

Example customized UnaryOp:

from graphblas import unary
from graphblas.operator import UnaryOp

def force_odd_func(x):
    if x % 2 == 0:
        return x + 1
    return x

UnaryOp.register_new('force_odd', force_odd_func)

v = Vector.from_values([0, 1, 3], [1, 2, 3])
w = v.apply(unary.force_odd).new()
w  # indexes=[0, 1, 3], values=[1, 3, 3]

Similar methods exist for BinaryOp, Monoid, and Semiring.

Import/Export connectors to the Python ecosystem

graphblas.io contains functions for converting to and from:

import graphblas as gb

# numpy arrays
# 1-D array becomes Vector, 2-D array becomes Matrix
A = gb.io.from_numpy(m)
m = gb.io.to_numpy(A)

# scipy.sparse matrices
A = gb.io.from_scipy_sparse_matrix(m)
m = gb.io.to_scipy_sparse_matrix(m, format='csr')

# networkx graphs
A = gb.io.from_networkx(g)
g = gb.io.to_networkx(A)
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].