Commit c0db02da authored by Joseph Weston's avatar Joseph Weston

replace usages of 'args' with 'params' in tests

Some tests are explicitly checking the use of 'args'
against the use of 'params', so these we leave in and
instead suppress the warnings.
parent 0cc83115
......@@ -114,5 +114,8 @@ def test_wavefunc_ldos_consistency():
_test_sparse.test_wavefunc_ldos_consistency(wave_function, ldos)
# We need to keep testing 'args', but we don't want to see
# all the deprecation warnings in the test logs
@pytest.mark.filterwarnings("ignore:.*'args' parameter")
def test_arg_passing():
_test_sparse.test_arg_passing(wave_function, ldos, smatrix)
......@@ -6,6 +6,8 @@
# the file AUTHORS.rst at the top-level directory of this distribution and at
# http://kwant-project.org/authors.
import pytest
from kwant.solvers.sparse import smatrix, greens_function, ldos, wave_function
from . import _test_sparse
......@@ -60,5 +62,9 @@ def test_ldos():
def test_wavefunc_ldos_consistency():
_test_sparse.test_wavefunc_ldos_consistency(wave_function, ldos)
# We need to keep testing 'args', but we don't want to see
# all the deprecation warnings in the test logs
@pytest.mark.filterwarnings("ignore:.*'args' parameter")
def test_arg_passing():
_test_sparse.test_arg_passing(wave_function, ldos, smatrix)
......@@ -14,6 +14,7 @@ from random import Random
import numpy as np
import tinyarray as ta
import pytest
from pytest import raises, warns
from numpy.testing import assert_almost_equal
......@@ -1145,12 +1146,14 @@ def test_discrete_symmetries():
syst[lat(1)] = np.identity(2)
syst[lat2(1)] = 1
sym = syst.finalized().discrete_symmetry(args=[0])
params=dict(p=0)
sym = syst.finalized().discrete_symmetry(params=params)
for proj, should_be in zip(sym.projectors, np.identity(3)):
assert np.allclose(proj.toarray(), should_be.reshape((3, 1)))
assert np.allclose(sym.time_reversal.toarray(), np.identity(3))
syst.conservation_law = lambda site, p: cons_law[site.family]
sym = syst.finalized().discrete_symmetry(args=[0])
sym = syst.finalized().discrete_symmetry(params=params)
for proj, should_be in zip(sym.projectors, np.identity(3)):
assert np.allclose(proj.toarray(), should_be.reshape((-1, 1)))
......@@ -1186,8 +1189,10 @@ def test_discrete_symmetries():
assert np.allclose(proj.toarray(), [[1]])
# We need to keep testing 'args', but we don't want to see
# all the deprecation warnings in the test logs
@pytest.mark.filterwarnings("ignore:.*'args' parameter")
def test_argument_passing():
chain = kwant.lattice.chain()
# Test for passing parameters to hamiltonian matrix elements
......
......@@ -48,8 +48,8 @@ def test_qhe(W=16, L=8):
((3.2, 3.7), 2, 1e-3),
((5.2, 5.5), 3, 1e-1)]:
for r_phi in r_phis:
args = (1.0 / r_phi, "")
pc = syst.precalculate(1.0, args, what='all')
for result in [kwant.smatrix(pc, 1, args),
kwant.solvers.default.greens_function(pc, 1, args)]:
params = dict(phi=1.0 / r_phi, salt="")
pc = syst.precalculate(1.0, params=params, what='all')
for result in [kwant.smatrix(pc, 1, params=params),
kwant.solvers.default.greens_function(pc, 1, params=params)]:
assert abs(T_nominal - result.transmission(1, 0)) < max_err
......@@ -156,22 +156,22 @@ def test_operator_construction():
A(fsyst, sum=True).sum == True
def _test(A, bra, ket=None, per_el_val=None, reduced_val=None, args=()):
def _test(A, bra, ket=None, per_el_val=None, reduced_val=None, params=None):
if per_el_val is not None:
val = A(bra, ket, args=args)
val = A(bra, ket, params=params)
assert np.allclose(val, per_el_val)
# with bound args
val = A.bind(args)(bra, ket)
val = A.bind(params=params)(bra, ket)
assert np.allclose(val, per_el_val)
# test that inner products give the same thing
ket = bra if ket is None else ket
act_val = np.dot(bra.conj(), A.act(ket, args=args))
inner_val = np.sum(A(bra, ket, args=args))
act_val = np.dot(bra.conj(), A.act(ket, params=params))
inner_val = np.sum(A(bra, ket, params=params))
# check also when sum is done internally by operator
try:
sum_reset = A.sum
A.sum = True
sum_inner_val = A(bra, ket, args=args)
sum_inner_val = A(bra, ket, params=params)
assert inner_val == sum_inner_val
finally:
A.sum = sum_reset
......@@ -300,22 +300,22 @@ def test_opservables_spin():
syst.attach_lead(lead)
syst.attach_lead(lead.reversed())
fsyst = syst.finalized()
args = (0.1,)
down, up = kwant.wave_function(fsyst, energy=1., args=args)(0)
params = dict(B=0.1)
down, up = kwant.wave_function(fsyst, energy=1., params=params)(0)
x_hoppings = kwant.builder.HoppingKind((1,), lat)
spin_current_z = ops.Current(fsyst, sigmaz, where=x_hoppings(syst))
_test(spin_current_z, up, args=args, per_el_val=1)
_test(spin_current_z, down, args=args, per_el_val=-1)
_test(spin_current_z, up, params=params, per_el_val=1)
_test(spin_current_z, down, params=params, per_el_val=-1)
# calculate spin_x torque
spin_torque_x = ops.Source(fsyst, sigmax, where=[lat(L//2)])
i = fsyst.id_by_site[lat(L//2)]
psi = up[2*i:2*(i+1)] + down[2*i:2*(i+1)]
H_ii = onsite(None, *args)
H_ii = onsite(None, **params)
K = np.dot(H_ii, sigmax) - np.dot(sigmax, H_ii)
expect = 1j * ft.reduce(np.dot, (psi.conj(), K, psi))
_test(spin_torque_x, up+down, args=args, reduced_val=expect)
_test(spin_torque_x, up+down, params=params, reduced_val=expect)
def test_opservables_gauged():
......@@ -397,17 +397,20 @@ def test_tocoo():
# No accidental transpose.
syst = kwant.Builder()
lat2 = kwant.lattice.chain(norbs=2)
syst[lat2(0)] = lambda site, paramerer: np.eye(2)
syst[lat2(0)] = lambda site, p: np.eye(2)
syst = syst.finalized()
op = ops.Density(syst, [[1, 1], [0, 1]], check_hermiticity=False)
assert np.all(op.tocoo().toarray() == [[1, 1], [0, 1]])
op = ops.Density(syst, lambda site, p: [[1, 1], [0, 1]],
check_hermiticity=False)
op = op.bind(args=(1,))
op = op.bind(params=dict(p=1))
raises(ValueError, op.tocoo, [1])
# We need to keep testing 'args', but we don't want to see
# all the deprecation warnings in the test logs
@pytest.mark.filterwarnings("ignore:.*'args' parameter")
@pytest.mark.parametrize("A", opservables)
def test_arg_passing(A):
lat1 = kwant.lattice.chain(norbs=1)
......
......@@ -41,12 +41,13 @@ def test_consistence_with_bands(kx=1.9, nkys=31):
wa_keep_1 = wraparound(syst, keep=1).finalized()
wa_keep_none = wraparound(syst).finalized()
bands = kwant.physics.Bands(wa_keep_1, (kx,))
bands = kwant.physics.Bands(wa_keep_1, params=dict(k_x=kx))
energies_a = [bands(ky) for ky in kys]
energies_b = []
for ky in kys:
H = wa_keep_none.hamiltonian_submatrix((kx, ky), sparse=False)
params = dict(k_x=kx, k_y=ky)
H = wa_keep_none.hamiltonian_submatrix(params=params, sparse=False)
evs = np.sort(np.linalg.eigvalsh(H).real)
energies_b.append(evs)
......@@ -63,10 +64,14 @@ def test_opposite_hoppings():
syst[lat(-1, 0), lat(-1, -1)] = val
fsyst = wraparound(syst).finalized()
np.testing.assert_almost_equal(fsyst.hamiltonian_submatrix([0]), 0)
params = dict(k_x=0)
np.testing.assert_almost_equal(
fsyst.hamiltonian_submatrix(params=params),
0)
def test_value_types(k=(-1.1, 0.5), E=2, t=1):
k = dict(zip(('k_x', 'k_y', 'k_z'), k))
sym_extents = [1, 2, 3]
lattices = [kwant.lattice.honeycomb(), kwant.lattice.square()]
lat_syms = [
......@@ -75,7 +80,7 @@ def test_value_types(k=(-1.1, 0.5), E=2, t=1):
]
for lat, sym in lat_syms:
syst = wraparound(_simple_syst(lat, E, t, sym)).finalized()
H = syst.hamiltonian_submatrix(k, sparse=False)
H = syst.hamiltonian_submatrix(params=k, sparse=False)
for E1, t1 in [(float(E), float(t)),
(np.array([[E]], float), np.array([[t]], float)),
(ta.array([[E]], float), ta.array([[t]], float))]:
......@@ -84,7 +89,7 @@ def test_value_types(k=(-1.1, 0.5), E=2, t=1):
for E2 in [E1, lambda a: E1]:
for t2 in [t1, lambda a, b: t1]:
syst = wraparound(_simple_syst(lat, E2, t2, sym)).finalized()
H_alt = syst.hamiltonian_submatrix(k, sparse=False)
H_alt = syst.hamiltonian_submatrix(params=k, sparse=False)
np.testing.assert_equal(H_alt, H)
# test when Hamiltonian value functions take extra parameters and
# have incompatible signatures (must be passed with 'params')
......@@ -98,7 +103,7 @@ def test_value_types(k=(-1.1, 0.5), E=2, t=1):
lambda a, b, t, E: t,
lambda a, b, E, t: t,
]
params = dict(E=E1, t=t1, k_x=k[0], k_y=k[1])
params = dict(E=E1, t=t1, **k)
for E2, t2 in itertools.product(onsites, hoppings):
syst = wraparound(_simple_syst(lat, E2, t2, sym)).finalized()
H_alt = syst.hamiltonian_submatrix(params=params, sparse=False)
......@@ -275,15 +280,17 @@ def test_fd_mismatch():
def spectrum(syst, keep):
syst = wraparound(syst, keep=keep).finalized()
ks = ('k_x', 'k_y', 'k_z')
if keep is None:
def _(*args):
return np.linalg.eigvalsh(syst.hamiltonian_submatrix(args=args))
params = dict(zip(ks, args))
return np.linalg.eigvalsh(
syst.hamiltonian_submatrix(params=params))
else:
def _(*args):
args = list(args)
kext = args.pop(keep)
kint = args
B = kwant.physics.Bands(syst, args=kint)
params = dict(zip(ks, args))
kext = params.pop(ks[keep])
B = kwant.physics.Bands(syst, params=params)
return B(kext)
return _
......@@ -348,7 +355,8 @@ def test_fd_mismatch():
finitewrapped.fill(wrapped, shape, start=np.zeros(3));
sysf = finitewrapped.finalized()
spectrum1 = [np.linalg.eigvalsh(sysf.hamiltonian_submatrix(args=(k, 0)))
spectrum1 = [np.linalg.eigvalsh(
sysf.hamiltonian_submatrix(params=dict(k_x=k, k_y=0)))
for k in np.linspace(-np.pi, np.pi, 5)]
# Second choice: doubled UC with third translation purely in z direction
......@@ -367,7 +375,8 @@ def test_fd_mismatch():
finitewrapped.fill(wrapped, shape, start=np.zeros(3));
sysf = finitewrapped.finalized()
spectrum2 = [np.linalg.eigvalsh(sysf.hamiltonian_submatrix(args=(k, 0)))
spectrum2 = [np.linalg.eigvalsh(
sysf.hamiltonian_submatrix(params=dict(k_x=k, k_y=0)))
for k in np.linspace(-np.pi, np.pi, 5)]
assert np.allclose(spectrum1, spectrum2)
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment