deepsnap.batch

Contents

DeepSNAP Batch

class Batch(batch=None, **kwargs)[source]

Bases: deepsnap.graph.Graph

A plain old python object modeling a batch of deepsnap.graph.Graph objects as one big (disconnected) graph, with torch_geometric.data.Data being the base class that all its methods can also be used here. In addition, graphs can be reconstructed via the assignment vector batch, which maps each node to its respective graph identifier.

Note

For more detailed use of deepsnap.batch.Batch, see the examples folder.

apply_transform(transform, update_tensor: bool = True, update_graph: bool = False, deep_copy: bool = False, **kwargs)[source]

Applies a transformation to each graph object in parallel by first calling to_data_list, applying the transform, and then perform re-batching again to a Batch. A transform should edit the graph object, including changing the graph structure, or adding node / edge / graph level attributes. The rest are automatically handled by the deepsnap.graph.Graph object, including everything ended with index.

Parameters
  • transform (callable) – Transformation function applied to each graph object.

  • update_tensor (bool) – Whether use nx graph to update tensor attributes.

  • update_graph (bool) – Whether use tensor attributes to update nx graphs.

  • deep_copy (bool) – True if a new deep copy of batch is returned. This option allows modifying the batch of graphs without changing the graphs in the original dataset.

  • kwargs – Parameters used in the transform function for each deepsnap.graph.Graph.

Returns

A batch object containing all transformed graph objects.

apply_transform_multi(transform, update_tensors: bool = True, update_graphs: bool = False, deep_copy: bool = False, **kwargs)[source]

Compared to apply_transform(), this allows multiple graph objects to be returned by the given transform function.

Parameters
  • transform (callable) – (Multiple return value) tranformation function applied to each graph object. It needs to return a tuple of Graph objects.

  • update_tensors (bool) – Whether use nx graph to update tensor attributes.

  • update_graphs (bool) – Whether use tensor attributes to update nx graphs.

  • deep_copy (bool) – True if a new deep copy of batch is returned. This option allows modifying the batch of graphs without changing the graphs in the original dataset.

  • kwargs – Parameters used in the transform function for each deepsnap.graph.Graph.

Returns

A tuple of batch objects. The i-th batch object contains the i-th return value of the transform function applied to all graphs in the batch.

static from_data_list(data_list: List[deepsnap.graph.Graph], follow_batch: Optional[List] = None, transform: Optional[Callable] = None, **kwargs)[source]

Constructs A deepsnap.batch.Batch object from a python list holding torch_geometric.data.Data objects. The assignment vector batch is created on the fly. Additionally, creates assignment batch vectors for each key in follow_batch.

Parameters
  • data_list (list) – A list of deepsnap.graph.Graph objects.

  • follow_batch (list) – Creates assignment batch vectors for each key.

  • transform (callable) – If it is not None, apply transform when batching.

  • **kwargs – Other parameters.

property num_graphs

Returns the number of graphs in the batch.

Returns

The number of graphs in the batch.

Return type

int

to_data_list()[source]

Reconstructs the list of torch_geometric.data.Data objects from the batch object. The batch object must have been created via from_data_list() in order to be able to reconstruct the initial objects.