Skip to content

Commit

Permalink
Add testing files
Browse files Browse the repository at this point in the history
  • Loading branch information
domna committed Feb 20, 2024
1 parent 2555d96 commit 73daf00
Show file tree
Hide file tree
Showing 5 changed files with 97 additions and 18 deletions.
2 changes: 1 addition & 1 deletion pynxtools_mpes/reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ def iterate_dictionary(dic, key_string):
"source_TYPE[source]/Pump": "source_TYPE[source_pump]",
"beam_TYPE[beam]/Probe": "beam_TYPE[beam_probe]",
"beam_TYPE[beam]/Pump": "beam_TYPE[beam_pump]",
"sample_history": "sample_history/notes/description",
"sample_history": "sample_history/notes",
"ELECTRONANALYSER[electronanalyser]/RESOLUTION[momentum_resolution]": (
"ELECTRONANALYSER[electronanalyser]/momentum_resolution"
),
Expand Down
2 changes: 0 additions & 2 deletions tests/data/ELN_metadata_example.yaml

This file was deleted.

13 changes: 6 additions & 7 deletions tests/data/config_file.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
"type": "estimated"
},
"RESOLUTION[momentum_resolution]": {
"resolution": "@attrs:metadata/instrument/analyzer/momentum_resolution",
"resolution": "@link:/entry/instrument/electronanalyser/momentum_resolution",
"resolution/@units": "1/angstrom",
"physical_quantity": "momentum",
"type": "estimated"
Expand Down Expand Up @@ -167,7 +167,7 @@
"mode": "Single Bunch",
"frequency": "@attrs:metadata/instrument/beam/probe/frequency",
"frequency/@units": "kHz",
"associated_beam": "@link:/entry/instrument/beam_probe"
"associated_beam": "/entry/instrument/beam_probe"
},
"/ENTRY[entry]/INSTRUMENT[instrument]/beam_TYPE[beam_probe]": {
"distance": 0.0,
Expand All @@ -182,7 +182,7 @@
"incident_polarization/@units": "V^2/mm^2",
"extent": "@attrs:metadata/instrument/beam/probe/extent",
"extent/@units": "µm",
"associated_source": "@link:/entry/instrument/source_probe"
"associated_source": "/entry/instrument/source_probe"
},
"/ENTRY[entry]/INSTRUMENT[instrument]/source_TYPE[source_pump]": {
"name": "OPCPA @ TR-ARPES @ FHI",
Expand All @@ -191,7 +191,7 @@
"mode": "Single Bunch",
"frequency": "@attrs:metadata/instrument/beam/pump/frequency",
"frequency/@units": "kHz",
"associated_beam": "@link:/entry/instrument/beam_pump"
"associated_beam": "/entry/instrument/beam_pump"
},
"/ENTRY[entry]/INSTRUMENT[instrument]/beam_TYPE[beam_pump]": {
"distance": 0.0,
Expand All @@ -214,7 +214,7 @@
"extent/@units": "µm",
"fluence": "@attrs:metadata/instrument/beam/pump/fluence",
"fluence/@units": "mJ/cm^2",
"associated_source": "@link:/entry/instrument/source_pump"
"associated_source": "/entry/instrument/source_pump"
},
"/ENTRY[entry]/INSTRUMENT[instrument]/MANIPULATOR[manipulator]": {
"temperature_sensor": {
Expand Down Expand Up @@ -262,8 +262,7 @@
},
"/ENTRY[entry]/SAMPLE[sample]": {
"preparation_date": "@attrs:metadata/sample/preparation_date",
"sample_history/notes/type": "text/plain",
"sample_history/notes/description": "@attrs:metadata/sample/sample_history",
"sample_history/notes": "@attrs:metadata/sample/sample_history",
"description": "@attrs:metadata/sample/chemical_formula",
"name": "@attrs:metadata/sample/chemical_formula",
"situation": "vacuum",
Expand Down
4 changes: 2 additions & 2 deletions tests/data/eln_data.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,14 @@ Instrument:
Source:
Probe:
frequency:
unit: kHz
unit: KHz
value: 500.0
photon_energy:
unit: eV
value: 21.7
Pump:
frequency:
unit: kHz
unit: KHz
value: 500.0
photon_energy:
unit: eV
Expand Down
94 changes: 88 additions & 6 deletions tests/test_reader.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,19 @@
"""
Basic example based test for the stm reader
"""
import logging
import os
import xml.etree.ElementTree as ET
from glob import glob
from pathlib import Path

import pynxtools.dataconverter.convert as dataconverter
from pynxtools.dataconverter.convert import get_reader
from pynxtools.dataconverter.helpers import (
generate_template_from_nxdl,
validate_data_dict,
)
from pynxtools.dataconverter.template import Template
from pynxtools.nexus import nexus # noqa: E402 # noqa: E402
from pynxtools.nexus.nxdl_utils import get_nexus_definitions_path

from pynxtools_mpes.reader import MPESReader
Expand All @@ -24,8 +28,11 @@ def test_example_data():

def_dir = get_nexus_definitions_path()

data_dir = os.path.join(os.path.dirname(__file__), "data")
input_files = sorted(glob(os.path.join(data_dir, "*")))
data_dir = Path(__file__).parent / "data"
input_files = (
str(data_dir / "config_file.json"),
str(data_dir / "xarray_saved_small_calibration.h5"),
)

for supported_nxdl in reader.supported_nxdls:
nxdl_file = os.path.join(
Expand All @@ -36,9 +43,84 @@ def test_example_data():
template = Template()
generate_template_from_nxdl(root, template)

read_data = reader().read(
template=Template(template), file_paths=tuple(input_files)
)
read_data = reader().read(template=Template(template), file_paths=input_files)

assert isinstance(read_data, Template)
assert validate_data_dict(template, read_data, root)


def test_mpes_writing(tmp_path):
"""Check if mpes example can be reproduced"""
# dataconverter
dir_path = Path(__file__).parent / "data"
dataconverter.convert(
(
str(dir_path / "xarray_saved_small_calibration.h5"),
str(dir_path / "config_file.json"),
),
"mpes",
"NXmpes",
os.path.join(tmp_path, "mpes.small_test.nxs"),
False,
False,
)
# check generated nexus file
test_data = os.path.join(tmp_path, "mpes.small_test.nxs")
logger = logging.getLogger(__name__)
logger.setLevel(logging.DEBUG)
handler = logging.FileHandler(os.path.join(tmp_path, "mpes_test.log"), "w")
formatter = logging.Formatter("%(levelname)s - %(message)s")
handler.setLevel(logging.DEBUG)
handler.setFormatter(formatter)
logger.addHandler(handler)
nexus_helper = nexus.HandleNexus(logger, test_data, None, None)
nexus_helper.process_nexus_master_file(None)
with open(
os.path.join(tmp_path, "mpes_test.log"), "r", encoding="utf-8"
) as logfile:
log = logfile.readlines()
with open(dir_path / "Ref_nexus_mpes.log", "r", encoding="utf-8") as logfile:
ref_log = logfile.readlines()
assert log == ref_log


def test_shows_correct_warnings():
"""
Checks whether the read function generates the correct warnings.
"""
def_dir = get_nexus_definitions_path()

data_dir = Path(__file__).parent / "data"
input_files = (
str(data_dir / "config_file.json"),
str(data_dir / "xarray_saved_small_calibration.h5"),
)
nxdl_file = os.path.join(def_dir, "contributed_definitions", "NXmpes.nxdl.xml")

root = ET.parse(nxdl_file).getroot()
template = Template()
generate_template_from_nxdl(root, template)

read_data = get_reader("mpes")().read(
template=Template(template), file_paths=tuple(input_files)
)

assert validate_data_dict(template, read_data, root)
assert not list(read_data.undocumented.keys())


def test_eln_data(tmp_path):
"""Check if the subsections in the eln_data.yml file work."""
dir_path = Path(__file__).parent / "data"
dataconverter.convert(
(
str(dir_path / "xarray_saved_small_calibration.h5"),
str(dir_path / "config_file.json"),
str(dir_path / "eln_data.yaml"),
),
"mpes",
"NXmpes",
os.path.join(tmp_path, "mpes.small_test.nxs"),
False,
False,
)

0 comments on commit 73daf00

Please sign in to comment.