-
Notifications
You must be signed in to change notification settings - Fork 0
/
sampling.py
313 lines (234 loc) · 8.98 KB
/
sampling.py
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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""Sampling module
"""
import numpy as np
from scipy import sparse
## MAIN FUNCTIONS ##
def sample_coordinates(n_coordinates, n_samples, probs=None, replace=False):
r"""
Sample a fixed number of coordinates independently at random.
Parameters
----------
n_coordinates : int
Number of coordinates from which to sample.
n_samples : int
Number of samples to take from the set `{0, 1, 2, ..., n_coordinates - 1}`.
probs : array, optional
The probability weights for each coordinate.
(the default is None, which results in uniform sampling)
replace: bool, optional
Sample with replacement. (default is False)
Returns
-------
array
The indices of the sampled coordinates from the set
`{0, 1, 2, ..., n_coordinates - 1}`.
"""
n_coordinates = int(n_coordinates)
n_samples = int(n_samples)
if probs is not None:
probs = np.true_divide(probs, np.sum(probs))
return np.random.choice(n_coordinates, n_samples, replace=replace, p=probs)
def sample_coordinates_bernoulli(n_coordinates, probs=None):
r"""
Sample coordinates using Bernoulli selectors.
Parameters
----------
n_coordinates : int
Number of coordinates from which to sample
probs : array, optional
A vector with the success probabilities of each coordinate selector.
(the default is None, which results in a fair coin toss for each coordinate)
Returns
-------
array
The indices of the sampled coordinates
Notes
-----
This sampling is always without replacement, but the number of samples is a random
variable.
"""
n_coordinates = int(n_coordinates)
sample_idx = []
probs = np.ones((n_coordinates,))/2. if probs is None else probs
for i in np.arange(n_coordinates):
if np.random.binomial(1, probs[i]) == 1:
sample_idx.append(i)
return np.array(sample_idx)
## SPECIAL CASES ##
def uniform_vertex(graph, n_samples, replace=False):
r"""
Sample vertices uniformly at random.
Parameters
----------
graph: :class:`pygsp.graphs.Graph`
A graph object.
n_samples: int
Number of measurements to take
replace: bool, optional
Sample with replacement. (default is True)
Returns
-------
(n_samples, ) array
Indices of the sampled vertices
"""
return sample_coordinates(graph.n_vertices,
n_samples,
probs=None,
replace=replace)
def degree_vertex(graph, n_samples, replace=False):
r"""
Sample vertices proportionally to their degree.
Parameters
----------
graph: :class:`pygsp.graphs.Graph`
A graph object.
n_samples: int
Number of measurements to take
replace: bool, optional
Sample with replacement. (default is True)
Returns
-------
(n_samples, ) array
Indices of the sampled vertices
"""
return sample_coordinates(graph.n_vertices,
n_samples,
probs=graph.dw,
replace=replace)
def inv_degree_vertex(graph, n_samples, replace=False):
r"""
Sample vertices proportionally to the inverse of their degree.
Parameters
----------
graph: :class:`pygsp.graphs.Graph`
A graph object.
n_samples: int
Number of measurements to take
replace: bool, optional
Sample with replacement. (default is True)
Returns
-------
(n_samples, ) array
Indices of the sampled vertices
"""
return sample_coordinates(graph.n_vertices,
n_samples,
probs=1./graph.dw,
replace=replace)
def inter_comm_degree_vertex(graph, n_samples, gt_signal, replace=False):
r"""
Sample vertices proportionally to their connection weight with members in other communities
Parameters
----------
graph: :class:`pygsp.graphs.Graph`
A graph object.
n_samples: int
Number of measurements to take
gt_signal : array
The ground truth signal to be sampled.
replace: bool, optional
Sample with replacement. (default is True)
Returns
-------
(n_samples, ) array
Indices of the sampled vertices
Notes
-----
A community is defined as a level-set of the ground-truth signal. For example, if `gt_signal`
is {0,1}-valued, one community will contain all the vertices `i` for which `gt_signal[i]=0`,
while the other community will contain all the vertices `j` for which `gt_signal[i]=1`.
"""
# Copy the (weigthed) adjacency matrix
adjacency = sparse.lil_matrix(graph.W.copy())
# Make sure the labels vector is a numpy array
labels = np.asarray(gt_signal)
for vertex in np.arange(len(labels)):
# Disconnect vertices that belong in the same community
adjacency[vertex, labels == labels[vertex]] = 0
inter_comm_degree = np.array(adjacency.sum(axis=1)).flatten()
return sample_coordinates(graph.n_vertices,
n_samples,
probs=inter_comm_degree,
replace=replace)
def naive_tv_coherence(graph, n_samples, replace=False):
r"""
Sample proportionally to a naive coherence measure from the TV interpolation problem.
Parameters
----------
graph: :class:`pygsp.graphs.Graph`
A graph object.
n_samples: int
Number of measurements to take.
replace: bool, optional
Sample with replacement. (default is True)
Returns
-------
(n_samples, ) array
Indices of the sampled vertices.
Notes
-----
Denote by :math:`D` the graph gradient matrix, and let :math:`\{e_i\}_i` be the
standard basis in :math:`\mathbb{R}^n`. The vertex sampling probabilities are set as
:math:`\pi_i \propto \|(D^+)^\top e_i\|_\infty \cdot \|D e_i\|_1`,
the product of the column-wise infinity-norm of :math:`(D^+)^\top` with
the column-wise l1-norm of :math:`D`.
"""
# Make sure the graph has D as an attribute
graph.compute_differential_operator()
# The incidence matrix in graph.D is the transpose of the gradient matrix
D = graph.D.T
# Get the Moore-Penrose pseudo-inverse of the gradient matrix
# Note: `np.linalg.pinv` can be quite slow for large graphs
D_plus = np.linalg.pinv(D.toarray())
# Set the sampling probability weights
probs = np.max(np.abs(D_plus.T), axis=0) * np.sum(np.abs(D).toarray(), axis=0)
return sample_coordinates(graph.n_vertices,
n_samples,
probs=probs,
replace=replace)
def jump_set_tv_coherence(graph, n_samples, gt_signal, replace=False):
r"""
Sample proportionally to a jump-set-restricted coherence measure from the TV interpolation problem.
Parameters
----------
graph: :class:`pygsp.graphs.Graph`
A graph object.
n_samples: int
Number of measurements to take.
gt_signal : array
The ground truth signal to be sampled.
replace: bool, optional
Sample with replacement. (default is True)
Returns
-------
(n_samples, ) array
Indices of the sampled vertices.
Notes
-----
Denote by :math:`D \in \mathbb{R}^{N \times n}` the graph gradient matrix, and by
:math:`x` the ground-truth signal to be sampled. Let :math:`\{e_i\}_i` be the
standard basis in :math:`\mathbb{R}^n` and :math:`P_S` be the orthogonal projection
of vectors in :math:`\mathbb{R}^N` onto the support :math:`S`of :math:`Dx`. The
vertex sampling probabilities are set as
:math:`\pi_i \propto \|(D^+)^\top e_i\|_\infty \cdot \|P_S D e_i\|_1`,
the product of the column-wise infinity-norm of :math:`(D^+)^\top` with
the column-wise l1-norm of :math:`P_S D`.
"""
# Make sure the graph has D as an attribute
graph.compute_differential_operator()
# The incidence matrix in graph.D is the transpose of the gradient matrix
D = graph.D.T
# Get the Moore-Penrose pseudo-inverse of the gradient matrix
# Note: `np.linalg.pinv` can be quite slow for large graphs
D_plus = np.linalg.pinv(D.toarray())
# Get the orthogonal projection matrix onto the support of the jump-set
from scipy.sparse import diags
P_S = diags((np.abs(D @ gt_signal) > 1e-6).astype(float))
# Set the sampling probability weights
probs = np.max(np.abs(D_plus.T), axis=0) * np.sum(np.abs(P_S @ D).toarray(), axis=0)
return sample_coordinates(graph.n_vertices,
n_samples,
probs=probs,
replace=replace)