Skip to content

Commit

Permalink
Merge pull request #117 from boschresearch/doctests
Browse files Browse the repository at this point in the history
Use doctests
  • Loading branch information
johannes-mueller authored Oct 10, 2024
2 parents 16324d4 + 0dad6d7 commit c1acd0f
Show file tree
Hide file tree
Showing 7 changed files with 101 additions and 79 deletions.
5 changes: 4 additions & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -115,15 +115,18 @@ extras =
# Comment those flags to avoid this py.test issue.
addopts =
--cov src/pylife --cov-append -m "not slow_acceptance and not demos"
--doctest-modules
--ignore=src/pylife/materialdata/woehler/bayesian.py

norecursedirs =
dist
build
.tox
testpaths = tests
testpaths = tests src/pylife
markers =
slow_acceptance: long running acceptance test (not run by default)
demos: demo notebooks by testbook
doctest_optionflags = NORMALIZE_WHITESPACE IGNORE_EXCEPTION_DETAIL

[bdist_wheel]
# Use this option if your package is pure-python
Expand Down
11 changes: 6 additions & 5 deletions src/pylife/core/pylifesignal.py
Original file line number Diff line number Diff line change
Expand Up @@ -273,10 +273,11 @@ def __init__(self, obj):
def bar(df):
return pd.DataFrame({'baz': df['foo'] + df['bar']})
>>> df = pd.DataFrame({'foo': [1.0, 2.0], 'bar': [-1.0, -2.0]})
>>> df.foo.bar()
baz
0 0.0
1 0.0
df = pd.DataFrame({'foo': [1.0, 2.0], 'bar': [-1.0, -2.0]})
df.foo.bar()
baz
0 0.0
1 0.0
"""
return cls._register_method(method_name)
1 change: 1 addition & 0 deletions src/pylife/materialdata/woehler/bayesian.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
__author__ = "Mustapha Kassem"
__maintainer__ = "Johannes Mueller"


raise NotImplementedError(
"pyLife's Bayesian Wöhler analyzer has been shutdown. "
"See documentation for details."
Expand Down
46 changes: 30 additions & 16 deletions src/pylife/mesh/meshsignal.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,20 +30,25 @@
--------
Read in a mesh from a vmap file:
>>> df = (vm = pylife.vmap.VMAPImport('demos/plate_with_hole.vmap')
.make_mesh('1', 'STATE-2')
.join_variable('STRESS_CAUCHY')
.join_variable('DISPLACEMENT')
.to_frame())
>>> from pylife.vmap import VMAPImport
>>> df = (
... VMAPImport('demos/plate_with_hole.vmap')
... .make_mesh('1', 'STATE-2')
... .join_coordinates()
... .join_variable('STRESS_CAUCHY')
... .join_variable('DISPLACEMENT')
... .to_frame()
... )
>>> df.head()
x y z S11 S22 S33 S12 S13 S23 dx dy dz
element_id node_id
1 1734 14.897208 5.269875 0.0 27.080811 6.927080 0.0 -13.687358 0.0 0.0 0.005345 0.000015 0.0
1582 14.555333 5.355806 0.0 28.319006 1.178649 0.0 -10.732705 0.0 0.0 0.005285 0.000003 0.0
1596 14.630658 4.908741 0.0 47.701195 5.512213 0.0 -17.866833 0.0 0.0 0.005376 0.000019 0.0
4923 14.726271 5.312840 0.0 27.699907 4.052865 0.0 -12.210032 0.0 0.0 0.005315 0.000009 0.0
4924 14.592996 5.132274 0.0 38.010101 3.345431 0.0 -14.299768 0.0 0.0 0.005326 0.000013 0.0
x y z ... dx dy dz
element_id node_id ...
1 1734 14.897208 5.269875 0.0 ... 0.005345 0.000015 0.0
1582 14.555333 5.355806 0.0 ... 0.005285 0.000003 0.0
1596 14.630658 4.908741 0.0 ... 0.005376 0.000019 0.0
4923 14.726271 5.312840 0.0 ... 0.005315 0.000009 0.0
4924 14.592996 5.132274 0.0 ... 0.005326 0.000013 0.0
<BLANKLINE>
[5 rows x 12 columns]
Get the coordinates of the mesh.
Expand Down Expand Up @@ -210,10 +215,19 @@ def vtk_data(self):
Example
-------
>>> import pyvista as pv
>>> grid = pv.UnstructuredGrid(*our_mesh.mesh.vtk_data())
>>> from pylife.vmap import VMAPImport
>>> df = (
... VMAPImport('demos/plate_with_hole.vmap')
... .make_mesh('1', 'STATE-2')
... .join_coordinates()
... .join_variable('STRESS_CAUCHY')
... .to_frame()
... )
>>> grid = pv.UnstructuredGrid(*df.mesh.vtk_data())
>>> plotter = pv.Plotter(window_size=[1920, 1080])
>>> plotter.add_mesh(grid, scalars=our_mesh.groupby('element_id')['val'].mean().to_numpy())
>>> plotter.show()
>>> plotter.add_mesh(grid, scalars=df.groupby('element_id')['S11'].mean().to_numpy()) # doctest: +SKIP
>>> plotter.show() # doctest: +SKIP
Note the `*` that needs to be added when calling ``pv.UnstructuredGrid()``.
"""
Expand Down
10 changes: 5 additions & 5 deletions src/pylife/strength/meanstress.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,11 @@ def from_dict(cls, segments_dict):
Example
-------
>>> hd = MST.HaighDiagram.from_dict({
>>> (1.0, np.inf): 0.0,
>>> (-np.inf, 0.0): 0.5,
>>> (0.0, 1.0): 0.167
>>> })
>>> hd = HaighDiagram.from_dict({
... (1.0, np.inf): 0.0,
... (-np.inf, 0.0): 0.5,
... (0.0, 1.0): 0.167
... })
sets up a FKM Goodman like Haigh diagram.
"""
Expand Down
21 changes: 9 additions & 12 deletions src/pylife/utils/histogram.py
Original file line number Diff line number Diff line change
Expand Up @@ -165,12 +165,6 @@ def rebin_histogram(histogram, binning, nan_default=False):
Examples
--------
>>> h
(0.0, 1.0] 1.0
(1.0, 2.0] 2.0
(2.0, 3.0] 3.0
(3.0, 4.0] 4.0
dtype: float64
>>> h = pd.Series([10.0, 20.0, 30.0, 40.0], index=pd.interval_range(0.0, 4.0, 4))
>>> h
(0.0, 1.0] 10.0
Expand Down Expand Up @@ -203,12 +197,15 @@ def rebin_histogram(histogram, binning, nan_default=False):
Define the target bin just by an int:
>>> rebin_histogram(h, 5)
(0.0, 0.8] 8.0
(0.8, 1.6] 14.0
(1.6, 2.4] 20.0
(2.4, 3.2] 26.0
(3.2, 4.0] 32.0
>>> rebin_histogram(h, 8)
(0.0, 0.5] 5.0
(0.5, 1.0] 5.0
(1.0, 1.5] 10.0
(1.5, 2.0] 10.0
(2.0, 2.5] 15.0
(2.5, 3.0] 15.0
(3.0, 3.5] 20.0
(3.5, 4.0] 20.0
dtype: float64
Limitations
Expand Down
86 changes: 46 additions & 40 deletions src/pylife/vmap/vmap_import.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,24 +131,29 @@ def make_mesh(self, geometry, state=None):
--------
Get the mesh data with the coordinates of geometry '1' and the stress tensor of 'STATE-2'
>>> (pylife.vmap.VMAPImport('demos/plate_with_hole.vmap')
.make_mesh('1', 'STATE-2')
.join_coordinates()
.join_variable('STRESS_CAUCHY')
.to_frame()
x y z S11 S22 S33 S12 S13 S23
element_id node_id
1 1734 14.897208 5.269875 0.0 27.080811 6.927080 0.0 -13.687358 0.0 0.0
1582 14.555333 5.355806 0.0 28.319006 1.178649 0.0 -10.732705 0.0 0.0
1596 14.630658 4.908741 0.0 47.701195 5.512213 0.0 -17.866833 0.0 0.0
4923 14.726271 5.312840 0.0 27.699907 4.052865 0.0 -12.210032 0.0 0.0
4924 14.592996 5.132274 0.0 38.010101 3.345431 0.0 -14.299768 0.0 0.0
... ... ... ... ... ... ... ... ... ...
4770 3812 -13.189782 -5.691876 0.0 36.527439 2.470588 0.0 -14.706686 0.0 0.0
12418 -13.560289 -5.278386 0.0 32.868889 3.320898 0.0 -14.260107 0.0 0.0
14446 -13.673285 -5.569107 0.0 34.291058 3.642457 0.0 -13.836027 0.0 0.0
14614 -13.389065 -5.709927 0.0 36.063541 2.828889 0.0 -13.774759 0.0 0.0
14534 -13.276068 -5.419206 0.0 33.804211 2.829817 0.0 -14.580153 0.0 0.0
>>> (
... VMAPImport('demos/plate_with_hole.vmap')
... .make_mesh('1', 'STATE-2')
... .join_coordinates()
... .join_variable('STRESS_CAUCHY')
... .to_frame()
... )
x y z ... S12 S13 S23
element_id node_id ...
1 1734 14.897208 5.269875 0.0 ... -13.687358 0.0 0.0
1582 14.555333 5.355806 0.0 ... -10.732705 0.0 0.0
1596 14.630658 4.908741 0.0 ... -17.866833 0.0 0.0
4923 14.726271 5.312840 0.0 ... -12.210032 0.0 0.0
4924 14.592996 5.132274 0.0 ... -14.299768 0.0 0.0
... ... ... ... ... ... ... ...
4770 3812 -13.189782 -5.691876 0.0 ... -14.706686 0.0 0.0
12418 -13.560289 -5.278386 0.0 ... -14.260107 0.0 0.0
14446 -13.673285 -5.569107 0.0 ... -13.836027 0.0 0.0
14614 -13.389065 -5.709927 0.0 ... -13.774759 0.0 0.0
14534 -13.276068 -5.419206 0.0 ... -14.580153 0.0 0.0
<BLANKLINE>
[37884 rows x 9 columns]
"""
self._mesh = pd.DataFrame(index=self._mesh_index(geometry))
self._geometry = geometry
Expand Down Expand Up @@ -219,7 +224,7 @@ def join_coordinates(self):
--------
Receive the mesh with the node coordinates
>>> pylife.vmap.VMAPImport('demos/plate_with_hole.vmap').make_mesh('1').join_coordinates().to_frame()
>>> VMAPImport('demos/plate_with_hole.vmap').make_mesh('1').join_coordinates().to_frame()
x y z
element_id node_id
1 1734 14.897208 5.269875 0.0
Expand All @@ -233,9 +238,8 @@ def join_coordinates(self):
14446 -13.673285 -5.569107 0.0
14614 -13.389065 -5.709927 0.0
14534 -13.276068 -5.419206 0.0
<BLANKLINE>
[37884 rows x 3 columns]
"""
if self._mesh is None:
raise APIUseError("Need to make_mesh() before joining the coordinates.")
Expand Down Expand Up @@ -340,25 +344,27 @@ def join_variable(self, var_name, state=None, column_names=None):
--------
Receiving the 'DISPLACEMENT' of 'STATE-1' , the stress and strain tensors of 'STATE-2'
>>> (pylife.vmap.VMAPImport('demos/plate_with_hole.vmap')
.make_mesh('1')
.join_variable('DISPLACEMENT', 'STATE-1')
.join_variable('STRESS_CAUCHY', 'STATE-2')
.join_variable('E').to_frame())
dx dy dz S11 S22 S33 S12 S13 S23 E11 E22 E33 E12 E13 E23
element_id node_id
1 1734 0.0 0.0 0.0 27.080811 6.927080 0.0 -13.687358 0.0 0.0 0.000119 -0.000006 0.0 -0.000169 0.0 0.0
1582 0.0 0.0 0.0 28.319006 1.178649 0.0 -10.732705 0.0 0.0 0.000133 -0.000035 0.0 -0.000133 0.0 0.0
1596 0.0 0.0 0.0 47.701195 5.512213 0.0 -17.866833 0.0 0.0 0.000219 -0.000042 0.0 -0.000221 0.0 0.0
4923 0.0 0.0 0.0 27.699907 4.052865 0.0 -12.210032 0.0 0.0 0.000126 -0.000020 0.0 -0.000151 0.0 0.0
4924 0.0 0.0 0.0 38.010101 3.345431 0.0 -14.299768 0.0 0.0 0.000176 -0.000038 0.0 -0.000177 0.0 0.0
... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ...
4770 3812 0.0 0.0 0.0 36.527439 2.470588 0.0 -14.706686 0.0 0.0 0.000170 -0.000040 0.0 -0.000182 0.0 0.0
12418 0.0 0.0 0.0 32.868889 3.320898 0.0 -14.260107 0.0 0.0 0.000152 -0.000031 0.0 -0.000177 0.0 0.0
14446 0.0 0.0 0.0 34.291058 3.642457 0.0 -13.836027 0.0 0.0 0.000158 -0.000032 0.0 -0.000171 0.0 0.0
14614 0.0 0.0 0.0 36.063541 2.828889 0.0 -13.774759 0.0 0.0 0.000168 -0.000038 0.0 -0.000171 0.0 0.0
14534 0.0 0.0 0.0 33.804211 2.829817 0.0 -14.580153 0.0 0.0 0.000157 -0.000035 0.0 -0.000181 0.0 0.0
>>> (
... VMAPImport('demos/plate_with_hole.vmap')
... .make_mesh('1')
... .join_variable('DISPLACEMENT', 'STATE-1')
... .join_variable('STRESS_CAUCHY', 'STATE-2')
... .join_variable('E').to_frame()
... )
dx dy dz S11 ... E33 E12 E13 E23
element_id node_id ...
1 1734 0.0 0.0 0.0 27.080811 ... 0.0 -0.000169 0.0 0.0
1582 0.0 0.0 0.0 28.319006 ... 0.0 -0.000133 0.0 0.0
1596 0.0 0.0 0.0 47.701195 ... 0.0 -0.000221 0.0 0.0
4923 0.0 0.0 0.0 27.699907 ... 0.0 -0.000151 0.0 0.0
4924 0.0 0.0 0.0 38.010101 ... 0.0 -0.000177 0.0 0.0
... ... ... ... ... ... ... ... ... ...
4770 3812 0.0 0.0 0.0 36.527439 ... 0.0 -0.000182 0.0 0.0
12418 0.0 0.0 0.0 32.868889 ... 0.0 -0.000177 0.0 0.0
14446 0.0 0.0 0.0 34.291058 ... 0.0 -0.000171 0.0 0.0
14614 0.0 0.0 0.0 36.063541 ... 0.0 -0.000171 0.0 0.0
14534 0.0 0.0 0.0 33.804211 ... 0.0 -0.000181 0.0 0.0
<BLANKLINE>
[37884 rows x 15 columns]
TODO
Expand Down

0 comments on commit c1acd0f

Please sign in to comment.