|
| 1 | +# -*- coding: ascii -*- |
| 2 | + |
| 3 | +""" |
| 4 | +Columns Selector Transformer Implementation. |
| 5 | +
|
| 6 | +This module provides a transformer that allows selecting specific columns |
| 7 | +from multivariate time series data. |
| 8 | +""" |
| 9 | + |
| 10 | +__author__ = "Danil Totmyanin" |
| 11 | +__copyright__ = "Copyright (c) 2026 PySATL project" |
| 12 | +__license__ = "SPDX-License-Identifier: MIT" |
| 13 | + |
| 14 | +import numpy as np |
| 15 | + |
| 16 | +from pysatl_cpd.core.data_providers.idata_provider import DataProvider |
| 17 | +from pysatl_cpd.core.data_providers.numpy_data_provider import ( |
| 18 | + NDArrayMultivariateProvider, |
| 19 | + NDArrayUnivariateProvider, |
| 20 | +) |
| 21 | +from pysatl_cpd.core.data_transformers.idata_transformer import IDataTransformer |
| 22 | + |
| 23 | + |
| 24 | +class ColumnsSelectorTransformer(IDataTransformer[np.ndarray, np.ndarray | float]): |
| 25 | + """ |
| 26 | + Transformer for selecting specific columns from multivariate data. |
| 27 | +
|
| 28 | + If a single integer index is provided, it transforms multivariate data |
| 29 | + into univariate data. If a list of indices is provided, it returns |
| 30 | + multivariate data containing only the specified columns. |
| 31 | +
|
| 32 | + Parameters |
| 33 | + ---------- |
| 34 | + columns : list[int] or int |
| 35 | + Indices of columns to select from the input multivariate array. |
| 36 | + """ |
| 37 | + |
| 38 | + def __init__(self, columns: list[int] | int) -> None: |
| 39 | + self.cols = columns |
| 40 | + |
| 41 | + @property |
| 42 | + def name(self) -> str: |
| 43 | + """ |
| 44 | + Return a unique name including selected column indices. |
| 45 | +
|
| 46 | + Returns |
| 47 | + ------- |
| 48 | + str |
| 49 | + Formatted name like 'Col_0' or 'Cols_0_2_3'. |
| 50 | + """ |
| 51 | + if isinstance(self.cols, int): |
| 52 | + return f"Col_{self.cols}" |
| 53 | + cols_str = "_".join(map(str, self.cols)) |
| 54 | + return f"Cols_{cols_str}" |
| 55 | + |
| 56 | + def transform(self, provider: DataProvider[np.ndarray]) -> DataProvider[np.ndarray | float]: |
| 57 | + """ |
| 58 | + Extract selected columns and wrap into a new NumPy data provider. |
| 59 | +
|
| 60 | + Parameters |
| 61 | + ---------- |
| 62 | + provider : DataProvider[np.ndarray] |
| 63 | + Multivariate data provider yielding 1-D NumPy arrays. |
| 64 | +
|
| 65 | + Returns |
| 66 | + ------- |
| 67 | + DataProvider[Any] |
| 68 | + NDArrayUnivariateProvider if `columns` is int, |
| 69 | + NDArrayMultivariateProvider if `columns` is list[int]. |
| 70 | +
|
| 71 | + Raises |
| 72 | + ------ |
| 73 | + ValueError |
| 74 | + If the data provided by the source is not 2-dimensional. |
| 75 | + """ |
| 76 | + raw_nd_data = np.array(list(provider)) |
| 77 | + |
| 78 | + if raw_nd_data.ndim < 2: |
| 79 | + raise ValueError( |
| 80 | + f"ColumnsSelectorTransformer expects 2D data, " |
| 81 | + f"got {raw_nd_data.ndim}D data from provider '{provider.name}'." |
| 82 | + ) |
| 83 | + |
| 84 | + cols_data = raw_nd_data[:, self.cols] |
| 85 | + |
| 86 | + new_provider_name = f"{provider.name}_{self.name}" |
| 87 | + |
| 88 | + if isinstance(self.cols, int): |
| 89 | + return NDArrayUnivariateProvider(data=cols_data, name=new_provider_name) |
| 90 | + |
| 91 | + return NDArrayMultivariateProvider(data=cols_data, name=new_provider_name) |
0 commit comments