Skip to content

Commit

Permalink
139 merge sections still breaking for pint quantity arrays (#140)
Browse files Browse the repository at this point in the history
* Added breaking test for float array with units

* Added fix for comparison of pint quantity arrays

* Added test for multi dimensional array

* Ruff
  • Loading branch information
hampusnasstrom authored Nov 19, 2024
1 parent ca534fb commit 057f4a9
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 8 deletions.
7 changes: 4 additions & 3 deletions src/nomad_measurements/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,10 @@ def create_archive(


def _not_equal(a, b) -> bool:
if isinstance(a, np.ndarray):
return (a != b).any()
return a != b
comparison = a != b
if isinstance(comparison, np.ndarray):
return comparison.any()
return comparison


def merge_sections( # noqa: PLR0912
Expand Down
16 changes: 11 additions & 5 deletions tests/test_first_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,29 +24,33 @@
PureSubstanceSection,
)
from nomad.metainfo import MEnum, Quantity
from nomad.units import ureg

from nomad_measurements.utils import (
merge_sections,
)


class TestComponent(Component):
float_array = Quantity(type=np.float64, shape=['*'])
float_array = Quantity(type=np.float64, shape=[2, '*'])
float_array_w_units = Quantity(type=np.float64, shape=['*'], unit='eV')
bool_array = Quantity(type=bool, shape=['*'])
enum_value = Quantity(type=MEnum(['A', 'B', 'C']))


def test_merge_sections(capfd):
component_1 = TestComponent(
mass_fraction=1,
float_array=[1.0, 1.0],
float_array=[[1.0, 1.0], [1.0, 3.0]],
float_array_w_units=[1.0, 1.0],
bool_array=[True, False],
enum_value='A',
)
component_2 = TestComponent(
name='Cu',
mass_fraction=1,
float_array=[1.0, 3.0],
float_array=[[1.0, 3.0], [1.0, 3.0]],
float_array_w_units=[1.0, 1.0],
bool_array=[True, True],
enum_value='A',
)
Expand Down Expand Up @@ -86,8 +90,10 @@ def test_merge_sections(capfd):
assert system_1.components[0].name == 'Cu'
assert system_1.components[0].bool_array[0] is True
assert system_1.components[0].bool_array[1] is False
assert system_1.components[0].float_array[0] == 1.0
assert system_1.components[0].float_array[1] == 1.0
assert system_1.components[0].float_array[0][0] == 1.0
assert system_1.components[0].float_array[0][1] == 1.0
assert system_1.components[0].float_array_w_units[0] == ureg.Quantity(1.0, 'eV')
assert system_1.components[0].float_array_w_units[1] == ureg.Quantity(1.0, 'eV')
assert system_1.components[0].enum_value == 'A'
assert system_1.components[1].name == 'Cu'
assert system_1.components[1].pure_substance.name == 'Cu'
Expand Down

0 comments on commit 057f4a9

Please sign in to comment.