pysdic.fem.compute_neighborhood_statistics#

compute_neighborhood_statistics(values, neighborhood, statistics)[source]#

Compute statistics of values over the neighborhood of each node of a graph (vertices, elements, …).

The neighborhood is a matrix of shape \((N, M)\) whose row \(i\) lists the indices of the values used for the node \(i\) (its non-zero columns), as returned by pysdic.fem.compute_neighborhood(), pysdic.fem.compute_vertices_neighborhood() or pysdic.fem.compute_elements_neighborhood() (\(M = N\)), or by pysdic.fem.compute_integration_points_neighborhood() (values defined at \(M\) integration points, statistics computed for each element).

Implemented statistics:

Statistic

Description

mean

Mean of the values of the neighborhood.

median

Median of the values of the neighborhood.

min

Minimum of the values of the neighborhood.

max

Maximum of the values of the neighborhood.

std

Standard deviation (population, ddof=0) of the values.

var

Variance (population, ddof=0) of the values.

Q1

First quartile (25th percentile) of the values.

Q3

Third quartile (75th percentile) of the values.

IQR

Interquartile range Q3 - Q1.

Note

  • Input values will be converted to numpy.float64. A 1D array of shape \((M,)\) is treated as \((M, 1)\). The statistics are computed independently for each of the \(P\) components.

  • The non-finite values are not ignored: a NaN in a neighborhood gives NaN statistics for this node (numpy semantics).

  • A node with an empty neighborhood gets NaN statistics.

Parameters:
  • values (ArrayLike) – An array of shape \((M,)\) or \((M, P)\) with the values.

  • neighborhood (Union[ArrayLike, scipy.sparse.spmatrix]) – A matrix of shape \((N, M)\), the non-zero entries of the row \(i\) being the indices of the values in the neighborhood of the node \(i\).

  • statistics (Union[str, Sequence[str]]) – The name of the statistics to compute, or a sequence of names.

Returns:

For each requested statistic, an array of shape \((N, P)\).

Return type:

Dict[str, numpy.ndarray]

Raises:
  • TypeError – If statistics is not a string or a sequence of strings.

  • ValueError – If a statistic is not implemented, or if the shapes are not consistent.

Examples

Statistics of a temperature defined at the 7 vertices of a disconnected graph, over the vertices within a distance 2.

../../_images/graph_bfs.png

Disconnected graph with 7 vertices.#

 1import numpy
 2import scipy.sparse
 3from pysdic.fem import compute_neighborhood, compute_neighborhood_statistics
 4
 5edges = [(0, 1), (0, 2), (1, 2), (2, 3), (3, 4), (5, 6)]
 6rows, columns = numpy.array(edges).T
 7graph = scipy.sparse.coo_matrix((numpy.ones(len(edges)), (rows, columns)), shape=(7, 7))
 8graph = graph + graph.T
 9
10neighborhood = compute_neighborhood(graph, max_distance=2, self_neighborhood=True)
11temperature = [10.0, 20.0, 30.0, 40.0, 50.0, 60.0, 70.0]
12
13statistics = compute_neighborhood_statistics(temperature, neighborhood, ["min", "max", "mean"])
14print(statistics["min"].ravel())
15print(statistics["max"].ravel())
16print(statistics["mean"].ravel())
[10. 10. 10. 10. 30. 60. 60.]
[40. 40. 50. 50. 50. 70. 70.]
[25. 25. 30. 30. 40. 65. 65.]