Skip to content

Commit

Permalink
signal: complete _ltisys.* (#385)
Browse files Browse the repository at this point in the history
  • Loading branch information
jorenham authored Dec 30, 2024
2 parents 5ffe837 + 960fb36 commit e116f8d
Show file tree
Hide file tree
Showing 4 changed files with 601 additions and 133 deletions.
11 changes: 2 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -188,15 +188,8 @@ But if you find that this isn't the case, then don't hesitate to open an issue o
The entire public API of `scipy` is **fully annotated** and **verifiably valid**.
For the most part, this can also be said about `scipy`'s private API and other internal machinery.

### `Untyped`

A small portion of the stubs uses the `Untyped` type (an alias of `Any`) as a "placeholder" or "to-do" annotation.
In those cases static type-checkers won't do any type-checking, and won't bother you with errors or warnings, so you probably
won't even notice it.
The current goal of `scipy-stubs` is to replace all `Untyped` annotations with more meaningful ones.

At the moment, out of the 21 `scipy.*` subpackages, the only one that still has (some) `Untyped` annotations, is `scipy.signal`.
See [scipy-stubs#99](https://github.com/jorenham/scipy-stubs/issues/99) for an overview.
Note that this does not mean that all annotations are optimal, and some might even be incorrect. If you encounter this, it would
help a lot if you could open an issue or a PR for it.

## Contributing

Expand Down
16 changes: 1 addition & 15 deletions scipy-stubs/_typing.pyi
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# NOTE: This private(!) module only exists in `if typing.TYPE_CHECKING: ...` and in `.pyi` stubs

from os import PathLike
from collections.abc import Callable, Sequence
from collections.abc import Sequence
from types import TracebackType
from typing import IO, Any, Literal, Protocol, TypeAlias, type_check_only
from typing_extensions import LiteralString, Self, TypeVar
Expand All @@ -28,12 +28,6 @@ __all__ = [
"OrderCF",
"OrderKACF",
"ToRNG",
"Untyped",
"UntypedArray",
"UntypedCallable",
"UntypedDict",
"UntypedList",
"UntypedTuple",
"_FortranFunction",
]

Expand Down Expand Up @@ -63,14 +57,6 @@ class _FortranFunction(Protocol):
def typecode(self, /) -> LiteralString: ...
def __call__(self, /, *args: object, **kwargs: object) -> object: ...

# placeholders for missing annotations
Untyped: TypeAlias = Any
UntypedTuple: TypeAlias = tuple[Untyped, ...]
UntypedList: TypeAlias = list[Untyped]
UntypedDict: TypeAlias = dict[Untyped, Untyped]
UntypedCallable: TypeAlias = Callable[..., Untyped]
UntypedArray: TypeAlias = onp.Array[Any, np.generic]

# I/O
_ByteSOrStr = TypeVar("_ByteSOrStr", bytes, str)
FileName: TypeAlias = str | PathLike[str]
Expand Down
103 changes: 84 additions & 19 deletions scipy-stubs/signal/_lti_conversion.pyi
Original file line number Diff line number Diff line change
@@ -1,47 +1,112 @@
from typing import Any, Literal, TypeAlias
from typing import Literal, TypeAlias, TypeVar, overload

import numpy as np
import optype.numpy as onp
from ._ltisys import lti
import optype.numpy.compat as npc
from ._ltisys import dlti, lti

__all__ = ["abcd_normalize", "cont2discrete", "ss2tf", "ss2zpk", "tf2ss", "zpk2ss"]

###

_ToSystemTF: TypeAlias = tuple[onp.ToComplex2D, onp.ToComplex1D] # (num, den)
_InSystemZPK: TypeAlias = tuple[onp.ToComplex1D, onp.ToComplex1D, onp.ToFloat] # (z, p, k)
_InSystemSS: TypeAlias = tuple[onp.ToComplex2D, onp.ToComplex2D, onp.ToComplex2D, onp.ToComplex2D] # (A, B, C, D)
_ToSystemZPK: TypeAlias = tuple[onp.ToComplex1D, onp.ToComplex1D, onp.ToFloat] # (z, p, k)
_ToSystemSS: TypeAlias = tuple[onp.ToComplex2D, onp.ToComplex2D, onp.ToComplex2D, onp.ToComplex2D] # (A, B, C, D)

_Inexact1D: TypeAlias = onp.Array1D[np.inexact[Any]]
_Inexact2D: TypeAlias = onp.Array2D[np.inexact[Any]]
_InexactT = TypeVar("_InexactT", bound=npc.inexact, default=npc.inexact)
_Inexact1D: TypeAlias = onp.Array1D[_InexactT]
_Inexact2D: TypeAlias = onp.Array2D[_InexactT]

_SystemTF: TypeAlias = tuple[_Inexact2D, _Inexact1D]
_SystemZPK: TypeAlias = tuple[_Inexact1D, _Inexact1D, float]
_SystemSS: TypeAlias = tuple[_Inexact2D, _Inexact2D, _Inexact2D, _Inexact2D]
_SystemTF: TypeAlias = tuple[_Inexact2D[_InexactT], _Inexact1D[_InexactT]]
_SystemZPK: TypeAlias = tuple[_Inexact1D[_InexactT], _Inexact1D[_InexactT], float | np.float64]
_SystemSS: TypeAlias = tuple[_Inexact2D[_InexactT], _Inexact2D[_InexactT], _Inexact2D[_InexactT], _Inexact2D[_InexactT]]

_Method: TypeAlias = Literal["gbt", "bilinear", "euler", "backward_diff", "foh", "impulse", "zoh"]
_DiscretizeMethod: TypeAlias = Literal["gbt", "bilinear", "euler", "backward_diff", "foh", "impulse", "zoh"]

###
@overload
def abcd_normalize(
A: onp.ToFloat2D | None = None,
B: onp.ToFloat2D | None = None,
C: onp.ToFloat2D | None = None,
D: onp.ToFloat2D | None = None,
) -> _SystemTF[npc.floating]: ...
@overload
def abcd_normalize(
A: onp.ToComplex2D | None = None,
B: onp.ToComplex2D | None = None,
C: onp.ToComplex2D | None = None,
D: onp.ToComplex2D | None = None,
) -> _SystemTF: ...

#
@overload
def tf2ss(num: onp.ToFloat2D, den: onp.ToFloat1D) -> _SystemSS[npc.floating]: ...
@overload
def tf2ss(num: onp.ToComplex2D, den: onp.ToComplex1D) -> _SystemSS: ...
def zpk2ss(z: onp.ToComplex1D, p: onp.ToComplex1D, k: onp.ToFloat) -> _SystemSS: ...

#
@overload
def ss2tf(
A: onp.ToFloat2D,
B: onp.ToFloat2D,
C: onp.ToFloat2D,
D: onp.ToFloat2D,
input: onp.ToInt = 0,
) -> _SystemTF[npc.floating]: ...
@overload
def ss2tf(A: onp.ToComplex2D, B: onp.ToComplex2D, C: onp.ToComplex2D, D: onp.ToComplex2D, input: onp.ToInt = 0) -> _SystemTF: ...

#
@overload
def zpk2ss(z: onp.ToFloat1D, p: onp.ToFloat1D, k: onp.ToFloat) -> _SystemSS[npc.floating]: ...
@overload
def zpk2ss(z: onp.ToComplex1D, p: onp.ToComplex1D, k: onp.ToFloat) -> _SystemSS: ...

#
@overload
def ss2zpk(
A: onp.ToFloat2D,
B: onp.ToFloat2D,
C: onp.ToFloat2D,
D: onp.ToFloat2D,
input: onp.ToInt = 0,
) -> _SystemZPK[npc.floating]: ...
@overload
def ss2zpk(
A: onp.ToComplex2D,
B: onp.ToComplex2D,
C: onp.ToComplex2D,
D: onp.ToComplex2D,
input: onp.ToInt = 0,
) -> _SystemZPK: ...

# TODO: overload on lti subclasses
@overload
def cont2discrete(
system: lti,
dt: float,
method: _DiscretizeMethod = "zoh",
alpha: onp.ToJustFloat | None = None,
) -> dlti: ...
@overload
def cont2discrete(
system: _ToSystemTF,
dt: float,
method: _DiscretizeMethod = "zoh",
alpha: onp.ToJustFloat | None = None,
) -> tuple[_Inexact2D[_InexactT], _Inexact1D[_InexactT], float]: ...
@overload
def cont2discrete(
system: _ToSystemZPK,
dt: float,
method: _DiscretizeMethod = "zoh",
alpha: onp.ToJustFloat | None = None,
) -> tuple[_Inexact1D[_InexactT], _Inexact1D[_InexactT], float, float]: ...
@overload
def cont2discrete(
system: lti | _ToSystemTF | _InSystemZPK | _InSystemSS,
system: _ToSystemSS,
dt: float,
method: _Method = "zoh",
alpha: onp.ToFloat | None = None,
) -> (
tuple[_Inexact2D, _Inexact1D, float]
| tuple[_Inexact1D, _Inexact1D, float, float]
| tuple[_Inexact2D, _Inexact2D, _Inexact2D, _Inexact2D, float]
): ...
method: _DiscretizeMethod = "zoh",
alpha: onp.ToJustFloat | None = None,
) -> tuple[_Inexact2D[_InexactT], _Inexact2D[_InexactT], _Inexact2D[_InexactT], _Inexact2D[_InexactT], float]: ...
Loading

0 comments on commit e116f8d

Please sign in to comment.