Skip to content

windowed_log_signature

from tensorsig.classical.windows import windowed_log_signature

Sliding-window log-signatures via windowed signatures + projection.

Windows are formed with size=window_size and step=hop_size. Signatures are obtained using windowed_signature (Chen reuse of streaming prefixes), then converted to log-signatures and projected to the requested basis.

Parameters:

Name Type Description Default
path Tensor

Tensor of shape (batch, length, dim) representing batched paths. For a single path, pass path.unsqueeze(0) to add a batch dimension.

required
depth int

Maximum depth to truncate log-signature computation. The output dimension is logsigdim(dim, depth) for mode="hall" and logsigdim_words(dim, depth) for mode="words".

required
window_size int

Size of the window to use for the signature.

required
hop_size int

Hop size to use for the signature.

required
mode str

Basis for the log-signature coordinates: "words" (default) or "hall". "words" is only available with method="default".

'words'

Returns:

Type Description
Tensor

Tensor of shape (batch, num_windows, D) where num_windows = 1 + (length - window_size) // hop_size and D = logsigdim_words(dim, depth) if mode="words" else logsigdim(dim, depth) for Hall.

Raises:

Type Description
ValueError

If path is not three-dimensional or if mode is unsupported.

Source code in src/tensorsig/classical/log_signature.py
def windowed_log_signature(
    path: Tensor,
    depth: int,
    window_size: int,
    hop_size: int,
    mode: str = "words",
) -> Tensor:
    """Sliding-window log-signatures via windowed signatures + projection.

    Windows are formed with ``size=window_size`` and ``step=hop_size``.
    Signatures are obtained using
    [`windowed_signature`][tensorsig.classical.windows.windowed_signature]
    (Chen reuse of streaming prefixes), then converted to log-signatures and
    projected to the requested basis.

    Parameters
    ----------
    path : Tensor
        Tensor of shape ``(batch, length, dim)`` representing batched paths.
        For a single path, pass ``path.unsqueeze(0)`` to add a batch dimension.
    depth : int
        Maximum depth to truncate log-signature computation. The output dimension
        is ``logsigdim(dim, depth)`` for ``mode="hall"`` and
        ``logsigdim_words(dim, depth)`` for ``mode="words"``.
    window_size : int
        Size of the window to use for the signature.
    hop_size : int
        Hop size to use for the signature.
    mode : str, optional
        Basis for the log-signature coordinates: "words" (default) or "hall".
        "words" is only available with ``method=\"default\"``.

    Returns
    -------
    Tensor
        Tensor of shape ``(batch, num_windows, D)`` where
        ``num_windows = 1 + (length - window_size) // hop_size`` and
        ``D = logsigdim_words(dim, depth)`` if ``mode=\"words\"`` else
        ``logsigdim(dim, depth)`` for Hall.

    Raises
    ------
    ValueError
        If ``path`` is not three-dimensional or if ``mode`` is unsupported.
    """
    if path.ndim != 3:
        msg = (
            f"Path must be of shape (batch, path_length, path_dim); got {path.shape}. "
            "Wrap a single path with path.unsqueeze(0)."
        )
        raise ValueError(msg)

    mode = (mode or "words").lower()
    if mode not in {"hall", "words"}:
        raise ValueError(f"Unsupported mode '{mode}'. Use 'hall' or 'words'.")

    batch, _, width = path.shape

    # Reuse efficient streaming→windowed signature computation.
    window_sig = windowed_signature(
        path,
        depth=depth,
        window_size=window_size,
        hop_size=hop_size,
    )  # (batch, num_windows, sigdim)

    batch_windows, num_windows = batch, window_sig.shape[1]

    flattened = window_sig.reshape(batch_windows * num_windows, -1)
    sig_tensors = _unflatten_signature(flattened, width, depth)
    log_sig_tensors = _signature_to_logsignature_tensor(sig_tensors, width, depth)

    projector = _project_to_hall_basis if mode == "hall" else _project_to_words_basis
    projected = projector(log_sig_tensors, width, depth)

    return projected.reshape(batch_windows, num_windows, -1)