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

update assert np.allclose to numpy.testing.assert_allclose in tests #1904

Open
wants to merge 1 commit into
base: v4-dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 26 additions & 25 deletions tests/test_advection.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import numpy as np
import pytest
import xarray as xr
from numpy.testing import assert_allclose

from parcels import (
AdvectionAnalytical,
Expand Down Expand Up @@ -98,7 +99,7 @@ def test_advection_meridional(lon, lat):
pset = ParticleSet(fieldset, pclass=Particle, lon=np.linspace(-60, 60, npart), lat=np.linspace(0, 30, npart))
delta_lat = np.diff(pset.lat)
pset.execute(AdvectionRK4, runtime=timedelta(hours=2), dt=timedelta(seconds=30))
assert np.allclose(np.diff(pset.lat), delta_lat, rtol=1.0e-4)
assert_allclose(np.diff(pset.lat), delta_lat, rtol=1.0e-4)


def test_advection_3D():
Expand All @@ -119,7 +120,7 @@ def test_advection_3D():
)
time = timedelta(hours=2).total_seconds()
pset.execute(AdvectionRK4, runtime=time, dt=timedelta(seconds=30))
assert np.allclose(pset.depth * pset.time, pset.lon, atol=1.0e-1)
assert_allclose(pset.depth * pset.time, pset.lon, atol=1.0e-1)


@pytest.mark.parametrize("direction", ["up", "down"])
Expand Down Expand Up @@ -161,8 +162,8 @@ def SubmergeParticle(particle, fieldset, time): # pragma: no cover
pset.execute(kernels, runtime=11.0, dt=1)

if direction == "up" and wErrorThroughSurface:
assert np.allclose(pset.lon[0], 0.6)
assert np.allclose(pset.depth[0], 0)
assert_allclose(pset.lon[0], 0.6)
assert_allclose(pset.depth[0], 0)
else:
assert len(pset) == 0

Expand Down Expand Up @@ -227,7 +228,7 @@ def test_conversion_3DCROCO():
for zi, z in enumerate(z_xroms):
sigma[zi] = _croco_from_z_to_sigma_scipy(fieldset, 0, z, lat, lon, None)

assert np.allclose(sigma, s_xroms, atol=1e-3)
assert_allclose(sigma, s_xroms, atol=1e-3)


@pytest.mark.v4alpha
Expand All @@ -246,8 +247,8 @@ def SampleW(particle, fieldset, time): # pragma: no cover
particle.w = fieldset.W[time, particle.depth, particle.lat, particle.lon]

pset.execute([AdvectionRK4_3D, SampleW], runtime=runtime, dt=100)
assert np.allclose(pset.depth, Z.flatten(), atol=5) # TODO lower this atol
assert np.allclose(pset.lon_nextloop, [x + runtime for x in X.flatten()], atol=1e-3)
assert_allclose(pset.depth, Z.flatten(), atol=5) # TODO lower this atol
assert_allclose(pset.lon_nextloop, [x + runtime for x in X.flatten()], atol=1e-3)


@pytest.mark.v4alpha
Expand All @@ -262,8 +263,8 @@ def test_advection_2DCROCO():
pset = ParticleSet(fieldset=fieldset, pclass=Particle, lon=X, lat=Y, depth=Z)

pset.execute([AdvectionRK4], runtime=runtime, dt=100)
assert np.allclose(pset.depth, Z.flatten(), atol=1e-3)
assert np.allclose(pset.lon_nextloop, [x + runtime for x in X], atol=1e-3)
assert_allclose(pset.depth, Z.flatten(), atol=1e-3)
assert_allclose(pset.lon_nextloop, [x + runtime for x in X], atol=1e-3)


def create_periodic_fieldset(xdim, ydim, uvel, vvel):
Expand Down Expand Up @@ -315,8 +316,8 @@ def test_advection_periodic_zonal_meridional():
fieldset.add_periodic_halo(zonal=True, meridional=True)
assert len(fieldset.U.lat) == ydim + 10 # default halo size is 5 grid points
assert len(fieldset.U.lon) == xdim + 10 # default halo size is 5 grid points
assert np.allclose(np.diff(fieldset.U.lat), fieldset.U.lat[1] - fieldset.U.lat[0], rtol=0.001)
assert np.allclose(np.diff(fieldset.U.lon), fieldset.U.lon[1] - fieldset.U.lon[0], rtol=0.001)
assert_allclose(np.diff(fieldset.U.lat), fieldset.U.lat[1] - fieldset.U.lat[0], rtol=0.001)
assert_allclose(np.diff(fieldset.U.lon), fieldset.U.lon[1] - fieldset.U.lon[0], rtol=0.001)

pset = ParticleSet(fieldset, pclass=Particle, lon=[0.4], lat=[0.5])
pset.execute(AdvectionRK4 + pset.Kernel(periodicBC), runtime=timedelta(hours=20), dt=timedelta(seconds=30))
Expand Down Expand Up @@ -431,8 +432,8 @@ def test_stationary_eddy(fieldset_stationary, method, rtol, diffField):

exp_lon = [truth_stationary(x, y, pset[0].time)[0] for x, y in zip(lon, lat, strict=True)]
exp_lat = [truth_stationary(x, y, pset[0].time)[1] for x, y in zip(lon, lat, strict=True)]
assert np.allclose(pset.lon, exp_lon, rtol=rtol)
assert np.allclose(pset.lat, exp_lat, rtol=rtol)
assert_allclose(pset.lon, exp_lon, rtol=rtol)
assert_allclose(pset.lat, exp_lat, rtol=rtol)


def test_stationary_eddy_vertical():
Expand Down Expand Up @@ -460,9 +461,9 @@ def test_stationary_eddy_vertical():
exp_lon = [truth_stationary(x, z, pset[0].time)[0] for x, z in zip(lon, depth, strict=True)]
exp_depth = [truth_stationary(x, z, pset[0].time)[1] for x, z in zip(lon, depth, strict=True)]
print(pset, exp_lon)
assert np.allclose(pset.lon, exp_lon, rtol=1e-5)
assert np.allclose(pset.lat, lat, rtol=1e-5)
assert np.allclose(pset.depth, exp_depth, rtol=1e-5)
assert_allclose(pset.lon, exp_lon, rtol=1e-5)
assert_allclose(pset.lat, lat, rtol=1e-5)
assert_allclose(pset.depth, exp_depth, rtol=1e-5)

data = {"U": fldzero, "V": fld2, "W": fld1}
fieldset = FieldSet.from_data(data, dimensions, mesh="flat")
Expand All @@ -471,9 +472,9 @@ def test_stationary_eddy_vertical():
pset.execute(AdvectionRK4_3D, dt=dt, endtime=endtime)
exp_depth = [truth_stationary(z, y, pset[0].time)[0] for z, y in zip(depth, lat, strict=True)]
exp_lat = [truth_stationary(z, y, pset[0].time)[1] for z, y in zip(depth, lat, strict=True)]
assert np.allclose(pset.lon, lon, rtol=1e-5)
assert np.allclose(pset.lat, exp_lat, rtol=1e-5)
assert np.allclose(pset.depth, exp_depth, rtol=1e-5)
assert_allclose(pset.lon, lon, rtol=1e-5)
assert_allclose(pset.lat, exp_lat, rtol=1e-5)
assert_allclose(pset.depth, exp_depth, rtol=1e-5)


def truth_moving(x_0, y_0, t):
Expand Down Expand Up @@ -536,8 +537,8 @@ def test_moving_eddy(fieldset_moving, method, rtol, diffField):

exp_lon = [truth_moving(x, y, t)[0] for x, y, t in zip(lon, lat, pset.time, strict=True)]
exp_lat = [truth_moving(x, y, t)[1] for x, y, t in zip(lon, lat, pset.time, strict=True)]
assert np.allclose(pset.lon, exp_lon, rtol=rtol)
assert np.allclose(pset.lat, exp_lat, rtol=rtol)
assert_allclose(pset.lon, exp_lon, rtol=rtol)
assert_allclose(pset.lat, exp_lat, rtol=rtol)


def truth_decaying(x_0, y_0, t):
Expand Down Expand Up @@ -619,8 +620,8 @@ def test_decaying_eddy(fieldset_decaying, method, rtol, diffField):

exp_lon = [truth_decaying(x, y, t)[0] for x, y, t in zip(lon, lat, pset.time, strict=True)]
exp_lat = [truth_decaying(x, y, t)[1] for x, y, t in zip(lon, lat, pset.time, strict=True)]
assert np.allclose(pset.lon, exp_lon, rtol=rtol)
assert np.allclose(pset.lat, exp_lat, rtol=rtol)
assert_allclose(pset.lon, exp_lon, rtol=rtol)
assert_allclose(pset.lat, exp_lat, rtol=rtol)


def test_analyticalAgrid():
Expand Down Expand Up @@ -669,6 +670,6 @@ def test_uniform_analytical(u, v, w, direction, tmp_zarrfile):
ds = xr.open_zarr(tmp_zarrfile)
times = (direction * ds["time"][:]).values.astype("timedelta64[s]")[0]
timeref = np.arange(1, 5).astype("timedelta64[s]")
assert np.allclose(times, timeref, atol=np.timedelta64(1, "ms"))
assert_allclose(times, timeref, atol=np.timedelta64(1, "ms"))
lons = ds["lon"][:].values
assert np.allclose(lons, x0 + direction * u * np.arange(1, 5))
assert_allclose(lons, x0 + direction * u * np.arange(1, 5))
21 changes: 11 additions & 10 deletions tests/test_diffusion.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

import numpy as np
import pytest
from numpy.testing import assert_allclose
from scipy import stats

from parcels import (
Expand Down Expand Up @@ -44,10 +45,10 @@ def test_fieldKh_Brownian(mesh):
lons = pset.lon

tol = 500 * mesh_conversion # effectively 500 m errors
assert np.allclose(np.std(lats), expected_std_lat, atol=tol)
assert np.allclose(np.std(lons), expected_std_lon, atol=tol)
assert np.allclose(np.mean(lons), 0, atol=tol)
assert np.allclose(np.mean(lats), 0, atol=tol)
assert_allclose(np.std(lats), expected_std_lat, atol=tol)
assert_allclose(np.std(lons), expected_std_lon, atol=tol)
assert_allclose(np.mean(lons), 0, atol=tol)
assert_allclose(np.mean(lats), 0, atol=tol)


@pytest.mark.parametrize("mesh", ["spherical", "flat"])
Expand Down Expand Up @@ -78,8 +79,8 @@ def test_fieldKh_SpatiallyVaryingDiffusion(mesh, kernel):
lats = pset.lat
lons = pset.lon
tol = 2000 * mesh_conversion # effectively 2000 m errors (because of low numbers of particles)
assert np.allclose(np.mean(lons), 0, atol=tol)
assert np.allclose(np.mean(lats), 0, atol=tol)
assert_allclose(np.mean(lons), 0, atol=tol)
assert_allclose(np.mean(lats), 0, atol=tol)
assert stats.skew(lons) > stats.skew(lats)


Expand All @@ -106,7 +107,7 @@ def vertical_randomexponential(particle, fieldset, time): # pragma: no cover

depth = pset.depth
expected_mean = 1.0 / fieldset.lambd
assert np.allclose(np.mean(depth), expected_mean, rtol=0.1)
assert_allclose(np.mean(depth), expected_mean, rtol=0.1)


@pytest.mark.parametrize("mu", [0.8 * np.pi, np.pi])
Expand Down Expand Up @@ -134,8 +135,8 @@ def vonmises(particle, fieldset, time): # pragma: no cover

angles = np.array([p.angle for p in pset])

assert np.allclose(np.mean(angles), mu, atol=0.1)
assert_allclose(np.mean(angles), mu, atol=0.1)
vonmises_mean = stats.vonmises.mean(kappa=kappa, loc=mu)
assert np.allclose(np.mean(angles), vonmises_mean, atol=0.1)
assert_allclose(np.mean(angles), vonmises_mean, atol=0.1)
vonmises_var = stats.vonmises.var(kappa=kappa, loc=mu)
assert np.allclose(np.var(angles), vonmises_var, atol=0.1)
assert_allclose(np.var(angles), vonmises_var, atol=0.1)
5 changes: 3 additions & 2 deletions tests/test_field.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import numpy as np
import pytest
import xarray as xr
from numpy.testing import assert_allclose

from parcels import Field
from parcels.tools.converters import (
Expand All @@ -22,8 +23,8 @@ def test_field_from_netcdf_variables():
variable = {"U": "vozocrtx"}
f3 = Field.from_netcdf(filename, variable, dims)

assert np.allclose(f1.data, f2.data, atol=1e-12)
assert np.allclose(f1.data, f3.data, atol=1e-12)
assert_allclose(f1.data, f2.data, atol=1e-12)
assert_allclose(f1.data, f3.data, atol=1e-12)

with pytest.raises(AssertionError):
variable = {"U": "vozocrtx", "nav_lat": "nav_lat"} # multiple variables will fail
Expand Down
47 changes: 24 additions & 23 deletions tests/test_fieldset.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import numpy as np
import pytest
import xarray as xr
from numpy.testing import assert_allclose

from parcels import (
AdvectionRK4,
Expand Down Expand Up @@ -89,8 +90,8 @@ def test_fieldset_from_data(xdim, ydim):
assert fieldset.U._creation_log == "from_data"
assert len(fieldset.U.data.shape) == 3
assert len(fieldset.V.data.shape) == 3
assert np.allclose(fieldset.U.data[0, :], data["U"], rtol=1e-12)
assert np.allclose(fieldset.V.data[0, :], data["V"], rtol=1e-12)
assert_allclose(fieldset.U.data[0, :], data["U"], rtol=1e-12)
assert_allclose(fieldset.V.data[0, :], data["V"], rtol=1e-12)


def test_fieldset_extra_syntax():
Expand Down Expand Up @@ -147,9 +148,9 @@ def test_fieldset_from_data_different_dimensions(xdim, ydim):
assert len(fieldset.V.data.shape) == 3
assert len(fieldset.P.data.shape) == 4
assert fieldset.P.data.shape == (tdim, zdim, ydim / 2, xdim / 2)
assert np.allclose(fieldset.U.data, 0.0, rtol=1e-12)
assert np.allclose(fieldset.V.data, 1.0, rtol=1e-12)
assert np.allclose(fieldset.P.data, 2.0, rtol=1e-12)
assert_allclose(fieldset.U.data, 0.0, rtol=1e-12)
assert_allclose(fieldset.V.data, 1.0, rtol=1e-12)
assert_allclose(fieldset.P.data, 2.0, rtol=1e-12)


@pytest.mark.parametrize("xdim", [100, 200])
Expand All @@ -163,8 +164,8 @@ def test_fieldset_from_parcels(xdim, ydim, tmpdir):
fieldset = FieldSet.from_parcels(filepath)
assert len(fieldset.U.data.shape) == 3 # Will be 4 once we use depth
assert len(fieldset.V.data.shape) == 3
assert np.allclose(fieldset.U.data[0, :], data["U"], rtol=1e-12)
assert np.allclose(fieldset.V.data[0, :], data["V"], rtol=1e-12)
assert_allclose(fieldset.U.data[0, :], data["U"], rtol=1e-12)
assert_allclose(fieldset.V.data[0, :], data["V"], rtol=1e-12)


def test_fieldset_from_modulefile():
Expand Down Expand Up @@ -392,7 +393,7 @@ def test_fieldset_write_curvilinear(tmpdir):
assert fieldset2.dx._creation_log == "from_netcdf"

for var in ["lon", "lat", "data"]:
assert np.allclose(getattr(fieldset2.dx, var), getattr(fieldset.dx, var))
assert_allclose(getattr(fieldset2.dx, var), getattr(fieldset.dx, var))


def addConst(particle, fieldset, time): # pragma: no cover
Expand Down Expand Up @@ -490,7 +491,7 @@ def UpdateU(particle, fieldset, time): # pragma: no cover
assert fieldset.U.data[0, 1, 0] == 11

da = xr.open_dataset(str(tmp_zarrfile).replace(".zarr", "_0005U.nc"))
assert np.allclose(fieldset.U.data, da["U"].values, atol=1.0)
assert_allclose(fieldset.U.data, da["U"].values, atol=1.0)


@pytest.mark.v4remove
Expand All @@ -517,12 +518,12 @@ def test_timestamps(datetype, tmpdir):
fieldset3 = FieldSet.from_parcels(tmpdir.join("file*"))
timestamps = [dims1["time"], dims2["time"]]
fieldset4 = FieldSet.from_parcels(tmpdir.join("file*"), timestamps=timestamps)
assert np.allclose(fieldset3.U.grid.time_full, fieldset4.U.grid.time_full)
assert_allclose(fieldset3.U.grid.time_full, fieldset4.U.grid.time_full)

for d in [0, 8, 10, 12]:
fieldset3.computeTimeChunk(d * 86400.0, 1.0)
fieldset4.computeTimeChunk(d * 86400.0, 1.0)
assert np.allclose(fieldset3.U.data, fieldset4.U.data)
assert_allclose(fieldset3.U.data, fieldset4.U.data)


@pytest.mark.v4remove
Expand Down Expand Up @@ -610,10 +611,10 @@ def sampleTemp(particle, fieldset, time): # pragma: no cover
temp_theo = temp_vec[-1]
elif dt_sign == -1:
temp_theo = temp_vec[0]
assert np.allclose(temp_theo, pset.temp[0], atol=1e-5)
assert np.allclose(pset.u1[0], pset.u2[0])
assert np.allclose(pset.v1[0], pset.v2[0])
assert np.allclose(pset.d[0], 1.0)
assert_allclose(temp_theo, pset.temp[0], atol=1e-5)
assert_allclose(pset.u1[0], pset.u2[0])
assert_allclose(pset.v1[0], pset.v2[0])
assert_allclose(pset.d[0], 1.0)


@pytest.mark.parametrize("tdim", [10, None])
Expand Down Expand Up @@ -654,9 +655,9 @@ def generate_dataset(xdim, ydim, zdim=1, tdim=1):

pset.execute(AdvectionRK4, dt=1, runtime=10)
if tdim == 10:
assert np.allclose(pset.lon_nextloop[0], 4.5) and np.allclose(pset.lat_nextloop[0], 10)
assert_allclose(pset.lon_nextloop[0], 4.5) and np.allclose(pset.lat_nextloop[0], 10)
else:
assert np.allclose(pset.lon_nextloop[0], 5.0) and np.allclose(pset.lat_nextloop[0], 10)
assert_allclose(pset.lon_nextloop[0], 5.0) and np.allclose(pset.lat_nextloop[0], 10)


@pytest.mark.v4alpha
Expand Down Expand Up @@ -695,16 +696,16 @@ def test_fieldset_from_data_gridtypes():
plon = pset.lon
plat = pset.lat
# sol of dx/dt = (init_depth+1)*x+0.1; x(0)=0
assert np.allclose(plon, [0.17173462592827032, 0.2177736932123214])
assert np.allclose(plat, [1, 1])
assert_allclose(plon, [0.17173462592827032, 0.2177736932123214])
assert_allclose(plat, [1, 1])

# Rectilinear S grid
dimensions["depth"] = depth_s
fieldset = FieldSet.from_data(data, dimensions, mesh="flat")
pset = ParticleSet(fieldset, Particle, [0, 0], [0, 0], [0, 0.4])
pset.execute(AdvectionRK4, runtime=1.5, dt=0.5)
assert np.allclose(plon, pset.lon)
assert np.allclose(plat, pset.lat)
assert_allclose(plon, pset.lon)
assert_allclose(plat, pset.lat)

# Curvilinear Z grid
dimensions["lon"] = lonm
Expand All @@ -713,8 +714,8 @@ def test_fieldset_from_data_gridtypes():
fieldset = FieldSet.from_data(data, dimensions, mesh="flat")
pset = ParticleSet(fieldset, Particle, [0, 0], [0, 0], [0, 0.4])
pset.execute(AdvectionRK4, runtime=1.5, dt=0.5)
assert np.allclose(plon, pset.lon)
assert np.allclose(plat, pset.lat)
assert_allclose(plon, pset.lon)
assert_allclose(plat, pset.lat)

# Curvilinear S grid
dimensions["depth"] = depth_s
Expand Down
Loading
Loading