Coverage for tests/test_processing.py: 100%

25 statements  

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

1import numpy as np 

2import pytest 

3from evutils.processing import mask_events, normalize_ts 

4from evutils.types import Event_dtype 

5 

6def test_mask_events(): 

7 events = np.array( 

8 [(100, 0, 0, 1), (200, 1, 1, 1), (300, 2, 2, 0)], 

9 dtype=Event_dtype 

10 ) 

11 mask = np.array([ 

12 [1, 0, 0], 

13 [0, 0, 0], 

14 [0, 0, 1] 

15 ]) 

16 

17 # Normal case 

18 masked = mask_events(events, mask) 

19 assert len(masked) == 2 

20 assert masked['x'].tolist() == [0, 2] 

21 

22 # Empty events 

23 empty_events = np.array([], dtype=Event_dtype) 

24 assert len(mask_events(empty_events, mask)) == 0 

25 

26 # ValueError: 1D mask 

27 with pytest.raises(ValueError, match="Mask must be a 2D array"): 

28 mask_events(events, np.array([1, 0, 1])) 

29 

30 # ValueError: Out of bounds 

31 out_of_bounds = np.array([(100, 3, 3, 1)], dtype=Event_dtype) 

32 with pytest.raises(ValueError, match="Events x and y coordinates must be within the mask dimensions"): 

33 mask_events(out_of_bounds, mask) 

34 

35 # ValueError: negative coordinates (using generic dtype since Event_dtype has unsigned x/y) 

36 # Event_dtype has x, y as u2 (unsigned 16-bit), so negative values wrap around to large positive. 

37 # Therefore, negative values are effectively out of bounds. 

38 

39def test_normalize_ts(): 

40 events = np.array( 

41 [(100, 0, 0, 1), (200, 1, 1, 1), (300, 2, 2, 0)], 

42 dtype=Event_dtype 

43 ) 

44 

45 # Normal case 

46 norm_events = normalize_ts(events.copy()) 

47 assert norm_events['t'].tolist() == [0, 100, 200] 

48 

49 # Start ts != 0 

50 norm_events_start = normalize_ts(events.copy(), start_ts=50) 

51 assert norm_events_start['t'].tolist() == [50, 150, 250] 

52 

53 # Empty array 

54 empty_events = np.array([], dtype=Event_dtype) 

55 assert len(normalize_ts(empty_events)) == 0