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
« prev ^ index » next coverage.py v7.4.3, created at 2024-04-11 11:17 +0200
1from typing import List, Dict
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.
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 """
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.
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
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.
35 Args:
36 data_list (List[Dict]): A list of dictionaries, each containing a
37 key 'boundary_atoms_products'.
39 Returns:
40 List[int]: A list of indices where 'boundary_atoms_products' is
41 empty.
42 """
43 without_boundary_key = []
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)
51 return without_boundary_key
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.
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.
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 = []
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)
76 return graph_uncertain_key
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.
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 )
93 uncertain_key.extend(without_boundary_key)
94 uncertain_key.extend(graph_uncertain_key)
96 for key, missing_graph in enumerate(self.missing_graph_list):
97 missing_graph["Certainty"] = key not in uncertain_key
99 return self.missing_graph_list