Skip to content

Commit

Permalink
Fix typing
Browse files Browse the repository at this point in the history
Change variables.Energy to Energy2 to avoid bugs with Normalizers (to be fixed)
  • Loading branch information
JosePizarro3 committed Apr 18, 2024
1 parent d267f34 commit c9df759
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 9 deletions.
14 changes: 12 additions & 2 deletions src/nomad_simulations/physical_property.py
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,9 @@ def _new_value(self) -> Quantity:
description=quant.description,
)

def __init__(self, m_def: Section = None, m_context: Context = None, **kwargs):
def __init__(
self, m_def: Section = None, m_context: Context = None, **kwargs
) -> None:
super().__init__(m_def, m_context, **kwargs)

def __setattr__(self, name: str, val: Any) -> None:
Expand All @@ -223,6 +225,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 @@ -235,7 +242,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
8 changes: 5 additions & 3 deletions src/nomad_simulations/properties/band_gap.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,10 +75,12 @@ class ElectronicBandGap(PhysicalProperty):
""",
)

def __init__(self, m_def: Section = None, m_context: Context = None, **kwargs):
def __init__(
self, m_def: Section = None, m_context: Context = None, **kwargs
) -> None:
super().__init__(m_def, m_context, **kwargs)
self.name = self.m_def.name
self.rank = [] # ? Is this here or in the attrs instantiation better?
self.rank = []

def check_negative_values(self, logger: BoundLogger) -> Optional[pint.Quantity]:
"""
Expand Down Expand Up @@ -149,7 +151,7 @@ def normalize(self, archive, logger) -> None:
# Resolve the `type` of the electronic band gap from `momentum_transfer`, ONLY for scalar `value`
if isinstance(self.value.magnitude, np.ndarray):
logger.info(
'Currently we do not support `type` which describe arrays of `value`.'
'We do not support `type` which describe individual elements in an array `value`.'
)
else:
self.type = self.resolve_type(logger)
11 changes: 8 additions & 3 deletions src/nomad_simulations/variables.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,15 +101,18 @@ class Temperature(Variables):
""",
)

def __init__(self, m_def: Section = None, m_context: Context = None, **kwargs):
def __init__(
self, m_def: Section = None, m_context: Context = None, **kwargs
) -> None:
super().__init__(m_def, m_context, **kwargs)
self.name = self.m_def.name

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


class Energy(Variables):
# ! This needs to be fixed as it gives errors when running normalizers with conflicting names (ask Area D)
class Energy2(Variables):
""" """

grid_points = Quantity(
Expand All @@ -121,7 +124,9 @@ class Energy(Variables):
""",
)

def __init__(self, m_def: Section = None, m_context: Context = None, **kwargs):
def __init__(
self, m_def: Section = None, m_context: Context = None, **kwargs
) -> None:
super().__init__(m_def, m_context, **kwargs)
self.name = self.m_def.name

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

0 comments on commit c9df759

Please sign in to comment.