From fe14b358de27ecd80fe876f959da9459536efe3e Mon Sep 17 00:00:00 2001 From: Ryuichi Arafune Date: Thu, 12 Oct 2023 08:58:42 +0900 Subject: [PATCH] =?UTF-8?q?=F0=9F=92=AC=20=20update=20type=20hints=20?= =?UTF-8?q?=F0=9F=94=A7=20=20update=20pyproject.toml?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit (cherry picked from commit 2e2045dbeb7980958e9fc7b7a78934950d449c6d) --- arpes/preparation/__init__.py | 21 ++++++++++++++++---- arpes/preparation/axis_preparation.py | 28 +++++++++++++++------------ arpes/trace.py | 6 +++--- arpes/utilities/widgets.py | 16 +++++++++++++++ arpes/workflow.py | 2 +- arpes/xarray_extensions.py | 3 ++- docs/source/index.rst | 5 ++++- pyproject.toml | 6 ++++-- 8 files changed, 63 insertions(+), 24 deletions(-) diff --git a/arpes/preparation/__init__.py b/arpes/preparation/__init__.py index 4e4fba39..e426a55e 100644 --- a/arpes/preparation/__init__.py +++ b/arpes/preparation/__init__.py @@ -1,7 +1,20 @@ """Utility functions used during data preparation and loading.""" from __future__ import annotations -from .axis_preparation import * -from .coord_preparation import * -from .hemisphere_preparation import * -from .tof_preparation import * +from .axis_preparation import ( + dim_normalizer, + flip_axis, + normalize_dim, + normalize_total, + sort_axis, + transform_dataarray_axis, + vstack_data, +) +from .coord_preparation import disambiguate_coordinates +from .hemisphere_preparation import stitch_maps +from .tof_preparation import ( + build_KE_coords_to_time_coords, + build_KE_coords_to_time_pixel_coords, + process_DLD, + process_SToF, +) diff --git a/arpes/preparation/axis_preparation.py b/arpes/preparation/axis_preparation.py index 6259c404..ac065198 100644 --- a/arpes/preparation/axis_preparation.py +++ b/arpes/preparation/axis_preparation.py @@ -126,11 +126,12 @@ def normalize_dim( """ dims: list[str] dims = [dim_or_dims] if isinstance(dim_or_dims, str) else dim_or_dims + assert isinstance(dims, list) summed_arr = arr.fillna(arr.mean()).sum([d for d in arr.dims if d not in dims]) normalized_arr = arr / (summed_arr / np.prod(summed_arr.shape)) - to_return = xr.DataArray(normalized_arr.values, arr.coords, arr.dims, attrs=arr.attrs) + to_return = xr.DataArray(normalized_arr.values, arr.coords, tuple(arr.dims), attrs=arr.attrs) if not keep_id and "id" in to_return.attrs: del to_return.attrs["id"] @@ -149,28 +150,29 @@ def normalize_dim( @update_provenance("Normalize total spectrum intensity") -def normalize_total(data: DataType) -> xr.DataArray: +def normalize_total(data: DataType, *, total_intensity: float = 1000000) -> xr.DataArray: """Normalizes data so that the total intensity is 1000000 (a bit arbitrary). Args: - data(DataType): [TODO:description] + data(DataType): Input ARPES data + total_intensity: value for normalizaiton Returns: xr.DataArray """ data_array = normalize_to_spectrum(data) assert isinstance(data_array, xr.DataArray) - return data_array / (data_array.sum(data.dims) / 1000000) + return data_array / (data_array.sum(data.dims) / total_intensity) -def dim_normalizer(dim_name): +def dim_normalizer(dim_name: str) -> Callable[[xr.Dataset | xr.DataArray], xr.DataArray]: """Safe partial application of dimension normalization. Args: - dim_name ([TODO:type]): [TODO:description] + dim_name (str): [TODO:description] """ - def normalize(arr: xr.DataArray): + def normalize(arr: xr.Dataset | xr.DataArray) -> xr.DataArray: if dim_name not in arr.dims: return arr return normalize_dim(arr, dim_name) @@ -179,14 +181,14 @@ def normalize(arr: xr.DataArray): def transform_dataarray_axis( - f, + func: Callable[..., ...], old_axis_name: str, new_axis_name: str, new_axis: NDArray[np.float_] | xr.DataArray, dataset: xr.Dataset, prep_name: Callable[[str], str], - transform_spectra=None, - remove_old=True, + transform_spectra: dict[str, xr.DataArray] | None = None, + remove_old: bool = True, ) -> xr.Dataset: """Applies a function onto a DataArray axis. @@ -202,8 +204,10 @@ def transform_dataarray_axis( """ ds = dataset.copy() if transform_spectra is None: - # transform *all* DataArrays in the dataset that have old_axis_name in their dimensions + # transform *all* DataArrays in the dataset that have old_axis_name in their dimensions. + # In the standard usage, k is "spectra", v is xr.DataArray transform_spectra = {k: v for k, v in ds.data_vars.items() if old_axis_name in v.dims} + assert isinstance(transform_spectra, dict) ds.coords[new_axis_name] = new_axis @@ -217,7 +221,7 @@ def transform_dataarray_axis( new_dims = list(dr.dims) new_dims[old_axis] = new_axis_name - g = functools.partial(f, axis=old_axis) + g = functools.partial(func, axis=old_axis) output = geometric_transform(dr.values, g, output_shape=shape, output="f", order=1) new_coords = dict(dr.coords) diff --git a/arpes/trace.py b/arpes/trace.py index d008f3c7..a4110472 100644 --- a/arpes/trace.py +++ b/arpes/trace.py @@ -4,7 +4,7 @@ import functools import time from dataclasses import dataclass, field -from logging import INFO, Formatter, StreamHandler, getLogger +from logging import DEBUG, INFO, Formatter, StreamHandler, getLogger from typing import TYPE_CHECKING if TYPE_CHECKING: @@ -15,8 +15,8 @@ __all__ = [ "traceable", ] - -LOGLEVEL = INFO +LOGLEVELS = (DEBUG, INFO) +LOGLEVEL = LOGLEVELS[0] logger = getLogger(__name__) fmt = "%(asctime)s %(levelname)s %(name)s :%(message)s" formatter = Formatter(fmt) diff --git a/arpes/utilities/widgets.py b/arpes/utilities/widgets.py index 4c740c21..76838c09 100644 --- a/arpes/utilities/widgets.py +++ b/arpes/utilities/widgets.py @@ -1,6 +1,7 @@ """Wraps Qt widgets in ones which use rx for signaling, Conrad's personal preference.""" from __future__ import annotations +from logging import DEBUG, INFO, Formatter, StreamHandler, getLogger from pathlib import Path from typing import TYPE_CHECKING @@ -35,6 +36,18 @@ "SubjectiveTextEdit", ) +LOGLEVELS = (DEBUG, INFO) +LOGLEVEL = LOGLEVELS[0] +logger = getLogger(__name__) +fmt = "%(asctime)s %(levelname)s %(name)s :%(message)s" +formatter = Formatter(fmt) +handler = StreamHandler() +handler.setLevel(LOGLEVEL) +logger.setLevel(LOGLEVEL) +handler.setFormatter(formatter) +logger.addHandler(handler) +logger.propagate = False + class SubjectiveComboBox(QComboBox): """A QComboBox using rx instead of signals.""" @@ -188,6 +201,9 @@ class SubjectiveCheckBox(QCheckBox): def __init__(self, *args: Incomplete, **kwargs: Incomplete) -> None: """Wrap signals in ``rx.BehaviorSubject``s.""" + if kwargs: + for k, v in kwargs.items(): + logger.debug(f"unused kwargs: key: {k}, value{v}") super().__init__(*args) self.subject = BehaviorSubject(self.checkState()) self.stateChanged.connect(self.subject.on_next) diff --git a/arpes/workflow.py b/arpes/workflow.py index ee37e88a..2d4415d5 100644 --- a/arpes/workflow.py +++ b/arpes/workflow.py @@ -94,7 +94,7 @@ def _open_path(p: Path | str) -> None: if "win" in sys.platform: subprocess.Popen(rf"explorer {p}") else: - print(p) # noqa: T201 + print(p) @with_workspace diff --git a/arpes/xarray_extensions.py b/arpes/xarray_extensions.py index 4563938e..a9a49b87 100644 --- a/arpes/xarray_extensions.py +++ b/arpes/xarray_extensions.py @@ -102,7 +102,8 @@ ANGLE_VARS = ("alpha", "beta", "chi", "psi", "phi", "theta") -LOGLEVEL = (DEBUG, INFO)[1] +LOGLEVELS = (DEBUG, INFO) +LOGLEVEL = LOGLEVELS[1] logger = getLogger(__name__) fmt = "%(asctime)s %(levelname)s %(name)s :%(message)s" formatter = Formatter(fmt) diff --git a/docs/source/index.rst b/docs/source/index.rst index 6f66312b..e4e43999 100644 --- a/docs/source/index.rst +++ b/docs/source/index.rst @@ -1,5 +1,8 @@ PyARPES ======= +**Non maintainer update** + + **December 2020, V3 Release**: The current relase focuses on improving usage and workflow for less experienced Python users, lifting version @@ -227,4 +230,4 @@ design, Michael Khachatrian dev-guide api CHANGELOG - \ No newline at end of file + diff --git a/pyproject.toml b/pyproject.toml index 7b7c9f70..bb86f5dc 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -85,6 +85,7 @@ ignore = [ "PD011", # pandas-use-of-dot-values "FBT002", # boolean-default-value-in-function-definition "FIX002", # line-contains-todo (FIX002)# + "G004", # logging-f-string ] select = ["ALL"] line-length = 100 @@ -93,9 +94,10 @@ exclude = ["scripts", "docs", "conda"] [tool.ruff.per-file-ignores] -"__init__.py" = ["F401"] # unused-import - +"__init__.py" = ["F401"] # unused-import +"arpes/__init__.py" = ["T201"] # print used "arpes/experiment/__init__.py" = ["ALL"] +"arpes/workflow.py" = ["T201", "T203"] # Bokeh based plotting tools will be removed. "arpes/plotting/band_tool.py" = ["ALL"]