Skip to content

Commit

Permalink
💬 Fix pyproject.toml & Update type hints
Browse files Browse the repository at this point in the history
  • Loading branch information
arafune committed Feb 5, 2024
1 parent a4bce35 commit ddddf18
Show file tree
Hide file tree
Showing 15 changed files with 165 additions and 123 deletions.
25 changes: 25 additions & 0 deletions arpes/_typing.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,12 @@
MarkEveryType,
)
from numpy.typing import ArrayLike, NDArray
from PySide6.QtCore.Qt import Orientation, WindowType
from PySide6.QtGui import QIcon, QPixmap
from PySide6.QtWidgets import (
QWidget,
)


__all__ = [
"DataType",
Expand Down Expand Up @@ -298,6 +304,25 @@ class ARPESAttrs(SPECTROMETER, LIGHTSOURCEINFO, SAMPLEINFO, total=False):
]


# TypedDict for Qt


class QSliderARGS(TypedDict, total=False):
orientation: Orientation
parent: QWidget | None


class QWidgetARGS(TypedDict, total=False):
parent: QWidget | None
f: WindowType


class QPushButtonARGS(TypedDict, total=False):
icon: QIcon | QPixmap
text: str
parent: QWidget | None


#
# TypedDict for plotting
#
Expand Down
18 changes: 10 additions & 8 deletions arpes/plotting/bz_tool/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
import numpy as np
import xarray as xr
from matplotlib.axes import Axes
from matplotlib.backends.backend_qt import FigureCanvas
from matplotlib.backends.backend_qt import FigureCanvasQT
from matplotlib.figure import Figure
from PySide6 import QtWidgets

Expand All @@ -26,7 +26,9 @@

if TYPE_CHECKING:
from _typeshed import Incomplete
from PySide6.QtWidgets import QLayout, QWidget
from PySide6.QtWidgets import QGridLayout, QWidget

from arpes.utilities.qt.data_array_image_view import DataArrayImageView

__all__ = ["bz_tool"]

Expand All @@ -52,20 +54,20 @@ class BZTool:

def __init__(self) -> None:
self.settings = None
self.context = {}
self.context: dict[str, Incomplete] = {}

self.content_layout: QLayout
self.main_layout: QLayout
self.views = {}
self.content_layout: QGridLayout
self.main_layout: QGridLayout
self.views: dict[str, DataArrayImageView] = {}
self.reactive_views = []
self.current_material: MaterialParams2D
self.cut_line = None

self.canvas = None
self.canvas: FigureCanvasQT
self.ax: Axes

def configure_main_widget(self) -> None:
self.canvas = FigureCanvas(Figure(figsize=(8, 8)))
self.canvas = FigureCanvasQT(Figure(figsize=(8, 8)))
self.ax = self.canvas.figure.subplots()
assert isinstance(self.ax, Axes)
self.content_layout.addWidget(self.canvas, 0, 0)
Expand Down
11 changes: 7 additions & 4 deletions arpes/plotting/qt_tool/AxisInfoWidget.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""A widget providing rudimentary information about an axis on a DataArray."""

# pylint: disable=import-error
from __future__ import annotations

Expand All @@ -8,6 +9,8 @@
from PySide6 import QtWidgets

if TYPE_CHECKING:
from PySide6.QtWidgets import QGridLayout, QLabel, QPushButton

from . import QtTool

__all__ = ("AxisInfoWidget",)
Expand All @@ -25,10 +28,10 @@ def __init__(
"""Configure inner widgets for axis info, and transpose to front button."""
super().__init__(title=str(axis_index), parent=parent)

self.layout = QtWidgets.QGridLayout(self)
self.layout: QGridLayout = QtWidgets.QGridLayout(self)

self.label = QtWidgets.QLabel("Cursor: ")
self.transpose_button = QtWidgets.QPushButton("To Front")
self.label: QLabel = QtWidgets.QLabel("Cursor: ")
self.transpose_button: QPushButton = QtWidgets.QPushButton("To Front")
self.transpose_button.clicked.connect(self.on_transpose)

self.layout.addWidget(self.label)
Expand All @@ -47,7 +50,7 @@ def root(self) -> QtTool:

def recompute(self) -> None:
"""Force a recomputation of dependent UI state: here, the title and text."""
self.setTitle(self.root.data.dims[self.axis_index])
self.setTitle(str(self.root.data.dims[self.axis_index]))
try:
cursor_index = self.root.context["cursor"][self.axis_index]
cursor_value = self.root.context["value_cursor"][self.axis_index]
Expand Down
22 changes: 10 additions & 12 deletions arpes/plotting/qt_tool/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@
import contextlib
import warnings
import weakref
from collections.abc import Sequence
from logging import DEBUG, INFO, Formatter, StreamHandler, getLogger
from typing import TYPE_CHECKING, reveal_type

import dill
import matplotlib as mpl
import numpy as np
import pyqtgraph as pg
from PySide6 import QtCore, QtGui, QtWidgets
from PySide6 import QtCore, QtWidgets
from PySide6.QtWidgets import QGridLayout

from arpes.utilities import normalize_to_spectrum
from arpes.utilities.qt import (
Expand All @@ -36,6 +36,7 @@
import xarray as xr
from _typeshed import Incomplete
from PySide6.QtCore import QEvent
from PySide6.QtGui import QKeyEvent
from PySide6.QtWidgets import QWidget

from arpes._typing import DataType
Expand Down Expand Up @@ -130,7 +131,7 @@ def transpose_swap(self, event: QEvent) -> None:
self.app().transpose_to_front(1)

@staticmethod
def _update_scroll_delta(delta: tuple[float, ...], event: QtGui.QKeyEvent) -> tuple:
def _update_scroll_delta(delta: tuple[float, float], event: QKeyEvent) -> tuple[float, float]:
logger.debug(f"method: _update_scroll_delta {event!s}")
if event.nativeModifiers() & 1: # shift key
delta = (delta[0], delta[1] * 5)
Expand All @@ -140,11 +141,11 @@ def _update_scroll_delta(delta: tuple[float, ...], event: QtGui.QKeyEvent) -> tu

return delta

def reset_intensity(self, event: QtGui.QKeyEvent) -> None:
def reset_intensity(self, event: QKeyEvent) -> None:
logger.debug(f"method: reset_intensity {event!s}")
self.app().reset_intensity()

def scroll_z(self, event: QtGui.QKeyEvent) -> None:
def scroll_z(self, event: QKeyEvent) -> None:
key_map = {
QtCore.Qt.Key.Key_N: (2, -1),
QtCore.Qt.Key.Key_M: (2, 1),
Expand All @@ -156,7 +157,7 @@ def scroll_z(self, event: QtGui.QKeyEvent) -> None:
if delta is not None and self.app() is not None:
self.app().scroll(delta)

def scroll(self, event: QtGui.QKeyEvent) -> None:
def scroll(self, event: QKeyEvent) -> None:
"""[TODO:summary].
Args:
Expand All @@ -171,10 +172,7 @@ def scroll(self, event: QtGui.QKeyEvent) -> None:
QtCore.Qt.Key.Key_Down: (1, -1),
QtCore.Qt.Key.Key_Up: (1, 1),
}

logger.debug(f"method: scroll {event!s}")
logger.debug(f"app {reveal_type(self.app)}")
logger.debug(f"app() {reveal_type(self.app())}")
delta = self._update_scroll_delta(key_map.get(event.key()), event)
if delta is not None and self.app() is not None:
self.app().scroll(delta)
Expand Down Expand Up @@ -492,10 +490,10 @@ def add_contextual_widgets(self) -> None:
self.main_layout.addLayout(self.content_layout, 0, 0)
self.main_layout.addWidget(self.tabs, 1, 0)

def layout(self) -> QtWidgets.QGridLayout:
def layout(self) -> QGridLayout:
"""Initialize the layout components."""
self.main_layout = QtWidgets.QGridLayout()
self.content_layout = QtWidgets.QGridLayout()
self.main_layout: QGridLayout = QGridLayout()
self.content_layout: QGridLayout = QGridLayout()
return self.main_layout

def before_show(self) -> None:
Expand Down
4 changes: 2 additions & 2 deletions arpes/provenance.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
import warnings
from datetime import UTC
from pathlib import Path
from typing import TYPE_CHECKING, TypedDict
from typing import TYPE_CHECKING, Any, TypedDict

import xarray as xr

Expand Down Expand Up @@ -138,7 +138,7 @@ def update_provenance(
A decorator which can be applied to a function.
"""

def update_provenance_decorator(fn: Callable) -> Callable[..., xr.DataArray | xr.Dataset]:
def update_provenance_decorator(fn: Callable) -> Callable[[Any], xr.DataArray | xr.Dataset]:
"""[TODO:summary].
Args:
Expand Down
6 changes: 3 additions & 3 deletions arpes/utilities/qt/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@


def run_tool_in_daemon_process(tool_handler: Callable) -> Callable:
"""Starts a Qt based tool as a daemon process.
"""Start a Qt based tool as a daemon process.
This is exceptionally useful because it let's you have multiple tool windows
open simultaneously and does not block the main "analysis" process.
Expand Down Expand Up @@ -89,7 +89,7 @@ def wrapped_handler(


def remove_dangling_viewboxes() -> None:
"""Removes ViewBoxes that don't get garbage collected on app close.
"""Remove ViewBoxes that don't get garbage collected on app close.
If you construct a view hierarchy which has circular references
then it can happen that Python will retain the references to Qt
Expand Down Expand Up @@ -168,7 +168,7 @@ def inches_to_px(
return tuple(int(x * self.screen_dpi) for x in arg)

def setup_pyqtgraph(self) -> None:
"""Does any patching required on PyQtGraph and configures options."""
"""Do any patching required on PyQtGraph and configures options."""
if self._pg_patched:
return

Expand Down
14 changes: 7 additions & 7 deletions arpes/utilities/qt/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ def __init__(self) -> None:
self.settings = arpes.config.SETTINGS.copy()

def copy_to_clipboard(self, value: object) -> None:
"""Attempts to copy the value to the clipboard."""
"""Attempt to copy the value to the clipboard."""
try:
import pprint

Expand Down Expand Up @@ -113,12 +113,12 @@ def ninety_eight_percentile(self) -> float:
return self._ninety_eight_percentile

def print(self, *args: Incomplete, **kwargs: Incomplete) -> None:
"""Forwards printing to the application so it ends up in Jupyter."""
"""Forward printing to the application so it ends up in Jupyter."""
self.window.window_print(*args, **kwargs)

@staticmethod
def build_pg_cmap(colormap: Colormap) -> pg.ColorMap:
"""Converts a matplotlib colormap to one suitable for pyqtgraph.
"""Convert a matplotlib colormap to one suitable for pyqtgraph.
pyqtgraph uses its own colormap format but for consistency and aesthetic
reasons we want to use the ones from matplotlib. This will sample the colors
Expand All @@ -134,7 +134,7 @@ def build_pg_cmap(colormap: Colormap) -> pg.ColorMap:
return pg.ColorMap(pos=np.linspace(0, 1, len(sampled_colormap)), color=sampled_colormap)

def set_colormap(self, colormap: Colormap | str) -> None:
"""Finds all `DataArrayImageView` instances and sets their color palette."""
"""Find all `DataArrayImageView` instances and sets their color palette."""
if isinstance(colormap, str):
colormap = mpl.colormaps.get_cmap(colormap)

Expand All @@ -154,7 +154,7 @@ def generate_marginal_for(
cursors: bool = False,
layout: QGridLayout | None = None,
) -> DataArrayImageView | DataArrayPlot:
"""Generates a marginal plot for the applications's data after selecting along `dimensions`.
"""Generate a marginal plot for the applications's data after selecting along `dimensions`.
This is used to generate the many different views of a volume in the browsable tools.
"""
Expand Down Expand Up @@ -252,12 +252,12 @@ def layout(self) -> QGridLayout:

@property
def window(self) -> SimpleWindow:
"""Gets the window instance on the current application."""
"""Get the window instance on the current application."""
assert self._window is not None
return self._window

def start(self, *, no_exec: bool = False, app: QtWidgets.QApplication | None = None) -> None:
"""Starts the Qt application, configures the window, and begins Qt execution."""
"""Start the Qt application, configures the window, and begins Qt execution."""
# When running in nbconvert, don't actually open tools.
import arpes.config

Expand Down
6 changes: 3 additions & 3 deletions arpes/utilities/qt/data_array_image_view.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ def plot(
*args: Incomplete,
**kwargs: Incomplete,
) -> pg.PlotDataItem:
"""Updates the UI with new data.
"""Update the UI with new data.
Data also needs to be forwarded to the coordinate axis in case of transpose
or changed range of data.
Expand Down Expand Up @@ -126,7 +126,7 @@ def setImage(
keep_levels: bool = False,
**kwargs: Incomplete,
) -> None:
"""Accepts an xarray.DataArray instead of a numpy array."""
"""Accept an xarray.DataArray instead of a numpy array."""
assert isinstance(img, xr.DataArray)
if keep_levels:
levels = self.getLevels()
Expand All @@ -140,4 +140,4 @@ def setImage(
self.setLevels(*levels)

def recompute(self) -> None:
"""A hook to recompute UI state, not used by this widget."""
"""Recompute UI state, not used by this widget."""
32 changes: 17 additions & 15 deletions arpes/utilities/qt/help_dialogs.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

if TYPE_CHECKING:
from PySide6.QtGui import QKeyEvent
from PySide6.QtWidgets import QGridLayout, QGroupBox, QLabel, QVBoxLayout

from arpes.utilities.ui import KeyBinding
__all__ = ("BasicHelpDialog",)
Expand All @@ -26,13 +27,15 @@ def __init__(self, shortcuts: list[KeyBinding] | None = None) -> None:
if shortcuts is None:
shortcuts = []

self.layout = QtWidgets.QVBoxLayout()
self.layout: QVBoxLayout = QtWidgets.QVBoxLayout()

keyboard_shortcuts_info = QtWidgets.QGroupBox(title="Keyboard Shortcuts")
keyboard_shortcuts_layout = QtWidgets.QGridLayout()
keyboard_shortcuts_info: QGroupBox = QtWidgets.QGroupBox(title="Keyboard Shortcuts")
keyboard_shortcuts_layout: QGridLayout = QtWidgets.QGridLayout()
for i, shortcut in enumerate(shortcuts):
the_label: QLabel = label(", ".join(PRETTY_KEYS[k] for k in shortcut.chord))
the_label.setWordWrap(on=True)
keyboard_shortcuts_layout.addWidget(
label(", ".join(PRETTY_KEYS[k] for k in shortcut.chord), wordWrap=True),
the_label,
i,
0,
)
Expand All @@ -41,18 +44,17 @@ def __init__(self, shortcuts: list[KeyBinding] | None = None) -> None:
keyboard_shortcuts_info.setLayout(keyboard_shortcuts_layout)

aboutInfo: QtWidgets.QGroupBox = QtWidgets.QGroupBox(title="About")
vertical(
label(
"QtTool is the work of Conrad Stansbury, with much inspiration "
"and thanks to the authors of ImageTool. QtTool is distributed "
"as part of the PyARPES data analysis framework.",
wordWrap=True,
),
label(
"Complaints and feature requests should be directed to [email protected].",
wordWrap=True,
),
the_label1 = label(
"QtTool is the work of Conrad Stansbury, with much inspiration "
"and thanks to the authors of ImageTool. QtTool is distributed "
"as part of the PyARPES data analysis framework.",
)
the_label1.setWordWrap(on=True)
the_label2 = label(
"Complaints and feature requests should be directed to [email protected].",
)
the_label2.setWordWrap(on=True)
vertical(the_label1, the_label2)

from . import qt_info

Expand Down
Loading

0 comments on commit ddddf18

Please sign in to comment.