Coverage for SynRBL/rsmi_utils.py: 19%

123 statements  

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

1import json 

2import random 

3 

4from typing import List, Dict, Set, Any 

5from typing import Optional, Union, Callable, Tuple 

6from rdkit import Chem 

7from collections import defaultdict 

8 

9 

10def save_database(database: list[dict], pathname: str = "./Data/database.json") -> None: 

11 """ 

12 Save a database (a list of dictionaries) to a JSON file. 

13 

14 Args: 

15 database: The database to be saved. 

16 pathname: The path where the database will be saved. Defaults to './Data/database.json'. 

17 

18 Raises: 

19 TypeError: If the database is not a list of dictionaries. 

20 ValueError: If there is an error writing the file. 

21 """ 

22 if not all(isinstance(item, dict) for item in database): 

23 raise TypeError("Database should be a list of dictionaries.") 

24 

25 try: 

26 with open(pathname, "w") as f: 

27 json.dump(database, f) 

28 except IOError as e: 

29 raise ValueError(f"Error writing to file {pathname}: {e}") 

30 

31 

32def load_database(pathname: str = "./Data/database.json") -> List[Dict]: 

33 """ 

34 Load a database (a list of dictionaries) from a JSON file. 

35 

36 Args: 

37 pathname: The path from where the database will be loaded. Defaults to './Data/database.json'. 

38 

39 Returns: 

40 The loaded database. 

41 

42 Raises: 

43 ValueError: If there is an error reading the file. 

44 """ 

45 try: 

46 with open(pathname, "r") as f: 

47 database = json.load(f) # Load the JSON data from the file 

48 return database 

49 except IOError as e: 

50 raise ValueError(f"Error reading to file {pathname}: {e}") 

51 

52 

53def extract_atomic_elements(rules: List[Dict[str, Dict[str, int]]]) -> Set[str]: 

54 """ 

55 Extracts the set of all atomic elements from a list of rules. 

56 

57 Args: 

58 rules: A list of rules, where each rule is a dictionary representing a 

59 composition of atomic elements. 

60 

61 Returns: 

62 A set of all atomic elements found in the rules. 

63 

64 Example: 

65 ```python 

66 rules = [{"Composition": {"A": 1, "B": 1}}, {"Composition": {"C": 1, "D": 1, "E": 1}}] 

67 atomic_elements = extract_atomic_elements(rules) 

68 print(atomic_elements) # Output: {'A', 'B', 'C', 'D', 'E'} 

69 ``` 

70 """ 

71 

72 atomic_elements = set() 

73 

74 for rule in rules: 

75 atomic_elements.update(rule["Composition"].keys()) 

76 

77 return atomic_elements 

78 

79 

80def _get_max_comp_len(database: List[Dict]) -> int: 

81 """ 

82 Determines the maximum length of a composition in the database. 

83 

84 Args: 

85 database (List[Dict]): A list of dictionaries representing the database. 

86 

87 Returns: 

88 int: The maximum length of a composition in the database. 

89 

90 Example: 

91 ```python 

92 database = [ 

93 {'Composition': ['H2O', 'CO2']}, 

94 {'Composition': ['NaCl', 'H2O']}, 

95 {'Composition': ['C6H12O6', 'H2O']}, 

96 ] 

97 

98 max_comp_len = _get_max_comp_len(database) 

99 print(max_comp_len) # Output: 3 

100 ``` 

101 """ 

102 

103 max_comp_len = 0 # Initialize the maximum composition length 

104 

105 # Iterate through each entry in the database 

106 for entry in database: 

107 # Check if the current entry's composition length exceeds the maximum 

108 if max_comp_len < len(entry["Composition"]): 

109 # Update the maximum composition length if necessary 

110 max_comp_len = len(entry["Composition"]) 

111 

112 # Return the maximum composition length 

113 return max_comp_len 

114 

115 

116def build_lookups(atomic_elements: set, database: list) -> list: 

117 """ 

118 Constructs lookup dictionaries for atomic elements in the database, removing empty lists from the lookup. 

119 

120 Args: 

121 atomic_elements (set): A set of unique atomic elements. 

122 database (list): A list of database entries, where each entry is a dictionary 

123 containing a "Composition" key. 

124 

125 Returns: 

126 list: A list of lookup dictionaries, where each dictionary maps atomic elements to 

127 their corresponding database indices for molecules with different composition lengths. 

128 

129 Example: 

130 ```python 

131 atomic_elements = {'H', 'C', 'O', 'N'} 

132 database = [ 

133 {'Composition': {'H': 2, 'O': 1}}, 

134 {'Composition': {'C': 1, 'H': 4}}, 

135 {'Composition': {'N': 2, 'O': 3}}, 

136 {'Composition': {'S': 1, 'O': 2}}, 

137 ] 

138 

139 lookup = build_lookups(atomic_elements, database) 

140 print(lookup) 

141 

142 

143 This example demonstrates how to use the `build_lookups` function to construct lookup dictionaries 

144 for the provided atomic elements and database. The resulting lookup dictionaries are optimized by 

145 removing empty lists, making them more compact and efficient. 

146 """ 

147 

148 max_comp_len = _get_max_comp_len(database) 

149 

150 lookup = [] 

151 

152 # Iterate through composition lengths from 2 to max_comp_len 

153 for comp_len in range(2, max_comp_len + 1): 

154 lookup_dict = {} 

155 

156 for atom in atomic_elements: 

157 atom_list = [] 

158 

159 # Iterate through database entries 

160 for key, entry in enumerate(database): 

161 if atom in entry["Composition"].keys(): 

162 if len(entry["Composition"]) == comp_len: 

163 atom_list.append(key) 

164 

165 # Remove empty lists from the lookup dictionary 

166 if not atom_list: 

167 continue 

168 lookup_dict[atom] = atom_list 

169 

170 lookup.append(lookup_dict) 

171 

172 return lookup 

173 

174 

175def calculate_net_charge(sublist: list[dict[str, Union[str, int]]]) -> int: 

176 """ 

177 Calculate the net charge from a list of molecules represented as SMILES strings. 

178 

179 Args: 

180 sublist: A list of dictionaries, each with a 'smiles' string and a 'Ratio' integer. 

181 

182 Returns: 

183 The net charge of the sublist as an integer. 

184 """ 

185 total_charge = 0 

186 for item in sublist: 

187 if "smiles" in item and "Ratio" in item: 

188 mol = Chem.MolFromSmiles(item["smiles"]) 

189 if mol: 

190 charge = ( 

191 sum(abs(atom.GetFormalCharge()) for atom in mol.GetAtoms()) 

192 * item["Ratio"] 

193 ) 

194 total_charge += charge 

195 return total_charge 

196 

197 

198def find_shortest_sublists(solution: List[List[Dict]]) -> List[List[Dict]]: 

199 """ 

200 Find all sublists of dictionaries that have the shortest length. 

201 

202 Args: 

203 solution: A list containing lists of dictionaries. 

204 

205 Returns: 

206 A list of all sublists with the shortest length. 

207 """ 

208 if not solution: 

209 return [] 

210 

211 min_length = min(len(sublist) for sublist in solution) 

212 shortest_sublists = [sublist for sublist in solution if len(sublist) == min_length] 

213 

214 return shortest_sublists 

215 

216 

217def filter_data( 

218 data: List[Dict[str, Any]], 

219 unbalance_values: Optional[List[str]] = None, 

220 formula_key: str = "Diff_formula", 

221 element_key: Optional[str] = None, 

222 min_count: int = 0, 

223 max_count: int = float("inf"), 

224) -> List[Dict[str, Any]]: 

225 """ 

226 Filter dictionaries based on a list of unbalance values and element count in a specified formula key. 

227 

228 This function filters the input list of dictionaries based on the specified list of unbalance values and 

229 the count of a specific element within a given formula key. It returns dictionaries that match any of the 

230 unbalance criteria and where the element count falls within the specified range. 

231 

232 Args: 

233 data: A list of dictionaries to be filtered. 

234 unbalance_values: The values to filter by in the 'Unbalance' key. If None, this criterion is ignored. 

235 formula_key: The key in the dictionaries that contains the element counts. Defaults to 'Diff_formula'. 

236 element_key: The element to filter by in the formula key. If None, this criterion is ignored. 

237 min_count: The minimum allowed count of the element. Defaults to 0. 

238 max_count: The maximum allowed count of the element. Defaults to infinity. 

239 

240 Returns: 

241 A list of dictionaries filtered based on the criteria. 

242 """ 

243 filtered_data = [] 

244 

245 for item in data: 

246 # Check for unbalance condition 

247 unbalance_matches = ( 

248 unbalance_values is None or item.get("Unbalance") in unbalance_values 

249 ) 

250 

251 # Check for element count condition 

252 if element_key is None: 

253 element_matches = True 

254 else: 

255 element_count = item.get(formula_key, {}).get(element_key, 0) 

256 element_matches = min_count <= element_count <= max_count 

257 

258 if unbalance_matches and element_matches: 

259 filtered_data.append(item) 

260 

261 return filtered_data 

262 

263 

264def remove_duplicates_by_key( 

265 data: List[dict], key_function: Callable[..., Any] 

266) -> List[dict]: 

267 """ 

268 Remove duplicate entries from a list based on a unique key for each entry. 

269 

270 Parameters: 

271 - `data` (List[dict]): A list of data entries (dictionaries, objects, etc.). 

272 - `key_function` (Callable[..., Any]): A function that takes an entry from `data` and returns a key for duplicate check. 

273 

274 Returns: 

275 - `List[dict]`: A list of unique entries, based on the unique keys generated. 

276 

277 Example: 

278 >>> data = [{'name': 'Alice', 'age': 30}, {'name': 'Bob', 'age': 25}, {'name': 'Alice', 'age': 30}] 

279 >>> remove_duplicates_by_key(data, lambda x: (x['name'], x['age'])) 

280 [{'name': 'Alice', 'age': 30}, {'name': 'Bob', 'age': 25}] 

281 """ 

282 

283 seen_keys = set() 

284 unique_data = [] 

285 

286 for entry in data: 

287 key = frozenset(key_function(entry)) 

288 if key not in seen_keys: 

289 seen_keys.add(key) 

290 unique_data.append(entry) 

291 

292 return unique_data 

293 

294 

295def sort_by_key_length( 

296 data: List[Any], key_function: Callable[[Any], Any] 

297) -> List[Any]: 

298 """ 

299 Sort a list of entries based on the length of a specific key. 

300 

301 Args: 

302 - data (List[Any]): A list of data entries. 

303 - key_function (Callable[[Any], Any]): A function that takes an entry from `data` and returns a key 

304 whose length is to be used for sorting. 

305 

306 Returns: 

307 - List[Any]: A list of entries sorted by the length of the specified key. 

308 """ 

309 

310 return sorted(data, key=lambda x: len(key_function(x))) 

311 

312 

313def add_missing_key_to_dicts( 

314 data: List[Dict[str, Dict[str, Any]]], 

315 dict_key: str, 

316 missing_key: str, 

317 default_value: Any, 

318) -> List[Dict[str, Dict[str, Any]]]: 

319 """ 

320 Iterates through a list of dictionaries and adds a specified key with a default value to a specified 

321 dictionary within each main dictionary, if the key is not already present. Returns a new list with the updates. 

322 

323 Args: 

324 data: A list of dictionaries. 

325 dict_key: The key in the main dictionaries that points to another dictionary where the check should be done. 

326 missing_key: The key to add if it's not present in the nested dictionary. 

327 default_value: The default value to assign to the missing key. 

328 

329 Returns: 

330 A new list of dictionaries with the missing key added where necessary. 

331 

332 Example: 

333 >>> data = [{'Composition': {'A': 1, 'B': 2}}, {'Composition': {'B': 3}}] 

334 >>> updated_data = add_missing_key_to_dicts(data, 'Composition', 'Q', 0) 

335 >>> updated_data 

336 [{'Composition': {'A': 1, 'B': 2, 'Q': 0}}, {'Composition': {'B': 3, 'Q': 0}}] 

337 """ 

338 

339 updated_data = [] 

340 

341 for entry in data: 

342 # Create a copy of the entry to avoid modifying the original data 

343 updated_entry = entry.copy() 

344 

345 # Check if the missing key is not in the specified dictionary 

346 if missing_key not in updated_entry.get(dict_key, {}): 

347 # Ensure the dict_key points to a dictionary 

348 if dict_key not in updated_entry: 

349 updated_entry[dict_key] = {} 

350 

351 # Add the missing key with the default value 

352 updated_entry[dict_key][missing_key] = default_value 

353 

354 updated_data.append(updated_entry) 

355 

356 return updated_data 

357 

358 

359def extract_results_by_key( 

360 data: List[Dict[str, any]], key: str = "new_reaction" 

361) -> Tuple[List[Dict[str, any]], List[Dict[str, any]]]: 

362 """ 

363 Separate dictionaries from a list into two lists based on the presence of a specific key. 

364 

365 Args: 

366 data: A list of dictionaries to be separated. 

367 key: The key to check for in each dictionary. Defaults to 'new_reaction'. 

368 

369 Returns: 

370 A tuple of two lists: 

371 - The first list contains dictionaries that have the specified key. 

372 - The second list contains dictionaries that do not have the specified key. 

373 """ 

374 

375 with_key: List[Dict[str, any]] = [] 

376 without_key: List[Dict[str, any]] = [] 

377 

378 # Separate dictionaries based on the presence of the key 

379 for item in data: 

380 if key in item: 

381 with_key.append(item) 

382 else: 

383 without_key.append(item) 

384 

385 return with_key, without_key 

386 

387 

388def get_random_samples_by_key( 

389 data: List[Dict[str, Any]], 

390 stratify_key: str, 

391 num_samples_per_group: int = 1, 

392 random_seed: int = None, 

393) -> List[Dict[str, Any]]: 

394 """ 

395 Get random samples from data, grouped by a specified key. 

396 

397 Parameters: 

398 - data: List of dictionaries containing various keys. 

399 - stratify_key: The key used for stratifying the data. 

400 - num_samples_per_group: Number of random samples to draw from each unique group. 

401 - random_seed: Seed for the random number generator for reproducibility. 

402 

403 Returns: 

404 - A list of randomly selected samples from each unique group based on the stratify_key. 

405 """ 

406 if random_seed is not None: 

407 random.seed(random_seed) 

408 

409 # Function to create a sortable key from the item 

410 def sortable_key(item: Dict[str, Any]) -> Union[str, tuple]: 

411 key_value = item.get(stratify_key, None) 

412 if isinstance(key_value, dict): 

413 return tuple(sorted(key_value.items())) 

414 elif isinstance(key_value, list): 

415 return tuple(sorted(key_value)) 

416 else: 

417 return key_value 

418 

419 # Group data by the specified stratify_key 

420 grouped_data = defaultdict(list) 

421 for item in data: 

422 grouped_key = sortable_key(item) 

423 grouped_data[grouped_key].append(item) 

424 

425 # Select random samples from each group 

426 random_samples = [] 

427 for _, items in grouped_data.items(): 

428 if len(items) >= num_samples_per_group: 

429 random_samples.extend(random.sample(items, num_samples_per_group)) 

430 else: 

431 # If there aren't enough items, take all available 

432 random_samples.extend(items) 

433 

434 return random_samples