Skip to content

Commit

Permalink
Changed grid_points in variables to points (#68)
Browse files Browse the repository at this point in the history
  • Loading branch information
JosePizarro3 authored May 14, 2024
1 parent efceab6 commit aa86732
Show file tree
Hide file tree
Showing 8 changed files with 59 additions and 64 deletions.
6 changes: 3 additions & 3 deletions src/nomad_simulations/physical_property.py
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ class PhysicalProperty(ArchiveSection):
def variables_shape(self) -> Optional[list]:
"""
Shape of the variables over which the physical property varies. This is extracted from
`Variables.n_grid_points` and appended in a list.
`Variables.n_points` and appended in a list.
Example, a physical property which varies with `Temperature` and `ElectricField` will
return `variables_shape = [n_temperatures, n_electric_fields]`.
Expand All @@ -174,7 +174,7 @@ def variables_shape(self) -> Optional[list]:
(list): The shape of the variables over which the physical property varies.
"""
if self.variables is not None:
return [v.get_n_grid_points(logger) for v in self.variables]
return [v.get_n_points(logger) for v in self.variables]
return []

@property
Expand Down Expand Up @@ -248,7 +248,7 @@ def __setattr__(self, name: str, val: Any) -> None:
if value_shape != self.full_shape:
raise ValueError(
f'The shape of the stored `value` {value_shape} does not match the full shape {self.full_shape} '
f'extracted from the variables `n_grid_points` and the `shape` defined in `PhysicalProperty`.'
f'extracted from the variables `n_points` and the `shape` defined in `PhysicalProperty`.'
)
_new_value.shape = self.full_shape
if hasattr(val, 'magnitude'):
Expand Down
8 changes: 4 additions & 4 deletions src/nomad_simulations/properties/spectral_profile.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,17 +51,17 @@ def __init__(

def _get_energy_points(self, logger: BoundLogger) -> Optional[pint.Quantity]:
"""
Gets the `grid_points` of the `Energy` variable if the required `Energy` variable is present in the `variables`.
Gets the `points` of the `Energy` variable if the required `Energy` variable is present in the `variables`.
Args:
logger (BoundLogger): The logger to log messages.
Returns:
(Optional[pint.Quantity]): The `grid_points` of the `Energy` variable.
(Optional[pint.Quantity]): The `points` of the `Energy` variable.
"""
for var in self.variables:
if isinstance(var, Energy):
return var.grid_points
return var.points
logger.error(
'The required `Energy` variable is not present in the `variables`.'
)
Expand Down Expand Up @@ -582,7 +582,7 @@ def generate_from_contributions(self, logger: BoundLogger) -> None:
)
return
self.variables = [
Energy(grid_points=np.concatenate([xanes_energies, exafs_energies]))
Energy(points=np.concatenate([xanes_energies, exafs_energies]))
]
# Concatenate XANES and EXAFS `value` if they have the same shape ['n_energies']
try:
Expand Down
61 changes: 29 additions & 32 deletions src/nomad_simulations/variables.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@

class Variables(ArchiveSection):
"""
Variables over which the physical property varies. These are defined as binned, i.e., discretized
values by `n_bins` and `bins`. These are used to calculate the `shape` of the physical property.
Variables over which the physical property varies, and they are defined as grid points, i.e., discretized
values by `n_points` and `points`. These are used to calculate the `shape` of the physical property.
"""

name = Quantity(
Expand All @@ -38,63 +38,60 @@ class Variables(ArchiveSection):
""",
)

n_grid_points = Quantity(
n_points = Quantity(
type=int,
description="""
Number of grid points in which the variable is discretized.
Number of points in which the variable is discretized.
""",
)

grid_points = Quantity(
points = Quantity(
type=np.float64,
shape=['n_grid_points'],
shape=['n_points'],
description="""
Grid points in which the variable is discretized. It might be overwritten with specific units.
Points in which the variable is discretized. It might be overwritten with specific units.
""",
)

# grid_points_error = Quantity()
# points_error = Quantity()

def get_n_grid_points(self, logger: BoundLogger) -> Optional[int]:
def get_n_points(self, logger: BoundLogger) -> Optional[int]:
"""
Get the number of grid points from the `grid_points` list. If `n_grid_points` is previously defined
and does not coincide with the length of `grid_points`, a warning is issued and this function re-assigns `n_grid_points`
as the length of `grid_points`.
Get the number of grid points from the `points` list. If `n_points` is previously defined
and does not coincide with the length of `points`, a warning is issued and this function re-assigns `n_points`
as the length of `points`.
Args:
logger (BoundLogger): The logger to log messages.
Returns:
(Optional[int]): The number of grid points.
(Optional[int]): The number of points.
"""
if self.grid_points is not None and len(self.grid_points) > 0:
if (
self.n_grid_points != len(self.grid_points)
and self.n_grid_points is not None
):
if self.points is not None and len(self.points) > 0:
if self.n_points != len(self.points) and self.n_points is not None:
logger.warning(
f'The stored `n_grid_points`, {self.n_grid_points}, does not coincide with the length of `grid_points`, '
f'{len(self.grid_points)}. We will re-assign `n_grid_points` as the length of `grid_points`.'
f'The stored `n_points`, {self.n_points}, does not coincide with the length of `points`, '
f'{len(self.points)}. We will re-assign `n_points` as the length of `points`.'
)
return len(self.grid_points)
return self.n_grid_points
return len(self.points)
return self.n_points

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

# Setting `n_grid_points` if these are not defined
self.n_grid_points = self.get_n_grid_points(logger)
# Setting `n_points` if these are not defined
self.n_points = self.get_n_points(logger)


class Temperature(Variables):
""" """

grid_points = Quantity(
points = Quantity(
type=np.float64,
unit='kelvin',
shape=['n_grid_points'],
shape=['n_points'],
description="""
Grid points in which the temperature is discretized.
Points in which the temperature is discretized.
""",
)

Expand All @@ -112,12 +109,12 @@ def normalize(self, archive, logger) -> None:
class Energy2(Variables):
""" """

grid_points = Quantity(
points = Quantity(
type=np.float64,
unit='joule',
shape=['n_grid_points'],
shape=['n_points'],
description="""
Grid points in which the energy is discretized.
Points in which the energy is discretized.
""",
)

Expand All @@ -137,9 +134,9 @@ class WignerSeitz(Variables):
other inter-cell properties. See, e.g., https://en.wikipedia.org/wiki/Wigner–Seitz_cell.
"""

grid_points = Quantity(
points = Quantity(
type=np.float64,
shape=['n_grid_points', 3],
shape=['n_points', 3],
description="""
Wigner-Seitz points with respect to the origin cell, (0, 0, 0). These are 3D arrays stored in fractional coordinates.
""",
Expand Down
2 changes: 1 addition & 1 deletion tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ def generate_simulation_electronic_dos(
simulation = generate_simulation(model_system=model_system, outputs=outputs)

# Populating the `ElectronicDensityOfStates` section
variables_energy = [Energy(grid_points=energy_points * ureg.joule)]
variables_energy = [Energy(points=energy_points * ureg.joule)]
electronic_dos = ElectronicDensityOfStates(variables=variables_energy)
outputs.electronic_dos.append(electronic_dos)
# electronic_dos.value = total_dos * ureg('1/joule')
Expand Down
4 changes: 2 additions & 2 deletions tests/test_band_gap.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ def test_check_negative_values(
"""
if isinstance(value, list):
electronic_band_gap = ElectronicBandGap(
variables=[Temperature(grid_points=[1, 2, 3] * ureg.kelvin)]
variables=[Temperature(points=[1, 2, 3] * ureg.kelvin)]
)
else:
electronic_band_gap = ElectronicBandGap()
Expand Down Expand Up @@ -120,7 +120,7 @@ def test_normalize(self):
assert np.isclose(scalar_band_gap.value.magnitude, 1.0)

t_dependent_band_gap = ElectronicBandGap(
variables=[Temperature(grid_points=[0, 10, 20, 30] * ureg.kelvin)],
variables=[Temperature(points=[0, 10, 20, 30] * ureg.kelvin)],
type='direct',
)
t_dependent_band_gap.value = [1.0, 2.0, 3.0, 4.0] * ureg.joule
Expand Down
16 changes: 8 additions & 8 deletions tests/test_physical_properties.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,24 +50,24 @@ class TestPhysicalProperty:
([], [], [], []),
([3], [], [], [3]),
([3, 3], [], [], [3, 3]),
([], [Variables(n_grid_points=4)], [4], [4]),
([3], [Variables(n_grid_points=4)], [4], [4, 3]),
([3, 3], [Variables(n_grid_points=4)], [4], [4, 3, 3]),
([], [Variables(n_points=4)], [4], [4]),
([3], [Variables(n_points=4)], [4], [4, 3]),
([3, 3], [Variables(n_points=4)], [4], [4, 3, 3]),
(
[],
[Variables(n_grid_points=4), Variables(n_grid_points=10)],
[Variables(n_points=4), Variables(n_points=10)],
[4, 10],
[4, 10],
),
(
[3],
[Variables(n_grid_points=4), Variables(n_grid_points=10)],
[Variables(n_points=4), Variables(n_points=10)],
[4, 10],
[4, 10, 3],
),
(
[3, 3],
[Variables(n_grid_points=4), Variables(n_grid_points=10)],
[Variables(n_points=4), Variables(n_points=10)],
[4, 10],
[4, 10, 3, 3],
),
Expand Down Expand Up @@ -98,7 +98,7 @@ def test_setattr_value(self):
physical_property = DummyPhysicalProperty(
source='simulation',
rank=[3, 3],
variables=[Variables(n_grid_points=4), Variables(n_grid_points=10)],
variables=[Variables(n_points=4), Variables(n_points=10)],
)
# `physical_property.value` must have full_shape=[4, 10, 3, 3]
value = np.ones((4, 10, 3, 3)) * ureg.eV
Expand All @@ -122,7 +122,7 @@ def test_setattr_value_wrong_shape(self):
physical_property.value = value
assert (
str(exc_info.value)
== f'The shape of the stored `value` {wrong_shape} does not match the full shape {physical_property.full_shape} extracted from the variables `n_grid_points` and the `shape` defined in `PhysicalProperty`.'
== f'The shape of the stored `value` {wrong_shape} does not match the full shape {physical_property.full_shape} extracted from the variables `n_points` and the `shape` defined in `PhysicalProperty`.'
)

def test_setattr_none(self):
Expand Down
12 changes: 5 additions & 7 deletions tests/test_spectral_profile.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ def test_is_valid_spectral_profile(self):
Test the `is_valid_spectral_profile` method.
"""
spectral_profile = SpectralProfile(
variables=[Energy(grid_points=[-1, 0, 1] * ureg.joule)]
variables=[Energy(points=[-1, 0, 1] * ureg.joule)]
)
spectral_profile.value = [1.5, 0, 0.8]
assert spectral_profile.is_valid_spectral_profile()
Expand Down Expand Up @@ -79,12 +79,10 @@ def test_get_energy_points(self):
"""
electronic_dos = ElectronicDensityOfStates()
electronic_dos.variables = [
Temperature(grid_points=list(range(-3, 4)) * ureg.kelvin)
Temperature(points=list(range(-3, 4)) * ureg.kelvin)
]
assert electronic_dos._get_energy_points(logger) is None
electronic_dos.variables.append(
Energy(grid_points=list(range(-3, 4)) * ureg.joule)
)
electronic_dos.variables.append(Energy(points=list(range(-3, 4)) * ureg.joule))
energies = electronic_dos._get_energy_points(logger)
assert (energies.magnitude == np.array([-3, -2, -1, 0, 1, 2, 3])).all()

Expand Down Expand Up @@ -289,12 +287,12 @@ def test_generate_from_contributions(
xas_spectra = XASSpectra()
if xanes_energies is not None:
xanes_spectra = SpectralProfile()
xanes_spectra.variables = [Energy(grid_points=xanes_energies * ureg.joule)]
xanes_spectra.variables = [Energy(points=xanes_energies * ureg.joule)]
xanes_spectra.value = [0.5, 0.1, 0.3]
xas_spectra.xanes_spectra = xanes_spectra
if exafs_energies is not None:
exafs_spectra = SpectralProfile()
exafs_spectra.variables = [Energy(grid_points=exafs_energies * ureg.joule)]
exafs_spectra.variables = [Energy(points=exafs_energies * ureg.joule)]
exafs_spectra.value = [0.2, 0.4, 0.6]
xas_spectra.exafs_spectra = exafs_spectra
xas_spectra.generate_from_contributions(logger)
Expand Down
14 changes: 7 additions & 7 deletions tests/test_variables.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class TestVariables:
"""

@pytest.mark.parametrize(
'n_grid_points, grid_points, result',
'n_points, points, result',
[
(3, [-1, 0, 1], 3),
(5, [-1, 0, 1], 3),
Expand All @@ -40,15 +40,15 @@ class TestVariables:
(4, [], 4),
],
)
def test_normalize(self, n_grid_points: int, grid_points: list, result: int):
def test_normalize(self, n_points: int, points: list, result: int):
"""
Test the `normalize` and `get_n_grid_points` methods.
Test the `normalize` and `get_n_points` methods.
"""
variable = Variables(
name='variable_1',
n_grid_points=n_grid_points,
grid_points=grid_points,
n_points=n_points,
points=points,
)
assert variable.get_n_grid_points(logger) == result
assert variable.get_n_points(logger) == result
variable.normalize(EntryArchive(), logger)
assert variable.n_grid_points == result
assert variable.n_points == result

0 comments on commit aa86732

Please sign in to comment.