Coverage for tdaad/utils/window_functions.py: 100%

10 statements  

« prev     ^ index     » next       coverage.py v7.9.1, created at 2025-06-13 13:45 +0000

1"""Window Functions.""" 

2 

3# Author: Martin Royer 

4 

5import numpy as np 

6import pandas as pd 

7 

8from tqdm import tqdm 

9 

10 

11def sliding_window_ppl(data, pipeline, step=5, window_size=120): 

12 """Applies a pipeline to timeseries data chunks using the Sliding Window algorithm. 

13 

14 @param data: pd.DataFrame with index to apply named_pipeline to. 

15 @param window_size: size of the sliding window algorithm to extract subsequences as input to named_pipeline. 

16 @param step: size of the sliding window steps between each window. 

17 @param pipeline: pipeline (sequence of operators that have a `name` attribute) to apply to each window. 

18 @return: pd.DataFrame that maps data to the result of applying named_pipeline to window view of data. 

19 """ 

20 

21 swv = np.lib.stride_tricks.sliding_window_view(data.index, window_size)[::step, :] 

22 result = {} 

23 for window in tqdm(swv): 

24 result[str(tuple(window))] = pipeline.transform(data.loc[window].copy()) 

25 post_result = pd.DataFrame.from_dict(result, orient="index") 

26 return post_result