Skip to content

Commit

Permalink
Merge 927673c into f6b5a1f
Browse files Browse the repository at this point in the history
  • Loading branch information
mdeff committed Dec 18, 2020
2 parents f6b5a1f + 927673c commit 856a6d6
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 12 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Expand Up @@ -11,7 +11,7 @@ addons:
apt:
sources:
- sourceline: 'deb https://downloads.skewed.de/apt bionic main'
key_url: 'http://ha.pool.sks-keyservers.net/pks/lookup?op=get&search=0x612DEFB798507F25'
key_url: 'https://keys.openpgp.org/vks/v1/by-keyid/612DEFB798507F25'
packages:
- python3-graph-tool
- libqt5gui5 # pyqt5>5.11 fails to load the xcb platform plugin without it
Expand Down
27 changes: 16 additions & 11 deletions pygsp/reduction.py
Expand Up @@ -34,31 +34,35 @@ def _analysis(g, s, **kwargs):
return s.swapaxes(1, 2).reshape(-1, s.shape[1], order='F')


def graph_sparsify(M, epsilon, maxiter=10):
def graph_sparsify(M, epsilon, maxiter=10, seed=None):
r"""Sparsify a graph (with Spielman-Srivastava).
Parameters
----------
M : Graph or sparse matrix
Graph structure or a Laplacian matrix
epsilon : int
Sparsification parameter
epsilon : float
Sparsification parameter, which must be between ``1/sqrt(N)`` and 1.
maxiter : int, optional
Maximum number of iterations.
seed : {None, int, RandomState, Generator}, optional
Seed for the random number generator (for reproducible sparsification).
Returns
-------
Mnew : Graph or sparse matrix
New graph structure or sparse matrix
Notes
-----
Epsilon should be between 1/sqrt(N) and 1
Examples
--------
>>> from pygsp import reduction
>>> G = graphs.Sensor(256, k=20, distributed=True)
>>> epsilon = 0.4
>>> G2 = reduction.graph_sparsify(G, epsilon)
>>> from matplotlib import pyplot as plt
>>> G = graphs.Sensor(100, k=20, distributed=True, seed=1)
>>> Gs = reduction.graph_sparsify(G, epsilon=0.4, seed=1)
>>> fig, axes = plt.subplots(1, 2)
>>> _ = G.plot(ax=axes[0], title='original')
>>> Gs.coords = G.coords
>>> _ = Gs.plot(ax=axes[1], title='sparsified')
References
----------
Expand Down Expand Up @@ -100,6 +104,7 @@ def graph_sparsify(M, epsilon, maxiter=10):
Re = np.maximum(0, resistance_distances[start_nodes, end_nodes])
Pe = weights * Re
Pe = Pe / np.sum(Pe)
dist = stats.rv_discrete(values=(np.arange(len(Pe)), Pe), seed=seed)

for i in range(maxiter):
# Rudelson, 1996 Random Vectors in the Isotropic Position
Expand All @@ -109,7 +114,7 @@ def graph_sparsify(M, epsilon, maxiter=10):
C = 4 * C0
q = round(N * np.log(N) * 9 * C**2 / (epsilon**2))

results = stats.rv_discrete(values=(np.arange(np.shape(Pe)[0]), Pe)).rvs(size=int(q))
results = dist.rvs(size=int(q))
spin_counts = stats.itemfreq(results).astype(int)
per_spin_weights = weights / (q * Pe)

Expand Down

0 comments on commit 856a6d6

Please sign in to comment.