Utilities

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

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

Compute logarithm scales for wavelets.

Parameters:

lmin : float

Smallest non-zero eigenvalue.

lmax : float

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

Nscales : int

Number of scales.

Returns:

scales : ndarray

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:

x : ndarray

First colon vector

y : ndarray

Second colon vector

Returns:

d : ndarray

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.extract_patches(img, patch_shape=(3, 3))[source]

Extract a patch feature vector for every pixel of an image.

Parameters:

img : array

Input image.

patch_shape : tuple, optional

Dimensions of the patch window. Syntax: (height, width), or (height,), in which case width = height.

Returns:

array

Feature matrix.

Notes

The feature vector of a pixel i will consist of the stacking of the intensity values of all pixels in the patch centered at i, for all color channels. So, if the input image has d color channels, the dimension of the feature vector of each pixel is (patch_shape[0] * patch_shape[1] * d).

Examples

>>> from pygsp import utils
>>> import skimage
>>> img = skimage.img_as_float(skimage.data.camera()[::2, ::2])
>>> X = utils.extract_patches(img)
pygsp.utils.filterbank_handler(func)[source]
pygsp.utils.graph_array_handler(func)[source]
pygsp.utils.import_classes(names, src, dst)[source]

Import classes in package from their implementation modules.

pygsp.utils.import_functions(names, src, dst)[source]

Import functions in package from their implementation modules.

pygsp.utils.import_modules(names, src, dst)[source]

Import modules in package.

pygsp.utils.loadmat(path)[source]

Load a matlab data file.

Parameters:

path : string

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

Returns:

data : dict

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.repmatline(A, ncol=1, nrow=1)[source]

Repeat the matrix A in a specific manner.

Parameters:

A : ndarray

ncol : int

default is 1

nrow : int

default is 1

Returns:

Ar : ndarray

Examples

>>> from pygsp import utils
>>> x = np.array([[1, 2], [3, 4]])
>>> x
array([[1, 2],
       [3, 4]])
>>> utils.repmatline(x, nrow=2, ncol=3)
array([[1, 1, 1, 2, 2, 2],
       [1, 1, 1, 2, 2, 2],
       [3, 3, 3, 4, 4, 4],
       [3, 3, 3, 4, 4, 4]])
pygsp.utils.rescale_center(x)[source]

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

Parameters:

x : ndarray

Data to be rescaled.

Returns:

r : ndarray

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:

G : Graph or sparse matrix

Graph structure or Laplacian matrix (L)

Returns:

rd : sparse matrix

distance matrix

References

[KRandic93]

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

Symmetrize a square matrix.

Parameters:

W : array_like

Square matrix to be symmetrized

method : string

  • ‘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.

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.]])
>>> 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.]])