Coverage for pyhiperta/timing.py: 100%

8 statements  

« prev     ^ index     » next       coverage.py v7.6.7, created at 2024-11-21 20:50 +0000

1from typing import Tuple 

2 

3import numpy as np 

4 

5 

6def slope_intercept(peak_times: np.ndarray, longitudinals: np.ndarray, cleaning_mask) -> Tuple[np.ndarray, np.ndarray]: 

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

8 

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

10 

11 Parameters 

12 ---------- 

13 peak_times : np.ndarray 

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

15 shape: ([N_batch,], N_pixels) 

16 longitudinals : np.ndarray 

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

18 shape: ([N_batch,], N_pixels) 

19 

20 Returns 

21 ------- 

22 Tuple[np.ndarray, np.ndarray] 

23 Slope and intercept values for each waveform. 

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

25 """ 

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

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

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

29 

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

31 cleaning_mask * (longitudinals - longitudinals_mean) ** 2 

32 ).sum(axis=-1) 

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

34 return slope, intercept