Utilities

The pygsp.utils module implements some utility functions used throughout the package.

pygsp.utils.build_logger(name)[source]
pygsp.utils.compute_log_scales(lmin, lmax, Nscales, t1=1, t2=2)[source]

Compute logarithm scales for wavelets.

Parameters:
lminfloat

Smallest non-zero eigenvalue.

lmaxfloat

Largest eigenvalue, i.e. pygsp.graphs.Graph.lmax.

Nscalesint

Number of scales.

Returns:
scalesndarray

List of scales of length Nscales.

Examples

>>> from pygsp import utils
>>> utils.compute_log_scales(1, 10, 3)
array([2.       , 0.4472136, 0.1      ])
pygsp.utils.distanz(x, y=None)[source]

Calculate the distance between two colon vectors.

Parameters:
xndarray

First colon vector

yndarray

Second colon vector

Returns:
dndarray

Distance between x and y

Examples

>>> from pygsp import utils
>>> x = np.arange(3)
>>> utils.distanz(x, x)
array([[0., 1., 2.],
       [1., 0., 1.],
       [2., 1., 0.]])
pygsp.utils.filterbank_handler(func)[source]
pygsp.utils.loadmat(path)[source]

Load a matlab data file.

Parameters:
pathstring

Path to the mat file from the data folder, without the .mat extension.

Returns:
datadict

dictionary with variable names as keys, and loaded matrices as values.

Examples

>>> from pygsp import utils
>>> data = utils.loadmat('pointclouds/bunny')
>>> data['bunny'].shape
(2503, 3)
pygsp.utils.rescale_center(x)[source]

Rescale and center data, e.g. embedding coordinates.

Parameters:
xndarray

Data to be rescaled.

Returns:
rndarray

Rescaled data.

Examples

>>> from pygsp import utils
>>> x = np.array([[1, 6], [2, 5], [3, 4]])
>>> utils.rescale_center(x)
array([[-1. ,  1. ],
       [-0.6,  0.6],
       [-0.2,  0.2]])
pygsp.utils.resistance_distance(G)[source]

Compute the resistance distances of a graph.

Parameters:
GGraph or sparse matrix

Graph structure or Laplacian matrix (L)

Returns:
rdsparse matrix

distance matrix

References

[KRandic93]

pygsp.utils.symmetrize(W, method='average')[source]

Symmetrize a square matrix.

Parameters:
Warray_like

Square matrix to be symmetrized

methodstring
  • ‘average’ : symmetrize by averaging with the transpose. Most useful when transforming a directed graph to an undirected one.

  • ‘maximum’ : symmetrize by taking the maximum with the transpose. Similar to ‘fill’ except that ambiguous entries are resolved by taking the largest value.

  • ‘fill’ : symmetrize by filling in the zeros in both the upper and lower triangular parts. Ambiguous entries are resolved by averaging the values.

  • ‘tril’ : symmetrize by considering the lower triangular part only.

  • ‘triu’ : symmetrize by considering the upper triangular part only.

Notes

You can have the sum by multiplying the average by two. It is however not a good candidate for this function as it modifies an already symmetric matrix.

Examples

>>> from pygsp import utils
>>> W = np.array([[0, 3, 0], [3, 1, 6], [4, 2, 3]], dtype=float)
>>> W
array([[0., 3., 0.],
       [3., 1., 6.],
       [4., 2., 3.]])
>>> utils.symmetrize(W, method='average')
array([[0., 3., 2.],
       [3., 1., 4.],
       [2., 4., 3.]])
>>> 2 * utils.symmetrize(W, method='average')
array([[0., 6., 4.],
       [6., 2., 8.],
       [4., 8., 6.]])
>>> utils.symmetrize(W, method='maximum')
array([[0., 3., 4.],
       [3., 1., 6.],
       [4., 6., 3.]])
>>> utils.symmetrize(W, method='fill')
array([[0., 3., 4.],
       [3., 1., 4.],
       [4., 4., 3.]])
>>> utils.symmetrize(W, method='tril')
array([[0., 3., 4.],
       [3., 1., 2.],
       [4., 2., 3.]])
>>> utils.symmetrize(W, method='triu')
array([[0., 3., 0.],
       [3., 1., 6.],
       [0., 6., 3.]])