PointUndirectedGraph

class menpo.shape.PointUndirectedGraph(points, adjacency_array, copy=True)[source]

Bases: PointGraph, UndirectedGraph

Class for defining an Undirected Graph with geometry.

Parameters:
  • points (ndarray) – The array of point locations.
  • adjacency_array ((n_edges, 2, ) ndarray) –

    The adjacency array of the graph, i.e. an array containing the sets of the graph’s edges. The numbering of vertices is assumed to start from 0. For example

    |---0---|        adjacency_array = ndarray([[0, 1],
    |       |                                   [0, 2],
    |       |                                   [1, 2],
    1-------2                                   [1, 3],
    |       |                                   [2, 4],
    |       |                                   [3, 4],
    3-------4                                   [3, 5]])
    |
    5
    
  • copy (bool, optional) – If False, the adjacency_list will not be copied on assignment.
Raises:

ValueError – A point for each graph vertex needs to be passed. Got n_points points instead of n_vertices.

_view_2d(figure_id=None, new_figure=False, image_view=True, render_lines=True, line_colour='r', line_style='-', line_width=1.0, render_markers=True, marker_style='o', marker_size=20, marker_face_colour='k', marker_edge_colour='k', marker_edge_width=1.0, render_axes=True, axes_font_name='sans-serif', axes_font_size=10, axes_font_style='normal', axes_font_weight='normal', axes_x_limits=None, axes_y_limits=None, figure_size=(10, 8), label=None)

Visualization of the pointgraph in 2D.

Returns:
  • figure_id (object, optional) – The id of the figure to be used.
  • new_figure (bool, optional) – If True, a new figure is created.
  • image_view (bool, optional) – If True the PointGraph will be viewed as if it is in the image coordinate system.
  • render_lines (bool, optional) – If True, the edges will be rendered.
  • line_colour (See Below, optional) – The colour of the lines. Example options:
    {r, g, b, c, m, k, w}
    or
    (3, ) ndarray
    
  • line_style ({-, --, -., :}, optional) – The style of the lines.
  • line_width (float, optional) – The width of the lines.
  • render_markers (bool, optional) – If True, the markers will be rendered.
  • marker_style (See Below, optional) – The style of the markers. Example options
    {., ,, o, v, ^, <, >, +, x, D, d, s, p, *, h, H, 1, 2, 3, 4, 8}
    
  • marker_size (int, optional) – The size of the markers in points^2.
  • marker_face_colour (See Below, optional) – The face (filling) colour of the markers. Example options
    {r, g, b, c, m, k, w}
    or
    (3, ) ndarray
    
  • marker_edge_colour (See Below, optional) – The edge colour of the markers. Example options
    {r, g, b, c, m, k, w}
    or
    (3, ) ndarray
    
  • marker_edge_width (float, optional) – The width of the markers’ edge.
  • render_axes (bool, optional) – If True, the axes will be rendered.
  • axes_font_name (See Below, optional) – The font of the axes. Example options
    {serif, sans-serif, cursive, fantasy, monospace}
    
  • axes_font_size (int, optional) – The font size of the axes.
  • axes_font_style ({normal, italic, oblique}, optional) – The font style of the axes.
  • axes_font_weight (See Below, optional) – The font weight of the axes. Example options
    {ultralight, light, normal, regular, book, medium, roman,
    semibold, demibold, demi, bold, heavy, extra bold, black}
    
  • axes_x_limits ((float, float) tuple or None, optional) – The limits of the x axis.
  • axes_y_limits ((float, float) tuple or None, optional) – The limits of the y axis.
  • figure_size ((float, float) tuple or None, optional) – The size of the figure in inches.
  • label (str, optional) – The name entry in case of a legend.
Returns:viewer (PointGraphViewer2d) – The viewer object.
_view_landmarks_2d(group=None, with_labels=None, without_labels=None, figure_id=None, new_figure=False, image_view=True, render_lines=True, line_colour=None, line_style='-', line_width=1, render_markers=True, marker_style='o', marker_size=20, marker_face_colour=None, marker_edge_colour=None, marker_edge_width=1.0, render_numbering=False, numbers_horizontal_align='center', numbers_vertical_align='bottom', numbers_font_name='sans-serif', numbers_font_size=10, numbers_font_style='normal', numbers_font_weight='normal', numbers_font_colour='k', render_legend=False, legend_title='', legend_font_name='sans-serif', legend_font_style='normal', legend_font_size=10, legend_font_weight='normal', legend_marker_scale=None, legend_location=2, legend_bbox_to_anchor=(1.05, 1.0), legend_border_axes_pad=None, legend_n_columns=1, legend_horizontal_spacing=None, legend_vertical_spacing=None, legend_border=True, legend_border_padding=None, legend_shadow=False, legend_rounded_corners=False, render_axes=False, axes_font_name='sans-serif', axes_font_size=10, axes_font_style='normal', axes_font_weight='normal', axes_x_limits=None, axes_y_limits=None, figure_size=(10, 8))

Visualize the landmarks. This method will appear on the Image as view_landmarks if the Image is 2D.

Parameters:
  • group (str or``None`` optional) – The landmark group to be visualized. If None and there are more than one landmark groups, an error is raised.
  • with_labels (None or str or list of str, optional) – If not None, only show the given label(s). Should not be used with the without_labels kwarg.
  • without_labels (None or str or list of str, optional) – If not None, show all except the given label(s). Should not be used with the with_labels kwarg.
  • figure_id (object, optional) – The id of the figure to be used.
  • new_figure (bool, optional) – If True, a new figure is created.
  • image_view (bool, optional) – If True the PointCloud will be viewed as if it is in the image coordinate system.
  • render_lines (bool, optional) – If True, the edges will be rendered.
  • line_colour (See Below, optional) –

    The colour of the lines. Example options:

    {r, g, b, c, m, k, w}
    or
    (3, ) ndarray
    
  • line_style ({-, --, -., :}, optional) – The style of the lines.
  • line_width (float, optional) – The width of the lines.
  • render_markers (bool, optional) – If True, the markers will be rendered.
  • marker_style (See Below, optional) –

    The style of the markers. Example options

    {., ,, o, v, ^, <, >, +, x, D, d, s, p, *, h, H, 1, 2, 3, 4, 8}
    
  • marker_size (int, optional) – The size of the markers in points^2.
  • marker_face_colour (See Below, optional) –

    The face (filling) colour of the markers. Example options

    {r, g, b, c, m, k, w}
    or
    (3, ) ndarray
    
  • marker_edge_colour (See Below, optional) –

    The edge colour of the markers. Example options

    {r, g, b, c, m, k, w}
    or
    (3, ) ndarray
    
  • marker_edge_width (float, optional) – The width of the markers’ edge.
  • render_numbering (bool, optional) – If True, the landmarks will be numbered.
  • numbers_horizontal_align ({center, right, left}, optional) – The horizontal alignment of the numbers’ texts.
  • numbers_vertical_align ({center, top, bottom, baseline}, optional) – The vertical alignment of the numbers’ texts.
  • numbers_font_name (See Below, optional) –

    The font of the numbers. Example options

    {serif, sans-serif, cursive, fantasy, monospace}
    
  • numbers_font_size (int, optional) – The font size of the numbers.
  • numbers_font_style ({normal, italic, oblique}, optional) – The font style of the numbers.
  • numbers_font_weight (See Below, optional) –

    The font weight of the numbers. Example options

    {ultralight, light, normal, regular, book, medium, roman,
    semibold, demibold, demi, bold, heavy, extra bold, black}
    
  • numbers_font_colour (See Below, optional) –

    The font colour of the numbers. Example options

    {r, g, b, c, m, k, w}
    or
    (3, ) ndarray
    
  • render_legend (bool, optional) – If True, the legend will be rendered.
  • legend_title (str, optional) – The title of the legend.
  • legend_font_name (See below, optional) –

    The font of the legend. Example options

    {serif, sans-serif, cursive, fantasy, monospace}
    
  • legend_font_style ({normal, italic, oblique}, optional) – The font style of the legend.
  • legend_font_size (int, optional) – The font size of the legend.
  • legend_font_weight (See Below, optional) –

    The font weight of the legend. Example options

    {ultralight, light, normal, regular, book, medium, roman,
    semibold, demibold, demi, bold, heavy, extra bold, black}
    
  • legend_marker_scale (float, optional) – The relative size of the legend markers with respect to the original
  • legend_location (int, optional) –

    The location of the legend. The predefined values are:

    ‘best’ 0
    ‘upper right’ 1
    ‘upper left’ 2
    ‘lower left’ 3
    ‘lower right’ 4
    ‘right’ 5
    ‘center left’ 6
    ‘center right’ 7
    ‘lower center’ 8
    ‘upper center’ 9
    ‘center’ 10
  • legend_bbox_to_anchor ((float, float) tuple, optional) – The bbox that the legend will be anchored.
  • legend_border_axes_pad (float, optional) – The pad between the axes and legend border.
  • legend_n_columns (int, optional) – The number of the legend’s columns.
  • legend_horizontal_spacing (float, optional) – The spacing between the columns.
  • legend_vertical_spacing (float, optional) – The vertical space between the legend entries.
  • legend_border (bool, optional) – If True, a frame will be drawn around the legend.
  • legend_border_padding (float, optional) – The fractional whitespace inside the legend border.
  • legend_shadow (bool, optional) – If True, a shadow will be drawn behind legend.
  • legend_rounded_corners (bool, optional) – If True, the frame’s corners will be rounded (fancybox).
  • render_axes (bool, optional) – If True, the axes will be rendered.
  • axes_font_name (See Below, optional) –

    The font of the axes. Example options

    {serif, sans-serif, cursive, fantasy, monospace}
    
  • axes_font_size (int, optional) – The font size of the axes.
  • axes_font_style ({normal, italic, oblique}, optional) – The font style of the axes.
  • axes_font_weight (See Below, optional) –

    The font weight of the axes. Example options

    {ultralight, light, normal, regular, book, medium, roman,
    semibold,demibold, demi, bold, heavy, extra bold, black}
    
  • axes_x_limits ((float, float) tuple or None optional) – The limits of the x axis.
  • axes_y_limits ((float, float) tuple or None optional) – The limits of the y axis.
  • figure_size ((float, float) tuple or None optional) – The size of the figure in inches.
Raises:
  • ValueError – If both with_labels and without_labels are passed.
  • ValueError – If the landmark manager doesn’t contain the provided group label.
as_vector(**kwargs)

Returns a flattened representation of the object as a single vector.

Returns:vector ((N,) ndarray) – The core representation of the object, flattened into a single vector. Note that this is always a view back on to the original object, but is not writable.
bounding_box()

Return the bounding box of this PointCloud as a directed graph. The the first point (0) will be nearest the origin for an axis aligned Pointcloud. In the case of an image, this ordering would appear as:

0<--3
|   ^
|   |
v   |
1-->2
Returns:bounding_box (PointDirectedGraph) – The axis aligned bounding box of the PointCloud.
bounds(boundary=0)

The minimum to maximum extent of the PointCloud. An optional boundary argument can be provided to expand the bounds by a constant margin.

Parameters:boundary (float) – A optional padding distance that is added to the bounds. Default is 0, meaning the max/min of tightest possible containing square/cube/hypercube is returned.
Returns:
  • min_b ((n_dims,) ndarray) – The minimum extent of the PointCloud and boundary along each dimension
  • max_b ((n_dims,) ndarray) – The maximum extent of the PointCloud and boundary along each dimension
centre()

The mean of all the points in this PointCloud (centre of mass).

Returns:centre ((n_dims) ndarray) – The mean of this PointCloud’s points.
centre_of_bounds()

The centre of the absolute bounds of this PointCloud. Contrast with centre(), which is the mean point position.

Returns:centre (n_dims ndarray) – The centre of the bounds of this PointCloud.
copy()

Generate an efficient copy of this object.

Note that Numpy arrays and other Copyable objects on self will be deeply copied. Dictionaries and sets will be shallow copied, and everything else will be assigned (no copy will be made).

Classes that store state other than numpy arrays and immutable types should overwrite this method to ensure all state is copied.

Returns:type(self) – A copy of this object
distance_to(pointcloud, **kwargs)

Returns a distance matrix between this PointCloud and another. By default the Euclidean distance is calculated - see scipy.spatial.distance.cdist for valid kwargs to change the metric and other properties.

Parameters:pointcloud (PointCloud) – The second pointcloud to compute distances between. This must be of the same dimension as this PointCloud.
Returns:distance_matrix ((n_points, n_points) ndarray) – The symmetric pairwise distance matrix between the two PointClouds s.t. distance_matrix[i, j] is the distance between the i’th point of this PointCloud and the j’th point of the input PointCloud.
find_all_paths(start, end, path=[])

Returns a list of lists with all the paths (without cycles) found from start vertex to end vertex.

Parameters:
  • start (int) – The vertex from which the paths start.
  • end (int) – The vertex from which the paths end.
  • path (list, optional) – An existing path to append to.
Returns:

paths (list of list) – The list containing all the paths from start to end.

find_path(start, end, path=None)

Returns a list with the first path (without cycles) found from start vertex to end vertex.

Parameters:
  • start (int) – The vertex from which the path starts.
  • end (int) – The vertex from which the path ends.
  • path (list, optional) – An existing path to append to.
Returns:

path (list) – The path’s vertices.

find_shortest_path(start, end, path=None)

Returns a list with the shortest path (without cycles) found from start vertex to end vertex.

Parameters:
  • start (int) – The vertex from which the path starts.
  • end (int) – The vertex from which the path ends.
  • path (list, optional) – An existing path to append to.
Returns:

path (list) – The shortest path’s vertices.

from_mask(mask)[source]

A 1D boolean array with the same number of elements as the number of points in the PointUndirectedGraph. This is then broadcast across the dimensions of the PointUndirectedGraph and returns a new PointUndirectedGraph containing only those points that were True in the mask.

Parameters:mask ((n_points,) ndarray) – 1D array of booleans
Returns:pointgraph (PointUndirectedGraph) – A new pointgraph that has been masked.
Raises:ValueError – Mask must have same number of points as pointgraph.
from_vector(vector)

Build a new instance of the object from it’s vectorized state.

self is used to fill out the missing state required to rebuild a full object from it’s standardized flattened state. This is the default implementation, which is which is a deepcopy of the object followed by a call to from_vector_inplace(). This method can be overridden for a performance benefit if desired.

Parameters:vector ((n_parameters,) ndarray) – Flattened representation of the object.
Returns:object (type(self)) – An new instance of this class.
from_vector_inplace(vector)

Updates the points of this PointCloud in-place with the reshaped points from the provided vector. Note that the vector should have the form [x0, y0, x1, y1, ....., xn, yn] for 2D.

Parameters:vector ((n_points,) ndarray) – The vector from which to create the points’ array.
get_adjacency_matrix()

Returns the adjacency matrix of the graph, i.e. the boolean ndarray that is True and False if there is an edge connecting the two vertices or not respectively.

Type:(n_vertices, n_vertices, ) ndarray
h_points()

Convert poincloud to a homogeneous array: (n_dims + 1, n_points)

Type:type(self)
has_cycles()

Whether the graph has at least on cycle.

Returns:has_cycles (bool) – True if it has at least one cycle.
is_edge(vertex_1, vertex_2)

Returns whether there is an edge between the provided vertices.

Parameters:
  • vertex_1 (int) – The first selected vertex.
  • vertex_2 (int) – The second selected vertex.
Returns:

is_edge (bool) – True if there is an edge.

Raises:

ValueError – The vertex must be between 0 and {n_vertices-1}.

is_tree()

Checks if the graph is tree.

Returns:is_true (bool) – If the graph is a tree.
minimum_spanning_tree(weights, root_vertex)

Returns the minimum spanning tree given weights to the graph’s edges using Kruskal’s algorithm.

Parameters:
  • weights ((n_vertices, n_vertices, ) ndarray) – A matrix of the same size as the adjacency matrix that attaches a weight to each edge of the undirected graph.
  • root_vertex (int) – The vertex that will be set as root in the output MST.
Returns:

mst (Tree) – The computed minimum spanning tree.

Raises:
  • ValueError – Provided graph is not an UndirectedGraph.
  • ValueError – Asymmetric weights provided.
n_neighbours(vertex)

Returns the number of neighbours of the selected vertex.

Parameters:vertex (int) – The selected vertex.
Returns:n_neighbours (int) – The number of neighbours.
Raises:ValueError – The vertex must be between 0 and {n_vertices-1}.
n_paths(start, end)

Returns the number of all the paths (without cycles) existing from start vertex to end vertex.

Parameters:
  • start (int) – The vertex from which the paths start.
  • end (int) – The vertex from which the paths end.
Returns:

paths (int) – The paths’ numbers.

neighbours(vertex)

Returns the neighbours of the selected vertex.

Parameters:vertex (int) – The selected vertex.
Returns:neighbours (list) – The list of neighbours.
Raises:ValueError – The vertex must be between 0 and {n_vertices-1}.
norm(**kwargs)

Returns the norm of this PointCloud. This is a translation and rotation invariant measure of the point cloud’s intrinsic size - in other words, it is always taken around the point cloud’s centre.

By default, the Frobenius norm is taken, but this can be changed by setting kwargs - see numpy.linalg.norm for valid options.

Returns:norm (float) – The norm of this PointCloud
range(boundary=0)

The range of the extent of the PointCloud.

Parameters:boundary (float) – A optional padding distance that is used to extend the bounds from which the range is computed. Default is 0, no extension is performed.
Returns:range ((n_dims,) ndarray) – The range of the PointCloud extent in each dimension.
tojson()

Convert this PointGraph to a dictionary representation suitable for inclusion in the LJSON landmark format.

Returns:json (dict) – Dictionary with points and connectivity keys.
view_widget(popup=False, browser_style='buttons', figure_size=(10, 8))

Visualization of the PointGraph using the visualize_pointclouds widget.

Parameters:
  • popup (bool, optional) – If True, the widget will be rendered in a popup window.
  • browser_style ({buttons, slider}, optional) – It defines whether the selector of the PointGraph objects will have the form of plus/minus buttons or a slider.
  • figure_size ((int, int) tuple, optional) – The initial size of the rendered figure.
has_landmarks

Whether the object has landmarks.

Type:bool
landmarks

The landmarks object.

Type:LandmarkManager
n_dims

The number of dimensions in the pointcloud.

Type:int
n_edges

Returns the number of the graph edges.

Type:int
n_landmark_groups

The number of landmark groups on this object.

Type:int
n_parameters

The length of the vector that this object produces.

Type:int
n_points

The number of points in the pointcloud.

Type:int
n_vertices

Returns the number of the graph vertices.

Type:int