class_docstr: The PGMExplainer model from the `"PGMExplainer: Probabilistic
  Graphical Model Explanations  for Graph Neural Networks"
  <https://arxiv.org/abs/1903.03894>`_ paper.
  
  The generated :class:`~torch_geometric.explain.Explanation` provides a
  :obj:`node_mask` and a :obj:`pgm_stats` tensor, which stores the
  :math:`p`-values of each node as calculated by the Chi-squared test.
  
  Args:
      feature_index (List): The indices of the perturbed features. If set
          to :obj:`None`, all features are perturbed. (default: :obj:`None`)
      perturb_mode (str, optional): The method to generate the variations in
          features. One of :obj:`"randint"`, :obj:`"mean"`, :obj:`"zero"`,
          :obj:`"max"` or :obj:`"uniform"`. (default: :obj:`"randint"`)
      perturbations_is_positive_only (bool, optional): If set to :obj:`True`,
          restrict perturbed values to be positive. (default: :obj:`False`)
      is_perturbation_scaled (bool, optional): If set to :obj:`True`, will
          normalize the range of the perturbed features.
          (default: :obj:`False`)
      num_samples (int, optional): The number of samples of perturbations
          used to test the significance of nodes to the prediction.
          (default: :obj:`100`)
      max_subgraph_size (int, optional): The maximum number of neighbors to
          consider for the explanation. (default: :obj:`None`)
      significance_threshold (float, optional): The statistical threshold
          (:math:`p`-value) for which a node is considered to have an effect
          on the prediction. (default: :obj:`0.05`)
      pred_threshold (float, optional): The buffer value (in range
          :obj:`[0, 1]`) to consider the output from a perturbed data to be
          different from the original. (default: :obj:`0.1`)
class_docstr_tok: ["The","PGMExplainer","model","from","the","`","``","PGMExplainer",":","Probabilistic","Graphical","Model","Explanations","for","Graph","Neural","Networks","''","<","https",":","\/\/arxiv.org\/abs\/1903.03894",">","`","_","paper",".","The","generated",":","class",":","`","~torch_geometric.explain.Explanation","`","provides","a",":","obj",":","`","node_mask","`","and","a",":","obj",":","`","pgm_stats","`","tensor",",","which","stores","the",":","math",":","`","p","`","-values","of","each","node","as","calculated","by","the","Chi-squared","test",".","Args",":","feature_index","(","List",")",":","The","indices","of","the","perturbed","features",".","If","set","to",":","obj",":","`","None","`",",","all","features","are","perturbed",".","(","default",":",":","obj",":","`","None","`",")","perturb_mode","(","str",",","optional",")",":","The","method","to","generate","the","variations","in","features",".","One","of",":","obj",":","`","``","randint","''","`",",",":","obj",":","`","``","mean","''","`",",",":","obj",":","`","``","zero","''","`",",",":","obj",":","`","``","max","''","`","or",":","obj",":","`","``","uniform","''","`",".","(","default",":",":","obj",":","`","``","randint","''","`",")","perturbations_is_positive_only","(","bool",",","optional",")",":","If","set","to",":","obj",":","`","True","`",",","restrict","perturbed","values","to","be","positive",".","(","default",":",":","obj",":","`","False","`",")","is_perturbation_scaled","(","bool",",","optional",")",":","If","set","to",":","obj",":","`","True","`",",","will","normalize","the","range","of","the","perturbed","features",".","(","default",":",":","obj",":","`","False","`",")","num_samples","(","int",",","optional",")",":","The","number","of","samples","of","perturbations","used","to","test","the","significance","of","nodes","to","the","prediction",".","(","default",":",":","obj",":","`","100","`",")","max_subgraph_size","(","int",",","optional",")",":","The","maximum","number","of","neighbors","to","consider","for","the","explanation",".","(","default",":",":","obj",":","`","None","`",")","significance_threshold","(","float",",","optional",")",":","The","statistical","threshold","(",":","math",":","`","p","`","-value",")","for","which","a","node","is","considered","to","have","an","effect","on","the","prediction",".","(","default",":",":","obj",":","`","0.05","`",")","pred_threshold","(","float",",","optional",")",":","The","buffer","value","(","in","range",":","obj",":","`","[","0",",","1","]","`",")","to","consider","the","output","from","a","perturbed","data","to","be","different","from","the","original",".","(","default",":",":","obj",":","`","0.1","`",")"]
docstr: Perturbs the node features of a batch of graphs for graph
  classification tasks.
  
  Args:
      model (torch.nn.Module): The GNN model.
      x (torch.Tensor): The node feature matrix
      edge_index (torch.Tensor): The edge indices.
      indices_to_perturb (np.array): The indices of nodes to perturb.
      percentage (float, optional): The percentage of times a node gets
          perturbed. (default: :obj:`50.`)
      **kwargs (optional): Additional arguments passed to
          :meth:`model.forward`.
docstr_tok: ["Perturbs","the","node","features","of","a","batch","of","graphs","for","graph","classification","tasks",".","Args",":","model","(","torch.nn.Module",")",":","The","GNN","model",".","x","(","torch.Tensor",")",":","The","node","feature","matrix","edge_index","(","torch.Tensor",")",":","The","edge","indices",".","indices_to_perturb","(","np.array",")",":","The","indices","of","nodes","to","perturb",".","percentage","(","float",",","optional",")",":","The","percentage","of","times","a","node","gets","perturbed",".","(","default",":",":","obj",":","`","50.","`",")","*","*","kwargs","(","optional",")",":","Additional","arguments","passed","to",":","meth",":","`","model.forward","`","."]
code: def _batch_perturb_features_on_node(
      self,
      model: torch.nn.Module,
      x: Tensor,
      edge_index: Tensor,
      indices_to_perturb: np.array,
      percentage: float = 50.0,  # % time node gets perturbed
      **kwargs,
  ) -> Tensor:
      r"""Perturbs the node features of a batch of graphs for graph
      classification tasks.
  
      Args:
          model (torch.nn.Module): The GNN model.
          x (torch.Tensor): The node feature matrix
          edge_index (torch.Tensor): The edge indices.
          indices_to_perturb (np.array): The indices of nodes to perturb.
          percentage (float, optional): The percentage of times a node gets
              perturbed. (default: :obj:`50.`)
          **kwargs (optional): Additional arguments passed to
              :meth:`model.forward`.
      """
      pred_torch = model(x, edge_index, **kwargs)
      soft_pred = torch.softmax(pred_torch, dim=1)
      pred_label = torch.argmax(soft_pred, dim=1)
      num_nodes = x.shape[0]
  
      samples = []
      for _ in range(self.num_samples):
          x_perturb = x.detach().clone()
  
          seeds = np.random.randint(
              0, 100, size=len(indices_to_perturb)
          )
          perturbed_node_indexes = indices_to_perturb[
              (seeds < percentage)
          ]
          x_perturb = self._perturb_features_on_nodes(
              x=x_perturb,
              index=perturbed_node_indexes,
          )
          sample = np.zeros(num_nodes + 1)
          sample[perturbed_node_indexes] = 1
  
          pred_perturb_torch = model(x_perturb, edge_index, **kwargs)
          soft_pred_perturb = torch.softmax(
              pred_perturb_torch, dim=1
          ).squeeze()
  
          pred_change = (
              torch.max(soft_pred) - soft_pred_perturb[pred_label]
          )
  
          sample[num_nodes] = pred_change
          samples.append(sample)
  
      samples = torch.tensor(np.array(samples))
      if self.perturbations_is_positive_only:
          samples = torch.abs(samples)
  
      top = int(self.num_samples / 8)
      top_idx = torch.argsort(samples[:, num_nodes])[-top:]
      for i in range(self.num_samples):
          if i in top_idx:
              samples[i, num_nodes] = 1
          else:
              samples[i, num_nodes] = 0
  
      return samples
  
file_path: pytorch_geometric/torch_geometric/contrib/explain/pgm_explainer.py
imports: import logging
  from typing import List, Optional, Tuple, Union
  import numpy
  import torch
  from torch import Tensor
  from torch_geometric.explain import ExplainerAlgorithm
  from torch_geometric.explain.config import ModelMode, ModelTaskLevel
  from torch_geometric.explain.explanation import Explanation
  from torch_geometric.utils import k_hop_subgraph
  from torch_geometric.utils._subgraph import get_num_hops
label_desc: Use image node_id 3 for calling the PGMExplainer obj's underlying member method code with example usage: obj._batch_perturb_features_on_node(model, x, edge_index, indices_to_perturb, percentage) and returns: samples