def test_parser_roundtrip_no_rule_sequence(self): mol = MolFromSmiles(smiles1) tree = hypergraph_parser(mol) graph4 = graph_from_graph_tree(tree) mol4 = to_mol(graph4) smiles4 = MolToSmiles(mol4) assert smiles4 == smiles1
def test_graph_from_graph_tree_idempotent(self): g = HypergraphGrammar() g.strings_to_actions(smiles) g.calc_terminal_distance() tree = g.normalize_tree(hypergraph_parser(MolFromSmiles(smiles1))) # The second call here would fail before # This was solved by copying in remove_nonterminals where the issue # was with mutating the parent tree.node state graph1 = graph_from_graph_tree(tree) graph2 = graph_from_graph_tree(tree) mol1 = to_mol(graph1) mol2 = to_mol(graph2) recovered_smiles1 = MolToSmiles(mol1) recovered_smiles2 = MolToSmiles(mol2) self.assertEqual(smiles1, recovered_smiles1) self.assertEqual(recovered_smiles1, recovered_smiles2)
def _parser_roundtrip(self, grammar, smiles_strings): collapsed_trees = [ grammar.raw_strings_to_trees([smile])[0] for smile in smiles_strings ] recovered_smiles = [] for tree in collapsed_trees: graph = graph_from_graph_tree(tree) mol = to_mol(graph) recovered_smiles.append(MolToSmiles(mol)) return recovered_smiles
def test_hypergraph_rpe_parser(self): g = HypergraphGrammar() g.strings_to_actions(smiles) trees = [ g.normalize_tree(hypergraph_parser(MolFromSmiles(smile))) for smile in smiles ] rule_pairs = extract_popular_hypergraph_pairs(g, trees, 10) parser = HypergraphRPEParser(g, rule_pairs) collapsed_trees = [parser.parse(smile) for smile in smiles] recovered_smiles = [] for tree in collapsed_trees: graph = graph_from_graph_tree(tree) mol = to_mol(graph) recovered_smiles.append(MolToSmiles(mol)) self.assertEqual(smiles, recovered_smiles)
def test_hypergraph_rpe(self): g = HypergraphGrammar() g.strings_to_actions(smiles) tree = g.normalize_tree(hypergraph_parser(MolFromSmiles(smiles1))) num_rules_before = len(g.rules) rule_pairs = extract_popular_hypergraph_pairs(g, [tree], 10) num_rules_after = len(g.rules) tree_rules_before = len(tree.rules()) collapsed_tree = apply_hypergraph_substitution(g, tree, rule_pairs[0]) tree_rules_after = len(collapsed_tree.rules()) graph = graph_from_graph_tree(collapsed_tree) mol = to_mol(graph) recovered_smiles = MolToSmiles(mol) self.assertEqual(smiles1, recovered_smiles) self.assertGreater(num_rules_after, num_rules_before) self.assertLess(tree_rules_after, tree_rules_before)