encyclopedia.py 20.2 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
# Copyright 2018 Markus Scheidgen
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#   http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an"AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

'''
The encyclopedia API of the nomad@FAIRDI APIs.
'''
18
import re
19
import math
20

21
22
from flask_restplus import Resource, abort, fields, marshal
from flask import request
23
from elasticsearch_dsl import Search, Q, A
24
25

from nomad import config
26
from nomad.units import ureg
Lauri Himanen's avatar
Lauri Himanen committed
27
from nomad.atomutils import get_hill_decomposition
28
from .api import api
29
30

ns = api.namespace('encyclopedia', description='Access encyclopedia metadata.')
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
re_formula = re.compile(r"([A-Z][a-z]?)(\d*)")


def add_result(result, key, function, default=""):
    """Convenience function that attempts to add a value from the ElasticSearch
    result into the given result object. Upon failing returns the specified
    default value.
    """
    try:
        value = function()
    except Exception:
        value = default
    result[key] = value


def get_material(es_doc):
47
48
    """Used to form a material definition for "materials/<material_id>" from
    the given ElasticSearch root document.
49
50
    """
    result = {}
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
    # General
    add_result(result, "material_id", lambda: es_doc.encyclopedia.material.material_id, None),
    add_result(result, "formula", lambda: es_doc.encyclopedia.material.formula, None)
    add_result(result, "formula_reduced", lambda: es_doc.encyclopedia.material.formula_reduced, None)
    add_result(result, "system_type", lambda: es_doc.encyclopedia.material.material_type, None)

    # Bulk only
    add_result(result, "has_free_wyckoff_parameters", lambda: es_doc.encyclopedia.material.bulk.has_free_wyckoff_parameters, None)
    add_result(result, "strukturbericht_designation", lambda: es_doc.encyclopedia.material.bulk.strukturbericht_designation, None)
    add_result(result, "material_name", lambda: es_doc.encyclopedia.material.material_name, None)
    add_result(result, "bravais_lattice", lambda: es_doc.encyclopedia.material.bulk.bravais_lattice, None),
    add_result(result, "crystal_system", lambda: es_doc.encyclopedia.material.bulk.crystal_system, None)
    add_result(result, "point_group", lambda: es_doc.encyclopedia.material.bulk.point_group, None)
    add_result(result, "space_group_number", lambda: es_doc.encyclopedia.material.bulk.space_group_number, None)
    add_result(result, "space_group_international_short_symbol", lambda: es_doc.encyclopedia.material.bulk.space_group_international_short_symbol, None)
    add_result(result, "structure_prototype", lambda: es_doc.encyclopedia.material.bulk.structure_prototype, None)
    add_result(result, "structure_type", lambda: es_doc.encyclopedia.material.bulk.structure_type, None)
68
69
70
71
72
73
74

    return result


material_query = api.parser()
material_query.add_argument('material_id', type=str, help='Identifier for the searched material.', location='args')
material_result = api.model('material_result', {
75
76
    # General
    "material_id": fields.String,
77
78
    "formula": fields.String,
    "formula_reduced": fields.String,
79
80
81
82
    "system_type": fields.String,
    # Bulk only
    "has_free_wyckoff_parameters": fields.String,
    "strukturbericht_designation": fields.String,
83
    "material_name": fields.String,
84
85
    "bravais_lattice": fields.String,
    "crystal_system": fields.String,
86
    "point_group": fields.String,
87
88
89
    "space_group_number": fields.Integer,
    "space_group_international_short_symbol": fields.String,
    "structure_prototype": fields.String,
90
91
    "structure_type": fields.String,
})
92
93
94
95
96
97


@ns.route('/materials/<string:material_id>')
class EncMaterialResource(Resource):
    @api.response(404, 'The material does not exist')
    @api.response(200, 'Metadata send', fields.Raw)
98
99
    @api.doc('material/<material_id>')
    @api.expect(material_query)
100
    @api.marshal_with(material_result, skip_none=True)
101
102
103
    def get(self, material_id):
        """Used to retrive basic information related to the specified material.
        """
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
        # Find the first public entry with this material id and take
        # information from there. In principle all other entries should have
        # the same information.
        s = Search(index=config.elastic.index_name)

        # Since we are looking for an exact match, we use filter context
        # together with term search for speed (instead of query context and
        # match search)
        query = Q(
            'bool',
            filter=[
                Q('term', published=True),
                Q('term', with_embargo=False),
                Q('term', encyclopedia__material__material_id=material_id),
            ]
        )
        s = s.query(query)
121
122
123
124
125
126
127

        # The query is collapsed already on the ES side so we don't need to
        # transfer so much data.
        s = s.extra(**{
            "collapse": {"field": "encyclopedia.material.material_id"},
        })

128
129
        response = s.execute()

130
        # No such material
131
132
133
        if len(response) == 0:
            abort(404, message='There is no material {}'.format(material_id))

134
        # Create result JSON
135
        entry = response[0]
136
        result = get_material(entry)
137

138
139
140
141
142
143
144
145
146
147
148
        return result, 200


range_query = api.model('range_query', {
    "max": fields.Float,
    "min": fields.Float,
})
materials_query = api.model('materials_input', {
    'search_by': fields.Nested(api.model('search_query', {
        "exclusive": fields.Boolean(default=False),
        "formula": fields.String,
Lauri Himanen's avatar
Lauri Himanen committed
149
        "element": fields.String,
150
151
152
        "page": fields.Integer(default=1),
        "per_page": fields.Integer(default=25),
        "pagination": fields.Boolean,
153
        "mode": fields.String(default="collapse"),
154
155
156
    })),
    'material_name': fields.List(fields.String),
    'structure_type': fields.List(fields.String),
157
    'space_group_number': fields.List(fields.Integer),
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
    'system_type': fields.List(fields.String),
    'crystal_system': fields.List(fields.String),
    'band_gap': fields.Nested(range_query, description="Band gap range in eV."),
    'band_gap_direct': fields.Boolean,
    'has_band_structure': fields.Boolean,
    'has_dos': fields.Boolean,
    'has_fermi_surface': fields.Boolean,
    'has_thermal_properties': fields.Boolean,
    'functional_type': fields.List(fields.String),
    'basis_set_type': fields.List(fields.String),
    'code_name': fields.List(fields.String),
    'mass_density': fields.Nested(range_query, description="Mass density range in kg / m ** 3."),
})
materials_result = api.model('materials_result', {
    'total_results': fields.Integer(allow_null=False),
173
174
175
176
177
178
179
180
    'results': fields.List(fields.Nested(material_result)),
    'pages': fields.Nested(api.model("page_info", {
        "per_page": fields.Integer,
        "total": fields.Integer,
        "page": fields.Integer,
        "pages": fields.Integer,
    })),
    'es_query': fields.String(allow_null=False),
181
182
183
184
185
186
187
188
189
})


@ns.route('/materials')
class EncMaterialsResource(Resource):
    @api.response(404, 'No materials found')
    @api.response(400, 'Bad request')
    @api.response(200, 'Metadata send', fields.Raw)
    @api.expect(materials_query, validate=False)
190
    @api.marshal_with(materials_result, skip_none=True)
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
    @api.doc('materials')
    def post(self):
        """Used to query a list of materials with the given search options.
        """
        # Get query parameters as json
        try:
            data = marshal(request.get_json(), materials_query)
        except Exception as e:
            abort(400, message=str(e))

        filters = []
        must_nots = []
        musts = []

        # Add term filters
        filters.append(Q('term', published=True))
        filters.append(Q('term', with_embargo=False))

        def add_terms_filter(source, target, query_type="terms"):
            if data[source]:
                filters.append(Q(query_type, **{target: data[source]}))

        add_terms_filter("material_name", "encyclopedia.material.material_name")
        add_terms_filter("structure_type", "encyclopedia.material.bulk.structure_type")
215
        add_terms_filter("space_group_number", "encyclopedia.material.bulk.space_group_number")
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
        add_terms_filter("system_type", "encyclopedia.material.material_type")
        add_terms_filter("crystal_system", "encyclopedia.material.bulk.crystal_system")
        add_terms_filter("band_gap_direct", "encyclopedia.properties.band_gap_direct", query_type="term")
        add_terms_filter("functional_type", "encyclopedia.method.functional_type")
        add_terms_filter("basis_set_type", "dft.basis_set")
        add_terms_filter("code_name", "dft.code_name")

        # Add exists filters
        def add_exists_filter(source, target):
            param = data[source]
            if param is not None:
                query = Q("exists", field=target)
                if param is True:
                    filters.append(query)
                elif param is False:
                    must_nots.append(query)

        add_exists_filter("has_thermal_properties", "encyclopedia.properties.thermodynamical_properties")
        add_exists_filter("has_band_structure", "encyclopedia.properties.electronic_band_structure")
        add_exists_filter("has_dos", "encyclopedia.properties.electronic_dos")
        add_exists_filter("has_fermi_surface", "encyclopedia.properties.fermi_surface")

        # Add range filters
        def add_range_filter(source, target, source_unit=None, target_unit=None):
            param = data[source]
            query_dict = {}
            if param["min"] is not None:
                if source_unit is None and target_unit is None:
                    gte = param["min"]
                else:
                    gte = (param["min"] * source_unit).to(target_unit).magnitude
                query_dict["gte"] = gte
            if param["max"] is not None:
                if source_unit is None and target_unit is None:
                    lte = param["max"]
                else:
                    lte = (param["max"] * source_unit).to(target_unit).magnitude
                query_dict["lte"] = lte
            if len(query_dict) != 0:
                query = Q("range", **{target: query_dict})
                filters.append(query)

        add_range_filter("band_gap", "encyclopedia.properties.band_gap", ureg.eV, ureg.J)
        add_range_filter("mass_density", "encyclopedia.properties.mass_density")
260

261
262
        # Create query for elements or formula
        search_by = data["search_by"]
263
        mode = search_by["mode"]
264
        formula = search_by["formula"]
Lauri Himanen's avatar
Lauri Himanen committed
265
        elements = search_by["element"]
266
267
268
        exclusive = search_by["exclusive"]

        if formula is not None:
Lauri Himanen's avatar
Lauri Himanen committed
269
270
            # Here we determine a list of atom types. The types may occur
            # multiple times and at multiple places.
271
272
273
274
275
276
277
278
279
280
            element_list = []
            matches = re_formula.finditer(formula)
            for match in matches:
                groups = match.groups()
                symbol = groups[0]
                count = groups[1]
                if symbol != "":
                    if count == "":
                        element_list.append(symbol)
                    else:
Lauri Himanen's avatar
Lauri Himanen committed
281
282
283
284
285
                        element_list += [symbol] * int(count)

            # The given list of species is reformatted with the Hill system
            # into a query string. The counts are reduced by the greatest
            # common divisor.
286
            names, reduced_counts = get_hill_decomposition(element_list, reduced=True)
Lauri Himanen's avatar
Lauri Himanen committed
287
288
289
290
291
            query_string = []
            for name, count in zip(names, reduced_counts):
                if count == 1:
                    query_string.append(name)
                else:
292
                    query_string.append("{}{}".format(name, int(count)))
Lauri Himanen's avatar
Lauri Himanen committed
293
            query_string = " ".join(query_string)
294
295
296

            # With exclusive search we look for exact match
            if exclusive:
Lauri Himanen's avatar
Lauri Himanen committed
297
                filters.append(Q("term", **{"encyclopedia.material.species_and_counts.keyword": query_string}))
298
299
300
301
302
            # With non-exclusive search we look for match that includes at
            # least all parts of the formula, possibly even more.
            else:
                musts.append(Q(
                    "match",
Lauri Himanen's avatar
Lauri Himanen committed
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
                    encyclopedia__material__species_and_counts={"query": query_string, "operator": "and"}
                ))
        elif elements is not None:
            # The given list of species is reformatted with the Hill system into a query string
            species, _ = get_hill_decomposition(elements.split(","))
            query_string = " ".join(species)

            # With exclusive search we look for exact match
            if exclusive:
                filters.append(Q("term", **{"encyclopedia.material.species.keyword": query_string}))
            # With non-exclusive search we look for match that includes at
            # least all species, possibly even more.
            else:
                musts.append(Q(
                    "match",
                    encyclopedia__material__species={"query": query_string, "operator": "and"}
319
320
                ))

321
322
323
324
325
326
327
328
        page = search_by["page"]
        per_page = search_by["per_page"]
        bool_query = Q(
            'bool',
            filter=filters,
            must_not=must_nots,
            must=musts,
        )
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
        # 1: The paginated approach: No way to know the amount of matches,
        # but can return aggregation results in a quick fashion including
        # the number of matches entries per material.
        if mode == "aggregate":
            after = None
            # The loop is awkward, but emulates the old behaviour until the GUI is adapted.
            for _ in range(page):

                # The top query filters out entries based on the user query
                s = Search(index=config.elastic.index_name)
                s = s.query(bool_query)

                # The materials are grouped by using three aggregations:
                # 'Composite' to enable scrolling, 'Terms' to enable selecting by
                # material_id and "Top Hits" to fetch a single representative
                # material document.
                terms_agg = A("terms", field="encyclopedia.material.material_id")
                composite_kwargs = {"sources": {"materials": terms_agg}, "size": per_page}
                if after is not None:
                    composite_kwargs['after'] = after
                composite_agg = A("composite", **composite_kwargs)
                composite_agg.metric('representative', A('top_hits', size=1))
                s.aggs.bucket("materials", composite_agg)

                # We ignore the top level hits
                s = s.extra(**{
                    "size": 0,
                })

                response = s.execute()
                materials = response.aggs.materials.buckets
                if len(materials) == 0:
                    abort(404, message='No materials found for the given search criteria or pagination.')
                after = response.aggs.materials["after_key"]

            # Gather results from aggregations
            result_list = []
            materials = response.aggs.materials.buckets
            for material in materials:
                representative = material["representative"][0]
                mat_dict = get_material(representative)
                mat_dict["n_of_calculations"] = material.doc_count
                result_list.append(mat_dict)

            # Page information is incomplete for aggregations
            pages = {
                "page": page,
                "per_page": per_page,
            }
        # 2. Collapse approach. Quickly provides a list of materials
        # corresponding to the query, offers full pagination, doesn't include
        # the number of matches per material.
        elif mode == "collapse":
            s = Search(index=config.elastic.index_name)
            s = s.query(bool_query)
            s = s.extra(**{
                "collapse": {"field": "encyclopedia.material.material_id"},
                "size": per_page,
                "from": (page - 1) * per_page,
            })

            # Execute query
            response = s.execute()

            # No matches
            if len(response) == 0:
                abort(404, message='No materials found for the given search criteria or pagination.')

            # Loop over materials
            result_list = []
            for material in response:
                mat_result = get_material(material)
                result_list.append(mat_result)

            # Full page information available for collapse
            pages = {
                "page": page,
                "per_page": per_page,
                "pages": math.ceil(response.hits.total / per_page),
                "total": response.hits.total,
            }
411
412
413

        result = {
            "results": result_list,
414
415
416
            "total_results": len(result_list),
            "es_query": s.to_dict(),
            "pages": pages,
417
        }
418
        return result, 200
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435


group_result = api.model('group_result', {
    "calculation_list": fields.List(fields.String),
    "energy_minimum": fields.Float,
    "group_hash": fields.String,
    "group_type": fields.String,
    "method_hash": fields.String,
    "nr_of_calculations": fields.Integer,
    "representative_calculation_id": fields.String,
})
groups_result = api.model('groups_result', {
    'total_groups': fields.Integer(allow_null=False),
    'groups': fields.List(fields.Nested(group_result)),
})


Lauri Himanen's avatar
Lauri Himanen committed
436
437
438
439
440
441
442
@ns.route('/materials/<string:material_id>/groups')
class EncGroupsResource(Resource):
    @api.response(404, 'Material not found')
    @api.response(400, 'Bad request')
    @api.response(200, 'Metadata send', fields.Raw)
    @api.expect(material_query, validate=False)
    @api.marshal_with(groups_result, skip_none=True)
443
    @api.doc('enc_materials')
Lauri Himanen's avatar
Lauri Himanen committed
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
    def get(self, material_id):

        def pipeline(hash_key, group_type, minsize):
            return [
                {"$match": {
                    "encyclopedia.material.material_id": material_id,
                    "published": True,
                    "with_embargo": False,
                }},
                {"$lookup": {
                    "from": "energies",
                    "localField": "_id",
                    "foreignField": "calc_id",
                    "as": "energy"
                }},
                {"$unwind": "$energy"},
                {"$match": {"energy.e_kind": "Total E"}},
                {"$sort": {"energy.e_val": 1}},
                {"$group": {
                    "_id": {
                        "group_hash": "$%s" % hash_key
                    },
                    "calculations_list": {"$push": "$_id"},
                    "minimum": {"$first": {"energy": "$energy.e_val", "calc_id": "$_id"}}
                }},
                {"$addFields": {"nr_of_calculations": {"$size": "$calculations_list"}}},
                {"$match": {"nr_of_calculations": {"$gt": minsize}}},
                {"$project": {
                    "_id": False,
                    "nr_of_calculations": True,
                    "calculations_list": True,
                    "energy_minimum": "$minimum.energy",
                    "representative_calculation_id": "$minimum.calc_id",
                    "group_hash": "$_id.group_hash",
                    "group_type": group_type,
                    "material_hash": material_id
                }}
            ]

        # Find EOS groups
        s = Search(index=config.elastic.index_name)
485
        s.aggs.pipeline("eos_pipeline", processors=pipeline("group_eos_hash", "equation of state", 4))
Lauri Himanen's avatar
Lauri Himanen committed
486
        eos_groups = s.execute()
487
488

        # Find convergence groups
Lauri Himanen's avatar
Lauri Himanen committed
489
        s = Search(index=config.elastic.index_name)
490
        s.aggs.pipeline("convergence_pipeline", processors=pipeline("group_parametervariation_hash", "parameter variation", 2))
Lauri Himanen's avatar
Lauri Himanen committed
491
        convergence_groups = s.execute()
492
493

        # Combine both groups
Lauri Himanen's avatar
Lauri Himanen committed
494
495
496
497
498
499
        groups = eos_groups + convergence_groups
        for group in groups:
            group["calculations_list"] = [int(calc) for calc in group["calculations_list"]]
            group["representative_calculation_id"] = int(group["representative_calculation_id"])

        return dict(groups=groups, total_groups=len(groups)), 200