graphinvent/GraphGenerator.py
def apply_actions(self, add : Tuple[torch.Tensor, ...],
                      conn : Tuple[torch.Tensor, ...], generation_round : int,
                      likelihoods_sampled : torch.Tensor) -> None:
        """
        Applies the batch of sampled actions (specified by `add` and `conn`) to
        the batch of graphs under construction. Also adds the NLLs for the newly
        sampled actions (`likelihoods_sampled`) to the running list of NLLs.
        Updates the following tensors:
            self.nodes (torch.Tensor)       : Updated node features tensor (batch).
            self.edges (torch.Tensor)       : Updated edge features tensor (batch).
            self.n_nodes (torch.Tensor)     : Updated number of nodes per graph
                                              (batch).
            self.likelihoods (torch.Tensor) : Updated sampled NLL per action
                                              for graphs (batch).

        Args:
        ----
            add (tuple)            : Indices for "add" actions sampled for batch
                                     of graphs.
            conn (tuple)           : Indices for "connect" 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.
        """
        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]

        def _conn_nodes(conn : Tuple[torch.Tensor, ...], generation_round : int,
                        likelihoods_sampled : torch.Tensor) -> None:
            """
            Connects nodes in graphs which sampled the "connect" action.

            Args:
            ----
                conn (tuple)                       : Indices for "connect" 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
            conn = [idx.long() for idx in conn]
            batch, bond_to, bond_type, bond_from = conn

            # apply the connect actions
            self.edges[batch, bond_from, bond_to, bond_type] = 1
            self.edges[batch, bond_to, bond_from, bond_type] = 1

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

        # first applies the "add" action to all graphs in batch (note: does
        # nothing if a graph did not sample "add")
        _add_nodes(add, generation_round, likelihoods_sampled)

        # then applies the "connect" action to all graphs in batch (note: does
        # nothing if a graph did not sample "connect")
        _conn_nodes(conn, generation_round, likelihoods_sampled)

        Applies the batch of sampled actions (specified by `add` and `conn`) to
        the batch of graphs under construction. Also adds the NLLs for the newly
        sampled actions (`likelihoods_sampled`) to the running list of NLLs.
        Updates the following tensors:
            self.nodes (torch.Tensor)       : Updated node features tensor (batch).
            self.edges (torch.Tensor)       : Updated edge features tensor (batch).
            self.n_nodes (torch.Tensor)     : Updated number of nodes per graph
                                              (batch).
            self.likelihoods (torch.Tensor) : Updated sampled NLL per action
                                              for graphs (batch).

        Args:
        ----
            add (tuple)            : Indices for "add" actions sampled for batch
                                     of graphs.
            conn (tuple)           : Indices for "connect" 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.
        
Applies the batch of sampled actions (specified by `add` and `conn`) to
the batch of graphs under construction. Also adds the NLLs for the newly
sampled actions (`likelihoods_sampled`) to the running list of NLLs.
Updates the following tensors:
self.nodes (torch.Tensor)       : Updated node features tensor (batch).
Applies the batch of sampled actions ( specified by ` add ` and ` conn ` ) to the batch of graphs under construction . Also adds the NLLs for the newly sampled actions ( ` likelihoods_sampled ` ) to the running list of NLLs . Updates the following tensors : self . nodes ( torch . Tensor ) : Updated node features tensor ( batch ) .
def apply_actions(self, add : Tuple[torch.Tensor, ...],
                      conn : Tuple[torch.Tensor, ...], generation_round : int,
                      likelihoods_sampled : torch.Tensor) -> None:
        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 _conn_nodes(conn : Tuple[torch.Tensor, ...], generation_round : int,
                        likelihoods_sampled : torch.Tensor) -> None:
            conn = [idx.long() for idx in conn]
            batch, bond_to, bond_type, bond_from = conn
            self.edges[batch, bond_from, bond_to, bond_type] = 1
            self.edges[batch, bond_to, bond_from, bond_type] = 1
            self.likelihoods[batch, generation_round] = likelihoods_sampled[batch]
        _add_nodes(add, generation_round, likelihoods_sampled)
        _conn_nodes(conn, generation_round, likelihoods_sampled)
def apply_actions ( self , add : Tuple [ torch . Tensor , ... ] , conn : Tuple [ torch . Tensor , ... ] , generation_round : int , likelihoods_sampled : torch . Tensor ) -> None : 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.
            """ 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 _conn_nodes ( conn : Tuple [ torch . Tensor , ... ] , generation_round : int , likelihoods_sampled : torch . Tensor ) -> None : """
            Connects nodes in graphs which sampled the "connect" action.

            Args:
            ----
                conn (tuple)                       : Indices for "connect" 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.
            """ conn = [ idx . long ( ) for idx in conn ] batch , bond_to , bond_type , bond_from = conn self . edges [ batch , bond_from , bond_to , bond_type ] = 1 self . edges [ batch , bond_to , bond_from , bond_type ] = 1 self . likelihoods [ batch , generation_round ] = likelihoods_sampled [ batch ] _add_nodes ( add , generation_round , likelihoods_sampled ) _conn_nodes ( conn , generation_round , likelihoods_sampled )
Applies the batch of sampled actions (specified by `add` and `conn`) to
the batch of graphs under construction.
Applies the batch of sampled actions ( specified by ` add ` and ` conn ` ) to the batch of graphs under construction .
"""
        Applies the batch of sampled actions (specified by `add` and `conn`) to
        the batch of graphs under construction. Also adds the NLLs for the newly
        sampled actions (`likelihoods_sampled`) to the running list of NLLs.
        Updates the following tensors:
            self.nodes (torch.Tensor)       : Updated node features tensor (batch).
            self.edges (torch.Tensor)       : Updated edge features tensor (batch).
            self.n_nodes (torch.Tensor)     : Updated number of nodes per graph
                                              (batch).
            self.likelihoods (torch.Tensor) : Updated sampled NLL per action
                                              for graphs (batch).

        Args:
        ----
            add (tuple)            : Indices for "add" actions sampled for batch
                                     of graphs.
            conn (tuple)           : Indices for "connect" 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.

            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 """
            Connects nodes in graphs which sampled the "connect" action.

            Args:
            ----
                conn (tuple)                       : Indices for "connect" 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 # apply the connect actions # include the NLLs for the connect actions for this generation round # first applies the "add" action to all graphs in batch (note: does # nothing if a graph did not sample "add") # then applies the "connect" action to all graphs in batch (note: does # nothing if a graph did not sample "connect")
self
add
Tuple[torch.Tensor, ...]
Indices for "add" actions sampled for batch
of graphs.
Indices for " add " actions sampled for batch of graphs .
conn
Tuple[torch.Tensor, ...]
Indices for "connect" actions sampled for
batch of graphs.
Indices for " connect " 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 .