-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathutils.py
152 lines (121 loc) · 4.31 KB
/
utils.py
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
import matplotlib.pyplot as plt
import numpy as np
import keras
import keras.backend as K
from keras.layers import BatchNormalization, Activation, Embedding, ZeroPadding2D, Dense
from keras.layers.advanced_activations import LeakyReLU
from keras.layers.convolutional import UpSampling2D, Conv2D
from keras.models import Sequential, Model
from keras.optimizers import Adam
from msmbuilder.preprocessing import MinMaxScaler
import mdtraj
from matplotlib import pyplot as plt
def plot_losses(losses):
losses = np.array(losses)
fig, ax = plt.subplots()
plt.plot(losses.T[0], label='Discriminator')
plt.plot(losses.T[1], label='Generator')
plt.title("Training Losses")
plt.legend()
ax.set(ylabel='BCE', xlabel='Epoch')
return fig, ax
def make_trajectory_trainable(traj_list):
"""
Build a train/test splittable array of cartesian coordinates from a list
of mdtraj.Trajectory objects
Parameters
----------
traj_list: list of mdtraj.Trajectory objects
Returns
-------
data: np.array, shape=(frames, n_atoms, 3)
A numpy array of the XYZ coordinates of all the frames in the list of
trajs. Coordinates are squised from -1 to 1.
Use a MinMaxScaler.inverse_transform to map them back to the original
space.
sc: MinMaxScaler, The scaler used to squish the coordinates.
"""
frame00 = traj_list[0][0]
trjs = [t.superpose(frame00) for t in traj_list]
sc = MinMaxScaler(feature_range=(-1, 1))
frames = []
for t in trjs:
for f in t:
frames.append(f.xyz.reshape(frame00.n_atoms, 3))
f_txx_sc = sc.fit_transform(frames)
data = np.dstack(f_txx_sc)
data = data.transpose(2, 0, 1)
return data, sc
def fake_traj_from_samples(samples, top, scaler):
fake_tr = samples[:, :, :, 0]
fake_traj_orig_space = [scaler.inverse_transform(t) for t in fake_tr]
fake_traj = mdtraj.Trajectory(fake_traj_orig_space, topology=top)
fake_traj.center_coordinates()
fake_traj.superpose(fake_traj, 0)
return fake_traj
def scatter(arr, ax=None, scatter_kws=None):
if ax is None:
f, ax = plt.subplots()
if scatter_kws is None:
scatter_kws = {}
ax.scatter(arr[:, 0], arr[:, 1], **scatter_kws)
return ax
def make_latent_samples(n_samples, sample_dim):
return np.random.normal(loc=0, scale=1, size=(n_samples, sample_dim))
def make_trainable(model, trainable):
for layer in model.layers:
layer.trainable = trainable
def make_labels(size):
return np.ones([size, 1]), np.zeros([size, 1])
def make_2dtraj_GAN(sample_size,
g_hidden_size,
d_hidden_size,
leaky_alpha,
g_learning_rate,
d_learning_rate):
K.clear_session()
generator = Sequential([
Dense(g_hidden_size, input_shape=(sample_size,)),
LeakyReLU(alpha=leaky_alpha),
Dense(2),
Activation('tanh')
], name='generator')
discriminator = Sequential([
Dense(d_hidden_size, input_shape=(2,)),
LeakyReLU(alpha=leaky_alpha),
Dense(1),
Activation('sigmoid')
], name='discriminator')
gan = Sequential([
generator,
discriminator
])
discriminator.compile(optimizer=Adam(lr=d_learning_rate), loss='binary_crossentropy')
gan.compile(optimizer=Adam(lr=g_learning_rate), loss='binary_crossentropy')
return gan, generator, discriminator
def make_3dtraj_GAN(sample_size,
g_hidden_size,
d_hidden_size,
leaky_alpha,
g_learning_rate,
d_learning_rate):
K.clear_session()
generator = Sequential([
Dense(g_hidden_size, input_shape=(sample_size,)),
LeakyReLU(alpha=leaky_alpha),
Dense(2),
Activation('tanh')
], name='generator')
discriminator = Sequential([
Dense(d_hidden_size, input_shape=(2,)),
LeakyReLU(alpha=leaky_alpha),
Dense(1),
Activation('sigmoid')
], name='discriminator')
gan = Sequential([
generator,
discriminator
])
discriminator.compile(optimizer=Adam(lr=d_learning_rate), loss='binary_crossentropy')
gan.compile(optimizer=Adam(lr=g_learning_rate), loss='binary_crossentropy')
return gan, generator, discriminator