Coverage for src/foapy/characteristics/ma/_depth.py: 100%
3 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 depth(intervals, dtype=None):
5 """
6 Calculates depth of the intervals grouped by congeneric sequence.
8 $$
9 \\left[ G_j \\right]_{1 \\le j \\le m} =
10 \\left[ \\sum_{i=1}^{n_j} \\log_2 \\Delta_{ij} \\right]_{1 \\le j \\le m}
11 $$
13 where \\( \\Delta_{ij} \\) represents $i$-th interval of $j$-th
14 congeneric intervals array, \\( n_j \\) is the total
15 number of intervals in $j$-th congeneric intervals array
16 and $m$ is number of congeneric intervals arrays.
18 Parameters
19 ----------
20 intervals : array_like
21 An array of congeneric intervals array
22 dtype : dtype, optional
23 The dtype of the output
25 Returns
26 -------
27 : array
28 An array of the depths of congeneric intervals.
30 Examples
31 --------
33 Calculate the depth of a sequence.
35 ``` py linenums="1"
36 import foapy
37 import numpy as np
39 source = np.array(['a', 'b', 'a', 'c', 'a', 'd'])
40 order = foapy.ma.order(source)
41 intervals = foapy.ma.intervals(order, foapy.binding.start, foapy.mode.normal)
42 result = foapy.characteristics.ma.depth(intervals)
43 print(result)
44 # [2. 1. 2. 2.5849625]
45 ```
47 Calculate the depth of congeneric intervals of a sequence.
49 ``` py linenums="1"
50 import foapy
52 X = []
53 X.append([1, 1, 4, 4])
54 X.append([3, 1, 3])
55 X.append([5, 3, 1])
57 result = foapy.characteristics.ma.depth(X)
58 print(result)
59 # [4. 3.169925 3.9068906]
60 ```
61 """ # noqa: W605
62 return np.asanyarray(
63 [np.sum(np.log2(line, dtype=dtype), dtype=dtype) for line in intervals]
64 )