Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove from_graphblas; convert to class constructor #35

Merged
merged 1 commit into from
Nov 19, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion graphblas_algorithms/algorithms/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,4 @@ def k_truss(G: Graph, k) -> Graph:
# Convert back to networkx graph with correct node ids
keys = G.list_to_keys(indices)
key_to_id = dict(zip(keys, range(len(indices))))
return Graph.from_graphblas(Ktruss, key_to_id=key_to_id)
return Graph(Ktruss, key_to_id=key_to_id)
4 changes: 2 additions & 2 deletions graphblas_algorithms/algorithms/tests/test_cluster.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ def test_triangles_full():
G = gb.Matrix(bool, 5, 5)
G[:, :] = True
G2 = gb.select.offdiag(G).new()
G = Graph.from_graphblas(G)
G2 = Graph.from_graphblas(G2)
G = Graph(G)
G2 = Graph(G2)
result = cluster.triangles(G)
expected = gb.Vector(int, 5)
expected[:] = 6
Expand Down
26 changes: 2 additions & 24 deletions graphblas_algorithms/classes/_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
import numpy as np
from graphblas import Matrix, Vector, binary
from graphblas.core.matrix import TransposedMatrix
from graphblas.core.utils import ensure_type

################
# Classmethods #
Expand All @@ -19,21 +18,6 @@ def from_networkx(cls, G, weight=None, dtype=None):
return rv


def from_graphblas(cls, A, *, key_to_id=None):
# Does not copy if A is a Matrix!
A = ensure_type(A, Matrix)
if A.nrows != A.ncols:
raise ValueError(f"Adjacency matrix must be square; got {A.nrows} x {A.ncols}")
rv = cls()
# If there is no mapping, it may be nice to keep this as None
if key_to_id is None:
rv._key_to_id = {i: i for i in range(A.nrows)}
else:
rv._key_to_id = key_to_id
rv._A = A
return rv


##############
# Properties #
##############
Expand Down Expand Up @@ -144,23 +128,17 @@ def vector_to_nodemap(self, v, *, mask=None, fillvalue=None):
elif fillvalue is not None and v.nvals < v.size:
v(mask=~v.S) << fillvalue

rv = object.__new__(NodeMap)
rv.vector = v
rv._key_to_id = self._key_to_id
rv = NodeMap(v, key_to_id=self._key_to_id)
rv._id_to_key = self._id_to_key
return rv
# return NodeMap.from_graphblas(v, key_to_id=self._key_to_id)


def vector_to_nodeset(self, v):
from .nodeset import NodeSet

rv = object.__new__(NodeSet)
rv.vector = v
rv._key_to_id = self._key_to_id
rv = NodeSet(v, key_to_id=self._key_to_id)
rv._id_to_key = self._id_to_key
return rv
# return NodeSet.from_graphblas(v, key_to_id=self._key_to_id)


def vector_to_set(self, v):
Expand Down
25 changes: 18 additions & 7 deletions graphblas_algorithms/classes/digraph.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from collections import defaultdict

import graphblas as gb
from graphblas import Matrix, Vector, binary, replace, select, unary

import graphblas_algorithms as ga
Expand Down Expand Up @@ -415,7 +416,7 @@ def to_directed_graph(G, weight=None, dtype=None):
if isinstance(G, DiGraph):
return G
try:
return DiGraph.from_graphblas(G)
return DiGraph(G)
except TypeError:
pass

Expand All @@ -435,7 +436,7 @@ def to_graph(G, weight=None, dtype=None):
return G
try:
# Should we check if it can be undirected?
return DiGraph.from_graphblas(G)
return DiGraph(G)
except TypeError:
pass

Expand Down Expand Up @@ -538,22 +539,28 @@ class DiGraph(Graph):
}
graph_attr_dict_factory = dict

def __init__(self, incoming_graph_data=None, **attr):
def __init__(self, incoming_graph_data=None, *, key_to_id=None, **attr):
if incoming_graph_data is not None:
raise NotImplementedError("incoming_graph_data is not None")
# Does not copy if A is a Matrix!
A = gb.core.utils.ensure_type(incoming_graph_data, Matrix)
if A.nrows != A.ncols:
raise ValueError(f"Adjacency matrix must be square; got {A.nrows} x {A.ncols}")
else:
A = Matrix()
self.graph_attr_dict_factory = self.graph_attr_dict_factory
self.graph = self.graph_attr_dict_factory() # dictionary for graph attributes
self.graph.update(attr)

# Graphblas-specific properties
self._A = Matrix()
self._key_to_id = {}
self._A = A
if key_to_id is None:
key_to_id = {i: i for i in range(A.nrows)}
self._key_to_id = key_to_id
self._id_to_key = None
self._cache = {}

# Graphblas-specific methods
from_networkx = classmethod(_utils.from_networkx)
from_graphblas = classmethod(_utils.from_graphblas)
id_to_key = property(_utils.id_to_key)
get_property = _utils.get_property
get_properties = _utils.get_properties
Expand Down Expand Up @@ -586,6 +593,10 @@ def name(self, s):
self._A.name = s
self.graph["name"] = s

@property
def matrix(self):
return self._A

def __iter__(self):
return iter(self._key_to_id)

Expand Down
23 changes: 17 additions & 6 deletions graphblas_algorithms/classes/graph.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from collections import defaultdict

import graphblas as gb
from graphblas import Matrix, Vector, select

import graphblas_algorithms as ga
Expand Down Expand Up @@ -153,7 +154,7 @@ def to_undirected_graph(G, weight=None, dtype=None):
if isinstance(G, Graph):
return G
try:
return Graph.from_graphblas(G)
return Graph(G)
except TypeError:
pass

Expand Down Expand Up @@ -243,22 +244,28 @@ class Graph:
}
graph_attr_dict_factory = dict

def __init__(self, incoming_graph_data=None, **attr):
def __init__(self, incoming_graph_data=None, *, key_to_id=None, **attr):
if incoming_graph_data is not None:
raise NotImplementedError("incoming_graph_data is not None")
# Does not copy if A is a Matrix!
A = gb.core.utils.ensure_type(incoming_graph_data, Matrix)
if A.nrows != A.ncols:
raise ValueError(f"Adjacency matrix must be square; got {A.nrows} x {A.ncols}")
else:
A = Matrix()
self.graph_attr_dict_factory = self.graph_attr_dict_factory
self.graph = self.graph_attr_dict_factory() # dictionary for graph attributes
self.graph.update(attr)

# Graphblas-specific properties
self._A = Matrix()
self._key_to_id = {}
self._A = A
if key_to_id is None:
key_to_id = {i: i for i in range(A.nrows)}
self._key_to_id = key_to_id
self._id_to_key = None
self._cache = {}

# Graphblas-specific methods
from_networkx = classmethod(_utils.from_networkx)
from_graphblas = classmethod(_utils.from_graphblas)
id_to_key = property(_utils.id_to_key)
get_property = _utils.get_property
get_properties = _utils.get_properties
Expand Down Expand Up @@ -292,6 +299,10 @@ def name(self, s):
self._A.name = s
self.graph["name"] = s

@property
def matrix(self):
return self._A

def __iter__(self):
return iter(self._key_to_id)

Expand Down
53 changes: 16 additions & 37 deletions graphblas_algorithms/classes/nodemap.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,13 @@


class NodeMap(MutableMapping):
def __init__(self):
raise NotImplementedError()
# .vector, ._key_to_id, ._id_to_key

@classmethod
def from_graphblas(cls, v, *, key_to_id=None):
rv = object.__new__(cls)
rv.vector = v
def __init__(self, v, *, key_to_id=None):
self.vector = v
if key_to_id is None:
rv._key_to_id = {i: i for i in range(v.size)}
self._key_to_id = {i: i for i in range(v.size)}
else:
rv._key_to_id = key_to_id
rv._id_to_key = None
return rv
self._key_to_id = key_to_id
self._id_to_key = None

id_to_key = property(_utils.id_to_key)
# get_property = _utils.get_property
Expand Down Expand Up @@ -104,15 +97,8 @@ def setdefault(self, key, default=None):


class VectorMap(MutableMapping):
def __init__(self):
raise NotImplementedError()
# .vector

@classmethod
def from_graphblas(cls, v):
rv = object.__new__(cls)
rv.vector = v
return rv
def __init__(self, v):
self.vector = v

# Requirements for MutableMapping
def __delitem__(self, key):
Expand Down Expand Up @@ -176,21 +162,14 @@ def setdefault(self, key, default=None):


class VectorNodeMap(MutableMapping):
def __init__(self):
raise NotImplementedError()
# .matrix, ._key_to_id, ._id_to_key, ._rows

@classmethod
def from_graphblas(cls, A, *, key_to_id=None):
rv = object.__new__(cls)
rv.matrix = A
def __init__(self, A, *, key_to_id=None):
self.matrix = A
if key_to_id is None:
rv._key_to_id = {i: i for i in range(A.size)}
self._key_to_id = {i: i for i in range(A.size)}
else:
rv._key_to_id = key_to_id
rv._id_to_key = None
rv._rows = None
return rv
self._key_to_id = key_to_id
self._id_to_key = None
self._rows = None

def _get_rows(self):
if self._rows is None:
Expand Down Expand Up @@ -226,7 +205,7 @@ def __getitem__(self, key):
idx = self._key_to_id[key]
if self._get_rows().get(idx) is None:
raise KeyError(key)
return VectorMap.from_graphblas(self.matrix[idx, :].new())
return VectorMap(self.matrix[idx, :].new())

def __iter__(self):
# Slow if we iterate over one; fast if we iterate over all
Expand Down Expand Up @@ -273,7 +252,7 @@ def get(self, key, default=None):
idx = self._key_to_id[key]
if self._get_rows().get(idx) is None:
return default
return VectorMap.from_graphblas(self.matrix[idx, :].new())
return VectorMap(self.matrix[idx, :].new())

# items
# keys
Expand All @@ -285,7 +264,7 @@ def popitem(self):
idx = next(rows.ss.iterkeys())
except StopIteration:
raise KeyError from None
value = VectorMap.from_graphblas(self.matrix[idx, :].new())
value = VectorMap(self.matrix[idx, :].new())
del self.matrix[idx, :]
del rows[idx]
return self.id_to_key[idx], value
Expand Down
17 changes: 5 additions & 12 deletions graphblas_algorithms/classes/nodeset.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,13 @@


class NodeSet(MutableSet):
def __init__(self):
raise NotImplementedError()
# .vector, ._key_to_id, ._id_to_key

@classmethod
def from_graphblas(cls, v, *, key_to_id=None):
rv = object.__new__(cls)
rv.vector = v
def __init__(self, v, *, key_to_id=None):
self.vector = v
if key_to_id is None:
rv._key_to_id = {i: i for i in range(v.size)}
self._key_to_id = {i: i for i in range(v.size)}
else:
rv._key_to_id = key_to_id
rv._id_to_key = None
return rv
self._key_to_id = key_to_id
self._id_to_key = None

id_to_key = property(_utils.id_to_key)
# get_property = _utils.get_property
Expand Down
2 changes: 1 addition & 1 deletion graphblas_algorithms/nxapi/cluster.py
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,6 @@ def generalized_degree(G, nodes=None):
return G.vector_to_nodemap(result)
mask = G.list_to_mask(nodes)
result = algorithms.generalized_degree(G, mask=mask)
rv = VectorNodeMap.from_graphblas(result, key_to_id=G._key_to_id)
rv = VectorNodeMap(result, key_to_id=G._key_to_id)
rv._id_to_key = G._id_to_key
return rv