Beispiel #1
0
    def from_dict(
        cls,
        dict_: StrDict,
        tree: SearchTree,
        config: Configuration,
        molecules: MoleculeDeserializer,
        parent: "Node" = None,
    ) -> "Node":
        """
        Create a new node from a dictionary, i.e. deserialization

        :param dict_: the serialized node
        :param tree: the search tree
        :param config: settings of the tree search algorithm
        :param molecules: the deserialized molecules
        :param parent: the parent node
        :return: a deserialized node
        """
        # pylint: disable=protected-access
        state = State.from_dict(dict_["state"], config, molecules)
        node = Node(state=state, owner=tree, config=config, parent=parent)
        node.is_expanded = dict_["is_expanded"]
        node.is_expandable = dict_["is_expandable"]
        node._children_values = dict_["children_values"]
        node._children_priors = dict_["children_priors"]
        node._children_visitations = dict_["children_visitations"]
        node._children_actions = []
        for action in dict_["children_actions"]:
            mol = molecules.get_tree_molecules([action["mol"]])[0]
            node._children_actions.append(
                RetroReaction(
                    mol,
                    action["smarts"],
                    action["index"],
                    action.get("metadata", {}),
                ))

        node._children = [
            Node.from_dict(child, tree, config, molecules, parent=node)
            if child else None for child in dict_["children"]
        ]
        return node
Beispiel #2
0
    def _make_child_states(self, reaction):
        mols = [mol for mol in self.state.mols if mol is not reaction.mol]
        reactants_list = reaction.apply()

        if not reactants_list:
            self._logger.debug(
                "Reactants_list empty %s, for mol %s and transformation %s" %
                (repr(reactants_list), reaction.mol.smiles, reaction.smarts))
            return []

        # fmt: off
        if (len(reactants_list) == 1 and len(reactants_list[0]) == 1
                and reaction.mol == reactants_list[0][0]):
            return []
        # fmt: on

        return [
            State(mols + list(reactants), self._config)
            for reactants in reactants_list
        ]
Beispiel #3
0
    def from_dict(cls, dict_, tree, config, molecules, parent=None):
        """
        Create a new node from a dictionary, i.e. deserialization

        :param dict_: the serialized node
        :type dict_: dict
        :param tree: the search tree
        :type tree: SearchTree
        :param config: settings of the tree search algorithm
        :type config: Configuration
        :param molecules: the deserialized molecules
        :type molecules: MoleculeDeserializer
        :param parent: the parent node
        :type parent: Node
        :return: a deserialized node
        :rtype: Node
        """
        state = State.from_dict(dict_["state"], config, molecules)
        node = Node(state=state, owner=tree, config=config, parent=parent)
        node.is_expanded = dict_["is_expanded"]
        node.is_expandable = dict_["is_expandable"]
        node._children_values = dict_["children_values"]
        node._children_priors = dict_["children_priors"]
        node._children_visitations = dict_["children_visitations"]
        node._children_actions = [
            RetroReaction(
                molecules[action["mol"]],
                action["smarts"],
                action["index"],
                action.get("metadata", {}),
            ) for action in dict_["children_actions"]
        ]
        node._children = [
            Node.from_dict(child, tree, config, molecules, parent=node)
            if child else None for child in dict_["children"]
        ]
        return node
 def wrapper(root_smiles, config):
     mol = TreeMolecule(parent=None, transform=0, smiles=root_smiles)
     state = State(mols=[mol], config=config)
     mocked_create_root.return_value = Node(state=state, owner=None, config=config)
     return mol