From aa8673212ecd1e26615097ae824e58cd33460db6 Mon Sep 17 00:00:00 2001 From: "Jose M. Pizarro" <112697669+JosePizarro3@users.noreply.github.com> Date: Tue, 14 May 2024 14:29:15 +0200 Subject: [PATCH] Changed grid_points in variables to points (#68) --- src/nomad_simulations/physical_property.py | 6 +- .../properties/spectral_profile.py | 8 +-- src/nomad_simulations/variables.py | 61 +++++++++---------- tests/conftest.py | 2 +- tests/test_band_gap.py | 4 +- tests/test_physical_properties.py | 16 ++--- tests/test_spectral_profile.py | 12 ++-- tests/test_variables.py | 14 ++--- 8 files changed, 59 insertions(+), 64 deletions(-) diff --git a/src/nomad_simulations/physical_property.py b/src/nomad_simulations/physical_property.py index afb0fab8..d1a1cd03 100644 --- a/src/nomad_simulations/physical_property.py +++ b/src/nomad_simulations/physical_property.py @@ -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]`. @@ -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 @@ -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'): diff --git a/src/nomad_simulations/properties/spectral_profile.py b/src/nomad_simulations/properties/spectral_profile.py index 943c3c6a..4b57ba60 100644 --- a/src/nomad_simulations/properties/spectral_profile.py +++ b/src/nomad_simulations/properties/spectral_profile.py @@ -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`.' ) @@ -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: diff --git a/src/nomad_simulations/variables.py b/src/nomad_simulations/variables.py index 7c713b15..03fdaa1e 100644 --- a/src/nomad_simulations/variables.py +++ b/src/nomad_simulations/variables.py @@ -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( @@ -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. """, ) @@ -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. """, ) @@ -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. """, diff --git a/tests/conftest.py b/tests/conftest.py index 7678d689..bde50e79 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -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') diff --git a/tests/test_band_gap.py b/tests/test_band_gap.py index bfc45dc7..1c2b3e63 100644 --- a/tests/test_band_gap.py +++ b/tests/test_band_gap.py @@ -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() @@ -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 diff --git a/tests/test_physical_properties.py b/tests/test_physical_properties.py index 4bed0cc4..d66c924e 100644 --- a/tests/test_physical_properties.py +++ b/tests/test_physical_properties.py @@ -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], ), @@ -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 @@ -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): diff --git a/tests/test_spectral_profile.py b/tests/test_spectral_profile.py index 83eafd04..4316ecf7 100644 --- a/tests/test_spectral_profile.py +++ b/tests/test_spectral_profile.py @@ -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() @@ -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() @@ -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) diff --git a/tests/test_variables.py b/tests/test_variables.py index 71ec5a97..ab71b4a2 100644 --- a/tests/test_variables.py +++ b/tests/test_variables.py @@ -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), @@ -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