Skip to content

Commit

Permalink
✨ Refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
arafune committed Oct 4, 2023
1 parent 952a46e commit fc59ff8
Show file tree
Hide file tree
Showing 34 changed files with 280 additions and 177 deletions.
30 changes: 30 additions & 0 deletions arpes/analysis/__init__.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,32 @@
"""Contains common ARPES analysis routines."""
from __future__ import annotations

from .band_analysis import fit_bands, fit_for_effective_mass
from .decomposition import (
decomposition_along,
factor_analysis_along,
ica_along,
nmf_along,
pca_along,
)
from .deconvolution import deconvolve_ice, deconvolve_rl, make_psf1d
from .derivative import curvature1d, curvature2d, d1_along_axis, d2_along_axis, minimum_gradient
from .filters import boxcar_filter, boxcar_filter_arr, gaussian_filter, gaussian_filter_arr
from .gap import determine_broadened_fermi_distribution, normalize_by_fermi_dirac, symmetrize
from .general import (
condense,
fit_fermi_edge,
normalize_by_fermi_distribution,
rebin,
symmetrize_axis,
)
from .kfermi import kfermi_from_mdcs
from .mask import apply_mask, apply_mask_to_coords, polys_to_mask, raw_poly_to_mask
from .pocket import (
curves_along_pocket,
edcs_along_pocket,
pocket_parameters,
radial_edcs_along_pocket,
)
from .tarpes import normalized_relative_change, relative_change
from .xps import approximate_core_levels
6 changes: 4 additions & 2 deletions arpes/analysis/derivative.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@
from arpes._typing import DataType

__all__ = (
"curvature",
"dn_along_axis",
"curvature2d",
"curvature1d",
"d2_along_axis",
"d1_along_axis",
"minimum_gradient",
Expand Down Expand Up @@ -136,6 +136,7 @@ def _gradient_modulus(data: DataType, *, delta: DELTA = 1) -> xr.DataArray:
return data_copy


@update_provenance("Maximum Curvature 1D")
def curvature1d(
arr: xr.DataArray,
dim: str = "",
Expand Down Expand Up @@ -184,6 +185,7 @@ def warpped_filter(arr: xr.DataArray):
return filterd_arr


@update_provenance("Maximum Curvature 2D")
def curvature2d(
arr: xr.DataArray,
directions: tuple[str, str] = ("phi", "eV"),
Expand Down
1 change: 1 addition & 0 deletions arpes/analysis/mask.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
if TYPE_CHECKING:
import xarray as xr
from _typeshed import Incomplete
from numpy.typing import NDArray

from arpes._typing import DataType

Expand Down
2 changes: 1 addition & 1 deletion arpes/analysis/pocket.py
Original file line number Diff line number Diff line change
Expand Up @@ -308,7 +308,7 @@ def edcs_along_pocket(
for kf, ss in zip(kfs, slices)
]

edcs = [data.S.select_around(l, radius=select_radius, fast=True) for l in locations]
edcs = [data.S.select_around(_, radius=select_radius, fast=True) for _ in locations]

data_vars = {}
index = np.array(angles)
Expand Down
4 changes: 2 additions & 2 deletions arpes/corrections/fermi_edge_corrections.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@


def _exclude_from_set(excluded):
def exclude(l):
return list(set(l).difference(excluded))
def exclude(_):
return list(set(_).difference(excluded))

return exclude

Expand Down
32 changes: 16 additions & 16 deletions arpes/endstations/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -369,37 +369,37 @@ def postprocess_final(
data.spectrum.attrs["spectrum_type"] = spectrum_type

ls = [data, *data.S.spectra]
for l in ls:
for _ in ls:
for k, key_fn in self.ATTR_TRANSFORMS.items():
if k in l.attrs:
transformed = key_fn(l.attrs[k])
if k in _.attrs:
transformed = key_fn(_.attrs[k])
if isinstance(transformed, dict):
l.attrs.update(transformed)
_.attrs.update(transformed)
else:
l.attrs[k] = transformed
_.attrs[k] = transformed

for l in ls:
for _ in ls:
for k, v in self.MERGE_ATTRS.items():
if k not in l.attrs:
l.attrs[k] = v
if k not in _.attrs:
_.attrs[k] = v

for l in ls:
for _ in ls:
for c in self.ENSURE_COORDS_EXIST:
if c not in l.coords:
if c in l.attrs:
l.coords[c] = l.attrs[c]
if c not in _.coords:
if c in _.attrs:
_.coords[c] = _.attrs[c]
else:
warnings_msg = f"Could not assign coordinate {c} from attributes,"
warnings_msg += "assigning np.nan instead."
warnings.warn(
warnings_msg,
stacklevel=2,
)
l.coords[c] = np.nan
_.coords[c] = np.nan

for l in ls:
if "chi" in l.coords and "chi_offset" not in l.attrs:
l.attrs["chi_offset"] = l.coords["chi"].item()
for _ in ls:
if "chi" in _.coords and "chi_offset" not in _.attrs:
_.attrs["chi_offset"] = _.coords["chi"].item()

# go and change endianness and datatypes to something reasonable
# this is done for performance reasons in momentum space conversion, primarily
Expand Down
2 changes: 1 addition & 1 deletion arpes/endstations/plugin/ALG_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class ALGMainChamber(HemisphericalEndstation, FITSEndstation):
]

ATTR_TRANSFORMS: ClassVar[dict] = {
"START_T": lambda l: {"time": " ".join(l.split(" ")[1:]).lower(), "date": l.split(" ")[0]},
"START_T": lambda _: {"time": " ".join(_.split(" ")[1:]).lower(), "date": _.split(" ")[0]},
}

RENAME_KEYS: ClassVar[dict[str, str]] = {
Expand Down
2 changes: 1 addition & 1 deletion arpes/endstations/plugin/ALG_spin_ToF.py
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,7 @@ def load_SToF_fits(self, scan_desc: dict | None = None, **kwargs: Incomplete):
hdulist.close()

relevant_dimensions = {
k for k in coords if k in set(itertools.chain(*[l[0] for l in data_vars.values()]))
k for k in coords if k in set(itertools.chain(*[_[0] for _ in data_vars.values()]))
}
relevant_coords = {k: v for k, v in coords.items() if k in relevant_dimensions}

Expand Down
4 changes: 2 additions & 2 deletions arpes/endstations/plugin/ANTARES.py
Original file line number Diff line number Diff line change
Expand Up @@ -306,8 +306,8 @@ def check_attrs(s):
pass

ls = [data, *data.S.spectra]
for l in ls:
check_attrs(l)
for _ in ls:
check_attrs(_)

# attempt to determine whether the energy is likely a kinetic energy
# if so, we will subtract the photon energy
Expand Down
12 changes: 6 additions & 6 deletions arpes/endstations/plugin/BL10_SARPES.py
Original file line number Diff line number Diff line change
Expand Up @@ -155,9 +155,9 @@ def postprocess_final(self, data: xr.Dataset, scan_desc: SCANDESC | None = None)
data.coords[c] = np.deg2rad(data.coords[c])

for angle_attr in deg_to_rad_attrs:
for l in ls:
if angle_attr in l.attrs:
l.attrs[angle_attr] = np.deg2rad(float(l.attrs[angle_attr]))
for _ in ls:
if angle_attr in _.attrs:
_.attrs[angle_attr] = np.deg2rad(float(_.attrs[angle_attr]))

data.attrs["alpha"] = np.pi / 2
data.attrs["psi"] = 0
Expand All @@ -169,9 +169,9 @@ def postprocess_final(self, data: xr.Dataset, scan_desc: SCANDESC | None = None)
# .spectrum.attrs, for now just paste them over
necessary_coord_names = {"theta", "beta", "chi", "phi"}
ls = data.S.spectra
for l in ls:
for _ in ls:
for cname in necessary_coord_names:
if cname not in l.attrs and cname not in l.coords and cname in data.attrs:
l.attrs[cname] = data.attrs[cname]
if cname not in _.attrs and cname not in _.coords and cname in data.attrs:
_.attrs[cname] = data.attrs[cname]

return super().postprocess_final(data, scan_desc)
2 changes: 1 addition & 1 deletion arpes/endstations/plugin/HERS.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ def load(self, scan_desc: dict | None = None, **kwargs: Incomplete):
hdulist.close()

relevant_dimensions = {
k for k in coords if k in set(itertools.chain(*[l[0] for l in data_vars.values()]))
k for k in coords if k in set(itertools.chain(*[_[0] for _ in data_vars.values()]))
}
relevant_coords = {k: v for k, v in coords.items() if k in relevant_dimensions}

Expand Down
38 changes: 19 additions & 19 deletions arpes/endstations/plugin/MAESTRO.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,13 +63,13 @@ def fix_prebinned_coordinates(self):

def postprocess_final(self, data: xr.Dataset, scan_desc: dict | None = None):
ls = [data, *data.S.spectra]
for l in ls:
l.attrs.update(self.ANALYZER_INFORMATION)
for _ in ls:
_.attrs.update(self.ANALYZER_INFORMATION)

if "GRATING" in l.attrs:
l.attrs["grating_lines_per_mm"] = {
if "GRATING" in _.attrs:
_.attrs["grating_lines_per_mm"] = {
"G201b": 600,
}.get(l.attrs["GRATING"])
}.get(_.attrs["GRATING"])

return super().postprocess_final(data, scan_desc)

Expand Down Expand Up @@ -126,14 +126,14 @@ class MAESTROMicroARPESEndstation(MAESTROARPESEndstationBase):
}

ATTR_TRANSFORMS: ClassVar[dict] = {
"START_T": lambda l: {
"time": " ".join(l.split(" ")[1:]).lower(),
"date": l.split(" ")[0],
"START_T": lambda _: {
"time": " ".join(_.split(" ")[1:]).lower(),
"date": _.split(" ")[0],
},
"SF_SLITN": lambda l: {
"slit_number": int(l.split(" ")[0]),
"slit_shape": l.split(" ")[-1].lower(),
"slit_width": float(l.split(" ")[2]),
"SF_SLITN": lambda _: {
"slit_number": int(_.split(" ")[0]),
"slit_shape": _.split(" ")[-1].lower(),
"slit_width": float(_.split(" ")[2]),
},
}

Expand Down Expand Up @@ -243,14 +243,14 @@ class MAESTRONanoARPESEndstation(MAESTROARPESEndstationBase):
}

ATTR_TRANSFORMS: ClassVar[dict] = {
"START_T": lambda l: {
"time": " ".join(l.split(" ")[1:]).lower(),
"date": l.split(" ")[0],
"START_T": lambda _: {
"time": " ".join(_.split(" ")[1:]).lower(),
"date": _.split(" ")[0],
},
"SF_SLITN": lambda l: {
"slit_number": int(l.split(" ")[0]),
"slit_shape": l.split(" ")[-1].lower(),
"slit_width": float(l.split(" ")[2]),
"SF_SLITN": lambda _: {
"slit_number": int(_.split(" ")[0]),
"slit_shape": _.split(" ")[-1].lower(),
"slit_width": float(_.split(" ")[2]),
},
}

Expand Down
10 changes: 5 additions & 5 deletions arpes/endstations/plugin/kaindl.py
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ def concatenate_frames(

axis_name = lines[0].strip()
axis_name = self.RENAME_KEYS.get(axis_name, axis_name)
values = [float(l.strip()) for l in lines[1 : len(frames) + 1]]
values = [float(_.strip()) for _ in lines[1 : len(frames) + 1]]

for v, f in zip(values, frames):
f.coords[axis_name] = v
Expand Down Expand Up @@ -239,9 +239,9 @@ def attach_attr(data, attr_name, as_name):
data.attrs[angle_attr] = np.deg2rad(float(data.attrs[angle_attr]))

ls = [data, *data.S.spectra]
for l in ls:
l.coords["x"] = np.nan
l.coords["y"] = np.nan
l.coords["z"] = np.nan
for _ in ls:
_.coords["x"] = np.nan
_.coords["y"] = np.nan
_.coords["z"] = np.nan

return super().postprocess_final(data, scan_desc)
Loading

0 comments on commit fc59ff8

Please sign in to comment.