def test_type_errors(): """Modifier return values should be arrays and satisfy a few dtype criteria""" build_model(pb.onsite_energy_modifier(lambda energy: energy + 1)) with pytest.raises(TypeError) as excinfo: build_model(pb.onsite_energy_modifier(lambda: 1)) assert "Modifiers must return ndarray(s)" in str(excinfo.value) build_model(pb.site_position_modifier(lambda x, y, z: (x, y, z))) with pytest.raises(TypeError) as excinfo: build_model(pb.site_position_modifier(lambda x, y, z: (x, y))) assert "expected to return 3 ndarray(s), but got 2" in str(excinfo.value) with pytest.raises(TypeError) as excinfo: build_model(pb.onsite_energy_modifier(lambda: (one, one))) assert "expected to return 1 ndarray(s), but got 2" in str(excinfo.value) with pytest.raises(TypeError) as excinfo: build_model(pb.onsite_energy_modifier(lambda x: np.zeros(x.size // 2))) assert "must return the same size" in str(excinfo.value) def complex_ones(energy): return np.ones_like(energy, dtype=np.complex128) build_model(pb.hopping_energy_modifier(complex_ones)) build_model(pb.onsite_energy_modifier(complex_ones)) with pytest.raises(TypeError) as excinfo: build_model( pb.site_position_modifier( lambda x: (np.ones_like(x, dtype=np.complex128), ) * 3)) assert "'complex128', but expected same kind as 'float32'" in str( excinfo.value)
def test_modifier_function_signature(): pb.onsite_energy_modifier(lambda energy: energy) with pytest.raises(RuntimeError) as excinfo: pb.onsite_energy_modifier(lambda this_is_unexpected: None) assert "Unexpected argument" in str(excinfo.value) with pytest.raises(RuntimeError) as excinfo: pb.onsite_energy_modifier(lambda energy, x, y, z, w: None) assert "Unexpected argument" in str(excinfo.value)
def test_decorator(): pb.onsite_energy_modifier(lambda energy: energy) with pytest.raises(RuntimeError) as excinfo: pb.onsite_energy_modifier(lambda this_is_unexpected: None) assert "Unexpected argument" in str(excinfo.value) with pytest.raises(RuntimeError) as excinfo: pb.onsite_energy_modifier(lambda energy, x, y, z, w: None) assert "Unexpected argument" in str(excinfo.value) pb.onsite_energy_modifier(lambda energy: energy + 1) with pytest.raises(RuntimeError) as excinfo: pb.onsite_energy_modifier(lambda: 1) assert "Modifier must return numpy.ndarray" in str(excinfo.value) pb.site_position_modifier(lambda x, y, z: (x, y, z)) with pytest.raises(RuntimeError) as excinfo: pb.site_position_modifier(lambda x, y, z: (x, y)) assert "expected to return 3 ndarray(s), but got 2" in str(excinfo.value) with pytest.raises(RuntimeError) as excinfo: pb.onsite_energy_modifier(lambda: (one, one)) assert "expected to return 1 ndarray(s), but got 2" in str(excinfo.value) with pytest.raises(RuntimeError) as excinfo: pb.onsite_energy_modifier(lambda x: np.zeros(x.size // 2)) assert "must return the same shape" in str(excinfo.value) pb.hopping_energy_modifier(lambda energy: np.ones_like(energy, dtype=np.complex128)) with pytest.raises(RuntimeError) as excinfo: pb.onsite_energy_modifier(lambda energy: np.ones_like(energy, dtype=np.complex128)) assert "must not return complex" in str(excinfo.value) @pb.onsite_energy_modifier def modifier_return_checker_should_ignore_invalid_sublattices(energy, sub_id): energy[sub_id == 'invalid'] = 0 return energy