Commit 0e146cc4 authored by Lauri Himanen's avatar Lauri Himanen
Browse files

Added the local angular2 metainfoviewer to nomadtoolkit

parent d443a581
cp2kparser.egg-info
*.pyc
nomadtoolkit/nomadtoolkit/metaviewer/frontend/static/frontend/node_modules
nomadtoolkit/nomadtoolkit/metaviewer/frontend/static/frontend/bower_components
......@@ -53,7 +53,7 @@ class CP2KOutputParser262(object):
SM(
startReStr="",
sections=["cp2k_system_description"],
otherMetaInfo=["atom_number"],
otherMetaInfo=["number_of_atoms"],
dependencies={"atom_number": ["cp2k_atom_number"]},
subMatchers=[
SM(
......@@ -76,7 +76,7 @@ class CP2KOutputParser262(object):
startReStr=" FUNCTIONAL\|",
forwardMatch=True,
sections=["section_method", "cp2k_section_functionals"],
otherMetaInfo=["XC_functional_scf"],
otherMetaInfo=["XC_functional"],
subMatchers=[
SM(
repeats=True,
......@@ -213,7 +213,7 @@ class CP2KOutputParser262(object):
n_atoms = numbers["cp2k_atom_number"]
if n_atoms:
n_atoms = n_atoms[0]
backend.addValue("atom_number", n_atoms)
backend.addValue("number_of_atoms", n_atoms)
# Close the common system description section
backend.closeSection("section_system_description", 0)
......@@ -244,7 +244,7 @@ class CP2KOutputParser262(object):
functionals = "_".join(sorted(functionals))
# Push the functional string into the backend
backend.addValue('XC_functional_scf', functionals)
backend.addValue('XC_functional', functionals)
def onClose_cp2k_section_atom_position(self, backend, gIndex, section):
"""Get the initial atomic positions from cp2kparser.
......
import os
import logging
from abc import ABCMeta, abstractmethod
import nomadtoolkit.config
from nomadcore.local_meta_info import loadJsonFile
from nomadtoolkit.local_backend import LocalBackend
logger = logging.getLogger(__name__)
......@@ -50,14 +48,6 @@ class Parser(object):
# specify which files are of interest or to include them all.
self.parser_context.files = self.search_parseable_files(files)
# If no backend provided, create Local one with default metainfos
if not backend:
metadir = nomadtoolkit.config.get_config("metaInfoPath")
default_metainfo_path = os.path.realpath(os.path.join(metadir, self.get_metainfo_filename()))
metainfoenv, warnings = loadJsonFile(default_metainfo_path)
backend = LocalBackend(metainfoenv)
self.parser_context.backend = backend
@abstractmethod
def setup(self):
"""Deduce the version of the software that was used and setup a correct
......
......@@ -25,3 +25,35 @@ To install this toolkit run the included nomadtoolkit/setup.py file as follows:
```sh
python setup.py develop --user
```
# Usage
## Analysis
To access the parsed data locally you can use the Analyzer class. This class
will take care reading the parsed data and providing it to the user. The usage
is simple as:
```python
from nomadtoolkit import Analyzer
from cp2kparser import CP2KParser
dirpaths = "/home/lauri/Dropbox/nomad-dev/parser-cp2k/cp2kparser/cp2kparser/tests/cp2k_2.6.2/forces/outputfile/n"
parser = CP2KParser(contents=dirpaths)
analyzer = Analyzer(parser)
results = analyzer.parse()
```
The results class is now a dictionary where you can access any parsed quantity
by it's metainfo name. The metainfo viewer which is discussed next can help you
to identify what the available metainfo names are.
##Metainfo Viewer
The toolkit contains a browser based metainfo viewer for inspection of the
different metainfos. This tool is useful when the online resources are not
accessible. After succesfull installation you can start the viewer with the following command:
```sh
python -m nomadtoolkit -v
```
This will open your default browser for viewing the metainfos.
from nomadtoolkit.analysis import Analyzer
from nomadtoolkit.analysis.analyzer import Analyzer
"""
When this package is run on a folder with python -m nomadviewer, the current
directory is searched for calculation data. If a JSON output file is found, it
is used directly, otherwise the folder is parsed normally and the resulting
JSON file is saved and used.
After the data has been succesfully created/written a local django webserver is
created and a browser is opened. The calculation will now be browseable through
a web interface.
"""
import os
import webbrowser
from multiprocessing import Process
import argparse
def runserver():
"""Runs the django web server that will show the parsing results.
"""
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "nomadtoolkit.metaviewer.mysite.settings")
from django.core.management import call_command
from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()
call_command('runserver', url)
def openbrowser():
"""Opens a browser instance and shows the contents served by the dango
server.
"""
webbrowser.open("http://{}".format(url))
parser = argparse.ArgumentParser()
parser.add_argument('-v', help='Opens the browser based metainfoviewer.', action='store_true')
args = parser.parse_args()
# Run the server and open a browser
if args.v:
url = "localhost:8000"
p1 = Process(target=runserver)
p1.start()
p2 = Process(target=openbrowser)
p2.start()
import sys
import os
import logging
from nomadcore.local_meta_info import loadJsonFile
from nomadcore.parser_backend import JsonParseEventsWriterBackend
from nomadtoolkit.local_backend import LocalBackend
import nomadtoolkit.utils.config
from nomadtoolkit.analysis.local_backend import LocalBackend
logger = logging.getLogger(__name__)
......@@ -12,6 +12,14 @@ class Analyzer(object):
def __init__(self, parser=None):
self.parser = parser
# Use a local backend with the default metainfos
if not parser.parser_context.backend:
metadir = nomadtoolkit.utils.config.get_config("metaInfoPath")
default_metainfo_path = os.path.realpath(os.path.join(metadir, parser.get_metainfo_filename()))
metainfoenv, warnings = loadJsonFile(default_metainfo_path)
backend = LocalBackend(metainfoenv)
parser.parser_context.backend = backend
def parse(self):
if not self.parser:
logger.error("A parser hasn't been defined.")
......
# Metainfo Viewer
Metainfo viewer is a browser based inspector for viewing the metainfo related to
the different parsers.
from django.contrib import admin
# Register your models here.
from django.apps import AppConfig
class BackendConfig(AppConfig):
name = 'backend'
import json
import os
from nomadtoolkit.utils.config import get_config
from nomadcore.local_meta_info import loadJsonFile
import logging
from collections import defaultdict
logger = logging.getLogger(__name__)
def load_metainfo():
path = get_config("metaInfoPath") + "/cp2k.nomadmetainfo.json"
env, warnings = loadJsonFile(path)
return env
def get_metainfos():
env = load_metainfo()
return reorganize_metainfos(env)
def reorganize_metainfos(env):
metas = {}
roots = {}
for meta in env.infoKinds.values():
metaobj = create_metainfo_object(meta)
metas[metaobj.name] = metaobj
for meta in metas.itervalues():
parentNames = meta.superNames
if len(parentNames) == 0:
roots[meta.name] = meta
else:
for parentName in parentNames:
parent = metas[parentName]
parent.children.append(meta)
section_run = roots["section_run"]
jsonobject = json.dumps(section_run, default=lambda o: o.__dict__)
return jsonobject
def create_metainfo_object(meta):
metaobj = MetaInfo()
metaobj.name = meta.name
metaobj.description = meta.description
if meta.shape:
metaobj.shape = meta.shape
else:
metaobj.shape = None
metaobj.units = meta.units
metaobj.dtypeStr = meta.dtypeStr
metaobj.kindStr = meta.kindStr
metaobj.superNames = meta.superNames
return metaobj
class MetaInfo(object):
def __init__(self):
self.name = None
self.description = None
self.shape = None
self.kindStr = None
self.units = None
self.dtypeStr = None
self.repeats = None
self.children = []
self.superNames = []
# def print_children(meta, level=0):
# for child in meta.children:
# print " "*level + child.name
# print_children(child, level+1)
# def get_results():
# results = load_results()
# reorganized_results = reorganize_results(results)
# return reorganized_results
# def load_results():
# """Load the parsed results from a json file and return them as a JSON
# object.
# """
# config = open_config_json()
# resultpath = config["outputpath"]
# resultfile = open(resultpath, "r")
# resultjson = json.load(resultfile)
# return resultjson
# def reorganize_results(jsonobject):
# metainfoenv = load_metainfo()
# events = jsonobject[0]["events"]
# sectionStack = []
# rootSection = None
# lastRepeated = None
# parent = None
# for event in events:
# eventName = event["event"]
# metaName = event["metaName"]
# metainfo = metainfoenv[metaName]
# repeats = metainfo.get("repeats", False)
# # Add new section
# if eventName == "openSection":
# section = Section()
# section.name = metaName
# section.metainfo = metainfo
# if repeats and lastRepeated is None:
# lastRepeated = section
# print "Repeating found: " + metaName
# if lastRepeated and lastRepeated.name != metaName:
# print "Closing repeated: " + metaName
# lastRepeated = None
# try:
# parentSection = sectionStack[-2]
# except IndexError:
# parentSection = None
# childSection = sectionStack[-1]
# if parentSection:
# print "Popping " + metaName
# parentSection.subSections.append(childSection)
# childSection.values = childSection.valueDict.values()
# childSection.arrayValues = childSection.arrayDict.values()
# childSection.valueDict = None
# childSection.arrayDict = None
# childSection.subSectionDict = None
# sectionStack.pop()
# if not(lastRepeated and lastRepeated.name == metaName):
# sectionStack.append(section)
# parent = section
# # Add value
# elif eventName == "addValue":
# value = parent.valueDict.get(metaName)
# if value is None:
# value = Value()
# value.name = metaName
# value.metainfo = metainfo
# value.values.append(event["value"])
# parent.valueDict[metaName] = value
# # Add array value
# elif eventName == "addArrayValues":
# arrayValue = ArrayValue()
# arrayValue.name = metaName
# arrayValue.metainfo = metainfo
# arrayValue.flatValues.append(event["flatValues"])
# arrayValue.valuesShape.append(event["valuesShape"])
# section = sectionStack[-1]
# section.arrayDict[metaName].append(arrayValue)
# # Close section
# elif eventName == "closeSection":
# if lastRepeated and lastRepeated.name == metaName:
# # print "Skipping repeated close"
# continue
# try:
# parentSection = sectionStack[-2]
# except IndexError:
# parentSection = None
# childSection = sectionStack[-1]
# if parentSection:
# parentSection.subSections.append(childSection)
# print "Popping " + metaName
# childSection.values = childSection.valueDict.values()
# childSection.arrayValues = childSection.arrayDict.values()
# childSection.valueDict = None
# childSection.arrayDict = None
# childSection.subSectionDict = None
# sectionStack.pop()
# rootSection = sectionStack[0]
# rootSection.values = rootSection.valueDict.values()
# rootSection.arrayValues = rootSection.arrayDict.values()
# rootSection.valueDict = None
# rootSection.arrayDict = None
# rootSection.subSectionDict = None
# print rootSection.subSections
# # print sectionStack[1]
# jsonobject = json.dumps(rootSection, default=lambda o: o.__dict__)
# return jsonobject
# class Result(object):
# def __init__(self, name=None, metainfo=None):
# self.name = name
# self.metainfo = metainfo
# class Section(Result):
# def __init__(self, name=None):
# self.name = name
# self.subSections = []
# self.valueDict = defaultdict(list)
# self.arrayDict = defaultdict(list)
# class Value(Result):
# def __init__(self, name=None, value=None):
# self.values = []
# class ArrayValue(Result):
# def __init__(self, name=None):
# self.flatValues = []
# self.valuesShape = []
# if __name__ == "__main__":
# print get_metainfos()
# # get_results()
from __future__ import unicode_literals
from django.db import models
# Create your models here.
from django.test import TestCase
# Create your tests here.
from django.conf.urls import url
from . import views
urlpatterns = [
url(r'^metainfos$', views.metainfos, name='metainfos'),
]
from django.http import HttpResponse
from nomadtoolkit.metaviewer.backend.contents import get_metainfos
def metainfos(request):
metainfos = get_metainfos()
return HttpResponse(metainfos)
Markdown is supported
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