Skip to content
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] sparse matrix aliasing scipy/eigen #411

Merged
merged 9 commits into from
Oct 18, 2017

Conversation

maxime-tournier
Copy link
Contributor

@maxime-tournier maxime-tournier commented Sep 19, 2017

This PR provides support for aliasing Eigen sparse matrices (classes derived from EigenBaseSparseMatrix) through scipy sparse matrices.

The scipy matrix aliases the eigen matrix, so that any modification will reflect on both sides provided the sparsity pattern does not change (on either side). A python context manager is provided to commit modifications back, should the sparsity pattern change, as shown in the example below.

The binding is somewhat unconventional as it uses ctypes for simplicity. Perhaps a cleaner version could be made using regular bindings + passing Eigen matrix pointers through python capsules if someone is motivated.

Example

from SofaPython import sparse
import numpy as np

import Sofa
Sofa.loadPlugin('Flexible')

def createScene(node):
    template = 'Affine'

    dofs = node.createObject('MechanicalObject', template = template, size = 1)
    dofs.init()
    
    mass = node.createObject('AffineMass', template = template)
    mass.init()
    mass.bwdInit()

    ref = np.identity(12)
    
    with sparse.data_view(mass, 'massMatrix') as m:
        assert (m == ref).all()

        m[10, 10] = 14
        ref[10, 10] = 14

        # assert our in-place modifications are reflected
        with sparse.data_view(mass, 'massMatrix') as mm:
            assert (mm == ref).all()

        m[0, 10] = 14
        ref[0, 10] = 14

        # sparsity change: scipy matrix reallocates, no longer aliases 
        with sparse.data_view(mass, 'massMatrix') as mm:
            assert not (mm == ref).all()
        
        # modification commit happens here

    with sparse.data_view(mass, 'massMatrix') as m:
        assert (m == ref).all()

Changelog

  • Added DataTypeInfo for EigenBaseSparseMatrix derived classes
  • Added a bunch of C functions in SofaPython/ctypes.cpp for aliasing logic
  • Added a ctypes binding for aliasing + contexts in SofaPython.sparse
  • Added an example in SofaPython/examples/sparse.py

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.

@damienmarchal
Copy link
Contributor

Hi Maxime,

Thanks for the PR. I will have a look at it.

@guparan guparan added enhancement About a possible enhancement pr: status to review To notify reviewers to review this pull-request labels Sep 27, 2017
@hugtalbot
Copy link
Contributor

Thank you @maxime-tournier and sorry for the delay
@damienmarchal @matthieu-nesme do you have any feedback regarding the PR, otherwise it should be merged. Thx guys

Copy link
Contributor

@damienmarchal damienmarchal left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't see this PR breaking other's code and the use of the the contextmanager to write the python is elegant. There is an example file. The description is nice as the changelog.

Currently there is no tests but making one shouldn't be hard :)

@@ -139,6 +142,8 @@ struct DataTypeInfo
{
}

// mtournier: wtf is this supposed to do?
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This kind of comment are useless.
Drop them or maybe use a more formal approach so that we really get an answer of the use of this function or the deprecation the weird function.

Copy link
Contributor Author

@maxime-tournier maxime-tournier Oct 11, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, those are my own personal comments and were not meant to make it in the final PR.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You often show your surprise with wtf comments which I found very funny because I have the same but I don't write them :)

@@ -251,6 +251,8 @@ if( SofaPython_FOUND )
mapping/PythonMultiMapping.cpp
python/_Compliant.cpp
python/Binding_AssembledSystem.cpp

# note: need to fix assembled solver in python before removing
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there something that still to be done ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The code in this PR is loosely based on Compliant/python/python.cpp, so I have to update the code there to use this PR's code to avoid duplication. Unfortunately little time to do so.

@damienmarchal
Copy link
Contributor

@hugtalbot I think this one should be merged.

@guparan guparan added pr: status ready Approved a pull-request, ready to be squashed and removed pr: status to review To notify reviewers to review this pull-request labels Oct 11, 2017
@damienmarchal
Copy link
Contributor

[ci-build]

@maxime-tournier
Copy link
Contributor Author

Also: the test is the associated example.

@damienmarchal
Copy link
Contributor

Is the examples automatically started during CI tests ?
If not please consider adding a dedicated test (even one loading the examples file) because we don't want to be forced to run manually all the examples scenes to detect possible regressions.

@hugtalbot
Copy link
Contributor

Rebuild and run the scene tests :
[ci-build] [with-scene-tests]

@damienmarchal
Copy link
Contributor

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

@hugtalbot hugtalbot added pr: status to review To notify reviewers to review this pull-request pr: status ready Approved a pull-request, ready to be squashed and removed pr: status ready Approved a pull-request, ready to be squashed pr: status to review To notify reviewers to review this pull-request labels Oct 18, 2017
@hugtalbot hugtalbot merged commit 2c618af into sofa-framework:master Oct 18, 2017
@guparan guparan added this to the v17.12 milestone Dec 14, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement About a possible enhancement pr: status ready Approved a pull-request, ready to be squashed
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants