test_parsing.py 12.3 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
# 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.

15
16
from io import StringIO
import json
17
import numpy as np
18
import pytest
19
import os
20
21

from nomadcore.local_meta_info import loadJsonFile
22
import nomad_meta_info
23

24
from nomad import utils, files
25
from nomad.parsing import JSONStreamWriter, parser_dict, match_parser, BrokenParser
26
from nomad.parsing import LocalBackend, BadContextURI
27

28
parser_examples = [
29
    ('parsers/random', 'test/data/parsers/random_0'),
30
    ('parsers/template', 'tests/data/parsers/template.json'),
31
32
    ('parsers/exciting', 'tests/data/parsers/exciting/Ag/INFO.OUT'),
    ('parsers/exciting', 'tests/data/parsers/exciting/GW/INFO.OUT'),
33
34
    ('parsers/exciting', 'tests/data/parsers/exciting/nitrogen/INFO.OUT_nitrogen'),
    ('parsers/exciting', 'tests/data/parsers/exciting/nitrogen/INFO.OUT_carbon'),
Markus Scheidgen's avatar
Markus Scheidgen committed
35
    ('parsers/vasp', 'tests/data/parsers/vasp/vasp.xml'),
36
    ('parsers/vasp', 'tests/data/parsers/vasp_compressed/vasp.xml.gz'),
37
    ('parsers/vaspoutcar', 'tests/data/parsers/vasp_outcar/OUTCAR'),
speckhard's avatar
speckhard committed
38
    ('parsers/fhi-aims', 'tests/data/parsers/fhi-aims/aims.out'),
39
    ('parsers/cp2k', 'tests/data/parsers/cp2k/si_bulk8.out'),
40
41
    ('parsers/crystal', 'tests/data/parsers/crystal/si.out'),
    ('parsers/cpmd', 'tests/data/parsers/cpmd/geo_output.out'),
speckhard's avatar
speckhard committed
42
    ('parsers/nwchem', 'tests/data/parsers/nwchem/single_point/output.out'),
43
    ('parsers/bigdft', 'tests/data/parsers/bigdft/n2_output.out'),
44
    ('parsers/wien2k', 'tests/data/parsers/wien2k/AlN/AlN_ZB.scf'),
45
    # ('parsers/band', 'tests/data/parsers/band_adf.out'),  # causes spglib to segfault
46
    ('parsers/gaussian', 'tests/data/parsers/gaussian/aniline.out'),
Daniel Speckhard's avatar
Daniel Speckhard committed
47
    ('parsers/abinit', 'tests/data/parsers/abinit/Fe.out'),
Daniel Speckhard's avatar
Daniel Speckhard committed
48
    ('parsers/quantumespresso', 'tests/data/parsers/quantum-espresso/benchmark.out'),
49
    ('parsers/orca', 'tests/data/parsers/orca/orca3dot2706823.out'),
Daniel Speckhard's avatar
Daniel Speckhard committed
50
    ('parsers/castep', 'tests/data/parsers/castep/BC2N-Pmm2-Raman.castep'),
Daniel Speckhard's avatar
Daniel Speckhard committed
51
    # ('parsers/dl-poly', 'tests/data/parsers/dl-poly/OUTPUT'), # timeout on Matid System Classification
Daniel Speckhard's avatar
Daniel Speckhard committed
52
    ('parsers/lib-atoms', 'tests/data/parsers/lib-atoms/gp.xml'),
Daniel Speckhard's avatar
Daniel Speckhard committed
53
    ('parsers/octopus', 'tests/data/parsers/octopus/stdout.txt'),
Markus Scheidgen's avatar
Markus Scheidgen committed
54
    ('parsers/phonopy', 'tests/data/parsers/phonopy/control.in'),
Daniel Speckhard's avatar
Daniel Speckhard committed
55
56
    ('parsers/gpaw', 'tests/data/parsers/gpaw/Fe2.gpw'),
    ('parsers/gpaw2', 'tests/data/parsers/gpaw2/H2_lcao.gpw2'),
Daniel Speckhard's avatar
Daniel Speckhard committed
57
    ('parsers/atk', 'tests/data/parsers/atk/Si2.nc'),
58
59
    ('parsers/gulp', 'tests/data/parsers/gulp/example6.got'),
    ('parsers/siesta', 'tests/data/parsers/siesta/Fe/out'),
Markus Scheidgen's avatar
Markus Scheidgen committed
60
    ('parsers/elk', 'tests/data/parsers/elk/Al/INFO.OUT'),
61
62
    ('parsers/elastic', 'dependencies/parsers/elastic/test/examples/2nd/INFO_ElaStic'),  # 70Mb file 2big4git
    ('parsers/turbomole', 'tests/data/parsers/turbomole/acrolein.out'),
Daniel Speckhard's avatar
Daniel Speckhard committed
63
    ('parsers/gamess', 'tests/data/parsers/gamess/exam01.out')
Daniel Speckhard's avatar
Daniel Speckhard committed
64
]
65

66
67
# We need to remove some cases with external mainfiles, which might not exist
# in all testing environments (e.g. in the nomad docker image)
Markus Scheidgen's avatar
Markus Scheidgen committed
68
fixed_parser_examples = []
69
70
71
for parser, mainfile in parser_examples:
    if os.path.exists(mainfile) or mainfile.startswith('tests'):
        fixed_parser_examples.append((parser, mainfile))
Markus Scheidgen's avatar
Markus Scheidgen committed
72
parser_examples = fixed_parser_examples
73

74

75
correct_num_output_files = 37
76

Daniel Speckhard's avatar
Daniel Speckhard committed
77

78
79
80
81
class TestLocalBackend(object):

    @pytest.fixture(scope='session')
    def meta_info(self):
82
83
        file_dir = os.path.dirname(os.path.abspath(nomad_meta_info.__file__))
        path = os.path.join(file_dir, 'all.nomadmetainfo.json')
84
85
86
87
88
89
90
        meta_info, _ = loadJsonFile(path)
        return meta_info

    @pytest.fixture(scope='function')
    def backend(self, meta_info):
        return LocalBackend(meta_info, debug=True)

91
    def test_meta_info(self, meta_info, no_warn):
92
93
        assert 'section_topology' in meta_info

94
    def test_section(self, backend, no_warn):
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
        g_index = backend.openSection('section_run')
        assert g_index == 0
        backend.addValue('program_name', 't0')
        backend.closeSection('section_run', 0)

        g_index = backend.openSection('section_run')
        assert g_index == 1

        g_index = backend.openSection('section_run')
        assert g_index == 2

        backend.addValue('program_name', 't1', 1)
        backend.addValue('program_name', 't2', 2)

        backend.closeSection('section_run', 1)
        backend.closeSection('section_run', 2)

        assert backend.get_sections('section_run') == [0, 1, 2]
        for i in range(0, 3):
            assert backend.get_value('program_name', i) == 't%d' % i

116
117
118
119
120
121
122
123
124
125
126
127
128
129
    def test_section_override(self, backend, no_warn):
        """ Test whether we can overwrite values already in the backend."""
        expected_value = ['Cl', 'Zn']
        backend.openSection('section_run')
        backend.openSection('section_system')
        backend.addArrayValues('atom_labels', np.array(['Al', 'Zn']))
        backend.addArrayValues('atom_labels', np.array(expected_value), override=True)
        backend.closeSection('section_system', 0)

        backend.closeSection('section_run', 0)
        output = StringIO()
        backend.write_json(output)
        assert backend.get_value('atom_labels').tolist() == expected_value

130
131
132
133
134
135
    def test_two_sections(self, backend, no_warn):
        g_index = backend.openSection('section_run')
        assert g_index == 0
        backend.addValue('program_name', 't0')
        backend.closeSection('section_run', 0)

136
        g_index = backend.openSection('section_entry_info')
137
138
        assert g_index == 0
        backend.addValue('parser_name', 'p0')
139
        backend.closeSection('section_entry_info', 0)
140
141

        assert backend.get_sections('section_run') == [0]
142
        assert backend.get_sections('section_entry_info') == [0]
143
144
145
146
147

        output = StringIO()
        backend.write_json(output)
        archive = json.loads(output.getvalue())
        assert 'section_run' in archive
148
        assert 'section_entry_info' in archive
149

150
    def test_subsection(self, backend: LocalBackend, no_warn):
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
        backend.openSection('section_run')
        backend.openSection('section_method')
        backend.closeSection('section_method', -1)

        backend.openSection('section_method')
        backend.closeSection('section_method', -1)

        backend.openSection('section_run')
        backend.closeSection('section_run', 0)
        backend.closeSection('section_run', 1)

        backend.openSection('section_method')
        backend.closeSection('section_method', -1)

        runs = backend.data['section_run']
        assert len(runs) == 2
        assert len(runs[0]['section_method']) == 2
        assert len(runs[1]['section_method']) == 1

170
    def test_context(self, backend: LocalBackend, no_warn):
171
172
173
174
175
        backend.openSection('section_run')
        backend.openSection('section_method')
        backend.closeSection('section_method', -1)
        backend.closeSection('section_run', -1)

176
177
        backend.openSection('section_run')
        backend.closeSection('section_run', -1)
178

179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
        backend.openContext('/section_run/0')
        backend.addValue('program_name', 't1')
        backend.closeContext('/section_run/0')

        backend.openContext('/section_run/1')
        backend.addValue('program_name', 't2')
        backend.closeContext('/section_run/1')

        backend.openContext('/section_run/0/section_method/0')
        backend.closeContext('/section_run/0/section_method/0')

        runs = backend.data['section_run']
        assert runs[0]['program_name'] == 't1'
        assert runs[1]['program_name'] == 't2'

194
    def test_multi_context(self, backend: LocalBackend, no_warn):
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
        backend.openSection('section_run')
        backend.closeSection('section_run', -1)

        backend.openContext('/section_run/0')
        backend.openSection('section_method')
        backend.closeSection('section_method', -1)
        backend.closeContext('/section_run/0')

        backend.openContext('/section_run/0')
        backend.openSection('section_method')
        backend.closeSection('section_method', -1)
        backend.closeContext('/section_run/0')

        assert len(backend.data['section_method']) == 1

210
    def test_bad_context(self, backend: LocalBackend, no_warn):
211
212
213
214
215
216
217
218
219
220
221
222
        try:
            backend.openContext('section_run/0')
            assert False
        except BadContextURI:
            pass

        try:
            backend.openContext('dsfds')
            assert False
        except BadContextURI:
            pass

223
224
225
226
227
228
229
230
231

def create_reference(data, pretty):
    if (pretty):
        return json.dumps(data, indent=2)
    else:
        return json.dumps(data, separators=(',', ':'))


@pytest.mark.parametrize("pretty", [False, True])
232
def test_stream_generator(pretty, no_warn):
233
234
235
236
237
238
239
240
241
242
243
244
245
    example_data = [
        {
            'key1': 'value',
            'key2': 1
        },
        {
            'key': {
                'key': 'value'
            }
        }
    ]

    out = StringIO()
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
    writer = JSONStreamWriter(out, pretty=pretty)
    writer.open_array()
    writer.open_object()
    writer.key('key1')
    writer.value('value')
    writer.key('key2')
    writer.value(1)
    writer.close_object()
    writer.open_object()
    writer.key('key')
    writer.open_object()
    writer.key('key')
    writer.value('value')
    writer.close_object()
    writer.close_object()
    writer.close_array()
    writer.close()
263
264
265

    assert create_reference(example_data, pretty) == out.getvalue()

266

267
268
def assert_parser_result(backend):
    status, errors = backend.status
269
270
    assert status == 'ParseSuccess'
    assert errors is None or len(errors) == 0
271
272


Daniel Speckhard's avatar
Daniel Speckhard committed
273
274
275
276
277
def assert_parser_dir_unchanged(previous_wd, current_wd):
    """Assert working directory has not been changed from parser."""
    assert previous_wd == current_wd


278
279
def run_parser(parser_name, mainfile):
    parser = parser_dict[parser_name]
280
    result = parser.run(mainfile, logger=utils.get_logger(__name__))
281
    return add_calculation_info(result, parser_name=parser_name)
Markus Scheidgen's avatar
Markus Scheidgen committed
282
283


284
285
286
@pytest.fixture
def parsed_vasp_example() -> LocalBackend:
    return run_parser(
287
        'parsers/vasp', 'dependencies/parsers/vasp/test/examples/xml/perovskite.xml')
Markus Scheidgen's avatar
Markus Scheidgen committed
288
289


290
291
292
293
294
295
@pytest.fixture
def parsed_template_example() -> LocalBackend:
    return run_parser(
        'parsers/template', 'tests/data/parsers/template.json')


296
297
298
# Function used by normalizer tests.
def parse_file(parser_name_and_mainfile) -> LocalBackend:
    parser_name, mainfile = parser_name_and_mainfile
299
300
301
    return run_parser(parser_name, mainfile)


302
303
304
@pytest.fixture(params=parser_examples, ids=lambda spec: '%s-%s' % spec)
def parsed_example(request) -> LocalBackend:
    parser_name, mainfile = request.param
Markus Scheidgen's avatar
Markus Scheidgen committed
305
306
    result = run_parser(parser_name, mainfile)
    return result
307
308


309
def add_calculation_info(backend: LocalBackend, **kwargs) -> LocalBackend:
310
    backend.openNonOverlappingSection('section_entry_info')
311
    backend.addValue('upload_id', 'test_upload_id')
312
313
    backend.addValue('calc_id', 'test_calc_id')
    backend.addValue('calc_hash', 'test_calc_hash')
314
    backend.addValue('mainfile', 'test/mainfile.txt')
315
316
    for key, value in kwargs.items():
        backend.addValue(key, value)
317
    backend.closeNonOverlappingSection('section_entry_info')
318
    return backend
319
320


321
@pytest.mark.parametrize('parser_name, mainfile', parser_examples)
322
def test_parser(parser_name, mainfile):
Daniel Speckhard's avatar
Daniel Speckhard committed
323
    previous_wd = os.getcwd()  # Get Working directory before parsing.
324
    parsed_example = run_parser(parser_name, mainfile)
325
    assert_parser_result(parsed_example)
Daniel Speckhard's avatar
Daniel Speckhard committed
326
327
    # Check that cwd has not changed.
    assert_parser_dir_unchanged(previous_wd, current_wd=os.getcwd())
Markus Scheidgen's avatar
Markus Scheidgen committed
328
329


330
331
332
333
334
def test_match(raw_files, no_warn):
    example_upload_id = 'example_upload_id'
    upload_files = files.StagingUploadFiles(example_upload_id, create=True, is_authorized=lambda: True)
    upload_files.add_rawfiles('tests/data/parsers')

335
    count = 0
336
337
    for mainfile in upload_files.raw_file_manifest():
        parser = match_parser(mainfile, upload_files)
338
        if parser is not None and not isinstance(parser, BrokenParser):
339
            count += 1
340

341
    assert count == correct_num_output_files