graphinvent/MolecularGraph.py
def graph_to_mol(self) -> rdkit.Chem.Mol:
        """
        Generates the `rdkit.Chem.Mol` object corresponding to the graph.

        The function uses for a given graph:
          * `n_nodes`       : number of nodes in graph
          * `node_features` : node feature matrix, a |V|x|NF| matrix
          * `edge_features` : edge feature tensor, a |V|x|V|x|B| tensor

        Above, V is the set of all nodes in a graph, NF is the set of node
        features, and B is the set of available bond types.
        """
        # create empty editable `rdkit.Chem.Mol` object
        molecule    = rdkit.Chem.RWMol()

        # add atoms to `rdkit.Chem.Mol` and keep track of idx
        node_to_idx = {}

        for v in range(0, self.n_nodes):
            atom_to_add    = self.features_to_atom(node_idx=v)
            molecule_idx   = molecule.AddAtom(atom_to_add)
            node_to_idx[v] = molecule_idx

        # add bonds between adjacent atoms
        for bond_type in range(self.constants.n_edge_features):
            # `self.edge_features[:, :, bond_type]` is an adjacency matrix
            #  for that specific `bond_type`
            for bond_idx1, row in enumerate(
                self.edge_features[:self.n_nodes, :self.n_nodes, bond_type]
                ):
                # traverse only half adjacency matrix to not duplicate bonds
                for bond_idx2 in range(bond_idx1):
                    bond = row[bond_idx2]
                    if bond:  # if `bond_idx1` and `bond_idx2` are bonded
                        try:  # try adding the bond to `rdkit.Chem.Mol` object
                            molecule.AddBond(
                                node_to_idx[bond_idx1],
                                node_to_idx[bond_idx2],
                                self.constants.int_to_bondtype[bond_type]
                            )
                        except (TypeError, RuntimeError, AttributeError):
                            # errors occur if the above `AddBond()` action tries
                            # to add multiple bonds to a node pair (should not
                            # happen, but kept here as a safety)
                            raise ValueError("MolecularGraphError: Multiple "
                                             "edges connecting a single pair "
                                             "of nodes in graph.")

        try:  # convert from `rdkit.Chem.RWMol` to Mol object
            molecule.GetMol()
        except AttributeError:  # raised if molecules is `None`
            pass

        # if `ignore_H` flag is used, "sanitize" the structure to correct
        # the number of implicit hydrogens (otherwise, they will all stay at 0)
        if self.constants.ignore_H and molecule:
            try:
                rdkit.Chem.SanitizeMol(molecule)
            except ValueError:
                # raised if `molecule` is False, None, or too ugly to sanitize
                pass

        return molecule

        Generates the `rdkit.Chem.Mol` object corresponding to the graph.

        The function uses for a given graph:
          * `n_nodes`       : number of nodes in graph
          * `node_features` : node feature matrix, a |V|x|NF| matrix
          * `edge_features` : edge feature tensor, a |V|x|V|x|B| tensor

        Above, V is the set of all nodes in a graph, NF is the set of node
        features, and B is the set of available bond types.
        
Generates the `rdkit.Chem.Mol` object corresponding to the graph.

Above, V is the set of all nodes in a graph, NF is the set of node
features, and B is the set of available bond types.
Generates the ` rdkit . Chem . Mol ` object corresponding to the graph . Above V is the set of all nodes in a graph NF is the set of node features and B is the set of available bond types .
def graph_to_mol(self) -> rdkit.Chem.Mol:
        molecule    = rdkit.Chem.RWMol()
        node_to_idx = {}
        for v in range(0, self.n_nodes):
            atom_to_add    = self.features_to_atom(node_idx=v)
            molecule_idx   = molecule.AddAtom(atom_to_add)
            node_to_idx[v] = molecule_idx
        for bond_type in range(self.constants.n_edge_features):
            for bond_idx1, row in enumerate(
                self.edge_features[:self.n_nodes, :self.n_nodes, bond_type]
                ):
                for bond_idx2 in range(bond_idx1):
                    bond = row[bond_idx2]
                    if bond:  
                        try:  
                            molecule.AddBond(
                                node_to_idx[bond_idx1],
                                node_to_idx[bond_idx2],
                                self.constants.int_to_bondtype[bond_type]
                            )
                        except (TypeError, RuntimeError, AttributeError):
                            raise ValueError("MolecularGraphError: Multiple "
                                             "edges connecting a single pair "
                                             "of nodes in graph.")
        try:  
            molecule.GetMol()
        except AttributeError:  
            pass
        if self.constants.ignore_H and molecule:
            try:
                rdkit.Chem.SanitizeMol(molecule)
            except ValueError:
                pass
        return molecule
def graph_to_mol ( self ) -> rdkit . Chem . Mol : molecule = rdkit . Chem . RWMol ( ) node_to_idx = { } for v in range ( 0 , self . n_nodes ) : atom_to_add = self . features_to_atom ( node_idx = v ) molecule_idx = molecule . AddAtom ( atom_to_add ) node_to_idx [ v ] = molecule_idx for bond_type in range ( self . constants . n_edge_features ) : for bond_idx1 , row in enumerate ( self . edge_features [ : self . n_nodes , : self . n_nodes , bond_type ] ) : for bond_idx2 in range ( bond_idx1 ) : bond = row [ bond_idx2 ] if bond : try : molecule . AddBond ( node_to_idx [ bond_idx1 ] , node_to_idx [ bond_idx2 ] , self . constants . int_to_bondtype [ bond_type ] ) except ( TypeError , RuntimeError , AttributeError ) : raise ValueError ( "MolecularGraphError: Multiple " "edges connecting a single pair " "of nodes in graph." ) try : molecule . GetMol ( ) except AttributeError : pass if self . constants . ignore_H and molecule : try : rdkit . Chem . SanitizeMol ( molecule ) except ValueError : pass return molecule
Generates the `rdkit.Chem.Mol` object corresponding to the graph.
Generates the ` rdkit . Chem . Mol ` object corresponding to the graph .
"""
        Generates the `rdkit.Chem.Mol` object corresponding to the graph.

        The function uses for a given graph:
          * `n_nodes`       : number of nodes in graph
          * `node_features` : node feature matrix, a |V|x|NF| matrix
          * `edge_features` : edge feature tensor, a |V|x|V|x|B| tensor

        Above, V is the set of all nodes in a graph, NF is the set of node
        features, and B is the set of available bond types.
        """ # create empty editable `rdkit.Chem.Mol` object # add atoms to `rdkit.Chem.Mol` and keep track of idx # add bonds between adjacent atoms # `self.edge_features[:, :, bond_type]` is an adjacency matrix #  for that specific `bond_type` # traverse only half adjacency matrix to not duplicate bonds # if `bond_idx1` and `bond_idx2` are bonded # try adding the bond to `rdkit.Chem.Mol` object # errors occur if the above `AddBond()` action tries # to add multiple bonds to a node pair (should not # happen, but kept here as a safety) # convert from `rdkit.Chem.RWMol` to Mol object # raised if molecules is `None` # if `ignore_H` flag is used, "sanitize" the structure to correct # the number of implicit hydrogens (otherwise, they will all stay at 0) # raised if `molecule` is False, None, or too ugly to sanitize
self