Skip to content

Commit

Permalink
Merge pull request #65 from FAIRmat-NFDI/61-add-tests-for-xrd-parser
Browse files Browse the repository at this point in the history
Add tests for xrd parser
  • Loading branch information
Pepe-Marquez authored Jan 30, 2024
2 parents 71b7e8c + e95cd30 commit 2ed41c4
Show file tree
Hide file tree
Showing 11 changed files with 78 additions and 18 deletions.
2 changes: 1 addition & 1 deletion nomad.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ normalize:
- MetainfoNormalizer
plugins:
include:
- nomad_measurements.xrd
- parsers/nomad_measurements/xrd
options:
parsers/nomad_measurements/xrd:
python_package: nomad_measurements.xrd
7 changes: 5 additions & 2 deletions src/nomad_measurements/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
# See the License for the specific language governing permissions and
# limitations under the License.
#
import os.path
from typing import (
TYPE_CHECKING,
Any,
Expand Down Expand Up @@ -49,10 +50,12 @@ def create_archive(
) -> str:
import json
from nomad.datamodel.context import ClientContext
entity_entry = entity.m_to_dict(with_root_def=True)
if isinstance(archive.m_context, ClientContext):
return None
with open(file_name, 'w') as outfile:
json.dump({"data": entity_entry}, outfile, indent=4)
return os.path.abspath(file_name)
if not archive.m_context.raw_path_exists(file_name):
entity_entry = entity.m_to_dict(with_root_def=True)
with archive.m_context.raw_file(file_name, 'w') as outfile:
json.dump({"data": entity_entry}, outfile)
archive.m_context.process_updated_raw_file(file_name)
Expand Down
Binary file added tests/data/23-012-AG_2thomegascan_long.brml
Binary file not shown.
Binary file not shown.
Binary file added tests/data/RSM_111_sdd=350.rasx
Binary file not shown.
Binary file added tests/data/TwoTheta_scan_powder.rasx
Binary file not shown.
2 changes: 1 addition & 1 deletion tests/data/XRD-918-16_10.xrdml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
<entry>Batch program=C:\PANalytical\Data Collector\Programs\BBHD-FASS_4-70_step01_40s_Omar_6 samples.xrdmp, Identifier={D0DF481D-C866-435E-BCBB-469939A807AC}</entry>
</comment>
<sample type="To be analyzed">
<id>918-16</id>
<id></id>
<name></name>
<preparedBy></preparedBy>
</sample>
Expand Down
3 changes: 0 additions & 3 deletions tests/data/test_x_ray_diffraction.archive.yaml

This file was deleted.

56 changes: 56 additions & 0 deletions tests/test_parser.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
#
# Copyright The NOMAD Authors.
#
# This file is part of NOMAD. See https://nomad-lab.eu for further info.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
import os
import pytest

from nomad.client import parse, normalize_all

@pytest.fixture(params=[
'XRD-918-16_10.xrdml',
'm54313_om2th_10.xrdml',
'23-012-AG_2thomegascan_long.brml',
'Omega-2Theta_scan_high_temperature.rasx',
'RSM_111_sdd=350.rasx',
'TwoTheta_scan_powder.rasx',
])
def parsed_archive(request):
'''
Sets up data for testing and cleans up after the test.
'''
rel_file = os.path.join('tests', 'data', request.param)
file_archive = parse(rel_file)[0]
measurement = os.path.join(
'tests', 'data', '.'.join(request.param.split('.')[:-1]) + '.archive.json'
)
assert file_archive.data.measurement.m_proxy_value == os.path.abspath(measurement)
measurement_archive = parse(measurement)[0]

yield measurement_archive

if os.path.exists(measurement):
os.remove(measurement)

def test_normalize_all(parsed_archive):
normalize_all(parsed_archive)
print(parsed_archive.data)
assert parsed_archive.data.xrd_settings.source.xray_tube_material == 'Cu'
assert parsed_archive.data.results[0].source_peak_wavelength.magnitude \
== pytest.approx(1.540598, 1e-2)
assert parsed_archive.results.properties.structural.diffraction_pattern[0]\
.incident_beam_wavelength.magnitude * 1e10 \
== pytest.approx(1.540598, 1e-2)
10 changes: 0 additions & 10 deletions tests/test_schema.py

This file was deleted.

16 changes: 15 additions & 1 deletion tests/test_utils.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
import numpy as np

from nomad.datamodel.metainfo.basesections import (
Component,
CompositeSystem,
PureSubstanceComponent,
PureSubstanceSection,
)
from nomad_measurements.utils import merge_sections
from nomad.units import ureg
from nomad_measurements.utils import(
merge_sections,
to_pint_quantity,
)

def test_merge_sections():
component_1 = Component(
Expand Down Expand Up @@ -49,5 +55,13 @@ def test_merge_sections():
merge_sections(system_3, system_2)
assert system_3.components[0].name == 'Cu'

def test_to_pint_quantity():
assert to_pint_quantity(1, 'mA') == 1 * ureg.mA
assert str(to_pint_quantity(np.asarray([1., 2.]), 'm')) \
== str(np.asarray([1., 2.]) * ureg.m)
assert str(to_pint_quantity(np.asarray([1., 2.]), '')) \
== str(np.asarray([1., 2.]))
assert to_pint_quantity('Copper', '') == 'Copper'

if __name__ == '__main__':
test_merge_sections()

0 comments on commit 2ed41c4

Please sign in to comment.