Coverage for src/foapy/characteristics/_average_remoteness.py: 100%

6 statements  

« prev     ^ index     » next       coverage.py v7.8.0, created at 2025-05-17 20:45 +0000

1def average_remoteness(intervals, dtype=None): 

2 """ 

3 Calculates average remoteness of intervals. 

4 

5 $$ g = \\frac{1}{n} * \\sum_{i=1}^{n} \\log_2 \\Delta_{i} $$ 

6 

7 

8 where \\( \\Delta_{i} \\) represents each interval and \\( n \\) 

9 is the total number of intervals. 

10 

11 Parameters 

12 ---------- 

13 intervals : array_like 

14 An array of intervals 

15 dtype : dtype, optional 

16 The dtype of the output 

17 

18 Returns 

19 ------- 

20 : float 

21 The average remoteness of the input array of intervals. 

22 

23 Examples 

24 -------- 

25 

26 Calculate the average remoteness of intervals of a sequence. 

27 

28 ``` py linenums="1" 

29 import foapy 

30 

31 source = ['a', 'b', 'a', 'c', 'a', 'd'] 

32 intervals = foapy.intervals(source, foapy.binding.start, foapy.mode.normal) 

33 result = foapy.characteristics.average_remoteness(intervals) 

34 print(result) 

35 # 1.2641604167868594 

36 

37 # Improve precision by specifying a dtype. 

38 result = foapy.characteristics.average_remoteness(intervals, dtype=np.longdouble) 

39 print(result) 

40 # 1.2641604167868593636 

41 ``` 

42 """ # noqa: W605 

43 

44 from foapy.characteristics import depth 

45 

46 n = len(intervals) 

47 

48 # Check for an empty list or a list with zeros 

49 if n == 0 or all(x == 0 for x in intervals): 

50 return 0 

51 

52 return depth(intervals, dtype=dtype) / n