|
7 | 7 | from qml2 import Compound |
8 | 8 | from qml2.models.loss_functions import MAE |
9 | 9 | from qml2.models.sorf import SORFLocalModel |
| 10 | +from qml2.representations.calculators import FCHL19Calculator |
10 | 11 |
|
11 | | -xyzs = [] |
12 | | -energies = [] |
13 | 12 |
|
14 | | -training_set_size = 501 |
15 | | -test_set_size = 1000 |
16 | | -num_mols = training_set_size + test_set_size |
| 13 | +def main(): |
| 14 | + xyzs = [] |
| 15 | + energies = [] |
17 | 16 |
|
18 | | -with open("../../tests/test_data/hof_qm7.txt") as csvfile: |
19 | | - reader = csv.reader(csvfile, delimiter=" ") |
20 | | - all_rows = list(reader) |
21 | | - random.shuffle(all_rows) |
22 | | - for row in all_rows[:num_mols]: |
23 | | - xyzs.append(row[0]) |
24 | | - energies.append(float(row[1])) |
| 17 | + training_set_size = 501 |
| 18 | + test_set_size = 1000 |
| 19 | + num_mols = training_set_size + test_set_size |
25 | 20 |
|
26 | | -energies = np.array(energies) |
| 21 | + with open("../../tests/test_data/hof_qm7.txt") as csvfile: |
| 22 | + reader = csv.reader(csvfile, delimiter=" ") |
| 23 | + all_rows = list(reader) |
| 24 | + random.shuffle(all_rows) |
| 25 | + for row in all_rows[:num_mols]: |
| 26 | + xyzs.append(row[0]) |
| 27 | + energies.append(float(row[1])) |
27 | 28 |
|
28 | | -compounds = [] |
29 | | -with tarfile.open("../../tests/test_data/qm7.tar.gz") as tar: |
30 | | - for xyz_name in xyzs: |
31 | | - xyz = tar.extractfile(xyz_name) |
32 | | - comp = Compound(xyz=xyz) |
33 | | - compounds.append(comp) |
| 29 | + energies = np.array(energies) |
34 | 30 |
|
35 | | -train_compounds = compounds[:training_set_size] |
36 | | -test_compounds = compounds[training_set_size:] |
| 31 | + compounds = [] |
| 32 | + with tarfile.open("../../tests/test_data/qm7.tar.gz") as tar: |
| 33 | + for xyz_name in xyzs: |
| 34 | + xyz = tar.extractfile(xyz_name) |
| 35 | + comp = Compound(xyz=xyz) |
| 36 | + compounds.append(comp) |
37 | 37 |
|
38 | | -train_quantities = energies[:training_set_size] |
39 | | -test_quantities = energies[training_set_size:] |
| 38 | + train_compounds = compounds[:training_set_size] |
| 39 | + test_compounds = compounds[training_set_size:] |
40 | 40 |
|
41 | | -# using "shift_quantites=True" means using dressed atom approach; requires defining `possible_nuclear_charges` though. |
42 | | -model = SORFLocalModel(shift_quantities=True, possible_nuclear_charges=np.array([1, 6, 7, 8, 16])) |
| 41 | + train_quantities = energies[:training_set_size] |
| 42 | + test_quantities = energies[training_set_size:] |
43 | 43 |
|
44 | | -model.train(training_compounds=train_compounds, training_quantities=train_quantities) |
| 44 | + possible_nuclear_charges = np.array([1, 6, 7, 8, 16]) |
| 45 | + FCHL19Calc = FCHL19Calculator(elements=possible_nuclear_charges) |
| 46 | + # using "shift_quantites=True" means using dressed atom approach; requires defining `possible_nuclear_charges` though. |
| 47 | + # in this example we will use FCHL19; note that it requires defining "elements" keyword argument defining nuclear charges that can be encountered in the chemical space of interest. |
| 48 | + model = SORFLocalModel( |
| 49 | + shift_quantities=True, |
| 50 | + possible_nuclear_charges=possible_nuclear_charges, |
| 51 | + representation_function=FCHL19Calc, |
| 52 | + ) |
45 | 53 |
|
46 | | -print("Optimized sigma:", model.sigma) |
47 | | -print("Optimized l2reg divided by average kernel element:", model.l2reg_diag_ratio) |
| 54 | + model.train(training_compounds=train_compounds, training_quantities=train_quantities) |
48 | 55 |
|
49 | | -predictions = model.predict_from_compounds(test_compounds) |
50 | | -print("Prediction MAE:", MAE()(predictions - test_quantities)) |
51 | | -print("Test set quantity STD:", np.std(test_quantities)) |
| 56 | + print("Optimized sigma:", model.sigma) |
| 57 | + print("Optimized l2reg divided by average kernel element:", model.l2reg_diag_ratio) |
| 58 | + |
| 59 | + predictions = model.predict_from_compounds(test_compounds) |
| 60 | + print("Prediction MAE:", MAE()(predictions - test_quantities)) |
| 61 | + print("Test set quantity STD:", np.std(test_quantities)) |
| 62 | + |
| 63 | + |
| 64 | +if __name__ == "__main__": |
| 65 | + main() |
0 commit comments