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
« 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.
5 $$ g = \\frac{1}{n} * \\sum_{i=1}^{n} \\log_2 \\Delta_{i} $$
8 where \\( \\Delta_{i} \\) represents each interval and \\( n \\)
9 is the total number of intervals.
11 Parameters
12 ----------
13 intervals : array_like
14 An array of intervals
15 dtype : dtype, optional
16 The dtype of the output
18 Returns
19 -------
20 : float
21 The average remoteness of the input array of intervals.
23 Examples
24 --------
26 Calculate the average remoteness of intervals of a sequence.
28 ``` py linenums="1"
29 import foapy
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
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
44 from foapy.characteristics import depth
46 n = len(intervals)
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
52 return depth(intervals, dtype=dtype) / n