Skip to content

Turn process classes into callables (unit-testing) #50

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

Closed
benbovy opened this issue Oct 6, 2018 · 3 comments
Closed

Turn process classes into callables (unit-testing) #50

benbovy opened this issue Oct 6, 2018 · 3 comments

Comments

@benbovy
Copy link
Member

benbovy commented Oct 6, 2018

From discussion in #49, one big limitation of using classes to define model processes is that these are not easy to test.

One solution to this issue would be to programmatically create a callable from a process class. This should be relatively easy to implement.

For example, considering this process class

@xs.process
class MyProcess(object):
    in_var1 = xs.variable(intent='in', default=0)
    in_var2 = xs.variable(intent='in', default='a')

    out_var1 = xs.variable(intent='out')
    out_var2 = xs.variable(intent='out')

    # simulation runtime methods
    def run_step(self):
        # ...

there would be a xs.process_to_func helper

my_process = xs.process_to_func(MyProcess)

such that my_process is a callable equivalent to

def my_process(in_var1=0, in_var2='a'):
    # execute runtime code in class MyProcess...
    
    return (out_var1, out_var2)    # or use a dict instead

Regarding the implementation details, my_process would actually correspond to a callable class that encapsulates:

  • the creation of a minimal model based only on MyProcess
  • the creation of a basic store for the simulation data
  • the execution of one simulation (go through the execution of runtime methods of MyProcess)

We could also programmatically add the docstrings of my_process.

@mancellin
Copy link

Just a quick thought:

inout variables can be supported when using functions.
For instance, the function

def f(a):
    a += 1
    return a

has an inout parameter.

@benbovy
Copy link
Member Author

benbovy commented Oct 8, 2018

Yes, I haven't considered all cases (e.g., on-demand and/or group variables) yet in my proposition above, but for inout variables this is indeed quite straightforward, they should be added as both input arguments and returned values.

I should also note here that the generated callables are not intended to be added as processes in a model (we could just use the process classes for that). As we discussed in #49, programmatically convert any callable to a process class (i.e., MyProcess = xs.func_to_process(my_process)) would be very handy too! In the later case, inout variables do not always need to be returned by the callable, it could be mutable inputs such as lists or numpy arrays.

@benbovy
Copy link
Member Author

benbovy commented Jan 16, 2020

Closed in #63.

@benbovy benbovy closed this as completed Jan 16, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants