Coverage for src/evutils/processing/_utils.py: 100%

7 statements  

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

1"""Utility functions for processing event arrays.""" 

2 

3import numpy as np 

4 

5 

6def normalize_ts(events: np.ndarray, start_ts: int = 0) -> np.ndarray: 

7 """Normalizes the timestamps of events to start from zero. 

8 

9 Parameters 

10 ---------- 

11 events : np.ndarray 

12 Array of events with a 't' field representing timestamps. 

13 start_ts : int, optional 

14 The timestamp to normalize from, by default 0. 

15 

16 Returns 

17 ------- 

18 np.ndarray 

19 Array of events with normalized timestamps. 

20 

21 Examples 

22 -------- 

23 >>> import numpy as np 

24 >>> from evutils.processing import normalize_ts 

25 >>> events = np.array( 

26 ... [(0, 0, 100, 1), (1, 1, 200, 1), (2, 2, 300, 0)], 

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

28 ... ) 

29 >>> norm_events = normalize_ts(events.copy()) 

30 >>> norm_events['t'] 

31 array([ 0, 100, 200]) 

32 

33 """ 

34 if len(events) == 0: 

35 return events 

36 

37 min_ts = events['t'].min() 

38 events['t'] -= min_ts - start_ts 

39 

40 return events