encyclopedia.py 11.6 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

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

from nomad import config
25
26
27
from nomad.units import ureg
from nomad.atomutils import get_hill_decomposition, get_formula_string
from .api import api
28
29

ns = api.namespace('encyclopedia', description='Access encyclopedia metadata.')
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
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):
    """Used to form a material definition from the given ElasticSearch root
    document.
    """
    result = {}
    add_result(result, "material_id", lambda: es_doc.encyclopedia.material.material_id, ""),
    add_result(result, "bravais_lattice", lambda: es_doc.encyclopedia.material.bulk.bravais_lattice, ""),
    add_result(result, "crystal_system", lambda: es_doc.encyclopedia.material.bulk.crystal_system, "")
    add_result(result, "formula", lambda: es_doc.encyclopedia.material.formula, "")
    add_result(result, "formula_reduced", lambda: es_doc.encyclopedia.material.formula_reduced, "")
    add_result(result, "material_name", lambda: es_doc.encyclopedia.material.material_name, "")
    add_result(result, "point_group", lambda: es_doc.encyclopedia.material.bulk.point_group, "")
    add_result(result, "space_group", lambda: es_doc.encyclopedia.material.bulk.space_group_number, "")
    add_result(result, "structure_type", lambda: es_doc.encyclopedia.material.bulk.structure_type, "")
    add_result(result, "system_type", lambda: es_doc.encyclopedia.material.material_type, "")

    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', {
    "bravais_lattice": fields.String,
    "crystal_system": fields.String,
    "formula": fields.String,
    "formula_reduced": fields.String,
    "material_name": fields.String,
    "point_group": fields.String,
    "space_group": fields.Integer(),
    "structure_type": fields.String,
    "system_type": fields.String,
})
77
78
79
80
81
82


@ns.route('/materials/<string:material_id>')
class EncMaterialResource(Resource):
    @api.response(404, 'The material does not exist')
    @api.response(200, 'Metadata send', fields.Raw)
83
84
85
    @api.doc('material/<material_id>')
    @api.expect(material_query)
    @api.marshal_with(material_result)
86
87
88
    def get(self, material_id):
        """Used to retrive basic information related to the specified material.
        """
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106

        # 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)
107
108
        response = s.execute()

109
        # No such material
110
111
112
        if len(response) == 0:
            abort(404, message='There is no material {}'.format(material_id))

113
        # Create result JSON
114
        entry = response[0]
115
        result = get_material(entry)
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
        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,
        "element": fields.List(fields.String),
        "page": fields.Integer(default=1),
        "per_page": fields.Integer(default=25),
        "pagination": fields.Boolean,
    })),
    'material_name': fields.List(fields.String),
    'structure_type': fields.List(fields.String),
    'space_group': fields.List(fields.Integer),
    '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', {
    'pages': fields.Integer(required=True),
    'results': fields.List(fields.Nested(material_result)),
    'total_results': fields.Integer(allow_null=False),
})


@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)
    @api.marshal_with(materials_result)
    @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))

        s = Search(index=config.elastic.index_name)
        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")
        add_terms_filter("space_group", "encyclopedia.material.bulk.space_group_number")
        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")
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
282
283
284
        # Create query for elements or formula
        search_by = data["search_by"]
        formula = search_by["formula"]
        exclusive = search_by["exclusive"]

        if formula is not None:
            # The given formula is reformatted with the Hill system
            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:
                        element_list += [[symbol] * int(count)]
            names, counts = get_hill_decomposition(element_list)

            # With exclusive search we look for exact match
            if exclusive:
                hill_formula = get_formula_string(names, counts)
                filters.append(Q("term", **{"encyclopedia.material.formula": hill_formula}))
            # With non-exclusive search we look for match that includes at
            # least all parts of the formula, possibly even more.
            else:
                parts = ["{}{}".format(name, count) for name, count in zip(names, counts)]
                musts.append(Q(
                    "match",
                    encyclopedia__material__formula_parts={"query": " ".join(parts), "operator": "and"}
                ))

        # Prepare the final boolean query that combines the different queries
        filter_query = Q('bool', filter=filters, must_not=must_nots, must=musts)
        s = s.query(filter_query)

        # Execute query
        response = s.execute()

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

        # Create final result dictionary
        result_list = [get_material(es_doc) for es_doc in response]
        result = {
            "total_results": len(result_list),
            "pages": None,
            "results": result_list,
        }
285
        return result, 200
286
287
288
289
290
291
292
293
294
295

# @ns.route('/esmaterials')
# class EncESMaterialsResource(Resource):
    # @api.response(404, 'No materials found')
    # @api.response(200, 'Metadata send', fields.Raw)
    # @api.doc('materials')
    # def post(self):
        # """Used to query a list of materials with the given ElasticSearch JSON
        # query.
        # """