Skip to content

Commit a12fe4f

Browse files
authored
Merge pull request #747 from mrava87/fix-bilinearcast
Fix: dtype consistency in operators
2 parents 5ee0eda + 0a54fdb commit a12fe4f

53 files changed

Lines changed: 2951 additions & 1141 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

pylops/avo/avo.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -677,7 +677,9 @@ def __init__(
677677
dimsd += self.spatdims
678678
super().__init__(dtype=np.dtype(dtype), dims=dims, dimsd=dimsd, name=name)
679679

680-
self.G = self.ncp.concatenate([gs.T[:, self.ncp.newaxis] for gs in Gs], axis=1)
680+
self.G = self.ncp.concatenate(
681+
[gs.astype(dtype).T[:, self.ncp.newaxis] for gs in Gs], axis=1
682+
)
681683
# add dimensions to G to account for horizonal axes
682684
for _ in range(len(self.spatdims)):
683685
self.G = self.G[..., np.newaxis]

pylops/basicoperators/laplacian.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
__all__ = ["Laplacian"]
22

3+
import numpy as np
34

45
from pylops import LinearOperator
56
from pylops.basicoperators import SecondDerivative
@@ -124,13 +125,17 @@ def _calc_l2op(
124125
kind: Tderivkind,
125126
dtype: DTypeLike,
126127
):
128+
weights = np.array(weights, dtype=dtype)
129+
sampling = np.array(sampling, dtype=dtype)
127130
l2op = SecondDerivative(
128131
dims, axis=axes[0], sampling=sampling[0], edge=edge, kind=kind, dtype=dtype
129132
)
130133
dims = l2op.dims
131134
l2op *= weights[0]
132135
for ax, samp, weight in zip(axes[1:], sampling[1:], weights[1:], strict=True):
133-
l2op += weight * SecondDerivative(
136+
tmpop = SecondDerivative(
134137
dims, axis=ax, sampling=samp, edge=edge, kind=kind, dtype=dtype
135138
)
139+
tmpop *= weight
140+
l2op += tmpop
136141
return l2op

pylops/basicoperators/smoothing1d.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ def __init__(
8787
):
8888
if nsmooth % 2 == 0:
8989
nsmooth += 1
90-
h = np.ones(nsmooth) / float(nsmooth)
90+
h = np.ones(nsmooth, dtype=dtype) / float(nsmooth)
9191
offset = (nsmooth - 1) // 2
9292
super().__init__(
9393
dims=dims, h=h, axis=axis, offset=offset, dtype=dtype, name=name

pylops/basicoperators/smoothing2d.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,9 @@ def __init__(
8080
nsmooth[0] += 1
8181
if nsmooth[1] % 2 == 0:
8282
nsmooth[1] += 1
83-
h = np.ones((nsmooth[0], nsmooth[1])) / float(nsmooth[0] * nsmooth[1])
83+
h = np.ones((nsmooth[0], nsmooth[1]), dtype=dtype) / float(
84+
nsmooth[0] * nsmooth[1]
85+
)
8486
offset = [(nsmooth[0] - 1) // 2, (nsmooth[1] - 1) // 2]
8587
super().__init__(
8688
dims=dims, h=h, offset=offset, axes=axes, dtype=dtype, name=name

pylops/basicoperators/smoothingnd.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ def __init__(
7878
for i in range(len(nsmooth)):
7979
if nsmooth[i] % 2 == 0:
8080
nsmooth[i] += 1
81-
h = np.ones(nsmooth) / float(np.prod(nsmooth))
81+
h = np.ones(nsmooth, dtype=dtype) / float(np.prod(nsmooth))
8282
offset = [(nsm - 1) // 2 for nsm in nsmooth]
8383
super().__init__(
8484
dims=dims, h=h, offset=offset, axes=axes, dtype=dtype, name=name

pylops/signalprocessing/bilinear.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,7 @@ def __init__(
132132
)
133133

134134
ncp = get_array_module(iava)
135+
135136
# check non-unique pairs (works only with numpy arrays)
136137
_ensure_iava_is_unique(
137138
iava=to_numpy(iava),
@@ -141,10 +142,10 @@ def __init__(
141142
# find indices and weights
142143
self.iava_t = ncp.floor(iava[0]).astype(int)
143144
self.iava_b = self.iava_t + 1
144-
self.weights_tb = iava[0] - self.iava_t
145+
self.weights_tb = (iava[0] - self.iava_t).astype(self.dtype)
145146
self.iava_l = ncp.floor(iava[1]).astype(int)
146147
self.iava_r = self.iava_l + 1
147-
self.weights_lr = iava[1] - self.iava_l
148+
self.weights_lr = (iava[1] - self.iava_l).astype(self.dtype)
148149

149150
# expand dims to weights for nd-arrays
150151
if ndims > 2:

pylops/signalprocessing/interp.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ def _linearinterp(
5555
# find indices and weights
5656
iva_l = ncp.floor(iava).astype(int)
5757
iva_r = iva_l + 1
58-
weights = iava - iva_l
58+
weights = (iava - iva_l).astype(dtype)
5959

6060
# create operators
6161
Op = Diagonal(1 - weights, dims=dimsd, axis=axis, dtype=dtype) * Restriction(
@@ -81,7 +81,7 @@ def _sincinterp(
8181
nreg = dims[axis]
8282
ireg = ncp.arange(nreg)
8383
sinc = ncp.tile(iava[:, np.newaxis], (1, nreg)) - ncp.tile(ireg, (len(iava), 1))
84-
sinc = ncp.sinc(sinc)
84+
sinc = ncp.sinc(sinc).astype(dtype)
8585

8686
# identify additional dimensions and create MatrixMult operator
8787
otherdims = np.array(dims)

pylops/signalprocessing/nonstatconvolve2d.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -484,7 +484,7 @@ def __rmatvec(
484484
# the same loop (see https://numba.pydata.org/numba-doc/latest/user/parallel.html?highlight=njit).
485485
# Until atomic operations are provided we create a temporary filter array and store intermediate
486486
# results from each ix and reduce them at the end.
487-
hstmp = np.zeros((xdims[0], *hs.shape))
487+
hstmp = np.zeros((xdims[0], *hs.shape), dtype=x.dtype)
488488
for ix in prange(xdims[0]):
489489
for iz in range(xdims[1]):
490490
# find extremes of model where to apply h (in case h is going out of model)

pylops/signalprocessing/seislet.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -451,7 +451,7 @@ def __init__(
451451
super().__init__(dtype=np.dtype(dtype), dims=dims, dimsd=dimsd, name=name)
452452

453453
pad = [(0, ndimpow2 - self.dims[0])] + [(0, 0)] * (len(self.dims) - 1)
454-
self.pad = Pad(self.dims, pad)
454+
self.pad = Pad(self.dims, pad, dtype=self.dtype)
455455
self.nx, self.nt = self.dimsd
456456

457457
# define levels
@@ -473,7 +473,9 @@ def __init__(
473473
def _matvec(self, x: NDArray) -> NDArray:
474474
x = self.pad.matvec(x)
475475
x = np.reshape(x, self.dimsd)
476-
y = np.zeros((np.sum(self.levels_size) + self.levels_size[-1], self.nt))
476+
y = np.zeros(
477+
(np.sum(self.levels_size) + self.levels_size[-1], self.nt), dtype=self.dtype
478+
)
477479
for ilevel in range(self.level):
478480
odd = x[1::2]
479481
even = x[::2]
@@ -519,7 +521,7 @@ def _rmatvec(self, x: NDArray) -> NDArray:
519521
backward=False,
520522
adj=True,
521523
)
522-
y = np.zeros((2 * even.shape[0], self.nt))
524+
y = np.zeros((2 * even.shape[0], self.nt), dtype=self.dtype)
523525
y[1::2] = odd
524526
y[::2] = even
525527
y = self.pad.rmatvec(y.ravel())
@@ -542,7 +544,7 @@ def inverse(self, x: NDArray) -> NDArray:
542544
odd = res + self.predict(
543545
even, self.dt, self.dx, self.slopes, repeat=ilevel - 1, backward=False
544546
)
545-
y = np.zeros((2 * even.shape[0], self.nt))
547+
y = np.zeros((2 * even.shape[0], self.nt), dtype=self.dtype)
546548
y[1::2] = odd
547549
y[::2] = even
548550
y = self.pad.rmatvec(y.ravel())

pylops/signalprocessing/sliding1d.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,7 @@ def __init__(
199199
self.tapertype = tapertype
200200
self.savetaper = savetaper
201201
if self.tapertype is not None:
202-
tap = taper(nwin, nover, tapertype=self.tapertype)
202+
tap = taper(nwin, nover, tapertype=self.tapertype).astype(Op.dtype)
203203
tapin = tap.copy()
204204
tapin[:nover] = 1
205205
tapend = tap.copy()
@@ -247,7 +247,7 @@ def _matvec_savetaper(self, x: NDArray) -> NDArray:
247247
self.taps = to_cupy_conditional(x, self.taps)
248248
y = ncp.zeros(self.dimsd, dtype=self.dtype)
249249
if self.simOp:
250-
x = self.Op @ x
250+
x = self.Op.matvec(x.ravel()).reshape(self.Op.dimsd)
251251
if self.tapertype is not None:
252252
x = self.taps * x
253253
for iwin0 in range(self.dims[0]):
@@ -270,7 +270,7 @@ def _rmatvec_savetaper(self, x: NDArray) -> NDArray:
270270
if self.tapertype is not None:
271271
ywins = ywins * self.taps
272272
if self.simOp:
273-
y = self.Op.H @ ywins
273+
y = self.Op.rmatvec(ywins.ravel()).reshape(self.dims)
274274
else:
275275
y = ncp.zeros(self.dims, dtype=self.dtype)
276276
for iwin0 in range(self.dims[0]):
@@ -284,7 +284,7 @@ def _matvec_nosavetaper(self, x: NDArray) -> NDArray:
284284
self.taps = to_cupy_conditional(x, self.taps)
285285
y = ncp.zeros(self.dimsd, dtype=self.dtype)
286286
if self.simOp:
287-
x = self.Op @ x
287+
x = self.Op.matvec(x.ravel()).reshape(self.Op.dimsd)
288288
for iwin0 in range(self.dims[0]):
289289
if self.simOp:
290290
xxwin = x[iwin0]
@@ -311,7 +311,7 @@ def _rmatvec_nosavetaper(self, x: NDArray) -> NDArray:
311311
if self.tapertype is not None:
312312
for iwin0 in range(self.dims[0]):
313313
ywins = self._apply_taper(ywins, iwin0)
314-
y = self.Op.H @ ywins
314+
y = self.Op.rmatvec(ywins.ravel())
315315
else:
316316
y = ncp.zeros(self.dims, dtype=self.dtype)
317317
for iwin0 in range(self.dims[0]):

0 commit comments

Comments
 (0)