Skip to content

Commit

Permalink
Merge pull request #1340 from compas-dev/fix-doctests
Browse files Browse the repository at this point in the history
Fix the doctests
  • Loading branch information
tomvanmele authored Jul 3, 2024
2 parents 6e85276 + bb5258e commit 0d71a41
Show file tree
Hide file tree
Showing 42 changed files with 438 additions and 389 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Changed

* Fixed bug in `compas.geometry.curves.curve.Curve.reversed` by adding missing parenthesis.
* Fixed all doctests so we can run `invoke test --doctest`.

### Removed

Expand Down
2 changes: 1 addition & 1 deletion src/compas/colors/color.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ class Color(Data):
By default, this class will create a color with the RGB components in the range ``[0.0, 1.0]``.
>>> Color(1, 0, 0)
Color(1.0, 0.0, 0.0, alpha=1.0)
Color(1, 0, 0, alpha=1.0)
Attempting to create a color with components outside of the range ``[0.0, 1.0]`` will raise a ``ValueError``.
Expand Down
12 changes: 0 additions & 12 deletions src/compas/data/coercion.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,6 @@ def coerce_sequence_of_tuple(sequence):
with each iterable item converted to a tuple,
and non-iterable items wrapped in a tuple.
Examples
--------
>>> items = coerce_sequence_of_tuple(["a", 1, (None,), [2.0, 3.0]])
>>> is_sequence_of_tuple(items)
True
"""
items = []
for item in sequence:
Expand Down Expand Up @@ -53,12 +47,6 @@ def coerce_sequence_of_list(sequence):
with each iterable item converted to a list,
and non-iterable items wrapped in a list.
Examples
--------
>>> items = coerce_sequence_of_list(["a", 1, (None,), [2.0, 3.0]])
>>> is_sequence_of_list(items)
True
"""
items = []
for item in sequence:
Expand Down
7 changes: 4 additions & 3 deletions src/compas/data/encoders.py
Original file line number Diff line number Diff line change
Expand Up @@ -181,13 +181,14 @@ class DataDecoder(json.JSONDecoder):
>>> import json
>>> from compas.data import DataDecoder
>>> with open("point.json", "r") as f:
... point = json.load(f, cls=DataDecoder)
>>> with open('point.json', 'r') as f: # doctest: +SKIP
... point = json.load(f, cls=DataDecoder) # doctest: +SKIP
...
Implicit use case.
>>> from compas.data import json_load
>>> point = json_load("point.json")
>>> point = json_load('point.json') # doctest: +SKIP
"""

Expand Down
56 changes: 29 additions & 27 deletions src/compas/datastructures/assembly/part.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,24 +34,27 @@ class GeometricFeature(Feature):
Examples
--------
>>> def trim_brep_plane(brep, plane):
>>> # trim brep with plane, return trimmed brep
>>> from compas.geometry import Brep
>>> from compas.datastructures import Mesh
>>>
>>> def trim_mesh_plane(mesh, plane):
>>> # trim mesh with plane, return trimmed mesh
>>> def trim_brep_plane(brep, plane):
... pass
...
>>> def trim_mesh_plane(mesh, plane):
... pass
...
>>> class TrimmingFeature(GeometricFeature):
... OPERATIONS = {Brep: trim_brep_plane, Mesh: trim_mesh_plane}
... def __init__(self, trimming_plane):
... super(TrimmingFeature, self).__init__()
... self._geometry = trimming_plane
... def apply(self, part):
... part_geometry = part.get_geometry(with_features=True)
... type_ = Brep if isinstance(part_geometry, Brep) else Mesh
... operation = OPERATIONS[type_]
... return operation(part_geometry, self._geometry)
...
>>>
>>> class TrimmingFeature(GeometricFeature):
>>> OPERATIONS = {Brep: trim_brep_plane, Mesh: trim_mesh_plane}
>>>
>>> def __init__(self, trimming_plane):
>>> super(TrimmingFeature, self).__init__()
>>> self._geometry = trimming_plane
>>>
>>> def apply(self, part):
>>> part_geometry = part.get_geometry(with_features=True)
>>> type_ = Brep if isinstance(part_geometry, Brep) else Mesh
>>> operation = OPERATIONS[type_]
>>> return operation(part_geometry, self._geometry)
"""

Expand Down Expand Up @@ -81,18 +84,17 @@ class ParametricFeature(Feature):
Examples
--------
>>> class ExtensionFeature(ParametricFeature):
>>> def __init__(self, extend_by):
>>> super(ExtensionFeature, self).__init__()
>>> self.extend_by = extend_by
>>>
>>> def apply(self, part):
>>> part.length += self._extend_by
>>>
>>> def restore(self, part):
>>> part.length -= self._extend_by
... def __init__(self, extend_by):
... super(ExtensionFeature, self).__init__()
... self.extend_by = extend_by
... def apply(self, part):
... part.length += self._extend_by
... def restore(self, part):
... part.length -= self._extend_by
... def accumulate(self, other):
... return BeamExtensionFeature(max(self.extend_by, other.extend_by))
...
>>>
>>> def accumulate(self, other):
>>> return BeamExtensionFeature(max(self.extend_by, other.extend_by))
"""

Expand Down
15 changes: 11 additions & 4 deletions src/compas/datastructures/cell_network/cell_network.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,10 +74,17 @@ class CellNetwork(Datastructure):
>>> vertices = [(0, 0, 0), (0, 1, 0), (1, 1, 0), (1, 0, 0), (0, 0, 1), (1, 0, 1), (1, 1, 1), (0, 1, 1)]
>>> faces = [[0, 1, 2, 3], [0, 3, 5, 4], [3, 2, 6, 5], [2, 1, 7, 6], [1, 0, 4, 7], [4, 5, 6, 7]]
>>> cells = [[0, 1, 2, 3, 4, 5]]
>>> [network.add_vertex(x=x, y=y, z=z) for x, y, z in vertices]
>>> [cell_network.add_face(fverts) for fverts in faces]
>>> [cell_network.add_cell(fkeys) for fkeys in cells]
>>> cell_network
>>> for x, y, z in vertices:
... vertex = cell_network.add_vertex(x=x, y=y, z=z)
...
>>> for face_vertices in faces:
... face = cell_network.add_face(face_vertices)
...
>>> for cell_faces in cells:
... cell = cell_network.add_cell(cell_faces)
...
>>> print(cell_network)
<CellNetwork with 8 vertices, 6 faces, 1 cells, 12 edges>
"""

Expand Down
4 changes: 2 additions & 2 deletions src/compas/datastructures/graph/graph.py
Original file line number Diff line number Diff line change
Expand Up @@ -2366,7 +2366,7 @@ def complement(self):
>>> from compas.datastructures import Graph
>>> graph = Graph.from_obj(compas.get("lines.obj"))
>>> complement = graph.complement()
>>> any(complement.has_edge(u, v, directed=False) for u, v in graph.edges())
>>> any(complement.has_edge((u, v), directed=False) for u, v in graph.edges())
False
"""
Expand All @@ -2381,7 +2381,7 @@ def complement(self):

for u, v in combinations(self.nodes(), 2):
if not self.has_edge((u, v), directed=False):
graph.add_edge(u, v, attr_dict=self.edge_attributes((u, v)))
graph.add_edge(u, v)

return graph

Expand Down
6 changes: 3 additions & 3 deletions src/compas/datastructures/mesh/duality.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ def mesh_dual(mesh, cls=None, include_boundary=False):
>>> mesh.delete_face(6)
>>> mesh.delete_face(7)
>>> mesh.quads_to_triangles()
>>> mesh = mesh.subdivide("corner")
>>> mesh = mesh.subdivided("corner")
>>> dual = mesh.dual(include_boundary=True)
"""
Expand Down Expand Up @@ -78,7 +78,7 @@ def mesh_dual(mesh, cls=None, include_boundary=False):
edge_vertex = {}
for boundary in mesh.edges_on_boundaries():
for u, v in boundary:
x, y, z = mesh.edge_midpoint(u, v)
x, y, z = mesh.edge_midpoint((u, v))
edge_vertex[u, v] = edge_vertex[v, u] = dual.add_vertex(x=x, y=y, z=z)

vertex_vertex = {}
Expand All @@ -97,7 +97,7 @@ def mesh_dual(mesh, cls=None, include_boundary=False):
nbrs = mesh.vertex_neighbors(vertex, ordered=True)[::-1]
vertices.append(edge_vertex[vertex, nbrs[0]])
for nbr in nbrs[:-1]:
vertices.append(mesh.halfedge_face(vertex, nbr))
vertices.append(mesh.halfedge_face((vertex, nbr)))
vertices.append(edge_vertex[vertex, nbrs[-1]])
dual.add_face(vertices[::-1])

Expand Down
24 changes: 14 additions & 10 deletions src/compas/datastructures/mesh/mesh.py
Original file line number Diff line number Diff line change
Expand Up @@ -2855,6 +2855,9 @@ def remove_duplicate_vertices(self, precision=None):
36
>>> for x, y, z in mesh.vertices_attributes("xyz", keys=list(mesh.vertices())[:5]):
... mesh.add_vertex(x=x, y=y, z=z)
...
36
37
38
39
40
Expand Down Expand Up @@ -4791,9 +4794,9 @@ def transform_numpy(self, T):
Examples
--------
>>> from compas.datastructures import Mesh
>>> from compas.geometry import matrix_from_axis_and_angle_numpy
>>> from compas.geometry import matrix_from_axis_and_angle
>>> mesh = Mesh.from_polyhedron(6)
>>> T = matrix_from_axis_and_angle_numpy([0, 0, 1], math.pi / 4)
>>> T = matrix_from_axis_and_angle([0, 0, 1], math.pi / 4)
>>> mesh.transform_numpy(T)
"""
Expand Down Expand Up @@ -4906,9 +4909,9 @@ def face_matrix(self, rtype="array"):
>>> type(F)
<class 'numpy.ndarray'>
>>> from numpy import allclose
>>> from numpy import allclose, asarray
>>> xyz = asarray(mesh.vertices_attributes('xyz'))
>>> F = mesh.face_matrix(mesh, rtype='csr')
>>> F = mesh.face_matrix(rtype='csr')
>>> c1 = F.dot(xyz) / F.sum(axis=1)
>>> c2 = [mesh.face_centroid(fkey) for fkey in mesh.faces()]
>>> allclose(c1, c2)
Expand Down Expand Up @@ -4963,10 +4966,11 @@ def laplacian_matrix(self, rtype="array"):
--------
>>> from compas.datastructures import Mesh
>>> mesh = Mesh.from_polyhedron(6)
>>> L = mesh.laplacian_matrix(mesh, rtype='array')
>>> L = mesh.laplacian_matrix(rtype='array')
>>> type(L)
<class 'numpy.ndarray'>
>>> from numpy import asarray
>>> xyz = asarray(mesh.vertices_attributes('xyz'))
>>> L = mesh.laplacian_matrix(mesh)
>>> d = L.dot(xyz)
Expand Down Expand Up @@ -5005,11 +5009,11 @@ def offset(self, distance=1.0):
Examples
--------
>>> from compas.datastructures import Mesh, mesh_offset
>>> from compas.datastructures import Mesh
>>> from compas.geometry import distance_point_point as dist
>>> mesh = Mesh.from_vertices_and_faces([[0, 0, 0], [1, 0, 0], [1, 1, 0], [0, 1, 0]], [[0, 1, 2, 3]])
>>> mesh.offset()
<compas.datastructures.mesh.mesh.Mesh object at 0x109eaad60>
>>> mesh.offset() # doctest: +ELLIPSIS
<compas.datastructures.mesh.mesh.Mesh object at ...>
"""
offset = self.copy()
Expand Down Expand Up @@ -5047,8 +5051,8 @@ def thickened(self, thickness=1.0, both=True):
--------
>>> from compas.datastructures import Mesh
>>> mesh = Mesh.from_vertices_and_faces([[0, 0, 0], [1, 0, 0], [1, 1, 0], [0, 1, 0]], [[0, 1, 2, 3]])
>>> mesh.thicken(mesh)
<compas.datastructures.mesh.mesh.Mesh object at 0x109eaad60>
>>> mesh.thickened() # doctest: +ELLIPSIS
<compas.datastructures.mesh.mesh.Mesh object at ...>
"""
if thickness <= 0:
Expand Down
2 changes: 1 addition & 1 deletion src/compas/datastructures/mesh/operations/merge.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ def mesh_merge_faces(mesh, faces):
--------
>>> from compas.datastructures import Mesh
>>> mesh = Mesh.from_vertices_and_faces([[0, 0, 0], [1, 0, 0], [1, 1, 0], [0, 1, 0]], [[0, 1, 2, 3]])
>>> mesh = mesh.subdivide(scheme="quad")
>>> mesh = mesh.subdivided(scheme='quad')
>>> mesh_merge_faces(mesh, [1, 2])
5
>>> mesh_merge_faces(mesh, [3, 5])
Expand Down
2 changes: 1 addition & 1 deletion src/compas/datastructures/mesh/subdivision.py
Original file line number Diff line number Diff line change
Expand Up @@ -313,7 +313,7 @@ def mesh_subdivide_catmullclark(mesh, k=1, fixed=None):
>>> top = sorted(cage.faces(), key=lambda face: dot_vectors(cage.face_normal(face), [0, 0, 1]))[-1]
>>> cage.edges_attribute("crease", 5, keys=list(cage.face_halfedges(top)))
>>> subd = cage.subdivide(k=4)
>>> subd = cage.subdivided(k=4)
"""
cls = type(mesh)
Expand Down
8 changes: 4 additions & 4 deletions src/compas/datastructures/tree/tree.py
Original file line number Diff line number Diff line change
Expand Up @@ -244,10 +244,10 @@ class Tree(Datastructure):
>>> branch.add(leaf2)
>>> print(tree)
<Tree with 4 nodes>
|--<TreeNode: root>
|-- <TreeNode: branch>
|-- <TreeNode: leaf1>
|-- <TreeNode: leaf2>
└── <TreeNode: root>
└── <TreeNode: branch>
├── <TreeNode: leaf1>
└── <TreeNode: leaf2>
"""

Expand Down
14 changes: 0 additions & 14 deletions src/compas/geometry/_core/_algebra.py
Original file line number Diff line number Diff line change
Expand Up @@ -2768,12 +2768,6 @@ def close(value1, value2, tol=1e-05):
It is more accurate to use a combination of absolute and relative tolerance.
Therefor, use :func:`TOL.is_close` instead.
Examples
--------
>>> close(1.0, 1.001)
False
>>> close(1.0, 1.001, tol=1e-2)
True
"""
return TOL.is_close(value1, value2, rtol=0.0, atol=tol)

Expand Down Expand Up @@ -2815,13 +2809,5 @@ def allclose(l1, l2, tol=None):
----------
.. [1] https://docs.scipy.org/doc/numpy/reference/generated/numpy.allclose.html
Examples
--------
>>> allclose([0.1, 0.2, 0.3, 0.4], [0.1, 0.20001, 0.3, 0.4])
True
>>> allclose([0.1, 0.2, 0.3, 0.4], [0.1, 0.20001, 0.3, 0.4], tol=1e-6)
False
"""
return TOL.is_allclose(l1, l2, atol=tol)
Loading

0 comments on commit 0d71a41

Please sign in to comment.