Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add docs to command output #822

Merged
merged 111 commits into from
Jan 13, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
111 commits
Select commit Hold shift + click to select a range
2ef7767
Changed signal termination
germa89 Dec 20, 2021
5f84f1c
Adding CommandOutput class.
germa89 Dec 20, 2021
9dc57fc
Substituting the output in mapdl_grpc for custom class.
germa89 Dec 20, 2021
2c508bb
Merge branch 'fix/os-error-when-using-os.kill-in-unit-testing' into f…
germa89 Dec 20, 2021
28a838f
Making sure all the methods call the parent class (str)
germa89 Dec 20, 2021
18525a7
Testing simplied version
germa89 Dec 20, 2021
9c2b52f
Added unit tests
germa89 Dec 21, 2021
915444c
Added unit tests
germa89 Dec 21, 2021
652ca3d
Fixing the style
germa89 Dec 21, 2021
c8ec465
Merge branch 'feat/richer-command-output' of https://github.com/pyans…
germa89 Dec 21, 2021
9cbda1e
Fixing grammar.
germa89 Dec 21, 2021
52c3e4a
Fixing grammar.
germa89 Dec 21, 2021
1ff363e
Merge branch 'feat/richer-command-output' of https://github.com/pyans…
germa89 Dec 21, 2021
12b143f
Merge branch 'feat/richer-command-output' of https://github.com/pyans…
germa89 Dec 21, 2021
8bc2768
Add test_class unit
germa89 Dec 21, 2021
64dbd92
Using first implementation because the second fail because of the mod…
germa89 Dec 21, 2021
c110b88
Changing implementation to not overwrite __class__.
germa89 Dec 21, 2021
9fdc7f7
Fixing sphinx building by rewriting __class__ method to not be overwr…
germa89 Dec 21, 2021
53aab29
Style check.
germa89 Dec 21, 2021
d98bd79
changing the API, cmd=command, and command= full command (cmd + args)
germa89 Dec 28, 2021
edde78f
Adding CommandOutputDataframe class.
germa89 Dec 28, 2021
3f17960
Small changes.
germa89 Dec 28, 2021
71665ec
Big restructure.
germa89 Dec 29, 2021
bcfd4b8
Added check if output is modified by /verify
germa89 Dec 29, 2021
bdb0ae3
Added docstrings to classes.
germa89 Dec 29, 2021
d7a15d2
Moved fixtures to main conf file
germa89 Dec 29, 2021
2bbe92d
Changed class name, added supported commands.
germa89 Dec 29, 2021
3aece4b
Added test units.
germa89 Dec 29, 2021
252660a
Added more unit test.
germa89 Dec 29, 2021
9449878
Fixing style.
germa89 Dec 29, 2021
0d4156e
small changes.
germa89 Dec 29, 2021
3210e24
small change.
germa89 Dec 29, 2021
a3e382b
incorrect package name.
germa89 Dec 30, 2021
4471208
UserString Implementation
germa89 Jan 3, 2022
ebcf76d
Simplification of unit test.
germa89 Jan 3, 2022
78c5fba
Simplification of unit test.
germa89 Jan 3, 2022
f9506b5
Merge
germa89 Jan 4, 2022
8cf5632
Using str as base class.
germa89 Jan 4, 2022
18c7364
Merge branch 'feat/richer-command-output' into feat/richer_cmd_output…
germa89 Jan 4, 2022
9c50fe9
removed unused import.
germa89 Jan 4, 2022
4cddd9c
Merge branch 'main' into feat/richer-command-output
akaszynski Jan 5, 2022
bf428a7
Generalization of 'CommandListing' based on function 'paprnt.F'.
germa89 Jan 5, 2022
18e76dd
Apply suggestions from code review
germa89 Jan 5, 2022
abf5b76
Merge branch 'feat/richer-command-output' into feat/richer_cmd_output…
germa89 Jan 5, 2022
4fa9a5e
Cleanning some commented code.
germa89 Jan 10, 2022
c646be0
Added automatical wrapper for listing functions.
germa89 Jan 10, 2022
b2760a7
Removed automatical wrapper.
germa89 Jan 10, 2022
ae7e09d
Fixing style.
germa89 Jan 10, 2022
db2b507
changed method name to `to_list`
germa89 Jan 10, 2022
546f96a
Fixing style
germa89 Jan 10, 2022
28353b6
Added unit test.
germa89 Jan 10, 2022
e646b2c
Fixing grammar.
germa89 Jan 10, 2022
462e7f3
Added dlist wrapper
germa89 Jan 10, 2022
b72c8f2
Externalizing @requires_pandas
germa89 Jan 10, 2022
08e21fa
Fixing conftest?
germa89 Jan 10, 2022
b84330d
Improving unit test to avoid empty object false assertion.
germa89 Jan 10, 2022
c5ccd61
Fixing unit test
germa89 Jan 10, 2022
9dd3355
replacing solve for one which does not change format.
germa89 Jan 10, 2022
5ed6011
checking format
germa89 Jan 10, 2022
2a65788
Fixing unit test mess
germa89 Jan 10, 2022
7257a78
testing disabling new test.
germa89 Jan 10, 2022
52595ed
removing unused imports
germa89 Jan 10, 2022
cb1bc8a
removing unused imports
germa89 Jan 10, 2022
13fe3ae
test
germa89 Jan 10, 2022
29de4b4
fixing wrong check against empty array and df
germa89 Jan 10, 2022
2a620b3
Removed unused import
germa89 Jan 10, 2022
d5b20ed
Merge branch 'feat/richer_cmd_output-dataframes-and-BC-list' into fea…
germa89 Jan 10, 2022
27dcb6e
Changing class name.
germa89 Jan 10, 2022
03aaa10
Merge branch 'main' into feat/richer_cmd_output-dataframes-and-BC-list
akaszynski Jan 10, 2022
b17baed
Merge branch 'main' into feat/dlist-command-output-wrapper
germa89 Jan 10, 2022
f14bc7f
Cleaning
germa89 Jan 10, 2022
aed9747
Cleaning again.
germa89 Jan 10, 2022
21c0bd5
Fixing style.
germa89 Jan 10, 2022
7b8b078
Fixing style.
germa89 Jan 10, 2022
1480eb4
Update ansys/mapdl/core/commands.py
germa89 Jan 10, 2022
47b3206
Added output checker
germa89 Jan 10, 2022
dd94b8b
Adding suggestions.
germa89 Jan 10, 2022
ebdc5bb
Fixing wrong wrapper.
germa89 Jan 10, 2022
df47754
Adding doc
germa89 Jan 10, 2022
31cb8a6
Improving test.
germa89 Jan 10, 2022
6359b95
Merge branch 'feat/richer_cmd_output-dataframes-and-BC-list' into fea…
germa89 Jan 10, 2022
874187a
Merge branch 'feat/dlist-command-output-wrapper' into docs/command-ou…
germa89 Jan 10, 2022
0846b4c
Added snippets example
germa89 Jan 10, 2022
e6e6a73
Fixing table.
germa89 Jan 11, 2022
528898d
Fixing table.
germa89 Jan 11, 2022
1ad2979
Improving table.
germa89 Jan 11, 2022
7cdf8e9
Improving table.
germa89 Jan 11, 2022
719e463
Moving the wrapping to main Mapdl, being common to gpc, console and c…
germa89 Jan 11, 2022
0b9ba45
Added docstring injector.
germa89 Jan 11, 2022
b3f7dda
Apply suggestions from code review
akaszynski Jan 11, 2022
5fce87a
Apply suggestions from code review
akaszynski Jan 11, 2022
995b4cd
Replacing the check of the pandas package.
germa89 Jan 11, 2022
708cb5c
importing pandas lazily
germa89 Jan 11, 2022
6ad1bba
removed trailing space.
germa89 Jan 11, 2022
6a969b9
Merge branch 'feat/richer_cmd_output-dataframes-and-BC-list' into fea…
germa89 Jan 11, 2022
aa76d63
Merge branch 'main' into feat/dlist-command-output-wrapper
germa89 Jan 11, 2022
c9fd6e6
Added docstring injector to dlist and flist
germa89 Jan 11, 2022
fd58da1
removing grpc wrapper
germa89 Jan 11, 2022
7a45727
Adding unit test for docstring injector.
germa89 Jan 11, 2022
d13e443
Merge branch 'feat/dlist-command-output-wrapper' into docs/command-ou…
germa89 Jan 11, 2022
101fb79
Merge branch 'main' into docs/command-output-doc
germa89 Jan 11, 2022
314f425
Improving docstrings
germa89 Jan 13, 2022
461ae04
adding command api doc
germa89 Jan 13, 2022
d0a6bea
Adding reference to commands api doc and reorganizing the sections by…
germa89 Jan 13, 2022
410abe6
Adding import convert_apdl_block to init.
germa89 Jan 13, 2022
f2323af
Fixing some function hyperlinks
germa89 Jan 13, 2022
a381aa6
Improving command output docs
germa89 Jan 13, 2022
91e3ad6
Update doc/source/user_guide/post.rst
germa89 Jan 13, 2022
d01ede9
Apply suggestions from code review
germa89 Jan 13, 2022
2dc21ea
Applying review suggestions.
germa89 Jan 13, 2022
3335256
Merge branch 'main' into docs/command-output-doc
germa89 Jan 13, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion ansys/mapdl/core/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
from pyvista.utilities.sphinx_gallery import _get_sg_image_scraper

from ansys.mapdl.core._version import __version__
from ansys.mapdl.core.convert import convert_script
from ansys.mapdl.core.convert import convert_script, convert_apdl_block
from ansys.mapdl.core.launcher import (
launch_mapdl,
change_default_ansys_path,
Expand Down
56 changes: 52 additions & 4 deletions ansys/mapdl/core/commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
misc,
inq_func
)

from functools import wraps
import re
import numpy as np

Expand Down Expand Up @@ -173,6 +173,7 @@ def inject_docs(docstring):
def check_valid_output(func):
"""Wrapper that check if output can be wrapped by pandas, if not, it will raise an exception."""

@wraps(func)
def func_wrapper(self, *args, **kwargs):
output = self.__str__()
if '*** WARNING ***' in output or '*** ERROR ***' in output: # Error should be caught in mapdl.run.
Expand Down Expand Up @@ -571,16 +572,38 @@ def to_array(self):

Returns
-------
Numpy array
numpy.ndarray
Numpy array of floats.
"""
return np.array(self.to_list(), dtype=float)

def to_dataframe(self, data=None, columns=None):
"""Export the command output as a Pandas DataFrame.

Parameters
----------
data : numpy.ndarray (structured or homogeneous), Iterable, dict, or DataFrame
The data to be converted to the dataframe values.
Passed directly to the pandas.DataFrame constructor.
Dict can contain Series, arrays, constants, dataclass or list-like objects. If
data is a dict, column order follows insertion-order.

columns : Index or array-like
Iterable with columns names.
Passed directly to the pandas.DataFrame constructor.
Column labels to use for resulting frame when data does not have them,
defaulting to RangeIndex(0, 1, 2, ..., n). If data contains column labels,
will perform column selection instead.

Returns
-------
Pandas Dataframe
pandas.DataFrame
Pandas DataFrame

Notes
-----
The returned dataframe has all its data converted to float
(inheritate from :func:`to_array() <ansys.mapdl.core.commands.CommandListingOutput.to_array>` method).
"""
try:
import pandas as pd
Expand All @@ -596,10 +619,35 @@ def to_dataframe(self, data=None, columns=None):


class BoundaryConditionsListingOutput(CommandListingOutput):
"""Allow the conversion of command output to native Python types.

Custom class for handling the boundary condition listing commands
whose output is sensible to be converted to a list of lists,
or a Pandas DataFrame.
"""

def to_array(self):
raise ValueError(MSG_BCListingOutput_to_array)

def to_dataframe(self):
"""Convert the command output to a Pandas Dataframe.

Returns
-------
pandas.DataFrame
Pandas Dataframe

Notes
-----

If present, the next columns will be converted to:

* ``'NODE'``: int
* ``'LABEL'``: str
* ``'REAL'``: float
* ``'IMAG'``: float

"""
df = super().to_dataframe(data=self.to_list())
if 'NODE' in df.columns:
df['NODE'] = df['NODE'].astype(int)
Expand All @@ -613,4 +661,4 @@ def to_dataframe(self):
if 'IMAG' in df.columns:
df['IMAG'] = df['IMAG'].astype(float)

return df
return df
32 changes: 32 additions & 0 deletions doc/source/api/commands.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
.. _ref_commands_api:

Commands Output
===============

Various PyMAPDL classes and commands which helps in data post-processing.

All these classes are subclasses of ``str`` class, hence they inherit all the
methods and attributes of :class:`string`.

.. currentmodule:: ansys.mapdl.core.commands


.. We should add the line ':toctree: _autosummary' to the following classes autosummary
to remove the warning during building, however, it shows only the methods links in the
sidebar (toctree), not the classes, and since both classes have the same method name, it
is confusing.

.. autoclass:: ansys.mapdl.core.commands.CommandListingOutput

.. autosummary::

CommandListingOutput.to_list
CommandListingOutput.to_array
CommandListingOutput.to_dataframe

.. autoclass:: ansys.mapdl.core.commands.BoundaryConditionsListingOutput

.. autosummary::

BoundaryConditionsListingOutput.to_list
BoundaryConditionsListingOutput.to_dataframe
14 changes: 8 additions & 6 deletions doc/source/api/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,18 @@ MAPDL commands mapped to PyMAPDL, see :ref:`ref_mapdl_commands`.
:maxdepth: 2
:hidden:

commands
geometry
helper
inline
logging
mapdl
math
plotting
helper
mesh
xpl
parameters
geometry
plotting
pool
post
solution
inline
logging
xpl

5 changes: 2 additions & 3 deletions doc/source/user_guide/convert.rst
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,7 @@ using :attr:`Mapdl.parameters <ansys.mapdl.core.Mapdl.parameters>` with:

Script Translation
~~~~~~~~~~~~~~~~~~
Existing ANSYS scripts can be translated using :func:`convert_script()
<ansys.mapdl.core.convert_script>`
Existing ANSYS scripts can be translated using :func:`convert_script() <ansys.mapdl.core.convert_script>`

.. code:: python

Expand All @@ -50,7 +49,7 @@ Existing ANSYS scripts can be translated using :func:`convert_script()
pymapdl.convert_script(inputfile, pyscript)

Or additionally, you can convert code in form of strings for later processing
using :func:`convert_apdl_block() <ansys.mapdl.core.convert.convert_apdl_block>`:
using :func:`convert_apdl_block() <ansys.mapdl.core.convert_apdl_block>` :

.. code:: python

Expand Down
93 changes: 84 additions & 9 deletions doc/source/user_guide/post.rst
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
Post-Processing
===============
You can post process using an active MAPDL session using the
:attr:`Mapdl.post_processing <ansys.mapdl.core.Mapdl.post_processing>`
attrbute of an instance of :class:`Mapdl
<ansys.mapdl.core.mapdl._MapdlCore>`. One advantage of this approach
:class:`Mapdl.post_processing <ansys.mapdl.core.post.PostProcessing>`
an attribute of an instance of :class:`Mapdl <ansys.mapdl.core.mapdl._MapdlCore>`.
One advantage of this approach
is it integrates well with existing MAPDL scripting or automation, but
can also be carried out on result files generated from other programs,
including ANSYS Mechanical.
Expand All @@ -23,6 +23,83 @@ streamed back to the client for review or visualization.
via a Python client.



Enriched Command Output
~~~~~~~~~~~~~~~~~~~~~~~
All :class:`Mapdl <ansys.mapdl.core.mapdl._MapdlCore>` commands output
a string object which can be parsed to obtain specific data from it.

In certain :class:`Mapdl <ansys.mapdl.core.mapdl._MapdlCore>` commands
the returned string contains some methods to process the output.
These commands are listed in Table-1_.

.. _Table-1:

**Table 1. Commands with extra processing methods in the output.**

+----------------+---------------------------------------------------------------------------------------------------+----------------------------------------------------------+
| Category | Extra Methods Available | Mapdl Commands |
+================+===================================================================================================+==========================================================+
| **Listing** | * :func:`to_list() <ansys.mapdl.core.commands.CommandListingOutput.to_list>` | * :func:`prcint() <ansys.mapdl.core.mapdl._MapdlCore>` |
| | * :func:`to_array() <ansys.mapdl.core.commands.CommandListingOutput.to_array>` | * :func:`prenergy() <ansys.mapdl.core.mapdl._MapdlCore>` |
| | * :func:`to_dataframe() <ansys.mapdl.core.commands.CommandListingOutput.to_dataframe>` | * :func:`prerr() <ansys.mapdl.core.mapdl._MapdlCore>` |
| | | * :func:`presol() <ansys.mapdl.core.mapdl._MapdlCore>` |
| | | * :func:`pretab() <ansys.mapdl.core.mapdl._MapdlCore>` |
| | | * :func:`print() <ansys.mapdl.core.mapdl._MapdlCore>` |
| | | * :func:`priter() <ansys.mapdl.core.mapdl._MapdlCore>` |
| | | * :func:`prjsol() <ansys.mapdl.core.mapdl._MapdlCore>` |
| | | * :func:`prnld() <ansys.mapdl.core.mapdl._MapdlCore>` |
| | | * :func:`prnsol() <ansys.mapdl.core.mapdl._MapdlCore>` |
| | | * :func:`prorb() <ansys.mapdl.core.mapdl._MapdlCore>` |
| | | * :func:`prpath() <ansys.mapdl.core.mapdl._MapdlCore>` |
| | | * :func:`prrfor() <ansys.mapdl.core.mapdl._MapdlCore>` |
| | | * :func:`prrsol() <ansys.mapdl.core.mapdl._MapdlCore>` |
| | | * :func:`prsect() <ansys.mapdl.core.mapdl._MapdlCore>` |
| | | * :func:`prvect() <ansys.mapdl.core.mapdl._MapdlCore>` |
| | | * :func:`stat() <ansys.mapdl.core.mapdl._MapdlCore>` |
| | | * :func:`swlist() <ansys.mapdl.core.mapdl._MapdlCore>` |
+----------------+---------------------------------------------------------------------------------------------------+----------------------------------------------------------+
| **Boundary** | * :func:`to_list() <ansys.mapdl.core.commands.BoundaryConditionsListingOutput.to_list>` | * :func:`dlist() <ansys.mapdl.core.mapdl._MapdlCore>` |
| **Conditions** | * :func:`to_dataframe() <ansys.mapdl.core.commands.BoundaryConditionsListingOutput.to_dataframe>` | * :func:`flist() <ansys.mapdl.core.mapdl._MapdlCore>` |
| **Listing** | | |
+----------------+---------------------------------------------------------------------------------------------------+----------------------------------------------------------+

Here's a simple example demonstrating the the usage:

.. code:: python


>>> from ansys.mapdl.core import launch_mapdl
>>> from ansys.mapdl.core import examples

>>> mapdl = launch_mapdl()
>>> example = examples.vmfiles['vm10']
>>> mapdl.input(example)

>>> mapdl.slashsolu()
>>> mapdl.solve()

>>> mapdl.post1()
>>> cmd = mapdl.prnsol('U', 'X')

Output as a list.

>>> cmd.to_list()
[['1', '0.0000'], ['2', '0.0000']]

Output as array.

>>> cmd.to_array()
array([[1., 0.],
[2., 0.]])

Output as dataframe.

>>> cmd.to_dataframe()
NODE UX
0 1.0
1 2.0

Examples
~~~~~~~~
Classically, one would request nodal results from MAPDL using the
Expand Down Expand Up @@ -55,8 +132,8 @@ Classically, one would request nodal results from MAPDL using the
MORE (YES,NO OR CONTINUOUS)=


However, using an instance of :class:`Mapdl
<ansys.mapdl.core.mapdl._MapdlCore>`, you can instead request the
However, using an instance of :class:`Mapdl <ansys.mapdl.core.mapdl._MapdlCore>`,
you can instead request the
nodal displacement with:

.. code:: python
Expand All @@ -82,10 +159,8 @@ Selected Nodes
The MAPDL database processes some results independently of if nodes or
elements are selected. If you have subselected a certain component
and wish to also limit the result of a certain output
(i.e. :func:`nodal_displacement()
<ansys.mapdl.core.post.PostProcessing.nodal_displacement`), use the
:attr:`selected_nodes
<ansys.mapdl.core.post.PostProcessing.selected_nodes` attribute to get
(i.e. :func:`nodal_displacement() <ansys.mapdl.core.post.PostProcessing.nodal_displacement>`),
use the :attr:`selected_nodes <ansys.mapdl.core.post.PostProcessing.selected_nodes>` attribute to get
a mask of the currently selected nodes.

.. code::
Expand Down