Coverage for pyhiperta/leakage.py: 100%
7 statements
« prev ^ index » next coverage.py v7.6.4, created at 2024-11-07 20:49 +0000
« prev ^ index » next coverage.py v7.6.4, created at 2024-11-07 20:49 +0000
1import numpy as np
4def leakage(waveforms: np.ndarray, intensity: np.ndarray, leakage_mask: np.ndarray) -> np.ndarray:
5 """Compute the leakage 1 and 2, and their intensity.
7 The leakage 1, respectively 2, is the number of pixels that are not 0 after cleaning on the outermost,
8 respectively 2nd outermost, ring of the camera.
9 The leakage intensity is the charge of the leakage pixels, so leakage 1, resp. 2, intensity is the sum of the signal
10 in all pixels of the outermost, resp. 2nd outermost, ring of the camera.
12 Parameters
13 ----------
14 waveforms : np.ndarray
15 1D shower image(s). If a single image is provided the shape must be (N_pixels,). If a
16 batch of images is provided, the shape should be (N_batch, N_pixels).
17 Shape: ([N_batch,] N_pixels)
18 intensity : np.ndarray
19 Total charge of each waveform.
20 Shape: ([B_batch,] 1)
21 leakage_mask : np.ndarray
22 Boolean array with value True for pixels on the outermost rings of the camera, False otherwise.
23 leakage_mask[0, :] selects the outermost ring (leakage 1) while leakage_mask[1, :] selects the
24 second outermost ring (leakage 2).
25 Shape: (2, N_pixels)
27 Returns
28 -------
29 np.ndarray
30 Leakage array. This is a 1D array with shape (4,) if a single image was
31 provided, or a 2D array with shape (N_batch, 4) if a batch of images was provided. The
32 parameters are ordered like so:
33 leakage[..., 0]: leakage 1: number of pixels with signal on the outermost ring of the camera, normalized by
34 the number of pixels with signal.
35 leakage[..., 1]: leakage 2: number of pixels with signal on the 1st and 2nd outermost ring of
36 the camera, normalized by the number of pixels with signal.
37 leakage[..., 3]: leakage 1 intensity: Waveform charge contained in the outermost ring of the camera,
38 normalized by the total charge in the camera
39 leakage[..., 4]: leakage 2 intensity: Waveform charge contained in the 1st and 2nd outermost ring of the camera,
40 normalized by the total charge in the camera
41 """
42 # number of pixels for normalization
43 nb_pixels = np.count_nonzero(waveforms, axis=-1, keepdims=True).astype(np.float32)
44 # apply leakage mask (both leakage 1 and leakage 2 with broadcasting)
45 leakage_charge = waveforms[..., np.newaxis, :] * leakage_mask
46 # normalized number of pixels with signal in leakage for both leakages
47 leakage = np.count_nonzero(leakage_charge, axis=-1).astype(np.float32) / nb_pixels
48 # normalized leakage intensity for both leakage (add axis to intensity for broadcasting!)
49 leakage_intensity = leakage_charge.sum(axis=-1, dtype=np.float32) / intensity[..., np.newaxis]
50 return np.concatenate([leakage, leakage_intensity], axis=-1)
51 # return np.stack([leakage[..., 0], leakage[..., 1], leakage_intensity[..., 0], leakage_intensity[..., 1]], axis=-1)