Skip to content

Commit 3258f88

Browse files
authored
Merge pull request #93 from FAIRmat-NFDI/helper-update
Update helper functions and streamline formatting
2 parents 432a4a3 + fc08540 commit 3258f88

32 files changed

+586
-203
lines changed

scripts/generate_ref_comments_jsons.py

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,3 @@
1-
"""
2-
Parser for reading XPS (X-ray Photoelectron Spectroscopy) data from
3-
Phi PHI VersaProbe 4 instruments (.spe or .pro format), to be passed to
4-
mpes nxdl (NeXus Definition Language) template.
5-
"""
61
# Copyright The NOMAD Authors.
72
#
83
# This file is part of NOMAD. See https://nomad-lab.eu for further info.
@@ -20,7 +15,9 @@
2015
# limitations under the License.
2116
#
2217

23-
# pylint: disable=too-many-lines,too-many-instance-attributes
18+
"""
19+
Regenerate reference JSON files for testing the VAMAS comment extraction.
20+
"""
2421

2522
import os
2623
import json
@@ -66,7 +63,7 @@ def generate_ref_comment_jsons():
6663
comments = handle_comments(comment_lines, comment_type=comment_type)
6764

6865
for key, val in comments.items():
69-
if type(val) == np.ndarray:
66+
if isinstance(val, np.ndarray):
7067
comments[key] = val.tolist()
7168

7269
with open(ref_json_filepath, "w") as json_file:

scripts/regenerate_ref_comments_jsons.sh

Lines changed: 0 additions & 2 deletions
This file was deleted.

src/pynxtools_xps/kratos/kratos_data_model.py

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
1-
"""
2-
Data model for Kratos spectrometers for export to CasaXPS.
3-
"""
41
# Copyright The NOMAD Authors.
52
#
63
# This file is part of NOMAD. See https://nomad-lab.eu for further info.
@@ -17,10 +14,11 @@
1714
# See the License for the specific language governing permissions and
1815
# limitations under the License.
1916
#
20-
# pylint: disable=too-many-instance-attributes
17+
"""
18+
Data model for Kratos spectrometers for export to CasaXPS.
19+
"""
2120

22-
from dataclasses import dataclass
23-
from dataclasses import field
21+
from dataclasses import dataclass, field
2422

2523
from pynxtools_xps.reader_utils import XpsDataclass
2624

src/pynxtools_xps/kratos/metadata_kratos.py

Lines changed: 15 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,3 @@
1-
"""
2-
Parser for reading XPS (X-ray Photoelectron Spectroscopy) metadata from
3-
Kratos instruments (currently only after exporting to .vms format), to be passed to
4-
mpes nxdl (NeXus Definition Language) template.
5-
"""
61
# Copyright The NOMAD Authors.
72
#
83
# This file is part of NOMAD. See https://nomad-lab.eu for further info.
@@ -19,21 +14,24 @@
1914
# See the License for the specific language governing permissions and
2015
# limitations under the License.
2116
#
22-
23-
# pylint: disable=too-many-lines,too-many-instance-attributes
17+
"""
18+
Parser for reading XPS (X-ray Photoelectron Spectroscopy) metadata from
19+
Kratos instruments (currently only after exporting to .vms format), to be
20+
passed to MPES nxdl (NeXus Definition Language) template.
21+
"""
2422

2523
import re
26-
import datetime
2724
from typing import Any, Dict, List, Union, Tuple
2825
from pathlib import Path
2926

3027
from pynxtools_xps.reader_utils import (
3128
convert_pascal_to_snake,
29+
extract_unit,
3230
)
3331

3432
from pynxtools_xps.value_mappers import (
3533
convert_bool,
36-
convert_units,
34+
parse_datetime,
3735
)
3836

3937
from pynxtools_xps.kratos.kratos_data_model import (
@@ -72,6 +70,8 @@
7270
"sample_tilt": "degree",
7371
}
7472

73+
POSSIBLE_DATE_FORMATS: List[str] = ["%d.%m.%Y %H:%M", "%d/%m/%Y %H:%M"]
74+
7575

7676
class KratosParser:
7777
"""
@@ -89,7 +89,7 @@ def __init__(self):
8989
self.metadata = KratosMetadata()
9090

9191
self.value_function_map: Dict[str, Any] = {
92-
"date_created": _parse_datetime,
92+
"date_created": parse_datetime,
9393
"description": _convert_description,
9494
"charge_neutraliser": convert_bool,
9595
"deflection": _convert_xray_deflection,
@@ -150,7 +150,7 @@ def parse_header_into_metadata(self, header: List[str]):
150150
field_type = type(getattr(self.metadata, key))
151151

152152
if key in KEYS_WITH_UNITS:
153-
value, unit = self.extract_unit(key, value)
153+
value, unit = extract_unit(key, value, UNIT_MISSING) # type: ignore[assignment]
154154
setattr(self.metadata, f"{key}_units", unit)
155155

156156
value = self.map_values(key, value, field_type)
@@ -159,46 +159,6 @@ def parse_header_into_metadata(self, header: List[str]):
159159

160160
self.metadata.validate_types()
161161

162-
def extract_unit(self, key: str, value: str) -> Tuple[Any, str]:
163-
"""
164-
Extract units for the metadata containing unit information.
165-
166-
Example:
167-
analyser_work_function: 4.506eV
168-
-> analyser_work_function: 4.506,
169-
analyser_work_function_units: eV,
170-
171-
Parameters
172-
----------
173-
key : str
174-
Key of the associated value.
175-
value : str
176-
Combined unit and value information.
177-
178-
Returns
179-
-------
180-
value :
181-
value with units.
182-
unit : str
183-
Associated unit.
184-
185-
"""
186-
187-
pattern = re.compile(r"([-+]?\d*\.?\d+(?:[eE][-+]?\d+)?)([a-zA-Z]+)")
188-
match = pattern.match(value)
189-
190-
if match:
191-
value, unit = match.groups()
192-
else:
193-
unit = ""
194-
195-
unit = convert_units(unit)
196-
197-
if key in UNIT_MISSING:
198-
unit = UNIT_MISSING[key]
199-
200-
return value, unit
201-
202162
def map_values(self, key: str, value, field_type):
203163
"""
204164
Map values to corresponding structure and field type.
@@ -221,7 +181,10 @@ def map_values(self, key: str, value, field_type):
221181

222182
if key in self.value_function_map:
223183
map_fn = self.value_function_map[key]
224-
value = map_fn(value)
184+
if key == "date_created":
185+
value = map_fn(value, POSSIBLE_DATE_FORMATS)
186+
else:
187+
value = map_fn(value)
225188
return field_type(value)
226189

227190
def flatten_metadata(self) -> Dict[str, Any]:
@@ -268,34 +231,6 @@ def setup_unit(flattened_dict: Dict[str, Any], unit_key: str):
268231
return flattened_dict
269232

270233

271-
def _parse_datetime(datetime_string: str) -> str:
272-
"""
273-
Convert the native time format to the datetime string
274-
in the ISO 8601 format: '%Y-%b-%dT%H:%M:%S.%fZ'.
275-
276-
Parameters
277-
----------
278-
value : str
279-
String representation of the date in the format
280-
"%Y-%m-%d", "%m/%d/%Y" or "%H:%M:%S", "%I:%M:%S %p".
281-
282-
Returns
283-
-------
284-
date_object : str
285-
Datetime in ISO8601 format.
286-
287-
"""
288-
possible_date_formats = ["%d.%m.%Y %H:%M", "%d/%m/%Y %H:%M"]
289-
for date_fmt in possible_date_formats:
290-
try:
291-
datetime_obj = datetime.datetime.strptime(datetime_string, date_fmt)
292-
return datetime_obj.astimezone().isoformat()
293-
294-
except ValueError:
295-
continue
296-
raise ValueError("Date and time could not be converted to ISO 8601 format.")
297-
298-
299234
def _convert_description(value: str) -> Dict[str, Any]:
300235
"""Map all items in description to a dictionary."""
301236
pattern = re.compile(

src/pynxtools_xps/mkdocs.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,20 @@
1+
#
2+
# Copyright The NOMAD Authors.
3+
#
4+
# This file is part of NOMAD. See https://nomad-lab.eu for further info.
5+
#
6+
# Licensed under the Apache License, Version 2.0 (the "License");
7+
# you may not use this file except in compliance with the License.
8+
# You may obtain a copy of the License at
9+
#
10+
# http://www.apache.org/licenses/LICENSE-2.0
11+
#
12+
# Unless required by applicable law or agreed to in writing, software
13+
# distributed under the License is distributed on an "AS IS" BASIS,
14+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
# See the License for the specific language governing permissions and
16+
# limitations under the License.
17+
#
118
"""
219
MKdocs macros for the documentation
320
"""

src/pynxtools_xps/phi/phi_data_model.py

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
1-
"""
2-
Data model for Phi VersaProbe 4 software (version SS 3.3.3.2).
3-
"""
41
# Copyright The NOMAD Authors.
52
#
63
# This file is part of NOMAD. See https://nomad-lab.eu for further info.
@@ -17,10 +14,11 @@
1714
# See the License for the specific language governing permissions and
1815
# limitations under the License.
1916
#
20-
# pylint: disable=too-many-instance-attributes
17+
"""
18+
Data model for Phi VersaProbe 4 software (version SS 3.3.3.2).
19+
"""
2120

22-
from dataclasses import dataclass
23-
from dataclasses import field
21+
from dataclasses import dataclass, field
2422

2523
import numpy as np
2624

src/pynxtools_xps/phi/spe_pro_phi.py

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,3 @@
1-
"""
2-
Parser for reading XPS (X-ray Photoelectron Spectroscopy) data from
3-
Phi PHI VersaProbe 4 instruments (.spe or .pro format), to be passed to
4-
mpes nxdl (NeXus Definition Language) template.
5-
"""
61
# Copyright The NOMAD Authors.
72
#
83
# This file is part of NOMAD. See https://nomad-lab.eu for further info.
@@ -19,8 +14,11 @@
1914
# See the License for the specific language governing permissions and
2015
# limitations under the License.
2116
#
22-
23-
# pylint: disable=too-many-lines,too-many-instance-attributes
17+
"""
18+
Parser for reading XPS (X-ray Photoelectron Spectroscopy) data from
19+
Phi PHI VersaProbe 4 instruments (.spe or .pro format), to be passed to
20+
MPES nxdl (NeXus Definition Language) template.
21+
"""
2422

2523
import re
2624
import warnings

src/pynxtools_xps/reader.py

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,3 @@
1-
"""
2-
A generic reader for loading XPS (X-ray Photoelectron Spectroscopy) data
3-
file into mpes nxdl (NeXus Definition Language) template.
4-
"""
51
# Copyright The NOMAD Authors.
62
#
73
# This file is part of NOMAD. See https://nomad-lab.eu for further info.
@@ -19,35 +15,36 @@
1915
# limitations under the License.
2016
#
2117
# pylint: disable=too-many-lines,too-few-public-methods
18+
"""
19+
A generic reader for loading XPS (X-ray Photoelectron Spectroscopy) data
20+
file into mpes nxdl (NeXus Definition Language) template.
21+
"""
2222

23-
import os
24-
import sys
25-
import re
26-
import datetime
2723
import copy
24+
import datetime
2825
import logging
26+
import os
27+
import re
28+
import sys
2929
from pathlib import Path
30-
from typing import Any, Dict, List, Tuple, Optional, Union, Set
31-
import numpy as np
30+
from typing import Any, Dict, List, Optional, Set, Tuple, Union
3231

32+
import numpy as np
3333
from pynxtools.dataconverter.helpers import extract_atom_types
34-
from pynxtools.dataconverter.readers.multi.reader import (
35-
MultiFormatReader,
36-
)
34+
from pynxtools.dataconverter.readers.multi.reader import MultiFormatReader
3735
from pynxtools.dataconverter.readers.utils import parse_yml
3836
from pynxtools.dataconverter.template import Template
3937

38+
from pynxtools_xps.reader_utils import check_units
39+
4040
from pynxtools_xps.phi.spe_pro_phi import MapperPhi
4141
from pynxtools_xps.scienta.scienta_reader import MapperScienta
4242
from pynxtools_xps.specs.sle.sle_specs import SleMapperSpecs
43-
from pynxtools_xps.specs.xy.xy_specs import XyMapperSpecs
4443
from pynxtools_xps.specs.xml.xml_specs import XmlMapperSpecs
44+
from pynxtools_xps.specs.xy.xy_specs import XyMapperSpecs
4545
from pynxtools_xps.vms.txt_vamas_export import TxtMapperVamasExport
4646
from pynxtools_xps.vms.vamas import VamasMapper
4747

48-
from pynxtools_xps.reader_utils import check_units
49-
50-
5148
logger = logging.getLogger(__name__)
5249
logger.setLevel(logging.INFO)
5350

0 commit comments

Comments
 (0)