pylops icon indicating copy to clipboard operation
pylops copied to clipboard

Feature: Add ABC abstractmethod to LinearOperator

Open mrava87 opened this issue 3 years ago • 1 comments

Motivation

Following the discussion in https://github.com/PyLops/pylops/pull/363 we should consider also doing the same for linear operator

Solution

Decorate _matvect and _rmatvect with @abstractmethod

mrava87 avatar Apr 16 '22 10:04 mrava87

Currently, adding @abstractmethod to these methods results in the following error:

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
Input In [7], in <cell line: 1>()
----> 1 Dop.H

File /opt/anaconda3/envs/pylops/lib/python3.9/site-packages/scipy/sparse/linalg/_interface.py:484, in LinearOperator.adjoint(self)
    470 def adjoint(self):
    471     """Hermitian adjoint.
    472 
    473     Returns the Hermitian adjoint of self, aka the Hermitian
   (...)
    482         Hermitian adjoint of self.
    483     """
--> 484     return self._adjoint()

File ~/Desktop/KAUST/OpenSource/pylops/pylops/LinearOperator.py:345, in LinearOperator._adjoint(self)
    344 def _adjoint(self):
--> 345     Op = aslinearoperator(super()._adjoint())
    346     self._copy_attributes(Op, exclude=["dims", "dimsd", "name"])
    347     Op.dims = self.dimsd

File ~/Desktop/KAUST/OpenSource/pylops/pylops/LinearOperator.py:1286, in aslinearoperator(Op)
   1284     return Op
   1285 else:
-> 1286     return LinearOperator(Op)

File /opt/anaconda3/envs/pylops/lib/python3.9/site-packages/scipy/sparse/linalg/_interface.py:147, in LinearOperator.__new__(cls, *args, **kwargs)
    145     return super(LinearOperator, cls).__new__(_CustomLinearOperator)
    146 else:
--> 147     obj = super(LinearOperator, cls).__new__(cls)
    149     if (type(obj)._matvec == LinearOperator._matvec
    150             and type(obj)._matmat == LinearOperator._matmat):
    151         warnings.warn("LinearOperator subclass should implement"
    152                       " at least one of _matvec and _matmat.",
    153                       category=RuntimeWarning, stacklevel=2)

TypeError: Can't instantiate abstract class LinearOperator with abstract methods _matvec, _rmatvec

mrava87 avatar Jun 21 '22 18:06 mrava87

If this issue is still not solved, would like to work on it @mrava87

rohanbabbar04 avatar Jan 30 '23 05:01 rohanbabbar04

Sure, we haven’t looked into this yet. I’ll assign to you :)

mrava87 avatar Jan 30 '23 05:01 mrava87