metricgraph documentation

class metricgraph.MetricGraph(vertices, edges)

Bases: object

Class that build a MetricGraph object

Parameters:
  • vertices (list | np.ndarray) – vertices coordinates of the graph

  • edges (list | np.ndarray) – connectivity matrix of the graph

distance(point)

Compute distance between point and the graph –> d(p, G). It returns:

  • ret[‘distance’] = distance between point and the graph,

  • ret[‘space_prj’] = projection on the graph in the nd space,

  • ret[‘abscissa’] = abscissa of the projection,

  • ret[‘nearest_edge’] = nearest edge

Parameters:

point (list[float] | np.ndarray[float]) – point to compute distance from

Returns:

dictionary of computed values

Return type:

dict

classmethod generate_graph(n_vertices, n_dim=3, domain_limits=None, minimum_edges=None, directed=False, loop=False, strong_connected=False, weak_connected=True, acyclic=True)

Generate MetricGraph object, given its properties.

Parameters:
  • n_vertices (int) – number of vertices of the graph

  • n_dim (int) – dimension of the space

  • domain_limits (list | np.ndarray) – limits of the bounding box containing the graph

  • minimum_edges (int) – number of minimum edges tried to be added (it may happen that the maximum number of possible edges is smaller)

  • directed (bool) – if True directed graph are considered

  • loop (bool) – if True and directed=True self loop are allowed

  • strong_connected (bool) – if True the graph has to be strongly connected (notice that the definition makes sense only for directed graph)

  • weak_connected (bool) – if True the graph has to be weakly connected in case of directed graph or just connected in case of undirected graph

  • acyclic (bool) – if True the graph has to be acyclic

Returns:

MetricGraph object

Return type:

MetricGraph

get_boundary_coordinates()

Compute the coordinates of boundary vertices. Boundary vertices are nodes with at most one incident edge.

Returns:

Array of boundary vertex coordinates

Return type:

np.ndarray

get_boundary_mask()

Compute naturally boundary vertices (nodes with at maximum one connection, incoming or outgoing).

Returns:

boolean numpy array

Return type:

np.ndarray[bool]

get_edges_length()

Compute length of edges between vertices.

Returns:

Edge length

Return type:

np.ndarray

get_graph_laplacian(w1=None, w2=None)

Compute graph laplacian. Consider the edge as undirected. Ignore self loops.

Parameters:
  • w1 (function) – optional weight function, if used L_jj = L_jj * w1(edges_length[i])

  • w2 (function) – optional weight function, if used L_jk = L_jk * w2(edges_length[i]), j != k

Returns:

numpy array containing the graph laplacian

Return type:

np.ndarray

get_inlets()

Compute inlet vertices (nodes with no incoming connections).

Returns:

numpy array of inlet vertices

Return type:

np.ndarray[int]

get_outlets()

Compute outlet vertices (nodes with no outgoing connections).

Returns:

numpy array of outlet vertices

Return type:

np.ndarray[int]

get_properties()

Compute and return properties of the graph.

Return type:

GraphProperties

classmethod load(name)

Read graph object from file.

Parameters:

name (str) – path and name of the file (without extension)

Returns:

Graph read from file

Return type:

MetricGraph

plot(color='blue', directed=False, interactive=False, line_widths=2, show_axis=False, title='', vertices_label=True)

Plot image with graph representation.

Parameters:
  • color (str) – color to use for plotting the edges

  • directed (bool) – if True, plot the graph using arrows

  • interactive (bool) – if True, display the plot in interactive mode

  • line_widths (int) – width of the lines

  • show_axis (bool) – if True, display the axis of the figure

  • title (str) – title of the plot

  • vertices_label (bool) – if True, label the vertices of the graph

refine_graph(n_refinements)

Refine the graph adding internal vertices, return the refined graph.

Parameters:

n_refinements (int) – number of internal vertices to add for each edge

Returns:

refined graph

Return type:

MetricGraph

save(name)

Save graph object to file.

Parameters:

name (str) – path and name of the file (without extension)

class metricgraph.WeightedGraph(vertices, edges)

Bases: MetricGraph

Extend MetricGraph adding weights on vertices and edges

Parameters:
  • vertices (list | np.ndarray) – vertices coordinates of the graph

  • edges (list | np.ndarray) – connectivity matrix of the graph

get_weight(name)

Get a value that represent a weight, given its name

Parameters:

name (str) – name of the weight

Returns:

value of the weight

Return type:

np.ndarray

get_weights_name()

Get all the weights’ name

Returns:

list of names

Return type:

list[str]

is_nodal_weight(name)

Check if a value represent a nodal weight

Parameters:

name (str) – name of the weight

Returns:

True if a value represent a nodal weight, else False

Return type:

bool

plot_weight(name, dimension=None, interactive=False, legend_label='', max_value=None, min_value=None, palette='viridis', show_axis=False, title='')

Plot the graph with weights on vertices or edges

Parameters:
  • name (str) – name of the weight

  • dimension (int | float) – dimension of plotted weight. For edges is the linewidth (default 3), for vertices is the nodal size (default 100)

  • interactive (bool) – if True, display the plot in interactive mode

  • legend_label (str) – label of the colormap

  • max_value (float) – maximum value to consider for the colormap, if None it’s the actual maximum

  • min_value (float) – minimum value to consider for the colormap, if None it’s the actual minimum

  • palette (str) – colormap palette

  • show_axis (bool) – if True, display the axis of the figure

  • title (str) – title of the plot

classmethod read_vtk(filename)

Read the WeightedGraph from a vtk file

Parameters:

filename (str) – name of the file (without extension)

Returns:

WeightedGraph

Return type:

WeightedGraph

set_edges_weight(name, value)

Add a weight on the edges

Parameters:
  • name (str) – name of the weight

  • value (list | np.ndarray) – value of the weight

set_vertices_weight(name, value)

Add a weight on the vertices

Parameters:
  • name (str) – name of the weight

  • value (list | np.ndarray) – value of the weight

write_vtk(filename)

Write the WeightedGraph on a vtk file

Parameters:

filename (str) – name of the file (without extension)