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 = [ Reaction(molecules[action["mol"]], action["smarts"], action["index"]) 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 test_serialize_deserialize_state(default_config): mol = TreeMolecule(parent=None, smiles="CCC", transform=1) state0 = State([mol], default_config) serializer = MoleculeSerializer() state_serialized = state0.serialize(serializer) assert len(state_serialized["mols"]) == 1 assert state_serialized["mols"][0] == id(mol) deserializer = MoleculeDeserializer(serializer.store) state1 = State.from_dict(state_serialized, default_config, deserializer) assert len(state1.mols) == 1 assert state1.mols[0] == state0.mols[0] assert state1.in_stock_list == state0.in_stock_list assert state1.score == state0.score
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 """ 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