o
    0j*                     @   s   d dl mZmZmZ d dlZd dlZddlm	Z	m
Z
 ddlmZ ddlmZmZ e
d	r0d dlZg d
ZejG dd dZeje	d	G dd dZejG dd dZejG dd dZejG dd dZejG dd dZdS )    )AnyDictListN   )class_requires_depsis_dep_available   )	benchmark   )load_from_dataframetime_featurejoblib)BuildTSDatasetTSCutOffTSNormalizeTimeFeature	TStoArray	TStoBatchc                       sR   e Zd ZdZdeeef f fddZdedefddZ	d	e
de
fd
dZ  ZS )r   a;  Truncates time series data to a specified length for training.

    This class provides a method to truncate or cut off time series data
    to a specified input length, optionally skipping some initial data
    points. This is useful for preparing data for training models that
    require a fixed input size.
    sizec                       t    || _dS )aK  Initializes the TSCutOff with size configurations.

        Args:
            size (Dict[str, int]): Dictionary containing size configurations,
                including 'in_chunk_len' for the input chunk length and
                optionally 'skip_chunk_len' for the number of initial data
                points to skip.
        N)super__init__r   )selfr   	__class__ n/var/www/html/Deteccion_Ine/venv/lib/python3.10/site-packages/paddlex/inference/models/common/ts/processors.pyr   /   s   
	
zTSCutOff.__init__ts_listreturnc                        fdd|D S )zApplies the cut off operation to a list of time series.

        Args:
            ts_list (List): List of time series data frames to be truncated.

        Returns:
            List: List of truncated time series data frames.
        c                       g | ]}  |qS r   )cutoff.0tsr   r   r   
<listcomp>D       z%TSCutOff.__call__.<locals>.<listcomp>r   r   r   r   r%   r   __call__;      	zTSCutOff.__call__r$   c                 C   sj   | j dd}t|| j d | k r'tdt| d| j d | j d   d|| j d |  d }|S )a\  Truncates a single time series data frame to the specified length.

        This method truncates the time series data to the specified input
        chunk length, optionally skipping some initial data points. It raises
        a ValueError if the time series is too short.

        Args:
            ts: A single time series data frame to be truncated.

        Returns:
            Any: The truncated time series data frame.

        Raises:
            ValueError: If the time series length is less than the required
            minimum length (input chunk length plus any skip chunk length).
        Zskip_chunk_lenr   Zin_chunk_lenz The length of the input data is z, but it should be at least z for training.N)r   getlen
ValueError)r   r$   Zskip_lents_datar   r   r   r!   F   s   $zTSCutOff.cutoff)__name__
__module____qualname____doc__r   strintr   r   r)   r   r!   __classcell__r   r   r   r   r   %   s
    r   c                       sf   e Zd ZdZdedeeef f fddZdee	j
 dee	j
 fdd	Zd
e	j
de	j
fddZ  ZS )r   a  Normalizes time series data using a pre-fitted scaler.

    This class normalizes specified columns of time series data using a
    pre-fitted scaler loaded from a specified path. It supports normalization
    of both target and feature columns as specified in the parameters.
    
scale_pathparams_infoc                    s    t    t|| _|| _dS )ab  Initializes the TSNormalize with a scaler and normalization parameters.

        Args:
            scale_path (str): Path to the pre-fitted scaler object file.
            params_info (Dict[str, Any]): Dictionary containing information
                about which columns to normalize, including 'target_cols'
                and 'feature_cols'.
        N)r   r   r   loadscalerr7   )r   r6   r7   r   r   r   r   j   s   
	
zTSNormalize.__init__r   r   c                    r   )a  Applies normalization to a list of time series data frames.

        Args:
            ts_list (List[pd.DataFrame]): List of time series data frames to be normalized.

        Returns:
            List[pd.DataFrame]: List of normalized time series data frames.
        c                    r    r   )tsnormr"   r%   r   r   r&      r'   z(TSNormalize.__call__.<locals>.<listcomp>r   r(   r   r%   r   r)   w   r*   zTSNormalize.__call__r$   c                 C   sh   | j dddur| j|| j d  || j d < | j dddur2| j|| j d  || j d < |S )av  Normalizes specified columns of a single time series data frame.

        This method applies the scaler to normalize the specified target
        and feature columns of the time series.

        Args:
            ts (pd.DataFrame): A single time series data frame to be normalized.

        Returns:
            pd.DataFrame: The normalized time series data frame.
        Ztarget_colsNZfeature_cols)r7   r+   r9   	transformr   r$   r   r   r   r:      s   zTSNormalize.tsnorm)r/   r0   r1   r2   r3   r   r   r   r   pdZ	DataFramer)   r:   r5   r   r   r   r   r   `   s
    r   c                       sN   e Zd ZdZdeeef f fddZdedefddZ	defd	d
Z
  ZS )r   zHConstructs a time series dataset from a list of time series data frames.r7   c                    r   )zInitializes the BuildTSDataset with parameters for dataset construction.

        Args:
            params_info (Dict[str, Any]): Dictionary containing parameters for
                constructing the time series dataset.
        N)r   r   r7   )r   r7   r   r   r   r      s   

zBuildTSDataset.__init__r   r   c                    r   )zApplies the dataset construction to a list of time series.

        Args:
            ts_list (List): List of time series data frames.

        Returns:
            List: List of constructed time series datasets.
        c                    r    r   )buildtsdatar"   r%   r   r   r&      r'   z+BuildTSDataset.__call__.<locals>.<listcomp>r   r(   r   r%   r   r)      r*   zBuildTSDataset.__call__c                 C   s   t |fi | j}|S )zBuilds a time series dataset from a single time series data frame.

        Args:
            ts: A single time series data frame.

        Returns:
            Any: A constructed time series dataset.
        )r   r7   )r   r$   r.   r   r   r   r>      s   	zBuildTSDataset.buildtsdata)r/   r0   r1   r2   r   r3   r   r   r   r)   r>   r5   r   r   r   r   r      s
    
r   c                       sn   e Zd ZdZ	ddeeef deeef def fddZ	de
d	e
fd
dZdeeef d	efddZ  ZS )r   z=Extracts time features from time series data for forecasting.Fr7   r   holidayc                    s$   t    |d | _|| _|| _dS )aE  Initializes the TimeFeature extractor.

        Args:
            params_info (Dict[str, Any]): Dictionary containing frequency information.
            size (Dict[str, int]): Dictionary containing the output chunk length.
            holiday (bool, optional): Whether to include holiday features. Defaults to False.
        freqN)r   r   r@   r   r?   )r   r7   r   r?   r   r   r   r      s   



zTimeFeature.__init__r   r   c                    r   )zApplies time feature extraction to a list of time series.

        Args:
            ts_list (List): List of time series data frames.

        Returns:
            List: List of time series with extracted time features.
        c                    r    r   )timefeatr"   r%   r   r   r&      r'   z(TimeFeature.__call__.<locals>.<listcomp>r   r(   r   r%   r   r)      r*   zTimeFeature.__call__r$   c                 C   sB   | j st|| jg d| jd }|S t|| jg d| jd }|S )zExtracts time features from a single time series data frame.

        Args:
            ts: A single time series data frame.

        Returns:
            Any: The time series with added time features.
        )	hourofday
dayofmonth	dayofweek	dayofyearZout_chunk_len)ZminuteofhourrB   rC   rD   rE   ZmonthofyearZ
weekofyearZholidays)r?   r   r@   r   r<   r   r   r   rA      s   	
zTimeFeature.timefeat)F)r/   r0   r1   r2   r   r3   r   r4   boolr   r   r)   rA   r5   r   r   r   r   r      s    

"r   c                       sv   e Zd ZdZdeeef f fddZdeeeef  deee	j
  fddZd	eeef dee	j
 fd
dZ  ZS )r   z6Converts time series data into arrays for model input.
input_datac                    r   )zInitializes the TStoArray converter.

        Args:
            input_data (Dict[str, Any]): Dictionary specifying the input data format.
        N)r   r   rG   )r   rG   r   r   r   r     s   

zTStoArray.__init__r   r   c                    r   )a   Converts a list of time series data frames into arrays.

        Args:
            ts_list (List[Dict[str, Any]]): List of time series data frames.

        Returns:
            List[List[np.ndarray]]: List of lists of arrays for each time series.
        c                    r    r   )	tstoarrayr"   r%   r   r   r&     r'   z&TStoArray.__call__.<locals>.<listcomp>r   r(   r   r%   r   r)     r*   zTStoArray.__call__r$   c                 C   sB   g }t | j }|  |D ]}|t|| d q|S )zConverts a single time series data frame into arrays.

        Args:
            ts (Dict[str, Any]): A single time series data frame.

        Returns:
            List[np.ndarray]: List of arrays representing the time series data.
        Zfloat32)listrG   keyssortappendnparrayZastype)r   r$   r   Z
input_namekeyr   r   r   rH     s   	zTStoArray.tstoarray)r/   r0   r1   r2   r   r3   r   r   r   rM   ndarrayr)   rH   r5   r   r   r   r   r      s
    (	(r   c                   @   s.   e Zd ZdZdeej deej fddZdS )r   zConvert a list of time series into batches for processing.

    This class provides a method to convert a list of time series data into
    batches. Each time series in the list is assumed to be a sequence of
    equal-length arrays or DataFrames.
    r   r   c                    s"   t  d } fddt|D S )a#  Convert a list of time series into batches.

        This method stacks time series data along a new axis to create batches.
        It assumes that each time series in the list has the same length.

        Args:
            ts_list (List[np.ndarray]): A list of time series, where each
                time series is represented as a list or array of equal length.

        Returns:
            List[np.ndarray]: A list of batches, where each batch is a stacked
            array of time series data at the same index across all series.
        r   c                    s(   g | ] t j fd dD ddqS )c                    s   g | ]}|  qS r   r   r"   ir   r   r&   B  s    z1TStoBatch.__call__.<locals>.<listcomp>.<listcomp>r   )Zaxis)rM   stack)r#   r   rQ   r   r&   B  s   ( z&TStoBatch.__call__.<locals>.<listcomp>)r,   range)r   r   nr   rT   r   r)   3  s   zTStoBatch.__call__N)r/   r0   r1   r2   r   rM   rP   r)   r   r   r   r   r   *  s    "r   )typingr   r   r   numpyrM   Zpandasr=   Z
utils.depsr   r   Zutils.benchmarkr	   funcsr   r   r   __all__Ztimeitr   r   r   r   r   r   r   r   r   r   <module>   s,   
:7%@)