Graph Augmentation Methods¶
This section covers the advanced graph augmentation techniques available in Augchem, designed specifically for molecular graphs using PyTorch Geometric.
Core Augmentation Functions¶
Edge Dropping¶
augchem.modules.graph.graphs_modules.edge_dropping(data: Data, drop_rate: float = 0.1) -> Data
¶
Remove complete bidirectional edges from the graph (edge dropping)
Parameters:
Name | Type | Description | Default |
---|---|---|---|
data
|
Data
|
torch_geometric graph |
required |
drop_rate
|
float
|
Bidirectional edge removal rate (0.0 to 1.0) |
0.1
|
Returns:
Type | Description |
---|---|
Data
|
Graph with edges removed |
Source code in augchem\modules\graph\graphs_modules.py
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
|
Node Dropping¶
augchem.modules.graph.graphs_modules.node_dropping(data: Data, drop_rate: float = 0.1) -> Data
¶
Remove nodes randomly from the graph (node dropping)
Parameters:
Name | Type | Description | Default |
---|---|---|---|
data
|
Data
|
torch_geometric graph |
required |
drop_rate
|
float
|
Node removal rate (0.0 to 1.0) |
0.1
|
Returns:
Type | Description |
---|---|
Data
|
Graph with nodes removed |
Source code in augchem\modules\graph\graphs_modules.py
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 |
|
Feature Masking¶
augchem.modules.graph.graphs_modules.feature_masking(data: Data, mask_rate: float = 0.1) -> Data
¶
Mask node features randomly (feature masking)
Parameters:
Name | Type | Description | Default |
---|---|---|---|
data
|
Data
|
torch_geometric graph |
required |
mask_rate
|
float
|
Feature masking rate (0.0 to 1.0) |
0.1
|
Returns:
Type | Description |
---|---|
Data
|
Graph with masked features |
Source code in augchem\modules\graph\graphs_modules.py
112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 |
|
Edge Perturbation¶
augchem.modules.graph.graphs_modules.edge_perturbation(data: Data, add_rate: float = 0.05, remove_rate: float = 0.05) -> Data
¶
Perturb the graph by adding and removing complete bidirectional edges (edge perturbation)
Parameters:
Name | Type | Description | Default |
---|---|---|---|
data
|
Data
|
torch_geometric graph |
required |
add_rate
|
float
|
Bidirectional connection addition rate |
0.05
|
remove_rate
|
float
|
Bidirectional connection removal rate |
0.05
|
Returns:
Type | Description |
---|---|
Data
|
Perturbed graph |
Source code in augchem\modules\graph\graphs_modules.py
137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 |
|
Dataset Augmentation¶
augchem.modules.graph.graphs_modules.augment_dataset(graphs: List[Data], augmentation_methods: List[str], edge_drop_rate: float = 0.1, node_drop_rate: float = 0.1, feature_mask_rate: float = 0.1, edge_add_rate: float = 0.05, edge_remove_rate: float = 0.05, augment_percentage: float = 0.2, seed: int = 42) -> List[Data]
¶
Apply data augmentation techniques to a list of graphs.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
graphs
|
List[Data]
|
List of torch_geometric Data objects representing the graphs |
required |
augmentation_methods
|
List[str]
|
List of methods ['edge_drop', 'node_drop', 'feature_mask', 'edge_perturb'] |
required |
edge_drop_rate
|
float
|
Rate of edge removal (0.0 to 1.0) |
0.1
|
node_drop_rate
|
float
|
Rate of node removal (0.0 to 1.0) |
0.1
|
feature_mask_rate
|
float
|
Rate of feature masking (0.0 to 1.0) |
0.1
|
edge_add_rate
|
float
|
Rate of edge addition for perturbation |
0.05
|
edge_remove_rate
|
float
|
Rate of edge removal for perturbation |
0.05
|
augment_percentage
|
float
|
Size of the augmented dataset as a fraction of the original |
0.2
|
seed
|
int
|
Seed for reproducibility |
42
|
Returns:
Type | Description |
---|---|
List[Data]
|
List of augmented graphs (original + augmented) |
Raises:
Type | Description |
---|---|
ValueError
|
If unknown augmentation methods are specified |
Source code in augchem\modules\graph\graphs_modules.py
195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 |
|
Usage Examples¶
Basic Graph Augmentation¶
from augchem.modules.graph.graphs_modules import augment_dataset
import torch
from torch_geometric.data import Data
# Example: Create sample molecular graphs
graphs = [
Data(x=torch.randn(10, 5), edge_index=torch.randint(0, 10, (2, 20))),
Data(x=torch.randn(8, 5), edge_index=torch.randint(0, 8, (2, 16)))
]
# Apply multiple augmentation techniques
augmented_graphs = augment_dataset(
graphs=graphs,
augmentation_methods=['edge_drop', 'node_drop', 'feature_mask', 'edge_perturb'],
edge_drop_rate=0.1,
node_drop_rate=0.1,
feature_mask_rate=0.15,
edge_add_rate=0.05,
edge_remove_rate=0.05,
augment_percentage=0.3,
seed=42
)
print(f"Original: {len(graphs)} graphs")
print(f"Augmented: {len(augmented_graphs)} graphs")
Individual Augmentation Techniques¶
from augchem.modules.graph.graphs_modules import (
edge_dropping, node_dropping, feature_masking, edge_perturbation
)
# Apply individual techniques
graph = your_molecular_graph
# Edge dropping - removes bidirectional connections
graph_edge_drop = edge_dropping(graph, drop_rate=0.1)
# Node dropping - removes nodes and their connections
graph_node_drop = node_dropping(graph, drop_rate=0.1)
# Feature masking - masks node features with -inf
graph_feature_mask = feature_masking(graph, mask_rate=0.15)
# Edge perturbation - adds and removes edges
graph_perturbed = edge_perturbation(
graph,
add_rate=0.05,
remove_rate=0.05
)
Working with PyTorch Geometric DataLoaders¶
from torch_geometric.loader import DataLoader
# Create DataLoader with augmented graphs
dataloader = DataLoader(
augmented_graphs,
batch_size=32,
shuffle=True
)
# Process batches
for batch in dataloader:
print(f"Batch size: {batch.num_graphs}")
print(f"Total nodes: {batch.x.size(0)}")
print(f"Total edges: {batch.edge_index.size(1)}")
break
Technical Notes¶
Graph Integrity¶
- All augmentation functions preserve graph structure validity
- Node indices are properly remapped after node dropping
- Edge attributes are handled consistently across operations
Bidirectional Edges¶
- Edge dropping and perturbation work with complete bidirectional edges
- This ensures molecular graph connectivity is maintained properly
- Single-direction edge operations would break chemical bond representation
Feature Masking¶
- Uses
-inf
as mask value for compatibility with attention mechanisms - Masked features can be easily identified and handled in downstream models
- Preserves tensor shapes for batch processing
Reproducibility¶
- All augmentation functions support random seed control
- Deterministic results for the same input parameters and seed
- Essential for experimental reproducibility in research
Memory Efficiency¶
- All functions create cloned graphs to preserve originals
- Efficient tensor operations using PyTorch primitives
- Batch processing optimized for GPU acceleration