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

13 statements  

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

1"""Remapping Functions.""" 

2 

3# Author: Martin Royer 

4 

5import numpy as np 

6 

7 

8def score_flat_fast_remapping(scores, window_size, stride, padding_length=0): 

9 """Univariate score remapping without window indices and with precomputed padding_length.""" 

10 if hasattr(scores, "index"): 

11 scores = scores.values 

12 begins = np.array([i * stride for i in range(scores.shape[0])]) 

13 ends = begins + window_size 

14 

15 remapped_scores = np.full(shape=stride * (scores.shape[0] - 1) + window_size + padding_length, fill_value=np.nan) 

16 

17 # iterate over window intersection points, interpolate (vote) in-between intersection and next_intersection 

18 intersections = np.unique(np.r_[begins, ends]) 

19 for intersection, next_intersection in zip(intersections[:-1], intersections[1:]): 

20 window_indices = np.flatnonzero((begins <= intersection) & (next_intersection < ends + 1)) 

21 remapped_scores[intersection:next_intersection] = np.nansum(scores[window_indices]) # or nanmedian 

22 

23 # replace unknown values with 0 = normal (for the padding at the end) 

24 np.nan_to_num(remapped_scores, copy=False) 

25 return remapped_scores