Skip to content

[FIX] pyopenms copy constructors (wrapping) - #5460

Merged
poshul merged 16 commits into
OpenMS:release/2.7.0from
oliveralka:fix/copyctro_wrapignore
Jul 31, 2021
Merged

poshul merged 16 commits into
OpenMS:release/2.7.0from
oliveralka:fix/copyctro_wrapignore

Conversation

@oliveralka

@oliveralka oliveralka commented Jul 26, 2021 •

Copy link
Copy Markdown
Contributor

Description

Inconsistency of copy constructor wrapping in pyopenms (.pxd) wrappers.

Only when using the correct wrapper format, a copy and deepcopy function will be present in pyopenms.

AASequence(AASequence &) nogil except +

cdef class AASequence:
    """
    Cython implementation of _AASequence

    Documentation is available at http://www.openms.de/current_doxygen/html/classOpenMS_1_1AASequence.html

    Representation of a peptide/protein sequence
    This class represents amino acid sequences in OpenMS. An AASequence
    instance primarily contains a sequence of residues.
    """

    # see .pxd file for cdef of inst ptr

    def __dealloc__(self):
         self.inst.reset()

    def __hash__(self):
      # The only required property is that objects which compare equal have
      # the same hash value:
      return hash(deref(self.inst.get()).toString().c_str() )

    
    def __copy__(self):
       cdef AASequence rv = AASequence.__new__(AASequence)
       rv.inst = shared_ptr[_AASequence](new _AASequence(deref(self.inst.get())))
       return rv
    
    def __deepcopy__(self, memo):
       cdef AASequence rv = AASequence.__new__(AASequence)
       rv.inst = shared_ptr[_AASequence](new _AASequence(deref(self.inst.get())))
       return rv
    
    def _init_0(self):
        """Cython signature: void AASequence()"""
        self.inst = shared_ptr[_AASequence](new _AASequence())
    
    def _init_1(self, AASequence in_0 ):
        """Cython signature: void AASequence(AASequence &)"""
        assert isinstance(in_0, AASequence), 'arg in_0 wrong type'
    
        self.inst = shared_ptr[_AASequence](new _AASequence((deref(in_0.inst.get()))))
    
    def __init__(self, *args , **kwargs):
        """
          - Cython signature: void AASequence()
          - Cython signature: void AASequence(AASequence &)
"""
        if not args:
             self._init_0(*args)
        elif (len(args)==1) and (isinstance(args[0], AASequence)):
             self._init_1(*args)
        else:
               raise Exception('can not handle type of %s' % (args,))

Also, see the example files provided in the autowrap repository:
https://github.com/uweschmitt/autowrap/blob/master/tests/test_files/minimal.hpp#L25
https://github.com/uweschmitt/autowrap/blob/master/tests/test_files/minimal.pxd#L22

In the other cases, the methods are not introduced. I was not aware of any use-case for that matter.

AASequence(AASequence) nogil except +

cdef class AASequence:
    """
    Cython implementation of _AASequence

    Documentation is available at http://www.openms.de/current_doxygen/html/classOpenMS_1_1AASequence.html

    Representation of a peptide/protein sequence
    This class represents amino acid sequences in OpenMS. An AASequence
    instance primarily contains a sequence of residues.
    """

    # see .pxd file for cdef of inst ptr

    def __dealloc__(self):
         self.inst.reset()

    def __hash__(self):
      # The only required property is that objects which compare equal have
      # the same hash value:
      return hash(deref(self.inst.get()).toString().c_str() )
    
    def _init_0(self):
        """Cython signature: void AASequence()"""
        self.inst = shared_ptr[_AASequence](new _AASequence())
    
    def _init_1(self, AASequence in_0 ):
        """Cython signature: void AASequence(AASequence)"""
        assert isinstance(in_0, AASequence), 'arg in_0 wrong type'
    
        self.inst = shared_ptr[_AASequence](new _AASequence((deref(in_0.inst.get()))))
    
    def __init__(self, *args , **kwargs):
        """
          - Cython signature: void AASequence()
          - Cython signature: void AASequence(AASequence)
"""
        if not args:
             self._init_0(*args)
        elif (len(args)==1) and (isinstance(args[0], AASequence)):
             self._init_1(*args)
        else:
               raise Exception('can not handle type of %s' % (args,))

AASequence(AASequence &) nogil except + # wrap-ignore

cdef class AASequence:
    """
    Cython implementation of _AASequence

    Documentation is available at http://www.openms.de/current_doxygen/html/classOpenMS_1_1AASequence.html

    Representation of a peptide/protein sequence
    This class represents amino acid sequences in OpenMS. An AASequence
    instance primarily contains a sequence of residues.
    """

    # see .pxd file for cdef of inst ptr

    def __dealloc__(self):
         self.inst.reset()


    def __hash__(self):
      # The only required property is that objects which compare equal have
      # the same hash value:
      return hash(deref(self.inst.get()).toString().c_str() )

    
    def __init__(self):
        """Cython signature: void AASequence()"""
        self.inst = shared_ptr[_AASequence](new _AASequence())

As shortly discussed with Hannes, I changed the wrappers to the respective format.

# compiler: This means that there was no user-specified constructor/copy constructor.
# private/deleted: Not implemented
# protected: Not available in pyopenms

I am not sure about additional testing - are all the classes tested in the unit tests?

Checklist:

  • Make sure that you are listed in the AUTHORS file
  • Add relevant changes and new features to the CHANGELOG file
  • I have commented my code, particularly in hard-to-understand areas
  • New and existing unit tests pass locally with my changes
  • Updated or added python bindings for changed or new classes. (Tick if no updates were necessary.)

How can I get additional information on failed tests during CI:

If your PR is failing you can check out

Note:

  • Once you opened a PR try to minimize the number of pushes to it as every push will trigger CI (automated builds and test) and is rather heavy on our infrastructure (e.g., if several pushes per day are performed).

@oliveralka

oliveralka commented Jul 27, 2021 •

Copy link
Copy Markdown
Contributor Author

The main question here is if additional tests are necessary, how such a test would look like and how the test generation could be automated.

Probably similar to the copy, deep copy test for MSExperiment (https://github.com/OpenMS/OpenMS/blob/develop/src/pyOpenMS/tests/unittests/test000.py)

Is it feasible to test that for every corresponding pxd?

    mse = pyopenms.MSExperiment()
    mse_ = copy.copy(mse)
    assert mse_ == mse
    mse_ = copy.deepcopy(mse)
    assert mse_ == mse
    mse_ = pyopenms.MSExperiment(mse)
    assert mse_ == mse

@timosachsenberg

Copy link
Copy Markdown
Contributor

So this means that "AASequence(AASequence) nogil except +" kind of works as it uses the copy constructor but it does not generate the deep copy code? What is the rational behind that?

@oliveralka

oliveralka commented Jul 27, 2021 •

Copy link
Copy Markdown
Contributor Author

So this means that "AASequence(AASequence) nogil except +" kind of works as it uses the copy constructor but it does not generate the deep copy code? What is the rationale behind that?

I actually have no idea about the rationale and the decision when this kind of wrapping should be used instead of the one with the reference.

@hroest Can you comment on that?

@oliveralka
oliveralka requested a review from hroest July 27, 2021 07:16
@oliveralka
oliveralka changed the base branch from develop to release/2.7.0 July 30, 2021 13:22
@oliveralka

Copy link
Copy Markdown
Contributor Author

rebuild jenkins

@poshul
poshul marked this pull request as ready for review July 31, 2021 13:19
@poshul
poshul merged commit 944a680 into OpenMS:release/2.7.0 Jul 31, 2021
@oliveralka
oliveralka deleted the fix/copyctro_wrapignore branch August 1, 2021 17:31
@oliveralka oliveralka mentioned this pull request Aug 2, 2021
5 tasks
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

Successfully merging this pull request may close these issues.

3 participants