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

Feat/Boundary conditions listing wrappers #819

Merged
merged 93 commits into from
Jan 11, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
93 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
31cb8a6
Improving test.
germa89 Jan 10, 2022
6359b95
Merge branch 'feat/richer_cmd_output-dataframes-and-BC-list' into fea…
germa89 Jan 10, 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
f52e273
Added code suggestions.
germa89 Jan 11, 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
52 changes: 49 additions & 3 deletions ansys/mapdl/core/commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,20 @@
pip install pandas
"""

MSG_BCListingOutput_to_array = """This command has strings values in some of its columns (such 'UX', 'FX', 'UY', 'TEMP', etc),
so it cannot be converted to Numpy Array.

Please use 'to_list' or 'to_dataframe' instead."""
germa89 marked this conversation as resolved.
Show resolved Hide resolved


# Identify where the data start in the output
GROUP_DATA_START = ['NODE', 'ELEM']

# Allowed commands to get output as array or dataframe.
# In theory, these commands should follow the same format.
# Some of them are not documented (already deprecated?)
# So they won't be wrapped.
# So they are not in the Mapdl class,
# so they won't be wrapped.
CMD_LISTING = [
'NLIN', # not documented
'PRCI',
Expand Down Expand Up @@ -66,6 +73,8 @@
'SWLI'
]

CMD_BC_LISTING = ['FLIS', 'DLIS']

# Adding empty lines to match current format.
docstring_injection = """
Returns
Expand Down Expand Up @@ -371,6 +380,16 @@ class Commands(

"""Wrapped MAPDL commands"""

def _requires_pandas(func):
"""Wrapper that check ``HAS_PANDAS``, if not, it will raise an exception."""

def func_wrapper(self, *args, **kwargs):
if HAS_PANDAS:
return func(self, *args, **kwargs)
else:
raise ModuleNotFoundError(MSG_NOT_PANDAS)
return func_wrapper


class CommandOutput(str):
"""Custom string subclass for handling the commands output.
Expand Down Expand Up @@ -556,7 +575,7 @@ def to_array(self):
"""
return np.array(self.to_list(), dtype=float)

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

Returns
Expand All @@ -567,4 +586,31 @@ def to_dataframe(self):
import pandas as pd
except ModuleNotFoundError:
raise ModuleNotFoundError(MSG_NOT_PANDAS)
return pd.DataFrame(data=self.to_array(), columns=self.get_columns())

if not data:
data = self.to_array()
if not columns:
columns = self.get_columns()

return pd.DataFrame(data=data, columns=data)


class BoundaryConditionsListingOutput(CommandListingOutput):
def to_array(self):
raise ValueError(MSG_BCListingOutput_to_array)

def to_dataframe(self):
df = super().to_dataframe(data=self.to_list())
if 'NODE' in df.columns:
df['NODE'] = df['NODE'].astype(int)

if 'LABEL' in df.columns:
df['LABEL'] = df['LABEL'].astype(str)

if 'REAL' in df.columns:
df['REAL'] = df['REAL'].astype(float)

if 'IMAG' in df.columns:
df['IMAG'] = df['IMAG'].astype(float)

return df
14 changes: 13 additions & 1 deletion ansys/mapdl/core/mapdl.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
from ansys.mapdl.core.errors import MapdlRuntimeError, MapdlInvalidRoutineError
from ansys.mapdl.core.plotting import general_plotter
from ansys.mapdl.core.post import PostProcessing
from ansys.mapdl.core.commands import Commands, CommandListingOutput, CMD_LISTING, inject_docs
from ansys.mapdl.core.commands import Commands, CommandListingOutput, BoundaryConditionsListingOutput, CMD_LISTING, CMD_BC_LISTING, inject_docs
from ansys.mapdl.core.inline_functions import Query
from ansys.mapdl.core import LOG as logger
from ansys.mapdl.reader.rst import Result
Expand Down Expand Up @@ -177,11 +177,23 @@ def inner_wrapper(*args, **kwargs):
return CommandListingOutput(func(*args, **kwargs))
return inner_wrapper

def wrap_BC_listing_function(func):
# Injecting doc string modification
func.__func__.__doc__ = inject_docs(func.__func__.__doc__)
@wraps(func)
def inner_wrapper(*args, **kwargs):
return BoundaryConditionsListingOutput(func(*args, **kwargs))
return inner_wrapper

for name in dir(self):
if name[0:4].upper() in CMD_LISTING:
func = self.__getattribute__(name)
setattr(self, name, wrap_listing_function(func))

if name[0:4].upper() in CMD_BC_LISTING:
func = self.__getattribute__(name)
setattr(self, name, wrap_BC_listing_function(func))

@property
def _name(self): # pragma: no cover
"""Implemented by child class"""
Expand Down
1 change: 0 additions & 1 deletion ansys/mapdl/core/mapdl_grpc.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,6 @@
from ansys.mapdl.core import __version__, _LOCAL_PORTS
from ansys.mapdl.core import check_version


TMP_VAR = '__tmpvar__'
VOID_REQUEST = anskernel.EmptyRequest()

Expand Down
49 changes: 44 additions & 5 deletions tests/test_commands.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
import pytest
import inspect
from ansys.mapdl.core.commands import CommandOutput as CommandOutput

import numpy as np

from ansys.mapdl.core import examples

from ansys.mapdl.core.commands import CommandListingOutput
from ansys.mapdl.core.commands import CommandOutput
from ansys.mapdl.core.commands import CommandOutput, CommandListingOutput, BoundaryConditionsListingOutput
from ansys.mapdl.core.commands import CMD_LISTING, CMD_BC_LISTING

try:
import pandas as pd
Expand Down Expand Up @@ -60,6 +58,8 @@
'kcmplx': 1
}

CMD_DOC_STRING_INJECTOR = CMD_LISTING.copy()
CMD_DOC_STRING_INJECTOR.extend(CMD_BC_LISTING)

@pytest.fixture(scope="module")
def plastic_solve(mapdl):
Expand All @@ -72,6 +72,16 @@ def plastic_solve(mapdl):
mapdl.set(1, 2)
mapdl.mute = False

@pytest.fixture(scope="module")
def beam_solve(mapdl):
mapdl.mute = True
mapdl.finish()
mapdl.clear()
mapdl.input(examples.verif_files.vmfiles["vm10"])

mapdl.post1()
mapdl.set(1, 2)
mapdl.mute = False

def test_cmd_class():
output = """This is the output.
Expand Down Expand Up @@ -105,7 +115,6 @@ def test_inquire_functions(mapdl, func):
assert '=' in output


# @pytest.mark.skipif(not HAS_GRPC, reason="Requires GRPC")
@pytest.mark.parametrize('func,args', [
('prnsol', ('U', 'X')),
('presol', ('S', 'X')),
Expand All @@ -126,3 +135,33 @@ def test_output_listing(mapdl, plastic_solve, func, args):
if HAS_PANDAS:
out_df = out.to_dataframe()
assert isinstance(out_df, pd.DataFrame) and not out_df.empty

germa89 marked this conversation as resolved.
Show resolved Hide resolved

@pytest.mark.parametrize('func', ['dlist', 'flist'])
def test_bclist(mapdl, beam_solve, func):
func_ = getattr(mapdl, func)
out = func_()

assert isinstance(out, BoundaryConditionsListingOutput)
assert isinstance(out.to_list(), list) and bool(out.to_list())

with pytest.raises(ValueError):
out.to_array()

if HAS_PANDAS:
out_df = out.to_dataframe()
assert isinstance(out_df, pd.DataFrame) and not out_df.empty


@pytest.mark.parametrize('method', CMD_DOC_STRING_INJECTOR)
def test_docstring_injector(mapdl, method):
"""Check if the docstring has been injected."""
for name in dir(mapdl):
if name[0:4].upper() == method:
func = mapdl.__getattribute__(name)
docstring = func.__doc__ # If '__func__' not present (AttributeError) very likely it has not been wrapped.

assert "Returns" in docstring
assert "``str.to_list()``" in docstring
assert "``str.to_array()``" in docstring
assert "``str.to_dataframe()``" in docstring