Skip to content

Commit

Permalink
Added check on quantities in and fix HoppingMatrix, CrystalFieldSplit…
Browse files Browse the repository at this point in the history
…ting tests
  • Loading branch information
JosePizarro3 committed May 13, 2024
1 parent 2530b84 commit 0f4d417
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 19 deletions.
9 changes: 9 additions & 0 deletions src/nomad_simulations/physical_property.py
Original file line number Diff line number Diff line change
Expand Up @@ -221,11 +221,20 @@ def __init__(
self, m_def: Section = None, m_context: Context = None, **kwargs
) -> None:
super().__init__(m_def, m_context, **kwargs)

# Checking if IRI is defined
if self.iri is None:
logger.warning(
'The used property is not defined in the FAIRmat taxonomy (https://fairmat-nfdi.github.io/fairmat-taxonomy/). You can contribute there if you want to extend the list of available materials properties.'
)

# Checking if the quantities `n_` are defined, as this are used to calculate `rank`
for quantity, _ in self.m_def.all_quantities.items():
if quantity.startswith('n_') and getattr(self, quantity) is None:
raise ValueError(
f'`{quantity}` is not defined during initialization of the class.'
)

def __setattr__(self, name: str, val: Any) -> None:
# For the special case of `value`, its `shape` needs to be defined from `_full_shape`
if name == 'value':
Expand Down
10 changes: 1 addition & 9 deletions src/nomad_simulations/properties/hopping_matrix.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,6 @@ def __init__(
) -> None:
super().__init__(m_def, m_context, **kwargs)
# ! n_orbitals need to be set up during initialization of the class
if self.n_orbitals is None:
raise ValueError(
'`n_orbitals` is not defined during initialization of the class.'
)
self.rank = [self.n_orbitals, self.n_orbitals]
self.name = self.m_def.name

Expand Down Expand Up @@ -100,11 +96,7 @@ def __init__(
self, m_def: Section = None, m_context: Context = None, **kwargs
) -> None:
super().__init__(m_def, m_context, **kwargs)
# ! n_orbitals need to be set up during initialization of the class
if self.n_orbitals is None:
raise ValueError(
'`n_orbitals` is not defined during initialization of the class.'
)
# ! `n_orbitals` need to be set up during initialization of the class
self.rank = [self.n_orbitals]
self.name = self.m_def.name

Expand Down
37 changes: 27 additions & 10 deletions tests/test_hopping_matrix.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,18 @@ def test_default_quantities(self, n_orbitals: int, rank: list):
"""
Test the default quantities assigned when creating an instance of the `HoppingMatrix` class.
"""
hopping_matrix = HoppingMatrix(n_orbitals=n_orbitals)
assert hopping_matrix.iri == 'http://fairmat-nfdi.eu/taxonomy/HoppingMatrix'
assert hopping_matrix.name == 'HoppingMatrix'
assert hopping_matrix.rank == rank
if n_orbitals is None:
with pytest.raises(ValueError) as exc_info:
hopping_matrix = HoppingMatrix(n_orbitals=n_orbitals)
assert (
str(exc_info.value)
== f'`n_orbitals` is not defined during initialization of the class.'
)
else:
hopping_matrix = HoppingMatrix(n_orbitals=n_orbitals)
assert hopping_matrix.iri == 'http://fairmat-nfdi.eu/taxonomy/HoppingMatrix'
assert hopping_matrix.name == 'HoppingMatrix'
assert hopping_matrix.rank == rank


class TestCrystalFieldSplitting:
Expand All @@ -62,9 +70,18 @@ def test_default_quantities(self, n_orbitals: int, rank: list):
"""
Test the default quantities assigned when creating an instance of the `CrystalFieldSplitting` class.
"""
crystal_field = CrystalFieldSplitting(n_orbitals=n_orbitals)
assert (
crystal_field.iri == 'http://fairmat-nfdi.eu/taxonomy/CrystalFieldSplitting'
)
assert crystal_field.name == 'CrystalFieldSplitting'
assert crystal_field.rank == rank
if n_orbitals is None:
with pytest.raises(ValueError) as exc_info:
crystal_field = CrystalFieldSplitting(n_orbitals=n_orbitals)
assert (
str(exc_info.value)
== f'`n_orbitals` is not defined during initialization of the class.'
)
else:
crystal_field = CrystalFieldSplitting(n_orbitals=n_orbitals)
assert (
crystal_field.iri
== 'http://fairmat-nfdi.eu/taxonomy/CrystalFieldSplitting'
)
assert crystal_field.name == 'CrystalFieldSplitting'
assert crystal_field.rank == rank

0 comments on commit 0f4d417

Please sign in to comment.