Coverage for emd/tests/test_utils.py: 100%

30 statements  

« prev     ^ index     » next       coverage.py v7.6.11, created at 2025-03-08 15:44 +0000

1"""Tests for general utility functions.""" 

2 

3import unittest 

4 

5import numpy as np 

6 

7 

8class TestAmplitudeNormalise(unittest.TestCase): 

9 """Ensure amplitude normalise is working.""" 

10 

11 def setUp(self): 

12 """Initialise cycles for testing.""" 

13 # Create core signal 

14 seconds = 5.1 

15 sample_rate = 2000 

16 f1 = 2 

17 f2 = 18 

18 time_vect = np.linspace(0, seconds, int(seconds * sample_rate)) 

19 am = np.sin(2*np.pi*f1*time_vect) + 1 

20 

21 self.x = am * np.cos(2.3 * np.pi * f2 * time_vect) 

22 

23 def test_amplitude_normalise(self): 

24 """Ensure amplitude normalise is working.""" 

25 from ..imftools import amplitude_normalise 

26 

27 amp_norm = amplitude_normalise(self.x[:, np.newaxis]) 

28 

29 # start signal should range between +/- 2 

30 assert(2 - np.max(self.x) < 1e-2) 

31 assert(2 + np.min(self.x) < 1e-2) 

32 

33 # norm signal should range between +/- 

34 assert(1 - np.max(amp_norm) < 1e-2) 

35 assert(1 + np.min(amp_norm) < 1e-2) 

36 

37 

38class TestEpochs(unittest.TestCase): 

39 """Ensure epoching is working.""" 

40 

41 def setUp(self): 

42 """Initialise data for testing.""" 

43 # Create core signal 

44 seconds = 5 

45 sample_rate = 2000 

46 f2 = 5 

47 time_vect = np.linspace(0, seconds, int(seconds * sample_rate)) 

48 

49 self.x = np.cos(2 * np.pi * f2 * time_vect + np.pi/2) 

50 

51 def test_find_extrema_locked_epochs(self): 

52 """Ensure extrema-epoching is working.""" 

53 from ..imftools import find_extrema_locked_epochs 

54 

55 trls = find_extrema_locked_epochs(self.x, 40) 

56 

57 assert(trls.shape[0] == 25) 

58 assert(np.all(np.unique(np.diff(trls)) == 40))