Skip to content
Snippets Groups Projects
Commit b3f6f3a8 authored by Daniel Speckhard's avatar Daniel Speckhard
Browse files

Nomad Fair

parent 0c709a6a
No related branches found
No related tags found
No related merge requests found
......@@ -9,6 +9,7 @@ syntax: glob
*.pyc
*.bk
*.swp
*.egg-info
.DS_Store
# logging files
......
from libatomsparser.libAtomsParser import LibAtomsParserWrapper
# Copyright 2016-2018 Fawzi Mohamed, Carl Poelking, Daria Tomecka
#
#
# 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.
......@@ -17,15 +17,15 @@ import os
import sys
import re
import json
#import logging
import setup_paths
import logging
# import setup_paths
import numpy as np
from nomadcore.local_meta_info import loadJsonFile, InfoKindEl
from nomadcore.parser_backend import JsonParseEventsWriterBackend
from contextlib import contextmanager
from libLibAtomsParser import *
from libatomsparser.libLibAtomsParser import *
try:
from libMomo import osio, endl, flush
......@@ -36,16 +36,36 @@ except:
green = None
parser_info = {
"name": "parser-lib-atoms",
"name": "parser-lib-atoms",
"version": "0.0",
"json": "../../../../nomad-meta-info/meta_info/nomad_meta_info/lib_atoms.nomadmetainfo.json"
}
class LibAtomsParserWrapper():
""" A proper class envolop for running this parser using Noamd-FAIRD infra. """
def __init__(self, backend, **kwargs):
self.backend_factory = backend
def parse(self, mainfile):
import nomad_meta_info
metaInfoPath = os.path.normpath(os.path.join(os.path.dirname(os.path.abspath(nomad_meta_info.__file__)), "lib_atoms.nomadmetainfo.json"))
metaInfoEnv, warnings = loadJsonFile(filePath = metaInfoPath, dependencyLoader = None, extraArgsHandling = InfoKindEl.ADD_EXTRA_ARGS, uri = None)
from unittest.mock import patch
logging.info('lib-atoms parser started')
logging.getLogger('nomadcore').setLevel(logging.WARNING)
backend = self.backend_factory(metaInfoEnv)
# Call the old parser without a class.
parserInfo = {'name': 'lib_atoms-parser', 'version': '0.0'}
backend = parse_without_class(mainfile, backend, parserInfo)
return backend
# LOGGING
def log(msg, highlight=None, enter=endl):
if osio:
if highlight==None: hightlight = osio.ww
osio << highlight << msg << enter
# if osio:
# if highlight==None: hightlight = osio.ww
# osio << highlight << msg << enter
return
# CONTEXT GUARD
......@@ -53,7 +73,7 @@ def log(msg, highlight=None, enter=endl):
def open_section(p, name):
gid = p.openSection(name)
yield gid
p.closeSection(name, gid)
p.closeSection(name, gid)
def push(jbe, terminal, key1, fct=lambda x: x.As(), key2=None):
if key2 == None: key2 = key1
......@@ -75,18 +95,23 @@ def push_array_values(jbe, value, key):
jbe.addArrayValues(key, value)
return value
def parse(output_file_name):
jbe = JsonParseEventsWriterBackend(meta_info_env)
def parse_without_class(output_file_name, backend, parser_info):
""" Parse method to parse mainfile and write output to backend."""
jbe = backend
jbe.startedParsingSession(output_file_name, parser_info)
base_dir = os.path.dirname(os.path.abspath(output_file_name))
# jbe = JsonParseEventsWriterBackend(meta_info_env)
# jbe.startedParsingSession(output_file_name, parser_info)
# base_dir = os.path.dirname(os.path.abspath(output_file_name))
terminal_gap = LibAtomsGapParser(osio)
terminal_gap.ParseOutput(output_file_name, base_dir)
terminal_trj = terminal_gap.trj
osio << "Start parsing ..." << osio.endl
osio << "Base directory = '%s'" % base_dir << osio.endl
# osio << "Start parsing ..." << osio.endl
# osio << "Base directory = '%s'" % base_dir << osio.endl
gap = terminal_gap
trj = terminal_trj
......@@ -142,7 +167,7 @@ def parse(output_file_name):
if frame.has_config_type:
push_value(jbe, frame.config_type, 'x_lib_atoms_config_type')
pass
# FRAME SEQUENCE
with open_section(jbe, 'section_frame_sequence'):
push_value(jbe, len(all_frames), 'number_of_frames_in_sequence')
......@@ -167,7 +192,7 @@ def parse(output_file_name):
push_array_values(jbe, gap['gpCoordinates.%s' % key].As(), 'x_lib_atoms_gpCoordinates_%s' % key.replace('.', '_'))
jbe.finishedParsingSession("ParseSuccess", None)
return
return jbe
if __name__ == '__main__':
......
# Copyright 2016-2018 Fawzi Mohamed, Carl Poelking, Daria Tomecka
#
#
# 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.
......@@ -139,7 +139,7 @@ class LibAtomsParser(object):
x = sp[:-1]
y = sp[-1]
key = ''
for i in range(len(x)-1):
for i in range(len(x)-1):
xi = x[i].replace('(','').replace(')','').lower()
key += '%s_' % xi
key += '%s' % x[-1].replace('(','').replace(')','').lower()
......@@ -148,7 +148,7 @@ class LibAtomsParser(object):
return block_data
def ApplyBlockXyData(self, block_data, key_map):
for key_in in key_map:
key_out = key_map[key_in]
key_out = key_map[key_in]
if key_in not in block_data:
# Missing key in output
self.missing_keys_lh.append(key_in)
......@@ -164,10 +164,10 @@ class LibAtomsParser(object):
# Missing key in transform map
self.ignored_keys.append(key)
return
def ParseOutput(self, output_file):
if self.log:
def ParseOutput(self, output_file):
if self.log:
self.log << self.log.mg << "libAtomsParser::ParseOutput ..." << self.log.endl
if HAVE_ASE:
read_fct = ase.io.read
read_fct_args = { 'index':':' }
......@@ -178,9 +178,9 @@ class LibAtomsParser(object):
# PARSE CONFIGURATIONS
self.ase_configs = read_fct(output_file, **read_fct_args)
for config in ase_configs:
print(config)
# for config in ase_configs:
# print(config)
self.Set('program_name', 'libAtoms')
self.Set('program_version', 'n/a')
return
......@@ -238,7 +238,7 @@ class LibAtomsGapParser(LibAtomsParser):
gp_coord_node_att = XmlGetAttributes(gp_coord_node)
for key in gp_coord_node_att.keys():
self.Set('gpCoordinates.%s' % key, gp_coord_node_att[key].value)
# 'GAP_params/gpSparse/gpCoordinates/theta
key = 'theta'
if key in gp_coord_child_nodes:
......@@ -304,8 +304,8 @@ class LibAtomsTrajectory(LibAtomsParser):
self.ase_configs = None
self.frames = []
self.logtag = 'trj'
def ParseOutput(self, output_file):
if self.log:
def ParseOutput(self, output_file):
if self.log:
self.log << self.log.mg << "libAtomsParser::ParseOutput ..." << self.log.endl
if HAVE_ASE:
read_fct = ase.io.read
......@@ -340,7 +340,7 @@ class LibAtomsFrame(LibAtomsParser):
self.config_type = None
def LoadAseConfig(self, ase_config):
self.ase_config = ase_config
print("INFO", self.ase_config.info)
# print("INFO", self.ase_config.info)
key = 'energy'
if key in self.ase_config.info:
self.has_energy = True
......@@ -383,7 +383,7 @@ class FileStream(object):
break
return ln
def SkipToMatch(self, expr):
while True:
while True:
ln = self.ifs.readline()
m = re.search(expr, ln)
if m:
......@@ -412,12 +412,12 @@ class FileStream(object):
# Block not started yet
pass
if self.all_read(): break
return block_stream
def GetBlockSequence(self,
expr_start,
expr_new,
expr_end,
remove_eol=True,
return block_stream
def GetBlockSequence(self,
expr_start,
expr_new,
expr_end,
remove_eol=True,
skip_empty=True):
inside = False
outside = False
......@@ -442,7 +442,7 @@ class FileStream(object):
while True:
# Log line position
last_pos = self.ifs.tell()
ln = self.ifs.readline()
ln = self.ifs.readline()
# Figure out where we are
if not inside and expr_start in ln:
#print "Enter", expr_start
......@@ -494,7 +494,7 @@ class FileStream(object):
for i in range(n):
self.ln()
return
class BlockStream(FileStream):
def __init__(self, label=None):
super(BlockStream, self).__init__(None)
......@@ -506,7 +506,7 @@ class BlockStream(FileStream):
self.lns.append(ln)
def readline(self):
if self.all_read():
return ''
return ''
ln = self.lns[self.idx]
self.idx += 1
return ln
......
# Copyright 2016-2018 Fawzi Mohamed, Carl Poelking, Daria Tomecka
#
#
# 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.
......@@ -112,7 +112,7 @@ def NamespaceFromDict(tree_dict):
#print "NSPACE '%s' value added" % s
add_to_nspace.Add(s, v)
return nspace
class XmlTree(list):
def __init__(self, xmlfile, paths_rel_to=None):
......@@ -134,7 +134,7 @@ class XmlNode(object):
def __init__(self, element, path):
self.path = path
self.node = element
self.tag = element.tag
self.tag = element.tag
self.value = element.text
self.attributes = element.attrib
def As(self, typ):
......@@ -220,14 +220,14 @@ class OptionsInterface(object):
except KeyError:
raise ValueError('CLIO does not know how to convert %s into a boolean.' % expr)
def InterpretAsNumpyArray(self, expr):
print("Interpret", expr)
# print("Interpret", expr)
array = [ float(e) for e in expr ]
array = np.array(array)
return array
def InterpretAsList(self, expr):
array = [ self.subtype(e) for e in expr ]
return array
def AddArg(self, name, typ=str, nickname=None,
def AddArg(self, name, typ=str, nickname=None,
default=None, destination=None, help=None):
# Sort out <name> (e.g. --time) vs <destination> (e.g., time)
if '--' != name[0:2]:
......@@ -270,7 +270,7 @@ class OptionsInterface(object):
else:
raise NotImplementedError("CLIO does not know how to generate type '%s'"\
% typ)
self.cmd_ln_args.add_argument(nickname, name,
self.cmd_ln_args.add_argument(nickname, name,
dest=dest,
action='store',
nargs=nargs,
......@@ -294,13 +294,13 @@ class OptionsInterface(object):
return
def __getitem__(self, key):
try:
return self.xspace.__dict__[key]
return self.xspace.__dict__[key]
except KeyError:
return self.cmd_ln_opts.__dict__[key]
except KeyError:
raise AttributeError('No such option registered: \'%s\'' % key)
return None
# =============================================================================
# OS SHELL INTERFACE
......@@ -315,7 +315,7 @@ class ShellInterface(object):
'mb' : '\033[34m',
'lb' : '\033[1;34m',
'my' : '\033[1;33m',
'mg' : '\033[92m',
'mg' : '\033[92m',
'mr' : '\033[91m',
'ww' : '\033[0;1m',
'ok' : '\033[92m',
......@@ -330,9 +330,9 @@ class ShellInterface(object):
'ro' : '\r o '}
self.pp = OS_COLOR('pp')
self.lb = OS_COLOR('lb')
self.mb = OS_COLOR('mb')
self.mb = OS_COLOR('mb')
self.mg = OS_COLOR('mg')
self.my = OS_COLOR('my')
self.my = OS_COLOR('my')
self.mr = OS_COLOR('mr')
self.ww = OS_COLOR('ww')
self.ok = OS_COLOR('ok')
......@@ -374,7 +374,7 @@ class ShellInterface(object):
mssg = self.color_dict[c] + mssg + self.color_dict['endcolor']
if h:
mssg = self.os_generate_header(mssg, t)
print(mssg)
# print(mssg)
# LOGFILE ADAPTOR =========================================================
def ConnectToFile(self, logfile):
self.logfile = logfile
......@@ -419,7 +419,7 @@ class ShellInterface(object):
mssg = OS_COLOR_DICT[c] + mssg + OS_COLOR_DICT['endcolor']
if h:
mssg = os_generate_header(mssg, t)
print(mssg)
# print(mssg)
return
def os_print_config(self, c=None, j=None, h=False, t="=", tl=' '):
self.sel_color = c
......@@ -465,7 +465,7 @@ class ShellInterface(object):
if self.debug: self << self.my << "exe:" << cmmd << endl
if self.os_exe_get:
output = subprocess.getoutput(cmmd)
self.os_exe_get = False
self.os_exe_get = False
return output
else:
sign = os.system(cmmd)
......@@ -528,7 +528,7 @@ class OS_EXE_DEV(object):
class OS_EXE_CATCH(object):
def __init__(self):
self.catch = True
class OS_EXE_ASSERT(object):
def __init__(self):
self.assert_0 = True
......
setup.py 0 → 100644
# Copyright 2015-2019 NOMAD
#
# 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 setuptools import setup, find_packages
def main():
setup(
name='libatomsparser',
version='0.1',
description='NOMAD parser implementation for Lib-Atoms.',
license='APACHE 2.0',
package_dir={'': './'},
packages=find_packages(),
install_requires=[
'nomadcore'
],
)
if __name__ == '__main__':
main()
/*
* Copyright 2016-2018 Fawzi Mohamed, Carl Poelking, Daria Tomecka
*
* 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 => lab }
import eu.nomad_lab.DefaultPythonInterpreter
import org.{ json4s => jn }
import scala.collection.breakOut
object LibAtomsParser extends SimpleExternalParserGenerator(
name = "LibAtomsParser",
parserInfo = jn.JObject(
("name" -> jn.JString("LibAtomsParser")) ::
("parserId" -> jn.JString("LibAtomsParser" + lab.LibAtomsVersionInfo.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.LibAtomsVersionInfo.toMap.map {
case (key, value) =>
(key -> jn.JString(value.toString))
}(breakOut): List[(String, jn.JString)])
)) :: Nil
),
mainFileTypes = Seq("application/xml"),
mainFileRe = """\s*<GAP_params\s""".r,
cmd = Seq(DefaultPythonInterpreter.pythonExe(), "${envDir}/parsers/lib-atoms/parser/parser-lib-atoms/libAtomsParser.py",
"${mainFilePath}"),
resList = Seq(
"parser-lib-atoms/libAtomsParser.py",
"parser-lib-atoms/libLibAtomsParser.py",
"parser-lib-atoms/libMomo.py",
"parser-lib-atoms/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/lib_atoms.nomadmetainfo.json"
) ++ DefaultPythonInterpreter.commonFiles(),
dirMap = Map(
"parser-lib-atoms" -> "parsers/lib-atoms/parser/parser-lib-atoms",
"nomad_meta_info" -> "nomad-meta-info/meta_info/nomad_meta_info"
) ++ DefaultPythonInterpreter.commonDirMapping()
)
/*
* Copyright 2016-2018 Fawzi Mohamed, Carl Poelking, Daria Tomecka
*
* 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 org.specs2.mutable.Specification
object LibAtomsParserSpec extends Specification {
"LibAtomsParserTest" >> {
"test with json-events" >> {
ParserRun.parse(LibAtomsParser, "parsers/lib-atoms/test/lib-atoms-gap-test/gp.xml", "json-events") must_== ParseResult.ParseSuccess
}
"test with json" >> {
ParserRun.parse(LibAtomsParser, "parsers/lib-atoms/test/lib-atoms-gap-test/gp.xml", "json") must_== ParseResult.ParseSuccess
}
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment