class _ReactionTreeLoader(abc.ABC): """ Base class for classes that creates a reaction tree object """ def __init__(self, *args, **kwargs): # To avoid circular imports from aizynthfinder.analysis import ReactionTree # noqa self.tree = ReactionTree() self._load(*args, **kwargs) self.tree.is_solved = all( self.tree.in_stock(node) for node in self.tree.leafs()) _RepeatingPatternIdentifier.find(self.tree) def _add_node(self, node, depth=0, transform=0, in_stock=False, hide=False): attributes = { "hide": hide, "depth": depth, } if isinstance(node, Molecule): attributes.update({"transform": transform, "in_stock": in_stock}) self.tree.graph.add_node(node, **attributes) if not self.tree.root: self.tree.root = node @abc.abstractmethod def _load(self, *args, **kwargs): pass
def _score_reaction_tree(self, tree: ReactionTree) -> float: mols = [ TreeMolecule(parent=None, transform=tree.depth(leaf) // 2, smiles=leaf.smiles) for leaf in tree.leafs() ] state = State(mols, self._config) return state.score
def _score_reaction_tree(self, tree: ReactionTree) -> float: def _recursive_score(node): # This list should contains 0 or 1 elements reaction_nodes = list(tree.graph[node]) if not reaction_nodes: return leaf_costs[node] child_sum = sum(1 / self.average_yield * _recursive_score(child) for child in tree.graph[reaction_nodes[0]]) return self.reaction_cost + child_sum leaf_costs = self._calculate_leaf_costs(tree.leafs()) return _recursive_score(tree.root)
def _score_reaction_tree(self, tree: ReactionTree) -> float: leaf_costs = self._calculate_leaf_costs(tree.leafs()) return sum(leaf_costs[leaf] for leaf in tree.leafs())
def _score_reaction_tree(self, tree: ReactionTree) -> float: return len([mol for mol in tree.leafs() if mol in self._stock])
def _score_reaction_tree(self, tree: ReactionTree) -> float: return len(list(tree.leafs()))