Coverage for src/evutils/repr/_histogram.py: 100%

39 statements  

« prev     ^ index     » next       coverage.py v7.15.1, created at 2026-07-15 09:33 +0000

1"""Module for generating histogram-based representations from events.""" 

2 

3import numba 

4import numpy as np 

5from typing import Any 

6 

7 

8@numba.njit 

9def histogram(events: np.ndarray, width: int = 1280, height: int = 720, fill: bool = False, dtype: Any = np.uint8) -> np.ndarray: 

10 """Generate a histogram frame from the events. 

11 

12 Parameters 

13 ---------- 

14 events : np.ndarray 

15 Array of events in the :class:`~evutils.types.Events` format 

16 width : int, optional 

17 Width of the frame, by default 1280 

18 height : int, optional 

19 Height of the frame, by default 720 

20 fill : bool, optional 

21 If True, the non-zero values are set to 255, by default False 

22 dtype : np.dtype, optional 

23 Data type of the output array, by default np.uint8 

24  

25 Returns 

26 ------- 

27 np.ndarray 

28 A numpy array with the histogram frame (height, width, 3) 

29 

30 Examples 

31 -------- 

32 >>> import numpy as np 

33 >>> from evutils.repr import histogram 

34 >>> events = np.array([(10, 20, 100, 1), (15, 25, 200, 0)], 

35 ... dtype=[('x', '<u2'), ('y', '<u2'), ('t', '<i8'), ('p', 'i1')]) 

36 >>> frame = histogram(events, width=100, height=100) 

37 >>> frame.shape 

38 (100, 100, 3) 

39 """ 

40 buffer = np.zeros((height, width, 3), dtype=dtype) 

41 

42 clip = 255 

43 

44 # clip = 255 if dtype == np.uint8 else 65535 

45 

46 for e in events: 

47 x = e['x'] 

48 y = e['y'] 

49 p = e['p'] 

50 

51 if p == 1: 

52 p = 0 # Red 

53 else: 

54 p = 2 # Blue 

55 

56 

57 

58 if buffer[y, x, p] < clip: 

59 buffer[y, x, p] += 1 

60 

61 # If the fill flag is set, we fill the non-zero values with 255 

62 if fill: 

63 for x in range(0, width): 

64 for y in range(0, height): 

65 for c in range(0, 3): 

66 if buffer[y, x, c] > 0: 

67 buffer[y, x, c] = 255 

68 

69 return buffer 

70 

71 

72@numba.njit 

73def wedge_histogram(events: np.ndarray, width: int = 1280, height: int = 720, tl: float = 30e6, dtype: Any = np.uint8) -> np.ndarray: 

74 """Generate a wedge histogram frame from the events. 

75 

76 Parameters 

77 ---------- 

78 events : np.ndarray 

79 Array of events in the :class:`~evutils.types.Events` format 

80 width : int, optional 

81 Width of the frame, by default 1280 

82 height : int, optional 

83 Height of the frame, by default 720 

84 tl : float, optional 

85 Time limit for the frame in us, by default 30e6 

86 dtype : np.dtype, optional 

87 Data type of the output array, by default np.uint8 

88  

89 Returns 

90 ------- 

91 np.ndarray 

92 A numpy array with the wedge frame (height, width, 3) 

93 

94 Examples 

95 -------- 

96 >>> import numpy as np 

97 >>> from evutils.repr import wedge_histogram 

98 >>> events = np.array([(10, 20, 100, 1), (15, 25, 200, 0)], 

99 ... dtype=[('x', '<u2'), ('y', '<u2'), ('t', '<i8'), ('p', 'i1')]) 

100 >>> frame = wedge_histogram(events, width=100, height=100) 

101 >>> frame.shape 

102 (100, 100, 3) 

103 """ 

104 buffer = np.zeros((height, width, 3), dtype=dtype) 

105 

106 ts_norm = (events['t'] - events['t'][0])/tl 

107 

108 # print(len(ev)) 

109 

110 sel = (height-events['y'])/height > ts_norm - 0.1 

111 ev_sel = events[sel] 

112 # print(len(ev_sel)) 

113 # print((720-ev['y'])/720, ts_norm, sel) 

114 

115 

116 for e in ev_sel: 

117 x = e['x'] 

118 y = e['y'] 

119 p = e['p'] 

120 

121 

122 if p == 1: 

123 p = 0 

124 else: 

125 p = 2 

126 

127 if buffer[y, x, p] < 128: 

128 buffer[y, x, p] += 255 

129 

130 return buffer