Coverage for src/pyhiperta/timing.py: 100%

7 statements  

« prev     ^ index     » next       coverage.py v7.15.3, created at 2026-08-07 14:41 +0000

1# Copyright 2026 CNRS 

2# This software is distributed under the terms of the CeCILL-C free software license. 

3 

4"""Time related parameters (slop, intercept) algorithms.""" 

5 

6 

7import numpy as np 

8 

9 

10def slope_intercept( 

11 peak_times: np.ndarray, longitudinals: np.ndarray, cleaning_mask 

12) -> tuple[np.ndarray, np.ndarray]: 

13 """Computes slope, intercept by fitting a line (LSE) on the `peak_times` of each pixels along the ellipsis major axis 

14 

15 This is essentially a less robust __batched__ numpy.linalg.lstsq 

16 

17 Parameters 

18 ---------- 

19 peak_times : np.ndarray 

20 The time of maximum signal for each pixels as computed by the integration. 

21 shape: ([N_batch,], N_pixels) 

22 longitudinals : np.ndarray 

23 The longitudinal coordinates (coordinate along the ellipsis major axis) of each pixels. 

24 shape: ([N_batch,], N_pixels) 

25 

26 Returns 

27 ------- 

28 Tuple[np.ndarray, np.ndarray] 

29 Slope and intercept values for each waveform. 

30 shape of each array: ([Batch,],) 

31 """ 

32 # Compute mean and sums only where the cleaning mask is True. 

33 longitudinals_mean = longitudinals.mean(axis=-1, keepdims=True, where=cleaning_mask) 

34 peak_times_mean = peak_times.mean(axis=-1, keepdims=True, where=cleaning_mask) 

35 

36 slope = (cleaning_mask * (longitudinals - longitudinals_mean) * (peak_times - peak_times_mean)).sum(axis=-1) / ( 

37 cleaning_mask * (longitudinals - longitudinals_mean) ** 2 

38 ).sum(axis=-1) 

39 intercept = peak_times_mean.squeeze() - slope * longitudinals_mean.squeeze() 

40 return slope, intercept