Skip to content
Draft
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
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,7 @@ docs_env
.venv
.env
uv.lock

# C++ build artifacts
*.so
_ext.pyi
86 changes: 86 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
cmake_minimum_required(VERSION 3.15...3.26)

project(darts LANGUAGES CXX)

if (NOT SKBUILD)
message(WARNING "\
This CMake file is meant to be executed using 'scikit-build'. Running
it directly will almost certainly not produce the desired result. If
you are a user trying to install this package, please use the command
below, which will install all necessary build dependencies, compile
the package in an isolated environment, and then install it.
=====================================================================
$ pip install .
=====================================================================
If you are a software developer, and this is your own package, then
it is usually much more efficient to install the build dependencies
in your environment once and use the following command that avoids
a costly creation of a new virtual environment at every compilation:
=====================================================================
$ pip install nanobind scikit-build-core[pyproject]
$ pip install --no-build-isolation -ve .
=====================================================================
You may optionally add -Ceditable.rebuild=true to auto-rebuild when
the package is imported. Otherwise, you need to re-run the above
after editing C++ files.")
endif()

# Try to import all Python components potentially needed by nanobind
find_package(Python 3.10
REQUIRED COMPONENTS Interpreter Development.Module
OPTIONAL_COMPONENTS Development.SABIModule)

# Import nanobind through CMake's find_package mechanism
find_package(nanobind CONFIG REQUIRED)

include_directories(include)

# We are now ready to compile the actual extension module
nanobind_add_module(
# Name of the extension
_ext

# This extension is free-threaded (only applies to Python 3.14t+)
FREE_THREADED

# Target the stable ABI for Python 3.12+, which reduces
# the number of binary wheels that must be built. This
# does nothing on older Python versions
STABLE_ABI

# Build libnanobind statically and merge it into the
# extension (which itself remains a shared library)
#
# If your project builds multiple extensions, you can
# replace this flag by NB_SHARED to conserve space by
# reusing a shared libnanobind across libraries
NB_STATIC

# Source code goes here
src/bind.cpp
src/dtw.cpp
)

nanobind_add_stub(
_ext_stub
MODULE _ext
OUTPUT _ext.pyi
PYTHON_PATH $<TARGET_FILE_DIR:_ext>
MARKER_FILE py.typed
DEPENDS _ext
)

# Keep generated typing artifacts visible to language servers during local/editable
# development. VS Code/Pylance resolves this workspace package from source.
add_custom_command(
TARGET _ext_stub POST_BUILD
COMMAND ${CMAKE_COMMAND} -E copy_if_different
${CMAKE_CURRENT_BINARY_DIR}/_ext.pyi
${CMAKE_CURRENT_SOURCE_DIR}/darts/_ext.pyi
)

# Install directive for scikit-build-core
install(TARGETS _ext LIBRARY DESTINATION darts)

# Install the generated stub file as well, so that it can be included in the wheel
install(FILES ${CMAKE_CURRENT_BINARY_DIR}/py.typed ${CMAKE_CURRENT_BINARY_DIR}/_ext.pyi DESTINATION darts)
1 change: 1 addition & 0 deletions MANIFEST.in
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
include include/*.h
26 changes: 19 additions & 7 deletions darts/dataprocessing/dtw/cost_matrix.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@

import array
from abc import ABC, abstractmethod
from collections.abc import Generator
from itertools import repeat
from typing import Any

import numpy as np

Expand All @@ -30,15 +32,15 @@ def fill(self, value: float):
pass

@abstractmethod
def __getitem__(self, item):
def __getitem__(self, item) -> Any:
pass

@abstractmethod
def __setitem__(self, key, value):
pass

@abstractmethod
def __iter__(self):
def __iter__(self) -> Generator:
pass

@abstractmethod
Expand Down Expand Up @@ -77,14 +79,24 @@ def _from_window(window: Window):
return DenseCostMatrix(window.n, window.m)


class DenseCostMatrix(np.ndarray, CostMatrix):
def __new__(self, n, m):
class DenseCostMatrix(CostMatrix):
def __init__(self, n: int, m: int, dense: np.ndarray | None = None):
self.n = n
self.m = m
return super().__new__(self, (n + 1, m + 1), float)
self.dense = np.ndarray((n + 1, m + 1), float) if dense is None else dense

def to_dense(self) -> np.ndarray:
return self[1:, 1:]
def to_dense(self, copy: bool = False) -> np.ndarray:
data = self.dense[1:, 1:]
return data.copy() if copy else data

def fill(self, value: float):
self.dense.fill(value)

def __getitem__(self, item):
return self.dense[item]

def __setitem__(self, key, value):
self.dense[key] = value

def __iter__(self):
for n in range(1, self.n):
Expand Down
6 changes: 5 additions & 1 deletion darts/dataprocessing/dtw/dtw.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
import pandas as pd

from darts import TimeSeries
from darts.dataprocessing.dtw.cost_matrix import CostMatrix
from darts._ext import dtw_cost_matrix_no_window
from darts.dataprocessing.dtw.cost_matrix import CostMatrix, DenseCostMatrix
from darts.dataprocessing.dtw.window import CRWindow, NoWindow, Window
from darts.logging import get_logger, raise_if, raise_if_not

Expand Down Expand Up @@ -362,6 +363,9 @@ def dtw(
logger,
)
cost_matrix = _fast_dtw(values_x, values_y, distance, multi_grid_radius)
elif isinstance(window, NoWindow):
cost_array = dtw_cost_matrix_no_window(values_x, values_y)
cost_matrix = DenseCostMatrix(window.n, window.m, cost_array)
else:
cost_matrix = _dtw_cost_matrix(values_x, values_y, distance, window)

Expand Down
Loading
Loading