Coverage for src/foapy/characteristics/_uniformity.py: 100%
7 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
1import numpy as np
4def uniformity(intervals, dtype=None):
5 """
6 Calculates uniformity of intervals grouped by element of the alphabet.
8 $$ u = \\frac {1} {n} * \\sum_{j=1}^{m}{\\log_2 \\frac{ (\\sum_{i=1}^{n_j} \\frac{\\Delta_{ij}}{n_j})^{n_j} } { \\prod_{i=1}^{n_j} \\Delta_{ij}}}$$
10 where \\( m \\) is count of groups (alphabet power), \\( n_j \\) is count of intervals in group \\( j \\),
11 \\( \\Delta_{ij} \\) represents an interval at index \\( i \\) in group \\( j \\) and \\( n \\) is total count of intervals across all groups.
13 $$n=\\sum_{j=1}^{m}{n_j} $$
15 Parameters
16 ----------
17 intervals_grouped : array_like
18 An array of intervals grouped by element
19 dtype : dtype, optional
20 The dtype of the output
22 Returns
23 -------
24 : float
25 The uniformity of the input array of intervals_grouped.
27 Examples
28 --------
30 Calculate the uniformity of intervals_grouped of a sequence.
32 ``` py linenums="1"
33 import foapy
34 import numpy as np
36 source = np.array(['a', 'b', 'a', 'c', 'a', 'd'])
37 order = foapy.ma.order(source)
38 print(order)
40 #[[0 -- 0 -- 0 --]
41 # [-- 1 -- -- -- --]
42 # [-- -- -- 2 -- --]
43 # [-- -- -- -- -- 3]]
45 intervals_grouped = foapy.ma.intervals(order, foapy.binding.start, foapy.mode.normal)
47 print(intervals_grouped)
48 # [
49 # array([1, 2, 2]),
50 # array([2]),
51 # array([4]),
52 # array([6])
53 # ]
55 # m = 4
56 # n_0 = 3
57 # n_1 = 1
58 # n_2 = 1
59 # n_3 = 1
60 # n = 6
62 result = foapy.characteristics.uniformity(intervals_grouped)
63 print(result)
64 # 0.03514946374976957
66 # Improve precision by specifying a dtype.
67 result = foapy.characteristics.uniformity(intervals_grouped, dtype=np.longdouble)
68 print(result)
69 # 0.03514946374976969819
70 ```
71 """ # noqa: E501
72 from foapy.characteristics import average_remoteness, identifying_information
74 total_elements = np.concatenate(intervals)
76 H = identifying_information(intervals, dtype=dtype)
77 g = average_remoteness(total_elements, dtype=dtype)
79 return H - g