# deeprobust.graph.data package

## Submodules

## deeprobust.graph.data.attacked_data module

class `PrePtbDataset`(root, name, attack_method='meta', ptb_rate=0.05)

  Dataset class manages pre-attacked adjacency matrix on different datasets. Note metattack is generated by deeprobust/graph/global_attack/metattack.py. While PrePtbDataset provides pre-attacked graph generate by Zugner, https://github.com/danielzuegner/gnn-meta-attack. The attacked graphs are downloaded from https://github.com/ChandlerBang/Pro-GNN/tree/master/meta.

  Parameters

    - root – root directory where the dataset should be saved.

    - name – dataset name. It can be choosen from [‘cora’, ‘citeseer’, ‘polblogs’, ‘pubmed’]

    - attack_method – currently this class only support metattack and nettack. Note ‘meta’, ‘metattack’ or ‘mettack’ will be interpreted as the same attack.

    - seed – random seed for splitting training/validation/test.

  Examples

      >>> from deeprobust.graph.data import Dataset, PrePtbDataset
      >>> data = Dataset(root='/tmp/', name='cora')
      >>> adj, features, labels = data.adj, data.features, data.labels
      >>> # Load meta attacked data
      >>> perturbed_data = PrePtbDataset(root='/tmp/',
                              name='cora',
                              attack_method='meta',
                              ptb_rate=0.05)
      >>> perturbed_adj = perturbed_data.adj
      >>> # Load nettacked data
      >>> perturbed_data = PrePtbDataset(root='/tmp/',
                              name='cora',
                              attack_method='nettack',
                              ptb_rate=1.0)
      >>> perturbed_adj = perturbed_data.adj
      >>> target_nodes = perturbed_data.target_nodes

  `get_target_nodes`()

    Get target nodes incides, which is the nodes with degree > 10 in the test set.

class `PtbDataset`(root, name, attack_method='mettack')

  Dataset class manages pre-attacked adjacency matrix on different datasets. Currently only support metattack under 5% perturbation. Note metattack is generated by deeprobust/graph/global_attack/metattack.py. While PrePtbDataset provides pre-attacked graph generate by Zugner, https://github.com/danielzuegner/gnn-meta-attack. The attacked graphs are downloaded from https://github.com/ChandlerBang/pytorch-gnn-meta-attack/tree/master/pre-attacked.

  Parameters

    - root – root directory where the dataset should be saved.

    - name – dataset name. It can be choosen from [‘cora’, ‘citeseer’, ‘cora_ml’, ‘polblogs’, ‘pubmed’]

    - attack_method – currently this class only support metattack. User can pass ‘meta’, ‘metattack’ or ‘mettack’ since all of them will be interpreted as the same attack.

    - seed – random seed for splitting training/validation/test.

  Examples

      >>> from deeprobust.graph.data import Dataset, PtbDataset
      >>> data = Dataset(root='/tmp/', name='cora')
      >>> adj, features, labels = data.adj, data.features, data.labels
      >>> perturbed_data = PtbDataset(root='/tmp/',
                              name='cora',
                              attack_method='meta')
      >>> perturbed_adj = perturbed_data.adj

## deeprobust.graph.data.dataset module

class `Dataset`(root, name, setting='nettack', seed=None, require_mask=False)

  Dataset class contains four citation network datasets “cora”, “cora-ml”, “citeseer” and “pubmed”, and one blog dataset “Polblogs”. Datasets “ACM”, “BlogCatalog”, “Flickr”, “UAI”, “Flickr” are also available. See more details in https://github.com/DSE-MSU/DeepRobust/tree/master/deeprobust/graph#supported-datasets. The ‘cora’, ‘cora-ml’, ‘polblogs’ and ‘citeseer’ are downloaded from https://github.com/danielzuegner/gnn-meta-attack/tree/master/data, and ‘pubmed’ is from https://github.com/tkipf/gcn/tree/master/gcn/data.

  Parameters

    - root (string) – root directory where the dataset should be saved.

    - name (string) – dataset name, it can be chosen from [‘cora’, ‘citeseer’, ‘cora_ml’, ‘polblogs’, ‘pubmed’, ‘acm’, ‘blogcatalog’, ‘uai’, ‘flickr’]

    - setting (string) – there are two data splits settings. It can be chosen from [‘nettack’, ‘gcn’, ‘prognn’] The ‘nettack’ setting follows nettack paper where they select the largest connected components of the graph and use 10%/10%/80% nodes for training/validation/test . The ‘gcn’ setting follows gcn paper where they use the full graph and 20 samples in each class for traing, 500 nodes for validation, and 1000 nodes for test. (Note here ‘netack’ and ‘gcn’ setting do not provide fixed split, i.e., different random seed would return different data splits)

    - seed (int) – random seed for splitting training/validation/test.

    - require_mask (bool) – setting require_mask True to get training, validation and test mask (self.train_mask, self.val_mask, self.test_mask)

  Examples

  We can first create an instance of the Dataset class and then take out its attributes.

      >>> from deeprobust.graph.data import Dataset
      >>> data = Dataset(root='/tmp/', name='cora', seed=15)
      >>> adj, features, labels = data.adj, data.features, data.labels
      >>> idx_train, idx_val, idx_test = data.idx_train, data.idx_val, data.idx_test

  `download_npz`()

    Download adjacen matrix npz file from self.url.

  `get_prognn_splits`()

    Get target nodes incides, which is the nodes with degree > 10 in the test set.

  `get_train_val_test`()

    Get training, validation, test splits according to self.setting (either ‘nettack’ or ‘gcn’).

  `largest_connected_components`(adj, n_components=1)

    Select k largest connected components.

    Parameters

      - adj (scipy.sparse.csr_matrix) – input adjacency matrix

      - n_components (int) – n largest connected components we want to select

## Module contents

class `Dataset`(root, name, setting='nettack', seed=None, require_mask=False)

  Dataset class contains four citation network datasets “cora”, “cora-ml”, “citeseer” and “pubmed”, and one blog dataset “Polblogs”. Datasets “ACM”, “BlogCatalog”, “Flickr”, “UAI”, “Flickr” are also available. See more details in https://github.com/DSE-MSU/DeepRobust/tree/master/deeprobust/graph#supported-datasets. The ‘cora’, ‘cora-ml’, ‘polblogs’ and ‘citeseer’ are downloaded from https://github.com/danielzuegner/gnn-meta-attack/tree/master/data, and ‘pubmed’ is from https://github.com/tkipf/gcn/tree/master/gcn/data.

  Parameters

    - root (string) – root directory where the dataset should be saved.

    - name (string) – dataset name, it can be chosen from [‘cora’, ‘citeseer’, ‘cora_ml’, ‘polblogs’, ‘pubmed’, ‘acm’, ‘blogcatalog’, ‘uai’, ‘flickr’]

    - setting (string) – there are two data splits settings. It can be chosen from [‘nettack’, ‘gcn’, ‘prognn’] The ‘nettack’ setting follows nettack paper where they select the largest connected components of the graph and use 10%/10%/80% nodes for training/validation/test . The ‘gcn’ setting follows gcn paper where they use the full graph and 20 samples in each class for traing, 500 nodes for validation, and 1000 nodes for test. (Note here ‘netack’ and ‘gcn’ setting do not provide fixed split, i.e., different random seed would return different data splits)

    - seed (int) – random seed for splitting training/validation/test.

    - require_mask (bool) – setting require_mask True to get training, validation and test mask (self.train_mask, self.val_mask, self.test_mask)

  Examples

  We can first create an instance of the Dataset class and then take out its attributes.

      >>> from deeprobust.graph.data import Dataset
      >>> data = Dataset(root='/tmp/', name='cora', seed=15)
      >>> adj, features, labels = data.adj, data.features, data.labels
      >>> idx_train, idx_val, idx_test = data.idx_train, data.idx_val, data.idx_test

  `download_npz`()

    Download adjacen matrix npz file from self.url.

  `get_prognn_splits`()

    Get target nodes incides, which is the nodes with degree > 10 in the test set.

  `get_train_val_test`()

    Get training, validation, test splits according to self.setting (either ‘nettack’ or ‘gcn’).

  `largest_connected_components`(adj, n_components=1)

    Select k largest connected components.

    Parameters

      - adj (scipy.sparse.csr_matrix) – input adjacency matrix

      - n_components (int) – n largest connected components we want to select

class `PtbDataset`(root, name, attack_method='mettack')

  Dataset class manages pre-attacked adjacency matrix on different datasets. Currently only support metattack under 5% perturbation. Note metattack is generated by deeprobust/graph/global_attack/metattack.py. While PrePtbDataset provides pre-attacked graph generate by Zugner, https://github.com/danielzuegner/gnn-meta-attack. The attacked graphs are downloaded from https://github.com/ChandlerBang/pytorch-gnn-meta-attack/tree/master/pre-attacked.

  Parameters

    - root – root directory where the dataset should be saved.

    - name – dataset name. It can be choosen from [‘cora’, ‘citeseer’, ‘cora_ml’, ‘polblogs’, ‘pubmed’]

    - attack_method – currently this class only support metattack. User can pass ‘meta’, ‘metattack’ or ‘mettack’ since all of them will be interpreted as the same attack.

    - seed – random seed for splitting training/validation/test.

  Examples

      >>> from deeprobust.graph.data import Dataset, PtbDataset
      >>> data = Dataset(root='/tmp/', name='cora')
      >>> adj, features, labels = data.adj, data.features, data.labels
      >>> perturbed_data = PtbDataset(root='/tmp/',
                              name='cora',
                              attack_method='meta')
      >>> perturbed_adj = perturbed_data.adj

class `PrePtbDataset`(root, name, attack_method='meta', ptb_rate=0.05)

  Dataset class manages pre-attacked adjacency matrix on different datasets. Note metattack is generated by deeprobust/graph/global_attack/metattack.py. While PrePtbDataset provides pre-attacked graph generate by Zugner, https://github.com/danielzuegner/gnn-meta-attack. The attacked graphs are downloaded from https://github.com/ChandlerBang/Pro-GNN/tree/master/meta.

  Parameters

    - root – root directory where the dataset should be saved.

    - name – dataset name. It can be choosen from [‘cora’, ‘citeseer’, ‘polblogs’, ‘pubmed’]

    - attack_method – currently this class only support metattack and nettack. Note ‘meta’, ‘metattack’ or ‘mettack’ will be interpreted as the same attack.

    - seed – random seed for splitting training/validation/test.

  Examples

      >>> from deeprobust.graph.data import Dataset, PrePtbDataset
      >>> data = Dataset(root='/tmp/', name='cora')
      >>> adj, features, labels = data.adj, data.features, data.labels
      >>> # Load meta attacked data
      >>> perturbed_data = PrePtbDataset(root='/tmp/',
                              name='cora',
                              attack_method='meta',
                              ptb_rate=0.05)
      >>> perturbed_adj = perturbed_data.adj
      >>> # Load nettacked data
      >>> perturbed_data = PrePtbDataset(root='/tmp/',
                              name='cora',
                              attack_method='nettack',
                              ptb_rate=1.0)
      >>> perturbed_adj = perturbed_data.adj
      >>> target_nodes = perturbed_data.target_nodes

  `get_target_nodes`()

    Get target nodes incides, which is the nodes with degree > 10 in the test set.

class `Pyg2Dpr`(pyg_data, **kwargs)

  Convert pytorch geometric data (tensor, edge_index) to deeprobust data (sparse matrix)

  Parameters

    pyg_data – data instance of class from pytorch geometric dataset

  Examples

  We can first create an instance of the Dataset class and convert it to pytorch geometric data format and then convert it back to Dataset class.

      >>> from deeprobust.graph.data import Dataset, Dpr2Pyg, Pyg2Dpr
      >>> data = Dataset(root='/tmp/', name='cora')
      >>> pyg_data = Dpr2Pyg(data)
      >>> print(pyg_data)
      >>> print(pyg_data[0])
      >>> dpr_data = Pyg2Dpr(pyg_data)
      >>> print(dpr_data.adj)

class `Dpr2Pyg`(dpr_data, transform=None, **kwargs)

  Convert deeprobust data (sparse matrix) to pytorch geometric data (tensor, edge_index)

  Parameters

    - dpr_data – data instance of class from deeprobust.graph.data, e.g., deeprobust.graph.data.Dataset, deeprobust.graph.data.PtbDataset, deeprobust.graph.data.PrePtbDataset

    - transform – A function/transform that takes in an object and returns a transformed version. The data object will be transformed before every access. For example, you can use torch_geometric.transforms.NormalizeFeatures()

  Examples

  We can first create an instance of the Dataset class and convert it to pytorch geometric data format.

      >>> from deeprobust.graph.data import Dataset, Dpr2Pyg
      >>> data = Dataset(root='/tmp/', name='cora')
      >>> pyg_data = Dpr2Pyg(data)
      >>> print(pyg_data)
      >>> print(pyg_data[0])

  `update_edge_index`(adj)

    This is an inplace operation to substitute the original edge_index with adj.nonzero()

    Parameters

      adj (sp.csr_matrix) – update the original adjacency into adj (by change edge_index)

class `AmazonPyg`(root, name, transform=None, pre_transform=None, **kwargs)

  Amazon-Computers and Amazon-Photo datasets loaded from pytorch geomtric; the way we split the dataset follows Towards Deeper Graph Neural Networks (https://github.com/mengliu1998/DeeperGNN/blob/master/DeeperGNN/train_eval.py). Specifically, 20 * num_classes labels for training, 30 * num_classes labels for validation, rest labels for testing.

  Parameters

    - root (string) – root directory where the dataset should be saved.

    - name (string) – dataset name, it can be choosen from [‘computers’, ‘photo’]

    - transform – A function/transform that takes in an torch_geometric.data.Data object and returns a transformed version. The data object will be transformed before every access. (default: None)

    - pre_transform – A function/transform that takes in an torch_geometric.data.Data object and returns a transformed version. The data object will be transformed before being saved to disk.

  Examples

  We can directly load Amazon dataset from deeprobust in the format of pyg.

      >>> from deeprobust.graph.data import AmazonPyg
      >>> computers = AmazonPyg(root='/tmp', name='computers')
      >>> print(computers)
      >>> print(computers[0])
      >>> photo = AmazonPyg(root='/tmp', name='photo')
      >>> print(photo)
      >>> print(photo[0])

class `CoauthorPyg`(root, name, transform=None, pre_transform=None, **kwargs)

  Coauthor-CS and Coauthor-Physics datasets loaded from pytorch geomtric; the way we split the dataset follows Towards Deeper Graph Neural Networks (https://github.com/mengliu1998/DeeperGNN/blob/master/DeeperGNN/train_eval.py). Specifically, 20 * num_classes labels for training, 30 * num_classes labels for validation, rest labels for testing.

  Parameters

    - root (string) – root directory where the dataset should be saved.

    - name (string) – dataset name, it can be choosen from [‘cs’, ‘physics’]

    - transform – A function/transform that takes in an torch_geometric.data.Data object and returns a transformed version. The data object will be transformed before every access. (default: None)

    - pre_transform – A function/transform that takes in an torch_geometric.data.Data object and returns a transformed version. The data object will be transformed before being saved to disk.

  Examples

  We can directly load Coauthor dataset from deeprobust in the format of pyg.

      >>> from deeprobust.graph.data import CoauthorPyg
      >>> cs = CoauthorPyg(root='/tmp', name='cs')
      >>> print(cs)
      >>> print(cs[0])
      >>> physics = CoauthorPyg(root='/tmp', name='physics')
      >>> print(physics)
      >>> print(physics[0])