-
Notifications
You must be signed in to change notification settings - Fork 0
/
fit-nmr-observables.py
1342 lines (1177 loc) · 50.7 KB
/
fit-nmr-observables.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
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
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
from pathlib import Path
from typing import TypedDict
import click
import numpy
import pandas
import scipy
from openff.toolkit import ForceField, Topology
from openff.units import unit
from openmm import unit as openmm_unit
from proteinbenchmark.benchmark_targets import benchmark_targets
from proteinbenchmark.force_fields import force_fields
class TorsionDict(TypedDict):
dihedral_name: str
smirks: str
periodicity: list[int]
phase: list[float]
def test_gradient(function_and_gradient, x, args=tuple()):
"""
Test function gradient using finite difference approximation.
"""
if not isinstance(args, tuple):
args = (args,)
value, gradient = function_and_gradient(x, *args)
gradient_approximation = scipy.optimize.approx_fprime(
x, lambda x: function_and_gradient(x, *args)[0]
)
sse = numpy.mean(numpy.square(gradient_approximation - gradient))
print(value)
print(numpy.sqrt(sse))
print(gradient)
print(gradient_approximation)
class NMRFitter:
"""
Optimize force field parameter targeting NMR observables by reweighting.
"""
def __init__(
self,
result_directory: str,
fit_parameters: TorsionDict,
force_field: ForceField,
force_field_name: str,
observable_list: list[str],
target_list: list[str],
truncate_observables: bool,
replica: int | None = None,
):
"""
Setup the time series of estimated observables and basis functions for
the reweighting potential.
Parameters
----------
result_directory
Top-level directory containing simulation results.
fit_parameters
Dictiondary of dihedral name, SMIRKS pattern, periodicities, and
phases for fit parameters.
force_field
OpenFF ForceField object representing the force field used to
parametrize the molecule.
force_field_name
Name of the force field used to sample the observable time series.
observable_list
List of observable names.
target_list
List of target names.
truncate_observables
Truncate experimental scalar couplings to extrema of the Karplus
curve.
replica
Replica index to read. Default is to use all replicas.
"""
self.N_parameters = sum(
[len(parameter["phase"]) for parameter in fit_parameters.values()]
)
# Sample indices and MBAR weight denominators by target
mbar_samples = list()
mbar_uncorrelated_samples = list()
for target in target_list:
target_directory = Path(
result_directory,
f"{target}-{force_field_name}",
"analysis",
)
if replica is None:
mbar_samples_path = Path(
target_directory,
f"{target}-{force_field_name}-mbar-samples.dat",
)
mbar_uncorrelated_samples_path = Path(
target_directory,
f"{target}-{force_field_name}-mbar-uncorrelated-samples.dat",
)
else:
mbar_samples_path = Path(
target_directory,
f"{target}-{force_field_name}-mbar-{replica}-samples.dat",
)
mbar_uncorrelated_samples_path = Path(
target_directory,
f"{target}-{force_field_name}-mbar-{replica}-uncorrelated-"
"samples.dat",
)
# Read correlated sample indices and MBAR weight denominator
target_samples_df = pandas.read_csv(
mbar_samples_path,
index_col=0,
usecols=lambda column: column != "Fraction Native Contacts",
)
# Read uncorrelated sample indices and MBAR weight denominators
target_uncorrelated_samples_df = pandas.read_csv(
mbar_uncorrelated_samples_path,
index_col=0,
usecols=lambda column: column != "Fraction Native Contacts",
)
mbar_samples.append(target_samples_df)
mbar_uncorrelated_samples.append(target_uncorrelated_samples_df)
self.get_target_betas(target_list)
self.set_up_observables(
result_directory,
force_field_name,
observable_list,
target_list,
truncate_observables,
mbar_samples,
mbar_uncorrelated_samples,
)
self.set_up_reweighting_basis_functions(
result_directory,
fit_parameters,
force_field,
force_field_name,
target_list,
mbar_samples,
mbar_uncorrelated_samples,
)
N_targets = len(target_list)
self.reweighting_potential = [None for _ in range(N_targets)]
self.uncorrelated_reweighting_potential = [None for _ in range(N_targets)]
self.chi_square = numpy.zeros(N_targets)
self.chi_square_uncertainty = numpy.zeros(N_targets)
self.effective_samples = numpy.zeros(N_targets)
self.effective_samples_uncertainty = numpy.zeros(N_targets)
def __call__(
self,
parameters: numpy.typing.ArrayLike,
alpha: float,
observables_to_skip: list[str] | None = None,
):
"""
Compute the value of the regularized loss function and its gradient with
respect to the fit parameters.
Parameters
----------
parameters
numpy array containing the values of the fit parameters.
alpha
Regularization strength
observables_to_skip
List of observable types to skip for evaluation of the objective
function. Used for leave-one-out cross validation.
"""
return self.compute_loss_function_and_gradient(
parameters, alpha, observables_to_skip=observables_to_skip
)
def get_target_betas(self, target_list: list[str]):
"""
Read ensemble temperature for observable targets and compute
thermodynamic beta, i.e. (k_B T)^-1.
Parameters
----------
target_list
List of target names.
"""
target_betas = list()
for target in target_list:
target_temperature = benchmark_targets[target]["temperature"]
RT = openmm_unit.MOLAR_GAS_CONSTANT_R * target_temperature.to_openmm()
target_betas.append(
1.0 / RT.value_in_unit(openmm_unit.kilocalorie_per_mole)
)
self.target_betas = target_betas
def set_up_observables(
self,
result_directory: str,
force_field_name: str,
observable_list: list[str],
target_list: list[str],
truncate_observables: bool,
mbar_samples: list[pandas.DataFrame],
mbar_uncorrelated_samples: list[pandas.DataFrame],
):
"""
Read the uncorrelated sample indices and weight denominator from MBAR
and the time series of estimated observables.
Parameters
----------
result_directory
Top-level directory containing simulation results.
force_field_name
Name of the force field used to sample the observable time series.
observable_list
List of observable names.
target_list
List of target names.
truncate_observables
Truncate experimental scalar couplings to extrema of the Karplus
curve.
mbar_samples
Correlated sample indices and MBAR weight denominators by target.
mbar_uncorrelated_samples
Uncorrelated sample indices and MBAR weight denominators by target.
"""
# List of columns to read for observable DataFrames
experiment_column = (
"Truncated Experiment" if truncate_observables else "Experiment"
)
df_columns = [
"Frame",
"Observable",
"Resid",
"Resname",
experiment_column,
"Experiment Uncertainty",
"Computed",
]
# Observable types, experimental values, experimental uncertainties, and
# unweighted estimates of observables by target
self.observable_types = list()
self.experimental_observables = list()
self.experimental_variances = list()
# Sample estimates of observables by target
self.sampled_observables = list()
self.uncorrelated_observables = list()
# MBAR weight denominators for correlated samples
self.mbar_weight_denominators = list()
# Bootstrap indices and MBAR weight denominators for bootstrap samples
self.bootstrap_sample_indices = list()
self.bootstrap_mbar_weight_denominators = list()
print(
"Reference_Force_Field Target Chi^2 "
"(StDev) N_eff (StDev)"
)
for target_index, target in enumerate(target_list):
# DataFrame of correlated and uncorrelated sample indices for this
# target
target_samples_df = mbar_samples[target_index]
target_uncorrelated_samples_df = mbar_uncorrelated_samples[target_index]
# Read observables for uncorrelated sample indices in each window
target_observable_df = pandas.DataFrame()
target_uncorrelated_observable_df = pandas.DataFrame()
target_directory = Path(
result_directory,
f"{target}-{force_field_name}",
"analysis",
)
for window in target_samples_df["Window"].unique():
window_samples_df = target_samples_df[
target_samples_df["Window"] == window
]
for replica in window_samples_df["Replica"].unique():
window_uncorrelated_sample_indices = (
target_uncorrelated_samples_df.loc[
(target_uncorrelated_samples_df["Replica"] == replica)
& (target_uncorrelated_samples_df["Window"] == window),
"Indices",
].values
)
observable_path = Path(
target_directory,
f"{target}-{force_field_name}-{replica}-{window:02d}-"
"scalar-couplings-time-series.dat",
)
observable_df = pandas.read_csv(
observable_path,
usecols=df_columns,
)
# All samples, correlated
observable_df = observable_df[
observable_df["Observable"].isin(observable_list)
]
target_observable_df = pandas.concat(
[target_observable_df, observable_df]
)
# Uncorrelated samples
observable_df = observable_df[
observable_df["Frame"].isin(window_uncorrelated_sample_indices)
]
target_uncorrelated_observable_df = pandas.concat(
[target_uncorrelated_observable_df, observable_df]
)
# Create a (N_observables, N_samples) numpy array of computed values
# of observables for each sample
index_columns = [
"Observable",
"Resid",
"Resname",
"Truncated Experiment",
"Experiment Uncertainty",
]
target_observable_df.set_index(index_columns, inplace=True)
target_observable_df.sort_index(inplace=True)
target_uncorrelated_observable_df.set_index(index_columns, inplace=True)
target_uncorrelated_observable_df.sort_index(inplace=True)
observable_groups = target_observable_df.index.unique()
N_observables = observable_groups.size
N_samples = target_samples_df.shape[0]
N_uncorrelated_samples = target_uncorrelated_samples_df.shape[0]
target_observable_types = list()
target_experimental_observables = numpy.zeros(N_observables)
target_experimental_uncertainties = numpy.zeros(N_observables)
target_sampled_observables = numpy.zeros((N_observables, N_samples))
target_uncorrelated_observables = numpy.zeros(
(N_observables, N_uncorrelated_samples)
)
for observable_index, observable_group in enumerate(observable_groups):
target_observable_types.append(observable_group[0])
target_experimental_observables[observable_index] = observable_group[3]
target_experimental_uncertainties[observable_index] = observable_group[
4
]
target_sampled_observables[observable_index] = target_observable_df.loc[
observable_group, "Computed"
].values
target_uncorrelated_observables[observable_index] = (
target_uncorrelated_observable_df.loc[
observable_group,
"Computed",
].values
)
self.observable_types.append(target_observable_types)
self.experimental_observables.append(target_experimental_observables)
self.experimental_variances.append(
numpy.square(target_experimental_uncertainties)
)
self.sampled_observables.append(target_sampled_observables)
self.uncorrelated_observables.append(target_uncorrelated_observables)
# Get MBAR weight denominators for correlated samples
target_mbar_weight_denominators = target_samples_df[
"MBAR Weight Denominator"
].values
self.mbar_weight_denominators.append(target_mbar_weight_denominators)
# Get bootstrap indices and MBAR weight denominators for bootstrap
# samples
target_bootstrap_sample_indices = target_uncorrelated_samples_df.loc[
:,
target_uncorrelated_samples_df.columns.str.startswith(
"Bootstrap Sample Indices"
),
].values
target_bootstrap_mbar_weight_denominators = (
target_uncorrelated_samples_df.loc[
:,
target_uncorrelated_samples_df.columns.str.startswith(
"MBAR Weight Denominator"
),
].values
)
self.bootstrap_sample_indices.append(target_bootstrap_sample_indices)
self.bootstrap_mbar_weight_denominators.append(
target_bootstrap_mbar_weight_denominators
)
# Get chi^2 value and number of effective samples for unbiased state
mbar_weights = 1.0 / target_mbar_weight_denominators
mbar_weights /= mbar_weights.sum()
estimated_observables = numpy.sum(
mbar_weights * target_sampled_observables,
axis=1,
)
reference_chi_square = numpy.mean(
numpy.square(estimated_observables - target_experimental_observables)
/ numpy.square(target_experimental_uncertainties)
)
reference_effective_samples = int(
numpy.round(1.0 / numpy.square(mbar_weights).sum())
)
# Get uncertainties in chi^2 and number of effective samples from
# bootstrapping over uncorrelated samples
bootstrap_mbar_weights = 1.0 / target_bootstrap_mbar_weight_denominators
bootstrap_mbar_weights /= bootstrap_mbar_weights.sum(axis=0)
bootstrap_observables = target_uncorrelated_observables[
:,
target_bootstrap_sample_indices,
]
bootstrap_estimated_observables = numpy.sum(
bootstrap_mbar_weights * bootstrap_observables,
axis=1,
)
bootstrap_chi_square = numpy.mean(
numpy.square(
bootstrap_estimated_observables.T - target_experimental_observables
)
/ numpy.square(target_experimental_uncertainties),
axis=1,
)
bootstrap_effective_samples = 1.0 / numpy.square(
bootstrap_mbar_weights
).sum(axis=0)
chi_square_uncertainty = bootstrap_chi_square.std(ddof=1)
effective_samples_uncertainty = bootstrap_effective_samples.std(ddof=1)
print(
f"{force_field_name:33s} {target:14s} "
f"{reference_chi_square:9.4f} ({chi_square_uncertainty:7.4}) "
f"{reference_effective_samples:6d} "
f"({effective_samples_uncertainty:5.1f})"
)
def set_up_reweighting_basis_functions(
self,
result_directory: str,
fit_parameters: TorsionDict,
force_field: ForceField,
force_field_name: str,
target_list: list[str],
mbar_samples: list[pandas.DataFrame],
mbar_uncorrelated_samples: list[pandas.DataFrame],
):
"""
Construct the time series of basis functions for the reweighting
potential from the time series of dihedral angles.
Parameters
----------
result_directory
Top-level directory containing simulation results.
fit_parameters
Dictiondary of dihedral name, SMIRKS pattern, periodicities, and
phases for fit parameters.
force_field
OpenFF ForceField object representing the force field used to
parametrize the molecule.
force_field_name
Name of the force field used to sample the observable time series.
target_list
List of target names.
mbar_samples
Correlated sample indices and MBAR weight denominators by target.
mbar_uncorrelated_samples
Uncorrelated sample indices and MBAR weight denominators by target.
"""
self.N_parameters = sum(
[len(parameter["phase"]) for parameter in fit_parameters.values()]
)
# Basis functions for reweighting potential
# Phi_p(t) = sum_{i in dihedrals_p} 1 + cos(n_i phi_i(t) - gamma_i)
# such that U_rw(k, t) = \sum_p k_p Phi_p(t)
self.reweighting_basis_functions = list()
self.uncorrelated_basis_functions = list()
for target_index, target in enumerate(target_list):
# DataFrame of correlated and uncorrelated sample indices for this
# target
target_samples_df = mbar_samples[target_index]
target_uncorrelated_samples_df = mbar_uncorrelated_samples[target_index]
target_directory = Path(
result_directory,
f"{target}-{force_field_name}",
)
# Get OpenFF Topology for protonated biopolymer with residue metadata
topology_path = Path(
target_directory,
"setup",
f"{target}-{force_field_name}-protonated.pdb",
)
topology = Topology.from_pdb(str(topology_path))
# Get list of residue indices that match each fit parameter SMIRKS
parameter_resids = {parameter_id: list() for parameter_id in fit_parameters}
torsion_matches = force_field["ProperTorsions"].find_matches(
topology,
unique=True,
)
for atom_indices, match in torsion_matches.items():
if match.parameter_type.id not in fit_parameters:
continue
# Residue indices for atoms in central bond of dihedral angle
resid_a = int(topology.atom(atom_indices[1]).metadata["residue_number"])
resid_b = int(topology.atom(atom_indices[2]).metadata["residue_number"])
dihedral_resid = resid_a if resid_a <= resid_b else resid_b
parameter_resids[match.parameter_type.id].append(dihedral_resid)
# Construct a (N_parameters, N_samples) numpy array of basis
# functions for this target
N_samples = target_samples_df.shape[0]
N_uncorrelated_samples = target_uncorrelated_samples_df.shape[0]
target_basis_functions = numpy.zeros((self.N_parameters, N_samples))
target_uncorrelated_basis_functions = numpy.zeros(
(self.N_parameters, N_uncorrelated_samples)
)
total_sample_index = 0
total_uncorrelated_sample_index = 0
# Read dihedral angles for uncorrelated sample indices in each window
for window in target_samples_df["Window"].unique():
window_samples_df = target_samples_df[
target_samples_df["Window"] == window
]
for replica in window_samples_df["Replica"].unique():
window_uncorrelated_sample_indices = (
target_uncorrelated_samples_df.loc[
(target_uncorrelated_samples_df["Replica"] == replica)
& (target_uncorrelated_samples_df["Window"] == window),
"Indices",
].values
)
dihedral_path = Path(
target_directory,
"analysis",
f"{target}-{force_field_name}-{replica}-{window:02d}-"
"dihedrals.dat",
)
dihedral_df = pandas.read_csv(
dihedral_path,
usecols=["Frame", "Resid", "Dihedral Name", "Dihedral (deg)"],
index_col=[1, 2],
)
dihedral_df.sort_index(inplace=True)
# Uncorrelated samples
uncorrelated_dihedral_df = dihedral_df[
dihedral_df["Frame"].isin(window_uncorrelated_sample_indices)
]
sample_start_index = total_sample_index
uncorrelated_sample_start_index = total_uncorrelated_sample_index
total_sample_index += dihedral_df.loc[dihedral_df.index[0]].shape[0]
total_uncorrelated_sample_index += uncorrelated_dihedral_df.loc[
uncorrelated_dihedral_df.index[0]
].shape[0]
# Loop over fit parameters
parameter_index = 0
for parameter_id, fit_parameter in fit_parameters.items():
dihedral_name = fit_parameter["dihedral_name"]
# Loop over dihedral angles matching this fit parameter
for dihedral_resid in parameter_resids[parameter_id]:
dihedral_time_series = numpy.deg2rad(
dihedral_df.loc[
(dihedral_resid, dihedral_name),
"Dihedral (deg)",
].values
)
dihedral_uncorrelated_time_series = numpy.deg2rad(
uncorrelated_dihedral_df.loc[
(dihedral_resid, dihedral_name),
"Dihedral (deg)",
].values
)
# Loop over Fourier terms for this fit parameter
fourier_index = parameter_index
for periodicity, phase in zip(
fit_parameter["periodicity"],
fit_parameter["phase"],
):
target_basis_functions[
fourier_index,
sample_start_index:total_sample_index,
] += 1 + numpy.cos(
periodicity * dihedral_time_series - phase
)
target_uncorrelated_basis_functions[
fourier_index,
uncorrelated_sample_start_index:total_uncorrelated_sample_index,
] += 1 + numpy.cos(
periodicity * dihedral_uncorrelated_time_series
- phase
)
fourier_index += 1
parameter_index += len(fit_parameter["phase"])
self.reweighting_basis_functions.append(target_basis_functions)
self.uncorrelated_basis_functions.append(
target_uncorrelated_basis_functions
)
def compute_loss_function_and_gradient(
self,
parameters: numpy.typing.ArrayLike,
alpha: float,
observables_to_skip: list[str] = None,
):
"""
Compute the value of the regularized loss function and its gradient with
respect to the fit parameters.
Parameters
----------
parameters
numpy array containing the values of the fit parameters.
alpha
Regularization strength.
observables_to_skip
List of observable types to skip for evaluation of the objective
function. Used for leave-one-out cross validation.
"""
if len(parameters) != self.N_parameters:
raise ValueError(
"Length of parameter vector to evaluate loss function "
f"({len(parameters)}) does not match length of fit parameter "
f"vector ({self.N_parameters})"
)
total_chi_square = 0
total_chi_square_gradient = numpy.zeros(self.N_parameters)
for target_index, target_basis_functions in enumerate(
self.reweighting_basis_functions
):
beta = self.target_betas[target_index]
target_experimental_observables = self.experimental_observables[
target_index
]
target_experimental_variances = self.experimental_variances[target_index]
target_sampled_observables = self.sampled_observables[target_index]
target_uncorrelated_observables = self.uncorrelated_observables[
target_index
]
target_mbar_weight_denominators = self.mbar_weight_denominators[
target_index
]
target_bootstrap_sample_indices = self.bootstrap_sample_indices[
target_index
]
target_bootstrap_mbar_weight_denominators = (
self.bootstrap_mbar_weight_denominators[target_index]
)
target_uncorrelated_basis_functions = self.uncorrelated_basis_functions[
target_index
]
if observables_to_skip is not None:
observable_mask = [
observable not in observables_to_skip
for observable in self.observable_types[target_index]
]
target_experimental_observables = target_experimental_observables[
observable_mask
]
target_experimental_variances = target_experimental_variances[
observable_mask
]
target_sampled_observables = target_sampled_observables[observable_mask]
target_uncorrelated_observables = target_uncorrelated_observables[
observable_mask
]
# U_rw(k, t) = sum_i k_i * (1 + cos(n_i * phi_i(t) - gamma_i)
reweighting_potential = beta * numpy.dot(
parameters,
target_basis_functions,
)
uncorrelated_reweighting_potential = beta * numpy.dot(
parameters,
target_uncorrelated_basis_functions,
)
# Offset the reweighting potential so that the lowest value is
# zero and we don't get NaNs when we exponentiate it
reweighting_potential_offset = reweighting_potential.min()
reweighting_potential -= reweighting_potential_offset
uncorrelated_reweighting_potential -= reweighting_potential_offset
self.reweighting_potential[target_index] = reweighting_potential
self.uncorrelated_reweighting_potential[target_index] = (
uncorrelated_reweighting_potential
)
# Compute MBAR weights for reweighting from mixture distribution
mbar_weights = (
numpy.exp(-reweighting_potential) / target_mbar_weight_denominators
)
# W(k, t) = exp(-U_rw(k, t)) / Z(t) / ( sum_t exp(-U_rw(k, t)) / Z(t) )
# N_eff(k) = (sum_t W(k, t))^2 / sum_t W(k, t)^2
mbar_weight_normalization = mbar_weights.sum()
if mbar_weight_normalization == 0.0:
mbar_weights = numpy.zeros(mbar_weights.size)
self.effective_samples[target_index] = 0.0
else:
mbar_weights = mbar_weights / mbar_weight_normalization
self.effective_samples[target_index] = (
1.0 / numpy.square(mbar_weights).sum()
)
# < O_j >(k) = sum_t W(k, t) * O_j(t)
reweighted_estimates = numpy.sum(
mbar_weights * target_sampled_observables,
axis=1,
)
# < dU_rw / dk_i >(k)
# = sum_t W(k, t) * (1 + cos(n_i * phi_i(t) - gamma_i))
reweighting_potential_derivatives = numpy.sum(
mbar_weights * target_basis_functions,
axis=1,
)
# < O_j * dU_rw / dk_i>(k)
# = sum_t W(k, t) * O_j(t) * (1 + cos(n_i * phi_i(t) - gamma_i))
# Building up the (N_parameters, N_observables, N_samples) array and
# then calling numpy.sum(axis=2) is faster but requires more memory
observable_derivative_product = numpy.zeros(
(self.N_parameters, target_experimental_observables.size)
)
for parameter_index in range(self.N_parameters):
observable_derivative_product[parameter_index] = numpy.sum(
target_basis_functions[parameter_index]
* target_sampled_observables
* mbar_weights,
axis=1,
)
# d< O_j > / dk_i = beta *
# (< O_j > * < dU_rw / dk_i > - < O_j * dU_rw / dk_i >)
# Multiply by beta once at the end
reweighted_estimate_derivatives = beta * (
reweighting_potential_derivatives[:, numpy.newaxis]
* reweighted_estimates
- observable_derivative_product
)
# chi^2(k) = sum_j (< O_j >(k) - O_j,exp )^2 / sigma_j,exp^2
target_errors = reweighted_estimates - target_experimental_observables
weighted_target_errors = target_errors / target_experimental_variances
chi_square = numpy.sum(weighted_target_errors * target_errors)
self.chi_square[target_index] = chi_square
total_chi_square += chi_square
# dchi^2 / dk_i = 2 *
# sum_j (< O_j >(k) - O_j,exp) / sigma_j,exp^2 * d< O_j > / dk_i
# Multiply by 2 once at the end
total_chi_square_gradient += numpy.sum(
reweighted_estimate_derivatives * weighted_target_errors,
axis=1,
)
# Get uncertainties in chi^2 and number of effective samples from
# bootstrapping over uncorrelated samples
uncorrelated_boltzmann_factors = numpy.exp(
-uncorrelated_reweighting_potential
)
bootstrap_mbar_weights = (
uncorrelated_boltzmann_factors[target_bootstrap_sample_indices]
/ target_bootstrap_mbar_weight_denominators
)
bootstrap_weight_normalization = bootstrap_mbar_weights.sum(axis=0)
if numpy.any(bootstrap_weight_normalization == 0.0):
bootstrap_mbar_weights = numpy.zeros(bootstrap_mbar_weights.shape)
bootstrap_effective_samples = numpy.zeros(
target_bootstrap_sample_indices.shape[1]
)
else:
bootstrap_mbar_weights = (
bootstrap_mbar_weights / bootstrap_weight_normalization
)
bootstrap_effective_samples = 1.0 / numpy.square(
bootstrap_mbar_weights
).sum(axis=0)
bootstrap_observables = target_uncorrelated_observables[
:,
target_bootstrap_sample_indices,
]
bootstrap_estimated_observables = numpy.sum(
bootstrap_mbar_weights * bootstrap_observables,
axis=1,
)
bootstrap_chi_square = numpy.mean(
numpy.square(
bootstrap_estimated_observables.T - target_experimental_observables
)
/ target_experimental_variances,
axis=1,
)
self.chi_square_uncertainty[target_index] = bootstrap_chi_square.std(ddof=1)
self.effective_samples_uncertainty[target_index] = (
bootstrap_effective_samples.std(ddof=1)
)
# L(k) = chi^2(k) + alpha sum_i k_i^2
loss_function = total_chi_square + alpha * numpy.sum(numpy.square(parameters))
# dL / dk_i = dchi^2 / dk_i + 2 alpha k_i
loss_gradient = 2 * (total_chi_square_gradient + alpha * parameters)
return loss_function, loss_gradient
@click.command()
@click.option(
"-a",
"--alpha",
default=None,
show_default=True,
type=click.FLOAT,
help="Strength of L2 regularization term. Default is to do leave-one-out "
"cross validation to choose the optimal value using a line search.",
)
@click.option(
"-c",
"--chi-square-only",
is_flag=True,
default=False,
help="Evaluate the loss function once with no parameter optimization.",
)
@click.option(
"-f",
"--force-field-name",
default="null-0.0.3-pair-opc3",
show_default=True,
type=click.STRING,
help="Name of force field to optimize.",
)
@click.option(
"-g",
"--gradient-test",
is_flag=True,
default=False,
help="Test the gradient of the loss function using a finite difference method.",
)
@click.option(
"-i",
"--input-directory",
default="results",
show_default=True,
type=click.STRING,
help="Directory path containing MBAR analysis and time series of observables.",
)
@click.option(
"-l",
"--log10-alpha",
default=None,
show_default=True,
type=click.FLOAT,
help="Logarithmic strength of L2 regularization term. Default is to do "
"leave-one-out cross validation to choose the optimal value using a "
"line search.",
)
@click.option(
"-o",
"--output-prefix",
default="final-force-field",
show_default=True,
type=click.STRING,
help="Prefix for file path of output files.",
)
@click.option(
"-r",
"--replica",
default=None,
show_default=True,
type=click.STRING,
help="Replica index to read. Default is to use all replicas.",
)
@click.option(
"-t/-e",
"--truncate-observables/--experimental-observables",
default=True,
help="Truncate scalar couplings to the extrema of the Karplus curve.",
)
def main(
alpha,
chi_square_only,
force_field_name,
gradient_test,
input_directory,
log10_alpha,
output_prefix,
replica,
truncate_observables,
):
# List of parameter ids for parameters to be fit and their associated
# protein dihedral
fit_parameter_ids = {
"Protein-phi-general": "phi",
"Protein-phi-sidechain": "phi'",
"Protein-phi-beta-branched": "phi'",
"Protein-psi-general": "psi",
"Protein-psi-sidechain": "psi'",
"Protein-psi-beta-branched": "psi'",
}
# List of observable types to train on
observable_list = ["3j_hn_cb", "3j_hn_co", "3j_hn_ha"]
# List of training targets
target_list = ["gb3"]
N_targets = len(target_list)
# Get initial force field
if force_field_name in [
f"null-0.0.3-pair-{water_model}"
for water_model in ["tip3p", "opc3", "tip3p-fb", "opc", "tip4p-fb"]
]:
force_field_path = Path(
Path(__file__).parent.absolute(),
"nmr-force-fields",
"null-0.0.3-pair-plus-backbone.offxml",
#"null-0.0.3-pair-plus-backbone-general.offxml",
)
force_field = ForceField(str(force_field_path))
elif force_field_name not in force_fields:
msg = f"Force field {force_field_name} is invalid. Must be one of"
for ff in force_fields:
msg += f"\n {ff}"
raise ValueError(msg)
else:
force_field = ForceField(force_fields[force_field_name]["force_field_file"])
# Get periodicities and phases for proper torsions associated with
# parameters to be fit
torsion_handler = force_field["ProperTorsions"]
fit_parameters = dict()
for parameter_id, dihedral_name in fit_parameter_ids.items():
parameter = torsion_handler.get_parameter({"id": parameter_id})[0]
fit_parameters[parameter_id] = {
"dihedral_name": dihedral_name,
"smirks": parameter.smirks,
"periodicity": parameter.periodicity,
"phase": [phase.to(unit.radian).m for phase in parameter.phase],
}