graphinvent/MolecularGraph.py
def features_to_atom(self, node_idx : int) -> rdkit.Chem.Atom:
        """
        Converts the atomic feature vector corresponding to the atom indicated
        by input `node_idx` into an `rdkit.Atom` object.

        The atomic feature vector describes a unique node on a graph using
        concatenated one-hot encoded vectors for the features of interest (e.g.
        atom type, formal charge), and is a row of `self.node_features`. Note
        that if `ignore_H` flag is used, will assign a placeholder of 0 to the
        number of implicit hydrogens in the atom (to be corrected for later via
        kekulization).

        Args:
        ----
            node_idx (int) : Index for a node feature vector (i.e. a row of
                             `self.node_features`).

        Returns:
        -------
            new_atom (rdkit.Atom) : Atom object.
        """
        # determine the nonzero indices of the feature vector
        feature_vector = self.node_features[node_idx]
        try:  # if `feature_vector` is a `torch.Tensor`
            nonzero_idc = torch.nonzero(feature_vector)
        except TypeError:  # if `feature_vector` is a `numpy.ndarray`
            nonzero_idc = np.nonzero(feature_vector)[0]

        # determine atom symbol
        atom_idx  = nonzero_idc[0]
        atom_type = self.constants.atom_types[atom_idx]
        new_atom  = rdkit.Chem.Atom(atom_type)

        # determine formal charge
        fc_idx        = nonzero_idc[1] - self.constants.n_atom_types
        formal_charge = self.constants.formal_charge[fc_idx]

        new_atom.SetFormalCharge(formal_charge)

        # determine number of implicit Hs
        if not self.constants.use_explicit_H and not self.constants.ignore_H:
            total_num_h_idx = (
                nonzero_idc[2]
                - self.constants.n_atom_types
                - self.constants.n_formal_charge
            )
            total_num_h = self.constants.imp_H[total_num_h_idx]

            new_atom.SetUnsignedProp("_TotalNumHs", total_num_h)

        elif self.constants.ignore_H:
            # Hs will be set with structure is sanitized later
            pass

        # determine chirality
        if self.constants.use_chirality:
            cip_code_idx = (
                    nonzero_idc[-1]
                    - self.constants.n_atom_types
                    - self.constants.n_formal_charge
                    - bool(not self.constants.use_explicit_H and
                           not self.constants.ignore_H)
                    * self.constants.n_imp_H
            )
            cip_code = self.constants.chirality[cip_code_idx]
            new_atom.SetProp("_CIPCode", cip_code)

        return new_atom

        Converts the atomic feature vector corresponding to the atom indicated
        by input `node_idx` into an `rdkit.Atom` object.

        The atomic feature vector describes a unique node on a graph using
        concatenated one-hot encoded vectors for the features of interest (e.g.
        atom type, formal charge), and is a row of `self.node_features`. Note
        that if `ignore_H` flag is used, will assign a placeholder of 0 to the
        number of implicit hydrogens in the atom (to be corrected for later via
        kekulization).

        Args:
        ----
            node_idx (int) : Index for a node feature vector (i.e. a row of
                             `self.node_features`).

        Returns:
        -------
            new_atom (rdkit.Atom) : Atom object.
        
Converts the atomic feature vector corresponding to the atom indicated
by input `node_idx` into an `rdkit.Atom` object.

The atomic feature vector describes a unique node on a graph using
concatenated one-hot encoded vectors for the features of interest , and is a row of `self.node_features`. Note
that if `ignore_H` flag is used, will assign a placeholder of 0 to the
number of implicit hydrogens in the atom (to be corrected for later via
kekulization).
Converts the atomic feature vector corresponding to the atom indicated by input ` node_idx ` into an ` rdkit . Atom ` object . The atomic feature vector describes a unique node on a graph using concatenated one - hot encoded vectors for the features of interest and is a row of ` self . node_features ` . Note that if ` ignore_H ` flag is used will assign a placeholder of 0 to the number of implicit hydrogens in the atom ( to be corrected for later via kekulization ) .
def features_to_atom(self, node_idx : int) -> rdkit.Chem.Atom:
        feature_vector = self.node_features[node_idx]
        try:  
            nonzero_idc = torch.nonzero(feature_vector)
        except TypeError:  
            nonzero_idc = np.nonzero(feature_vector)[0]
        atom_idx  = nonzero_idc[0]
        atom_type = self.constants.atom_types[atom_idx]
        new_atom  = rdkit.Chem.Atom(atom_type)
        fc_idx        = nonzero_idc[1] - self.constants.n_atom_types
        formal_charge = self.constants.formal_charge[fc_idx]
        new_atom.SetFormalCharge(formal_charge)
        if not self.constants.use_explicit_H and not self.constants.ignore_H:
            total_num_h_idx = (
                nonzero_idc[2]
                - self.constants.n_atom_types
                - self.constants.n_formal_charge
            )
            total_num_h = self.constants.imp_H[total_num_h_idx]
            new_atom.SetUnsignedProp("_TotalNumHs", total_num_h)
        elif self.constants.ignore_H:
            pass
        if self.constants.use_chirality:
            cip_code_idx = (
                    nonzero_idc[-1]
                    - self.constants.n_atom_types
                    - self.constants.n_formal_charge
                    - bool(not self.constants.use_explicit_H and
                           not self.constants.ignore_H)
                    * self.constants.n_imp_H
            )
            cip_code = self.constants.chirality[cip_code_idx]
            new_atom.SetProp("_CIPCode", cip_code)
        return new_atom
def features_to_atom ( self , node_idx : int ) -> rdkit . Chem . Atom : feature_vector = self . node_features [ node_idx ] try : nonzero_idc = torch . nonzero ( feature_vector ) except TypeError : nonzero_idc = np . nonzero ( feature_vector ) [ 0 ] atom_idx = nonzero_idc [ 0 ] atom_type = self . constants . atom_types [ atom_idx ] new_atom = rdkit . Chem . Atom ( atom_type ) fc_idx = nonzero_idc [ 1 ] - self . constants . n_atom_types formal_charge = self . constants . formal_charge [ fc_idx ] new_atom . SetFormalCharge ( formal_charge ) if not self . constants . use_explicit_H and not self . constants . ignore_H : total_num_h_idx = ( nonzero_idc [ 2 ] - self . constants . n_atom_types - self . constants . n_formal_charge ) total_num_h = self . constants . imp_H [ total_num_h_idx ] new_atom . SetUnsignedProp ( "_TotalNumHs" , total_num_h ) elif self . constants . ignore_H : pass if self . constants . use_chirality : cip_code_idx = ( nonzero_idc [ - 1 ] - self . constants . n_atom_types - self . constants . n_formal_charge - bool ( not self . constants . use_explicit_H and not self . constants . ignore_H ) * self . constants . n_imp_H ) cip_code = self . constants . chirality [ cip_code_idx ] new_atom . SetProp ( "_CIPCode" , cip_code ) return new_atom
Converts the atomic feature vector corresponding to the atom indicated
by input `node_idx` into an `rdkit.Atom` object.
Converts the atomic feature vector corresponding to the atom indicated by input ` node_idx ` into an ` rdkit . Atom ` object .
"""
        Converts the atomic feature vector corresponding to the atom indicated
        by input `node_idx` into an `rdkit.Atom` object.

        The atomic feature vector describes a unique node on a graph using
        concatenated one-hot encoded vectors for the features of interest (e.g.
        atom type, formal charge), and is a row of `self.node_features`. Note
        that if `ignore_H` flag is used, will assign a placeholder of 0 to the
        number of implicit hydrogens in the atom (to be corrected for later via
        kekulization).

        Args:
        ----
            node_idx (int) : Index for a node feature vector (i.e. a row of
                             `self.node_features`).

        Returns:
        -------
            new_atom (rdkit.Atom) : Atom object.
        """ # determine the nonzero indices of the feature vector # if `feature_vector` is a `torch.Tensor` # if `feature_vector` is a `numpy.ndarray` # determine atom symbol # determine formal charge # determine number of implicit Hs # Hs will be set with structure is sanitized later # determine chirality
new_atom (rdkit.Atom) : Atom object.
new_atom ( rdkit . Atom ) : Atom object .
self
node_idx
int
Index for a node feature vector .
Index for a node feature vector .