Skip to content

Commit

Permalink
refactor np.isclose(...).all() -> np.allclose(...)
Browse files Browse the repository at this point in the history
  • Loading branch information
wolearyc committed Sep 26, 2024
1 parent b7b08dd commit 4ac7389
Show file tree
Hide file tree
Showing 13 changed files with 47 additions and 47 deletions.
2 changes: 1 addition & 1 deletion ramannoodle/io/io_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ def _read_polarizability_dataset(
read_structure_and_polarizability_fn(filepath)
)
if file_index != 0:
if not np.isclose(lattice, read_lattice, atol=1e-5).all():
if not np.allclose(lattice, read_lattice, atol=1e-5):
raise IncompatibleStructureException(
f"incompatible lattice: {filepath}"
)
Expand Down
4 changes: 2 additions & 2 deletions ramannoodle/polarizability/art.py
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ def add_art_from_files(
_displacement = displacements[0].copy()
atom_index = int(np.argmax(np.sum(_displacement**2, axis=1)))
_displacement[atom_index] = np.zeros(3)
if not np.isclose(_displacement, 0.0, atol=1e-6).all():
if not np.allclose(_displacement, 0.0, atol=1e-6):
raise InvalidDOFException("multiple atoms displaced simultaneously")

# Checks displacement
Expand Down Expand Up @@ -288,7 +288,7 @@ def get_dof_indexes(
for atom_index in atom_indexes:
for index, basis_vector in enumerate(self._cart_basis_vectors):
direction = basis_vector[atom_index]
if not np.isclose(direction, 0, atol=1e-5).all():
if not np.allclose(direction, 0, atol=1e-5):
dof_indexes.append(index)
return dof_indexes

Expand Down
6 changes: 3 additions & 3 deletions ramannoodle/polarizability/interpolation.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ def find_duplicates(vectors: Iterable[ArrayLike]) -> NDArray | None:
raise get_type_error("vectors", vectors, "Iterable") from exc
try:
for vector_1, vector_2 in combinations:
if np.isclose(vector_1, vector_2).all():
if np.allclose(vector_1, vector_2):
return np.array(vector_1)
return None
except TypeError as exc:
Expand Down Expand Up @@ -368,12 +368,12 @@ def _construct_and_add_interpolations(
# Warn user if amplitudes don't span zero
max_amplitude = np.max(interpolation_x)
min_amplitude = np.min(interpolation_x)
if np.isclose(max_amplitude, 0, atol=1e-3).all() or max_amplitude <= 0:
if np.allclose(max_amplitude, 0, atol=1e-3) or max_amplitude <= 0:
warn(
"max amplitude <= 0, when usually it should be > 0",
DOFWarning,
)
if np.isclose(min_amplitude, 0, atol=1e-3).all() or min_amplitude >= 0:
if np.allclose(min_amplitude, 0, atol=1e-3) or min_amplitude >= 0:
warn(
"min amplitude >= 0, when usually it should be < 0",
DOFWarning,
Expand Down
4 changes: 2 additions & 2 deletions ramannoodle/structure/symmetry_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ def are_collinear(vector_1: NDArray[np.float64], vector_2: NDArray[np.float64])
raise ValueError(
f"vector_1 and vector_2 have different lengths: {length_expr}"
) from exc
return bool(np.isclose(dot_product, 1).all() or np.isclose(dot_product, -1).all())
return bool(np.allclose(dot_product, 1) or np.isclose(dot_product, -1))


def is_orthogonal_to_all(
Expand Down Expand Up @@ -69,7 +69,7 @@ def is_orthogonal_to_all(
except TypeError as exc:
raise get_type_error(f"vectors[{index}]", vector_2, "ndarray") from exc

if not np.isclose(np.dot(vector_1.flatten(), vector_2.flatten()) + 1, 1).all():
if not np.allclose(np.dot(vector_1.flatten(), vector_2.flatten()) + 1, 1):
return index

return -1
Expand Down
8 changes: 4 additions & 4 deletions test/tests/test_outcar.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,15 +46,15 @@ def test_read_phonons_from_outcar(

known_degrees_of_freedom = known_num_atoms * 3
assert phonons.wavenumbers.shape == (known_degrees_of_freedom,)
assert np.isclose(phonons.wavenumbers[0:4], known_wavenumbers).all()
assert np.allclose(phonons.wavenumbers[0:4], known_wavenumbers)
assert phonons.displacements.shape == (
known_degrees_of_freedom,
known_num_atoms,
3,
)
assert np.isclose(phonons.displacements[0, 0], known_first_displacement).all()
assert np.allclose(phonons.displacements[0, 0], known_first_displacement)
print(phonons.displacements[-1, -1])
assert np.isclose(phonons.displacements[-1, -1], known_last_displacement).all()
assert np.allclose(phonons.displacements[-1, -1], known_last_displacement)


@pytest.mark.parametrize(
Expand Down Expand Up @@ -91,4 +91,4 @@ def test_read_trajectory_from_outcar(
"""Test read_trajectory for outcar (normal)."""
trajectory = generic_io.read_trajectory(path_fixture, file_format="outcar")
assert len(trajectory) == trajectory_length
assert np.isclose(last_position, trajectory[-1][-1]).all()
assert np.allclose(last_position, trajectory[-1][-1])
12 changes: 6 additions & 6 deletions test/tests/test_outcar_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,8 +122,8 @@ def test_read_cart_positions_from_outcar(
cart_positions = vasp_outcar._read_cart_positions(file_fixture, 135)

assert len(cart_positions) == 135
assert np.isclose(cart_positions[0], known_first_position).all()
assert np.isclose(cart_positions[-1], known_last_position).all()
assert np.allclose(cart_positions[0], known_first_position)
assert np.allclose(cart_positions[-1], known_last_position)


@pytest.mark.parametrize(
Expand Down Expand Up @@ -173,8 +173,8 @@ def test_read_positions_from_outcar(
positions = vasp_outcar._read_positions(file_fixture, 135)

assert len(positions) == 135
assert np.isclose(positions[0], known_first_position).all()
assert np.isclose(positions[-1], known_last_position).all()
assert np.allclose(positions[0], known_first_position)
assert np.allclose(positions[-1], known_last_position)


@pytest.mark.parametrize(
Expand Down Expand Up @@ -227,7 +227,7 @@ def test_read_polarizability_from_outcar(
"""Test _read_polarizability_from_outcar (normal)."""
polarizability = vasp_outcar._read_polarizability(file_fixture)

assert np.isclose(polarizability, known_polarizability).all()
assert np.allclose(polarizability, known_polarizability)


@pytest.mark.parametrize(
Expand Down Expand Up @@ -288,7 +288,7 @@ def test_read_lattice_from_outcar(
) -> None:
"""Test _read_lattice_from_outcar (normal)."""
result = vasp_outcar._read_lattice(file_fixture)
assert np.isclose(result, known_lattice).all()
assert np.allclose(result, known_lattice)


@pytest.mark.parametrize(
Expand Down
22 changes: 11 additions & 11 deletions test/tests/test_phonon_spectrum.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ def _validate_polarizabilities(model: InterpolationModel, data_directory: str) -
)
)
model_polarizability = model.calc_polarizabilities(np.array([positions]))[0]
assert np.isclose(model_polarizability, known_polarizability, atol=1e-4).all()
assert np.allclose(model_polarizability, known_polarizability, atol=1e-4)


@pytest.mark.parametrize(
Expand Down Expand Up @@ -100,8 +100,8 @@ def test_interpolation_spectrum(
known_wavenumbers = known_spectrum["wavenumbers"]
known_intensities = known_spectrum["intensities"]

assert np.isclose(wavenumbers, known_wavenumbers).all()
assert np.isclose(intensities, known_intensities, atol=1e-4).all()
assert np.allclose(wavenumbers, known_wavenumbers)
assert np.allclose(intensities, known_intensities, atol=1e-4)


@pytest.mark.parametrize(
Expand Down Expand Up @@ -157,8 +157,8 @@ def test_art_spectrum(
known_wavenumbers = known_spectrum["wavenumbers"]
known_intensities = known_spectrum["intensities"]

assert np.isclose(wavenumbers, known_wavenumbers).all()
assert np.isclose(intensities, known_intensities, atol=1e-4).all()
assert np.allclose(wavenumbers, known_wavenumbers)
assert np.allclose(intensities, known_intensities, atol=1e-4)


@pytest.mark.parametrize(
Expand Down Expand Up @@ -232,8 +232,8 @@ def test_art_masked_spectrum(
known_wavenumbers = known_spectrum["wavenumbers"]
known_intensities = known_spectrum["intensities"]

assert np.isclose(wavenumbers, known_wavenumbers).all()
assert np.isclose(intensities, known_intensities, atol=1e-4).all()
assert np.allclose(wavenumbers, known_wavenumbers)
assert np.allclose(intensities, known_intensities, atol=1e-4)


@pytest.mark.parametrize(
Expand Down Expand Up @@ -267,8 +267,8 @@ def test_convolve_intensities(
with np.load(known_gaussian_spectrum_path) as known_spectrum:
known_wavenumbers = known_spectrum["wavenumbers"]
known_intensities = known_spectrum["intensities"]
assert np.isclose(gaussian_wavenumbers, known_wavenumbers).all()
assert np.isclose(gaussian_intensities, known_intensities).all()
assert np.allclose(gaussian_wavenumbers, known_wavenumbers)
assert np.allclose(gaussian_intensities, known_intensities)

lorentzian_wavenumbers, lorentzian_intensities = convolve_spectrum(
wavenumbers, intensities, "lorentzian"
Expand All @@ -281,8 +281,8 @@ def test_convolve_intensities(
with np.load(known_lorentzian_spectrum_path) as known_spectrum:
known_wavenumbers = known_spectrum["wavenumbers"]
known_intensities = known_spectrum["intensities"]
assert np.isclose(lorentzian_wavenumbers, known_wavenumbers).all()
assert np.isclose(lorentzian_intensities, known_intensities).all()
assert np.allclose(lorentzian_wavenumbers, known_wavenumbers)
assert np.allclose(lorentzian_intensities, known_intensities)


@pytest.mark.parametrize(
Expand Down
10 changes: 5 additions & 5 deletions test/tests/test_poscar.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,9 +92,9 @@ def test_write_read_poscar(
reference_structure = ramannoodle.io.generic.read_ref_structure(
"test/data/temp", file_format="poscar"
)
assert np.isclose(reference_structure._lattice, lattice).all()
assert np.isclose(reference_structure._atomic_numbers, atomic_numbers).all()
assert np.isclose(reference_structure.positions, positions).all()
assert np.allclose(reference_structure._lattice, lattice)
assert np.allclose(reference_structure._atomic_numbers, atomic_numbers)
assert np.allclose(reference_structure.positions, positions)


@pytest.mark.parametrize(
Expand Down Expand Up @@ -166,8 +166,8 @@ def test_read_cart_poscar( # pylint: disable=too-many-arguments
ref_structure = vasp_io.poscar.read_ref_structure(cart_poscar_path)
known_ref_structure = vasp_io.poscar.read_ref_structure(ref_frac_poscar_path)

assert np.isclose(ref_structure.lattice, known_ref_structure.lattice).all()
assert np.isclose(ref_structure.positions, known_ref_structure.positions).all()
assert np.allclose(ref_structure.lattice, known_ref_structure.lattice)
assert np.allclose(ref_structure.positions, known_ref_structure.positions)


@pytest.mark.parametrize(
Expand Down
4 changes: 2 additions & 2 deletions test/tests/test_structure.py
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ def test_get_positions_permutation_matrix(
)
def test_apply_pbc(positions: NDArray[np.float64], known: NDArray[np.float64]) -> None:
"""Test apply_pbc (normal)."""
assert np.isclose(apply_pbc(positions), known).all()
assert np.allclose(apply_pbc(positions), known)


@pytest.mark.parametrize(
Expand Down Expand Up @@ -229,7 +229,7 @@ def test_apply_pbc_displacement(
displacement: NDArray[np.float64], known: NDArray[np.float64]
) -> None:
"""Test test_apply_pbc_displacement (normal)."""
assert np.isclose(apply_pbc_displacement(displacement), known).all()
assert np.allclose(apply_pbc_displacement(displacement), known)


@pytest.mark.parametrize(
Expand Down
4 changes: 2 additions & 2 deletions test/tests/test_trajectory_spectrum.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,8 @@ def test_spectrum(
known_wavenumbers = known_spectrum["wavenumbers"]
known_intensities = known_spectrum["intensities"]

assert np.isclose(wavenumbers, known_wavenumbers).all()
assert np.isclose(intensities, known_intensities, atol=1e-3).all()
assert np.allclose(wavenumbers, known_wavenumbers)
assert np.allclose(intensities, known_intensities, atol=1e-3)


@pytest.mark.parametrize(
Expand Down
14 changes: 7 additions & 7 deletions test/tests/test_vasprun.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@ def test_read_positions_and_polarizability(
path_fixture, file_format="vasprun.xml"
)

assert np.isclose(positions[-1], known_last_positions).all()
assert np.isclose(polarizability, known_polarizability).all()
assert np.allclose(positions[-1], known_last_positions)
assert np.allclose(polarizability, known_polarizability)


@pytest.mark.parametrize(
Expand All @@ -64,7 +64,7 @@ def test_read_positions(
"""Test read_positions (normal)."""
positions = generic_io.read_positions(path_fixture, file_format="vasprun.xml")

assert np.isclose(positions[-1], known_last_positions).all()
assert np.allclose(positions[-1], known_last_positions)


@pytest.mark.parametrize(
Expand Down Expand Up @@ -95,7 +95,7 @@ def test_read_ref_structure(
)

assert ref_structure.atomic_numbers == known_atomic_numbers
assert np.isclose(ref_structure.lattice, known_lattice).all()
assert np.allclose(ref_structure.lattice, known_lattice)


@pytest.mark.parametrize(
Expand All @@ -119,7 +119,7 @@ def test_read_trajectory(
"""Test read_ref_structure (normal)."""
trajectory = generic_io.read_trajectory(path_fixture, file_format="vasprun.xml")

assert np.isclose(trajectory.positions_ts[-1][-1], known_final_position).all()
assert np.allclose(trajectory.positions_ts[-1][-1], known_final_position)
assert len(trajectory) == known_trajectory_length
assert np.isclose(trajectory.timestep, known_timestep)

Expand All @@ -146,8 +146,8 @@ def test_read_phonons(

assert len(phonons.wavenumbers) == len(phonons.displacements)
assert len(phonons.wavenumbers) == degrees_of_freedom
assert np.isclose(phonons.wavenumbers[0:5], known_wavenumbers).all()
assert np.isclose(phonons.displacements[-1][-1], known_last_displacement).all()
assert np.allclose(phonons.wavenumbers[0:5], known_wavenumbers)
assert np.allclose(phonons.displacements[-1][-1], known_last_displacement)


@pytest.mark.parametrize(
Expand Down
2 changes: 1 addition & 1 deletion test/tests/test_xdatcar.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,5 +37,5 @@ def test_read_write_xdatcar(
overwrite=True,
)
written_trajectory = vasp_io.xdatcar.read_trajectory("test/data/temp", 1.0)
assert np.isclose(written_trajectory.positions_ts, trajectory.positions_ts).all()
assert np.allclose(written_trajectory.positions_ts, trajectory.positions_ts)
assert written_trajectory.timestep == trajectory.timestep
2 changes: 1 addition & 1 deletion test/tests/torch/test_gnn.py
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ def test_calc_polarizabilities(
# for rotation in ref_structure._symmetry_dict["rotations"]:
# rotated_polarizability = np.linalg.inv(rotation) @ polarizability @ rotation

# assert np.isclose(polarizability, rotated_polarizability, atol=1e-5).all()
# assert np.allclose(polarizability, rotated_polarizability, atol=1e-5)


# def test_symmetry_displaced() -> None:
Expand Down

0 comments on commit 4ac7389

Please sign in to comment.