-
Notifications
You must be signed in to change notification settings - Fork 266
Expand file tree
/
Copy pathsparse_matrix.py
More file actions
243 lines (190 loc) · 7.36 KB
/
sparse_matrix.py
File metadata and controls
243 lines (190 loc) · 7.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
# vim:ts=4:sw=4:sts=4:et
# -*- coding: utf-8 -*-
"""Implementation of Python-level sparse matrix operations."""
from __future__ import with_statement
__all__ = ()
__docformat__ = "restructuredtext en"
from operator import add
from igraph._igraph import (
ADJ_DIRECTED,
ADJ_UNDIRECTED,
ADJ_MAX,
ADJ_MIN,
ADJ_PLUS,
ADJ_UPPER,
ADJ_LOWER,
)
_SUPPORTED_MODES = ("directed", "undirected", "max", "min", "plus", "lower", "upper")
def _convert_mode_argument(mode):
# resolve mode constants, convert to lowercase
mode = {
ADJ_DIRECTED: "directed",
ADJ_UNDIRECTED: "undirected",
ADJ_MAX: "max",
ADJ_MIN: "min",
ADJ_PLUS: "plus",
ADJ_UPPER: "upper",
ADJ_LOWER: "lower",
}.get(mode, mode).lower()
if mode not in _SUPPORTED_MODES:
raise ValueError("mode should be one of " + (" ".join(_SUPPORTED_MODES)))
if mode == "undirected":
mode = "max"
return mode
def _maybe_halve_diagonal(m, condition):
"""Halves all items in the diagonal of the given SciPy sparse matrix in
coo mode *if* and *only if* the given condition is True.
Returns the row, column and data arrays.
"""
data_array = m.data
if condition:
# We can't do data_array[m.row == m.col] /= 2 here because we would be
# modifying the array in-place and the end user wouldn't like if we
# messed with their matrix. So we make a copy.
data_array = data_array.copy()
(idxs,) = (m.row == m.col).nonzero()
for i in idxs:
data_array[i] /= 2
return m.row, m.col, data_array
# Logic to get graph from scipy sparse matrix. This would be simple if there
# weren't so many modes.
def _graph_from_sparse_matrix(klass, matrix, mode="directed", loops="once"):
"""Construct graph from sparse matrix, unweighted.
@param loops: specifies how the diagonal of the matrix should be handled:
- C{"ignore"} - ignore loop edges in the diagonal
- C{"once"} - treat the diagonal entries as loop edge counts
- C{"twice"} - treat the diagonal entries as I{twice} the number
of loop edges
"""
# This function assumes there is scipy and the matrix is a scipy sparse
# matrix. The caller should make sure those conditions are met.
from scipy import sparse
if not isinstance(matrix, sparse.coo_matrix):
matrix = matrix.tocoo()
nvert = max(matrix.shape)
if min(matrix.shape) != nvert:
raise ValueError("Matrix must be square")
# Shorthand notation
m = matrix
mode = _convert_mode_argument(mode)
keep_loops = loops == "twice" or loops == "once" or loops is True
m_row, m_col, m_data = _maybe_halve_diagonal(m, loops == "twice")
if mode == "directed":
edges = []
for i, j, n in zip(m_row, m_col, m_data):
if i != j or keep_loops:
edges.extend([(i, j)] * n)
elif mode in ("max", "plus"):
fun = max if mode == "max" else add
nedges = {}
for i, j, n in zip(m_row, m_col, m_data):
if i == j and not keep_loops:
continue
pair = (i, j) if i < j else (j, i)
nedges[pair] = fun(nedges.get(pair, 0), n)
edges = sum(
([e] * n for e, n in nedges.items()),
[],
)
elif mode == "min":
tmp = {(i, j): n for i, j, n in zip(m_row, m_col, m_data)}
nedges = {}
for pair, weight in tmp.items():
i, j = pair
if i == j and keep_loops:
nedges[pair] = weight
elif i < j:
nedges[pair] = min(weight, tmp.get((j, i), 0))
edges = sum(
([e] * n for e, n in nedges.items()),
[],
)
elif mode == "upper":
edges = []
for i, j, n in zip(m_row, m_col, m_data):
if j > i or (keep_loops and j == i):
edges.extend([(i, j)] * n)
elif mode == "lower":
edges = []
for i, j, n in zip(m_row, m_col, m_data):
if j < i or (keep_loops and j == i):
edges.extend([(i, j)] * n)
else:
raise ValueError(f"invalid mode: {mode!r}")
return klass(nvert, edges=edges, directed=(mode == "directed"))
def _graph_from_weighted_sparse_matrix(
klass, matrix, mode=ADJ_DIRECTED, attr="weight", loops="once"
):
"""Construct graph from sparse matrix, weighted
NOTE: Of course, you cannot emcompass a fully general weighted multigraph
with a single adjacency matrix, so we don't try to do it here either.
@param loops: specifies how to handle loop edges. When C{False} or
C{"ignore"}, the diagonal of the adjacency matrix will be ignored. When
C{True} or C{"once"}, the diagonal is assumed to contain the weight of the
corresponding loop edge. When C{"twice"}, the diagonal is assumed to
contain I{twice} the weight of the corresponding loop edge.
"""
# This function assumes there is scipy and the matrix is a scipy sparse
# matrix. The caller should make sure those conditions are met.
from scipy import sparse
if not isinstance(matrix, sparse.coo_matrix):
matrix = matrix.tocoo()
nvert = max(matrix.shape)
if min(matrix.shape) != nvert:
raise ValueError("Matrix must be square")
# Shorthand notation
m = matrix
mode = _convert_mode_argument(mode)
keep_loops = loops == "twice" or loops == "once" or loops is True
m_row, m_col, m_data = _maybe_halve_diagonal(m, loops == "twice")
if mode == "directed":
if not keep_loops:
edges, weights = [], []
for i, j, n in zip(m_row, m_col, m_data):
if i != j:
edges.append((i, j))
weights.append(n)
else: # loops == "once" or True
edges = list(zip(m_row, m_col))
weights = list(m_data)
elif mode in ("max", "plus"):
fun = max if mode == "max" else add
nedges = {}
for i, j, n in zip(m_row, m_col, m_data):
if i == j and not keep_loops:
continue
pair = (i, j) if i < j else (j, i)
nedges[pair] = fun(nedges.get(pair, 0), n)
edges, weights = zip(*nedges.items())
elif mode == "min":
tmp = {(i, j): n for i, j, n in zip(m_row, m_col, m_data)}
nedges = {}
for pair, weight in tmp.items():
i, j = pair
if i == j and keep_loops:
nedges[pair] = weight
elif i < j:
nedges[pair] = min(weight, tmp.get((j, i), 0))
edges, weights = [], []
for pair in sorted(nedges.keys()):
weight = nedges[pair]
if weight != 0:
edges.append(pair)
weights.append(nedges[pair])
elif mode == "upper":
edges, weights = [], []
for i, j, n in zip(m_row, m_col, m_data):
if j > i or (keep_loops and j == i):
edges.append((i, j))
weights.append(n)
elif mode == "lower":
edges, weights = [], []
for i, j, n in zip(m_row, m_col, m_data):
if j < i or (keep_loops and j == i):
edges.append((i, j))
weights.append(n)
else:
raise ValueError(f"invalid mode: {mode!r}")
return klass(
nvert, edges=edges, directed=(mode == "directed"), edge_attrs={attr: weights}
)