batch_generator Module
The batch_generator module provides high-performance vectorized protein structure generation optimized for deep learning and large-scale simulation.
Overview
Unlike the serial generator, batch_generator leverages NumPy's vectorized operations to build hundreds or thousands of structures in parallel. This approach is "ML-Ready" - producing contiguous tensors that can be passed directly to frameworks like MLX, PyTorch, or JAX.
Main Classes
BatchedGenerator
High-performance vectorized protein structure generator. Optimized for generating millions of labeled samples for AI training.
Source code in synth_pdb/batch_generator.py
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 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 | |
Functions
__init__(sequence_str, n_batch=1, full_atom=False)
Initialize the batched generator.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
sequence_str
|
str
|
The primary sequence (e.g. 'ACDEF' or 'ALA-CYS-ASP'). |
required |
n_batch
|
int
|
Number of structures to generate in a single vectorized pass. |
1
|
full_atom
|
bool
|
If True, generates all heavy atoms (Kabsch superimposition). |
False
|
Source code in synth_pdb/batch_generator.py
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 | |
generate_batch(seed=None, conformation='alpha', drift=0.0)
Generates B structures in parallel.
This method replaces the traditional per-residue loop with a "Batch Walk". Instead of placing atoms for structure 1, then structure 2... it places atom 'N' for ALL structures, then 'CA' for ALL structures, and so on.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
seed
|
Optional[int]
|
Random seed for reproducible batch generation. |
None
|
conformation
|
str
|
The secondary structure preset to use for all members. |
'alpha'
|
drift
|
float
|
Gaussian noise (std dev) in degrees. Use this to generate "hard decoys" that challenge AI models with near-native but slightly incorrect geometry. |
0.0
|
Source code in synth_pdb/batch_generator.py
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 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 | |
BatchedPeptide
A lightweight container for batched protein coordinates. Designed for high-performance handover to ML frameworks.
Source code in synth_pdb/batch_generator.py
56 57 58 59 60 61 62 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 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 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 | |
Functions
__init__(coords, sequence, atom_names, residue_indices)
Initialize the container.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
coords
|
ndarray
|
Coordinate tensor of shape (B, N_atoms, 3). |
required |
sequence
|
List[str]
|
List of residue types (3-letter codes). |
required |
atom_names
|
List[str]
|
List of atom names in the structure. |
required |
residue_indices
|
List[int]
|
Mapping from each atom to its residue (1-indexed). |
required |
Source code in synth_pdb/batch_generator.py
to_pdb(index=0)
Converts one structure in the batch to a PDB string.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
index
|
int
|
Batch index of the structure to convert. |
0
|
Returns:
| Type | Description |
|---|---|
str
|
The PDB content as a string. |
Source code in synth_pdb/batch_generator.py
save_pdb(path, index=0)
Saves one structure from the batch to a PDB file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
str
|
Target file path. |
required |
index
|
int
|
Batch index of the structure to save. |
0
|
Source code in synth_pdb/batch_generator.py
get_6d_orientations()
Computes 6D inter-residue orientations (trRosetta style).
Returns:
| Type | Description |
|---|---|
Dict[str, ndarray]
|
A dictionary of (B, L, L) tensors: - 'dist': Cb-Cb distance. - 'omega': Cb1-Ca1-Ca2-Cb2 torsion. - 'theta': N1-Ca1-Cb1-Cb2 torsion. - 'phi': Ca1-Cb1-Cb2 angle. |
Source code in synth_pdb/batch_generator.py
analyze_ensemble(superimpose=True)
Performs NMR-style ensemble analysis on the batch.
Calculates the average structure, the average RMSD to that structure (measuring batch precision), and identifies the medoid structure.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
superimpose
|
bool
|
If True, aligns all structures before analysis. |
True
|
Returns:
| Type | Description |
|---|---|
Dict[str, Any]
|
A dictionary containing: - 'avg_rmsd': The mean RMSD of all structures to the average. - 'medoid_index': The index of the most representative structure. - 'avg_coords': (N_atoms, 3) array of the centroid structure. |
Source code in synth_pdb/batch_generator.py
Usage Examples
Batched Structure Generation
Generate 100 alpha-helical structures in a single vectorized pass.
from synth_pdb.batch_generator import BatchedGenerator
# Create generator
gen = BatchedGenerator("ALA-GLY-SER-LEU-VAL", n_batch=100)
# Generate batch
batch = gen.generate_batch(conformation="alpha")
# Output: Batch coordinate tensor (100, N_atoms, 3)
coords = batch.coords
Ensemble Analysis
Perform NMR-style analysis on the generated batch to find the medoid structure and average RMSD.
analysis = batch.analyze_ensemble(superimpose=True)
print(f"Medoid index: {analysis['medoid_index']}")
print(f"Average RMSD: {analysis['avg_rmsd']:.2f} Å")
Exporting Orientograms
Extract 6D inter-residue orientations (distances, \(\omega, \theta, \phi\) torsions) for all pairs.
Educational Notes
Batched Generation (GPU-First)
Traditional generators process structures one-by-one. batch_generator uses Vectorized Math to:
1. Broadcasting: Using NumPy's broadcasting, a single mathematical expression calculates positions for all members of the batch simultaneously.
2. Hardware Acceleration: On modern architectures (like Apple Silicon M4), this leverages AMX/Accelerate units, often providing 10-100x speedups over Python loops.
The "Memory Wall" in AI Training
When generating millions of samples, the bottleneck is often the "Memory Wall":
- Latency: Copying large tensors from CPU to GPU memory can be slower than the math itself.
- Contiguity: Deep Learning models require contiguous memory. BatchedGenerator ensures the output is one massive C-style array, avoiding the overhead of "gather" operations on Python lists.
- Unified Memory: On unified memory architectures, the coordinate tensor can be "zero-copy" - generated by NumPy and immediately visible to the GPU without movement.
Peptidyl Chain Walk
The module implements a vectorized NeRF (Natural Extension Reference Frame) walk. It places atoms for ALL structures in the batch iteratively: 1. Place N for all members. 2. Place CA for all members using \(N(i), CA(i-1), C(i-1)\). 3. Place C for all members. 4. Place O for all members.
See Also
- generator Module - Serial structure generation
- dataset Module - Bulk dataset orchestration
- Scientific Background: NeRF Geometry