Skip to content

Commit

Permalink
Moved testing to test_model_system
Browse files Browse the repository at this point in the history
Added __ne__ method
  • Loading branch information
JosePizarro3 committed Sep 17, 2024
1 parent 362a926 commit 60c30de
Show file tree
Hide file tree
Showing 5 changed files with 162 additions and 130 deletions.
10 changes: 10 additions & 0 deletions src/nomad_simulations/schema_packages/model_system.py
Original file line number Diff line number Diff line change
Expand Up @@ -312,6 +312,10 @@ def __eq__(self, other: 'Cell') -> bool:
if not isinstance(other, Cell):
return False

# If the `positions` are empty, return False
if self.positions is None or other.positions is None:
return False

# The `positions` should have the same length (same number of positions)
if len(self.positions) != len(other.positions):
return False
Expand All @@ -322,6 +326,9 @@ def __eq__(self, other: 'Cell') -> bool:
return False
return True

def __ne__(self, other: 'Cell') -> bool:
return not self.__eq__(other)

def normalize(self, archive: 'EntryArchive', logger: 'BoundLogger') -> None:
super().normalize(archive, logger)

Expand Down Expand Up @@ -385,6 +392,9 @@ def __eq__(self, other: 'AtomicCell') -> bool:
return False
return True

def __ne__(self, other: 'AtomicCell') -> bool:
return not self.__eq__(other)

def to_ase_atoms(self, logger: 'BoundLogger') -> Optional[ase.Atoms]:
"""
Generates an ASE Atoms object with the most basic information from the parsed `AtomicCell`
Expand Down
1 change: 0 additions & 1 deletion src/nomad_simulations/schema_packages/utils/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,5 @@
get_composition,
get_sibling_section,
get_variables,
is_equal_cell,
is_not_representative,
)
22 changes: 0 additions & 22 deletions src/nomad_simulations/schema_packages/utils/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -175,25 +175,3 @@ def get_composition(children_names: 'list[str]') -> str:
children_count_tup = np.unique(children_names, return_counts=True)
formula = ''.join([f'{name}({count})' for name, count in zip(*children_count_tup)])
return formula if formula else None


def is_equal_cell(cell_1: 'Cell', cell_2: 'Cell') -> bool:
"""
Check if the two `Cell` objects are the same by checking if the defined `positions` are all matching. If
the objects are `AtomicCell`, it checks if the `AtomsState[*].chemical_symbol` are the same.
Args:
cell_1 (Cell): The first `Cell` to compare.
cell_2 (Cell): The second `Cell` to compare.
Returns:
bool: True if the cells are the same, False otherwise.
"""
# TODO extend this function to compare more information of the cells (`lattice_vectors`) and check ase.Atoms functions
# If any of the cells is None or the positions are empty, return False
if cell_1 is None or cell_2 is None:
return False
if cell_1.positions is None or cell_2.positions is None:
return False

return cell_1 == cell_2
152 changes: 152 additions & 0 deletions tests/test_model_system.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,10 @@
import pytest
from nomad.datamodel import EntryArchive

from nomad_simulations.schema_packages.atoms_state import AtomsState
from nomad_simulations.schema_packages.model_system import (
AtomicCell,
Cell,
ChemicalFormula,
ModelSystem,
Symmetry,
Expand All @@ -32,11 +35,160 @@
from .conftest import generate_atomic_cell


class TestCell:
"""
Test the `Cell` section defined in model_system.py
"""

@pytest.mark.parametrize(
'cell_1, cell_2, result',
[
(Cell(), None, False), # one cell is None
(Cell(), Cell(), False), # both cells are empty
(
Cell(positions=[[1, 0, 0]]),
Cell(),
False,
), # one cell has positions, the other is empty
(
Cell(positions=[[1, 0, 0], [0, 1, 0]]),
Cell(positions=[[1, 0, 0]]),
False,
), # length mismatch
(
Cell(positions=[[1, 0, 0], [0, 1, 0]]),
Cell(positions=[[1, 0, 0], [0, -1, 0]]),
False,
), # different positions
(
Cell(positions=[[1, 0, 0], [0, 1, 0], [0, 0, 1]]),
Cell(positions=[[1, 0, 0], [0, 1, 0], [0, 0, 1]]),
True,
), # same ordered positions
(
Cell(positions=[[1, 0, 0], [0, 1, 0], [0, 0, 1]]),
Cell(positions=[[1, 0, 0], [0, 0, 1], [0, 1, 0]]),
True,
), # different ordered positions but same cell
],
)
def test_eq(self, cell_1: Cell, cell_2: Cell, result: bool):
"""
Test the `__eq__` operator function of `Cell`.
"""
assert (cell_1 == cell_2) == result


class TestAtomicCell:
"""
Test the `AtomicCell`, `Cell` and `GeometricSpace` classes defined in model_system.py
"""

@pytest.mark.parametrize(
'cell_1, cell_2, result',
[
(Cell(), None, False), # one cell is None
(Cell(), Cell(), False), # both cells are empty
(
Cell(positions=[[1, 0, 0]]),
Cell(),
False,
), # one cell has positions, the other is empty
(
Cell(positions=[[1, 0, 0], [0, 1, 0]]),
Cell(positions=[[1, 0, 0]]),
False,
), # length mismatch
(
Cell(positions=[[1, 0, 0], [0, 1, 0]]),
Cell(positions=[[1, 0, 0], [0, -1, 0]]),
False,
), # different positions
(
Cell(positions=[[1, 0, 0], [0, 1, 0], [0, 0, 1]]),
Cell(positions=[[1, 0, 0], [0, 1, 0], [0, 0, 1]]),
True,
), # same ordered positions
(
Cell(positions=[[1, 0, 0], [0, 1, 0], [0, 0, 1]]),
Cell(positions=[[1, 0, 0], [0, 0, 1], [0, 1, 0]]),
True,
), # different ordered positions but same cell
(
AtomicCell(positions=[[1, 0, 0], [0, 1, 0], [0, 0, 1]]),
Cell(positions=[[1, 0, 0], [0, 1, 0], [0, 0, 1]]),
False,
), # one atomic cell and another cell (missing chemical symbols)
(
AtomicCell(positions=[[1, 0, 0], [0, 1, 0], [0, 0, 1]]),
AtomicCell(positions=[[1, 0, 0], [0, 1, 0], [0, 0, 1]]),
False,
), # missing chemical symbols
(
AtomicCell(
positions=[[1, 0, 0], [0, 1, 0], [0, 0, 1]],
atoms_state=[
AtomsState(chemical_symbol='H'),
AtomsState(chemical_symbol='H'),
AtomsState(chemical_symbol='O'),
],
),
AtomicCell(
positions=[[1, 0, 0], [0, 1, 0], [0, 0, 1]],
atoms_state=[
AtomsState(chemical_symbol='H'),
AtomsState(chemical_symbol='H'),
AtomsState(chemical_symbol='O'),
],
),
True,
), # same ordered positions and chemical symbols
(
AtomicCell(
positions=[[1, 0, 0], [0, 1, 0], [0, 0, 1]],
atoms_state=[
AtomsState(chemical_symbol='H'),
AtomsState(chemical_symbol='H'),
AtomsState(chemical_symbol='O'),
],
),
AtomicCell(
positions=[[1, 0, 0], [0, 1, 0], [0, 0, 1]],
atoms_state=[
AtomsState(chemical_symbol='H'),
AtomsState(chemical_symbol='Cu'),
AtomsState(chemical_symbol='O'),
],
),
False,
), # same ordered positions but different chemical symbols
(
AtomicCell(
positions=[[1, 0, 0], [0, 1, 0], [0, 0, 1]],
atoms_state=[
AtomsState(chemical_symbol='H'),
AtomsState(chemical_symbol='H'),
AtomsState(chemical_symbol='O'),
],
),
AtomicCell(
positions=[[1, 0, 0], [0, 0, 1], [0, 1, 0]],
atoms_state=[
AtomsState(chemical_symbol='H'),
AtomsState(chemical_symbol='O'),
AtomsState(chemical_symbol='H'),
],
),
True,
), # different ordered positions but same chemical symbols
],
)
def test_eq(self, cell_1: Cell, cell_2: Cell, result: bool):
"""
Test the `__eq__` operator function of `AtomicCell`.
"""
assert (cell_1 == cell_2) == result

@pytest.mark.parametrize(
'chemical_symbols, atomic_numbers, formula, lattice_vectors, positions, periodic_boundary_conditions',
[
Expand Down
107 changes: 0 additions & 107 deletions tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,110 +96,3 @@ def test_get_variables(variables: list, result: list, result_length: int):
assert len(energies) == result_length
for i, energy in enumerate(energies): # asserting energies == result does not work
assert energy.n_points == result[i].n_points


@pytest.mark.parametrize(
'cell_1, cell_2, result',
[
(None, None, False), # both are None
(Cell(), None, False), # one cell is None
(Cell(), Cell(), False), # both cells are empty
(
Cell(positions=[[1, 0, 0]]),
Cell(),
False,
), # one cell has positions, the other is empty
(
Cell(positions=[[1, 0, 0], [0, 1, 0]]),
Cell(positions=[[1, 0, 0]]),
False,
), # length mismatch
(
Cell(positions=[[1, 0, 0], [0, 1, 0]]),
Cell(positions=[[1, 0, 0], [0, -1, 0]]),
False,
), # different positions
(
Cell(positions=[[1, 0, 0], [0, 1, 0], [0, 0, 1]]),
Cell(positions=[[1, 0, 0], [0, 1, 0], [0, 0, 1]]),
True,
), # same ordered positions
(
Cell(positions=[[1, 0, 0], [0, 1, 0], [0, 0, 1]]),
Cell(positions=[[1, 0, 0], [0, 0, 1], [0, 1, 0]]),
True,
), # different ordered positions but same cell
(
AtomicCell(positions=[[1, 0, 0], [0, 1, 0], [0, 0, 1]]),
Cell(positions=[[1, 0, 0], [0, 1, 0], [0, 0, 1]]),
False,
), # one atomic cell and another cell (missing chemical symbols)
(
AtomicCell(positions=[[1, 0, 0], [0, 1, 0], [0, 0, 1]]),
AtomicCell(positions=[[1, 0, 0], [0, 1, 0], [0, 0, 1]]),
False,
), # missing chemical symbols
(
AtomicCell(
positions=[[1, 0, 0], [0, 1, 0], [0, 0, 1]],
atoms_state=[
AtomsState(chemical_symbol='H'),
AtomsState(chemical_symbol='H'),
AtomsState(chemical_symbol='O'),
],
),
AtomicCell(
positions=[[1, 0, 0], [0, 1, 0], [0, 0, 1]],
atoms_state=[
AtomsState(chemical_symbol='H'),
AtomsState(chemical_symbol='H'),
AtomsState(chemical_symbol='O'),
],
),
True,
), # same ordered positions and chemical symbols
(
AtomicCell(
positions=[[1, 0, 0], [0, 1, 0], [0, 0, 1]],
atoms_state=[
AtomsState(chemical_symbol='H'),
AtomsState(chemical_symbol='H'),
AtomsState(chemical_symbol='O'),
],
),
AtomicCell(
positions=[[1, 0, 0], [0, 1, 0], [0, 0, 1]],
atoms_state=[
AtomsState(chemical_symbol='H'),
AtomsState(chemical_symbol='Cu'),
AtomsState(chemical_symbol='O'),
],
),
False,
), # same ordered positions but different chemical symbols
(
AtomicCell(
positions=[[1, 0, 0], [0, 1, 0], [0, 0, 1]],
atoms_state=[
AtomsState(chemical_symbol='H'),
AtomsState(chemical_symbol='H'),
AtomsState(chemical_symbol='O'),
],
),
AtomicCell(
positions=[[1, 0, 0], [0, 0, 1], [0, 1, 0]],
atoms_state=[
AtomsState(chemical_symbol='H'),
AtomsState(chemical_symbol='O'),
AtomsState(chemical_symbol='H'),
],
),
True,
), # different ordered positions but same chemical symbols
],
)
def test_is_equal_cell(cell_1: Cell, cell_2: Cell, result: bool):
"""
Test the `is_equal_cell` utility function.
"""
assert is_equal_cell(cell_1=cell_1, cell_2=cell_2) == result

1 comment on commit 60c30de

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Coverage

Coverage Report
FileStmtsMissCoverMissing
src/nomad_simulations
   __init__.py4250%3–4
   _version.py11282%5–6
src/nomad_simulations/schema_packages
   __init__.py15287%57–59
   atoms_state.py19012932%31–33, 177–197, 215–250, 263–294, 297–315, 362–373, 376–390, 509–524, 536–544, 547–568, 626–633, 645–652, 655–661
   basis_set.py24015336%8–9, 78–79, 115–119, 122–133, 162–165, 168–169, 172–185, 208, 264, 270–271, 275–279, 290–294, 298–302, 306–327, 335, 370–381, 385–397, 417–418, 453–466, 470, 475–476, 513–521, 530, 562–589, 595–600, 603–618, 647–698
   general.py894253%22–25, 69–72, 139, 203, 232–234, 246–304, 307–329
   model_method.py26515243%28–30, 91, 110, 143, 189–192, 195–202, 294–295, 315, 336–355, 371–397, 400–417, 490, 513–556, 559–580, 628–636, 695–697, 723–744, 747–753, 771, 782, 824–831, 869, 888, 968, 1025, 1100, 1214
   model_system.py30020332%43–45, 200–208, 212–218, 301–309, 312–327, 330, 333, 376–393, 396, 410–441, 444–447, 575–602, 620–702, 705–721, 787–791, 794–815, 1020–1058, 1061–1100
   numerical_settings.py25919325%30–32, 54, 165, 193–209, 231–302, 392–412, 428–443, 462–484, 487–514, 579–600, 612–628, 650–680, 698–760, 763–784, 810–812, 827–845, 848–853, 907
   outputs.py1206843%27–28, 171–177, 187–191, 201–205, 208–216, 259–277, 294–327, 330–347, 380, 399
   physical_property.py1024952%38–40, 64–80, 218–220, 241, 253–256, 269, 276, 283–309, 319–321, 324–327, 349–351
   variables.py862571%26–28, 81–88, 91–94, 112–113, 116, 135–136, 139, 163, 185, 207, 229, 251, 274, 290–291, 294
src/nomad_simulations/schema_packages/properties
   band_gap.py513237%26–28, 97–110, 122–144, 147–162
   band_structure.py1237638%27–29, 71–73, 76, 149–150, 162–177, 192–217, 227–236, 250–283, 292–304, 307–326, 339–340, 343, 390–391, 396
   energies.py421857%25–27, 54, 75, 95–97, 100, 117–118, 121, 133–134, 137, 148–149, 152
   fermi_surface.py17759%25–27, 52–55, 58
   forces.py22864%26–28, 55, 75, 94–95, 98
   greens_function.py995247%25–27, 153–155, 167–197, 200–207, 228–229, 232, 253–254, 257, 278–279, 282, 367–373, 383–385, 394–402, 405–421
   hopping_matrix.py291162%25–27, 68–71, 76, 106–109, 112
   permittivity.py483233%25–27, 71–74, 77–80, 89–112, 115–123
   spectral_profile.py26021517%27–29, 57–58, 67–69, 72–78, 98, 112–124, 127–130, 194–195, 217–318, 331–363, 374–386, 400–418, 433–481, 484–520, 539–541, 544, 573–575, 586–611, 616–622
   thermodynamics.py752764%25–27, 53, 74, 90, 99, 108, 119, 128, 155, 165, 175, 190–192, 195, 211, 231–233, 236, 252, 272–274, 277
src/nomad_simulations/schema_packages/utils
   utils.py715128%27–32, 69–79, 86–95, 99–105, 108–113, 117, 121, 138–143, 160–166, 175–177
TOTAL2529154939% 

Tests Skipped Failures Errors Time
1 0 💤 0 ❌ 1 🔥 1.382s ⏱️

Please sign in to comment.