/
materials.py
281 lines (235 loc) · 9.66 KB
/
materials.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
from datetime import datetime
from itertools import chain
from operator import itemgetter
from typing import Dict, Iterator, List, Optional
from maggma.builders import Builder
from maggma.stores import Store
from pymatgen import Structure
from pymatgen.analysis.structure_analyzer import oxide_type
from pymatgen.analysis.structure_matcher import ElementComparator, StructureMatcher
from emmet.builders.utils import maximal_spanning_non_intersecting_subsets
from emmet.core import SETTINGS
from emmet.core.utils import group_structures, jsanitize
from emmet.core.vasp.calc_types import TaskType
from emmet.core.vasp.material import MaterialsDoc
from emmet.core.vasp.task import TaskDocument
__author__ = "Shyam Dwaraknath <shyamd@lbl.gov>"
class MaterialsBuilder(Builder):
"""
The Materials Builder matches VASP task documents by structure similarity into materials
document. The purpose of this builder is group calculations and determine the best structure.
All other properties are derived from other builders.
The process is as follows:
1.) Find all documents with the same formula
2.) Select only task documents for the task_types we can select properties from
3.) Aggregate task documents based on strucutre similarity
4.) Convert task docs to property docs with metadata for selection and aggregation
5.) Select the best property doc for each property
6.) Build material document from best property docs
7.) Post-process material document
8.) Validate material document
"""
def __init__(
self,
tasks: Store,
materials: Store,
task_validation: Optional[Store] = None,
query: Optional[Dict] = None,
allowed_task_types: Optional[List[str]] = None,
symprec: float = SETTINGS.SYMPREC,
ltol: float = SETTINGS.LTOL,
stol: float = SETTINGS.STOL,
angle_tol: float = SETTINGS.ANGLE_TOL,
**kwargs,
):
"""
Args:
tasks: Store of task documents
materials: Store of materials documents to generate
task_validation: Store for storing task validation results
query: dictionary to limit tasks to be analyzed
allowed_task_types: list of task_types that can be processed
symprec: tolerance for SPGLib spacegroup finding
ltol: StructureMatcher tuning parameter for matching tasks to materials
stol: StructureMatcher tuning parameter for matching tasks to materials
angle_tol: StructureMatcher tuning parameter for matching tasks to materials
"""
self.tasks = tasks
self.materials = materials
self.task_validation = task_validation
self.allowed_task_types = (
[t.value for t in TaskType]
if allowed_task_types is None
else allowed_task_types
)
self._allowed_task_types = {TaskType(t) for t in self.allowed_task_types}
self.query = query if query else {}
self.symprec = symprec
self.ltol = ltol
self.stol = stol
self.angle_tol = angle_tol
self.kwargs = kwargs
sources = [tasks]
if self.task_validation:
sources.append(self.task_validation)
super().__init__(sources=sources, targets=[materials], **kwargs)
def ensure_indexes(self):
"""
Ensures indicies on the tasks and materials collections
"""
# Basic search index for tasks
self.tasks.ensure_index("task_id")
self.tasks.ensure_index("last_updated")
self.tasks.ensure_index("state")
self.tasks.ensure_index("formula_pretty")
# Search index for materials
self.materials.ensure_index("material_id")
self.materials.ensure_index("last_updated")
self.materials.ensure_index("sandboxes")
self.materials.ensure_index("task_ids")
if self.task_validation:
self.task_validation.ensure_index("task_id")
self.task_validation.ensure_index("valid")
def get_items(self) -> Iterator[List[Dict]]:
"""
Gets all items to process into materials documents.
This does no datetime checking; relying on on whether
task_ids are included in the Materials Colection
Returns:
generator or list relevant tasks and materials to process into materials documents
"""
self.logger.info("Materials builder started")
self.logger.info(
f"Allowed task types: {[task_type.value for task_type in self._allowed_task_types]}"
)
self.logger.info("Setting indexes")
self.ensure_indexes()
# Save timestamp to mark buildtime for material documents
self.timestamp = datetime.utcnow()
# Get all processed tasks:
temp_query = dict(self.query)
temp_query["state"] = "successful"
self.logger.info("Finding tasks to process")
all_tasks = {
doc[self.tasks.key]
for doc in self.tasks.query(temp_query, [self.tasks.key])
}
processed_tasks = {
t_id
for d in self.materials.query({}, ["task_ids"])
for t_id in d.get("task_ids", [])
}
to_process_tasks = all_tasks - processed_tasks
to_process_forms = self.tasks.distinct(
"formula_pretty", {self.tasks.key: {"$in": list(to_process_tasks)}}
)
self.logger.info(f"Found {len(to_process_tasks)} unprocessed tasks")
self.logger.info(f"Found {len(to_process_forms)} unprocessed formulas")
# Set total for builder bars to have a total
self.total = len(to_process_forms)
if self.task_validation:
invalid_ids = {
doc[self.tasks.key]
for doc in self.task_validation.query(
{"valid": False}, [self.task_validation.key]
)
}
else:
invalid_ids = set()
projected_fields = [
"last_updated",
"completed_at",
"task_id",
"formula_pretty",
"output.energy_per_atom",
"output.energy",
"output.structure",
"input.parameters",
"orig_inputs",
"input.structure",
"tags",
]
for formula in to_process_forms:
tasks_query = dict(temp_query)
tasks_query["formula_pretty"] = formula
tasks = list(
self.tasks.query(criteria=tasks_query, properties=projected_fields)
)
for t in tasks:
if t[self.tasks.key] in invalid_ids:
t["is_valid"] = False
else:
t["is_valid"] = True
yield tasks
def process_item(self, tasks: List[Dict]) -> List[Dict]:
"""
Process the tasks into a list of materials
Args:
tasks [dict] : a list of task docs
Returns:
([dict],list) : a list of new materials docs and a list of task_ids that were processsed
"""
tasks = [TaskDocument(**task) for task in tasks]
formula = tasks[0].formula_pretty
task_ids = [task.task_id for task in tasks]
self.logger.debug(f"Processing {formula} : {task_ids}")
grouped_tasks = self.filter_and_group_tasks(tasks)
materials = []
for group in grouped_tasks:
try:
materials.append(MaterialsDoc.from_tasks(group))
except Exception:
failed_ids = list({t_.task_id for t_ in group})
self.logger.warn(
f"No valid ids found among ids {failed_ids}. This can be the case if the required "
"calculation types are missing from your tasks database."
)
self.logger.debug(f"Produced {len(materials)} materials for {formula}")
return [mat.dict() for mat in materials]
def update_targets(self, items: List[List[Dict]]):
"""
Inserts the new task_types into the task_types collection
Args:
items ([([dict],[int])]): A list of tuples of materials to update and the corresponding
processed task_ids
"""
items = list(filter(None, chain.from_iterable(items)))
for item in items:
item.update({"_bt": self.timestamp})
material_ids = list({item["material_id"] for item in items})
if len(items) > 0:
self.logger.info(f"Updating {len(items)} materials")
self.materials.remove_docs({self.materials.key: {"$in": material_ids}})
self.materials.update(
docs=jsanitize(items, allow_bson=True),
key=["material_id"],
)
else:
self.logger.info("No items to update")
def filter_and_group_tasks(self, tasks: List[TaskDocument]) -> Iterator[List[Dict]]:
"""
Groups tasks by structure matching
"""
filtered_tasks = [
task
for task in tasks
if any(
allowed_type is task.task_type
for allowed_type in self._allowed_task_types
)
]
structures = []
for idx, task in enumerate(filtered_tasks):
s = task.output.structure
s.index = idx
structures.append(s)
grouped_structures = group_structures(
structures,
ltol=self.ltol,
stol=self.stol,
angle_tol=self.angle_tol,
symprec=self.symprec,
)
for group in grouped_structures:
grouped_tasks = [filtered_tasks[struc.index] for struc in group]
yield grouped_tasks