curvelets 0.0.2a0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- curvelets/__init__.py +13 -0
- curvelets/_version.py +16 -0
- curvelets/_version.pyi +4 -0
- curvelets/numpy/__init__.py +7 -0
- curvelets/numpy/typing.py +21 -0
- curvelets/numpy/udct.py +167 -0
- curvelets/numpy/udctmdwin.py +315 -0
- curvelets/numpy/utils.py +170 -0
- curvelets/plot/__init__.py +4 -0
- curvelets/plot/_matplotlib.py +70 -0
- curvelets/py.typed +0 -0
- curvelets/reference/__init__.py +5 -0
- curvelets/reference/_adapt_grid.py +39 -0
- curvelets/reference/_angle_fun.py +30 -0
- curvelets/reference/_angle_kron.py +23 -0
- curvelets/reference/_circshift.py +8 -0
- curvelets/reference/_downsamp.py +54 -0
- curvelets/reference/_fftflip.py +16 -0
- curvelets/reference/_fun_meyer.py +58 -0
- curvelets/reference/_travel.py +5 -0
- curvelets/reference/_upsamp.py +11 -0
- curvelets/reference/udct.py +94 -0
- curvelets/reference/udctmdwin.py +283 -0
- curvelets-0.0.2a0.dist-info/METADATA +205 -0
- curvelets-0.0.2a0.dist-info/RECORD +27 -0
- curvelets-0.0.2a0.dist-info/WHEEL +4 -0
- curvelets-0.0.2a0.dist-info/licenses/LICENSE +19 -0
curvelets/__init__.py
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
"""
|
|
2
|
+
Copyright (c) 2024 Carlos Alberto da Costa Filho. All rights reserved.
|
|
3
|
+
|
|
4
|
+
Curvelets: Python package for curvelet transforms.
|
|
5
|
+
"""
|
|
6
|
+
|
|
7
|
+
from __future__ import annotations
|
|
8
|
+
|
|
9
|
+
from ._version import version as __version__
|
|
10
|
+
|
|
11
|
+
__all__ = ["__version__", "numpy", "reference"]
|
|
12
|
+
|
|
13
|
+
from . import numpy, reference
|
curvelets/_version.py
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
# file generated by setuptools_scm
|
|
2
|
+
# don't change, don't track in version control
|
|
3
|
+
TYPE_CHECKING = False
|
|
4
|
+
if TYPE_CHECKING:
|
|
5
|
+
from typing import Tuple, Union
|
|
6
|
+
VERSION_TUPLE = Tuple[Union[int, str], ...]
|
|
7
|
+
else:
|
|
8
|
+
VERSION_TUPLE = object
|
|
9
|
+
|
|
10
|
+
version: str
|
|
11
|
+
__version__: str
|
|
12
|
+
__version_tuple__: VERSION_TUPLE
|
|
13
|
+
version_tuple: VERSION_TUPLE
|
|
14
|
+
|
|
15
|
+
__version__ = version = '0.0.2a0'
|
|
16
|
+
__version_tuple__ = version_tuple = (0, 0, 2)
|
curvelets/_version.pyi
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
__all__ = ["ParamUDCT", "udctmddec", "udctmdrec", "udctmdwin", "UDCT", "SimpleUDCT"]
|
|
4
|
+
|
|
5
|
+
from curvelets.numpy.udct import UDCT, SimpleUDCT, udctmddec, udctmdrec
|
|
6
|
+
from curvelets.numpy.udctmdwin import udctmdwin
|
|
7
|
+
from curvelets.numpy.utils import ParamUDCT
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
import sys
|
|
4
|
+
|
|
5
|
+
if sys.version_info >= (3, 10):
|
|
6
|
+
from typing import TypeAlias
|
|
7
|
+
else:
|
|
8
|
+
from typing_extensions import TypeAlias
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
import numpy as np
|
|
12
|
+
import numpy.typing as npt
|
|
13
|
+
|
|
14
|
+
if sys.version_info <= (3, 9):
|
|
15
|
+
from typing import List # noqa: UP035
|
|
16
|
+
|
|
17
|
+
UDCTCoefficients: TypeAlias = List[List[List[npt.NDArray[np.complexfloating]]]] # noqa: UP006
|
|
18
|
+
UDCTWindows: TypeAlias = List[List[List[List[npt.NDArray[np.floating]]]]] # noqa: UP006
|
|
19
|
+
else:
|
|
20
|
+
UDCTCoefficients: TypeAlias = list[list[list[npt.NDArray[np.complexfloating]]]]
|
|
21
|
+
UDCTWindows: TypeAlias = list[list[list[list[npt.NDArray[np.floating]]]]]
|
curvelets/numpy/udct.py
ADDED
|
@@ -0,0 +1,167 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
import logging
|
|
4
|
+
from math import prod
|
|
5
|
+
|
|
6
|
+
import numpy as np
|
|
7
|
+
import numpy.typing as npt
|
|
8
|
+
|
|
9
|
+
from .typing import UDCTCoefficients, UDCTWindows
|
|
10
|
+
from .udctmdwin import udctmdwin
|
|
11
|
+
from .utils import ParamUDCT, downsamp, from_sparse_new, upsamp
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
def udctmddec(
|
|
15
|
+
im: np.ndarray,
|
|
16
|
+
param_udct: ParamUDCT,
|
|
17
|
+
udctwin: UDCTWindows,
|
|
18
|
+
decimation_ratio: list[npt.NDArray[np.int_]],
|
|
19
|
+
) -> UDCTCoefficients:
|
|
20
|
+
imf = np.fft.fftn(im)
|
|
21
|
+
cdtype = imf.dtype
|
|
22
|
+
|
|
23
|
+
fband = np.zeros_like(imf)
|
|
24
|
+
idx, val = from_sparse_new(udctwin[0][0][0])
|
|
25
|
+
fband.flat[idx] = imf.flat[idx] * val.astype(cdtype)
|
|
26
|
+
cband = np.fft.ifftn(fband)
|
|
27
|
+
|
|
28
|
+
coeff: UDCTCoefficients = [[[downsamp(cband, decimation_ratio[0][0])]]]
|
|
29
|
+
norm = np.sqrt(
|
|
30
|
+
np.prod(np.full((param_udct.dim,), fill_value=2 ** (param_udct.res - 1)))
|
|
31
|
+
)
|
|
32
|
+
coeff[0][0][0] *= norm
|
|
33
|
+
|
|
34
|
+
for ires in range(1, 1 + param_udct.res):
|
|
35
|
+
coeff.append([])
|
|
36
|
+
for idir in range(param_udct.dim):
|
|
37
|
+
coeff[ires].append([])
|
|
38
|
+
for iang in range(len(udctwin[ires][idir])):
|
|
39
|
+
fband = np.zeros_like(imf)
|
|
40
|
+
idx, val = from_sparse_new(udctwin[ires][idir][iang])
|
|
41
|
+
fband.flat[idx] = imf.flat[idx] * val.astype(cdtype)
|
|
42
|
+
|
|
43
|
+
cband = np.fft.ifftn(fband)
|
|
44
|
+
decim = decimation_ratio[ires][idir, :]
|
|
45
|
+
coeff[ires][idir].append(downsamp(cband, decim))
|
|
46
|
+
coeff[ires][idir][iang] *= np.sqrt(2 * np.prod(decim))
|
|
47
|
+
return coeff
|
|
48
|
+
|
|
49
|
+
|
|
50
|
+
def udctmdrec(
|
|
51
|
+
coeff: UDCTCoefficients,
|
|
52
|
+
param_udct: ParamUDCT,
|
|
53
|
+
udctwin: UDCTWindows,
|
|
54
|
+
decimation_ratio: list[npt.NDArray[np.int_]],
|
|
55
|
+
) -> np.ndarray:
|
|
56
|
+
rdtype = coeff[0][0][0].real.dtype
|
|
57
|
+
cdtype = (np.ones(1, dtype=rdtype) + 1j * np.ones(1, dtype=rdtype)).dtype
|
|
58
|
+
imf = np.zeros(param_udct.size, dtype=cdtype)
|
|
59
|
+
|
|
60
|
+
for ires in range(1, 1 + param_udct.res):
|
|
61
|
+
for idir in range(param_udct.dim):
|
|
62
|
+
for iang in range(len(udctwin[ires][idir])):
|
|
63
|
+
decim = decimation_ratio[ires][idir, :]
|
|
64
|
+
cband = upsamp(coeff[ires][idir][iang], decim)
|
|
65
|
+
cband /= np.sqrt(2 * np.prod(decim))
|
|
66
|
+
cband = np.prod(decim) * np.fft.fftn(cband)
|
|
67
|
+
idx, val = from_sparse_new(udctwin[ires][idir][iang])
|
|
68
|
+
imf.flat[idx] += cband.flat[idx] * val.astype(cdtype)
|
|
69
|
+
|
|
70
|
+
imfl = np.zeros(param_udct.size, dtype=cdtype)
|
|
71
|
+
decim = decimation_ratio[0][0]
|
|
72
|
+
cband = upsamp(coeff[0][0][0], decim)
|
|
73
|
+
cband = np.sqrt(np.prod(decim)) * np.fft.fftn(cband)
|
|
74
|
+
idx, val = from_sparse_new(udctwin[0][0][0])
|
|
75
|
+
imfl.flat[idx] += cband.flat[idx] * val.astype(cdtype)
|
|
76
|
+
imf = 2 * imf + imfl
|
|
77
|
+
return np.fft.ifftn(imf).real
|
|
78
|
+
|
|
79
|
+
|
|
80
|
+
class UDCT:
|
|
81
|
+
def __init__(
|
|
82
|
+
self,
|
|
83
|
+
shape: tuple[int, ...],
|
|
84
|
+
cfg: np.ndarray | None = None,
|
|
85
|
+
alpha: float = 0.15,
|
|
86
|
+
r: tuple[float, float, float, float] | None = None,
|
|
87
|
+
winthresh: float = 1e-5,
|
|
88
|
+
) -> None:
|
|
89
|
+
self.shape = shape
|
|
90
|
+
dim = len(shape)
|
|
91
|
+
cfg1 = np.c_[np.ones((dim,)) * 3, np.ones((dim,)) * 6].T if cfg is None else cfg
|
|
92
|
+
r1: tuple[float, float, float, float] = (
|
|
93
|
+
tuple(np.array([1.0, 2.0, 2.0, 4.0]) * np.pi / 3) if r is None else r
|
|
94
|
+
)
|
|
95
|
+
self.params = ParamUDCT(
|
|
96
|
+
dim=dim, size=shape, cfg=cfg1, alpha=alpha, r=r1, winthresh=winthresh
|
|
97
|
+
)
|
|
98
|
+
|
|
99
|
+
self.windows, self.decimation, self.indices = udctmdwin(self.params)
|
|
100
|
+
|
|
101
|
+
def vect(self, coeffs: UDCTCoefficients) -> npt.NDArray[np.complexfloating]:
|
|
102
|
+
coeffs_vec = []
|
|
103
|
+
for c in coeffs:
|
|
104
|
+
for d in c:
|
|
105
|
+
for a in d:
|
|
106
|
+
coeffs_vec.append(a.ravel())
|
|
107
|
+
return np.concatenate(coeffs_vec)
|
|
108
|
+
|
|
109
|
+
def struct(self, coeffs_vec: npt.NDArray[np.complexfloating]) -> UDCTCoefficients:
|
|
110
|
+
ibeg = 0
|
|
111
|
+
coeffs: UDCTCoefficients = []
|
|
112
|
+
for ires, decres in enumerate(self.decimation):
|
|
113
|
+
coeffs.append([])
|
|
114
|
+
for idir, decdir in enumerate(decres):
|
|
115
|
+
coeffs[ires].append([])
|
|
116
|
+
for _ in self.windows[ires][idir]:
|
|
117
|
+
shape_decim = self.shape // decdir
|
|
118
|
+
iend = ibeg + prod(shape_decim)
|
|
119
|
+
coeffs[ires][idir].append(
|
|
120
|
+
coeffs_vec[ibeg:iend].reshape(shape_decim)
|
|
121
|
+
)
|
|
122
|
+
ibeg = iend
|
|
123
|
+
return coeffs
|
|
124
|
+
|
|
125
|
+
def forward(self, x: np.ndarray) -> UDCTCoefficients:
|
|
126
|
+
return udctmddec(x, self.params, self.windows, self.decimation)
|
|
127
|
+
|
|
128
|
+
def backward(self, c: UDCTCoefficients) -> np.ndarray:
|
|
129
|
+
return udctmdrec(c, self.params, self.windows, self.decimation)
|
|
130
|
+
|
|
131
|
+
|
|
132
|
+
class SimpleUDCT(UDCT):
|
|
133
|
+
def __init__(
|
|
134
|
+
self,
|
|
135
|
+
shape: tuple[int, ...],
|
|
136
|
+
nscales: int = 3,
|
|
137
|
+
nbands_per_direction: int = 3,
|
|
138
|
+
alpha: float | None = None,
|
|
139
|
+
winthresh: float = 1e-5,
|
|
140
|
+
) -> None:
|
|
141
|
+
assert nscales > 1
|
|
142
|
+
assert nbands_per_direction >= 3
|
|
143
|
+
|
|
144
|
+
dim = len(shape)
|
|
145
|
+
nbands: npt.NDArray[np.int_] = (
|
|
146
|
+
nbands_per_direction * 2 ** np.arange(nscales - 1)
|
|
147
|
+
).astype(int)
|
|
148
|
+
|
|
149
|
+
if alpha is None:
|
|
150
|
+
if nbands_per_direction == 3:
|
|
151
|
+
alpha = 0.15
|
|
152
|
+
elif nbands_per_direction == 4:
|
|
153
|
+
alpha = 0.3
|
|
154
|
+
elif nbands_per_direction == 5:
|
|
155
|
+
alpha = 0.5
|
|
156
|
+
else:
|
|
157
|
+
alpha = 0.5
|
|
158
|
+
for i, nb in enumerate(nbands, start=1):
|
|
159
|
+
if 2**i * (1 + 2 * alpha) * (1 + alpha) >= nb:
|
|
160
|
+
msg = f"alpha={alpha:.3f} does not respect respect the relationship (2^{i}/{nb})(1+2α)(1+α) < 1 for scale {i+1}" # noqa: RUF001
|
|
161
|
+
logging.warning(msg)
|
|
162
|
+
cfg = np.tile(nbands[:, None], dim)
|
|
163
|
+
r: tuple[float, float, float, float] = tuple(
|
|
164
|
+
np.array([1.0, 2.0, 2.0, 4.0]) * np.pi / 3
|
|
165
|
+
)
|
|
166
|
+
|
|
167
|
+
super().__init__(shape=shape, cfg=cfg, alpha=alpha, r=r, winthresh=winthresh)
|
|
@@ -0,0 +1,315 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
__all__ = ["udctmdwin"]
|
|
4
|
+
from itertools import combinations
|
|
5
|
+
from typing import Any
|
|
6
|
+
|
|
7
|
+
# import matplotlib.pyplot as plt
|
|
8
|
+
import numpy as np
|
|
9
|
+
import numpy.typing as npt
|
|
10
|
+
|
|
11
|
+
from .typing import UDCTWindows
|
|
12
|
+
from .utils import (
|
|
13
|
+
ParamUDCT,
|
|
14
|
+
adapt_grid,
|
|
15
|
+
angle_fun,
|
|
16
|
+
angle_kron,
|
|
17
|
+
circshift,
|
|
18
|
+
fftflip,
|
|
19
|
+
from_sparse_new,
|
|
20
|
+
fun_meyer,
|
|
21
|
+
to_sparse_new,
|
|
22
|
+
)
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
def _create_bandpass_windows(
|
|
26
|
+
nscales: int, shape: tuple[int, ...], r: tuple[float, float, float, float]
|
|
27
|
+
) -> tuple[dict[int, np.ndarray], dict[int, np.ndarray]]:
|
|
28
|
+
dim = len(shape)
|
|
29
|
+
shape_grid: dict[int, np.ndarray] = {}
|
|
30
|
+
meyers: dict[tuple[int, int], np.ndarray] = {}
|
|
31
|
+
for idim in range(dim):
|
|
32
|
+
# Don't take the np.pi out of the linspace
|
|
33
|
+
shape_grid[idim] = np.linspace(
|
|
34
|
+
-1.5 * np.pi, 0.5 * np.pi, shape[idim], endpoint=False
|
|
35
|
+
)
|
|
36
|
+
|
|
37
|
+
params = np.array([-2, -1, *r[:2]])
|
|
38
|
+
abs_shape_grid = np.abs(shape_grid[idim])
|
|
39
|
+
meyers[(nscales, idim)] = fun_meyer(abs_shape_grid, *params)
|
|
40
|
+
if nscales == 1:
|
|
41
|
+
meyers[(nscales, idim)] += fun_meyer(
|
|
42
|
+
np.abs(shape_grid[idim] + 2 * np.pi), *params
|
|
43
|
+
)
|
|
44
|
+
params[2:] = r[2:]
|
|
45
|
+
meyers[(nscales + 1, idim)] = fun_meyer(abs_shape_grid, *params)
|
|
46
|
+
|
|
47
|
+
for jn in range(nscales - 1, 0, -1):
|
|
48
|
+
params[2:] = r[:2]
|
|
49
|
+
params[2:] /= 2 ** (nscales - jn)
|
|
50
|
+
meyers[(jn, idim)] = fun_meyer(abs_shape_grid, *params)
|
|
51
|
+
|
|
52
|
+
bandpasses: dict[int, np.ndarray] = {}
|
|
53
|
+
for jn in range(nscales, 0, -1):
|
|
54
|
+
lo = np.array([1.0])
|
|
55
|
+
hi = np.array([1.0])
|
|
56
|
+
for idim in range(dim - 1, -1, -1):
|
|
57
|
+
lo = np.kron(meyers[(jn, idim)], lo)
|
|
58
|
+
hi = np.kron(meyers[(jn + 1, idim)], hi)
|
|
59
|
+
lo_nd = lo.reshape(*shape)
|
|
60
|
+
hi_nd = hi.reshape(*shape)
|
|
61
|
+
bp_nd = hi_nd - lo_nd
|
|
62
|
+
bp_nd[bp_nd < 0] = 0
|
|
63
|
+
bandpasses[jn] = bp_nd
|
|
64
|
+
bandpasses[0] = lo_nd
|
|
65
|
+
return shape_grid, bandpasses
|
|
66
|
+
|
|
67
|
+
|
|
68
|
+
def _nchoosek(n: Any, k: Any) -> np.ndarray:
|
|
69
|
+
return np.asarray(list(combinations(n, k)))
|
|
70
|
+
|
|
71
|
+
|
|
72
|
+
def _create_mdirs(dim: int, res: int) -> list[np.ndarray]:
|
|
73
|
+
# Mdir is dimension of need to calculate angle function on each
|
|
74
|
+
# hyperpyramid
|
|
75
|
+
return [
|
|
76
|
+
np.c_[[np.r_[np.arange(idim), np.arange(idim + 1, dim)] for idim in range(dim)]]
|
|
77
|
+
for ires in range(res)
|
|
78
|
+
]
|
|
79
|
+
# Mdirs: dict[int, np.ndarray] = {}
|
|
80
|
+
# for ires in range(res):
|
|
81
|
+
# Mdirs[ires] = np.zeros((dim, dim - 1), dtype=int)
|
|
82
|
+
# for idim in range(dim):
|
|
83
|
+
# Mdirs[ires][idim, :] = np.r_[range(idim), range(idim + 1, dim)]
|
|
84
|
+
|
|
85
|
+
# return Mdirs
|
|
86
|
+
|
|
87
|
+
|
|
88
|
+
def _create_angle_info(
|
|
89
|
+
Sgrid: dict[int, np.ndarray], dim: int, res: int, cfg: np.ndarray, alpha: float
|
|
90
|
+
) -> tuple[
|
|
91
|
+
dict[int, dict[tuple[int, int], np.ndarray]],
|
|
92
|
+
dict[int, dict[tuple[int, int], np.ndarray]],
|
|
93
|
+
]:
|
|
94
|
+
# every combination of 2 dimension out of 1:dim
|
|
95
|
+
mperms = _nchoosek(np.arange(dim), 2)
|
|
96
|
+
Mgrid: dict[tuple[int, int], np.ndarray] = {}
|
|
97
|
+
for ind, perm in enumerate(mperms):
|
|
98
|
+
out = adapt_grid(Sgrid[perm[0]], Sgrid[perm[1]])
|
|
99
|
+
Mgrid[(ind, 0)] = out[0]
|
|
100
|
+
Mgrid[(ind, 1)] = out[1]
|
|
101
|
+
|
|
102
|
+
# gather angle function for each pyramid
|
|
103
|
+
Mangs: dict[int, dict[tuple[int, int], np.ndarray]] = {}
|
|
104
|
+
Minds: dict[int, dict[tuple[int, int], np.ndarray]] = {}
|
|
105
|
+
for ires in range(res):
|
|
106
|
+
Mangs[ires] = {}
|
|
107
|
+
Minds[ires] = {}
|
|
108
|
+
# for each resolution
|
|
109
|
+
for idim in range(dim):
|
|
110
|
+
# for each pyramid in resolution res
|
|
111
|
+
cnt = 0
|
|
112
|
+
# cnt is number of angle function required for each pyramid
|
|
113
|
+
# now loop through mperms
|
|
114
|
+
for ihyp in range(mperms.shape[0]):
|
|
115
|
+
for idir in range(mperms.shape[1]):
|
|
116
|
+
if mperms[ihyp, idir] == idim:
|
|
117
|
+
Mangs[ires][(idim, cnt)] = angle_fun(
|
|
118
|
+
Mgrid[(ihyp, idir)],
|
|
119
|
+
idir + 1,
|
|
120
|
+
cfg[ires, mperms[ihyp, 1 - idir]],
|
|
121
|
+
alpha,
|
|
122
|
+
)
|
|
123
|
+
Minds[ires][(idim, cnt)] = mperms[ihyp, :] + 1
|
|
124
|
+
cnt += 1
|
|
125
|
+
return Mangs, Minds
|
|
126
|
+
|
|
127
|
+
|
|
128
|
+
def _inplace_normalize_windows(
|
|
129
|
+
udctwin: UDCTWindows, size: tuple[int, ...], dim: int, res: int
|
|
130
|
+
) -> None:
|
|
131
|
+
sumw2 = np.zeros(size)
|
|
132
|
+
idx, val = from_sparse_new(udctwin[0][0][0])
|
|
133
|
+
sumw2.flat[idx] += val**2
|
|
134
|
+
for ires in range(1, res + 1):
|
|
135
|
+
for idir in range(dim):
|
|
136
|
+
for iang in range(len(udctwin[ires][idir])):
|
|
137
|
+
tmpw = np.zeros(size)
|
|
138
|
+
idx, val = from_sparse_new(udctwin[ires][idir][iang])
|
|
139
|
+
tmpw.flat[idx] += val**2
|
|
140
|
+
sumw2 += tmpw
|
|
141
|
+
tmpw = fftflip(tmpw, idir)
|
|
142
|
+
sumw2 += tmpw
|
|
143
|
+
|
|
144
|
+
sumw2 = np.sqrt(sumw2)
|
|
145
|
+
idx, val = from_sparse_new(udctwin[0][0][0])
|
|
146
|
+
val /= sumw2.ravel()[idx]
|
|
147
|
+
for ires in range(1, res + 1):
|
|
148
|
+
for idir in range(dim):
|
|
149
|
+
for iang in range(len(udctwin[ires][idir])):
|
|
150
|
+
idx, val = from_sparse_new(udctwin[ires][idir][iang])
|
|
151
|
+
val /= sumw2.ravel()[idx]
|
|
152
|
+
|
|
153
|
+
|
|
154
|
+
def _calculate_decimation_ratios_with_lowest(
|
|
155
|
+
res: int, dim: int, cfg: np.ndarray, Mdirs: list[np.ndarray]
|
|
156
|
+
) -> list[npt.NDArray[np.int_]]:
|
|
157
|
+
decimation_ratio: list[npt.NDArray[np.int_]] = [
|
|
158
|
+
np.full((1, dim), fill_value=2 ** (res - 1), dtype=int)
|
|
159
|
+
]
|
|
160
|
+
for ires in range(1, res + 1):
|
|
161
|
+
decimation_ratio.append(
|
|
162
|
+
np.full((dim, dim), fill_value=2.0 ** (res - ires + 1), dtype=int)
|
|
163
|
+
)
|
|
164
|
+
for i0 in range(dim):
|
|
165
|
+
i1s = Mdirs[ires - 1][i0, :]
|
|
166
|
+
decimation_ratio[ires][i0, i1s] = (
|
|
167
|
+
2 * cfg[ires - 1, i1s] * 2 ** (res - ires) // 3
|
|
168
|
+
)
|
|
169
|
+
return decimation_ratio
|
|
170
|
+
|
|
171
|
+
|
|
172
|
+
def _inplace_sort_windows(
|
|
173
|
+
udctwin: UDCTWindows, indices: dict[int, dict[int, np.ndarray]], res: int, dim: int
|
|
174
|
+
) -> None:
|
|
175
|
+
for ires in range(1, res + 1):
|
|
176
|
+
for idim in range(dim):
|
|
177
|
+
mlist = indices[ires][idim]
|
|
178
|
+
|
|
179
|
+
# # Approach 1: Create a structured array and then sort by fields
|
|
180
|
+
# struct = [(f"x{i}", "<i8") for i in range(mlist.shape[1])]
|
|
181
|
+
# ix = np.argsort(
|
|
182
|
+
# np.array([tuple(m) for m in mlist], dtype=struct),
|
|
183
|
+
# order=tuple(t[0] for t in struct),
|
|
184
|
+
# )
|
|
185
|
+
#
|
|
186
|
+
# Approach 2: Create a 1D array then sort that array
|
|
187
|
+
m = mlist.max() + 1
|
|
188
|
+
ix = np.argsort(
|
|
189
|
+
sum(
|
|
190
|
+
m**i2 * mlist[:, i1]
|
|
191
|
+
for i1, i2 in enumerate(range(mlist.shape[1] - 1, -1, -1))
|
|
192
|
+
)
|
|
193
|
+
)
|
|
194
|
+
|
|
195
|
+
indices[ires][idim] = mlist[ix]
|
|
196
|
+
udctwin[ires][idim] = [udctwin[ires][idim][idx] for idx in ix]
|
|
197
|
+
|
|
198
|
+
|
|
199
|
+
def udctmdwin(
|
|
200
|
+
param_udct: ParamUDCT,
|
|
201
|
+
) -> tuple[UDCTWindows, list[npt.NDArray[np.int_]], dict[int, dict[int, np.ndarray]]]:
|
|
202
|
+
Sgrid, F2d = _create_bandpass_windows(
|
|
203
|
+
nscales=param_udct.res, shape=param_udct.size, r=param_udct.r
|
|
204
|
+
)
|
|
205
|
+
Winlow = circshift(np.sqrt(F2d[0]), tuple(s // 4 for s in param_udct.size))
|
|
206
|
+
|
|
207
|
+
# convert to sparse format
|
|
208
|
+
udctwin: UDCTWindows = []
|
|
209
|
+
udctwin.append([])
|
|
210
|
+
udctwin[0].append([])
|
|
211
|
+
udctwin[0][0] = [to_sparse_new(Winlow, param_udct.winthresh)]
|
|
212
|
+
|
|
213
|
+
# `indices` gets stored as `param_udct.ind` in the original.
|
|
214
|
+
indices: dict[int, dict[int, np.ndarray]] = {}
|
|
215
|
+
indices[0] = {}
|
|
216
|
+
indices[0][0] = np.zeros((1, 1), dtype=int)
|
|
217
|
+
Mdirs = _create_mdirs(dim=param_udct.dim, res=param_udct.res)
|
|
218
|
+
Mangs, Minds = _create_angle_info(
|
|
219
|
+
Sgrid,
|
|
220
|
+
dim=param_udct.dim,
|
|
221
|
+
res=param_udct.res,
|
|
222
|
+
cfg=param_udct.cfg,
|
|
223
|
+
alpha=param_udct.alpha,
|
|
224
|
+
)
|
|
225
|
+
|
|
226
|
+
# decimation ratio for each band
|
|
227
|
+
decimation_ratio = _calculate_decimation_ratios_with_lowest(
|
|
228
|
+
res=param_udct.res, dim=param_udct.dim, cfg=param_udct.cfg, Mdirs=Mdirs
|
|
229
|
+
)
|
|
230
|
+
|
|
231
|
+
# Mang is 1-d angle function for each hyper pyramid (row) and each angle
|
|
232
|
+
# dimension (column)
|
|
233
|
+
for ires in range(1, param_udct.res + 1): # pylint: disable=too-many-nested-blocks
|
|
234
|
+
# for each resolution
|
|
235
|
+
udctwin.append([])
|
|
236
|
+
indices[ires] = {}
|
|
237
|
+
for idim in range(param_udct.dim):
|
|
238
|
+
udctwin[ires].append([])
|
|
239
|
+
# for each hyperpyramid
|
|
240
|
+
i1_ang = np.arange(len(Mangs[ires - 1][(idim, 0)]))[:, None]
|
|
241
|
+
for i2 in range(1, param_udct.dim - 1):
|
|
242
|
+
ln = len(Mangs[ires - 1][(idim, i2)])
|
|
243
|
+
tmp2 = np.arange(len(Mangs[ires - 1][(idim, i2)]))[:, None]
|
|
244
|
+
tmp3 = np.kron(i1_ang, np.ones((ln, 1), dtype=int))
|
|
245
|
+
tmp4 = np.kron(np.ones((i1_ang.shape[0], 1), dtype=int), tmp2)
|
|
246
|
+
i1_ang = np.c_[tmp3, tmp4]
|
|
247
|
+
lent = i1_ang.shape[0]
|
|
248
|
+
ang_inmax = param_udct.cfg[ires - 1, Mdirs[ires - 1][idim, :]]
|
|
249
|
+
# lent is the smallest number of windows need to calculated on each
|
|
250
|
+
# pyramid
|
|
251
|
+
# ang_inmax is M-1 vector contain number of angle function per each
|
|
252
|
+
# dimension of the hyperpyramid
|
|
253
|
+
for i3 in range(lent):
|
|
254
|
+
# for each calculated windows function, estimated all the other
|
|
255
|
+
# flipped window functions
|
|
256
|
+
win: npt.NDArray[np.floating] = np.ones(param_udct.size, dtype=float)
|
|
257
|
+
for i4 in range(param_udct.dim - 1):
|
|
258
|
+
idx = i1_ang.reshape(len(i1_ang), -1)[i3, i4]
|
|
259
|
+
tmp1 = Mangs[ires - 1][(idim, i4)][idx]
|
|
260
|
+
tmp2 = Minds[ires - 1][(idim, i4)]
|
|
261
|
+
afun2 = angle_kron(tmp1, tmp2, param_udct)
|
|
262
|
+
win *= afun2
|
|
263
|
+
win *= F2d[ires]
|
|
264
|
+
win = np.sqrt(circshift(win, tuple(s // 4 for s in param_udct.size)))
|
|
265
|
+
|
|
266
|
+
# first windows function
|
|
267
|
+
angle_functions = []
|
|
268
|
+
angle_functions.append(win)
|
|
269
|
+
|
|
270
|
+
# index of current angle
|
|
271
|
+
i2_ang = i1_ang[i3 : i3 + 1, :] + 1
|
|
272
|
+
|
|
273
|
+
# all possible flip along different dimension
|
|
274
|
+
for i5 in range(param_udct.dim - 2, -1, -1):
|
|
275
|
+
for i6 in range(i2_ang.shape[0]):
|
|
276
|
+
if 2 * i2_ang[i6, i5] <= ang_inmax[i5]:
|
|
277
|
+
i2_ang_tmp = i2_ang[i6 : i6 + 1, :].copy()
|
|
278
|
+
i2_ang_tmp[0, i5] = ang_inmax[i5] + 1 - i2_ang[i6, i5]
|
|
279
|
+
i2_ang = np.r_[i2_ang, i2_ang_tmp]
|
|
280
|
+
win = fftflip(
|
|
281
|
+
angle_functions[i6], Mdirs[ires - 1][idim, i5]
|
|
282
|
+
)
|
|
283
|
+
angle_functions.append(win)
|
|
284
|
+
i2_ang -= 1 # Adjust so that `indices` is 0-based
|
|
285
|
+
angle_functions = np.c_[angle_functions]
|
|
286
|
+
|
|
287
|
+
if i3 == 0:
|
|
288
|
+
ang_ind = i2_ang
|
|
289
|
+
for i7 in range(ang_ind.shape[0]):
|
|
290
|
+
udctwin[ires][idim].append(
|
|
291
|
+
to_sparse_new(angle_functions[i7], param_udct.winthresh)
|
|
292
|
+
)
|
|
293
|
+
else:
|
|
294
|
+
inold = ang_ind.shape[0]
|
|
295
|
+
ang_ind = np.concatenate((ang_ind, i2_ang), axis=0)
|
|
296
|
+
innew = ang_ind.shape[0]
|
|
297
|
+
for i7 in range(inold, innew):
|
|
298
|
+
udctwin[ires][idim].append(
|
|
299
|
+
to_sparse_new(
|
|
300
|
+
angle_functions[i7 - inold], param_udct.winthresh
|
|
301
|
+
)
|
|
302
|
+
)
|
|
303
|
+
indices[ires][idim] = ang_ind.copy()
|
|
304
|
+
|
|
305
|
+
# Normalization
|
|
306
|
+
_inplace_normalize_windows(
|
|
307
|
+
udctwin, size=param_udct.size, dim=param_udct.dim, res=param_udct.res
|
|
308
|
+
)
|
|
309
|
+
|
|
310
|
+
# sort the window
|
|
311
|
+
_inplace_sort_windows(
|
|
312
|
+
udctwin=udctwin, indices=indices, res=param_udct.res, dim=param_udct.dim
|
|
313
|
+
)
|
|
314
|
+
|
|
315
|
+
return udctwin, decimation_ratio, indices
|