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

1import numpy as np 

2 

3 

4def depth(intervals, dtype=None): 

5 """ 

6 Calculates depth of the intervals grouped by congeneric sequence. 

7 

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 $$ 

12 

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. 

17 

18 Parameters 

19 ---------- 

20 intervals : array_like 

21 An array of congeneric intervals array 

22 dtype : dtype, optional 

23 The dtype of the output 

24 

25 Returns 

26 ------- 

27 : array 

28 An array of the depths of congeneric intervals. 

29 

30 Examples 

31 -------- 

32 

33 Calculate the depth of a sequence. 

34 

35 ``` py linenums="1" 

36 import foapy 

37 import numpy as np 

38 

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 ``` 

46 

47 Calculate the depth of congeneric intervals of a sequence. 

48 

49 ``` py linenums="1" 

50 import foapy 

51 

52 X = [] 

53 X.append([1, 1, 4, 4]) 

54 X.append([3, 1, 3]) 

55 X.append([5, 3, 1]) 

56 

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 )