Coverage for SynRBL/SynMCSImputer/MissingGraph/uncertainty_graph.py: 100%

29 statements  

« prev     ^ index     » next       coverage.py v7.4.3, created at 2024-04-11 11:17 +0200

1from typing import List, Dict 

2 

3 

4class GraphMissingUncertainty: 

5 """ 

6 A class to handle uncertainty in graph data. It identifies uncertain 

7 elements in the graphs based on boundary conditions and fragment count. 

8 

9 Attributes: 

10 missing_graph_list (List[Dict]): A list of graph data in dictionary 

11 format. 

12 threshold (int): A threshold value to determine uncertainty based on 

13 the number of fragments in a SMILES string. 

14 """ 

15 

16 def __init__(self, missing_graph_list: List[Dict], threshold: int = 2) -> None: 

17 """ 

18 Initializes the GraphMissingUncertainty class with missing graph data 

19 and a threshold. 

20 

21 Args: 

22 missing_graph_list (List[Dict]): The list of missing graph data. 

23 threshold (int, optional): The threshold for determining 

24 uncertainty based on fragments. Defaults to 2. 

25 """ 

26 self.missing_graph_list = missing_graph_list 

27 self.threshold = threshold 

28 

29 @staticmethod 

30 def check_boundary(data_list: List[Dict]) -> List[int]: 

31 """ 

32 Identifies the indices of entries in the data list where 

33 'boundary_atoms_products' is empty. 

34 

35 Args: 

36 data_list (List[Dict]): A list of dictionaries, each containing a 

37 key 'boundary_atoms_products'. 

38 

39 Returns: 

40 List[int]: A list of indices where 'boundary_atoms_products' is 

41 empty. 

42 """ 

43 without_boundary_key = [] 

44 

45 for key, item in enumerate(data_list): 

46 if all( 

47 element is None for element in item["boundary_atoms_products"] 

48 ): # Checks if 'boundary_atoms_products' is empty 

49 without_boundary_key.append(key) 

50 

51 return without_boundary_key 

52 

53 @staticmethod 

54 def check_fragments(data_list: List[Dict], threshold: int = 2) -> List[int]: 

55 """ 

56 Identifies the indices of entries in the data list where the number of 

57 SMILES string fragments meets or exceeds a specified threshold. 

58 

59 Args: 

60 data_list (List[Dict]): A list of dictionaries, each containing a 

61 'smiles' key. 

62 threshold (int): The minimum number of fragments in a SMILES string 

63 to be considered uncertain. 

64 

65 Returns: 

66 List[int]: A list of indices where the number of SMILES string 

67 fragments meets or exceeds the threshold. 

68 """ 

69 graph_uncertain_key = [] 

70 

71 for key, entry in enumerate(data_list): 

72 for i in entry["smiles"]: 

73 if i is not None and len(i.split(".")) >= threshold: 

74 graph_uncertain_key.append(key) 

75 

76 return graph_uncertain_key 

77 

78 def fit(self) -> List[Dict]: 

79 """ 

80 Processes the missing graph data to update their 'Certainty' status 

81 based on boundary and fragment checks. 

82 

83 Returns: 

84 List[Dict]: The updated list of missing graph data with 'Certainty' 

85 status marked. 

86 """ 

87 uncertain_key = [] 

88 without_boundary_key = self.check_boundary(self.missing_graph_list) 

89 graph_uncertain_key = self.check_fragments( 

90 self.missing_graph_list, threshold=self.threshold 

91 ) 

92 

93 uncertain_key.extend(without_boundary_key) 

94 uncertain_key.extend(graph_uncertain_key) 

95 

96 for key, missing_graph in enumerate(self.missing_graph_list): 

97 missing_graph["Certainty"] = key not in uncertain_key 

98 

99 return self.missing_graph_list