Graph

Graph Creation.

Contains the functions used in EEGrasp to create Graphs

eegrasp.graph.compute_graph(W=None, epsilon=0.5, sigma=0.1, distances=None, graph=None, coordinates=None, method='enn', k=5)[source]
Parameters:
Wnumpy ndarray | None

If W is passed, then the graph is computed. Otherwise the graph will be computed with self.W. W should correspond to a non-sparse 2-D array. If None, the function will use the distance matrix computed in the instance of the class (self.W).

epsilonfloat

Any distance greater than epsilon will be set to zero on the adjacency matrix.

sigmafloat

Sigma parameter for the gaussian kernel.

methodstr, default=”enn”

Method to determine graph connectivity. - “enn”: Use ε-neighbourhood with Gaussian kernel and distance thresholding. - “knn”: Use K-Nearest Neighbors. Each node connects to its k nearest neighbors with Gaussian weighting.

kint, default=5

Number of neighbors to connect when using method=”knn”. Ignored if method=”epsilon”.

Returns:
G: PyGSP2 Graph object.
eegrasp.graph.fit_epsilon(missing_idx: int | list[int] | tuple[int], data=None, distances=None, sigma=0.1)[source]

Find the best distance to use as threshold.

Parameters:
missing_idxint

Index of the missing channel. Not optional.

datandarray | None

2d array of channels by samples. If None, the function will use the data computed in the instance of the class (self.data). Default is None.

distancesndarray | None.

Unthresholded distance matrix (2-dimensional array). It can be passed to the instance of the class or as an argument of the method. If None, the function will use the distance computed in the instance of the class (self.distances). Default is None.

sigmafloat

Parameter of the Gaussian Kernel transformation. Default is 0.1.

Returns:
resultsdict

Dictionary containing the error, signal, best_epsilon and epsilon values.

Notes

It will iterate through all the unique values of the distance matrix. data : 2-dimensional array. The first dim. is Channels and second is time. It can be passed to the instance class or the method

eegrasp.graph.fit_sigma(missing_idx: int | list[int] | tuple[int], data=None, distances=None, epsilon=0.5, min_sigma=0.1, max_sigma=1.0, step=0.1)[source]

Find the best parameter for the gaussian kernel.

Parameters:
missing_idxint | list | tuple

Index of the missing channel.

datandarray | None

2d array of channels by samples. If None, the function will use the data computed in the instance of the class (self.data).

distancesndarray | None

Distance matrix (2-dimensional array). It can be passed to the instance of the class or as an argument of the method. If None, the function will use the distance computed in the instance of the class (self.distances).

epsilonfloat

Maximum distance to threshold the array. Default is 0.5.

min_sigmafloat

Minimum value for the sigma parameter. Default is 0.1.

max_sigmafloat

Maximum value for the sigma parameter. Default is 1.

stepfloat

Step for the sigma parameter. Default is 0.1.

Notes

Look for the best parameter of sigma for the gaussian kernel. This is done by interpolating a channel and comparing the interpolated data to the real data. After finding the parameter the graph is saved and computed in the instance class. The distance threshold is maintained.

eegrasp.graph.gaussian_kernel(x, sigma=0.1)[source]

Gaussian Kernel Weighting function.

Notes

This function is supposed to be used in the PyGSP2 module but is repeated here since there is an error in the available version of the toolbox.

References

      1. Shuman, S. K. Narang, P. Frossard, A. Ortega and

P. Vandergheynst, “The emerging field of signal processing on graphs: Extending high-dimensional data analysis to networks and other irregular domains,” in IEEE Signal Processing Magazine, vol. 30, no. 3, pp. 83-98, May 2013, doi: 10.1109/MSP.2012.2235192.

eegrasp.graph.learn_graph(Z=None, a=0.1, b=0.1, gamma=0.04, maxiter=1000, w_max=inf, mode='Average', data=None, **kwargs)[source]

Learn the graph based on smooth signals.

Parameters:
Zndarray

Distance between the nodes. If not passed, the function will try to compute the euclidean distance using self.data. If self.data is a 2d array it will compute the euclidean distance between the channels. If the data is a 3d array it will compute a Z matrix per trial, assuming the first dimension in data is trials/epochs. Depending on the mode parameter, the function will average distance matrizes and learn the graph on the average distance or return a collection of adjacency matrices. Default is None.

afloat

Parameter for the graph learning algorithm, this controls the weights of the learned graph. Bigger a -> bigger weights in W. Default is 0.1.

bfloat

Parameter for the graph learning algorithm, this controls the density of the learned graph. Bigger b -> more dense W. Default is 0.1.

modestring

Options are: ‘Average’, ‘Trials’. If ‘average’, the function returns a single W and Z. If ‘Trials’ the function returns a generator list of Ws and Zs. Default is ‘Average’.

datandarray | None

2d array of channels by samples. If None, the function will use the data computed in the instance of the class (self.data).

Returns:
Wndarray

Weighted adjacency matrix or matrices depending on mode parameter used. If run in ‘Trials’ mode then Z is a 3d array where the first dim corresponds to trials.

Zndarray.

Used distance matrix or matrices depending on mode parameter used. If run in ‘Trials’ mode then Z is a 3d array where the first dim corresponds to trials.