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

[SofaPython] PythonScriptDataEngine (PSDE) #583

Merged
merged 51 commits into from Mar 26, 2018

Conversation

6 participants
@sescaida
Copy link

sescaida commented Feb 6, 2018

The PSDE works similar to a function block in Simulink or comparable software. The user can define a block (a PSDE) declaring its Inputs, Outputs (I/Os) and its behavior using Python alone. It has some similarities with the "PythonScriptController" (PSC). The two main differences are:

  • PSDE-objects are not triggered by events (not conceptually at least). Rather, the functionality should be implemented in the update() function of the associated script, which is called once per simulation frame.
  • I/Os can be added directly to the object inside the parse() function of the PSDE-object. Linking using the '@'-syntax also works. Therefore, the PSDE-object behaves like any other object that has I/Os (or Data in general). Inside the script these newly defined I/Os can be accessed as members of the object, providing an easy syntax.

You can think of the PSDE as a possibility of subclassing the Sofa 'DataEngine' in a scripted way. In the second example provided it is shown how a versatile ROI functionality can be implemented with the help of the PSDE.

Two examples with some documentation are provided in:
applications/plugins/SofaPython/examples/PSDEExamples/

Example of use (01-basic/PSDEBasic.pyscn):

#!/usr/bin/env python
# -*- coding: utf-8 -*-
import Sofa
import os

path = os.path.dirname(os.path.abspath(__file__))+'/../../../../../../share/mesh/'
        
def createScene(rootNode):
        rootNode.createObject("MeshVTKLoader", name="loader", filename=path+'liver.vtk')
        rootNode.createObject('PythonScriptDataEngine', name='PSDE', filename="DataEngine.py", classname="MyDataEngine")	       

Example for DataEngine class in Python (01-basic/DataEngine.py):

class MyDataEngine(Sofa.PythonScriptDataEngine):           
  
    def parse(self):        
        # this is the correct place to define inputs and outputs! (linking will not work in init() for instance)
        self.addNewInput('Positions',datatype='p',value='@loader.position')       
        self.addNewOutput('NumPoints',datatype='d', value='0')                

    def init(self):
	pass

    def update(self):        
        self.NumPoints = len(self.Positions)

What it does NOT provide: A new form of parsing from Python data to Sofa data. It uses the available mechanism that uses string conversion and parsing. Also, the type of the Data has to be declared/selected with a parameter in Python, because there is no logic to automatically derive the type desired by the user (could be future work).


This PR:

  • builds with SUCCESS for all platforms on the CI.
  • does not generate new warnings.
  • does not generate new unit test failures.
  • does not generate new scene test failures.
  • does not break API compatibility.
  • is more than 1 week old (or has fast-merge label).

Reviewers will merge only if all these checks are true.

Stefan Escaida added some commits Jan 4, 2018

@damienmarchal

This comment has been minimized.

Copy link
Contributor

damienmarchal commented Feb 21, 2018

Args, shouldn't this one should be in to-review status ?

@damienmarchal

This comment has been minimized.

Copy link
Contributor

damienmarchal commented Feb 21, 2018

[ci-build][with-scene-tests]

Stefan Escaida added some commits Feb 21, 2018

@sescaida

This comment has been minimized.

Copy link

sescaida commented Feb 22, 2018

Hi guys, I made the changes concerning your feedback and now the PR is ready for review from my side!

The main changes are:

  • Created a basic example of usage
  • Now uses a factory for data creation
  • The use of both args and kwargs for I/O creation is possible
  • No unnecessary changes in files / no geometries added, etc. (cleaning)
@damienmarchal

This comment has been minimized.

Copy link
Contributor

damienmarchal commented Feb 27, 2018

[ci-build][with-scene-tests]

@damienmarchal

This comment has been minimized.

Copy link
Contributor

damienmarchal commented Mar 1, 2018

@sescaida the CI is reporting there is new warning added by this PR (not a lot). Can you fix them before we merge ?

@guparan guparan added status: wip and removed status: ready labels Mar 5, 2018

@bcarrez

This comment has been minimized.

Copy link
Contributor

bcarrez commented Mar 20, 2018

[ci-build][with-scene-tests]

@bcarrez

This comment has been minimized.

Copy link
Contributor

bcarrez commented Mar 23, 2018

[ci-build][with-scene-tests]

@guparan guparan merged commit 1a51a4e into sofa-framework:master Mar 26, 2018

6 checks passed

Dashboard Builds triggered.
Details
Scene tests Triggered in latest build.
Details
centos_clang-3.4_options OK (tests ignored, see details)
Details
mac_clang-3.4_options OK (tests ignored, see details)
Details
ubuntu_gcc-5.4_options OK (tests ignored, see details)
Details
windows7_VS-2015_options_amd64 OK (tests ignored, see details)
Details

@guparan guparan added this to the v18.06 milestone Apr 5, 2018

@damienmarchal damienmarchal deleted the SofaDefrost:PSDataEngine branch Aug 6, 2018

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment