summaryrefslogtreecommitdiff
path: root/ml_exp/do_ml.py
blob: 758bf01aece9efe4c019c1a86f0875815d86e58c (plain)
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
"""MIT License

Copyright (c) 2019 David Luevano Alvarado

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
"""
import time
import numpy as np
from ml_exp.misc import printc
from ml_exp.kernels import gaussian_kernel
from ml_exp.math import cholesky_solve
from ml_exp.qm7db import qm7db


def simple_ml(descriptors,
              energies,
              training_size,
              test_size=None,
              sigma=1000.0,
              opt=True,
              identifier=None,
              show_msgs=True):
    """
    Basic ML methodology for a single descriptor type.
    descriptors: array of descriptors.
    energies: array of energies.
    training_size: size of the training set to use.
    test_size: size of the test set to use. If no size is given,
        the last remaining molecules are used.
    sigma: depth of the kernel.
    opt: if the optimized algorithm should be used. For benchmarking purposes.
    identifier: string with the name of the descriptor used.
    show_msgs: if debug messages should be shown.
    NOTE: identifier is just a string and is only for identification purposes.
    Also, training is done with the first part of the data and
        testing with the ending part of the data.
    """
    tic = time.perf_counter()
    # Initial calculations for later use.
    data_size = descriptors.shape[0]

    if not identifier:
        identifier = 'NOT SPECIFIED'

    if not data_size == energies.shape[0]:
        raise ValueError('Energies size is different than descriptors size.')

    if training_size >= data_size:
        raise ValueError('Training size is greater or equal to the data size.')

    # If test_size is not set, it is set to a maximum size of 1500.
    if not test_size:
        test_size = data_size - training_size
        if test_size > 1500:
            test_size = 1500

    if show_msgs:
        printc(f'{identifier} ML started.', 'GREEN')
        printc(f'\tTraining size: {training_size}', 'CYAN')
        printc(f'\tTest size: {test_size}', 'CYAN')
        printc(f'\tSigma: {test_size}', 'CYAN')

    X_training = descriptors[:training_size]
    Y_training = energies[:training_size]
    K_training = gaussian_kernel(X_training,
                                 X_training,
                                 sigma,
                                 opt=opt)
    alpha = cholesky_solve(K_training,
                           Y_training)

    X_test = descriptors[-test_size:]
    Y_test = energies[-test_size:]
    K_test = gaussian_kernel(X_test,
                             X_training,
                             sigma,
                             opt=opt)
    Y_predicted = np.dot(K_test,
                         alpha)

    mae = np.mean(np.abs(Y_predicted - Y_test))
    if show_msgs:
        printc(f'\tMAE for {identifier}: {mae:.4f}', 'GREEN')

    toc = time.perf_counter()
    tictoc = toc - tic
    if show_msgs:
        printc(f'\t{identifier} ML took {tictoc:.4f} seconds.', 'GREEN')
        printc(f'\t\tTraining size: {training_size}', 'CYAN')
        printc(f'\t\tTest size: {test_size}', 'CYAN')
        printc(f'\t\tSigma: {sigma}', 'CYAN')

    return mae, tictoc


def do_ml(db_path='data',
          is_shuffled=True,
          r_seed=111,
          diag_value=None,
          lj_sigma=1.0,
          lj_epsilon=1.0,
          use_forces=False,
          stuff='bonds',
          size=23,
          as_eig=True,
          bohr_ru=False,
          training_size=1500,
          test_size=None,
          sigma=1000.0,
          opt=True,
          identifiers=["CM"],
          show_msgs=True):
    """
    Main function that does the whole ML process.
    db_path: path to the database directory.
    is_shuffled: if the resulting list of compounds should be shuffled.
    r_seed: random seed to use for the shuffling.
    diag_value: if special diagonal value is to be used.
    lj_sigma: sigma value.
    lj_epsilon: epsilon value.
    use_forces: if the use of forces instead of k_cx should be used.
    stuff: elements of the bag, by default the known bag of bonds.
    size: compound size.
    as_eig: if the representation should be as the eigenvalues.
    bohr_ru: if radius units should be in bohr's radius units.
    training_size: size of the training set to use.
    test_size: size of the test set to use. If no size is given,
        the last remaining molecules are used.
    sigma: depth of the kernel.
    opt: if the optimized algorithm should be used. For benchmarking purposes.
    identifiers: list of names (strings) of descriptors to use.
    show_msgs: if debug messages should be shown.
    """
    if type(identifiers) != list:
        raise TypeError('\'identifiers\' is not a list.')

    init_time = time.perf_counter()

    # Data reading.
    tic = time.perf_counter()
    compounds, energy_pbe0, energy_delta = qm7db(db_path=db_path,
                                                 is_shuffled=is_shuffled,
                                                 r_seed=r_seed)
    toc = time.perf_counter()
    tictoc = toc - tic
    if show_msgs:
        printc(f'Data reading took {tictoc:.4f} seconds.', 'CYAN')

    # Matrices calculation.
    tic = time.perf_counter()
    for compound in compounds:
        if 'CM' in identifiers:
            compound.gen_cm(size=size,
                            as_eig=as_eig,
                            bohr_ru=bohr_ru)
        if 'LJM' in identifiers:
            compound.gen_ljm(diag_value=diag_value,
                             sigma=lj_sigma,
                             epsilon=lj_epsilon,
                             size=size,
                             as_eig=as_eig,
                             bohr_ru=bohr_ru)
        if 'AM' in identifiers:
            compound.gen_hd(size=size,
                            bohr_ru=bohr_ru)
            compound.gen_am(use_forces=use_forces,
                            size=size)
        if 'BOB' in identifiers:
            compound.gen_bob(size=size)

    # Create a numpy array for the descriptors.
    if 'CM' in identifiers:
        cm_data = np.array([comp.cm for comp in compounds], dtype=float)
    if 'LJM' in identifiers:
        ljm_data = np.array([comp.ljm for comp in compounds], dtype=float)
    if 'AM' in identifiers:
        am_data = np.array([comp.cm for comp in compounds], dtype=float)
    if 'BOB' in identifiers:
        bob_data = np.array([comp.bob for comp in compounds], dtype=float)

    toc = time.perf_counter()
    tictoc = toc - tic
    if show_msgs:
        printc(f'Matrices calculation took {tictoc:.4f} seconds.', 'CYAN')

    # ML calculation.
    if 'CM' in identifiers:
        cm_mae, cm_tictoc = simple_ml(cm_data,
                                      energy_pbe0,
                                      training_size=training_size,
                                      test_size=test_size,
                                      sigma=sigma,
                                      opt=opt,
                                      identifier='CM',
                                      show_msgs=show_msgs)
    if 'LJM' in identifiers:
        ljm_mae, ljm_tictoc = simple_ml(ljm_data,
                                        energy_pbe0,
                                        training_size=training_size,
                                        test_size=test_size,
                                        sigma=sigma,
                                        opt=opt,
                                        identifier='LJM',
                                        show_msgs=show_msgs)
    if 'AM' in identifiers:
        am_mae, am_tictoc = simple_ml(am_data,
                                      energy_pbe0,
                                      training_size=training_size,
                                      test_size=test_size,
                                      sigma=sigma,
                                      opt=opt,
                                      identifier='CM',
                                      show_msgs=show_msgs)
    if 'BOB' in identifiers:
        bob_mae, bob_tictoc = simple_ml(bob_data,
                                        energy_pbe0,
                                        training_size=training_size,
                                        test_size=test_size,
                                        sigma=sigma,
                                        opt=opt,
                                        identifier='CM',
                                        show_msgs=show_msgs)

    # End of program
    end_time = time.perf_counter()
    totaltime = end_time - init_time
    printc(f'Program took {totaltime:.4f} seconds.', 'CYAN')