geometry#

Functions for measuring geometric properties of grains.

Functions#

bounding_box_cartesian_points_float(...)

Calculate the bounding box from a set of points.

bounding_box_cartesian_points_integer(...)

Calculate the bounding box from a set of points.

do_points_in_arrays_touch(→ tuple[bool, ...)

Check if any points in two arrays are touching.

calculate_shortest_branch_distances(...)

Calculate the shortest distances between branches emanating from nodes.

connect_best_matches(→ numpy.typing.NDArray[numpy.int32])

Connect the branches between node pairs that have been deemed to be best matches.

find_branches_for_nodes(→ dict[int, ...)

Locate branch starting positions for each node in a network.

Module Contents#

geometry.bounding_box_cartesian_points_float(points: numpy.typing.NDArray[numpy.number]) tuple[numpy.float64, numpy.float64, numpy.float64, numpy.float64]#

Calculate the bounding box from a set of points.

Parameters:

points (npt.NDArray[np.number]) – Nx2 numpy array of points.

Returns:

Tuple of (min_x, min_y, max_x, max_y).

Return type:

Tuple[np.float64, np.float64, np.float64, np.float64]

Raises:

ValueError – If the input array is not Nx2.

geometry.bounding_box_cartesian_points_integer(points: numpy.typing.NDArray[numpy.number]) tuple[numpy.int32, numpy.int32, numpy.int32, numpy.int32]#

Calculate the bounding box from a set of points.

Parameters:

points (npt.NDArray[np.number]) – Nx2 numpy array of points.

Returns:

Tuple of (min_x, min_y, max_x, max_y).

Return type:

Tuple[np.int32, np.int32, np.int32, np.int32]

Raises:

ValueError – If the input array is not Nx2.

geometry.do_points_in_arrays_touch(points1: numpy.typing.NDArray[numpy.int32], points2: numpy.typing.NDArray[numpy.int32]) tuple[bool, numpy.typing.NDArray[numpy.int32] | None, numpy.typing.NDArray[numpy.int32] | None]#

Check if any points in two arrays are touching.

Parameters:
  • points1 (npt.NDArray[np.int32]) – Nx2 numpy array of points.

  • points2 (npt.NDArray[np.int32]) – Mx2 numpy array of points.

Returns:

True if any points in the two arrays are touching, False otherwise, followed by the first touching point pair that was found. If no points are touching, the second and third elements of the tuple will be None.

Return type:

tuple[bool, npt.NDArray[np.int32] | None, npt.NDArray[np.int32] | None]

Raises:

ValueError – If the input arrays are not Nx2 and Mx2.

geometry.calculate_shortest_branch_distances(nodes_with_branch_starting_coords: dict[int, list[numpy.typing.NDArray[numpy.int32]]], whole_skeleton_graph: networkx.classes.graph.Graph) tuple[numpy.typing.NDArray[numpy.number], numpy.typing.NDArray[numpy.int32], numpy.typing.NDArray[numpy.number]]#

Calculate the shortest distances between branches emanating from nodes.

Parameters:
  • nodes_with_branch_starting_coords (dict[int, list[npt.NDArray[np.int32]]]) – Dictionary where the key is the node number and the value is an Nx2 numpy array of the starting coordinates of its branches.

  • whole_skeleton_graph (networkx.classes.graph.Graph) – Networkx graph representing the whole network.

Returns:

  • NxN numpy array of shortest distances between every node pair. Indexes of this array represent the nodes.

Eg for a 3x3 matrix, there are 3 nodes being compared with each other. This matrix is diagonally symmetric and the diagonal values are 0 since a node is always 0 distance from itself. - NxNx2 numpy array of indexes of the best branches to connect between each node pair. Eg for node 1 and 3, the closest branches might be indexes 2 and 4, so the value at [1, 3] would be [2, 4]. - NxNx2x2 numpy array of the coordinates of the branches to connect between each node pair. Eg for node 1 and 3, the closest branches might be at coordinates [2, 3] and [4, 5], so the value at [1, 3] would be [[2, 3], [4, 5]].

Return type:

Tuple[npt.NDArray[np.number], npt.NDArray[np.int32], npt.NDArray[np.int32]]

geometry.connect_best_matches(network_array_representation: numpy.typing.NDArray[numpy.int32], whole_skeleton_graph: networkx.classes.graph.Graph, match_indexes: numpy.typing.NDArray[numpy.int32], shortest_distances_between_nodes: numpy.typing.NDArray[numpy.number], shortest_distances_branch_indexes: numpy.typing.NDArray[numpy.int32], emanating_branch_starts_by_node: dict[int, list[numpy.typing.NDArray[numpy.int32]]], extend_distance: float = -1) numpy.typing.NDArray[numpy.int32]#

Connect the branches between node pairs that have been deemed to be best matches.

Parameters:
  • network_array_representation (npt.NDArray[np.int32]) – 2D numpy array representing the network using integers to represent branches, nodes etc.

  • whole_skeleton_graph (networkx.classes.graph.Graph) – Networkx graph representing the whole network.

  • match_indexes (npt.NDArray[np.int32]) – Nx2 numpy array of indexes of the best matching nodes. Eg: np.array([[1, 0], [2, 3]]) means that the best matching nodes are node 1 and node 0, and node 2 and node 3.

  • shortest_distances_between_nodes (npt.NDArray[np.number]) – NxN numpy array of shortest distances between every node pair. Index positions indicate which node it’s referring to, so index 2, 3 will be the shortest distance between nodes 2 and 3. Values on the diagonal will be 0 because the shortest distance between a node and itself is 0. Eg: np.array([[0.0, 6.0], [6.0, 0.0]]) means that the shortest distance between node 0 and node 1 is 6.0.

  • shortest_distances_branch_indexes (npt.NDArray[np.int32]) – NxNx2 numpy array of indexes of the branches to connect between the best matching nodes. Not entirely sure what it does so won’t attempt to explain more to avoid confusion.

  • emanating_branch_starts_by_node (dict[int, list[npt.NDArray[np.int32]]]) –

    Dictionary where the key is the node number and the value is an Nx2 numpy array of the starting coordinates of the branches emanating from that node. Rather self-explanatory. Eg: ```python {

    0: [np.array([6, 1]), np.array([7, 3]), np.array([8, 1])], 1: [np.array([6, 11]), np.array([7, 9]), np.array([8, 11])],

    }, ```.

  • extend_distance (float) – The distance to extend the branches to connect. If the shortest distance between two nodes is less than or equal to this distance, the branches will be connected. If -1, the branches will be connected regardless of distance.

Returns:

2D numpy array representing the network using integers to represent branches, nodes etc.

Return type:

npt.NDArray[np.int32]

geometry.find_branches_for_nodes(network_array_representation: numpy.typing.NDArray[numpy.int32], labelled_nodes: numpy.typing.NDArray[numpy.int32], labelled_branches: numpy.typing.NDArray[numpy.int32]) dict[int, list[numpy.typing.NDArray[numpy.int32]]]#

Locate branch starting positions for each node in a network.

Parameters:
  • network_array_representation (npt.NDArray[np.int32]) – 2D numpy array representing the network using integers to represent branches, nodes etc.

  • labelled_nodes (npt.NDArray[np.int32]) – 2D numpy array representing the network using integers to represent nodes.

  • labelled_branches (npt.NDArray[np.int32]) – 2D numpy array representing the network using integers to represent branches.

Returns:

Dictionary where the key is the node number and the value is an Nx2 numpy array of the starting coordinates of the branches emanating from that node.

Return type:

dict[int, list[npt.NDArray[np.int32]]]