This repository has been archived by the owner on Sep 12, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
newma_coronavirus.py
182 lines (136 loc) · 5.75 KB
/
newma_coronavirus.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
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
# -*- coding: utf-8 -*-
"""
Script to recover the results for detecting the conformational changes of
SARS-CoV-2. Is it possible to choose
between real opu (to use it contact lighton at
https://www.lighton.ai/contact-us/) and its synthetic version.
"""
import numpy as np
import time
import onlinecp.algos as algos
import onlinecp.utils.feature_functions as feat
import onlinecp.utils.fastfood as ff
from MDAnalysis import Universe
from lightonml.encoding.base import MultiThresholdEncoder
from lightonml.projections.sklearn import OPUMap
if __name__ == '__main__':
# Path of the file containing the trajectory
pathtraj = "./11764158/QHD_LP1/QHD_LP1/"
# Importing the trajectory using MDAnalysis
# Note: different tools might be needed depending on the format of the data
universe = Universe(pathtraj + 'QHD_LP1.gro', pathtraj + 'QHD_LP1.xtc')
all_atoms = universe.select_atoms("all")
traj = np.asarray([all_atoms.positions for frame in universe.trajectory],
dtype='f8')
traj = traj.reshape(traj.shape[0], -1)
timestart = 0
timestop = traj.shape[0]
n_feat = traj.shape[1]
n_atoms = np.int(n_feat/3)
print(f"Number of atoms: {n_atoms}.")
print(f"Number of features: {n_features}.")
print(f"Number of timeframes: {timestop - timestart}")
# ----------
# NEWMA RP OPU
startingtime = time.time()
# Pararameters for the NEWMA algorithm
B = 80 # window size
d = n_feat
# Forget factors chosen with heuristic in the paper
big_Lambda, small_lambda = algos.select_optimal_parameters(B)
thres_ff = small_lambda
# Number of random features is set automatically with this criterion
m = int((1 / 4) / (small_lambda + big_Lambda) ** 2)
m_OPU = 10 * m
print(f'{m_OPU} random features.')
# Opening the OPU
opu_mapping = OPUMap(n_components=m_OPU)
# Encoding the data
n_levels = 38
minX, maxX = np.min(traj), np.max(traj)
print('Rescaling X')
X = 255 * ((traj - minX) / (maxX - minX))
X = X.astype('uint8')
print('Encoding X with the multi threshold encoder')
thresholds = np.arange(n_levels, step=5)
thresholds += 10
encoder = MultiThresholdEncoder(thresholds=thresholds)
Xencode = encoder.transform(X)
del X
X = opu_mapping.transform(Xencode)
del Xencode
# NEWMA
mult = 0.5
detector = algos.NEWMA(X[0], forget_factor=big_Lambda,
feat_func=lambda x: x.astype('float32'),
forget_factor2=small_lambda,
adapt_forget_factor=thres_ff*mult,
thresholding_quantile=0.95,
dist_func=lambda z1, z2: np.linalg.norm(z1 - z2))
detector.apply_to_data(X)
detection_stat = np.array([i[0] for i in detector.stat_stored])
online_th = np.array([i[1] for i in detector.stat_stored])
computation_duration = time.time() - startingtime
print(f'Computation RP OPU took {computation_duration:.2f}s.')
# ----------
# NEWMA RFF CPU
startingtime = time.time()
X = traj
# Pararameters for the NEWMA algorithm
B = 80 # window size
d = n_feat
# Forget factors chosen with heuristic in the paper
big_Lambda, small_lambda = algos.select_optimal_parameters(B)
thres_ff = small_lambda
# number of random features is set automatically with this criterion
m = 10 * int((1 / 4) / (small_lambda + big_Lambda) ** 2)
choice_sigma = 'median'
numel = 100
data_sigma_estimate = X[:numel] # data for median trick to estimate sigma
W, sigmasq = feat.generate_frequencies(m, d, data=data_sigma_estimate,
choice_sigma=choice_sigma)
def feat_func(x):
return feat.fourier_feat(x, W)
# NEWMA
detectorb = algos.NEWMA(X[0], forget_factor=big_Lambda,
forget_factor2=small_lambda,
feat_func=feat_func,
adapt_forget_factor=0.5 * thres_ff)
detectorb.apply_to_data(X)
detection_statrff = np.array([i[0] for i in detectorb.stat_stored])
online_thrff = np.array([i[1] for i in detectorb.stat_stored])
computation_duration = time.time() - startingtime
print(f'Computation RFF took {computation_duration:.2f}s.')
# ---------
# NEWMA FF CPU
startingtime = time.time()
X = traj
# Parameters for the NEWMA algorithm
B = 80 # window size
d = n_feat
# Forget factors chosen with heuristic in the paper
big_Lambda, small_lambda = algos.select_optimal_parameters(B)
thres_ff = small_lambda
# number of random features is set automatically with this criterion
m = 10 * int((1 / 4) / (small_lambda + big_Lambda) ** 2)
choice_sigma = 'median'
numel = 100
data_sigma_estimate = X[:numel] # data for median trick to estimate sigma
W, sigmasq = feat.generate_frequencies(m, d, data=data_sigma_estimate,
choice_sigma=choice_sigma)
FF = ff.Fastfood(sigma=np.sqrt(sigmasq), n_components=m)
FF.fit(X)
X = FF.transform(X)
# NEWMA
detectorff = algos.NEWMA(X[0], forget_factor=big_Lambda,
forget_factor2=small_lambda,
adapt_forget_factor=0.5 * thres_ff)
detectorff.apply_to_data(X)
detection_statff = np.array([i[0] for i in detectorff.stat_stored])
online_thff = np.array([i[1] for i in detectorff.stat_stored])
computation_duration = time.time() - startingtime
print(f'Computation FF took {computation_duration:.2f}s.')
# Save data for analysis in jupyter notebook.
np.savez_compressed(f'newmaopu_corona_natoms_{n_atoms}.npz',
detection_stat=detection_stat,
online_th=online_th)