Skip to content

Commit

Permalink
137 merge sections break for non scalar quantities (#138)
Browse files Browse the repository at this point in the history
* Added breaking test for boolean array

* Added more breaking test cases

* Added fix for comparing non numpy arrays

* Ruff

* Enhance test_merge_sections to capture output and validate float_array values

* Refine warning message for merging sections with differing quantity values

* Refactor merge_sections to improve warning logic for differing quantity values
  • Loading branch information
hampusnasstrom authored Nov 18, 2024
1 parent 230852c commit ca534fb
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 9 deletions.
13 changes: 7 additions & 6 deletions src/nomad_measurements/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand All @@ -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)
Expand Down
31 changes: 28 additions & 3 deletions tests/test_first_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down Expand Up @@ -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'
Expand Down

0 comments on commit ca534fb

Please sign in to comment.