Commit b3f6f3a8 authored by Daniel Speckhard's avatar Daniel Speckhard
Browse files

Nomad Fair

parent 0c709a6a
...@@ -9,6 +9,7 @@ syntax: glob ...@@ -9,6 +9,7 @@ syntax: glob
*.pyc *.pyc
*.bk *.bk
*.swp *.swp
*.egg-info
.DS_Store .DS_Store
# logging files # logging files
......
from libatomsparser.libAtomsParser import LibAtomsParserWrapper
# Copyright 2016-2018 Fawzi Mohamed, Carl Poelking, Daria Tomecka # Copyright 2016-2018 Fawzi Mohamed, Carl Poelking, Daria Tomecka
# #
# Licensed under the Apache License, Version 2.0 (the "License"); # Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License. # you may not use this file except in compliance with the License.
# You may obtain a copy of the License at # You may obtain a copy of the License at
# #
# http://www.apache.org/licenses/LICENSE-2.0 # http://www.apache.org/licenses/LICENSE-2.0
# #
# Unless required by applicable law or agreed to in writing, software # Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, # distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
...@@ -17,15 +17,15 @@ import os ...@@ -17,15 +17,15 @@ import os
import sys import sys
import re import re
import json import json
#import logging import logging
import setup_paths # import setup_paths
import numpy as np import numpy as np
from nomadcore.local_meta_info import loadJsonFile, InfoKindEl from nomadcore.local_meta_info import loadJsonFile, InfoKindEl
from nomadcore.parser_backend import JsonParseEventsWriterBackend from nomadcore.parser_backend import JsonParseEventsWriterBackend
from contextlib import contextmanager from contextlib import contextmanager
from libLibAtomsParser import * from libatomsparser.libLibAtomsParser import *
try: try:
from libMomo import osio, endl, flush from libMomo import osio, endl, flush
...@@ -36,16 +36,36 @@ except: ...@@ -36,16 +36,36 @@ except:
green = None green = None
parser_info = { parser_info = {
"name": "parser-lib-atoms", "name": "parser-lib-atoms",
"version": "0.0", "version": "0.0",
"json": "../../../../nomad-meta-info/meta_info/nomad_meta_info/lib_atoms.nomadmetainfo.json" "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 # LOGGING
def log(msg, highlight=None, enter=endl): def log(msg, highlight=None, enter=endl):
if osio: # if osio:
if highlight==None: hightlight = osio.ww # if highlight==None: hightlight = osio.ww
osio << highlight << msg << enter # osio << highlight << msg << enter
return return
# CONTEXT GUARD # CONTEXT GUARD
...@@ -53,7 +73,7 @@ def log(msg, highlight=None, enter=endl): ...@@ -53,7 +73,7 @@ def log(msg, highlight=None, enter=endl):
def open_section(p, name): def open_section(p, name):
gid = p.openSection(name) gid = p.openSection(name)
yield gid yield gid
p.closeSection(name, gid) p.closeSection(name, gid)
def push(jbe, terminal, key1, fct=lambda x: x.As(), key2=None): def push(jbe, terminal, key1, fct=lambda x: x.As(), key2=None):
if key2 == None: key2 = key1 if key2 == None: key2 = key1
...@@ -75,18 +95,23 @@ def push_array_values(jbe, value, key): ...@@ -75,18 +95,23 @@ def push_array_values(jbe, value, key):
jbe.addArrayValues(key, value) jbe.addArrayValues(key, value)
return value return value
def parse(output_file_name): def parse_without_class(output_file_name, backend, parser_info):
jbe = JsonParseEventsWriterBackend(meta_info_env) """ Parse method to parse mainfile and write output to backend."""
jbe = backend
jbe.startedParsingSession(output_file_name, parser_info) jbe.startedParsingSession(output_file_name, parser_info)
base_dir = os.path.dirname(os.path.abspath(output_file_name)) 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 = LibAtomsGapParser(osio)
terminal_gap.ParseOutput(output_file_name, base_dir) terminal_gap.ParseOutput(output_file_name, base_dir)
terminal_trj = terminal_gap.trj terminal_trj = terminal_gap.trj
osio << "Start parsing ..." << osio.endl # osio << "Start parsing ..." << osio.endl
osio << "Base directory = '%s'" % base_dir << osio.endl # osio << "Base directory = '%s'" % base_dir << osio.endl
gap = terminal_gap gap = terminal_gap
trj = terminal_trj trj = terminal_trj
...@@ -142,7 +167,7 @@ def parse(output_file_name): ...@@ -142,7 +167,7 @@ def parse(output_file_name):
if frame.has_config_type: if frame.has_config_type:
push_value(jbe, frame.config_type, 'x_lib_atoms_config_type') push_value(jbe, frame.config_type, 'x_lib_atoms_config_type')
pass pass
# FRAME SEQUENCE # FRAME SEQUENCE
with open_section(jbe, 'section_frame_sequence'): with open_section(jbe, 'section_frame_sequence'):
push_value(jbe, len(all_frames), 'number_of_frames_in_sequence') push_value(jbe, len(all_frames), 'number_of_frames_in_sequence')
...@@ -167,7 +192,7 @@ def parse(output_file_name): ...@@ -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('.', '_')) push_array_values(jbe, gap['gpCoordinates.%s' % key].As(), 'x_lib_atoms_gpCoordinates_%s' % key.replace('.', '_'))
jbe.finishedParsingSession("ParseSuccess", None) jbe.finishedParsingSession("ParseSuccess", None)
return return jbe
if __name__ == '__main__': if __name__ == '__main__':
......
# Copyright 2016-2018 Fawzi Mohamed, Carl Poelking, Daria Tomecka # Copyright 2016-2018 Fawzi Mohamed, Carl Poelking, Daria Tomecka
# #
# Licensed under the Apache License, Version 2.0 (the "License"); # Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License. # you may not use this file except in compliance with the License.
# You may obtain a copy of the License at # You may obtain a copy of the License at
# #
# http://www.apache.org/licenses/LICENSE-2.0 # http://www.apache.org/licenses/LICENSE-2.0
# #
# Unless required by applicable law or agreed to in writing, software # Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, # distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
...@@ -139,7 +139,7 @@ class LibAtomsParser(object): ...@@ -139,7 +139,7 @@ class LibAtomsParser(object):
x = sp[:-1] x = sp[:-1]
y = sp[-1] y = sp[-1]
key = '' key = ''
for i in range(len(x)-1): for i in range(len(x)-1):
xi = x[i].replace('(','').replace(')','').lower() xi = x[i].replace('(','').replace(')','').lower()
key += '%s_' % xi key += '%s_' % xi
key += '%s' % x[-1].replace('(','').replace(')','').lower() key += '%s' % x[-1].replace('(','').replace(')','').lower()
...@@ -148,7 +148,7 @@ class LibAtomsParser(object): ...@@ -148,7 +148,7 @@ class LibAtomsParser(object):
return block_data return block_data
def ApplyBlockXyData(self, block_data, key_map): def ApplyBlockXyData(self, block_data, key_map):
for key_in in 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: if key_in not in block_data:
# Missing key in output # Missing key in output
self.missing_keys_lh.append(key_in) self.missing_keys_lh.append(key_in)
...@@ -164,10 +164,10 @@ class LibAtomsParser(object): ...@@ -164,10 +164,10 @@ class LibAtomsParser(object):
# Missing key in transform map # Missing key in transform map
self.ignored_keys.append(key) self.ignored_keys.append(key)
return return
def ParseOutput(self, output_file): def ParseOutput(self, output_file):
if self.log: if self.log:
self.log << self.log.mg << "libAtomsParser::ParseOutput ..." << self.log.endl self.log << self.log.mg << "libAtomsParser::ParseOutput ..." << self.log.endl
if HAVE_ASE: if HAVE_ASE:
read_fct = ase.io.read read_fct = ase.io.read
read_fct_args = { 'index':':' } read_fct_args = { 'index':':' }
...@@ -178,9 +178,9 @@ class LibAtomsParser(object): ...@@ -178,9 +178,9 @@ class LibAtomsParser(object):
# PARSE CONFIGURATIONS # PARSE CONFIGURATIONS
self.ase_configs = read_fct(output_file, **read_fct_args) self.ase_configs = read_fct(output_file, **read_fct_args)
for config in ase_configs: # for config in ase_configs:
print(config) # print(config)
self.Set('program_name', 'libAtoms') self.Set('program_name', 'libAtoms')
self.Set('program_version', 'n/a') self.Set('program_version', 'n/a')
return return
...@@ -238,7 +238,7 @@ class LibAtomsGapParser(LibAtomsParser): ...@@ -238,7 +238,7 @@ class LibAtomsGapParser(LibAtomsParser):
gp_coord_node_att = XmlGetAttributes(gp_coord_node) gp_coord_node_att = XmlGetAttributes(gp_coord_node)
for key in gp_coord_node_att.keys(): for key in gp_coord_node_att.keys():
self.Set('gpCoordinates.%s' % key, gp_coord_node_att[key].value) self.Set('gpCoordinates.%s' % key, gp_coord_node_att[key].value)
# 'GAP_params/gpSparse/gpCoordinates/theta # 'GAP_params/gpSparse/gpCoordinates/theta
key = 'theta' key = 'theta'
if key in gp_coord_child_nodes: if key in gp_coord_child_nodes:
...@@ -304,8 +304,8 @@ class LibAtomsTrajectory(LibAtomsParser): ...@@ -304,8 +304,8 @@ class LibAtomsTrajectory(LibAtomsParser):
self.ase_configs = None self.ase_configs = None
self.frames = [] self.frames = []
self.logtag = 'trj' self.logtag = 'trj'
def ParseOutput(self, output_file): def ParseOutput(self, output_file):
if self.log: if self.log:
self.log << self.log.mg << "libAtomsParser::ParseOutput ..." << self.log.endl self.log << self.log.mg << "libAtomsParser::ParseOutput ..." << self.log.endl
if HAVE_ASE: if HAVE_ASE:
read_fct = ase.io.read read_fct = ase.io.read
...@@ -340,7 +340,7 @@ class LibAtomsFrame(LibAtomsParser): ...@@ -340,7 +340,7 @@ class LibAtomsFrame(LibAtomsParser):
self.config_type = None self.config_type = None
def LoadAseConfig(self, ase_config): def LoadAseConfig(self, ase_config):
self.ase_config = ase_config self.ase_config = ase_config
print("INFO", self.ase_config.info) # print("INFO", self.ase_config.info)
key = 'energy' key = 'energy'
if key in self.ase_config.info: if key in self.ase_config.info:
self.has_energy = True self.has_energy = True
...@@ -383,7 +383,7 @@ class FileStream(object): ...@@ -383,7 +383,7 @@ class FileStream(object):
break break
return ln return ln
def SkipToMatch(self, expr): def SkipToMatch(self, expr):
while True: while True:
ln = self.ifs.readline() ln = self.ifs.readline()
m = re.search(expr, ln) m = re.search(expr, ln)
if m: if m:
...@@ -412,12 +412,12 @@ class FileStream(object): ...@@ -412,12 +412,12 @@ class FileStream(object):
# Block not started yet # Block not started yet
pass pass
if self.all_read(): break if self.all_read(): break
return block_stream return block_stream
def GetBlockSequence(self, def GetBlockSequence(self,
expr_start, expr_start,
expr_new, expr_new,
expr_end, expr_end,
remove_eol=True, remove_eol=True,
skip_empty=True): skip_empty=True):
inside = False inside = False
outside = False outside = False
...@@ -442,7 +442,7 @@ class FileStream(object): ...@@ -442,7 +442,7 @@ class FileStream(object):
while True: while True:
# Log line position # Log line position
last_pos = self.ifs.tell() last_pos = self.ifs.tell()
ln = self.ifs.readline() ln = self.ifs.readline()
# Figure out where we are # Figure out where we are
if not inside and expr_start in ln: if not inside and expr_start in ln:
#print "Enter", expr_start #print "Enter", expr_start
...@@ -494,7 +494,7 @@ class FileStream(object): ...@@ -494,7 +494,7 @@ class FileStream(object):
for i in range(n): for i in range(n):
self.ln() self.ln()
return return
class BlockStream(FileStream): class BlockStream(FileStream):
def __init__(self, label=None): def __init__(self, label=None):
super(BlockStream, self).__init__(None) super(BlockStream, self).__init__(None)
...@@ -506,7 +506,7 @@ class BlockStream(FileStream): ...@@ -506,7 +506,7 @@ class BlockStream(FileStream):
self.lns.append(ln) self.lns.append(ln)
def readline(self): def readline(self):
if self.all_read(): if self.all_read():
return '' return ''
ln = self.lns[self.idx] ln = self.lns[self.idx]
self.idx += 1 self.idx += 1
return ln return ln
......
# Copyright 2016-2018 Fawzi Mohamed, Carl Poelking, Daria Tomecka # Copyright 2016-2018 Fawzi Mohamed, Carl Poelking, Daria Tomecka
# #
# Licensed under the Apache License, Version 2.0 (the "License"); # Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License. # you may not use this file except in compliance with the License.
# You may obtain a copy of the License at # You may obtain a copy of the License at
# #
# http://www.apache.org/licenses/LICENSE-2.0 # http://www.apache.org/licenses/LICENSE-2.0
# #
# Unless required by applicable law or agreed to in writing, software # Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, # distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
...@@ -112,7 +112,7 @@ def NamespaceFromDict(tree_dict): ...@@ -112,7 +112,7 @@ def NamespaceFromDict(tree_dict):
#print "NSPACE '%s' value added" % s #print "NSPACE '%s' value added" % s
add_to_nspace.Add(s, v) add_to_nspace.Add(s, v)
return nspace return nspace
class XmlTree(list): class XmlTree(list):
def __init__(self, xmlfile, paths_rel_to=None): def __init__(self, xmlfile, paths_rel_to=None):
...@@ -134,7 +134,7 @@ class XmlNode(object): ...@@ -134,7 +134,7 @@ class XmlNode(object):
def __init__(self, element, path): def __init__(self, element, path):
self.path = path self.path = path
self.node = element self.node = element
self.tag = element.tag self.tag = element.tag
self.value = element.text self.value = element.text
self.attributes = element.attrib self.attributes = element.attrib
def As(self, typ): def As(self, typ):
...@@ -220,14 +220,14 @@ class OptionsInterface(object): ...@@ -220,14 +220,14 @@ class OptionsInterface(object):
except KeyError: except KeyError:
raise ValueError('CLIO does not know how to convert %s into a boolean.' % expr) raise ValueError('CLIO does not know how to convert %s into a boolean.' % expr)
def InterpretAsNumpyArray(self, expr): def InterpretAsNumpyArray(self, expr):
print("Interpret", expr) # print("Interpret", expr)
array = [ float(e) for e in expr ] array = [ float(e) for e in expr ]
array = np.array(array) array = np.array(array)
return array return array
def InterpretAsList(self, expr): def InterpretAsList(self, expr):
array = [ self.subtype(e) for e in expr ] array = [ self.subtype(e) for e in expr ]
return array return array
def AddArg(self, name, typ=str, nickname=None, def AddArg(self, name, typ=str, nickname=None,
default=None, destination=None, help=None): default=None, destination=None, help=None):
# Sort out <name> (e.g. --time) vs <destination> (e.g., time) # Sort out <name> (e.g. --time) vs <destination> (e.g., time)
if '--' != name[0:2]: if '--' != name[0:2]:
...@@ -270,7 +270,7 @@ class OptionsInterface(object): ...@@ -270,7 +270,7 @@ class OptionsInterface(object):
else: else:
raise NotImplementedError("CLIO does not know how to generate type '%s'"\ raise NotImplementedError("CLIO does not know how to generate type '%s'"\
% typ) % typ)
self.cmd_ln_args.add_argument(nickname, name, self.cmd_ln_args.add_argument(nickname, name,
dest=dest, dest=dest,
action='store', action='store',
nargs=nargs, nargs=nargs,
...@@ -294,13 +294,13 @@ class OptionsInterface(object): ...@@ -294,13 +294,13 @@ class OptionsInterface(object):
return return
def __getitem__(self, key): def __getitem__(self, key):
try: try:
return self.xspace.__dict__[key] return self.xspace.__dict__[key]
except KeyError: except KeyError:
return self.cmd_ln_opts.__dict__[key] return self.cmd_ln_opts.__dict__[key]
except KeyError: except KeyError:
raise AttributeError('No such option registered: \'%s\'' % key) raise AttributeError('No such option registered: \'%s\'' % key)
return None return None
# ============================================================================= # =============================================================================
# OS SHELL INTERFACE # OS SHELL INTERFACE
...@@ -315,7 +315,7 @@ class ShellInterface(object): ...@@ -315,7 +315,7 @@ class ShellInterface(object):
'mb' : '\033[34m', 'mb' : '\033[34m',
'lb' : '\033[1;34m', 'lb' : '\033[1;34m',
'my' : '\033[1;33m', 'my' : '\033[1;33m',
'mg' : '\033[92m', 'mg' : '\033[92m',
'mr' : '\033[91m', 'mr' : '\033[91m',
'ww' : '\033[0;1m', 'ww' : '\033[0;1m',
'ok' : '\033[92m', 'ok' : '\033[92m',
...@@ -330,9 +330,9 @@ class ShellInterface(object): ...@@ -330,9 +330,9 @@ class ShellInterface(object):
'ro' : '\r o '} 'ro' : '\r o '}
self.pp = OS_COLOR('pp') self.pp = OS_COLOR('pp')
self.lb = OS_COLOR('lb') self.lb = OS_COLOR('lb')
self.mb = OS_COLOR('mb') self.mb = OS_COLOR('mb')
self.mg = OS_COLOR('mg') self.mg = OS_COLOR('mg')
self.my = OS_COLOR('my') self.my = OS_COLOR('my')
self.mr = OS_COLOR('mr') self.mr = OS_COLOR('mr')
self.ww = OS_COLOR('ww') self.ww = OS_COLOR('ww')
self.ok = OS_COLOR('ok') self.ok = OS_COLOR('ok')
...@@ -374,7 +374,7 @@ class ShellInterface(object): ...@@ -374,7 +374,7 @@ class ShellInterface(object):
mssg = self.color_dict[c] + mssg + self.color_dict['endcolor'] mssg = self.color_dict[c] + mssg + self.color_dict['endcolor']
if h: if h:
mssg = self.os_generate_header(mssg, t) mssg = self.os_generate_header(mssg, t)
print(mssg) # print(mssg)
# LOGFILE ADAPTOR ========================================================= # LOGFILE ADAPTOR =========================================================
def ConnectToFile(self, logfile): def ConnectToFile(self, logfile):
self.logfile = logfile self.logfile = logfile
...@@ -419,7 +419,7 @@ class ShellInterface(object): ...@@ -419,7 +419,7 @@ class ShellInterface(object):
mssg = OS_COLOR_DICT[c] + mssg + OS_COLOR_DICT['endcolor'] mssg = OS_COLOR_DICT[c] + mssg + OS_COLOR_DICT['endcolor']
if h: if h:
mssg = os_generate_header(mssg, t) mssg = os_generate_header(mssg, t)
print(mssg) # print(mssg)
return return
def os_print_config(self, c=None, j=None, h=False, t="=", tl=' '): def os_print_config(self, c=None, j=None, h=False, t="=", tl=' '):
self.sel_color = c self.sel_color = c
...@@ -465,7 +465,7 @@ class ShellInterface(object): ...@@ -465,7 +465,7 @@ class ShellInterface(object):
if self.debug: self << self.my << "exe:" << cmmd << endl if self.debug: self << self.my << "exe:" << cmmd << endl
if self.os_exe_get: if self.os_exe_get:
output = subprocess.getoutput(cmmd) output = subprocess.getoutput(cmmd)
self.os_exe_get = False self.os_exe_get = False
return output return output
else: else:
sign = os.system(cmmd) sign = os.system(cmmd)
...@@ -528,7 +528,7 @@ class OS_EXE_DEV(object): ...@@ -528,7 +528,7 @@ class OS_EXE_DEV(object):
class OS_EXE_CATCH(object): class OS_EXE_CATCH(object):
def __init__(self): def __init__(self):
self.catch = True self.catch = True
class OS_EXE_ASSERT(object): class OS_EXE_ASSERT(object):
def __init__(self): def __init__(self):
self.assert_0 = True self.assert_0 = True
......
# 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={'': './'},