From 95e3f71b40494e7134f6425104c421ab5e6911b1 Mon Sep 17 00:00:00 2001 From: Jan Petykiewicz Date: Wed, 17 Jul 2024 23:15:34 -0700 Subject: [PATCH] use f-strings in place of .format() --- examples/fdtd.py | 3 ++- meanas/fdfd/bloch.py | 2 +- meanas/fdfd/solvers.py | 3 ++- meanas/fdfd/waveguide_2d.py | 2 +- meanas/fdmath/operators.py | 13 ++++++------- meanas/fdtd/boundaries.py | 4 ++-- meanas/fdtd/pml.py | 4 ++-- meanas/test/test_fdtd.py | 2 +- 8 files changed, 17 insertions(+), 16 deletions(-) diff --git a/examples/fdtd.py b/examples/fdtd.py index 8dc0d98..8378b34 100644 --- a/examples/fdtd.py +++ b/examples/fdtd.py @@ -157,7 +157,8 @@ def main(): e[1][tuple(grid.shape//2)] += field_source(t) update_H(e, h) - print('iteration {}: average {} iterations per sec'.format(t, (t+1)/(time.perf_counter()-start))) + avg_rate = (t + 1)/(time.perf_counter() - start)) + print(f'iteration {t}: average {avg_rate} iterations per sec') sys.stdout.flush() if t % 20 == 0: diff --git a/meanas/fdfd/bloch.py b/meanas/fdfd/bloch.py index 800a603..55abbee 100644 --- a/meanas/fdfd/bloch.py +++ b/meanas/fdfd/bloch.py @@ -781,7 +781,7 @@ def linmin(x_guess, f0, df0, x_max, f_tol=0.1, df_tol=min(tolerance, 1e-6), x_to x_min, x_max, isave, dsave) for i in range(int(1e6)): if task != 'F': - logging.info('search converged in {} iterations'.format(i)) + logging.info(f'search converged in {i} iterations') break fx = f(x, dfx) x, fx, dfx, task = minpack2.dsrch(x, fx, dfx, f_tol, df_tol, x_tol, task, diff --git a/meanas/fdfd/solvers.py b/meanas/fdfd/solvers.py index 19cb418..8ac157c 100644 --- a/meanas/fdfd/solvers.py +++ b/meanas/fdfd/solvers.py @@ -43,7 +43,8 @@ def _scipy_qmr( nonlocal ii ii += 1 if ii % 100 == 0: - logger.info('Solver residual at iteration {} : {}'.format(ii, norm(A @ xk - b))) + cur_norm = norm(A @ xk - b) + logger.info(f'Solver residual at iteration {ii} : {cur_norm}') if 'callback' in kwargs: def augmented_callback(xk: ArrayLike) -> None: diff --git a/meanas/fdfd/waveguide_2d.py b/meanas/fdfd/waveguide_2d.py index cfda1af..df4df73 100644 --- a/meanas/fdfd/waveguide_2d.py +++ b/meanas/fdfd/waveguide_2d.py @@ -420,7 +420,7 @@ def _normalized_fields( Sz_a = E[0] * numpy.conj(H[1] * phase) * dxes_real[0][1] * dxes_real[1][0] Sz_b = E[1] * numpy.conj(H[0] * phase) * dxes_real[0][0] * dxes_real[1][1] Sz_tavg = numpy.real(Sz_a.sum() - Sz_b.sum()) * 0.5 # 0.5 since E, H are assumed to be peak (not RMS) amplitudes - assert Sz_tavg > 0, 'Found a mode propagating in the wrong direction! Sz_tavg={}'.format(Sz_tavg) + assert Sz_tavg > 0, f'Found a mode propagating in the wrong direction! {Sz_tavg=}' energy = epsilon * e.conj() * e diff --git a/meanas/fdmath/operators.py b/meanas/fdmath/operators.py index 95101c5..9d5988d 100644 --- a/meanas/fdmath/operators.py +++ b/meanas/fdmath/operators.py @@ -29,9 +29,9 @@ def shift_circ( Sparse matrix for performing the circular shift. """ if len(shape) not in (2, 3): - raise Exception('Invalid shape: {}'.format(shape)) + raise Exception(f'Invalid shape: {shape}') if axis not in range(len(shape)): - raise Exception('Invalid direction: {}, shape is {}'.format(axis, shape)) + raise Exception(f'Invalid direction: {axis}, shape is {shape}') shifts = [abs(shift_distance) if a == axis else 0 for a in range(3)] shifted_diags = [(numpy.arange(n) + s) % n for n, s in zip(shape, shifts)] @@ -69,12 +69,11 @@ def shift_with_mirror( Sparse matrix for performing the shift-with-mirror. """ if len(shape) not in (2, 3): - raise Exception('Invalid shape: {}'.format(shape)) + raise Exception(f'Invalid shape: {shape}') if axis not in range(len(shape)): - raise Exception('Invalid direction: {}, shape is {}'.format(axis, shape)) + raise Exception(f'Invalid direction: {axis}, shape is {shape}') if shift_distance >= shape[axis]: - raise Exception('Shift ({}) is too large for axis {} of size {}'.format( - shift_distance, axis, shape[axis])) + raise Exception(f'Shift ({shift_distance}) is too large for axis {axis} of size {shape[axis]}') def mirrored_range(n: int, s: int) -> NDArray[numpy.int_]: v = numpy.arange(n) + s @@ -198,7 +197,7 @@ def avg_forward(axis: int, shape: Sequence[int]) -> sparse.spmatrix: Sparse matrix for forward average operation. """ if len(shape) not in (2, 3): - raise Exception('Invalid shape: {}'.format(shape)) + raise Exception(f'Invalid shape: {shape}') n = numpy.prod(shape) return 0.5 * (sparse.eye(n) + shift_circ(axis, shape)) diff --git a/meanas/fdtd/boundaries.py b/meanas/fdtd/boundaries.py index 652d957..e82deef 100644 --- a/meanas/fdtd/boundaries.py +++ b/meanas/fdtd/boundaries.py @@ -15,7 +15,7 @@ def conducting_boundary( ) -> tuple[fdfield_updater_t, fdfield_updater_t]: dirs = [0, 1, 2] if direction not in dirs: - raise Exception('Invalid direction: {}'.format(direction)) + raise Exception(f'Invalid direction: {direction}') dirs.remove(direction) u, v = dirs @@ -64,4 +64,4 @@ def conducting_boundary( return ep, hp - raise Exception('Bad polarity: {}'.format(polarity)) + raise Exception(f'Bad polarity: {polarity}') diff --git a/meanas/fdtd/pml.py b/meanas/fdtd/pml.py index b11b3b5..65d71e6 100644 --- a/meanas/fdtd/pml.py +++ b/meanas/fdtd/pml.py @@ -33,10 +33,10 @@ def cpml_params( ) -> dict[str, Any]: if axis not in range(3): - raise Exception('Invalid axis: {}'.format(axis)) + raise Exception(f'Invalid axis: {axis}') if polarity not in (-1, 1): - raise Exception('Invalid polarity: {}'.format(polarity)) + raise Exception(f'Invalid polarity: {polarity}') if thickness <= 2: raise Exception('It would be wise to have a pml with 4+ cells of thickness') diff --git a/meanas/test/test_fdtd.py b/meanas/test/test_fdtd.py index 701275e..0a92c73 100644 --- a/meanas/test/test_fdtd.py +++ b/meanas/test/test_fdtd.py @@ -101,7 +101,7 @@ def test_poynting_divergence(sim: 'TDResult') -> None: def test_poynting_planes(sim: 'TDResult') -> None: mask = (sim.js[0] != 0).any(axis=0) if mask.sum() > 1: - pytest.skip('test_poynting_planes can only test single point sources, got {}'.format(mask.sum())) + pytest.skip(f'test_poynting_planes can only test single point sources, got {mask.sum()}') args: dict[str, Any] = { 'dxes': sim.dxes,