Skip to content

Commit

Permalink
Merge pull request #10 from FAIRmat-NFDI/9-xps-vamas-sub-reader-add-r…
Browse files Browse the repository at this point in the history
…egular-parsing-enhance-data-extraction

Vamas sub-reader: enhance data extraction, add IRREGULAR parsing
  • Loading branch information
lukaspie authored Mar 15, 2024
2 parents 6603d86 + 2728461 commit ac2768b
Show file tree
Hide file tree
Showing 14 changed files with 8,445 additions and 778 deletions.
576 changes: 290 additions & 286 deletions pynxtools_xps/config/config_vms.json

Large diffs are not rendered by default.

52 changes: 48 additions & 4 deletions pynxtools_xps/reader_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,44 @@
# See the License for the specific language governing permissions and
# limitations under the License.
#
import re
from abc import ABC, abstractmethod
from typing import Any, Dict, List, Union
from pathlib import Path
from scipy.interpolate import interp1d
import numpy as np

from dataclasses import dataclass


@dataclass
class XpsDataclass:
"""Generic class to hold a data model and a type validation method."""

def validate_types(self):
ret = True
for field_name, field_def in self.__dataclass_fields__.items():
actual_type = type(getattr(self, field_name))
if actual_type != field_def.type:
print(f"\t{field_name}: '{actual_type}' instead of '{field_def.type}'")
ret = False
return ret

def __post_init__(self):
if not self.validate_types():
raise ValueError("Wrong types")

def dict(self):
return self.__dict__.copy()


class XPSMapper(ABC):
"""Abstract base class from mapping from a parser to NXmpes template"""

def __init__(self):
self.file = None
self.raw_data: list = []
self._xps_dict: dict = {}
self.file: Union[str, Path] = ""
self.raw_data: List[str] = []
self._xps_dict: Dict[str, Any] = {}
self._root_path = "/ENTRY[entry]"

self.parser = None
Expand Down Expand Up @@ -77,6 +103,24 @@ def construct_data(self):
"""


def to_snake_case(str_value):
"""Convert a string to snake_case."""
if " " in str_value:
return "_".join(word.lower() for word in str_value.split())
return convert_pascal_to_snake(str_value)


def convert_pascal_to_snake(str_value):
"""Convert pascal case text to snake case."""
pattern = re.compile(r"(?<!^)(?=[A-Z])")
return pattern.sub("_", str_value).lower()


def convert_snake_to_pascal(str_value):
"""Convert snakecase text to pascal case."""
return str_value.replace("_", " ").title().replace(" ", "")


def safe_arange_with_edges(start, stop, step):
"""
In order to avoid float point errors in the division by step.
Expand All @@ -102,7 +146,7 @@ def safe_arange_with_edges(start, stop, step):

def check_uniform_step_width(lst):
"""
Check to see if a non-uniform step width is used in an lst
Check to see if a non-uniform step width is used in an list.
Parameters
----------
Expand Down
4 changes: 2 additions & 2 deletions pynxtools_xps/sle/sle_specs.py
Original file line number Diff line number Diff line change
Expand Up @@ -198,8 +198,8 @@ def _update_xps_dict_with_spectrum(self, spectrum, key_map):
"""
# pylint: disable=too-many-locals,duplicate-code
group_parent = f'{self._root_path}/RegionGroup_{spectrum["group_name"]}'
region_parent = f'{group_parent}/regions/RegionData_{spectrum["spectrum_type"]}'
group_parent = f'{self._root_path}/Group_{spectrum["group_name"]}'
region_parent = f'{group_parent}/regions/Region_{spectrum["spectrum_type"]}'
instrument_parent = f"{region_parent}/instrument"
analyser_parent = f"{instrument_parent}/analyser"

Expand Down
4 changes: 2 additions & 2 deletions pynxtools_xps/txt/txt_scienta.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,8 +130,8 @@ def _update_xps_dict_with_spectrum(self, spectrum, key_map):
"""
# pylint: disable=too-many-locals,duplicate-code
group_parent = f'{self._root_path}/RegionGroup_{spectrum["spectrum_type"]}'
region_parent = f'{group_parent}/regions/RegionData_{spectrum["region_name"]}'
group_parent = f'{self._root_path}/Region_{spectrum["spectrum_type"]}'
region_parent = f'{group_parent}/regions/Region_{spectrum["region_name"]}'
file_parent = f"{region_parent}/file_info"
instrument_parent = f"{region_parent}/instrument"
analyser_parent = f"{instrument_parent}/analyser"
Expand Down
4 changes: 2 additions & 2 deletions pynxtools_xps/txt/txt_vamas_export.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,8 +130,8 @@ def _update_xps_dict_with_spectrum(self, spectrum, key_map):
"""
# pylint: disable=too-many-locals,duplicate-code
group_parent = f'{self._root_path}/RegionGroup_{spectrum["group_name"]}'
region_parent = f'{group_parent}/regions/RegionData_{spectrum["spectrum_type"]}'
group_parent = f'{self._root_path}/Group_{spectrum["group_name"]}'
region_parent = f'{group_parent}/regions/Region_{spectrum["spectrum_type"]}'
file_parent = f"{region_parent}/file_info"
instrument_parent = f"{region_parent}/instrument"
analyser_parent = f"{instrument_parent}/analyser"
Expand Down
Loading

0 comments on commit ac2768b

Please sign in to comment.