linter-related test updates

This commit is contained in:
Jan Petykiewicz 2024-07-29 00:30:00 -07:00
parent 43f038d761
commit e19968bb9f
5 changed files with 66 additions and 64 deletions

View File

@ -3,7 +3,8 @@
Test fixtures
"""
from typing import Iterable, Any
# ruff: noqa: ARG001
from typing import Any
import numpy
from numpy.typing import NDArray
import pytest # type: ignore
@ -20,18 +21,18 @@ FixtureRequest = Any
(5, 5, 5),
# (7, 7, 7),
])
def shape(request: FixtureRequest) -> Iterable[tuple[int, ...]]:
yield (3, *request.param)
def shape(request: FixtureRequest) -> tuple[int, ...]:
return (3, *request.param)
@pytest.fixture(scope='module', params=[1.0, 1.5])
def epsilon_bg(request: FixtureRequest) -> Iterable[float]:
yield request.param
def epsilon_bg(request: FixtureRequest) -> float:
return request.param
@pytest.fixture(scope='module', params=[1.0, 2.5])
def epsilon_fg(request: FixtureRequest) -> Iterable[float]:
yield request.param
def epsilon_fg(request: FixtureRequest) -> float:
return request.param
@pytest.fixture(scope='module', params=['center', '000', 'random'])
@ -40,7 +41,7 @@ def epsilon(
shape: tuple[int, ...],
epsilon_bg: float,
epsilon_fg: float,
) -> Iterable[NDArray[numpy.float64]]:
) -> NDArray[numpy.float64]:
is3d = (numpy.array(shape) == 1).sum() == 0
if is3d:
if request.param == '000':
@ -60,17 +61,17 @@ def epsilon(
high=max(epsilon_bg, epsilon_fg),
size=shape)
yield epsilon
return epsilon
@pytest.fixture(scope='module', params=[1.0]) # 1.5
def j_mag(request: FixtureRequest) -> Iterable[float]:
yield request.param
def j_mag(request: FixtureRequest) -> float:
return request.param
@pytest.fixture(scope='module', params=[1.0, 1.5])
def dx(request: FixtureRequest) -> Iterable[float]:
yield request.param
def dx(request: FixtureRequest) -> float:
return request.param
@pytest.fixture(scope='module', params=['uniform', 'centerbig'])
@ -78,7 +79,7 @@ def dxes(
request: FixtureRequest,
shape: tuple[int, ...],
dx: float,
) -> Iterable[list[list[NDArray[numpy.float64]]]]:
) -> list[list[NDArray[numpy.float64]]]:
if request.param == 'uniform':
dxes = [[numpy.full(s, dx) for s in shape[1:]] for _ in range(2)]
elif request.param == 'centerbig':
@ -90,5 +91,5 @@ def dxes(
dxe = [PRNG.uniform(low=1.0 * dx, high=1.1 * dx, size=s) for s in shape[1:]]
dxh = [(d + numpy.roll(d, -1)) / 2 for d in dxe]
dxes = [dxe, dxh]
yield dxes
return dxes

View File

@ -1,4 +1,4 @@
from typing import Iterable
# ruff: noqa: ARG001
import dataclasses
import pytest # type: ignore
import numpy
@ -61,24 +61,24 @@ def test_poynting_planes(sim: 'FDResult') -> None:
# Also see conftest.py
@pytest.fixture(params=[1 / 1500])
def omega(request: FixtureRequest) -> Iterable[float]:
yield request.param
def omega(request: FixtureRequest) -> float:
return request.param
@pytest.fixture(params=[None])
def pec(request: FixtureRequest) -> Iterable[NDArray[numpy.float64] | None]:
yield request.param
def pec(request: FixtureRequest) -> NDArray[numpy.float64] | None:
return request.param
@pytest.fixture(params=[None])
def pmc(request: FixtureRequest) -> Iterable[NDArray[numpy.float64] | None]:
yield request.param
def pmc(request: FixtureRequest) -> NDArray[numpy.float64] | None:
return request.param
#@pytest.fixture(scope='module',
# params=[(25, 5, 5)])
#def shape(request):
# yield (3, *request.param)
#def shape(request: FixtureRequest):
# return (3, *request.param)
@pytest.fixture(params=['diag']) # 'center'
@ -86,7 +86,7 @@ def j_distribution(
request: FixtureRequest,
shape: tuple[int, ...],
j_mag: float,
) -> Iterable[NDArray[numpy.float64]]:
) -> NDArray[numpy.float64]:
j = numpy.zeros(shape, dtype=complex)
center_mask = numpy.zeros(shape, dtype=bool)
center_mask[:, shape[1] // 2, shape[2] // 2, shape[3] // 2] = True
@ -96,7 +96,7 @@ def j_distribution(
elif request.param == 'diag':
j[numpy.roll(center_mask, [1, 1, 1], axis=(1, 2, 3))] = (1 + 1j) * j_mag
j[numpy.roll(center_mask, [-1, -1, -1], axis=(1, 2, 3))] = (1 - 1j) * j_mag
yield j
return j
@dataclasses.dataclass()

View File

@ -1,4 +1,4 @@
from typing import Iterable
# ruff: noqa: ARG001
import pytest # type: ignore
import numpy
from numpy.typing import NDArray
@ -44,30 +44,30 @@ def test_pml(sim: FDResult, src_polarity: int) -> None:
# Also see conftest.py
@pytest.fixture(params=[1 / 1500])
def omega(request: FixtureRequest) -> Iterable[float]:
yield request.param
def omega(request: FixtureRequest) -> float:
return request.param
@pytest.fixture(params=[None])
def pec(request: FixtureRequest) -> Iterable[NDArray[numpy.float64] | None]:
yield request.param
def pec(request: FixtureRequest) -> NDArray[numpy.float64] | None:
return request.param
@pytest.fixture(params=[None])
def pmc(request: FixtureRequest) -> Iterable[NDArray[numpy.float64] | None]:
yield request.param
def pmc(request: FixtureRequest) -> NDArray[numpy.float64] | None:
return request.param
@pytest.fixture(params=[(30, 1, 1),
(1, 30, 1),
(1, 1, 30)])
def shape(request: FixtureRequest) -> Iterable[tuple[int, ...]]:
yield (3, *request.param)
def shape(request: FixtureRequest) -> tuple[int, int, int]:
return (3, *request.param)
@pytest.fixture(params=[+1, -1])
def src_polarity(request: FixtureRequest) -> Iterable[int]:
yield request.param
def src_polarity(request: FixtureRequest) -> int:
return request.param
@pytest.fixture()
@ -78,7 +78,7 @@ def j_distribution(
dxes: dx_lists_mut,
omega: float,
src_polarity: int,
) -> Iterable[NDArray[numpy.complex128]]:
) -> NDArray[numpy.complex128]:
j = numpy.zeros(shape, dtype=complex)
dim = numpy.where(numpy.array(shape[1:]) > 1)[0][0] # Propagation axis
@ -106,7 +106,7 @@ def j_distribution(
j = fdfd.waveguide_3d.compute_source(E=e, wavenumber=wavenumber_corrected, omega=omega, dxes=dxes,
axis=dim, polarity=src_polarity, slices=slices, epsilon=epsilon)
yield j
return j
@pytest.fixture()
@ -115,9 +115,9 @@ def epsilon(
shape: tuple[int, ...],
epsilon_bg: float,
epsilon_fg: float,
) -> Iterable[NDArray[numpy.float64]]:
) -> NDArray[numpy.float64]:
epsilon = numpy.full(shape, epsilon_fg, dtype=float)
yield epsilon
return epsilon
@pytest.fixture(params=['uniform'])
@ -127,7 +127,7 @@ def dxes(
dx: float,
omega: float,
epsilon_fg: float,
) -> Iterable[list[list[NDArray[numpy.float64]]]]:
) -> list[list[NDArray[numpy.float64]]]:
if request.param == 'uniform':
dxes = [[numpy.full(s, dx) for s in shape[1:]] for _ in range(2)]
dim = numpy.where(numpy.array(shape[1:]) > 1)[0][0] # Propagation axis
@ -141,7 +141,7 @@ def dxes(
epsilon_effective=epsilon_fg,
thickness=10,
)
yield dxes
return dxes
@pytest.fixture()

View File

@ -1,4 +1,5 @@
from typing import Iterable, Any
# ruff: noqa: ARG001
from typing import Any
import dataclasses
import pytest # type: ignore
import numpy
@ -150,8 +151,8 @@ def test_poynting_planes(sim: 'TDResult') -> None:
@pytest.fixture(params=[0.3])
def dt(request: FixtureRequest) -> Iterable[float]:
yield request.param
def dt(request: FixtureRequest) -> float:
return request.param
@dataclasses.dataclass()
@ -168,8 +169,8 @@ class TDResult:
@pytest.fixture(params=[(0, 4, 8)]) # (0,)
def j_steps(request: FixtureRequest) -> Iterable[tuple[int, ...]]:
yield request.param
def j_steps(request: FixtureRequest) -> tuple[int, ...]:
return request.param
@pytest.fixture(params=['center', 'random'])
@ -177,7 +178,7 @@ def j_distribution(
request: FixtureRequest,
shape: tuple[int, ...],
j_mag: float,
) -> Iterable[NDArray[numpy.float64]]:
) -> NDArray[numpy.float64]:
j = numpy.zeros(shape)
if request.param == 'center':
j[:, shape[1] // 2, shape[2] // 2, shape[3] // 2] = j_mag
@ -185,7 +186,7 @@ def j_distribution(
j[:, 0, 0, 0] = j_mag
elif request.param == 'random':
j[:] = PRNG.uniform(low=-j_mag, high=j_mag, size=shape)
yield j
return j
@pytest.fixture()
@ -199,8 +200,7 @@ def sim(
j_steps: tuple[int, ...],
) -> TDResult:
is3d = (numpy.array(shape) == 1).sum() == 0
if is3d:
if dt != 0.3:
if is3d and dt != 0.3:
pytest.skip('Skipping dt != 0.3 because test is 3D (for speed)')
sim = TDResult(

View File

@ -1,5 +1,3 @@
from typing import Any
import numpy
from numpy.typing import NDArray
@ -10,22 +8,25 @@ PRNG = numpy.random.RandomState(12345)
def assert_fields_close(
x: NDArray,
y: NDArray,
*args: Any,
**kwargs: Any,
) -> None:
numpy.testing.assert_allclose(
x, y, verbose=False, # type: ignore
err_msg='Fields did not match:\n{}\n{}'.format(numpy.moveaxis(x, -1, 0),
numpy.moveaxis(y, -1, 0)),
*args,
**kwargs,
) -> None:
x_disp = numpy.moveaxis(x, -1, 0)
y_disp = numpy.moveaxis(y, -1, 0)
numpy.testing.assert_allclose(
x, # type: ignore
y, # type: ignore
*args,
verbose=False,
err_msg=f'Fields did not match:\n{x_disp}\n{y_disp}',
**kwargs,
)
def assert_close(
x: NDArray,
y: NDArray,
*args: Any,
**kwargs: Any,
*args,
**kwargs,
) -> None:
numpy.testing.assert_allclose(x, y, *args, **kwargs)