Skip to content

Commit

Permalink
Minor upgrade of the prodixy_xy (#7)
Browse files Browse the repository at this point in the history
* Don't use TypedDict in prodigy_xy.

There was a problem when a value that was expected to be float
 (like excitation energy) was stored in text file without dot
 and was recognised as int. Now all values are treated as float
 if they are numeric and only "curves_scan" and "values_scan"
 are converted to integer.

 Casting to TypedDict was not solving that problem.

* Add Marek Kopciuszynski to Additional contributors.
  • Loading branch information
mkopciuszynski authored Apr 23, 2024
1 parent 81946e1 commit 47e7c12
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 31 deletions.
1 change: 1 addition & 0 deletions AUTHORS.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,4 @@ rainsty
kcurrier6
Tommaso (Tommaso Pincelli)
cgfatuzzo (Claudia Fatuzzo)
Marek Kopciuszynski
42 changes: 11 additions & 31 deletions src/arpes/endstations/prodigy_xy.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@

import re
from pathlib import Path
from typing import TYPE_CHECKING, Literal, TypedDict, cast
from typing import TYPE_CHECKING

import numpy as np
import xarray as xr
Expand All @@ -35,32 +35,9 @@
MAP_DIMENSION = 3
SECOND_DIM_NAME = "nonenergy"

Measure_type = Literal["FAT", "SnapshotFAT", "Magnification"]
__all__ = ["load_xy"]


class ProdigyXYParams(TypedDict, total=False):
group: str
region: str
acquisition_date: str
analysis_method: str
analyzer: str
analyzer_lens: str
analyzer_slit: str
scan_mode: Measure_type
curves_scan: int
values_curve: int
dwell_time: float
excitation_energy: float
kinetic_energy: float
pass_energy: float
bias_voltage: float
detector_voltage: float
eff_workfunction: float
source: str
comment: str
ordinate_range: str


class ProdigyXY:
"""Class for Prodigy exported xy file.
Expand All @@ -79,7 +56,7 @@ class ProdigyXY:

def __init__(self, list_from_xy_file: list[str] | None = None) -> None:
"""Initialize."""
self.params: ProdigyXYParams = cast(ProdigyXYParams, {})
self.params: dict[str, str | float | int] = {}
self.axis_info: dict[str, tuple[NDArray[np.float_], str]] = {}
self.intensity: NDArray[np.float_]
if list_from_xy_file is not None:
Expand Down Expand Up @@ -203,7 +180,7 @@ def load_xy(
return data_array


def _parse_xy_head(xy_data_params: list[str]) -> ProdigyXYParams:
def _parse_xy_head(xy_data_params: list[str]) -> dict[str, str | int | float]:
"""Parse Common head part.
Parameters
Expand All @@ -228,8 +205,12 @@ def _parse_xy_head(xy_data_params: list[str]) -> ProdigyXYParams:
break
key, _, value = line[1:].partition(":")
temp_params[key.strip()] = _formatted_value(value)
common_params: ProdigyXYParams = cast(ProdigyXYParams, clean_keys(temp_params))
return common_params

temp_params = clean_keys(temp_params)
temp_params["curves_scan"] = int(temp_params["curves_scan"])
temp_params["values_curve"] = int(temp_params["values_curve"])

return temp_params


def _parse_xy_dims(xy_data_params: list[str]) -> dict[str, NDArray[np.float_]]:
Expand Down Expand Up @@ -282,10 +263,9 @@ def _parse_xy_dims(xy_data_params: list[str]) -> dict[str, NDArray[np.float_]]:
return clean_keys(xy_dims)


def _formatted_value(value: str) -> int | float | str:
def _formatted_value(value: str) -> float | str:
"""Convert string value to float if possible."""
value = value.strip()
if value.isnumeric():
return int(value)
try:
return float(value)
except ValueError:
Expand Down
2 changes: 2 additions & 0 deletions tests/test_prodigy_xy.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ def test_parameters(self, sample_xy: ProdigyXY) -> None:
assert sample_xy.axis_info["d1"][1] == "eV"
assert sample_xy.axis_info["d2"][1] == "nonenergy"
assert sample_xy.axis_info["d3"][1] == "polar"
assert isinstance(sample_xy.params["detector_voltage"], float)
assert isinstance(sample_xy.params["values_curve"], int)
np.testing.assert_almost_equal(sample_xy.params["eff_workfunction"], 4.31)
np.testing.assert_almost_equal(sample_xy.params["excitation_energy"], 21.2182)
np.testing.assert_almost_equal(sample_xy.axis_info["d1"][0][5], 20.080305)
Expand Down

0 comments on commit 47e7c12

Please sign in to comment.