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

Patch utils: tentative v0.3. #11

Merged
merged 15 commits into from
Feb 27, 2023
Prev Previous commit
Next Next commit
add typing, improve FDCT docs
  • Loading branch information
cako committed Jan 22, 2023
commit 8bb61075569a66443e972f58bbce889d261d9287
7 changes: 7 additions & 0 deletions curvelops/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
"""
``curvelops``
=============

Python wrapper for CurveLab's 2D and 3D curvelet transforms.
"""
from .curvelops import *
from .typing import *


try:
Expand Down
67 changes: 48 additions & 19 deletions curvelops/curvelops.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
"""
``curvelops.curvelops``
=======================

Provides a LinearOperator for the 2D and 3D curvelet transforms.
"""

from itertools import product
from typing import List, Optional, Tuple, Callable, Union, Sequence
from typing import Optional, Tuple, Callable, Union
import numpy as np
from numpy.typing import DTypeLike, NDArray
from numpy.core.multiarray import normalize_axis_index # type: ignore
Expand All @@ -12,8 +15,7 @@
from .fdct2d_wrapper import * # noqa: F403
from .fdct3d_wrapper import * # noqa: F403

InputDimsLike = Union[Sequence[int], NDArray[np.int_]]
FDCTStructLike = List[List[NDArray]]
from .typing import InputDimsLike, FDCTStructLike


def _fdct_docs(dimension: int) -> str:
Expand All @@ -30,30 +32,20 @@ def _fdct_docs(dimension: int) -> str:
Parameters
----------
dims : :obj:`tuple`
Number of samples for each dimension
Number of samples for each dimension.
axes : :obj:`tuple`, optional
Axes along which FDCT is applied
Axes along which FDCT is applied.
nbscales : :obj:`int`, optional
Number of scales (including the coarsest level);
Defaults to ceil(log2(min(input_dims)) - 3).
nbangles_coarse : :obj:`int`, optional
Number of angles at 2nd coarsest scale
Number of angles at 2nd coarsest scale.
allcurvelets : :obj:`bool`, optional
Use curvelets at all scales, including coarsest scale.
If ``False``, a wavelet transform will be used for the
coarsest scale.
dtype : :obj:`str`, optional
Type of the transform

PyLops Attributes
----------
shape : :obj:`tuple`
Operator shape
dtype : :obj:`numpy.dtype`
Type of operator. Only supports complex types at the moment
explicit : :obj:`bool`
Operator contains a matrix that can be solved explicitly.
Always False
dtype : :obj:`DTypeLike <numpy.typing.DTypeLike>`, optional
``dtype`` of the transform.
"""


Expand Down Expand Up @@ -187,7 +179,26 @@ def inverse(self, x: NDArray) -> NDArray:
return self._rmatvec(x)

def struct(self, x: NDArray) -> FDCTStructLike:
c_struct = []
"""Convert curvelet flattened vector to curvelet structure.

The FDCT always returns a 1D vector that has all curvelet
coefficients. These coefficients can be organized into
scales, wedges and spatial positions. Applying this
function to a 1D vector generates this structure.

Parameters
----------
x : :obj:`NDArray <numpy.typing.NDArray>`
Input flattened vector.

Returns
-------
:obj:`FDCTStructLike <curvelops.typing.FDCTStructLike>`
Curvelet structure, a list of lists of multidimensional arrays.
The first index corresponds to scale, the second corresponds to
angular wedge.
"""
c_struct: FDCTStructLike = []
k = 0
for i in range(len(self.shapes)):
angles = []
Expand All @@ -199,6 +210,24 @@ def struct(self, x: NDArray) -> FDCTStructLike:
return c_struct

def vect(self, x: FDCTStructLike) -> NDArray:
"""Convert curvelet structure to curvelet flattened vector.

The FDCT always returns a 1D vector that has all curvelet
coefficients. These coefficients can be organized into
scales, wedges and spatial positions. Applying this
function to a curvelet structure returns the flattened
vector.

Parameters
----------
x : :obj:`FDCTStructLike <curvelops.typing.FDCTStructLike>`
Input curvelet structure.

Returns
-------
:obj:`NDArray <numpy.typing.NDArray>`
Flattened vector.
"""
return np.concatenate([coef.ravel() for angle in x for coef in angle])


Expand Down
13 changes: 13 additions & 0 deletions curvelops/typing/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
"""
``curvelops.typing``
====================

Typing submodule.
"""

from . import _typing

__all__ = _typing.__all__.copy()


from ._typing import *
9 changes: 9 additions & 0 deletions curvelops/typing/_typing.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
__all__ = ["InputDimsLike", "FDCTStructLike", "RecursiveListNDArray"]
from typing import List, Sequence, Union

import numpy as np
from numpy.typing import NDArray

InputDimsLike = Union[Sequence[int], NDArray[np.int_]]
FDCTStructLike = List[List[NDArray]]
RecursiveListNDArray = Union[List[NDArray], List["RecursiveListNDArray"]]
42 changes: 42 additions & 0 deletions docs/source/curvelops.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
curvelops package
=================

.. automodule:: curvelops
:members:
:undoc-members:
:show-inheritance:

Subpackages
-----------

.. toctree::
:maxdepth: 4

curvelops.typing

Submodules
----------

curvelops.curvelops module
--------------------------

.. automodule:: curvelops.curvelops
:members:
:undoc-members:
:show-inheritance:

curvelops.fdct2d\_wrapper module
--------------------------------

.. automodule:: curvelops.fdct2d_wrapper
:members:
:undoc-members:
:show-inheritance:

curvelops.fdct3d\_wrapper module
--------------------------------

.. automodule:: curvelops.fdct3d_wrapper
:members:
:undoc-members:
:show-inheritance:
7 changes: 7 additions & 0 deletions docs/source/curvelops.typing.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
curvelops.typing package
========================

.. automodule:: curvelops.typing
:members:
:undoc-members:
:show-inheritance:
4 changes: 3 additions & 1 deletion docs/source/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,13 @@
Welcome to curvelops's documentation!
=====================================

Python wrapper for `CurveLab <http://www.curvelet.org>`_'s 2D and 3D curvelet transforms. It uses the `PyLops <https://pylops.readthedocs.io/>`_ design framework to provide the forward and inverse curvelet transforms as matrix-free linear operations. Check out `some examples <https://github.com/PyLops/curvelops/tree/main/examples>`_ or view the documentation below.

.. toctree::
:maxdepth: 2
:caption: Contents:


modules.rst

Indices and tables
==================
Expand Down
7 changes: 7 additions & 0 deletions docs/source/modules.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
curvelops
=========

.. toctree::
:maxdepth: 4

curvelops