Skip to content
Snippets Groups Projects
Commit 310c15dc authored by Himanen, Lauri (himanel1)'s avatar Himanen, Lauri (himanel1)
Browse files

Added unit conversion support to ActiveBackend. Added datamanagers in function...

Added unit conversion support to ActiveBackend. Added datamanagers in function ActiveBackend.activeBackend() for the metainfo type 'type_dimension' because I couldn't add metainfo 'atom_number' without it.
parent 57fb1692
Branches
Tags
No related merge requests found
...@@ -3,6 +3,7 @@ import numpy as np ...@@ -3,6 +3,7 @@ import numpy as np
import sys import sys
import logging import logging
from nomadcore import parser_backend from nomadcore import parser_backend
from nomadcore.unit_conversion import unit_conversion
class CachingLevel(object): class CachingLevel(object):
Forward = 1 Forward = 1
...@@ -192,7 +193,7 @@ class ActiveBackend(object): ...@@ -192,7 +193,7 @@ class ActiveBackend(object):
onClose = onClose.get(ik.name, [])) onClose = onClose.get(ik.name, []))
dataManagers = {} dataManagers = {}
for ikNames, ik in metaInfoEnv.infoKinds.items(): for ikNames, ik in metaInfoEnv.infoKinds.items():
if ik.kindStr == "type_document_content": if ik.kindStr == "type_document_content" or ik.kindStr == "type_dimension":
superSectionNames = metaInfoEnv.firstAncestorsByType(ik.name).get("type_section", [[]])[0] superSectionNames = metaInfoEnv.firstAncestorsByType(ik.name).get("type_section", [[]])[0]
if not superSectionNames: if not superSectionNames:
raise Exception("MetaInfo of conrete value %s is not in any superSection" % ik.name) raise Exception("MetaInfo of conrete value %s is not in any superSection" % ik.name)
...@@ -290,10 +291,14 @@ class ActiveBackend(object): ...@@ -290,10 +291,14 @@ class ActiveBackend(object):
if cachingLevel == CachingLevel.ForwardAndCache or cachingLevel == CachingLevel.Cache: if cachingLevel == CachingLevel.ForwardAndCache or cachingLevel == CachingLevel.Cache:
dataManager.superSectionManager.addValue(dataManager.metaInfo, value, gIndex) dataManager.superSectionManager.addValue(dataManager.metaInfo, value, gIndex)
def addRealValue(self, metaName, value, gIndex = -1): def addRealValue(self, metaName, value, gIndex = -1, unit=None):
"""adds a floating point value corresponding to metaName """Adds a floating point value corresponding to metaName The value is
The value is added to the section the meta info metaName is in added to the section the meta info metaName is in A gIndex of -1 means
A gIndex of -1 means the latest section""" the latest section
"""
if unit:
value = self.convert_unit(value, unit)
dataManager = self.dataManagers[metaName] dataManager = self.dataManagers[metaName]
cachingLevel = dataManager.cachingLevel cachingLevel = dataManager.cachingLevel
if cachingLevel == CachingLevel.Forward or cachingLevel == CachingLevel.ForwardAndCache: if cachingLevel == CachingLevel.Forward or cachingLevel == CachingLevel.ForwardAndCache:
...@@ -315,8 +320,12 @@ class ActiveBackend(object): ...@@ -315,8 +320,12 @@ class ActiveBackend(object):
if cachingLevel == CachingLevel.ForwardAndCache or cachingLevel == CachingLevel.Cache: if cachingLevel == CachingLevel.ForwardAndCache or cachingLevel == CachingLevel.Cache:
dataManager.superSectionManager.addArray(dataManager.metaInfo, shape, gIndex) dataManager.superSectionManager.addArray(dataManager.metaInfo, shape, gIndex)
def setArrayValues(self, metaName, values, offset = None, gIndex = -1): def setArrayValues(self, metaName, values, offset = None, gIndex = -1, unit=None):
"""adds values to the last array added, array must be a numpy array""" """Adds values to the last array added, array must be a numpy array
"""
if unit:
values = self.convert_unit(values, unit)
dataManager = self.dataManagers[metaName] dataManager = self.dataManagers[metaName]
cachingLevel = dataManager.cachingLevel cachingLevel = dataManager.cachingLevel
if cachingLevel == CachingLevel.Forward or cachingLevel == CachingLevel.ForwardAndCache: if cachingLevel == CachingLevel.Forward or cachingLevel == CachingLevel.ForwardAndCache:
...@@ -325,9 +334,13 @@ class ActiveBackend(object): ...@@ -325,9 +334,13 @@ class ActiveBackend(object):
if cachingLevel == CachingLevel.ForwardAndCache or cachingLevel == CachingLevel.Cache: if cachingLevel == CachingLevel.ForwardAndCache or cachingLevel == CachingLevel.Cache:
dataManager.superSectionManager.setArrayValues(dataManager.metaInfo, values, offset, gIndex) dataManager.superSectionManager.setArrayValues(dataManager.metaInfo, values, offset, gIndex)
def addArrayValues(self, metaName, values, gIndex = -1): def addArrayValues(self, metaName, values, gIndex = -1, unit=None):
"""adds an array value with the given array values. """Adds an array value with the given array values. values must be a
values must be a numpy array""" numpy array.
"""
if unit:
values = self.convert_unit(values, unit)
dataManager = self.dataManagers[metaName] dataManager = self.dataManagers[metaName]
cachingLevel = dataManager.cachingLevel cachingLevel = dataManager.cachingLevel
if cachingLevel == CachingLevel.Forward or cachingLevel == CachingLevel.ForwardAndCache: if cachingLevel == CachingLevel.Forward or cachingLevel == CachingLevel.ForwardAndCache:
...@@ -347,3 +360,11 @@ class ActiveBackend(object): ...@@ -347,3 +360,11 @@ class ActiveBackend(object):
metaInfo = self.metaInfoEnv().infoKindEl(metaName) metaInfo = self.metaInfoEnv().infoKindEl(metaName)
dtypeStr = metaInfo.dtypeStr dtypeStr = metaInfo.dtypeStr
return numpy.zeros(shape, dtype = parser_backend.numpyDtypeForDtypeStr(dtypeStr)) return numpy.zeros(shape, dtype = parser_backend.numpyDtypeForDtypeStr(dtypeStr))
def convert_unit(self, value, unit):
"""Try to perform a unit conversion.
"""
converted = unit_conversion.convert_unit(value, unit)
if converted is None:
raise Exception("ActiveBackend cannot convert to unit '{}'".format(unit))
return converted
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment