From 0dad6d7d3db367722bd9027a2c9b84cb31edb9e9 Mon Sep 17 00:00:00 2001 From: Johannes Mueller Date: Thu, 10 Oct 2024 15:41:18 +0200 Subject: [PATCH] Use doctests Signed-off-by: Johannes Mueller --- setup.cfg | 5 +- src/pylife/core/pylifesignal.py | 11 +-- src/pylife/materialdata/woehler/bayesian.py | 1 + src/pylife/mesh/meshsignal.py | 46 +++++++---- src/pylife/strength/meanstress.py | 10 +-- src/pylife/utils/histogram.py | 21 +++-- src/pylife/vmap/vmap_import.py | 86 +++++++++++---------- 7 files changed, 101 insertions(+), 79 deletions(-) diff --git a/setup.cfg b/setup.cfg index 8f3c5fb1..93cf728d 100644 --- a/setup.cfg +++ b/setup.cfg @@ -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 diff --git a/src/pylife/core/pylifesignal.py b/src/pylife/core/pylifesignal.py index 352f30fc..eaf8f024 100644 --- a/src/pylife/core/pylifesignal.py +++ b/src/pylife/core/pylifesignal.py @@ -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) diff --git a/src/pylife/materialdata/woehler/bayesian.py b/src/pylife/materialdata/woehler/bayesian.py index 2ba2044c..92444c31 100644 --- a/src/pylife/materialdata/woehler/bayesian.py +++ b/src/pylife/materialdata/woehler/bayesian.py @@ -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." diff --git a/src/pylife/mesh/meshsignal.py b/src/pylife/mesh/meshsignal.py index ebfb160e..97e648ea 100644 --- a/src/pylife/mesh/meshsignal.py +++ b/src/pylife/mesh/meshsignal.py @@ -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 + +[5 rows x 12 columns] Get the coordinates of the mesh. @@ -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()``. """ diff --git a/src/pylife/strength/meanstress.py b/src/pylife/strength/meanstress.py index 6ad6f3c2..1bb77bc6 100644 --- a/src/pylife/strength/meanstress.py +++ b/src/pylife/strength/meanstress.py @@ -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. """ diff --git a/src/pylife/utils/histogram.py b/src/pylife/utils/histogram.py index d2f648f5..9584e6e7 100644 --- a/src/pylife/utils/histogram.py +++ b/src/pylife/utils/histogram.py @@ -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 @@ -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 diff --git a/src/pylife/vmap/vmap_import.py b/src/pylife/vmap/vmap_import.py index be8019f2..c4c675a8 100644 --- a/src/pylife/vmap/vmap_import.py +++ b/src/pylife/vmap/vmap_import.py @@ -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 + + [37884 rows x 9 columns] + """ self._mesh = pd.DataFrame(index=self._mesh_index(geometry)) self._geometry = geometry @@ -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 @@ -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 - + [37884 rows x 3 columns] - """ if self._mesh is None: raise APIUseError("Need to make_mesh() before joining the coordinates.") @@ -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 + [37884 rows x 15 columns] TODO