graphinvent/GraphGenerator.py
def _add_nodes(add : Tuple[torch.Tensor, ...], generation_round : int,
                       likelihoods_sampled : torch.Tensor) -> None:
            """
            Adds new nodes to graphs which sampled the "add" action.

            Args:
            ----
                add (tuple)                        : Indices for "add" actions sampled
                                                     for batch of graphs.
                generation_round (int)             : Indicates current generation
                                                     round.
                likelihoods_sampled (torch.Tensor) : NLL per action sampled for
                                                     the most recent set of actions.
            """
            # get the action indices
            add = [idx.long() for idx in add]
            n_node_features = [constants.n_atom_types,
                               constants.n_formal_charge,
                               constants.n_imp_H,
                               constants.n_chirality]

            if not constants.use_explicit_H and not constants.ignore_H:
                if constants.use_chirality:
                    (batch, bond_to, atom_type, charge,
                     imp_h, chirality, bond_type, bond_from) = add

                    # add the new nodes to the node features tensors
                    self.nodes[batch, bond_from, atom_type] = 1
                    self.nodes[batch, bond_from, charge + n_node_features[0]] = 1
                    self.nodes[batch, bond_from, imp_h + sum(n_node_features[0:2])] = 1
                    self.nodes[batch, bond_from, chirality + sum(n_node_features[0:3])] = 1
                else:
                    (batch, bond_to, atom_type, charge,
                     imp_h, bond_type, bond_from) = add

                    # add the new nodes to the node features tensors
                    self.nodes[batch, bond_from, atom_type] = 1
                    self.nodes[batch, bond_from, charge + n_node_features[0]] = 1
                    self.nodes[batch, bond_from, imp_h + sum(n_node_features[0:2])] = 1
            elif constants.use_chirality:
                (batch, bond_to, atom_type, charge,
                 chirality, bond_type, bond_from) = add

                # add the new nodes to the node features tensors
                self.nodes[batch, bond_from, atom_type] = 1
                self.nodes[batch, bond_from, charge + n_node_features[0]] = 1
                self.nodes[batch, bond_from, chirality + sum(n_node_features[0:2])] = 1
            else:
                (batch, bond_to, atom_type, charge,
                 bond_type, bond_from) = add

                # add the new nodes to the node features tensors
                self.nodes[batch, bond_from, atom_type] = 1
                self.nodes[batch, bond_from, charge + n_node_features[0]] = 1

            # mask dummy edges (self-loops) introduced from adding node to empty graph
            batch_masked = batch[torch.nonzero(self.n_nodes[batch] != 0)]
            bond_to_masked = bond_to[torch.nonzero(self.n_nodes[batch] != 0)]
            bond_from_masked = bond_from[torch.nonzero(self.n_nodes[batch] != 0)]
            bond_type_masked = bond_type[torch.nonzero(self.n_nodes[batch] != 0)]

            # connect newly added nodes to the graphs
            self.edges[batch_masked, bond_to_masked, bond_from_masked, bond_type_masked] = 1
            self.edges[batch_masked, bond_from_masked, bond_to_masked, bond_type_masked] = 1

            # keep track of the newly added node
            self.n_nodes[batch] += 1

            # include the NLLs for the add actions for this generation round
            self.likelihoods[batch, generation_round] = likelihoods_sampled[batch]

            Adds new nodes to graphs which sampled the "add" action.

            Args:
            ----
                add (tuple)                        : Indices for "add" actions sampled
                                                     for batch of graphs.
                generation_round (int)             : Indicates current generation
                                                     round.
                likelihoods_sampled (torch.Tensor) : NLL per action sampled for
                                                     the most recent set of actions.
            
Adds new nodes to graphs which sampled the "add" action.
Adds new nodes to graphs which sampled the " add " action .
def _add_nodes(add : Tuple[torch.Tensor, ...], generation_round : int,
                       likelihoods_sampled : torch.Tensor) -> None:
            add = [idx.long() for idx in add]
            n_node_features = [constants.n_atom_types,
                               constants.n_formal_charge,
                               constants.n_imp_H,
                               constants.n_chirality]
            if not constants.use_explicit_H and not constants.ignore_H:
                if constants.use_chirality:
                    (batch, bond_to, atom_type, charge,
                     imp_h, chirality, bond_type, bond_from) = add
                    self.nodes[batch, bond_from, atom_type] = 1
                    self.nodes[batch, bond_from, charge + n_node_features[0]] = 1
                    self.nodes[batch, bond_from, imp_h + sum(n_node_features[0:2])] = 1
                    self.nodes[batch, bond_from, chirality + sum(n_node_features[0:3])] = 1
                else:
                    (batch, bond_to, atom_type, charge,
                     imp_h, bond_type, bond_from) = add
                    self.nodes[batch, bond_from, atom_type] = 1
                    self.nodes[batch, bond_from, charge + n_node_features[0]] = 1
                    self.nodes[batch, bond_from, imp_h + sum(n_node_features[0:2])] = 1
            elif constants.use_chirality:
                (batch, bond_to, atom_type, charge,
                 chirality, bond_type, bond_from) = add
                self.nodes[batch, bond_from, atom_type] = 1
                self.nodes[batch, bond_from, charge + n_node_features[0]] = 1
                self.nodes[batch, bond_from, chirality + sum(n_node_features[0:2])] = 1
            else:
                (batch, bond_to, atom_type, charge,
                 bond_type, bond_from) = add
                self.nodes[batch, bond_from, atom_type] = 1
                self.nodes[batch, bond_from, charge + n_node_features[0]] = 1
            batch_masked = batch[torch.nonzero(self.n_nodes[batch] != 0)]
            bond_to_masked = bond_to[torch.nonzero(self.n_nodes[batch] != 0)]
            bond_from_masked = bond_from[torch.nonzero(self.n_nodes[batch] != 0)]
            bond_type_masked = bond_type[torch.nonzero(self.n_nodes[batch] != 0)]
            self.edges[batch_masked, bond_to_masked, bond_from_masked, bond_type_masked] = 1
            self.edges[batch_masked, bond_from_masked, bond_to_masked, bond_type_masked] = 1
            self.n_nodes[batch] += 1
            self.likelihoods[batch, generation_round] = likelihoods_sampled[batch]
def _add_nodes ( add : Tuple [ torch . Tensor , ... ] , generation_round : int , likelihoods_sampled : torch . Tensor ) -> None : add = [ idx . long ( ) for idx in add ] n_node_features = [ constants . n_atom_types , constants . n_formal_charge , constants . n_imp_H , constants . n_chirality ] if not constants . use_explicit_H and not constants . ignore_H : if constants . use_chirality : ( batch , bond_to , atom_type , charge , imp_h , chirality , bond_type , bond_from ) = add self . nodes [ batch , bond_from , atom_type ] = 1 self . nodes [ batch , bond_from , charge + n_node_features [ 0 ] ] = 1 self . nodes [ batch , bond_from , imp_h + sum ( n_node_features [ 0 : 2 ] ) ] = 1 self . nodes [ batch , bond_from , chirality + sum ( n_node_features [ 0 : 3 ] ) ] = 1 else : ( batch , bond_to , atom_type , charge , imp_h , bond_type , bond_from ) = add self . nodes [ batch , bond_from , atom_type ] = 1 self . nodes [ batch , bond_from , charge + n_node_features [ 0 ] ] = 1 self . nodes [ batch , bond_from , imp_h + sum ( n_node_features [ 0 : 2 ] ) ] = 1 elif constants . use_chirality : ( batch , bond_to , atom_type , charge , chirality , bond_type , bond_from ) = add self . nodes [ batch , bond_from , atom_type ] = 1 self . nodes [ batch , bond_from , charge + n_node_features [ 0 ] ] = 1 self . nodes [ batch , bond_from , chirality + sum ( n_node_features [ 0 : 2 ] ) ] = 1 else : ( batch , bond_to , atom_type , charge , bond_type , bond_from ) = add self . nodes [ batch , bond_from , atom_type ] = 1 self . nodes [ batch , bond_from , charge + n_node_features [ 0 ] ] = 1 batch_masked = batch [ torch . nonzero ( self . n_nodes [ batch ] != 0 ) ] bond_to_masked = bond_to [ torch . nonzero ( self . n_nodes [ batch ] != 0 ) ] bond_from_masked = bond_from [ torch . nonzero ( self . n_nodes [ batch ] != 0 ) ] bond_type_masked = bond_type [ torch . nonzero ( self . n_nodes [ batch ] != 0 ) ] self . edges [ batch_masked , bond_to_masked , bond_from_masked , bond_type_masked ] = 1 self . edges [ batch_masked , bond_from_masked , bond_to_masked , bond_type_masked ] = 1 self . n_nodes [ batch ] += 1 self . likelihoods [ batch , generation_round ] = likelihoods_sampled [ batch ]
Adds new nodes to graphs which sampled the "add" action.
Adds new nodes to graphs which sampled the " add " action .
"""
            Adds new nodes to graphs which sampled the "add" action.

            Args:
            ----
                add (tuple)                        : Indices for "add" actions sampled
                                                     for batch of graphs.
                generation_round (int)             : Indicates current generation
                                                     round.
                likelihoods_sampled (torch.Tensor) : NLL per action sampled for
                                                     the most recent set of actions.
            """ # get the action indices # add the new nodes to the node features tensors # add the new nodes to the node features tensors # add the new nodes to the node features tensors # add the new nodes to the node features tensors # mask dummy edges (self-loops) introduced from adding node to empty graph # connect newly added nodes to the graphs # keep track of the newly added node # include the NLLs for the add actions for this generation round
add
Tuple[torch.Tensor, ...]
Indices for "add" actions sampled
for batch of graphs.
Indices for " add " actions sampled for batch of graphs .
generation_round
int
Indicates current generation
round.
Indicates current generation round .
likelihoods_sampled
torch.Tensor
NLL per action sampled for
the most recent set of actions.
NLL per action sampled for the most recent set of actions .