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

7 statements  

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

1import numpy as np 

2 

3 

4def average_remoteness(intervals, dtype=None): 

5 """ 

6 Calculates average remoteness of the intervals grouped by congeneric sequence. 

7 

8 $$ 

9 \\left[ g_j \\right]_{1 \\le j \\le m} = 

10 \\left[ 

11 \\frac{1}{n_j} * \\sum_{i=1}^{n_j} \\log_2 \\Delta_{ij} 

12 \\right]_{1 \\le j \\le m} 

13 $$ 

14 

15 where \\( \\Delta_{ij} \\) represents $i$-th interval of $j$-th 

16 congeneric intervals array, \\( n_j \\) is the total 

17 number of intervals in $j$-th congeneric intervals array 

18 and $m$ is number of congeneric intervals arrays. 

19 

20 Parameters 

21 ---------- 

22 intervals : array_like 

23 An array of congeneric intervals array 

24 dtype : dtype, optional 

25 The dtype of the output 

26 

27 Returns 

28 ------- 

29 : array 

30 An array of the average remoteness of congeneric intervals. 

31 

32 Examples 

33 -------- 

34 

35 Calculate the average remoteness of a sequence. 

36 

37 ``` py linenums="1" 

38 import foapy 

39 import numpy as np 

40 

41 source = np.array(['a', 'b', 'a', 'c', 'a', 'd']) 

42 order = foapy.ma.order(source) 

43 intervals = foapy.ma.intervals(order, foapy.binding.start, foapy.mode.normal) 

44 result = foapy.characteristics.ma.average_remoteness(intervals) 

45 print(result) 

46 # [0.66666667 1. 2. 2.5849625 ] 

47 ``` 

48 

49 Calculate the average remoteness of congeneric intervals of a sequence. 

50 

51 ``` py linenums="1" 

52 import foapy 

53 

54 X = [] 

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

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

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

58 

59 result = foapy.characteristics.ma.average_remoteness(X) 

60 print(result) 

61 # [1. 1.05664167 1.30229687] 

62 ``` 

63 """ # noqa: W605 

64 

65 from foapy.characteristics.ma import depth 

66 

67 size = np.array([len(elem) for elem in intervals]) 

68 depth_seq = depth(intervals, dtype=dtype) 

69 res = np.divide( 

70 depth_seq, 

71 size, 

72 out=np.zeros_like(depth_seq), 

73 where=size != 0, 

74 dtype=dtype, 

75 ) 

76 return res