class_docstr: The method class for GraphEBM algorithm proposed in the paper `GraphEBM: Molecular Graph Generation with Energy-Based Models <https://arxiv.org/abs/2102.00546>`_. This class provides interfaces for running random generation, goal-directed generation (including property
  optimization and constrained optimization), and compositional generation with GraphEBM algorithm. Please refer to the `example codes <https://github.com/divelab/DIG/tree/dig-stable/examples/ggraph/GraphEBM>`_ for usage examples.
  
  Args:
      n_atom (int): Maximum number of atoms.
      n_atom_type (int): Number of possible atom types.
      n_edge_type (int): Number of possible bond types.
      hidden (int): Hidden dimensions.
      device (torch.device, optional): The device where the model is deployed.
class_docstr_tok: ["The","method","class","for","GraphEBM","algorithm","proposed","in","the","paper","`","GraphEBM",":","Molecular","Graph","Generation","with","Energy-Based","Models","<","https",":","\/\/arxiv.org\/abs\/2102.00546",">","`","_",".","This","class","provides","interfaces","for","running","random","generation",",","goal-directed","generation","(","including","property","optimization","and","constrained","optimization",")",",","and","compositional","generation","with","GraphEBM","algorithm",".","Please","refer","to","the","`","example","codes","<","https",":","\/\/github.com\/divelab\/DIG\/tree\/dig-stable\/examples\/ggraph\/GraphEBM",">","`","_","for","usage","examples",".","Args",":","n_atom","(","int",")",":","Maximum","number","of","atoms",".","n_atom_type","(","int",")",":","Number","of","possible","atom","types",".","n_edge_type","(","int",")",":","Number","of","possible","bond","types",".","hidden","(","int",")",":","Hidden","dimensions",".","device","(","torch.device",",","optional",")",":","The","device","where","the","model","is","deployed","."]
docstr: Running graph generation for compositional generation task.
  
  Args:
      checkpoint_path_qed (str): The path of the model trained on QED property, *i.e.*, the .pt file.
      checkpoint_path_plogp (str): The path of the model trained on plogp property, *i.e.*, the .pt file.
      n_samples (int): the number of molecules to generate.
      c (float): The scaling hyperparameter for dequantization.
      ld_step (int): The number of iteration steps of Langevin dynamics.
      ld_noise (float): The standard deviation of the added noise in Langevin dynamics.
      ld_step_size (int): The step size of Langevin dynamics.
      clamp (bool): Whether to use gradient clamp in Langevin dynamics.
      atomic_num_list (list): The list used to indicate atom types.
  
  :rtype:
      gen_mols (list): A list of generated molecules represented by rdkit Chem.Mol objects;
docstr_tok: ["Running","graph","generation","for","compositional","generation","task",".","Args",":","checkpoint_path_qed","(","str",")",":","The","path","of","the","model","trained","on","QED","property",",","*","i.e",".","*",",","the",".pt","file",".","checkpoint_path_plogp","(","str",")",":","The","path","of","the","model","trained","on","plogp","property",",","*","i.e",".","*",",","the",".pt","file",".","n_samples","(","int",")",":","the","number","of","molecules","to","generate",".","c","(","float",")",":","The","scaling","hyperparameter","for","dequantization",".","ld_step","(","int",")",":","The","number","of","iteration","steps","of","Langevin","dynamics",".","ld_noise","(","float",")",":","The","standard","deviation","of","the","added","noise","in","Langevin","dynamics",".","ld_step_size","(","int",")",":","The","step","size","of","Langevin","dynamics",".","clamp","(","bool",")",":","Whether","to","use","gradient","clamp","in","Langevin","dynamics",".","atomic_num_list","(","list",")",":","The","list","used","to","indicate","atom","types",".",":","rtype",":","gen_mols","(","list",")",":","A","list","of","generated","molecules","represented","by","rdkit","Chem.Mol","objects",";"]
code: def run_comp_gen(
      self,
      checkpoint_path_qed,
      checkpoint_path_plogp,
      n_samples,
      c,
      ld_step,
      ld_noise,
      ld_step_size,
      clamp,
      atomic_num_list,
  ):
      r"""
      Running graph generation for compositional generation task.
  
      Args:
          checkpoint_path_qed (str): The path of the model trained on QED property, *i.e.*, the .pt file.
          checkpoint_path_plogp (str): The path of the model trained on plogp property, *i.e.*, the .pt file.
          n_samples (int): the number of molecules to generate.
          c (float): The scaling hyperparameter for dequantization.
          ld_step (int): The number of iteration steps of Langevin dynamics.
          ld_noise (float): The standard deviation of the added noise in Langevin dynamics.
          ld_step_size (int): The step size of Langevin dynamics.
          clamp (bool): Whether to use gradient clamp in Langevin dynamics.
          atomic_num_list (list): The list used to indicate atom types.
  
      :rtype:
          gen_mols (list): A list of generated molecules represented by rdkit Chem.Mol objects;
      """
      model_qed = self.energy_function
      model_plogp = copy.deepcopy(self.energy_function)
      print("Loading paramaters from {}".format(checkpoint_path_qed))
      model_qed.load_state_dict(torch.load(checkpoint_path_qed))
      parameters_qed = model_qed.parameters()
      print("Loading paramaters from {}".format(checkpoint_path_plogp))
      model_plogp.load_state_dict(torch.load(checkpoint_path_plogp))
      parameters_plogp = model_plogp.parameters()
  
      ### Initialization
      print("Initializing samples...")
      gen_x = torch.rand(
          n_samples, self.n_atom_type, self.n_atom, device=self.device
      ) * (1 + c)
      gen_adj = torch.rand(
          n_samples,
          self.n_edge_type,
          self.n_atom,
          self.n_atom,
          device=self.device,
      )
  
      gen_x.requires_grad = True
      gen_adj.requires_grad = True
      requires_grad(parameters_qed, False)
      requires_grad(parameters_plogp, False)
      model_qed.eval()
      model_plogp.eval()
  
      noise_x = torch.randn_like(gen_x, device=self.device)
      noise_adj = torch.randn_like(gen_adj, device=self.device)
  
      ### Langevin dynamics
      print("Generating samples...")
      for _ in range(ld_step):
          noise_x.normal_(0, ld_noise)
          noise_adj.normal_(0, ld_noise)
          gen_x.data.add_(noise_x.data)
          gen_adj.data.add_(noise_adj.data)
  
          gen_out_qed = model_qed(gen_adj, gen_x)
          gen_out_plogp = model_plogp(gen_adj, gen_x)
          gen_out = 0.5 * gen_out_qed + 0.5 * gen_out_plogp
          gen_out.sum().backward()
          if clamp:
              gen_x.grad.data.clamp_(-0.01, 0.01)
              gen_adj.grad.data.clamp_(-0.01, 0.01)
  
          gen_x.data.add_(gen_x.grad.data, alpha=-ld_step_size)
          gen_adj.data.add_(gen_adj.grad.data, alpha=-ld_step_size)
  
          gen_x.grad.detach_()
          gen_x.grad.zero_()
          gen_adj.grad.detach_()
          gen_adj.grad.zero_()
  
          gen_x.data.clamp_(0, 1 + c)
          gen_adj.data.clamp_(0, 1)
  
      gen_x = gen_x.detach()
      gen_adj = gen_adj.detach()
      gen_adj = (gen_adj + gen_adj.permute(0, 1, 3, 2)) / 2
  
      gen_mols = gen_mol_from_one_shot_tensor(
          gen_adj, gen_x, atomic_num_list, correct_validity=True
      )
  
      return gen_mols
  
imports: import time
  import os
  import copy
  import torch
  from torch.optim import Adam
  from tqdm import tqdm
  from rdkit import Chem
  from dig.ggraph.method import Generator
  from dig.ggraph.utils import gen_mol_from_one_shot_tensor
  from dig.ggraph.utils import qed, calculate_min_plogp, reward_target_molecule_similarity
  from .energy_func import EnergyFunc
  from .util import rescale_adj, requires_grad, clip_grad
label_desc: Use image node_id 7 for calling the GraphEBM obj's underlying member method code with example usage: obj.run_comp_gen(checkpoint_path_qed, checkpoint_path_plogp, n_samples, c, ld_step, ld_noise, ld_step_size, clamp, atomic_num_list) and returns: gen_mols