SofaPython3 icon indicating copy to clipboard operation
SofaPython3 copied to clipboard

Documentation about how sofa::Data is bound

Open jjcasmar opened this issue 4 years ago • 1 comments

I have a lot of doubts about how sofa::Data is bound to SofaPython3. Apparently, when sofa::Data contains a VecCoord, is it converted to a numpy array, which i can access with I can access and modify with [], but I can't iterate. For other types, its not clear how is bound (for example with a filepath).

Is there any documentation on how sofa::Data<> is bound to sofa, and how it affects regarding read/write access in C++ side?

jjcasmar avatar Mar 13 '21 12:03 jjcasmar

Hello @jjcasmar,

This is a very good question and making that part clear is a yet to do work.

Data are binded to a SofaData python type or one of its in-herited class. In the case of a VecCoord this should be a SofaDataContainer. This SofaDataContainer has a property called 'value', this property can/should be used to manipulate the Data content as a numpy.array. As this is a numpy array you can perfectly iterate over and do any numpy operation you want.

To preserve the consistancy of the Sofa Data update mechanisme (begin/end), the value obtained with array is read-only. In order to get writable data it is mandatory to get an access to it.

Here is how access should be done:

def dumpIterableData(data):
    # Type 
    print(type(data.value))

    # Access
    print(data.value[0])

    # Iterations 
    for i in data.value:
        print("   ", i)
    
    try:
        data.value[2] = [7,8,0]
    except ValueError as e:
        print("You cannot because:", e)

    try:            
        data.value[2][2] = 9
    except ValueError as e:
        print("You cannot because:", e)

def createScene(root):
    root.addObject("MechanicalObject", name="dofs", position=[[1,2,3], [4,5,6], [0,0,0]])
    dumpIterableData(root.dofs.position)

That's said there is also in SofaDataContainer extra methods to allow short syntax (without the .value)

    data[0] = [1,2,3]
    data[3:4] = [3,4,5]

but the conclusion was that generalizing that would have the following consequence of:

  • every function need to be implemented (... ex: as you notice it is missing iterations... and in the end it will be duplicating in our code base the same feature as there is in the array numpy API)
  • it makes seems fuzzy between the fact that the data is not a numpy array but a SofaData object.

From what I know, the decision to remove (or generalize) the short syntax was never taken. So we are in an unpleasant in-between state.

damienmarchal avatar Sep 23 '21 13:09 damienmarchal