From 8bca442935442074c7af42dd708bf7bfe28f8770 Mon Sep 17 00:00:00 2001 From: Ryuichi Arafune Date: Mon, 12 Feb 2024 09:36:43 +0900 Subject: [PATCH] * Tidy up MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 🔥 - Remove the class and functions in corrections/**init**.py (HashableDict, reference_key, correc… ➕ add dependency about notebook (v7 or later is required.) --- Changes.md | 2 + README.rst | 13 +++--- pyproject.toml | 5 ++- src/arpes/analysis/decomposition.py | 3 +- src/arpes/analysis/mask.py | 2 +- src/arpes/analysis/moire.py | 5 ++- src/arpes/corrections/__init__.py | 44 ------------------- src/arpes/endstations/__init__.py | 2 +- src/arpes/endstations/nexus_utils.py | 16 ++++++- src/arpes/plotting/fermi_surface.py | 4 +- src/arpes/utilities/conversion/core.py | 3 +- src/arpes/utilities/excepthook.py | 29 +++++++----- src/arpes/utilities/qt/windows.py | 2 +- tests/conftest.py | 5 +-- tests/test_basic_data_loading.py | 3 +- tests/test_bz.py | 1 - tests/test_bz_spec.py | 1 - tests/test_curve_fitting.py | 1 - tests/test_derivative_analysis.py | 1 - tests/test_direct_and_example_data_loading.py | 1 - tests/test_generic_utilities.py | 1 - tests/test_momentum_conversion.py | 3 +- tests/test_prodigy_itx.py | 1 - tests/test_qt.py | 3 +- tests/test_xarray_extensions.py | 2 +- 25 files changed, 63 insertions(+), 90 deletions(-) diff --git a/Changes.md b/Changes.md index 93b4d3c0..1ac1576b 100644 --- a/Changes.md +++ b/Changes.md @@ -59,6 +59,8 @@ Major Changes from 3.0.1 - Remove condensed_attrs: We should not enfoce camelcase on attributes, while original version did. Rather, the snake_case would be better from the modern pythonic viewpoint. - Remove `trace` arg for debugging. This technique may be sharp, but not so well fitted the current python trend; typing oriented. + - Remove the class and functions in corrections/**init**.py (HashableDict, reference_key, correction_from_reference_set), which have not used. + Fix from 3.0.1 - bug of concatenating in broadcast_model diff --git a/README.rst b/README.rst index 307c721c..969122c7 100644 --- a/README.rst +++ b/README.rst @@ -68,19 +68,17 @@ If you use PyARPES in your work, please support the development of scientific so Installation ============ -PyARPES can be installed from source. Python version 3.11 or newer is strongly recommmended. +PyARPES (>= V.4.0) can be installed from source. Python version 3.11 or newer is strongly recommmended. -The current version has been largely revised from the original version which are in PyPI and conda site. -Unfortunately, I don't have the right the updated version to these site, and I would not like to take over it from the original author. +The current version has been largely revised from the original version which are in PyPI and conda site. +Unfortunately, I don't have the right the upload the current version to these site, and I would not like to take over it from the original author. -The main purpose of revision of the package is to make this be reliable. Actually, the original version outputs wrong results in many +The main purpose of revision of the package is to make this be reliable for us. Actually, the original version outputs the wrong results in many case, especially for angle-momentum conversion. - Thus, the current package can be installed only through the github. - Pip installation ---------------- @@ -96,6 +94,7 @@ If you want to modify the source for PyARPES as you use it, you might prefer a l Details can be found on `the documentation site`_. + Suggested steps --------------- @@ -108,6 +107,8 @@ Suggested steps Contact ======= +Very unfortunately, we cannot get any responses from the original author. The comment below does not make sense at present. + Questions, difficulties, and suggestions can be directed to Conrad Stansbury (chstan@berkeley.edu) or added to the repository as an issue. In the case of trouble, also check the `FAQ`_. diff --git a/pyproject.toml b/pyproject.toml index 148f9ca7..3791c018 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -10,7 +10,7 @@ readme = "README.rst" requires-python = ">= 3.10" dependencies = [ - "holoviews", + "holoviews>=1.16.0", "astropy>=5.3", "xarray>=2023.5.0", "lmfit>=1.2.1", @@ -28,6 +28,7 @@ dependencies = [ "scikit-image>=0.20.0", "netcdf4>=1.6.4", "rx>=3.2.0", + "notebook>=7.0", "ipywidgets", ] @@ -94,8 +95,8 @@ lint.ignore = [ # "NPY201", # Numpy 2.0, ] -target-version = "py310" lint.select = ["ALL"] +target-version = "py310" line-length = 100 exclude = ["scripts", "docs", "conda"] diff --git a/src/arpes/analysis/decomposition.py b/src/arpes/analysis/decomposition.py index 8deba529..06e79364 100644 --- a/src/arpes/analysis/decomposition.py +++ b/src/arpes/analysis/decomposition.py @@ -5,13 +5,14 @@ from functools import wraps from typing import TYPE_CHECKING +import xarray as xr + from arpes.constants import TWO_DIMENSION from arpes.provenance import PROVENANCE, provenance from arpes.utilities import normalize_to_spectrum if TYPE_CHECKING: import sklearn - import xarray as xr from _typeshed import Incomplete __all__ = ( diff --git a/src/arpes/analysis/mask.py b/src/arpes/analysis/mask.py index 0e357b1a..68ea5195 100644 --- a/src/arpes/analysis/mask.py +++ b/src/arpes/analysis/mask.py @@ -5,13 +5,13 @@ from typing import TYPE_CHECKING import numpy as np +import xarray as xr from matplotlib.path import Path from arpes.provenance import update_provenance from arpes.utilities import normalize_to_spectrum if TYPE_CHECKING: - import xarray as xr from _typeshed import Incomplete from numpy.typing import NDArray diff --git a/src/arpes/analysis/moire.py b/src/arpes/analysis/moire.py index c16aef8d..8add4188 100644 --- a/src/arpes/analysis/moire.py +++ b/src/arpes/analysis/moire.py @@ -2,6 +2,7 @@ All of the moirés discussed here are on hexagonal crystal systems. """ + from __future__ import annotations from typing import TYPE_CHECKING, Any @@ -119,7 +120,7 @@ def calculate_bz_vertices_from_direct_cell(cell: NDArray[np.float_]) -> list: assert all(abs(cell[2][0:2]) < RTOL) assert all(abs(cell.T[2][0:2]) < RTOL) else: - cell = [[*list(c), 0] for c in cell] + [[0, 0, 1]] + cell = np.array([[*list(c), 0] for c in cell] + [[0, 0, 1]]) icell = np.linalg.inv(cell).T try: @@ -167,7 +168,7 @@ def calc_commensurate_moire_cell( if swap_angle: moire_angle = -moire_angle - moire_cell = hex_cell_2d(moire_a) + moire_cell = hex_cell_2d(float(moire_a)) moire_cell = [[*list(c), 0] for c in moire_cell] + [[0, 0, 1]] moire_cell = Rotation.from_rotvec([0, 0, moire_angle]).apply(moire_cell) moire_icell = np.linalg.inv(moire_cell).T diff --git a/src/arpes/corrections/__init__.py b/src/arpes/corrections/__init__.py index e4a01160..7a936989 100644 --- a/src/arpes/corrections/__init__.py +++ b/src/arpes/corrections/__init__.py @@ -11,13 +11,6 @@ from __future__ import annotations -from collections import OrderedDict -from typing import TYPE_CHECKING - -import xarray as xr - -from arpes.utilities import deep_equals, normalize_to_dataset - from .fermi_edge_corrections import ( apply_direct_fermi_edge_correction, apply_photon_energy_fermi_edge_correction, @@ -27,40 +20,3 @@ build_quadratic_fermi_edge_correction, find_e_fermi_linear_dos, ) - -if TYPE_CHECKING: - from arpes._typing import XrTypes - -__all__ = ( - "reference_key", - "correction_from_reference_set", -) - - -class HashableDict(OrderedDict): - """Implements hashing for ordered dictionaries. - - The dictionary must be ordered for the hash to be stable. - """ - - def __hash__(self): - return hash(frozenset(self.items())) - - -def reference_key(data: xr.DataArray) -> HashableDict: - """Calculates a key/hash for data determining reference/correction equality.""" - data_array = data if isinstance(data, xr.DataArray) else normalize_to_dataset(data) - assert isinstance(data_array, xr.DataArray) - return HashableDict(data_array.S.reference_settings) - - -def correction_from_reference_set(data: xr.DataArray, reference_set): - """Determines which correction to use from a set of references.""" - data_array = data if isinstance(data, xr.DataArray) else normalize_to_dataset(data) - correction = None - for k, corr in reference_set.items(): - if deep_equals(dict(reference_key(data_array)), dict(k)): - correction = corr - break - - return correction diff --git a/src/arpes/endstations/__init__.py b/src/arpes/endstations/__init__.py index f0d32f1e..abb0cc3d 100644 --- a/src/arpes/endstations/__init__.py +++ b/src/arpes/endstations/__init__.py @@ -9,7 +9,7 @@ import warnings from logging import DEBUG, INFO, Formatter, StreamHandler, getLogger from pathlib import Path -from typing import TYPE_CHECKING, ClassVar, Literal, TypedDict, reveal_type +from typing import TYPE_CHECKING, ClassVar, Literal, TypedDict import h5py import numpy as np diff --git a/src/arpes/endstations/nexus_utils.py b/src/arpes/endstations/nexus_utils.py index c168d570..d0012ab1 100644 --- a/src/arpes/endstations/nexus_utils.py +++ b/src/arpes/endstations/nexus_utils.py @@ -2,10 +2,12 @@ Currently we assume that the raw file format is actually HDF. """ + from __future__ import annotations import contextlib from dataclasses import dataclass, field +from logging import DEBUG, INFO, Formatter, StreamHandler, getLogger from typing import TYPE_CHECKING, Any import numpy as np @@ -17,6 +19,18 @@ __all__ = ["read_data_attributes_from"] +LOGLEVELS = (DEBUG, INFO) +LOGLEVEL = LOGLEVELS[1] +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 + def read_group_data(group: dict, attribute_name: str = "") -> xr.Dataset: if attribute_name: @@ -65,7 +79,7 @@ class DebugTarget(Target): name = "debug" def read_h5(self, g, path) -> None: - print(path, self.read(read_group_data(g))) + logger.info(f"path {path}, group_data {self.read(read_group_data(g))}") @dataclass diff --git a/src/arpes/plotting/fermi_surface.py b/src/arpes/plotting/fermi_surface.py index 29a33584..633cb874 100644 --- a/src/arpes/plotting/fermi_surface.py +++ b/src/arpes/plotting/fermi_surface.py @@ -98,10 +98,10 @@ def magnify_circular_regions_plot( data: [TODO:description] magnified_points: [TODO:description] mag: [TODO:description] - radius: [TODO:description] + radius (float): [TODO:description] cmap: [TODO:description] color: [TODO:description] - edgecolor: [TODO:description] + edgecolor (ColorType): [TODO:description] out: [TODO:description] ax: [TODO:description] kwargs: [TODO:description] diff --git a/src/arpes/utilities/conversion/core.py b/src/arpes/utilities/conversion/core.py index 5e768762..a8ae7671 100644 --- a/src/arpes/utilities/conversion/core.py +++ b/src/arpes/utilities/conversion/core.py @@ -179,6 +179,7 @@ def slice_along_path( # noqa: PLR0913 for x in interpolation_points ] + logger.debug(f"parsed_interpolation_points: {parsed_interpolation_points}") free_coordinates = list(arr.dims) seen_coordinates = collections.defaultdict(set) for point in parsed_interpolation_points: @@ -659,7 +660,7 @@ def _extract_symmetry_point( arr: xr.DataArray, *, extend_to_edge: bool = False, -) -> dict: +) -> dict[Hashable, float]: """[TODO:summary]. Args: diff --git a/src/arpes/utilities/excepthook.py b/src/arpes/utilities/excepthook.py index 759c91d5..c620732b 100644 --- a/src/arpes/utilities/excepthook.py +++ b/src/arpes/utilities/excepthook.py @@ -1,30 +1,37 @@ """A utility excepthook for Qt applications which ensures errors are visible in Jupyter.""" + from __future__ import annotations import traceback -from collections import namedtuple +from typing import TYPE_CHECKING, NamedTuple + +if TYPE_CHECKING: + from types import FrameType, TracebackType __all__ = ("patched_excepthook",) -def patched_excepthook(exc_type, exc_value, exc_tb) -> None: +def patched_excepthook( + exc_type: type[BaseException] | None, + exc_value: BaseException | None, + exc_tb: TracebackType | FakeTB | None, +) -> None: """Prints the traceback instead of dying silently. Useful for debugging Qt apps in Jupyter.""" enriched_tb = _add_missing_frames(exc_tb) if exc_tb else exc_tb traceback.print_exception(exc_type, exc_value, enriched_tb) -def _add_missing_frames(tb): - result = fake_tb(tb.tb_frame, tb.tb_lasti, tb.tb_lineno, tb.tb_next) +def _add_missing_frames(tb: TracebackType | FakeTB) -> FakeTB: + result = FakeTB(tb.tb_frame, tb.tb_lasti, tb.tb_lineno, tb.tb_next) frame = tb.tb_frame.f_back while frame: - result = fake_tb(frame, frame.f_lasti, frame.f_lineno, result) + result = FakeTB(frame, frame.f_lasti, frame.f_lineno, result) frame = frame.f_back return result -fake_tb = namedtuple("fake_tb", ("tb_frame", "tb_lasti", "tb_lineno", "tb_next")) -# class fake_tb(NamedTuple): -# tb_frame -# tb_lasti -# tb_lineno -# tb_next +class FakeTB(NamedTuple): + tb_frame: FrameType + tb_lasti: int + tb_lineno: int + tb_next: TracebackType | FakeTB | None diff --git a/src/arpes/utilities/qt/windows.py b/src/arpes/utilities/qt/windows.py index 88aee956..78a988cc 100644 --- a/src/arpes/utilities/qt/windows.py +++ b/src/arpes/utilities/qt/windows.py @@ -149,4 +149,4 @@ def toggle_help(self, event: QKeyEvent) -> None: def window_print(self, *args: Incomplete, **kwargs: Incomplete) -> None: """Forward prints to the application instance so they end up in Jupyter.""" - print(*args, **kwargs) + logger.info(f"args: {args}, kwargs: {kwargs}") diff --git a/tests/conftest.py b/tests/conftest.py index 9b4b505b..ce9ab3b0 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -5,17 +5,16 @@ from pathlib import Path from typing import TYPE_CHECKING, TypedDict -import pytest - import arpes.config import arpes.endstations +import pytest + from tests.utils import cache_loader if TYPE_CHECKING: from collections.abc import Callable, Generator import xarray as xr - from arpes._typing import SCANINFO, WORKSPACETYPE diff --git a/tests/test_basic_data_loading.py b/tests/test_basic_data_loading.py index 7279c759..426dd99b 100644 --- a/tests/test_basic_data_loading.py +++ b/tests/test_basic_data_loading.py @@ -5,11 +5,10 @@ import contextlib from typing import TYPE_CHECKING, Any, ClassVar +import arpes.xarray_extensions # pylint: disable=unused-import, redefined-outer-name # noqa: F401 import numpy as np import pytest import xarray as xr - -import arpes.xarray_extensions # pylint: disable=unused-import, redefined-outer-name # noqa: F401 from arpes.utilities.conversion import convert_to_kspace if TYPE_CHECKING: diff --git a/tests/test_bz.py b/tests/test_bz.py index e27bdb5a..2aeadd21 100644 --- a/tests/test_bz.py +++ b/tests/test_bz.py @@ -1,7 +1,6 @@ """Test for utilitiy.bz.""" import numpy as np - from arpes.utilities import bz diff --git a/tests/test_bz_spec.py b/tests/test_bz_spec.py index efcc9f16..5e1fff19 100644 --- a/tests/test_bz_spec.py +++ b/tests/test_bz_spec.py @@ -1,6 +1,5 @@ """Test for utilitiy.bz.""" import numpy as np - from arpes.utilities import bz_spec diff --git a/tests/test_curve_fitting.py b/tests/test_curve_fitting.py index 0ffc62c4..8cf70df8 100644 --- a/tests/test_curve_fitting.py +++ b/tests/test_curve_fitting.py @@ -1,7 +1,6 @@ """Unit test for curve fitting.""" import numpy as np - from arpes.analysis import rebin from arpes.fits import AffineBroadenedFD, broadcast_model from arpes.io import example_data diff --git a/tests/test_derivative_analysis.py b/tests/test_derivative_analysis.py index c93deb4d..24c41208 100644 --- a/tests/test_derivative_analysis.py +++ b/tests/test_derivative_analysis.py @@ -5,7 +5,6 @@ import numpy as np import pytest - from arpes.analysis.derivative import dn_along_axis from arpes.analysis.filters import gaussian_filter_arr diff --git a/tests/test_direct_and_example_data_loading.py b/tests/test_direct_and_example_data_loading.py index 46b56cb9..050e9013 100644 --- a/tests/test_direct_and_example_data_loading.py +++ b/tests/test_direct_and_example_data_loading.py @@ -6,7 +6,6 @@ import numpy as np import xarray as xr - from arpes.endstations.plugin.ALG_main import ALGMainChamber from arpes.io import load_data, load_example_data diff --git a/tests/test_generic_utilities.py b/tests/test_generic_utilities.py index 671d4413..3d39ed85 100644 --- a/tests/test_generic_utilities.py +++ b/tests/test_generic_utilities.py @@ -1,7 +1,6 @@ """Test for generic utility.""" import pytest - from arpes.utilities import clean_keys, deep_equals, deep_update diff --git a/tests/test_momentum_conversion.py b/tests/test_momentum_conversion.py index a98a4b2a..288019e0 100644 --- a/tests/test_momentum_conversion.py +++ b/tests/test_momentum_conversion.py @@ -1,10 +1,9 @@ """Unit test for k-conversion.""" +import arpes.xarray_extensions # pylint: disable=unused-import, redefined-outer-name # noqa: F401 import numpy as np import pytest import xarray as xr - -import arpes.xarray_extensions # pylint: disable=unused-import, redefined-outer-name # noqa: F401 from arpes.io import example_data from arpes.utilities.conversion import convert_to_kspace from arpes.utilities.conversion.base import CoordinateConverter diff --git a/tests/test_prodigy_itx.py b/tests/test_prodigy_itx.py index 57bc1881..1a104a8c 100644 --- a/tests/test_prodigy_itx.py +++ b/tests/test_prodigy_itx.py @@ -5,7 +5,6 @@ import numpy as np import pytest import xarray as xr - from arpes.endstations.prodigy_itx import ProdigyItx, load_sp2 data_dir = Path(__file__).parent.parent / "src" / "arpes" / "example_data" diff --git a/tests/test_qt.py b/tests/test_qt.py index 5d4d2669..7233aefd 100644 --- a/tests/test_qt.py +++ b/tests/test_qt.py @@ -1,12 +1,11 @@ """Unit test for qt related.""" from typing import TYPE_CHECKING +from arpes.io import example_data from PySide6 import QtCore from pytestqt.qt_compat import qt_api from pytestqt.qtbot import QtBot -from arpes.io import example_data - if TYPE_CHECKING: from arpes.plotting.qt_tool import QtTool diff --git a/tests/test_xarray_extensions.py b/tests/test_xarray_extensions.py index 18f90d5b..09b36936 100644 --- a/tests/test_xarray_extensions.py +++ b/tests/test_xarray_extensions.py @@ -3,7 +3,6 @@ import numpy as np import pytest import xarray as xr - from arpes.io import example_data @@ -44,6 +43,7 @@ def test_degrees_of_freedom_dims(self, xps_map: xr.Dataset) -> None: assert xps_map.S.scan_degrees_of_freedom == {"x", "y"} def test_is_functions(self, xps_map: xr.Dataset) -> None: + """Test for is_* function.""" assert xps_map.S.is_spatial def test_find_spectrum_energy_edges(self, dataarray_cut: xr.DataArray) -> None: