diff --git a/src/nomad_measurements/utils.py b/src/nomad_measurements/utils.py index dd0be820..32d2ee9b 100644 --- a/src/nomad_measurements/utils.py +++ b/src/nomad_measurements/utils.py @@ -67,6 +67,12 @@ def create_archive( ) +def _not_equal(a, b) -> bool: + if isinstance(a, np.ndarray): + return (a != b).any() + return a != b + + def merge_sections( # noqa: PLR0912 section: 'ArchiveSection', update: 'ArchiveSection', @@ -87,12 +93,7 @@ def merge_sections( # noqa: PLR0912 continue if not section.m_is_set(quantity): section.m_set(quantity, update.m_get(quantity)) - elif ( - quantity.is_scalar - and section.m_get(quantity) != update.m_get(quantity) - or not quantity.is_scalar - and (section.m_get(quantity) != update.m_get(quantity)).any() - ): + elif _not_equal(section.m_get(quantity), update.m_get(quantity)): warning = f'Merging sections with different values for quantity "{name}".' if logger: logger.warning(warning) diff --git a/tests/test_first_utils.py b/tests/test_first_utils.py index 9c30e1ce..854f8b4b 100644 --- a/tests/test_first_utils.py +++ b/tests/test_first_utils.py @@ -16,25 +16,39 @@ # limitations under the License. # +import numpy as np from nomad.datamodel.metainfo.basesections import ( Component, CompositeSystem, PureSubstanceComponent, PureSubstanceSection, ) +from nomad.metainfo import MEnum, Quantity from nomad_measurements.utils import ( merge_sections, ) -def test_merge_sections(): - component_1 = Component( +class TestComponent(Component): + float_array = Quantity(type=np.float64, shape=['*']) + 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], + bool_array=[True, False], + enum_value='A', ) - component_2 = Component( + component_2 = TestComponent( name='Cu', mass_fraction=1, + float_array=[1.0, 3.0], + bool_array=[True, True], + enum_value='A', ) substance_1 = PureSubstanceSection( name='Cu', @@ -62,8 +76,19 @@ def test_merge_sections(): ) system_3 = CompositeSystem() merge_sections(system_1, system_2) + out, _ = capfd.readouterr() + assert out == ( + 'Merging sections with different values for quantity "float_array".\n' + 'Merging sections with different values for quantity "bool_array".\n' + 'Merging sections with different values for quantity "name".\n' + ) assert system_1.components[0].mass_fraction == 1 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].enum_value == 'A' assert system_1.components[1].name == 'Cu' assert system_1.components[1].pure_substance.name == 'Cu' assert system_1.components[1].pure_substance.iupac_name == 'Copper'