Skip to content

Commit

Permalink
🧼 correction: add type hints more
Browse files Browse the repository at this point in the history
  • Loading branch information
arafune committed Sep 5, 2023
1 parent 1babc0c commit 62233af
Show file tree
Hide file tree
Showing 10 changed files with 129 additions and 76 deletions.
9 changes: 7 additions & 2 deletions arpes/fits/fit_models/dirac.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,10 +76,15 @@ def __init__(
self.set_param_hint("sigma_1", min=0.0)
self.set_param_hint("sigma_2", min=0.0)

def guess(self, data, x=None, **kwargs: Incomplete) -> lf.Parameters:
def guess(
self,
data: xr.DataArray | xr.Dataset,
x: None = None,
**kwargs: Incomplete,
) -> lf.Parameters:
"""Placeholder for making better heuristic guesses here."""
pars = self.make_params()

assert x is None
return update_param_vals(pars, self.prefix, **kwargs)

__init__.doc = lf.models.COMMON_INIT_DOC
Expand Down
16 changes: 11 additions & 5 deletions arpes/fits/lmfit_html_repr.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,19 @@
like including in group meeting notes.
"""

# pylint: disable=protected-access
from __future__ import annotations

from typing import TYPE_CHECKING

# pylint: disable=protected-access
import numpy as np
from lmfit import model

if TYPE_CHECKING:
from _typeshed import Incomplete


def repr_multiline_ModelResult(self: model.Model, **kwargs) -> str: # noqa: N802
def repr_multiline_ModelResult(self: model.Model, **kwargs: Incomplete) -> str: # noqa: N802
"""Provides a text-based multiline representation used in Qt based interactive tools."""
template = "ModelResult\n Converged: {success}\n "
template += "Components:\n {formatted_components}\n Parameters:\n{parameters}"
Expand All @@ -29,7 +35,7 @@ def repr_multiline_ModelResult(self: model.Model, **kwargs) -> str: # noqa: N80
)


def repr_html_ModelResult(self, **kwargs):
def repr_html_ModelResult(self, **kwargs: Incomplete):
"""Provides a better Jupyter representation of an `lmfit.ModelResult` instance."""
template = """
<div>
Expand All @@ -55,7 +61,7 @@ def repr_html_Model(self):
return template.format(name=self.name)


def repr_multiline_Model(self, **kwargs):
def repr_multiline_Model(self, **kwargs: Incomplete):
"""Provides a text-based multiline representation used in Qt based interactive tools."""
return self.name

Expand All @@ -64,7 +70,7 @@ def repr_multiline_Model(self, **kwargs):
SKIP_ON_SHORT = {"min", "max", "vary", "expr", "brute_step"}


def repr_html_Parameters(self, short=False):
def repr_html_Parameters(self, *, short: bool = False) -> str:
"""HTML representation for `lmfit.Parameters` instances."""
keys = sorted(self.keys())
template = """
Expand Down
9 changes: 8 additions & 1 deletion arpes/fits/lmfit_plot.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,17 @@
This is a very safe monkey patch as we defer to the original plotting function in cases
where it is appropriate, rather than reimplementing this functionality.
"""
from __future__ import annotations

from typing import TYPE_CHECKING

import matplotlib.pyplot as plt
import xarray as xr
from lmfit import model

if TYPE_CHECKING:
from _typeshed import Incomplete

original_plot = model.ModelResult.plot


Expand All @@ -18,7 +25,7 @@ def transform_lmfit_titles(label: str = "", *, is_title: bool = False) -> str:
return label or ""


def patched_plot(self, *args, **kwargs):
def patched_plot(self, *args: Incomplete, **kwargs: Incomplete):
"""A patch for `lmfit` summary plots in PyARPES.
Scientists like to have LaTeX in their plots,
Expand Down
3 changes: 2 additions & 1 deletion arpes/plotting/spatial.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
if TYPE_CHECKING:
from pathlib import Path

from _typeshed import Incomplete
from matplotlib.axes import Axes
from matplotlib.figure import Figure
from numpy.typing import NDArray
Expand Down Expand Up @@ -202,7 +203,7 @@ def plot_spatial_reference(
def reference_scan_spatial(
data: DataType,
out: str | Path = "",
**kwargs,
**kwargs: Incomplete,
) -> Path | tuple[Figure, NDArray[Axes]]:
"""Plots the spatial content of a dataset, useful as a quick reference."""
data_arr = normalize_to_spectrum(data)
Expand Down
44 changes: 26 additions & 18 deletions arpes/plotting/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
from collections.abc import Sequence
from datetime import UTC
from pathlib import Path
from typing import TYPE_CHECKING, Any, Literal
from typing import TYPE_CHECKING, Literal

import matplotlib as mpl
import matplotlib.pyplot as plt
Expand All @@ -36,6 +36,7 @@
if TYPE_CHECKING:
from collections.abc import Callable

from _typeshed import Incomplete
from matplotlib.image import AxesImage
from numpy.typing import NDArray

Expand Down Expand Up @@ -252,7 +253,7 @@ def v_gradient_fill(
def simple_ax_grid(
n_axes: int,
figsize: tuple[float, float] = (0, 0),
**kwargs: Any,
**kwargs: Incomplete,
) -> tuple[Figure, NDArray[Axes], NDArray[Axes]]:
"""Generates a square-ish set of axes and hides the extra ones.
Expand Down Expand Up @@ -554,7 +555,7 @@ def lineplot_arr(
method: Literal["plot", "scatter"] = "plot",
mask=None,
mask_kwargs=None,
**kwargs,
**kwargs: Incomplete,
) -> Axes:
"""Convenience method to plot an array with a mask over some other data."""
if mask_kwargs is None:
Expand Down Expand Up @@ -589,7 +590,7 @@ def plot_arr(
ax: Axes | None = None,
over=None,
mask: DataType | None = None,
**kwargs,
**kwargs: Incomplete,
) -> Axes:
"""Convenience method to plot an array with a mask over some other data."""
to_plot = arr if mask is None else mask
Expand All @@ -616,7 +617,7 @@ def imshow_mask(
ax: Axes | None = None,
over=None,
cmap: str | Colormap = "Reds",
**kwargs,
**kwargs: Incomplete,
) -> None:
"""Plots a mask by using a fixed color and transparency."""
assert over is not None
Expand Down Expand Up @@ -653,7 +654,7 @@ def imshow_arr(
vmin: float | None = None,
vmax: float | None = None,
cmap: str | Colormap = "Viridis",
**kwargs,
**kwargs: Incomplete,
) -> tuple[Axes, AxesImage]:
"""Similar to plt.imshow but users different default origin, and sets appropriate extents.
Expand Down Expand Up @@ -752,7 +753,7 @@ def inset_cut_locator(
ax: Axes | None = None,
location=None,
color: RGBColorType = "red",
**kwargs,
**kwargs: Incomplete,
):
"""Plots a reference cut location over a figure.
Expand Down Expand Up @@ -885,7 +886,7 @@ def generic_colorbar(
label: str = "",
cmap: str | Colormap = "Blues",
ticks=None,
**kwargs,
**kwargs: Incomplete,
) -> colorbar.Colorbar:
"""Generate colorbar.
Expand Down Expand Up @@ -922,7 +923,7 @@ def phase_angle_colorbar(
low: float = 0,
high: float = np.pi * 2,
ax: Axes | None = None,
**kwargs,
**kwargs: Incomplete,
) -> colorbar.Colorbar:
"""Generates a colorbar suitable for plotting an angle or value on a unit circle."""
assert isinstance(ax, Axes)
Expand All @@ -949,7 +950,7 @@ def temperature_colorbar(
high: float = 300,
ax: Axes | None = None,
cmap: str | Colormap = "Blues_r",
**kwargs,
**kwargs: Incomplete,
):
"""Generates a colorbar suitable for temperature data with fixed extent."""
assert isinstance(ax, Axes)
Expand All @@ -974,7 +975,7 @@ def delay_colorbar(
low: float = -1,
high: float = 1,
ax: Axes | None = None,
**kwargs,
**kwargs: Incomplete,
) -> colorbar.Colorbar:
assert isinstance(ax, Axes)
"""Generates a colorbar suitable for delay data.
Expand All @@ -1001,7 +1002,7 @@ def temperature_colorbar_around(
central,
range=50,
ax: Axes | None = None,
**kwargs,
**kwargs: Incomplete,
) -> colorbar.Colorbar:
"""Generates a colorbar suitable for temperature axes around a central value."""
assert isinstance(ax, Axes)
Expand Down Expand Up @@ -1083,7 +1084,7 @@ def generic_colorbarmap_for_data(
ax: Axes,
*,
keep_ticks: bool = True,
**kwargs,
**kwargs: Incomplete,
) -> tuple[colorbar.Colorbar, Callable[[float], RGBAColorType]]:
"""Generates a colorbar and colormap which is useful in general context.
Expand Down Expand Up @@ -1152,7 +1153,7 @@ def __init__(
prop=None,
label_color=None,
frameon=True,
**kwargs,
**kwargs: Incomplete,
) -> None:
"""Setup the scale bar and coordinate transforms to the parent axis."""
if not ax:
Expand Down Expand Up @@ -1208,7 +1209,14 @@ def load_data_for_figure(p: str | Path):
return pickle.load(f)


def savefig(desired_path, dpi: int = 400, data=None, save_data=None, paper=False, **kwargs):
def savefig(
desired_path,
dpi: int = 400,
data=None,
save_data=None,
paper=False,
**kwargs: Incomplete,
):
"""The PyARPES preferred figure saving routine.
Provides a number of conveniences over matplotlib's `savefig`:
Expand Down Expand Up @@ -1615,7 +1623,7 @@ class CoincidentLinesPlot:

linewidth = 3

def __init__(self, **kwargs) -> None:
def __init__(self, **kwargs: Incomplete) -> None:
self.ax = kwargs.pop("ax", plt.gca())
self.fig = kwargs.pop("fig", plt.gcf())
self.extra_kwargs = kwargs
Expand All @@ -1636,7 +1644,7 @@ def __init__(self, **kwargs) -> None:
self.handles = []
self.lines = [] # saved args and kwargs for plotting, does not verify coincidence

def add_line(self, *args, **kwargs):
def add_line(self, *args: Incomplete, **kwargs: Incomplete) -> None:
"""Adds an additional line into the collection to be drawn."""
assert not self.has_drawn
self.lines.append(
Expand All @@ -1646,7 +1654,7 @@ def add_line(self, *args, **kwargs):
),
)

def draw(self):
def draw(self) -> None:
"""Draw all of the lines after offsetting them slightly."""
self.has_drawn = True

Expand Down
28 changes: 19 additions & 9 deletions arpes/utilities/widgets.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
"""Wraps Qt widgets in ones which use rx for signaling, Conrad's personal preference."""
from __future__ import annotations

from pathlib import Path
from typing import TYPE_CHECKING

from PyQt5.QtWidgets import (
QCheckBox,
Expand All @@ -17,6 +19,9 @@
)
from rx.subject import BehaviorSubject, Subject

if TYPE_CHECKING:
from _typeshed import Incomplete

__all__ = (
"SubjectivePushButton",
"SubjectiveCheckBox",
Expand All @@ -33,7 +38,7 @@
class SubjectiveComboBox(QComboBox):
"""A QComboBox using rx instead of signals."""

def __init__(self, *args, **kwargs) -> None:
def __init__(self, *args: Incomplete, **kwargs: Incomplete) -> None:
"""Wrap signals in ``rx.BehaviorSubject``s."""
super().__init__(*args, **kwargs)
self.subject = BehaviorSubject(self.currentData())
Expand All @@ -43,7 +48,7 @@ def __init__(self, *args, **kwargs) -> None:
class SubjectiveSpinBox(QSpinBox):
"""A QSpinBox using rx instead of signals."""

def __init__(self, *args, **kwargs) -> None:
def __init__(self, *args: Incomplete, **kwargs: Incomplete) -> None:
"""Wrap signals in ``rx.BehaviorSubject``s."""
super().__init__(*args, **kwargs)
self.subject = BehaviorSubject(self.value())
Expand All @@ -58,7 +63,7 @@ def update_ui(self, value):
class SubjectiveTextEdit(QTextEdit):
"""A QTextEdit using rx instead of signals."""

def __init__(self, *args) -> None:
def __init__(self, *args: Incomplete) -> None:
"""Wrap signals in ``rx.BehaviorSubject``s."""
super().__init__(*args)
self.subject = BehaviorSubject(self.toPlainText())
Expand All @@ -74,7 +79,7 @@ def update_ui(self, value):
class SubjectiveSlider(QSlider):
"""A QSlider using rx instead of signals."""

def __init__(self, *args, **kwargs) -> None:
def __init__(self, *args: Incomplete, **kwargs: Incomplete) -> None:
"""Wrap signals in ``rx.BehaviorSubject``s."""
super().__init__(*args, **kwargs)
self.subject = BehaviorSubject(self.value())
Expand All @@ -89,7 +94,7 @@ def update_ui(self, value):
class SubjectiveLineEdit(QLineEdit):
"""A QLineEdit using rx instead of signals."""

def __init__(self, *args) -> None:
def __init__(self, *args: Incomplete) -> None:
"""Wrap signals in ``rx.BehaviorSubject``s."""
super().__init__(*args)
self.subject = BehaviorSubject(self.text())
Expand All @@ -105,7 +110,7 @@ def update_ui(self, value):
class SubjectiveRadioButton(QRadioButton):
"""A QRadioButton using rx instead of signals."""

def __init__(self, *args) -> None:
def __init__(self, *args: Incomplete) -> None:
"""Wrap signals in ``rx.BehaviorSubject``s."""
super().__init__(*args)
self.subject = BehaviorSubject(self.isChecked())
Expand All @@ -120,7 +125,12 @@ def update_ui(self, value):
class SubjectiveFileDialog(QWidget):
"""A file dialog implemented in Qt supporting single or multiple file selection."""

def __init__(self, *args, single=True, dialog_root=None) -> None:
def __init__(
self,
*args: Incomplete,
single: bool = True,
dialog_root: Path | None = None,
) -> None:
"""Sets up the file dialog widget.
In addition to wrapping signals in BehaviorSubject as we do elsewhere,
Expand Down Expand Up @@ -165,7 +175,7 @@ def get_files(self):
class SubjectivePushButton(QPushButton):
"""A QCheckBox using rx instead of signals."""

def __init__(self, *args, **kwargs) -> None:
def __init__(self, *args: Incomplete, **kwargs: Incomplete) -> None:
"""Wrap signals in ``rx.BehaviorSubject``s."""
super().__init__(*args)
self.subject = Subject()
Expand All @@ -175,7 +185,7 @@ def __init__(self, *args, **kwargs) -> None:
class SubjectiveCheckBox(QCheckBox):
"""A QCheckBox using rx instead of signals."""

def __init__(self, *args, **kwargs) -> None:
def __init__(self, *args: Incomplete, **kwargs: Incomplete) -> None:
"""Wrap signals in ``rx.BehaviorSubject``s."""
super().__init__(*args)
self.subject = BehaviorSubject(self.checkState())
Expand Down
Loading

0 comments on commit 62233af

Please sign in to comment.