Skip to content

Commit

Permalink
unit parsing, datatype checks
Browse files Browse the repository at this point in the history
  • Loading branch information
lukaspie committed Mar 26, 2024
1 parent adfbaf4 commit 61d6fde
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 55 deletions.
120 changes: 66 additions & 54 deletions pynxtools_xps/scienta/scienta_txt.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,37 @@

from pynxtools_xps.scienta.scienta_txt_data_model import ScientaHeader, ScientaRegion

KEY_MAP: Dict[str, str] = {
"number_of_regions": "no_of_regions",
"version": "software_version",
"dimension_name": "energy_type",
"dimension_size": "energy_size",
"dimension_scale": "energy_axis",
"number_of_sweeps": "no_of_scans",
"low_energy": "start_energy",
"high_energy": "stop_energy",
"energy_step": "step_size",
"step_time": "dwell_time",
"detector_first_x-_channel": "detector_first_x_channel",
"detector_last_x-_channel": "detector_last_x_channel",
"detector_first_y-_channel": "detector_first_y_channel",
"detector_last_y-_channel": "detector_last_y_channel",
"file": "data_file",
"sequence": "sequence_file",
"spectrum_name": "spectrum_type",
"instrument": "instrument_name",
"location": "vendor",
"user": "user_name",
"sample": "sample_name",
"comments": "spectrum_comment",
"date": "start_date",
"time": "start_time",
"name": "energy_type",
"size": "energy_size",
"scale": "energy_axis",
"Transmission": "fixed analyzer transmission",
}


class TxtMapperScienta(XPSMapper):
"""
Expand Down Expand Up @@ -78,14 +109,18 @@ def construct_data(self):
"vendor",
],
"source": [],
"beam": ["excitation_energy"],
"beam": [
"excitation_energy",
"excitation_energy/@units",
],
"analyser": [],
"collectioncolumn": [
"lens_mode",
],
"energydispersion": [
"acquisition_mode",
"pass_energy",
"pass_energy/@units",
],
"detector": [
"detector_first_x_channel",
Expand All @@ -94,18 +129,28 @@ def construct_data(self):
"detector_last_y_channel",
"detector_mode",
"dwell_time",
"dwell_time/@units",
"time_per_spectrum_channel",
"time_per_spectrum_channel/@units",
],
"manipulator": [],
"calibration": [],
"sample": ["sample_name"],
"data": [
"x_units",
"energy_unit",
"energy/@units",
"energy_axis",
"energy_type",
"center_energy",
"center_energy/@units",
"start_energy",
"start_energy/@units",
"stop_energy",
"stop_energy/@units",
"step_size",
"step_size/@units",
],
"region": [
"center_energy",
"energy_scale",
"energy_size",
"no_of_scans",
Expand Down Expand Up @@ -177,15 +222,15 @@ def _update_xps_dict_with_spectrum(self, spectrum, key_map):
detector_data_key = f'{path_map["detector"]}/{detector_data_key_child}/counts'

# Write raw data to detector.
self._xps_dict[detector_data_key] = spectrum["data"]["y"]
self._xps_dict[detector_data_key] = spectrum["data"]["intensity"]

# If multiple spectra exist to entry, only create a new
# xr.Dataset if the entry occurs for the first time.
if entry not in self._xps_dict["data"]:
self._xps_dict["data"][entry] = xr.Dataset()

energy = np.array(spectrum["data"]["x"])
intensity = spectrum["data"]["y"]
energy = np.array(spectrum["data"]["energy"])
intensity = spectrum["data"]["intensity"]

# Write to data in order: scan, cycle, channel

Expand Down Expand Up @@ -217,47 +262,6 @@ def _update_xps_dict_with_spectrum(self, spectrum, key_map):
)


# =============================================================================
# dimension_1_name
# dimension_1_size
# dimension_1_size
# dimension_1_scale
# dimension_1_scale
# =============================================================================

KEY_MAP = {
"number_of_regions": "no_of_regions",
"version": "software_version",
"dimension_name": "energy_type",
"dimension_size": "energy_size",
"dimension_scale": "energy_axis",
"number_of_sweeps": "no_of_scans",
"energy_unit": "x_units",
"low_energy": "start_energy",
"high_energy": "stop_energy",
"energy_step": "step_size",
"step_time": "dwell_time",
"detector_first_x-_channel": "detector_first_x_channel",
"detector_last_x-_channel": "detector_last_x_channel",
"detector_first_y-_channel": "detector_first_y_channel",
"detector_last_y-_channel": "detector_last_y_channel",
"file": "data_file",
"sequence": "sequence_file",
"spectrum_name": "spectrum_type",
"instrument": "instrument_name",
"location": "vendor",
"user": "user_name",
"sample": "sample_name",
"comments": "spectrum_comment",
"date": "start_date",
"time": "start_time",
"name": "energy_type",
"size": "energy_size",
"scale": "energy_axis",
"Transmission": "fixed analyzer transmission",
}


class ScientaTxtHelper:
"""Parser for Scienta TXT exports."""

Expand Down Expand Up @@ -380,8 +384,8 @@ def _parse_region(self, region_id):
key = self._re_map_keys(key_part)

value = self._re_map_values(key, value)
if self._check_valid_value(value):
setattr(region, key, value)
if self._check_valid_value(value):
setattr(region, key, value)

if begin_info:
# Read instrument meta data for this region.
Expand All @@ -396,7 +400,7 @@ def _parse_region(self, region_id):
energies.append(energy)
intensities.append(intensity)

region.data = {"x": np.array(intensities), "y": np.array(energies)}
region.data = {"energy": np.array(energies), "intensity": np.array(intensities)}

# Convert date and time to ISO8601 date time.
region.time_stamp = _construct_date_time(region.start_date, region.start_time)
Expand All @@ -405,6 +409,11 @@ def _parse_region(self, region_id):

region_dict = {**self.header.dict(), **region.dict()}

for key in region_dict.copy().keys():
if "_units" in key:
new_key = key.replace("_units", "/@units")
region_dict[new_key] = region_dict.pop(key)

self.spectra.append(region_dict)

def _check_valid_value(self, value):
Expand All @@ -422,7 +431,10 @@ def _check_valid_value(self, value):
True if the string or np.ndarray is not empty.
"""
if isinstance(value, str) and value:
for datatype in [str, int, float]:
if isinstance(value, datatype) and value:
return True
if isinstance(value, bool):
return True
if isinstance(value, np.ndarray) and value.size != 0:
return True
Expand Down Expand Up @@ -498,7 +510,7 @@ def _re_map_values(self, input_key: str, value: str):
"detector_first_y_channel": int,
"detector_last_y_channel": int,
"time_per_spectrum_channel": float,
"x_units": _change_energy_type,
"energy_unit": _change_energy_type,
"energy_axis": _separate_dimension_scale,
"energy_scale": _change_energy_type,
"acquisition_mode": _change_scan_mode,
Expand Down Expand Up @@ -536,9 +548,9 @@ def _change_energy_type(energy_type: str):
"""
if "Binding" in energy_type:
return "binding energy"
return "binding"
if "Kinetic" in energy_type:
return "kinetic energy"
return "kinetic"
return None


Expand Down
2 changes: 1 addition & 1 deletion pynxtools_xps/scienta/scienta_txt_data_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ class ScientaRegion(XpsDataclass):
dim_energy_type: str = ""
energy_type: str = "binding" # energy_scale
energy_size: int = 0
energy_units: int = 0
energy_units: str = "eV"
energy_axis: np.ndarray = field(default_factory=lambda: np.zeros(0))
lens_mode: str = ""
pass_energy: float = 0.0
Expand Down

0 comments on commit 61d6fde

Please sign in to comment.