/
utils.py
252 lines (189 loc) · 7.01 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
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
import hashlib
import importlib
import logging
import torch
from ase.neighborlist import NeighborList
from collections import OrderedDict
def get_hash(image):
"""Get the SHA1 hash of an image object
Parameters
----------
image : object
An image to be hashed.
Returns
-------
_hash : str
Hash of image in string format
"""
string = ""
for atom in image:
string += str(atom)
sha1 = hashlib.sha1(string.encode("utf-8"))
_hash = sha1.hexdigest()
return _hash
def get_neighborlist(image, cutoff):
"""Get the list of neighbors
Parameters
----------
image : object
ASE image.
Returns
-------
A list of neighbors with offset distances.
"""
cutoffs = [cutoff / 2.0] * len(image)
nlist = NeighborList(
cutoffs=cutoffs, self_interaction=False, bothways=True, skin=0.0
)
nlist.update(image)
return [nlist.get_neighbors(index) for index in range(len(image))]
def convert_elapsed_time(seconds):
"""Convert elapsed time in seconds to HH:MM:SS format"""
minutes, seconds = divmod(seconds, 60)
hours, minutes = divmod(minutes, 60)
return int(hours), int(minutes), seconds
def get_chunks(sequence, chunk_size, svm=True):
"""A function that yields a list in chunks
Parameters
----------
sequence : list or dictionary
A list or a dictionary to be split.
chunk_size : int
Number of elements in each group.
svm : bool
Whether or not these chunks are going to be used for kernel methods.
"""
res = []
if svm is False and isinstance(sequence, dict):
sequence = sequence.items()
for item in sequence:
res.append(item)
if len(res) >= chunk_size:
yield res
res = []
if res:
yield res # yield the last, incomplete, portion
def dynamic_import(name, package, alt_name=None):
"""A dynamic module importer
Parameters
----------
name : str
Name of the module to be imported.
package : str
Path to package. Example: ml4chem.fingerprints
alt_name : str
Alternative module_name.
Returns
-------
_class : obj
An class object.
"""
if alt_name is None:
module_name = ".{}".format(name.lower())
else:
module_name = ".{}".format(alt_name.lower())
module = importlib.import_module(module_name, package=package)
imported_class = getattr(module, name)
return imported_class
def logger(filename=None, level=None, format=None, filemode="a"):
"""A wrapper to the logging python module
This module is useful for cases where we need to log in a for loop
different files. It also will allow more flexibility later on how the
logging format could evolve.
Parameters
----------
filename : str, optional
Name of logfile. If no filename is provided, we output to stdout.
level : str, optional
Level of logging messages, by default 'info'. Supported are: 'info'
and 'debug'.
format : str, optional
Format of logging messages, by default '%(message)s'.
filemode : str, optional
If filename is specified, open the file in this mode. Defaults to
"a". Supported modes are: "r" (read), "w" (write), "a" (append).
Returns
-------
logger
A logger object.
"""
levels = {"info": logging.INFO, "debug": logging.DEBUG}
if level is None:
level = levels["info"]
else:
level = levels[level.lower()]
if format is None:
format = "%(message)s"
# https://stackoverflow.com/a/12158233/1995261
for handler in logging.root.handlers[:]:
logging.root.removeHandler(handler)
logger = logging.basicConfig(
filename=filename, level=level, format=format, filemode=filemode
)
return logger
def lod_to_list(data, svm=False, requires_grad=False):
"""List Of Dict (lod) to list
Parameters
----------
data : list
A list with ml4chem dictionaries. Those ones coming from get_chunks()
svm : bool, optional.
Whether or not these chunks are going to be used for kernel methods,
by default False.
requires_grad : bool, optional.
Do we require gradients?, by default False.
Returns
-------
_list
A list of tensors or list of float.
"""
_list = []
for d in data:
t = OrderedDict(d)
vectors = []
for hash in t.keys():
features = t[hash]
for symbol, vector in features:
vectors.append(vector.detach().numpy())
if svm is False:
vectors = torch.tensor(vectors, requires_grad=requires_grad)
else:
vectors = torch.tensor(vectors)
_list.append(vectors)
return _list
def get_number_of_parameters(model):
"""Get the number of parameters
Parameters
----------
model : obj
Pytorch model to perform forward() and get gradients.
Returns
-------
(total_params, train_params) tuple with total number of parameters and
number of trainable parameters.
"""
total_params = sum(p.numel() for p in model.parameters())
train_params = sum(p.numel() for p in model.parameters() if p.requires_grad)
return total_params, train_params
def get_header_message():
"""Function that returns ML4Chem header"""
header = """
===============================================================================
███╗ ███╗██╗██╗ ██╗ ██████╗██╗ ██╗███████╗███╗ ███╗
████╗ ████║██║██║ ██║██╔════╝██║ ██║██╔════╝████╗ ████║
██╔████╔██║██║███████║██║ ███████║█████╗ ██╔████╔██║
██║╚██╔╝██║██║╚════██║██║ ██╔══██║██╔══╝ ██║╚██╔╝██║
██║ ╚═╝ ██║███████╗██║╚██████╗██║ ██║███████╗██║ ╚═╝ ██║
╚═╝ ╚═╝╚══════╝╚═╝ ╚═════╝╚═╝ ╚═╝╚══════╝╚═╝ ╚═╝\n
ML4Chem is Machine Learning for Chemistry and Materials. This package is
written in Python 3, and intends to offer modern and rich features to perform
machine learning workflows for chemical physics.
This project is directed by Muammar El Khatib.
Contributors (in alphabetic order):
-----------------------------------
Elijah Gardella : Interatomic potentials for ionic systems.
Jacklyn Gee : Gaussian features class improvements, and cjson
reader.
===============================================================================
"""
return header