ansys / pymapdl

Pythonic interface to MAPDL
https://mapdl.docs.pyansys.com
MIT License
428 stars 120 forks source link

Richer `run` command output #735

Closed germa89 closed 2 years ago

germa89 commented 2 years ago

Current situation

Commands like ELIST, NLIST, etc. returns a string, which is fine. Other commands such as PRNSOL and similar, returns also a string.

This is the default behavior.

Proposition

I believe that output from the commands should be "richer". For example, we should parse commands like ELIST to output a table.

To not break compatibility, we could make an object (CommandOutput ?) which its representation (__repr__) is the command output, hence we could keep the current code.

Progress

Example


class CommandOutput():

    def __repr__(self): 
        # Used when ``>>> CommandOutput``
        return self.cmd_output 

    def __str__(self):
        # Used when ``>>> print(CommandOutput())``

        return self.cmd_output

    def __init__(self, cmd, string_):
        self.cmd_output = string_
        self.cmd = cmd

        if cmd in ['ELIST', 'NLIST']:
            #parse_XLIST_command()
            pass

    def parse_XLIST_command(self):
        self.table = np.([[1,2,3],[1,2,3]])

out = 'SELECT       FOR ITEM = ENAM COMPONENT = n  IN RANGE       170 TO        170 STEP          1\n\n        531  ELEMENTS(OF       7891  DEFINED) SELECTED BY  ESEL  COMMAND.'

out = CommandOutput('ESEL', out)
out

This could be used in the output of run:

    def run(self, command, write_to_log=True, **kwargs):
        """Run single APDL command.

        For multiple commands, use ``run_multiline``.

        Parameters
        ----------
        ... 

        """
        # Skipped code
        # ...

        # special returns for certain geometry commands
        short_cmd = parse_to_short_cmd(command)

        response = CommandOutput(command, self._response) #New bit

        # return self._response #Old one
        return response
akaszynski commented 2 years ago

Prototype this. I agree that a simple string isn't sufficient sometimes.

akaszynski commented 2 years ago

Also, thinking about this more, provided that we can maintain backwards compatibility, being able to represent for jupyter notebooks would be really powerful as well.

I'm thinking something like this for returning the command output as different array types.

The point of this is, always return string, but allow output as native python types (array, list, pandas, etc.)


class CommandOutput():

    def __init__(self, cmd, string_):
        self._cmd_output = string_
        self._cmd = cmd
        self._str_parse = None

    def __repr__(self): 
        # consider adding ipython representation
        return self._cmd_output 

    def __str__(self):
        return self._cmd_output

    def _parse(self):
        """Prettify the command output into a more readable string

        Something along the lines of: https://docs.python.org/3/library/pprint.html

        """
        if _cmd in ['ELIST', 'NLIST']:
            # parse_xlist_command()
            pass

    def _parse_xlist_command(self):
        return # parse ...

    def as_array(self):
        """Return the command output to a numpy array"""
        if self._cmd in ['PRNSOL']:
            # do something
        else:
            raise TypeError("This command output cannot be converted to a ``numpy.ndarray``")

        return array

    def as_dataframe(self):
        """Return the command output to a ``pandas.DataFrame`` if available."""
        if self._cmd in ['PRNSOL']:
            # do something
        else:
            raise TypeError("This command output cannot be converted to a ``pandas.dataframe``")

        return df