Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

First mypy refactorings #1204

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,9 @@ repos:
hooks:
- id: pyupgrade
args: ['--py37-plus']

- repo: https://github.com/pre-commit/mirrors-mypy
rev: v0.971
hooks:
- id: mypy
files: ^darts
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ Darts is still in an early development phase, and we cannot always guarantee bac
- Added support for retraining model(s) every `n` iteration and on custom condition in `historical_forecasts` method of `ForecastingModel` abstract class. Addressed issues [#135](https://github.com/unit8co/darts/issues/135) and [#623](https://github.com/unit8co/darts/issues/623) by [Francesco Bruzzesi](https://github.com/fbruzzesi).
- New LayerNorm alternatives, RMSNorm and LayerNormNoBias [#1113](https://github.com/unit8co/darts/issues/1113) by [Greg DeVos](https://github.com/gdevos010).
- Fixed type hinting for ExponentialSmoothing model [#1185](https://https://github.com/unit8co/darts/pull/1185) by [Rijk van der Meulen](https://github.com/rijkvandermeulen)
- Enabled set-up for running mypy in pre-commit hooks and performed necessary mypy refactorings for dtw [#1202](https://https://github.com/unit8co/darts/pull/1202) by [Rijk van der Meulen](https://github.com/rijkvandermeulen)

[Full Changelog](https://github.com/unit8co/darts/compare/0.21.0...master)

Expand Down
12 changes: 5 additions & 7 deletions darts/dataprocessing/dtw/cost_matrix.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,14 +93,12 @@ def __init__(self, window: CRWindow):
self.n = window.n
self.m = window.m
self.window = window
self.offsets = np.empty(self.n + 2, dtype=int)
self.column_ranges = window.column_ranges
self.offsets[0] = 0
np.cumsum(window.column_lengths(), out=self.offsets[1:])

len = self.offsets[-1]

self.offsets = array.array("i", self.offsets)
offsets = np.empty(self.n + 2, dtype=int)
offsets[0] = 0
np.cumsum(window.column_lengths(), out=offsets[1:])
len = offsets[-1]
self.offsets = array.array("i", offsets)
self.dense = array.array("f", repeat(np.inf, len))

def fill(self, value):
Expand Down
28 changes: 15 additions & 13 deletions darts/dataprocessing/dtw/dtw.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import copy
from typing import Callable, Union
from typing import Callable, Optional, Tuple, Union

import numpy as np
import pandas as pd
Expand All @@ -14,14 +14,15 @@

logger = get_logger(__name__)

SeriesValue = Union[np.ndarray, np.floating]
DistanceFunc = Callable[[SeriesValue, SeriesValue], float]
DistanceFunc = Union[
Callable[[float, float], float], Callable[[np.ndarray, np.ndarray], np.ndarray]
]


# CORE ALGORITHM
def _dtw_cost_matrix(
x: np.ndarray, y: np.ndarray, dist: DistanceFunc, window: Window
) -> np.ndarray:
) -> CostMatrix:

dtw = CostMatrix._from_window(window)

Expand Down Expand Up @@ -120,6 +121,7 @@ def _fast_dtw(
m = len(y)
min_size = radius + 2

window: Union[NoWindow, CRWindow]
if n < min_size or m < min_size or radius == -1:
window = NoWindow()
window.init_size(n, m)
Expand Down Expand Up @@ -154,7 +156,7 @@ def __init__(self, series1: TimeSeries, series2: TimeSeries, cost: CostMatrix):
self.series2 = series2
self.cost = cost

from ._plot import plot, plot_alignment
from ._plot import plot, plot_alignment # type: ignore

def path(self) -> np.ndarray:
"""
Expand All @@ -168,7 +170,7 @@ def path(self) -> np.ndarray:

if hasattr(self, "_path"):
return self._path
self._path = _dtw_path(self.cost)
self._path: np.ndarray = _dtw_path(self.cost)
return self._path

def distance(self) -> float:
Expand All @@ -191,10 +193,10 @@ def mean_distance(self) -> float:
return self._mean_distance

path = self.path()
self._mean_distance = self.distance() / len(path)
self._mean_distance: float = self.distance() / len(path)
return self._mean_distance

def warped(self) -> (TimeSeries, TimeSeries):
def warped(self) -> Tuple[TimeSeries, TimeSeries]:
"""
Warps the two time series according to the warp path returned by .path(), which minimizes
the pair-wise distance.
Expand Down Expand Up @@ -254,19 +256,19 @@ def warped(self) -> (TimeSeries, TimeSeries):
)


def default_distance_multi(x_values: np.ndarray, y_values: np.ndarray):
def default_distance_multi(x_values: np.ndarray, y_values: np.ndarray) -> np.ndarray:
return np.sum(np.abs(x_values - y_values))


def default_distance_uni(x_value: float, y_value: float):
def default_distance_uni(x_value: float, y_value: float) -> float:
return abs(x_value - y_value)


def dtw(
series1: TimeSeries,
series2: TimeSeries,
window: Window = NoWindow(),
distance: Union[DistanceFunc, None] = None,
distance: Optional[DistanceFunc] = None,
multi_grid_radius: int = -1,
) -> DTWAlignment:
"""
Expand Down Expand Up @@ -338,14 +340,14 @@ def dtw(
values_y = series2.values(copy=False)

raise_if(
np.any(np.isnan(values_x)),
bool(np.any(np.isnan(values_x))),
"Dynamic Time Warping does not support nan values. "
"You can use the module darts.utils.missing_values to fill them, "
"before passing them to dtw.",
logger,
)
raise_if(
np.any(np.isnan(values_y)),
bool(np.any(np.isnan(values_y))),
"Dynamic Time Warping does not support nan values. "
"You can use the module darts.utils.missing_values to fill them,"
"before passing it into dtw",
Expand Down
30 changes: 16 additions & 14 deletions darts/dataprocessing/dtw/window.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import array
from abc import ABC, abstractmethod
from math import atan, tan
from typing import Optional, Tuple

import numpy as np

Expand Down Expand Up @@ -30,7 +31,7 @@ def __len__(self) -> int:
pass

@abstractmethod
def column_index(self, elem: (int, int)) -> int:
def column_index(self, elem: Tuple[int, int]) -> int:
"""
Parameters
----------
Expand Down Expand Up @@ -96,7 +97,7 @@ class NoWindow(Window):
def __len__(self):
return self.n * self.m + 1 # include (0,0) element

def column_index(self, elem: (int, int)):
def column_index(self, elem: Tuple[int, int]):
return elem[1] - 1

def column_length(self, column: int) -> int:
Expand All @@ -106,6 +107,7 @@ def column_lengths(self) -> np.ndarray:
result = np.empty(self.n + 1)
result.fill(self.m)
result[0] = 1
return result

def __iter__(self):
for i in range(1, self.n + 1):
Expand All @@ -117,7 +119,7 @@ def gtz(value): # greater than zero
return value if value > 0 else 0


class CRWindow:
class CRWindow(Window):
"""
Compressed row representation window.
Stores the range of active grid cells in each column.
Expand All @@ -128,7 +130,7 @@ class CRWindow:
length: int
column_ranges: array.array

def __init__(self, n: int, m: int, ranges: np.ndarray = None):
def __init__(self, n: int, m: int, ranges: Optional[np.ndarray] = None):
"""
Parameters
----------
Expand All @@ -154,25 +156,25 @@ def __init__(self, n: int, m: int, ranges: np.ndarray = None):
start = ranges[:, 0]
end = ranges[:, 1]

raise_if(np.any(start < 0), "Start must be >=0")
raise_if(np.any(end > m), "End must be <m")
raise_if(bool(np.any(start < 0)), "Start must be >=0")
raise_if(bool(np.any(end > m)), "End must be <m")

diff = np.maximum(end - start, 0)
self.length = np.sum(diff)

ranges[1:] += 1
ranges = ranges.flatten()
self.column_ranges = array.array("i", ranges)
else:
ranges = np.zeros((n + 1) * 2, dtype=int)
ranges[0::2] = self.m # start
ranges[1::2] = 0 # end
ranges = array.array("i", ranges)
ranges_array = array.array("i", ranges)

ranges[0] = 0
ranges[1] = 1
ranges_array[0] = 0
ranges_array[1] = 1
self.length = 1

self.column_ranges = array.array("i", ranges)
self.column_ranges = array.array("i", ranges_array)

def add_range(self, column: int, start: int, end: int):
"""
Expand Down Expand Up @@ -210,7 +212,7 @@ def add_range(self, column: int, start: int, end: int):
self.column_ranges[start_idx] = start
self.column_ranges[end_idx] = end

def add(self, elem: (int, int)):
def add(self, elem: Tuple[int, int]):
"""
Mark grid cell as active.

Expand All @@ -226,7 +228,7 @@ def column_length(self, column: int) -> int:
start, end = self.column_ranges[column]
return gtz(end - start)

def column_index(self, elem: (int, int)) -> int:
def column_index(self, elem: Tuple[int, int]) -> int:
i, j = elem

start, end = self.column_ranges[i]
Expand All @@ -235,7 +237,7 @@ def column_index(self, elem: (int, int)) -> int:
else:
return j - start

def __contains__(self, elem: (int, int)) -> bool:
def __contains__(self, elem: Tuple[int, int]) -> bool:
i, j = elem
start, end = self.column_ranges[i]
return start <= j < end
Expand Down
5 changes: 3 additions & 2 deletions darts/dataprocessing/pipeline.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ def __init__(

if transformers is None or len(transformers) == 0:
logger.warning("Empty pipeline created")
self._transformers: Sequence[BaseDataTransformer[TimeSeries]] = []
self._transformers: Sequence[BaseDataTransformer] = []
elif copy:
self._transformers = deepcopy(transformers)
else:
Expand Down Expand Up @@ -196,7 +196,7 @@ def inverse_transform(
)

for transformer in reversed(self._transformers):
data = transformer.inverse_transform(data)
data = transformer.inverse_transform(data) # type: ignore
return data
else:
for transformer in reversed(self._transformers):
Expand Down Expand Up @@ -237,6 +237,7 @@ def __getitem__(self, key: Union[int, slice]) -> "Pipeline":
logger,
)

transformers: Sequence[BaseDataTransformer]
if isinstance(key, int):
transformers = [self._transformers[key]]
else:
Expand Down
71 changes: 71 additions & 0 deletions mypy.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
[mypy]
python_version = 3.9

[mypy-numpy.*]
ignore_missing_imports = True

[mypy-pandas.*]
ignore_missing_imports = True

[mypy-sklearn.*]
ignore_missing_imports = True

[mypy-scipy.*]
ignore_missing_imports = True

[mypy-statsmodels.*]
ignore_missing_imports = True

[mypy-prophet.*]
ignore_missing_imports = True

[mypy-catboost.*]
ignore_missing_imports = True

[mypy-lightgbm.*]
ignore_missing_imports = True

[mypy-pmdarima.*]
ignore_missing_imports = True

[mypy-statsforecast.*]
ignore_missing_imports = True

[mypy-nfoursid.*]
ignore_missing_imports = True

[mypy-tbats.*]
ignore_missing_imports = True

[mypy-joblib.*]
ignore_missing_imports = True

[mypy-IPython.*]
ignore_missing_imports = True

[mypy-matplotlib.*]
ignore_missing_imports = True

[mypy-tqdm.*]
ignore_missing_imports = True

[mypy-darts.tests.*]
ignore_errors = True

[mypy-darts.datasets.*]
ignore_errors = True

[mypy-darts.metrics.*]
ignore_errors = True

[mypy-darts.models.*]
ignore_errors = True

[mypy-darts.utils.*]
ignore_errors = True

[mypy-darts.timeseries.*]
ignore_errors = True

[mypy-darts.dataprocessing.transformers.*]
ignore_errors = True
1 change: 1 addition & 0 deletions requirements/dev.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ pre-commit
pytest-cov
pyupgrade==2.31.0
testfixtures
mypy==0.971