-
Notifications
You must be signed in to change notification settings - Fork 171
/
rescaling.py
422 lines (346 loc) · 16 KB
/
rescaling.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
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
"""Rescaling methods for persistent homology."""
# License: GNU AGPLv3
import itertools
from numbers import Real
from types import FunctionType
import numpy as np
from joblib import Parallel, delayed
from sklearn.base import BaseEstimator, TransformerMixin
from sklearn.metrics import pairwise_distances
from sklearn.utils.validation import check_array, check_is_fitted
from ..base import PlotterMixin
from ..plotting import plot_heatmap
from ..utils._docs import adapt_fit_transform_docs
from ..utils.intervals import Interval
from ..utils.validation import validate_params
@adapt_fit_transform_docs
class ConsistentRescaling(BaseEstimator, TransformerMixin, PlotterMixin):
"""Rescaling of distances between pairs of points by the geometric mean
of the distances to the respective :math:`k`-th nearest neighbours.
Based on ideas in [1]_. The computation during :meth:`transform` depends on
the nature of the array `X`. If each entry in `X` along axis 0 represents a
distance matrix :math:`D`, then the corresponding entry in the transformed
array is the distance matrix
:math:`D'_{i,j} = D_{i,j}/\\sqrt{D_{i,k_i}D_{j,k_j}}`, where :math:`k_i` is
the index of the :math:`k`-th largest value in row :math:`i` (and similarly
for :math:`j`). If the entries in `X` represent point clouds, their
distance matrices are first computed, and then rescaled according to the
same formula.
Parameters
----------
metric : string or callable, optional, default: ``'euclidean'``
If set to ``'precomputed'``, each entry in `X` along axis 0 is
interpreted to be a distance matrix. Otherwise, entries are
interpreted as feature arrays, and `metric` determines a rule with
which to calculate distances between pairs of instances (i.e. rows)
in these arrays.
If `metric` is a string, it must be one of the options allowed by
:func:`scipy.spatial.distance.pdist` for its metric parameter, or a
metric listed in :obj:`sklearn.pairwise.PAIRWISE_DISTANCE_FUNCTIONS`,
including "euclidean", "manhattan" or "cosine".
If `metric` is a callable function, it is called on each pair of
instances and the resulting value recorded. The callable should take
two arrays from the entry in `X` as input, and return a value
indicating the distance between them.
metric_params : dict or None, optional, default: ``None``
Additional keyword arguments for the metric function.
neighbor_rank : int, optional, default: ``1``
Rank of the neighbors used to modify the metric structure according
to the "consistent rescaling" procedure.
n_jobs : int or None, optional, default: ``None``
The number of jobs to use for the computation. ``None`` means 1 unless
in a :obj:`joblib.parallel_backend` context. ``-1`` means using all
processors.
Attributes
----------
effective_metric_params_ : dict
Dictionary containing all information present in `metric_params`.
If `metric_params` is ``None``, it is set to the empty dictionary.
Examples
--------
>>> import numpy as np
>>> from gtda.point_clouds import ConsistentRescaling
>>> X = np.array([[[0, 0], [1, 2], [5, 6]]])
>>> cr = ConsistentRescaling()
>>> X_rescaled = cr.fit_transform(X)
>>> print(X_rescaled.shape)
(1, 3, 3)
See also
--------
ConsecutiveRescaling
References
----------
.. [1] T. Berry and T. Sauer, "Consistent manifold representation for
topological data analysis"; *Foundations of data analysis* **1**,
pp. 1--38, 2019; `DOI: 10.3934/fods.2019001
<http://dx.doi.org/10.3934/fods.2019001>`_.
"""
_hyperparameters = {
'metric': {'type': (str, FunctionType)},
'metric_params': {'type': (dict, type(None))},
'neighbor_rank': {'type': int,
'in': Interval(1, np.inf, closed='left')}
}
def __init__(self, metric='euclidean', metric_params=None, neighbor_rank=1,
n_jobs=None):
self.metric = metric
self.metric_params = metric_params
self.neighbor_rank = neighbor_rank
self.n_jobs = n_jobs
def _consistent_rescaling(self, X):
Xm = pairwise_distances(X, metric=self.metric, n_jobs=1,
**self.effective_metric_params_)
indices_k_neighbor = np.argsort(Xm)[:, self.neighbor_rank]
distance_k_neighbor = Xm[np.arange(X.shape[0]),
indices_k_neighbor]
# Only calculate metric for upper triangle
Xc = np.zeros(Xm.shape)
iterator = itertools.combinations(range(Xm.shape[0]), 2)
for i, j in iterator:
Xc[i, j] = Xm[i, j] / (np.sqrt(distance_k_neighbor[i] *
distance_k_neighbor[j]))
return Xc + Xc.T
def fit(self, X, y=None):
"""Calculate :attr:`effective_metric_params_`. Then, return the
estimator.
This method is here to implement the usual scikit-learn API and hence
work in pipelines.
Parameters
----------
X : ndarray of shape (n_samples, n_points, n_points) or (n_samples, \
n_points, n_dimensions)
Input data. If ``metric == 'precomputed'``, the input should be an
ndarray whose each entry along axis 0 is a distance matrix of shape
``(n_points, n_points)``. Otherwise, each such entry will be
interpreted as an array of ``n_points`` row vectors in
``n_dimensions``-dimensional space.
y : None
There is no need for a target in a transformer, yet the pipeline
API requires this parameter.
Returns
-------
self : object
"""
check_array(X, allow_nd=True)
validate_params(
self.get_params(), self._hyperparameters, exclude=['n_jobs'])
if self.metric_params is None:
self.effective_metric_params_ = {}
else:
self.effective_metric_params_ = self.metric_params.copy()
return self
def transform(self, X, y=None):
"""For each entry in the input data array X, find the metric structure
after consistent rescaling and encode it as a distance matrix.
Parameters
----------
X : ndarray of shape (n_samples, n_points, n_points) or (n_samples, \
n_points, n_dimensions)
Input data. If ``metric == 'precomputed'``, the input should be an
ndarray whose each entry along axis 0 is a distance matrix of shape
``(n_points, n_points)``. Otherwise, each such entry will be
interpreted as an array of ``n_points`` row vectors in
``n_dimensions``-dimensional space.
y : None
There is no need for a target in a transformer, yet the pipeline
API requires this parameter.
Returns
-------
Xt : ndarray of shape (n_samples, n_points, n_points)
Array containing (as entries along axis 0) the distance matrices
after consistent rescaling.
"""
check_is_fitted(self)
Xt = check_array(X, allow_nd=True)
Xt = Parallel(n_jobs=self.n_jobs)(
delayed(self._consistent_rescaling)(x) for x in Xt)
Xt = np.array(Xt)
return Xt
@staticmethod
def plot(Xt, sample=0, colorscale='blues', plotly_params=None):
"""Plot a sample from a collection of distance matrices.
Parameters
----------
Xt : ndarray of shape (n_samples, n_points, n_points)
Collection of distance matrices, such as returned by
:meth:`transform`.
sample : int, optional, default: ``0``
Index of the sample to be plotted.
colorscale : str, optional, default: ``'blues'``
Color scale to be used in the heat map. Can be anything allowed by
:class:`plotly.graph_objects.Heatmap`.
plotly_params : dict or None, optional, default: ``None``
Custom parameters to configure the plotly figure. Allowed keys are
``"trace"`` and ``"layout"``, and the corresponding values should
be dictionaries containing keyword arguments as would be fed to the
:meth:`update_traces` and :meth:`update_layout` methods of
:class:`plotly.graph_objects.Figure`.
Returns
-------
fig : :class:`plotly.graph_objects.Figure` object
Plotly figure.
"""
return plot_heatmap(
Xt[sample], colorscale=colorscale,
title=f"{sample}-th distance matrix after consistent rescaling",
plotly_params=plotly_params
)
@adapt_fit_transform_docs
class ConsecutiveRescaling(BaseEstimator, TransformerMixin, PlotterMixin):
"""Rescaling of distances between consecutive pairs of points by a fixed
factor.
The computation during :meth:`transform` depends on the nature of the array
`X`. If each entry in `X` along axis 0 represents a distance matrix
:math:`D`, then the corresponding entry in the transformed array is the
distance matrix :math:`D'_{i,i+1} = \\alpha D_{i,i+1}` where
:math:`\\alpha` is a positive factor. If the entries in `X` represent point
clouds, their distance matrices are first computed, and then rescaled
according to the same formula.
Parameters
----------
metric : string or callable, optional, default: ``'euclidean'``
If set to ``'precomputed'``, each entry in `X` along axis 0 is
interpreted to be a distance matrix. Otherwise, entries are
interpreted as feature arrays, and `metric` determines a rule with
which to calculate distances between pairs of instances (i.e. rows)
in these arrays.
If `metric` is a string, it must be one of the options allowed by
:func:`scipy.spatial.distance.pdist` for its metric parameter, or a
metric listed in :obj:`sklearn.pairwise.PAIRWISE_DISTANCE_FUNCTIONS`,
including "euclidean", "manhattan" or "cosine".
If `metric` is a callable function, it is called on each pair of
instances and the resulting value recorded. The callable should take
two arrays from the entry in `X` as input, and return a value
indicating the distance between them.
metric_params : dict or None, optional, default: ``None``
Additional keyword arguments for the metric function.
factor : float, optional, default: ``0.``
Factor by which to multiply the distance between consecutive
points.
n_jobs : int or None, optional, default: ``None``
The number of jobs to use for the computation. ``None`` means 1 unless
in a :obj:`joblib.parallel_backend` context. ``-1`` means using all
processors.
Attributes
----------
effective_metric_params_ : dict
Dictionary containing all information present in `metric_params`.
If `metric_params` is ``None``, it is set to the empty dictionary.
Examples
--------
>>> import numpy as np
>>> from gtda.point_clouds import ConsecutiveRescaling
>>> X = np.array([[[0, 0], [1, 2], [5, 6]]])
>>> cr = ConsecutiveRescaling()
>>> X_rescaled = cr.fit_transform(X)
>>> print(X_rescaled.shape)
(1, 3, 3)
See also
--------
ConsistentRescaling
"""
_hyperparameters = {
'metric': {'type': (str, FunctionType)},
'metric_params': {'type': (dict, type(None))},
'factor': {'type': Real, 'in': Interval(0, np.inf, closed='both')}
}
def __init__(self, metric='euclidean', metric_params=None, factor=0.,
n_jobs=None):
self.metric = metric
self.metric_params = metric_params
self.factor = factor
self.n_jobs = n_jobs
def fit(self, X, y=None):
"""Calculate :attr:`effective_metric_params_`. Then, return the
estimator.
This method is here to implement the usual scikit-learn API and hence
work in pipelines.
Parameters
----------
X : ndarray of shape (n_samples, n_points, n_points) or (n_samples, \
n_points, n_dimensions)
Input data. If ``metric == 'precomputed'``, the input should be an
ndarray whose each entry along axis 0 is a distance matrix of shape
``(n_points, n_points)``. Otherwise, each such entry will be
interpreted as an array of ``n_points`` row vectors in
``n_dimensions``-dimensional space.
y : None
There is no need for a target in a transformer, yet the pipeline
API requires this parameter.
Returns
-------
self : object
"""
check_array(X, allow_nd=True)
validate_params(
self.get_params(), self._hyperparameters, exclude=['n_jobs'])
if self.metric_params is None:
self.effective_metric_params_ = {}
else:
self.effective_metric_params_ = self.metric_params.copy()
return self
def transform(self, X, y=None):
"""For each entry in the input data array X, find the metric structure
after consecutive rescaling and encode it as a distance matrix.
Parameters
----------
X : ndarray of shape (n_samples, n_points, n_points) or (n_samples, \
n_points, n_dimensions)
Input data. If ``metric == 'precomputed'``, the input should be an
ndarray whose each entry along axis 0 is a distance matrix of shape
``(n_points, n_points)``. Otherwise, each such entry will be
interpreted as an array of ``n_points`` row vectors in
``n_dimensions``-dimensional space.
y : None
There is no need for a target in a transformer, yet the pipeline
API requires this parameter.
Returns
-------
Xt : ndarray of shape (n_samples, n_points, n_points)
Array containing (as entries along axis 0) the distance matrices
after consecutive rescaling.
"""
check_is_fitted(self)
is_precomputed = self.metric == 'precomputed'
X = check_array(X, allow_nd=True, copy=is_precomputed)
Xt = Parallel(n_jobs=self.n_jobs)(
delayed(pairwise_distances)(
x, metric=self.metric, n_jobs=1,
**self.effective_metric_params_)
for x in X)
if is_precomputed:
# Parallel loop above serves only as additional input validation
Xt = X
else:
Xt = np.array(Xt)
Xt[:, range(Xt.shape[1] - 1), range(1, Xt.shape[1])] *= self.factor
return Xt
@staticmethod
def plot(Xt, sample=0, colorscale='blues', plotly_params=None):
"""Plot a sample from a collection of distance matrices.
Parameters
----------
Xt : ndarray of shape (n_samples, n_points, n_points)
Collection of distance matrices, such as returned by
:meth:`transform`.
sample : int, optional, default: ``0``
Index of the sample to be plotted.
colorscale : str, optional, default: ``'blues'``
Color scale to be used in the heat map. Can be anything allowed by
:class:`plotly.graph_objects.Heatmap`.
plotly_params : dict or None, optional, default: ``None``
Custom parameters to configure the plotly figure. Allowed keys are
``"trace"`` and ``"layout"``, and the corresponding values should
be dictionaries containing keyword arguments as would be fed to the
:meth:`update_traces` and :meth:`update_layout` methods of
:class:`plotly.graph_objects.Figure`.
Returns
-------
fig : :class:`plotly.graph_objects.Figure` object
Plotly figure.
"""
return plot_heatmap(
Xt[sample], colorscale=colorscale,
title=f"{sample}-th distance matrix after consecutive rescaling",
plotly_params=plotly_params
)