Skip to content

Commit 15b9497

Browse files
committed
Future typing of Basis Smoother.
Currently not working in Mypy because of python/mypy#11347
1 parent 18baf7e commit 15b9497

2 files changed

Lines changed: 56 additions & 4 deletions

File tree

‎skfda/preprocessing/smoothing/_basis.py‎

Lines changed: 49 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@
66
"""
77
from __future__ import annotations
88

9-
from typing import Optional
9+
from typing import Generic, Optional, TypeVar, overload
1010

1111
import numpy as np
12-
from typing_extensions import Final
12+
from typing_extensions import Final, Literal
1313

1414
from ..._utils import _cartesian_product, _to_grid_points
1515
from ...misc.lstsq import LstsqMethod, solve_regularized_weighted_lstsq
@@ -19,8 +19,10 @@
1919
from ...representation.basis import Basis
2020
from ._linear import _LinearSmoother
2121

22+
T = TypeVar("T", Literal[True], Literal[False])
2223

23-
class BasisSmoother(_LinearSmoother):
24+
25+
class BasisSmoother(Generic[T], _LinearSmoother):
2426
r"""
2527
Transform raw data to a smooth functional form.
2628
@@ -203,6 +205,33 @@ class BasisSmoother(_LinearSmoother):
203205

204206
_required_parameters = ["basis"]
205207

208+
@overload
209+
def __init__(
210+
self: BasisSmoother[Literal[False]],
211+
basis: Basis,
212+
*,
213+
smoothing_parameter: float = 1.0,
214+
weights: Optional[NDArrayFloat] = None,
215+
regularization: Optional[TikhonovRegularization[FDataGrid]] = None,
216+
output_points: Optional[GridPointsLike] = None,
217+
method: LstsqMethod = 'svd',
218+
) -> None:
219+
pass
220+
221+
@overload
222+
def __init__(
223+
self: BasisSmoother[T],
224+
basis: Basis,
225+
*,
226+
smoothing_parameter: float = 1.0,
227+
weights: Optional[NDArrayFloat] = None,
228+
regularization: Optional[TikhonovRegularization[FDataGrid]] = None,
229+
output_points: Optional[GridPointsLike] = None,
230+
method: LstsqMethod = 'svd',
231+
return_basis: T,
232+
) -> None:
233+
pass
234+
206235
def __init__(
207236
self,
208237
basis: Basis,
@@ -271,7 +300,7 @@ def fit(
271300
self,
272301
X: FDataGrid,
273302
y: None = None,
274-
) -> BasisSmoother:
303+
) -> BasisSmoother[T]:
275304
"""Compute the hat matrix for the desired output points.
276305
277306
Args:
@@ -294,6 +323,22 @@ def fit(
294323

295324
return self
296325

326+
@overload
327+
def transform(
328+
self: BasisSmoother[Literal[False]],
329+
X: FDataGrid,
330+
y: None = None,
331+
) -> FDataGrid:
332+
pass
333+
334+
@overload
335+
def transform(
336+
self: BasisSmoother[Literal[True]],
337+
X: FDataGrid,
338+
y: None = None,
339+
) -> FDataBasis:
340+
pass
341+
297342
def transform(
298343
self,
299344
X: FDataGrid,

‎tests/test_smoothing.py‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,13 @@ def test_cholesky(self):
7979
x = np.sin(2 * np.pi * t) + np.cos(2 * np.pi * t)
8080
basis = BSpline((0, 1), n_basis=5)
8181
fd = FDataGrid(data_matrix=x, grid_points=t)
82+
83+
reveal_type(smoothing.BasisSmoother(basis).transform(fd))
84+
reveal_type(smoothing.BasisSmoother(
85+
basis, return_basis=False).transform(fd))
86+
reveal_type(smoothing.BasisSmoother(
87+
basis, return_basis=True).transform(fd))
88+
8289
smoother = smoothing.BasisSmoother(
8390
basis=basis,
8491
smoothing_parameter=10,

0 commit comments

Comments
 (0)