1- import numpy as np
1+ import os
2+
3+ if int (os .environ .get ("TEST_CUPY_PYLOPS" , 0 )):
4+ import cupy as np
5+ from cupy .testing import assert_array_almost_equal
6+
7+ backend = "cupy"
8+ else :
9+ import numpy as np
10+ from numpy .testing import assert_array_almost_equal
11+
12+ backend = "numpy"
13+ import numpy as npp
214import pytest
3- from numpy .testing import assert_array_almost_equal
415from scipy .signal import filtfilt
5- from scipy .sparse .linalg import lsqr
616
717from pylops .avo .avo import (
818 akirichards ,
1323 zoeppritz_scattering ,
1424)
1525from pylops .avo .prestack import AVOLinearModelling
26+ from pylops .optimization .basic import lsqr
1627from pylops .utils import dottest
28+ from pylops .utils .backend import to_numpy
1729
1830np .random .seed (0 )
1931
2436# Create medium parameters for multiple contrasts
2537nt0 = 201
2638dt0 = 0.004
27- t0 = np .arange (nt0 ) * dt0
28- vp = 1200 + np .arange (nt0 ) + filtfilt (np .ones (5 ) / 5.0 , 1 , np .random .normal (0 , 80 , nt0 ))
29- vs = 600 + vp / 2 + filtfilt (np .ones (5 ) / 5.0 , 1 , np .random .normal (0 , 20 , nt0 ))
30- rho = 1000 + vp + filtfilt (np .ones (5 ) / 5.0 , 1 , np .random .normal (0 , 30 , nt0 ))
31- m = (np .stack ((np .log (vp ), np .log (vs ), np .log (rho )), axis = 1 )).ravel ()
39+ t0 = npp .arange (nt0 ) * dt0
40+ vp = (
41+ 1200
42+ + npp .arange (nt0 )
43+ + filtfilt (npp .ones (5 ) / 5.0 , 1 , npp .random .normal (0 , 80 , nt0 ))
44+ )
45+ vs = 600 + vp / 2 + filtfilt (npp .ones (5 ) / 5.0 , 1 , npp .random .normal (0 , 20 , nt0 ))
46+ rho = 1000 + vp + filtfilt (npp .ones (5 ) / 5.0 , 1 , npp .random .normal (0 , 30 , nt0 ))
47+ m = npp .stack ((npp .log (vp ), npp .log (vs ), npp .log (rho )), axis = 1 ).ravel ()
3248
3349# Angles
3450ntheta = 21
3551thetamin , thetamax = 0 , 40
36- theta = np .linspace (thetamin , thetamax , ntheta )
52+ theta = npp .linspace (thetamin , thetamax , ntheta )
3753
3854# Parameters
3955par1 = {"vsvp" : 0.5 , "linearization" : "akirich" } # constant vsvp
@@ -47,16 +63,16 @@ def test_zoeppritz():
4763 `<https://www.crewes.org/ResearchLinks/ExplorerPrograms/ZE/index.html>`_
4864 as benchmark
4965 """
50- r_zoep = zoeppritz_scattering (vp1 , vs1 , rho1 , vp0 , vs0 , rho0 , theta [ 0 ])
66+ r_zoep = zoeppritz_scattering (vp1 , vs1 , rho1 , vp0 , vs0 , rho0 , np . asarray ( theta )[: 1 ])
5167 rpp_zoep = zoeppritz_element (
52- vp1 , vs1 , rho1 , vp0 , vs0 , rho0 , theta [ 0 ], element = "PdPu"
68+ vp1 , vs1 , rho1 , vp0 , vs0 , rho0 , np . asarray ( theta )[: 1 ], element = "PdPu"
5369 )
54- rpp_zoep1 = zoeppritz_pp (vp1 , vs1 , rho1 , vp0 , vs0 , rho0 , theta [ 0 ])
70+ rpp_zoep1 = zoeppritz_pp (vp1 , vs1 , rho1 , vp0 , vs0 , rho0 , np . asarray ( theta )[: 1 ])
5571
5672 assert r_zoep .shape == (4 , 4 , 1 )
57- assert r_zoep [0 , 0 ] == pytest .approx (0.04658 , rel = 1e-3 )
58- assert rpp_zoep == pytest .approx (0.04658 , rel = 1e-3 )
59- assert rpp_zoep1 == pytest .approx (0.04658 , rel = 1e-3 )
73+ assert to_numpy ( r_zoep [0 , 0 , 0 ]) == pytest .approx (0.04658 , rel = 1e-3 )
74+ assert to_numpy ( rpp_zoep ) == pytest .approx (0.04658 , rel = 1e-3 )
75+ assert to_numpy ( rpp_zoep1 ) == pytest .approx (0.04658 , rel = 1e-3 )
6076
6177
6278def test_zoeppritz_and_approx_zeroangle ():
@@ -66,22 +82,24 @@ def test_zoeppritz_and_approx_zeroangle():
6682 ai1 , si1 , _ = vp1 * rho1 , vs1 * rho1 , vp1 / vs1
6783
6884 # Zoeppritz
69- rpp_zoep = zoeppritz_pp (vp1 , vs1 , rho1 , vp0 , vs0 , rho0 , theta [0 ])
70- rpp_zoep_approx = approx_zoeppritz_pp (vp1 , vs1 , rho1 , vp0 , vs0 , rho0 , theta [0 ])
85+ rpp_zoep = zoeppritz_pp (vp1 , vs1 , rho1 , vp0 , vs0 , rho0 , np .asarray (theta )[:1 ])
86+ rpp_zoep_approx = approx_zoeppritz_pp (
87+ vp1 , vs1 , rho1 , vp0 , vs0 , rho0 , np .asarray (theta )[:1 ]
88+ )
7189
7290 # Aki Richards
73- rvp = np .log (vp0 ) - np .log (vp1 )
74- rvs = np .log (vs0 ) - np .log (vs1 )
75- rrho = np .log (rho0 ) - np .log (rho1 )
91+ rvp = np .asarray ( np . log (vp0 ) - np .log (vp1 ) )
92+ rvs = np .asarray ( np . log (vs0 ) - np .log (vs1 ) )
93+ rrho = np .asarray ( np . log (rho0 ) - np .log (rho1 ) )
7694
77- G1 , G2 , G3 = akirichards (theta [ 0 ], vs1 / vp1 )
95+ G1 , G2 , G3 = akirichards (np . asarray ( theta )[: 1 ], vs1 / vp1 )
7896 rpp_aki = G1 * rvp + G2 * rvs + G3 * rrho
7997
8098 # Fatti
81- rai = np .log (ai0 ) - np .log (ai1 )
82- rsi = np .log (si0 ) - np .log (si1 )
99+ rai = np .asarray ( np . log (ai0 ) - np .log (ai1 ) )
100+ rsi = np .asarray ( np . log (si0 ) - np .log (si1 ) )
83101
84- G1 , G2 , G3 = fatti (theta [ 0 ], vs1 / vp1 )
102+ G1 , G2 , G3 = fatti (np . asarray ( theta )[: 1 ], vs1 / vp1 )
85103 rpp_fatti = G1 * rai + G2 * rsi + G3 * rrho
86104
87105 assert_array_almost_equal (rpp_zoep , rpp_zoep_approx , decimal = 3 )
@@ -97,22 +115,24 @@ def test_zoeppritz_and_approx_multipleangles():
97115 ai1 , si1 = vp1 * rho1 , vs1 * rho1
98116
99117 # Zoeppritz
100- rpp_zoep = zoeppritz_pp (vp1 , vs1 , rho1 , vp0 , vs0 , rho0 , theta )
101- rpp_zoep_approx = approx_zoeppritz_pp (vp1 , vs1 , rho1 , vp0 , vs0 , rho0 , theta )
118+ rpp_zoep = zoeppritz_pp (vp1 , vs1 , rho1 , vp0 , vs0 , rho0 , np .asarray (theta ))
119+ rpp_zoep_approx = approx_zoeppritz_pp (
120+ vp1 , vs1 , rho1 , vp0 , vs0 , rho0 , np .asarray (theta )
121+ )
102122
103123 # Aki Richards
104- rvp = np .log (vp0 ) - np .log (vp1 )
105- rvs = np .log (vs0 ) - np .log (vs1 )
106- rrho = np .log (rho0 ) - np .log (rho1 )
124+ rvp = np .asarray ( np . log (vp0 ) - np .log (vp1 ) )
125+ rvs = np .asarray ( np . log (vs0 ) - np .log (vs1 ) )
126+ rrho = np .asarray ( np . log (rho0 ) - np .log (rho1 ) )
107127
108- G1 , G2 , G3 = akirichards (theta , vs1 / vp1 )
128+ G1 , G2 , G3 = akirichards (np . asarray ( theta ) , vs1 / vp1 )
109129 rpp_aki = G1 * rvp + G2 * rvs + G3 * rrho
110130
111131 # Fatti
112- rai = np .log (ai0 ) - np .log (ai1 )
113- rsi = np .log (si0 ) - np .log (si1 )
132+ rai = np .asarray ( np . log (ai0 ) - np .log (ai1 ) )
133+ rsi = np .asarray ( np . log (si0 ) - np .log (si1 ) )
114134
115- G1 , G2 , G3 = fatti (theta , vs1 / vp1 )
135+ G1 , G2 , G3 = fatti (np . asarray ( theta ) , vs1 / vp1 )
116136 rpp_fatti = G1 * rai + G2 * rsi + G3 * rrho
117137
118138 assert_array_almost_equal (rpp_zoep , rpp_zoep_approx , decimal = 3 )
@@ -124,11 +144,21 @@ def test_zoeppritz_and_approx_multipleangles():
124144def test_AVOLinearModelling (par ):
125145 """Dot-test and inversion for AVOLinearModelling"""
126146 AVOop = AVOLinearModelling (
127- theta , vsvp = par ["vsvp" ], nt0 = nt0 , linearization = par ["linearization" ]
147+ np .asarray (theta ),
148+ vsvp = par ["vsvp" ] if isinstance (par ["vsvp" ], float ) else np .asarray (par ["vsvp" ]),
149+ nt0 = nt0 ,
150+ linearization = par ["linearization" ],
128151 )
129- assert dottest (AVOop , ntheta * nt0 , 3 * nt0 )
152+ assert dottest (AVOop , ntheta * nt0 , 3 * nt0 , backend = backend )
130153
131154 minv = lsqr (
132- AVOop , AVOop * m , damp = 1e-20 , iter_lim = 1000 , atol = 1e-8 , btol = 1e-8 , show = 0
155+ AVOop ,
156+ AVOop * np .asarray (m ),
157+ x0 = np .zeros_like (m ),
158+ damp = 1e-20 ,
159+ niter = 1000 ,
160+ atol = 1e-8 ,
161+ btol = 1e-8 ,
162+ show = 0 ,
133163 )[0 ]
134164 assert_array_almost_equal (m , minv , decimal = 3 )
0 commit comments