-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathutils.py
More file actions
528 lines (453 loc) · 21.2 KB
/
utils.py
File metadata and controls
528 lines (453 loc) · 21.2 KB
1
2
3
4
5
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
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
194
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
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
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
import torch
import numpy as np
import matplotlib.pyplot as plt
class Coef:
def __init__(self, coef, a=1.0, b=0.001):
self.coef = coef
self.a = a
self.b = b
def t(self, t):
if self.coef == 't':
return t
elif self.coef == '1-t':
return 1-t
elif self.coef == 'cos(pi*t/2)':
return torch.cos( 1/2 * np.pi * t)
elif self.coef == 'sin(pi*t/2)':
return torch.sin( 1/2 * np.pi * t)
elif self.coef == 'sqrt(a*t*(1-t))':
return torch.sqrt(self.a*t*(1-t))
elif self.coef == 'sqrt(a*t*(1-t))+b':
return torch.sqrt(self.a*t*(1-t)) + self.b
elif self.coef == 'a*t*(1-t)':
return self.a*t*(1-t)
elif self.coef == 'a*sin(pi*t)':
return self.a*torch.sin(np.pi*t)
else:
return torch.zeros_like(t) + float(self.coef)
def dt(self, t):
if self.coef == 't':
return torch.ones_like(t)
elif self.coef == '1-t':
return -torch.ones_like(t)
elif self.coef == 'cos(pi*t/2)':
return -torch.sin( 1/2 * np.pi * t) * 1/2 * np.pi
elif self.coef == 'sin(pi*t/2)':
return torch.cos( 1/2 * np.pi * t) * 1/2 * np.pi
elif self.coef == 'sqrt(a*t*(1-t))':
return 1 / 2 / torch.sqrt(t*(1-t)) * self.a**0.5 * (1 - 2*t)
elif self.coef == 'sqrt(a*t*(1-t))+b':
return 1 / 2 / torch.sqrt(t*(1-t)) * self.a**0.5 * (1 - 2*t)
elif self.coef == 'a*t*(1-t)':
return self.a - 2 * t * self.a
elif self.coef == 'a*sin(pi*t)':
return self.a * np.pi * torch.cos(np.pi*t)
else:
return torch.zeros_like(t)
def tdt(self, t):
if self.coef == 'sqrt(a*t*(1-t))':
return 1/2 * self.a * (1 - 2*t)
else:
return self.t(t) * self.dt(t)
def get_target(cfg):
if cfg.target.name == "gmm":
try:
if cfg.train.deepbar == True:
from energy.gmm import GMM
from energy.gaussian import Gaussian
target1 = GMM(dim=cfg.target.dim,
n_mixes=cfg.target.gmm1.num_gaussian,
loc_scaling=cfg.target.gmm1.loc,
log_var_scaling=cfg.target.gmm1.scale,
mean_shift=cfg.target.gmm1.shift,
seed=cfg.target.gmm1.seed,
device=cfg.device)
target2 = GMM(dim=cfg.target.dim,
n_mixes=cfg.target.gmm2.num_gaussian,
loc_scaling=cfg.target.gmm2.loc,
log_var_scaling=cfg.target.gmm2.scale,
mean_shift=cfg.target.gmm2.shift,
seed=cfg.target.gmm2.seed,
device=cfg.device)
# make one to Gaussian
if cfg.train.deepbar_target == 1:
target2 = Gaussian(dim=cfg.target.dim, device=cfg.device)
elif cfg.train.deepbar_target == 2:
target1 = Gaussian(dim=cfg.target.dim, device=cfg.device)
print("Using GMM with deepbar")
return target1, target2
except:
pass
from energy.gmm import GMM
target1 = GMM(dim=cfg.target.dim,
n_mixes=cfg.target.gmm1.num_gaussian,
loc_scaling=cfg.target.gmm1.loc,
log_var_scaling=cfg.target.gmm1.scale,
mean_shift=cfg.target.gmm1.shift,
seed=cfg.target.gmm1.seed,
device=cfg.device)
target2 = GMM(dim=cfg.target.dim,
n_mixes=cfg.target.gmm2.num_gaussian,
loc_scaling=cfg.target.gmm2.loc,
log_var_scaling=cfg.target.gmm2.scale,
mean_shift=cfg.target.gmm2.shift,
seed=cfg.target.gmm2.seed,
device=cfg.device)
return target1, target2
elif cfg.target.name == "lj":
from energy.lj import LennardJonesPotential
try:
score_path = cfg.target.lj1.score_path
except:
score_path = None
try:
rescaling = cfg.target.lj1.rescaling
except:
rescaling = 1
print('Scaling:', rescaling)
target1 = LennardJonesPotential(dim=cfg.target.dim,
n_particles=cfg.target.lj1.n_particles,
two_event_dims=False,
device=cfg.device,
eps=cfg.target.lj1.scale,
rm=cfg.target.lj1.scale,
sample_path=cfg.target.lj1.sample_path,
score_path=score_path,
rescaling=rescaling)
try:
score_path = cfg.target.lj2.score_path
except:
score_path = None
try:
rescaling = cfg.target.lj2.rescaling
except:
rescaling = 1
print('Scaling:', rescaling)
target2 = LennardJonesPotential(dim=cfg.target.dim,
n_particles=cfg.target.lj2.n_particles,
two_event_dims=False,
device=cfg.device,
eps=cfg.target.lj2.scale,
rm=cfg.target.lj2.scale,
sample_path=cfg.target.lj2.sample_path,
score_path=score_path,
rescaling=rescaling)
return target1, target2
elif cfg.target.name == "aldp":
from energy.aldp import AldpBoltzmann
try:
score_path = cfg.target.aldp1.score_path
except:
score_path = None
target1 = AldpBoltzmann(temperature=cfg.target.aldp1.temperature,
env=cfg.target.aldp1.env,
n_threads=cfg.target.aldp1.n_threads,
sample_path=cfg.target.aldp1.sample_path,
device=cfg.device,
score_path=score_path,
lamb=cfg.target.aldp1.lambd,
scaling=cfg.target.aldp1.scaling)
try:
score_path = cfg.target.aldp2.score_path
except:
score_path = None
target2 = AldpBoltzmann(temperature=cfg.target.aldp2.temperature,
env=cfg.target.aldp2.env,
n_threads=cfg.target.aldp2.n_threads,
sample_path=cfg.target.aldp2.sample_path,
device=cfg.device,
score_path=score_path,
lamb=cfg.target.aldp2.lambd,
scaling=cfg.target.aldp2.scaling)
try:
if cfg.train.deepbar == True:
from energy.gaussian import Gaussian_zero_center
# make one to Gaussian
if cfg.train.deepbar_target == 1:
target2 = Gaussian_zero_center(3, 22, device=cfg.device)
elif cfg.train.deepbar_target == 2:
target1 = Gaussian_zero_center(3, 22, device=cfg.device)
print("ALDP with deepbar")
# return target1, target2
except:
pass
return target1, target2
elif cfg.target.name == "phi4":
from energy.phi4 import phi4
target1 = phi4(positive=cfg.target.phi4_1.positive,
sample_path=cfg.target.phi4_1.sample_path,
device=cfg.device,)
target2 = phi4(positive=cfg.target.phi4_2.positive,
sample_path=cfg.target.phi4_2.sample_path,
device=cfg.device,)
return target1, target2
def get_sampler_from_samples(cfg, x1, x2):
if cfg.train.OT_pair:
from torchcfm.optimal_transport import OTPlanSampler
def sample(n):
x1_shuffled, x2_shuffled = x1[torch.randperm(x1.shape[0])], x2[torch.randperm(x2.shape[0])]
x1_subset, x2_subset = x1_shuffled[:n], x2_shuffled[:n]
if cfg.train.OT_pair:
sampler = OTPlanSampler(method="exact")
x1_subset, x2_subset = sampler.sample_plan(x1_subset, x2_subset)
return x1_subset, x2_subset
return sample
def get_sampler_from_dataset(cfg, dataset1, dataset2):
if cfg.train.OT_pair:
from torchcfm.optimal_transport import OTPlanSampler
def sample(n):
x1, x2 = next(iter(dataset1)), next(iter(dataset2))
return x1, x2
# TODO: OT sampler not supported yet
return sample
def get_sampler_from_target(cfg, target1, target2):
if cfg.train.OT_pair:
from torchcfm.optimal_transport import OTPlanSampler
def sample(n, use_ot=True):
try:
ot_batch = cfg.train.OT_batch
except:
ot_batch = n
x1 = target1.sample((ot_batch, ))
x2 = target2.sample((ot_batch, ))
if cfg.train.OT_pair and use_ot:
sampler = OTPlanSampler(method="exact")
x1, x2 = sampler.sample_plan(x1, x2)
return x1[:n], x2[:n]
return sample
def get_sampler_with_grad_from_target(cfg, target1, target2):
if cfg.train.OT_pair:
from torchcfm.optimal_transport import OTPlanSampler
def sample(n, use_ot=True):
try:
ot_batch = cfg.train.OT_batch
except:
ot_batch = n
if use_ot == False:
ot_batch = n
x1, score1 = target1.get_sample_and_score((ot_batch, ))
x2, score2 = target2.get_sample_and_score((ot_batch, ))
if cfg.train.OT_pair and use_ot:
sampler = OTPlanSampler(method="exact")
pi = sampler.get_map(x1, x2)
i, j = sampler.sample_map(pi, x1.shape[0])
return x1[i][:n], x2[j][:n], score1[i][:n], score2[j][:n]
return x1[:n], x2[:n], score1[:n], score2[:n]
return sample
def get_sampler_with_grad_fix_order(cfg, target1, target2):
if cfg.train.OT_pair:
from torchcfm.optimal_transport import OTPlanSampler
def sample(n, use_ot=True):
try:
ot_batch = cfg.train.OT_batch
except:
ot_batch = n
idx = np.random.choice(target1.sample_data.shape[0], size=ot_batch, replace=False)
x1, score1 = target1.get_sample_and_score_idx(idx)
x2, score2 = target2.get_sample_and_score_idx(idx)
if cfg.train.OT_pair and use_ot:
sampler = OTPlanSampler(method="exact")
pi = sampler.get_map(x1, x2)
i, j = sampler.sample_map(pi, x1.shape[0])
return x1[i][:n], x2[j][:n], score1[i][:n], score2[j][:n]
return x1[:n], x2[:n], score1[:n], score2[:n]
return sample
def get_marginal_plot_fn(cfg):
if cfg.target.name == "gmm":
def evaluate(x1, x2, x1_hat, x2_hat, save_path, *args, **kwargs):
# Create subplots
fig, axes = plt.subplots(1, 2, figsize=(6.5, 3))
axes[0].scatter(x1[:, 0], x1[:, 1], s=1, alpha=0.5)
axes[0].scatter(x1_hat[:, 0], x1_hat[:, 1], s=1, alpha=0.5)
axes[1].scatter(x2[:, 0], x2[:, 1], s=1, alpha=0.5)
axes[1].scatter(x2_hat[:, 0], x2_hat[:, 1], s=1, alpha=0.5)
plt.savefig(save_path)
plt.close()
return evaluate
elif cfg.target.name == "lj":
def evaluate(x1, x2, x1_hat, x2_hat, save_path, target1, target2, *args, **kwargs):
fig, axes = plt.subplots(2, 2, figsize=(6.5, 6.5))
x1_energy = target1.energy(x1).detach().cpu().numpy()
x1_energy_hat = target1.energy(x1_hat).detach().cpu().numpy()
x2_energy = target2.energy(x2).detach().cpu().numpy()
x2_energy_hat = target2.energy(x2_hat).detach().cpu().numpy()
if cfg.target.lj1.n_particles == 13:
min1, max1 = (-100, 50)
min2, max2 = (-100, 50)
elif cfg.target.lj1.n_particles == 55:
min1, max1 = (-400, 100)
min2, max2 = (-400, 100)
else:
min1, max1 = (0, 200) if target1._eps == 0 else (-200, 0)
min2, max2 = (-550, -350)
axes[1, 0].hist(x1_energy, np.linspace(min1, max1, 100), density=1, alpha=1, histtype='step', label='x1')
axes[1, 0].hist(x1_energy_hat, np.linspace(min1, max1, 100), density=1, alpha=1, histtype='step', label='x1 pred')
axes[1, 1].hist(x2_energy, np.linspace(min2, max2, 100), density=1, alpha=1, histtype='step', label='x2')
axes[1, 1].hist(x2_energy_hat, np.linspace(min2, max2, 100), density=1, alpha=1, histtype='step', label='x2 pred')
axes[1, 0].legend()
axes[1, 1].legend()
x1 = x1 / target1.scaling
x2 = x2 / target2.scaling
x1_hat = x1_hat / target1.scaling
x2_hat = x2_hat / target2.scaling
def get_dist(x):
x = (((x.reshape(-1, cfg.target.lj1.n_particles, 1, cfg.target.lj1.n_dim) - x.reshape(-1, 1,cfg.target.lj1.n_particles, cfg.target.lj1.n_dim))**2).sum(-1).sqrt()).cpu()
diagx = torch.triu_indices(x.shape[1], x.shape[1], 1)
return x[:, diagx[0], diagx[1]].flatten()
x1_dist = get_dist(x1)
x1_hat_dist = get_dist(x1_hat)
x2_dist = get_dist(x2)
x2_hat_dist = get_dist(x2_hat)
axes[0, 0].hist(x1_dist, 100, density=1, alpha=1, histtype='step', label='x1')
axes[0, 0].hist(x1_hat_dist, 100, density=1, alpha=1, histtype='step', label='x1 pred')
axes[0, 1].hist(x2_dist, 100, density=1, alpha=1, histtype='step', label='x2')
axes[0, 1].hist(x2_hat_dist, 100, density=1, alpha=1, histtype='step', label='x2 pred')
axes[0, 0].legend()
axes[0, 1].legend()
plt.savefig(save_path)
plt.close()
return evaluate
elif cfg.target.name == "aldp":
import mdtraj
import matplotlib as mpl
def evaluate(x1, x2, x1_hat, x2_hat, save_path, target1, target2, *args, **kwargs):
x1 = x1 / target1.scaling
x2 = x2 / target2.scaling
x1_hat = x1_hat / target1.scaling
x2_hat = x2_hat / target2.scaling
# target2 = target1
# x1 = flip_chirality(x1)
# x2 = flip_chirality(x2)
# x1_hat = flip_chirality(x1_hat)
# x2_hat = flip_chirality(x2_hat)
x1_np = x1.detach().cpu().numpy()
x2_np = x2.detach().cpu().numpy()
x1_hat_np = x1_hat.detach().cpu().numpy()
x2_hat_np = x2_hat.detach().cpu().numpy()
# plot x1 and x1_hat
aldp = target1.system
topology = mdtraj.Topology.from_openmm(aldp.topology)
test_traj = mdtraj.Trajectory(x1_np.reshape(-1, 22, 3), topology)
sampled_traj = mdtraj.Trajectory(x1_hat_np.reshape(-1, 22, 3), topology)
psi_d = mdtraj.compute_psi(test_traj)[1].reshape(-1)
phi_d = mdtraj.compute_phi(test_traj)[1].reshape(-1)
is_nan = np.logical_or(np.isnan(psi_d), np.isnan(phi_d))
not_nan = np.logical_not(is_nan)
psi_d = psi_d[not_nan]
phi_d = phi_d[not_nan]
psi = mdtraj.compute_psi(sampled_traj)[1].reshape(-1)
phi = mdtraj.compute_phi(sampled_traj)[1].reshape(-1)
is_nan = np.logical_or(np.isnan(psi), np.isnan(phi))
not_nan = np.logical_not(is_nan)
psi = psi[not_nan]
phi = phi[not_nan]
# Ramachandran plot
plt.figure(figsize=(20, 20))
plt.subplot(2, 2, 1)
plt.hist2d(phi_d, psi_d, bins=64, norm=mpl.colors.LogNorm(),
range=[[-np.pi, np.pi], [-np.pi, np.pi]])
plt.xticks(fontsize=20)
plt.yticks(fontsize=20)
plt.xlabel('$\phi$', fontsize=24)
plt.ylabel('$\psi$', fontsize=24)
plt.subplot(2, 2, 2)
plt.hist2d(phi, psi, bins=64, norm=mpl.colors.LogNorm(),
range=[[-np.pi, np.pi], [-np.pi, np.pi]])
plt.xticks(fontsize=20)
plt.yticks(fontsize=20)
plt.xlabel('$\phi$', fontsize=24)
plt.ylabel('$\psi$', fontsize=24)
# plot x2 and x2_hat
aldp = target1.system
topology = mdtraj.Topology.from_openmm(aldp.topology)
test_traj = mdtraj.Trajectory(x2_np.reshape(-1, 22, 3), topology)
sampled_traj = mdtraj.Trajectory(x2_hat_np.reshape(-1, 22, 3), topology)
psi_d = mdtraj.compute_psi(test_traj)[1].reshape(-1)
phi_d = mdtraj.compute_phi(test_traj)[1].reshape(-1)
is_nan = np.logical_or(np.isnan(psi_d), np.isnan(phi_d))
not_nan = np.logical_not(is_nan)
psi_d = psi_d[not_nan]
phi_d = phi_d[not_nan]
psi = mdtraj.compute_psi(sampled_traj)[1].reshape(-1)
phi = mdtraj.compute_phi(sampled_traj)[1].reshape(-1)
is_nan = np.logical_or(np.isnan(psi), np.isnan(phi))
not_nan = np.logical_not(is_nan)
psi = psi[not_nan]
phi = phi[not_nan]
plt.subplot(2, 2, 3)
plt.hist2d(phi_d, psi_d, bins=64, norm=mpl.colors.LogNorm(),
range=[[-np.pi, np.pi], [-np.pi, np.pi]])
plt.xticks(fontsize=20)
plt.yticks(fontsize=20)
plt.xlabel('$\phi$', fontsize=24)
plt.ylabel('$\psi$', fontsize=24)
plt.subplot(2, 2, 4)
plt.hist2d(phi, psi, bins=64, norm=mpl.colors.LogNorm(),
range=[[-np.pi, np.pi], [-np.pi, np.pi]])
plt.xticks(fontsize=20)
plt.yticks(fontsize=20)
plt.xlabel('$\phi$', fontsize=24)
plt.ylabel('$\psi$', fontsize=24)
plt.savefig(save_path, dpi=300)
plt.close()
return evaluate
if cfg.target.name == "phi4":
def evaluate(x1, x2, x1_hat, x2_hat, save_path, *args, **kwargs):
# Create subplots
fig, axes = plt.subplots(1, 2, figsize=(6.5, 3))
axes[0].hist(x1.mean(-1), bins=100, density=True, alpha=0.5, histtype='step')
axes[0].hist(x1_hat.mean(-1), bins=100, density=True, alpha=0.5, histtype='step')
axes[1].hist(x2.mean(-1), bins=100, density=True, alpha=0.5, histtype='step')
axes[1].hist(x2_hat.mean(-1), bins=100, density=True, alpha=0.5, histtype='step')
plt.savefig(save_path)
plt.close()
return evaluate
def detect_chirality(coords):
"""
Detects D-residues in a batch of ALDP molecules.
Input: coords of shape (N, 66) (N samples, 22 atoms × 3D)
Output: Boolean mask of shape (N,) where True means D-configuration.
"""
coords = coords.view(-1, 22, 3) # (N, 22, 3)
# ALDP atom indices
n, ca, c, cb = 6, 8, 14, 10
# Compute vectors
vec_n = coords[:, n, :] - coords[:, ca, :] # N - CA
vec_c = coords[:, c, :] - coords[:, ca, :] # C - CA
vec_cb = coords[:, cb, :] - coords[:, ca, :] # CB - CA
# **Normalization Fix**: Normalize vectors to avoid scale issues
vec_n = vec_n / vec_n.norm(dim=1, keepdim=True)
vec_c = vec_c / vec_c.norm(dim=1, keepdim=True)
vec_cb = vec_cb / vec_cb.norm(dim=1, keepdim=True)
# Compute chirality determinant
chirality = torch.einsum('ij,ij->i', torch.cross(vec_n, vec_c, dim=1), vec_cb)
# **Fix Chirality Check**: Negative means D-form
d_mask = chirality < 0
return d_mask # Shape: (N,)
def flip_d_residues(coords, d_mask):
"""
Flips only the D-residues in the batch.
Input:
- coords: (N, 66) tensor of atomic positions
- d_mask: (N,) boolean tensor indicating which rows are D-configuration
Output: Flipped tensor with only D-residues corrected.
"""
coords = coords.view(-1, 22, 3) # (N, 22, 3)
# Get Cα (8) and Cβ (10) positions
ca_pos = coords[:, 8, :] # (N, 3)
cb_pos = coords[:, 10, :] # (N, 3)
# **Fix Shape Issue**: Ensure we modify CB only when there are D-residues
if d_mask.any():
cb_pos[d_mask] = 2 * ca_pos[d_mask] - cb_pos[d_mask] # Mirror CB across CA
# Store the corrected coordinates
coords[:, 10, :] = cb_pos # Update CB positions
return coords.view(-1, 66) # Flatten back to original shape
def flip_chirality(input_tensor):
"""
Detects and fixes chirality issues in a batch of ALDP molecules.
Input: Tensor of shape (N, 66)
Output: Corrected tensor of shape (N, 66)
"""
d_mask = detect_chirality(input_tensor) # Find D-residues
corrected_coords = flip_d_residues(input_tensor, d_mask) # Flip only D-configurations
return corrected_coords # Shape: (N, 66)