Commit 29d54775 authored by Markus Scheidgen's avatar Markus Scheidgen
Browse files

Refactoring and logging.

parent 7c97a280
# use glob syntax.
syntax: glob
*.ser
*.class
*~
*.bak
#*.off
*.off
*.old
*.pyc
*.bk
*.swp
.DS_Store
__pycache__/
# logging files
detailed.log
......@@ -54,4 +53,4 @@ lib/
env/
# Setuptools install folder
parser/parser-vasp/vaspparser.egg-info/
vaspparser.egg-info/
# TODO this is still the nomad-coe ci config
stages:
- test
......
......@@ -3,12 +3,12 @@ This is the main repository of the [NOMAD](https://www.nomad-coe.eu/) parser for
# Example
```python
from vaspparser import VASPParser
from vaspparser import VASPRunParserInterface
import matplotlib.pyplot as mpl
# 1. Initialize a parser with a set of default units.
default_units = ["eV"]
parser = VASPParser(default_units=default_units)
parser = VASPRunParserInterface(default_units=default_units)
# 2. Parse a file
path = "path/to/main.file"
......@@ -21,7 +21,7 @@ This is the main repository of the [NOMAD](https://www.nomad-coe.eu/) parser for
```
# Installation
The code is python 2 and python 3 compatible. First download and install
The code is python 3 compatible. First download and install
the nomadcore package:
```sh
......
# Copyright 2016-2018 Fawzi Mohamed, Lauri Himanen, Danio Brambila, Ankit Kariryaa, Henning Glawe
#
# 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.
from vaspparser.parser import VASPParser
# Copyright 2016-2018 Fawzi Mohamed, Lauri Himanen, Danio Brambila, Ankit Kariryaa, Henning Glawe
#
# 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.
from builtins import object
import vaspparser.setup_paths
from nomadcore.simple_parser import mainFunction, SimpleMatcher as SM
from nomadcore.local_meta_info import loadJsonFile, InfoKindEl
from nomadcore.caching_backend import CachingLevel
import os, sys, json, logging
import numpy as np
# description of the input
mainFileDescription = SM(
name = 'root',
weak = True,
startReStr = "",
subMatchers = [
SM(name = 'newRun',
startReStr = r"^\s*vasp.(?:[0-9.]+)\s+(?:[0-9]+[A-Za-z]+[0-9]+)\s+\(build (:[^)]+)\)\s+complex\s*",
repeats = True,
required = True,
forwardMatch = True,
sections = ['section_run','section_method'],
subMatchers = [
SM(name = 'header',
startReStr = r"^\s*vasp.(?P<program_version>[0-9.]+)\s+(?P<vasp_src_date>[0-9]+[A-Za-z]+[0-9]+)\s+\(build (?P<vasp_build_date>[^)]+)\)\s+complex\s*",
subMatchers = [
]
),
])
])
# loading metadata from nomad-meta-info/meta_info/nomad_meta_info/gaussian.nomadmetainfo.json
metaInfoPath = os.path.normpath(os.path.join(os.path.dirname(os.path.abspath(__file__)),"../../../../../nomad-meta-info/meta_info/nomad_meta_info/vasp.nomadmetainfo.json"))
metaInfoEnv, warnings = loadJsonFile(filePath = metaInfoPath, dependencyLoader = None, extraArgsHandling = InfoKindEl.ADD_EXTRA_ARGS, uri = None)
parserInfo = {
"name": "parser_vasp",
"version": "1.0"
}
class VaspParserContext(object):
"""main place to keep the parser status, open ancillary files,..."""
def __init__(self):
pass
def startedParsing(self, path, parser):
self.parser = parser
# which values to cache or forward (mapping meta name -> CachingLevel)
cachingLevelForMetaName = {
}
if __name__ == "__main__":
mainFunction(mainFileDescription, metaInfoEnv, parserInfo,
cachingLevelForMetaName = cachingLevelForMetaName,
superContext = VaspParserContext())
# Copyright 2016-2018 Fawzi Mohamed, Lauri Himanen, Danio Brambila, Ankit Kariryaa, Henning Glawe
#
# 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.
"""
This is the access point to the parser for the scala layer in the
NOMAD project.
"""
from __future__ import absolute_import
import sys
import setup_paths
from nomadcore.parser_backend import JsonParseEventsWriterBackend
from vaspparser import VASPParser
if __name__ == "__main__":
# Initialise the parser with the main filename and a JSON backend
main_file = sys.argv[1]
parser = VASPParser(backend=JsonParseEventsWriterBackend)
parser.parse(main_file)
# Copyright 2016-2018 Fawzi Mohamed, Lauri Himanen, Danio Brambila, Ankit Kariryaa, Henning Glawe
#
# 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.
"""
Setups the python-common library in the PYTHONPATH system variable.
"""
import sys
import os
import os.path
baseDir = os.path.dirname(os.path.abspath(__file__))
commonDir = os.path.normpath(os.path.join(baseDir, "../../../../../python-common/common/python"))
parserDir = os.path.normpath(os.path.join(baseDir, ".."))
# Using sys.path.insert(1, ...) instead of sys.path.insert(0, ...) based on
# this discusssion:
# http://stackoverflow.com/questions/10095037/why-use-sys-path-appendpath-instead-of-sys-path-insert1-path
if commonDir not in sys.path:
sys.path.insert(1, commonDir)
if parserDir not in sys.path:
sys.path.insert(1, parserDir)
# Copyright 2016-2018 Fawzi Mohamed, Lauri Himanen, Danio Brambila, Ankit Kariryaa, Henning Glawe
#
# 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.
from __future__ import absolute_import
import logging
import os
from vaspparser.parser_vasprun import VasprunContext, XmlParser, parserInfo
LOGGER = logging.getLogger("nomad")
class VASPMainParser(object):
"""The main parser class that is called for all run types. Parses the VASP
XML output files.
"""
def __init__(self, parser_context):
"""
"""
self.parser_context = parser_context
def parse(self, filepath):
superContext = VasprunContext()
parser = XmlParser(parserInfo, superContext)
backend = self.parser_context.super_backend
parser.parse(os.path.abspath(filepath), open(filepath), backend)
......@@ -12,7 +12,7 @@
# See the License for the specific language governing permissions and
# limitations under the License.
from setuptools import setup, find_packages
from setuptools import setup
def main():
......@@ -20,10 +20,9 @@ def main():
name='vaspparser',
version='0.1',
description='NOMAD parser implementation for VASP.',
author='Fawzi Mohamed',
author='Fawzi Mohamed, Lauri Himanen, Danio Brambila, Ankit Kariryaa, Henning Glawe',
license='APACHE 2.0',
package_dir={'': 'parser/parser-vasp'},
packages=find_packages('parser/parser-vasp'),
package='vaspparser',
install_requires=[
'nomadcore'
],
......
/*
* Copyright 2016-2018 Fawzi Mohamed, Lauri Himanen, Danio Brambila, Ankit Kariryaa, Henning Glawe
*
* 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.
*/
package eu.nomad_lab.parsers
import eu.nomad_lab.DefaultPythonInterpreter
import org.{ json4s => jn }
import eu.{ nomad_lab => lab }
import scala.collection.breakOut
object VaspParser extends SimpleExternalParserGenerator(
name = "VaspParser",
parserInfo = jn.JObject(
("name" -> jn.JString("VaspParser")) ::
("parserId" -> jn.JString("VaspParser" + lab.VaspVersionInfo.version)) ::
("versionInfo" -> jn.JObject(
("nomadCoreVersion" -> jn.JObject(lab.NomadCoreVersionInfo.toMap.map {
case (k, v) => k -> jn.JString(v.toString)
}(breakOut): List[(String, jn.JString)])) ::
(lab.VaspVersionInfo.toMap.map {
case (key, value) =>
(key -> jn.JString(value.toString))
}(breakOut): List[(String, jn.JString)])
)) :: Nil
),
mainFileTypes = Seq("text/.*"),
mainFileRe = """^\s*vasp.(?<version>[0-9.]+)\s+(?<srcDate>[0-9]+[A-Za-z]+[0-9]+)\s+\(build (?<buildDate>[^)]+)\)\s+complex\s*
""".r,
cmd = Seq(DefaultPythonInterpreter.pythonExe(), "${envDir}/parsers/vasp/parser/parser-vasp/vaspparser/parser_vasp.py",
"--uri", "${mainFileUri}", "${mainFilePath}"),
cmdCwd = "${mainFilePath}/..",
resList = Seq(
"parser-vasp/vaspparser/parser_vasp.py",
"parser-vasp/vaspparser/setup_paths.py",
"nomad_meta_info/public.nomadmetainfo.json",
"nomad_meta_info/common.nomadmetainfo.json",
"nomad_meta_info/meta_types.nomadmetainfo.json",
"nomad_meta_info/vasp.nomadmetainfo.json"
) ++ DefaultPythonInterpreter.commonFiles(),
dirMap = Map(
"parser-vasp" -> "parsers/vasp/parser/parser-vasp",
"nomad_meta_info" -> "nomad-meta-info/meta_info/nomad_meta_info",
"python" -> "python-common/common/python/nomadcore"
) ++ DefaultPythonInterpreter.commonDirMapping()
)
object VaspRunParser extends SimpleExternalParserGenerator(
name = "VaspRunParser",
parserInfo = jn.JObject(
("name" -> jn.JString("VaspRunParser")) ::
("parserId" -> jn.JString("VaspRunParser" + lab.VaspVersionInfo.version)) ::
("versionInfo" -> jn.JObject(
("nomadCoreVersion" -> jn.JObject(lab.NomadCoreVersionInfo.toMap.map {
case (k, v) => k -> jn.JString(v.toString)
}(breakOut): List[(String, jn.JString)])) ::
(lab.VaspVersionInfo.toMap.map {
case (key, value) =>
(key -> jn.JString(value.toString))
}(breakOut): List[(String, jn.JString)])
)) :: Nil
),
mainFileTypes = Seq("application/xml"),
mainFileRe = """\s*<\?xml version="1\.0" encoding="ISO-8859-1"\?>\s*
?\s*<modeling>
?\s*<generator>
?\s*<i name="program" type="string">\s*vasp\s*</i>
?""".r,
cmd = Seq(DefaultPythonInterpreter.pythonExe(), "${envDir}/parsers/vasp/parser/parser-vasp/vaspparser/scalainterface.py",
"${mainFilePath}", "${mainFileUri}"),
cmdCwd = "${mainFilePath}/..",
resList = Seq(
"parser-vasp/vaspparser/__init__.py",
"parser-vasp/vaspparser/parser.py",
"parser-vasp/vaspparser/parser_vasprun.py",
"parser-vasp/vaspparser/scalainterface.py",
"parser-vasp/vaspparser/setup_paths.py",
"parser-vasp/vaspparser/vaspmainparser.py",
"nomad_meta_info/public.nomadmetainfo.json",
"nomad_meta_info/common.nomadmetainfo.json",
"nomad_meta_info/meta_types.nomadmetainfo.json",
"nomad_meta_info/vasp.nomadmetainfo.json"
) ++ DefaultPythonInterpreter.commonFiles(),
dirMap = Map(
"parser-vasp" -> "parsers/vasp/parser/parser-vasp",
"nomad_meta_info" -> "nomad-meta-info/meta_info/nomad_meta_info",
"python" -> "python-common/common/python/nomadcore"
) ++ DefaultPythonInterpreter.commonDirMapping()
)
/*
* Copyright 2016-2018 Fawzi Mohamed, Lauri Himanen, Danio Brambila, Ankit Kariryaa, Henning Glawe
*
* 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.
*/
package eu.nomad_lab.parsers
import eu.nomad_lab.{ parsers, DefaultPythonInterpreter }
import org.scalacheck.Properties
import org.specs2.mutable.Specification
import org.{ json4s => jn }
object VaspParserSpec extends Specification {
"VaspParserTest" >> {
"test with Al.out" >> {
"test with json-events" >> {
ParserRun.parse(VaspParser, "test/examples/oqmd/relaxation/OUTCAR", "json-events") must_== ParseResult.ParseSuccess
}
"test with json" >> {
ParserRun.parse(VaspParser, "test/examples/oqmd/relaxation/OUTCAR", "json") must_== ParseResult.ParseSuccess
}
}
}
}
object VaspRunParserSpec extends Specification {
"VaspRunParserTest" >> {
"test with Al.out" >> {
"test with json-events" >> {
ParserRun.parse(VaspRunParser, "test/examples/oqmd/relaxation/vasprun.xml", "json-events") must_== ParseResult.ParseSuccess
}
"test with json" >> {
ParserRun.parse(VaspRunParser, "test/examples/oqmd/relaxation/vasprun.xml", "json") must_== ParseResult.ParseSuccess
}
}
}
}
# Copyright 2016-2018 Fawzi Mohamed, Lauri Himanen, Danio Brambila, Ankit Kariryaa, Henning Glawe
#
#
# 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.
......@@ -15,7 +15,8 @@
import os
import unittest
import logging
from vaspparser import VASPParser
from vaspparser import VASPRunParserInterface
def get_result(folder, metaname=None):
......@@ -28,8 +29,8 @@ def get_result(folder, metaname=None):
the full dictionary of results.
"""
dirname = os.path.dirname(__file__)
filename = os.path.join("vasp", dirname, folder, "vasprun.xml.static")
parser = VASPParser(None, debug=True, log_level=logging.CRITICAL)
filename = os.path.join(dirname, folder, "vasprun.xml.static")
parser = VASPRunParserInterface(None, debug=True, log_level=logging.CRITICAL)
results = parser.parse(filename)
if metaname is None:
......@@ -43,7 +44,7 @@ class TestDOS(unittest.TestCase):
"""
@classmethod
def setUpClass(cls):
cls.results = get_result("dos")
cls.results = get_result(os.path.join("examples", "dos"))
def test_program_name(self):
result = self.results["program_name"]
......
......@@ -14,25 +14,50 @@
import os
import logging
from nomadcore.baseclasses import ParserInterface
from vaspparser.parser_vasprun import parserInfo
from vaspparser.vaspmainparser import VASPMainParser
logger = logging.getLogger("nomad")
from vaspparser.parser_vasprun import VasprunContext, XmlParser, parserInfo
logger = logging.getLogger(__name__)
class VASPParser(ParserInterface):
"""This class handles the initial setup before any parsing can happen. It
class VASPRunParser:
"""
The main parser class that is called for all run types. Parses the VASP
.out output files.
"""
def __init__(self, parser_context):
self.parser_context = parser_context
def parse(self, filepath):
superContext = VasprunContext(logger=logger)
parser = XmlParser(parserInfo, superContext)
backend = self.parser_context.super_backend
parser.parse(os.path.abspath(filepath), open(filepath), backend)
class VASPRunParserInterface(ParserInterface):
"""
This class handles the initial setup before any parsing can happen. It
determines which version of BigDFT was used to generate the output and then
sets up a correct main parser.
After the implementation has been setup, you can parse the files with
parse().
"""
def __init__(self, metainfo_to_keep=None, backend=None, default_units=None, metainfo_units=None, debug=True, log_level=logging.ERROR, store=True):
super(VASPParser, self).__init__(metainfo_to_keep, backend, default_units, metainfo_units, debug, log_level, store)
def __init__(
self,
metainfo_to_keep=None, backend=None, default_units=None,
metainfo_units=None, debug=True, log_level=logging.ERROR, store=True):
super(VASPRunParserInterface, self).__init__(
metainfo_to_keep, backend, default_units, metainfo_units, debug, log_level, store)
def setup_version(self):
"""Setups the version by looking at the output file and the version
"""
Setups the version by looking at the output file and the version
specified in it.
"""
# Setup the root folder to the fileservice that is used to access files
......@@ -40,7 +65,7 @@ class VASPParser(ParserInterface):
dirpath = os.path.abspath(dirpath)
self.parser_context.file_service.setup_root_folder(dirpath)
self.parser_context.file_service.set_file_id(filename, "output")
self.main_parser = VASPMainParser(self.parser_context)
self.main_parser = VASPRunParser(self.parser_context)
def get_metainfo_filename(self):
return "vasp.nomadmetainfo.json"
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment