From a7bb9a4fa7d331eff55367c020280a4ebf594422 Mon Sep 17 00:00:00 2001 From: jan Date: Sun, 7 Feb 2021 19:28:37 -0800 Subject: [PATCH] modify function docstring style --- adze/boundary.py | 29 +++++++++++++++++++---------- adze/moments.py | 24 +++++++++++++++--------- adze/shapes.py | 32 ++++++++++++++++++++++---------- adze/split.py | 23 ++++++++++++++--------- adze/test/test_voxel.py | 7 ++++--- adze/transform.py | 38 +++++++++++++++++++++++++------------- adze/util.py | 34 ++++++++++++++++++++++------------ adze/voxel.py | 29 ++++++++++++++++------------- 8 files changed, 137 insertions(+), 79 deletions(-) diff --git a/adze/boundary.py b/adze/boundary.py index 84205db..cf3a346 100644 --- a/adze/boundary.py +++ b/adze/boundary.py @@ -20,10 +20,13 @@ def planes_from_boundary(vertices: numpy.ndarray, Generate a list of planes (normal, signed distance) from the faces of a boundary- representation polyhedron. - :param vertices: Nx3 ndarray specifying the polyhedron's vertex positions. - :param faces: List of 1d ndarrays. Each ndarray is a list of vertex indices, - specifying a face. - :return: List of planes, represented by (normal vector, signed distance to origin) tuples. + Args: + vertices: Nx3 ndarray specifying the polyhedron's vertex positions. + faces: List of 1d ndarrays. Each ndarray is a list of vertex indices, + specifying a face. + + Returns: + List of planes, represented by (normal vector, signed distance to origin) tuples. ''' planes = [] for face in faces: @@ -44,10 +47,13 @@ def build_polyhedron(vertices: numpy.ndarray, faces: numpy.ndarray) -> poly_t: Generate a 3-verted-connected Schlegel diagram representation corresponding to the provided boundary-representation polyhedron. - :param vertices: Nx3 ndarray specifying the polyhedron's vertex positions. - :param faces: List of 1d ndarrays. Each ndarray is a list of vertex indices, - specifying a face. - :return: Schelgel-representation polyhedron + Args: + vertices: Nx3 ndarray specifying the polyhedron's vertex positions. + faces: List of 1d ndarrays. Each ndarray is a list of vertex indices, + specifying a face. + + Returns: + Schelgel-representation polyhedron ''' vertices = numpy.array(vertices, dtype=float) faces = [numpy.array(face, dtype=int) for face in faces] @@ -263,8 +269,11 @@ def boundary_from_poly(poly: poly_t) -> Dict: Generate a boundary represeentation corresponding to the provided 3-vertex-connected Schlegel diagram. - :param poly: Polyhedron to convert, in Schlegel representation. - :return: Dict containing 'vertices' (Nx3 ndarray of vertex locations) + Args: + poly: Polyhedron to convert, in Schlegel representation. + + Returns: + Dict containing 'vertices' (Nx3 ndarray of vertex locations) and 'faces' (list of ndarrays specifying the indices of vertices used for constructing each face). ''' diff --git a/adze/moments.py b/adze/moments.py index 794e465..d362d14 100644 --- a/adze/moments.py +++ b/adze/moments.py @@ -9,15 +9,16 @@ try: profile except NameError: def profile(f): - return f - def number_of_moments(order: int) -> int: ''' Returns the number of moments with a given order or below. - :param order: Maximum polynomial order for the moments. - :return: Number of moments with that order or fewer. + Args: + order: Maximum polynomial order for the moments. + + Returns: + Number of moments with that order or fewer. ''' return ((order + 1) * (order + 2) * (order + 3)) // 6 @@ -26,8 +27,11 @@ def get_volume(poly: poly_t) -> float: ''' Calculates the volume of a polyhedron (0th moment). - :param poly: Polyhedron to evaluate, in Schlegel representation. - :return: Volume of the polyhedron. + Args: + poly: Polyhedron to evaluate, in Schlegel representation. + + Returns: + Volume of the polyhedron. ''' return get_moments(poly, 0)[0] @@ -37,9 +41,11 @@ def get_moments(poly: poly_t, order: int) -> numpy.ndarray: ''' Calculates the moments of a polyhedron, up to the specified polynomial order. - :param poly: Polyhedron to evaluate, in Schlegel representation. - :param order: Maximum polynomial order for moments. - :return: Moments, increasing polynomial order: + Args: + poly: Polyhedron to evaluate, in Schlegel representation. + order: Maximum polynomial order for moments. + + Returns: Moments, increasing polynomial order: [1, x, y, z, xx, xy, xz, yy, yz, zz, xxx, xxy, xxz, ...] ''' vertices = poly['vertices'] diff --git a/adze/shapes.py b/adze/shapes.py index 05d0c5e..20f422e 100644 --- a/adze/shapes.py +++ b/adze/shapes.py @@ -9,8 +9,11 @@ def build_tetrahedron(vertices: numpy.ndarray) -> poly_t: ''' Build a Schelegel-representation tetrahedron. - :param vertices: 4x3 array, representing the vertex coordinates. - :return: Tetrahedron in Schelgel representation. + Args: + vertices: 4x3 array, representing the vertex coordinates. + + Returns: + Tetrahedron in Schelgel representation. ''' poly = { 'vertices': numpy.array(vertices, dtype=float), @@ -27,9 +30,12 @@ def build_box(box_corners: numpy.ndarray) -> poly_t: ''' Build a Schelegel-representation box. - :param box_corners: 2x3 array, representing the vertex coordinates of - two diagonally-opposite corners of the box. - :return: Box in Schelgel representation. + Args: + box_corners: 2x3 array, representing the vertex coordinates of + two diagonally-opposite corners of the box. + + Returns: + Box in Schelgel representation. ''' neighbors = [ [1, 4, 3], @@ -63,9 +69,12 @@ def planes_from_box(box_corners: numpy.ndarray) -> List[plane_t]: ''' Construct a list of planes corresponding to the faces of a box. - :param box_corners: 2x3 array, representing the vertex coordinates of - two diagonally-opposite corners of the box. - :return: List of planes, facing inwards. + Args: + box_corners: 2x3 array, representing the vertex coordinates of + two diagonally-opposite corners of the box. + + Returns: + List of planes, facing inwards. ''' box_corners = numpy.array(box_corners) faces = [ @@ -84,8 +93,11 @@ def planes_from_tetrahedron(vertices: numpy.ndarray) -> List[plane_t]: ''' Construct a list of planes corresponding to the faces of a tetrahedron. - :param vertices: 4x3 array, representing the vertex coordinates. - :return: List of planes, facing inwards. + Args: + vertices: 4x3 array, representing the vertex coordinates. + + Returns: + List of planes, facing inwards. ''' verts = numpy.array(vertices, dtype=float) vset_inds = numpy.array(( diff --git a/adze/split.py b/adze/split.py index 71715a9..1924dce 100644 --- a/adze/split.py +++ b/adze/split.py @@ -18,9 +18,12 @@ def clip(poly: poly_t, plane: plane_t) -> poly_t: This function is equivalent to calling split(poly, plane, same_only=True). - :param poly: Polyhedron to clip, in chiral Schlegel representation. - :param plane: Plane to clip against. - :return: Clipped polyhedron. + Args: + poly: Polyhedron to clip, in chiral Schlegel representation. + plane: Plane to clip against. + + Returns: + Clipped polyhedron. ''' left_poly = split(poly, plane, same_only=True) return left_poly @@ -35,12 +38,14 @@ def split(poly, plane, same_only: bool = False): where same_side_poly is on the same side as the plane's normal vector. Otherwise, returns only same_side_poly (somewhat faster). - :param poly: Polyhedron to clip, in chiral Schlegel representation. - :param plane: Plane to clip against. - :param same_only: If True, only returns the clipped poly from the same side as the - plane's normal vector. Default False. - :return: (same_side_poly, other_side_poly) if same_only is False (default), otherwise - only returns same_side_poly. + Args: + poly: Polyhedron to clip, in chiral Schlegel representation. + plane: Plane to clip against. + same_only: If True, only returns the clipped poly from the same side as the + plane's normal vector. Default False. + Returns: + (same_side_poly, other_side_poly) if same_only is False (default), otherwise + only returns same_side_poly. ''' vertices = poly['vertices'] neighbors = poly['neighbors'] diff --git a/adze/test/test_voxel.py b/adze/test/test_voxel.py index 5e6dd36..c537e59 100644 --- a/adze/test/test_voxel.py +++ b/adze/test/test_voxel.py @@ -50,9 +50,10 @@ def visualize_isosurface(grid, """ Draw an isosurface plot of the device. - :param level: Value at which to find isosurface. Default (None) uses mean value in grid. - :param show_edges: Whether to draw triangle edges. Default True - :param finalize: Whether to call pyplot.show() after constructing the plot. Default True + Args: + level: Value at which to find isosurface. Default (None) uses mean value in grid. + show_edges: Whether to draw triangle edges. Default True + finalize: Whether to call pyplot.show() after constructing the plot. Default True """ from matplotlib import pyplot import skimage.measure diff --git a/adze/transform.py b/adze/transform.py index 8c77c71..56e6faa 100644 --- a/adze/transform.py +++ b/adze/transform.py @@ -8,10 +8,13 @@ def rotate(poly: poly_t, theta: float, axis: int) -> poly_t: ''' Rotate the polyhedron around an axis. - :param poly: Polyhedron to rotate. Changed in-place. - :param theta: Angle to rotate by (radians). - :param axis: Axis to rotate around (0-2). - :return: Rotated polyhedron. + Args: + poly: Polyhedron to rotate. Changed in-place. + theta: Angle to rotate by (radians). + axis: Axis to rotate around (0-2). + + Returns: + Rotated polyhedron. Data is altered in-place but also returned for convenience. ''' s, c = sin(theta), cos(theta) @@ -26,9 +29,12 @@ def translate(poly: poly_t, shift: numpy.ndarray) -> poly_t: ''' Translate the polyhedron by a vector. - :param poly: Polyhedron to translate. Changed in-place. - :param shift: Vector to translate by. - :return: Translated polyhedron. + Args: + poly: Polyhedron to translate. Changed in-place. + shift: Vector to translate by. + + Returns: + Translated polyhedron. Data is altered in-place but also returned for convenience. ''' poly['vertices'] += shift @@ -40,9 +46,12 @@ def scale(poly: poly_t, factor: numpy.ndarray or float) -> poly_t: Scale the polyhedron. Can scale by a single value, or scale each axis individually - :param poly: Polyhedron to scale. Changed in-place. - :param factor: Scalar or vector to scale by. - :return: Scaled polyhedron. + Args: + poly: Polyhedron to scale. Changed in-place. + factor: Scalar or vector to scale by. + + Returns: + Scaled polyhedron. Data is altered in-place but also returned for convenience. ''' poly['vertices'] *= factor @@ -53,9 +62,12 @@ def affine(poly: poly_t, xform: numpy.ndarray) -> poly_t: ''' Perform arbitrary affine trasnformation on the polyhedron. - :param poly: Polyhedron to transform. Changed in-place. - :param xform: 4x4 affine transformation matrix. - :return: Transformed polyhedron. + Args: + poly: Polyhedron to transform. Changed in-place. + xform: 4x4 affine transformation matrix. + + Returns: + Transformed polyhedron. Data is altered in-place but also returned for convenience. ''' mat4x3 = xform[:, :3] diff --git a/adze/util.py b/adze/util.py index ee179b8..4df2ad0 100644 --- a/adze/util.py +++ b/adze/util.py @@ -17,7 +17,8 @@ def build_empty() -> poly_t: ''' Build an empty Schlegel representation (containing no vertices). - :return: Empty Schelegel representation. + Returns: + Empty Schelegel representation. ''' return { 'vertices': numpy.empty((0, 3), dtype=float), @@ -30,10 +31,13 @@ def find_components(neighbors: numpy.ndarray) -> numpy.ndarray: ''' Given an adjacency list, flood-fill to find and label connected components. - :param neighbors: NxM adjacency list. Each of the N vertices is assumed - to have the same number of neighbors, which are specified by their - indices. - :return: 1D ndarray with N entries. Entries are integer labels denoting + Args: + neighbors: NxM adjacency list. Each of the N vertices is assumed + to have the same number of neighbors, which are specified by their + indices. + + Returns: + 1D ndarray with N entries. Entries are integer labels denoting which connected component the vertex belongs to. ''' num_vertices = neighbors.shape[0] @@ -61,9 +65,12 @@ def find_faces(neighbors: numpy.ndarray) -> List[numpy.ndarray]: graph with exactly 3 neighbors per vertex. Neighbors are ordered counter-clockwise. - :param neighbors: Nx3 adjacency list. Neighbors are specified by their - indices. - :return: List of ndarrays, each of which contains the indices in a given + Args: + neighbors: Nx3 adjacency list. Neighbors are specified by their + indices. + + Returns: + List of ndarrays, each of which contains the indices in a given face, starting with the lowest index in the face. ''' num_verts = neighbors.shape[0] @@ -215,10 +222,13 @@ def check_poly(poly: poly_t, check_connectivity: bool = True): - has exactly 3 edges per vertex - is 3-vertex-connected (if check_connectivity is True; slow!) - :param poly: Polyhedron to check. - :param check_connectivity: Whether to check for 3-vertex-connectedness. - This is slow! Default True. - :raises: Exception if the polyhedron fails a check + Args: + poly: Polyhedron to check. + check_connectivity: Whether to check for 3-vertex-connectedness. + This is slow! Default `True`. + + Raises: + Exception if the polyhedron fails a check ''' neighbors = poly['neighbors'] vertices = poly['vertices'] diff --git a/adze/voxel.py b/adze/voxel.py index 79f91a0..67e8b90 100644 --- a/adze/voxel.py +++ b/adze/voxel.py @@ -24,25 +24,28 @@ def voxelize(poly: poly_t, Voxelize a Schelegel-representation polyhedron on an arbitrary rectangular grid. The grid is specified by supplying 3 sets of axis-aligned planes, in the form - [[x0, x1, x2, ..., xX], [y0, y1, y2, ..., yY], [z0, z1, ..., zZ]] + `[[x0, x1, x2, ..., xX], [y0, y1, y2, ..., yY], [z0, z1, ..., zZ]]` The output array has size [X-1, Y-1, Z-1, N] (include_outside False) or - [X+1, Y+1, Z+1, N] (include_outside True), where N is the number of moments + `[X+1, Y+1, Z+1, N]` (`include_outside` True), where `N` is the number of moments corresponding to the requested order. If include_outside is true, the outer borders of the first three dimensions of the output correspond to portions of the polyhedron that protruded outside the provided grid. - :param poly: Polyhedron to voxelize. - :param planes: List of coordinate lists, specifying the axis-aligned planes - which form the voxel grid: - [[x0, x1, ..., xX], [y0, y1, ..., yY], [z0, z1, ..., zZ]] - :param include_outside: Whether to include cells corresponding to portions of the - polyhedron that fall outside of the grid. Default False. - :param order: Polynomial order of moments to calculate. Default 0. - :return: (X - 1) x (Y - 1) x (Z - 1) x N (include_outside False) or - (X + 1) x (Y + 1) x (Z + 1) x N (include_outside True) ndarray. - If include_outside is true, the outer borders of the first three dimensions + Args: + poly: Polyhedron to voxelize. + planes: List of coordinate lists, specifying the axis-aligned planes + which form the voxel grid: + `[[x0, x1, ..., xX], [y0, y1, ..., yY], [z0, z1, ..., zZ]]` + include_outside: Whether to include cells corresponding to portions of the + polyhedron that fall outside of the grid. Default `False`. + order: Polynomial order of moments to calculate. Default `0`. + + Returns: + (X - 1) x (Y - 1) x (Z - 1) x N (`include_outside` False) or + (X + 1) x (Y + 1) x (Z + 1) x N (`include_outside` True) ndarray. + If include_outside is True, the outer borders of the first three dimensions correspond to portions of the polyhedron that protruded outside the outermost - planes. N is number_of_moments(order). + planes. N is `number_of_moments(order)`. ''' planes = [numpy.array(p) for p in planes]