Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove mappings #25

Merged
merged 6 commits into from
Aug 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 0 additions & 64 deletions pynxtools_mpes/mappings.py

This file was deleted.

22 changes: 6 additions & 16 deletions pynxtools_mpes/reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,6 @@
from pynxtools.dataconverter.readers.multi.reader import MultiFormatReader
from pynxtools.dataconverter.readers.utils import parse_yml

from pynxtools_mpes.mappings import CONVERT_DICT, DEFAULT_UNITS, REPLACE_NESTED

logger = logging.getLogger("pynxtools")


Expand Down Expand Up @@ -121,15 +119,15 @@ def h5_to_xarray(faddr: str, mode: str = "r") -> xr.DataArray:
f"ax{axis}"
].attrs["unit"]
except (KeyError, TypeError):
xarray[bin_names[axis]].attrs["unit"] = DEFAULT_UNITS[bin_names[axis]]
xarray[bin_names[axis]].attrs["unit"] = ""
try:
xarray.attrs["units"] = h5_file["binned"]["BinnedData"].attrs["units"]
xarray.attrs["long_name"] = h5_file["binned"]["BinnedData"].attrs[
"long_name"
]
except (KeyError, TypeError):
xarray.attrs["units"] = "counts"
xarray.attrs["long_name"] = "photoelectron counts"
xarray.attrs["units"] = ""
xarray.attrs["long_name"] = ""

if metadata is not None:
xarray.attrs["metadata"] = metadata
Expand All @@ -146,8 +144,7 @@ def iterate_dictionary(dic, key_string):
if not len(keys) == 1:
return iterate_dictionary(dic[keys[0]], keys[1])
else:
raise KeyError
return None
return None


def rgetattr(obj, attr):
Expand Down Expand Up @@ -196,8 +193,6 @@ def set_config_file(self, file_path: str) -> Dict[str, Any]:
def handle_eln_file(self, file_path: str) -> Dict[str, Any]:
self.eln_data = parse_yml(
file_path,
convert_dict=CONVERT_DICT,
replace_nested=REPLACE_NESTED,
parent_key="/ENTRY",
)

Expand All @@ -208,9 +203,7 @@ def get_eln_data(self, key: str, path: str) -> Any:
if self.eln_data is None:
return None

return self.eln_data.get(
key.replace(f"/ENTRY[{self.callbacks.entry_name}]", "/ENTRY")
)
return self.eln_data.get(path)

def handle_objects(self, objects: Tuple[Any]) -> Dict[str, Any]:
if isinstance(objects, xr.DataArray):
Expand Down Expand Up @@ -253,10 +246,7 @@ def get_data_dims(self, key: str, path: str) -> List[str]:
return list(map(str, self.data_xarray.dims))

def get_attr(self, key: str, path: str) -> Any:
try:
return iterate_dictionary(self.data_xarray.attrs, path)
except KeyError:
logger.info(f"Path {path} not found. Skipping the entry.")
return iterate_dictionary(self.data_xarray.attrs, path)


READER = MPESReader
8 changes: 7 additions & 1 deletion scripts/regenerate_examples.sh
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,13 @@ function update_mpes_example {
dataconverter xarray_saved_small_calibration.h5 config_file.json --reader $READER --nxdl $NXDL --output example.nxs
}

function update_mpes_eln_example {
echo "Update mpes example with eln file"
dataconverter xarray_saved_small_calibration.h5 config_file.json eln_data.yaml --reader $READER --nxdl $NXDL --output example_eln.nxs
}

project_dir=$(dirname $(dirname $(realpath $0)))
cd $project_dir/tests/data

update_mpes_example
update_mpes_example
update_mpes_eln_example
6 changes: 3 additions & 3 deletions tests/data/config_file.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"/@default": "entry",
"/ENTRY/@default": "data",
"/ENTRY/definition": "NXmpes",
"/ENTRY/title": "@attrs:metadata/entry_title",
"/ENTRY/title": "['@eln:/ENTRY/title', '@attrs:metadata/entry_title']",
"/ENTRY/start_time": "@attrs:metadata/timing/acquisition_start",
"/ENTRY/experiment_institution": "Fritz Haber Institute - Max Planck Society",
"/ENTRY/experiment_facility": "Time Resolved ARPES",
Expand All @@ -24,8 +24,8 @@
"name": "Time-of-flight momentum microscope equipped delay line detector, at the endstation of the high rep-rate HHG source at FHI",
"name/@short_name": "TR-ARPES @ FHI",
"energy_resolution": {
"resolution": 140.0,
"resolution/@units": "meV",
"resolution": "['@eln:/ENTRY/Instrument/energy_resolution', '140.0']",
"resolution/@units": "['@eln:/ENTRY/Instrument/energy_resolution/@units', 'meV']",
"physical_quantity": "energy",
"type": "estimated"
},
Expand Down
111 changes: 5 additions & 106 deletions tests/data/eln_data.yaml
Original file line number Diff line number Diff line change
@@ -1,109 +1,8 @@
title: Valence Band Dynamics - 1030 nm linear p-polarized pump, 0.6 mJ/cm2 absorbed fluence
title: Title from ELN file
Instrument:
energy_resolution:
unit: meV
value: 140.0
momentum_resolution:
unit: 1/angstrom
value: 0.08
temporal_resolution:
unit: fs
value: 35.0
Analyzer:
energy_resolution:
unit: eV
value: 110.0
momentum_resolution:
unit: 1/angstrom
value: 0.08
slow_axes: delay
spatial_resolution:
unit: µm
value: 10.0
Manipulator:
sample_temperature:
unit: K
value: 300.0
Source:
Probe:
frequency:
unit: KHz
value: 500.0
photon_energy:
unit: eV
value: 21.7
Pump:
frequency:
unit: KHz
value: 500.0
photon_energy:
unit: eV
value: 1.55
Beam:
Probe:
extent:
unit: µm
value:
- 80.0
- 80.0
incident_energy:
unit: eV
value: 21.7
incident_energy_spread:
unit: eV
value: 0.11
incident_polarization:
- 1
- 1
- 0
- 0
pulse_duration:
unit: fs
value: 20.0
Pump:
extent:
unit: µm
value:
- 230.0
- 265.0
incident_energy:
unit: eV
value: 1.55
incident_energy_spread:
unit: eV
value: 0.08
incident_polarization:
- 1
- -1
- 0
- 0
incident_wavelength:
unit: nm
value: 800.0
average_power:
unit: mW
value: 300.0
pulse_energy:
unit: µJ
value: 0.6
fluence:
unit: mJ / cm ** 2
value: 0.15
pulse_duration:
unit: fs
value: 35.0
unit: eV
value: 0.14
Sample:
chemical_formula: WSe2
description: Sample
name: WSe2 Single Crystal
preparation_date: "2019-01-13T09:00:00+00:00"
pressure:
unit: bar
value: 5.0e-14
sample_history: Cleaved
User:
address: Faradayweg 4-6, 14915 Berlin
affiliation: Fritz Haber Institute of the Max Planck Society
email: [email protected]
name: Julian Maklar
role: Principal Investigator
name: My ELN sample name
preparation_date: "2019-01-13T09:00:00+00:00"
Binary file modified tests/data/example.nxs
Binary file not shown.
Binary file added tests/data/example_eln.nxs
Binary file not shown.
Binary file modified tests/data/xarray_saved_small_calibration.h5
Binary file not shown.
27 changes: 15 additions & 12 deletions tests/test_reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,8 @@
Basic example based test for the stm reader
"""

import os
from pathlib import Path

import pynxtools.dataconverter.convert as dataconverter
from pynxtools.testing.nexus_conversion import ReaderTest


Expand All @@ -30,18 +28,23 @@ def test_nexus_conversion(caplog, tmp_path):
test.check_reproducibility_of_nexus()


def test_eln_data(tmp_path):
"""Check if the subsections in the eln_data.yml file work."""
def test_conversion_w_eln_data(caplog, tmp_path):
"""
Tests the conversion with additional ELN data
"""
caplog.clear()
dir_path = Path(__file__).parent / "data"
dataconverter.convert(
input_file=(
test = ReaderTest(
nxdl="NXmpes",
reader_name="mpes",
files_or_dir=[
str(dir_path / "xarray_saved_small_calibration.h5"),
str(dir_path / "config_file.json"),
str(dir_path / "eln_data.yaml"),
),
reader="mpes",
nxdl="NXmpes",
output=os.path.join(tmp_path, "mpes.small_test.nxs"),
skip_verify=False,
ignore_undocumented=False,
str(dir_path / "example_eln.nxs"),
],
tmp_path=tmp_path,
caplog=caplog,
)
test.convert_to_nexus(caplog_level="WARNING", ignore_undocumented=False)
test.check_reproducibility_of_nexus()