Skip to content

Commit

Permalink
Fixed PhysicalProperty when it does not have a magnitude or is a list
Browse files Browse the repository at this point in the history
Added tests for SpectralProfile
  • Loading branch information
JosePizarro3 committed Apr 16, 2024
1 parent 04789c7 commit 163e0cc
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 14 deletions.
11 changes: 10 additions & 1 deletion src/nomad_simulations/physical_property.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
# limitations under the License.
#

import numpy as np
from typing import Any, Optional

from nomad import utils
Expand Down Expand Up @@ -229,6 +230,11 @@ def __setattr__(self, name: str, val: Any) -> None:
f'The value of the physical property {self.name} is None. Please provide a finite valid value.'
)
_new_value = self._new_value

# patch for when `val` does not have units and it is passed as a list (instead of np.array)
if isinstance(val, list):
val = np.array(val)

# non-scalar or scalar `val`
try:
value_shape = list(val.shape)
Expand All @@ -241,7 +247,10 @@ def __setattr__(self, name: str, val: Any) -> None:
f'extracted from the variables `n_grid_points` and the `shape` defined in `PhysicalProperty`.'
)
_new_value.shape = self.full_shape
_new_value = val.magnitude * val.u
if hasattr(val, 'magnitude'):
_new_value = val.magnitude * val.u
else:
_new_value = val
return super().__setattr__(name, _new_value)
return super().__setattr__(name, val)

Expand Down
6 changes: 5 additions & 1 deletion src/nomad_simulations/properties/spectral_profile.py
Original file line number Diff line number Diff line change
Expand Up @@ -187,8 +187,12 @@ def _check_spin_polarized(self, logger: BoundLogger) -> bool:
(bool): True if the simulation is spin-polarized, False otherwise.
"""
# TODO use this in `ElectronicBandGap` too
outputs = self.m_parent
if outputs is None:
logger.warning('Could not resolve the parent `Outputs`.')
return False
model_method = get_sibling_section(
section=self, sibling_section_name='model_method', logger=logger
section=outputs, sibling_section_name='model_method', logger=logger
)
return (
True
Expand Down
3 changes: 2 additions & 1 deletion tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,9 @@

from nomad.units import ureg

from nomad_simulations.outputs import ElectronicBandGap, Outputs, SCFOutputs
from nomad_simulations.outputs import Outputs, SCFOutputs
from nomad_simulations.numerical_settings import SelfConsistency
from nomad_simulations.properties import ElectronicBandGap

if os.getenv('_PYTEST_RAISE', '0') != '0':

Expand Down
46 changes: 35 additions & 11 deletions tests/test_spectral_profile.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,16 +28,25 @@
ElectronicDensityOfStates,
XASSpectra,
)
from nomad_simulations.variables import Temperature
from nomad_simulations.variables import Temperature, Energy2 as Energy


class TestSpectralProfile:
"""
Test the `SpectralProfile` class defined in `properties/spectral_profile.py`.
"""

def test_negative_value(self):
spectral_profile = SpectralProfile()
def test_is_valid_spectral_profile(self):
"""
Test the `is_valid_spectral_profile` method.
"""
spectral_profile = SpectralProfile(
variables=[Energy(grid_points=[-3, -2, -1, 0, 1, 2, 3] * ureg.joule)]
)
spectral_profile.value = [1.5, 1.2, 0, 0, 0, 0.8, 1.3]
assert spectral_profile.is_valid_spectral_profile()
spectral_profile.value = [3, 2, 0, 0, 0, -4, 1]
assert not spectral_profile.is_valid_spectral_profile()
# default value inherited in other SpectralProfile classes
assert spectral_profile.rank == []

Expand All @@ -52,13 +61,28 @@ def test_default_quantities(self):
"""
Test the default quantities assigned when creating an instance of the `ElectronicDensityOfStates` class.
"""
electronic_band_gap = ElectronicDensityOfStates()
electronic_dos = ElectronicDensityOfStates()
assert (
electronic_band_gap.iri
electronic_dos.iri
== 'http://fairmat-nfdi.eu/taxonomy/ElectronicDensityOfStates'
)
assert electronic_band_gap.name == 'ElectronicDensityOfStates'
assert electronic_band_gap.rank == []
assert electronic_dos.name == 'ElectronicDensityOfStates'
assert electronic_dos.rank == []

def test_check_energy_variables(self):
"""
Test the `_check_energy_variables` method.
"""
electronic_dos = ElectronicDensityOfStates()
electronic_dos.variables = [
Temperature(grid_points=[-3, -2, -1, 0, 1, 2, 3] * ureg.kelvin)
]
assert electronic_dos._check_energy_variables(logger) is None
electronic_dos.variables.append(
Energy(grid_points=[-3, -2, -1, 0, 1, 2, 3] * ureg.joule)
)
energies = electronic_dos._check_energy_variables(logger)
assert (energies.magnitude == np.array([-3, -2, -1, 0, 1, 2, 3])).all()


class TestXASSpectra:
Expand All @@ -71,7 +95,7 @@ def test_default_quantities(self):
"""
Test the default quantities assigned when creating an instance of the `XASSpectra` class.
"""
electronic_band_gap = XASSpectra()
assert electronic_band_gap.iri is None # Add iri when available
assert electronic_band_gap.name == 'XASSpectra'
assert electronic_band_gap.rank == []
xas_spectra = XASSpectra()
assert xas_spectra.iri is None # Add iri when available
assert xas_spectra.name == 'XASSpectra'
assert xas_spectra.rank == []

0 comments on commit 163e0cc

Please sign in to comment.