Skip to content

Commit

Permalink
updated datageneration base class
Browse files Browse the repository at this point in the history
  • Loading branch information
mpvanderschelling committed Sep 7, 2023
1 parent fda9bda commit 8f380e9
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 19 deletions.
39 changes: 32 additions & 7 deletions docs/source/rst_doc_files/classes/datageneration/datagenerator.rst
Original file line number Diff line number Diff line change
Expand Up @@ -87,22 +87,47 @@ The datagenerator protocol class has three main methods:

.. note::

The :meth:`~f3dasm.datagenerationr.datagenerator.DataGenerator.__call__` method chains these three methods together.
The :meth:`~f3dasm.datagenerationr.datagenerator.DataGenerator.run` method chains these three methods together and is inherited from the parent class.


In order to create your own data-generator, you need to

1. inherit from the :class:`~f3dasm.datageneration.datagenerator.DataGenerator` class
2. implement at least the :meth:`~f3dasm.datageneration.datagenerator.DataGenerator.execute` method
1. inherit from the :class:`~f3dasm.datageneration.datagenerator.DataGenerator` class.
2. implement at least the :meth:`~f3dasm.datageneration.datagenerator.DataGenerator.execute` method, the :meth:`~f3dasm.datageneration.datagenerator.DataGenerator.pre_process` and :meth:`~f3dasm.datageneration.datagenerator.DataGenerator.post_process` are optional.

The :meth:`~f3dasm.datageneration.datagenerator.DataGenerator.execute` method should have the following signature:
The the :meth:`~f3dasm.datageneration.datagenerator.DataGenerator.pre_process`, :meth:`~f3dasm.datageneration.datagenerator.DataGenerator.execute` and :meth:`~f3dasm.datageneration.datagenerator.DataGenerator.post_process` have to accept arbitrary key-word arguments (:code:`**kwargs`).
This is because the :meth:`~f3dasm.datageneration.datagenerator.DataGenerator.run` method will pass any key-word arguments to these methods.

An example is given in the following code block:

.. code-block:: python
def execute(self, design: f3dasm.Design, **kwargs) -> f3dasm.Design:
# do something with the design
return design
class MySimulator(DataGenerator):
def __init__(self, simulator_specific_parameters: Any):
self.simulator_specific_parameters = simulator_specific_parameters
def pre_process(self, any_pre_process_arg: str, **kwargs) -> None:
...
def execute(self, any_argument: str, **kwargs) -> None:
# Retrieve parameters
parameter_1 = self.design['parameter1']
# Run a simulation
...
# Store the results
self.design['result'] = result
def post_process(self, any_post_process_arg: str, **kwargs) -> None:
...
In order to run this simulator, you need to create an instance of the class and pass it to the :meth:`~f3dasm.design.experimentdata.ExperimentData.run` function:

.. code-block:: python
simulator = MySimulator(simulator_specific_parameters)
experimentdata.run(simulator.run, method='sequential', kwargs={'any_pre_process_arg': 'pre', 'any_argument': 'arg', 'any_post_process_arg': 'post'})
Create a data-generator from a functional approach
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Expand Down
8 changes: 3 additions & 5 deletions docs/source/rst_doc_files/general/gettingstarted.rst
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,9 @@ Installing the latest release

:mod:`f3dasm` is purely Python code and compatible with:

1. Python 3.7 to 3.10.
1. Python 3.7 or higher.
2. the three major operations system (Linux, MacOS, Ubuntu).
3. the default environment of Google Colab (Python 3.8, Linux)
4. the `pip <https://pypi.org/>`_ package manager system.
3. the `pip <https://pypi.org/>`_ package manager system.

----

Expand Down Expand Up @@ -106,7 +105,6 @@ Installing from source
- Studies
- Test suite
- Documentation source
- Tutorial notebooks

Building from source is required to work on a contribution (bug fix, new feature, code or documentation improvement). We recommend using a `Linux distribution system <https://releases.ubuntu.com/focal/>`_.

Expand All @@ -123,7 +121,7 @@ Building from source is required to work on a contribution (bug fix, new feature
2. Install a recent version of Python (3.7, 3.8, 3.9 or 3.10)
for instance using `Miniconda3 <https://docs.conda.io/en/latest/miniconda.html>`_.
for instance using `Miniconda3 <https://docs.conda.io/en/latest/miniconda.html>`_. or `Mamba <https://github.com/conda-forge/miniforge#install>`_.
If you installed Python with conda, we recommend to create a dedicated
conda environment with all the build dependencies of f3dasm:

Expand Down
18 changes: 11 additions & 7 deletions src/f3dasm/datageneration/datagenerator.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,16 @@
class DataGenerator:
"""Base class for a data generator"""

def pre_process(self, design: Design, **kwargs) -> None:
def pre_process() -> None:
"""Function that handles the pre-processing"""
...
# raise NotImplementedError("No pre-process function implemented!")

def execute(self, design: Design, **kwargs) -> None:
def execute() -> None:
"""Function that calls the FEM simulator the pre-processing"""
raise NotImplementedError("No execute function implemented!")

def post_process(self, design: Design, **kwargs) -> None:
def post_process() -> None:
"""Function that handles the post-processing"""
...

Expand All @@ -48,7 +48,11 @@ def run(self, design: Design, **kwargs) -> Design:
Design
Processed design
"""
self.pre_process(design, **kwargs)
design = self.execute(design, **kwargs)
self.post_process(design, **kwargs)
return design
# Cache the design
self.design: Design = design

self.pre_process(**kwargs)
self.execute(**kwargs)
self.post_process(**kwargs)

return self.design

0 comments on commit 8f380e9

Please sign in to comment.