Commit 5bc09fba authored by Markus Scheidgen's avatar Markus Scheidgen
Browse files

More examples for working with metainfo definitions.

parent 1ecc24e6
# pylint: skip-file
# type: ignore
from nomad import metainfo
from nomad.datamodel.metainfo import public, common
# Access the quantities of a section definition
......@@ -40,3 +41,52 @@ import nomad.datamodel.datamodel # noqa
print(json.dumps(nomad.datamodel.datamodel.m_package.m_to_dict(), indent=2))
print(json.dumps(public.m_package.m_to_dict(), indent=2))
# Using an environment that manages multiple packages and provides utility functions
# to find definitions by name.
from nomad.datamodel.metainfo import m_env # noqa, contains all common, public, general metainfo
from vaspparser.metainfo import m_env as vasp_m_env # noqa, contains also the vasp specific definitions
# Resolve definition by name
print(m_env.resolve_definition('atom_labels', metainfo.Quantity))
# Traverse all definitions:
for definition in m_env.m_all_contents():
# Dimensions are either numbers or rangens (e.g. 3, 1..3, 0..*) or references to
# shapeless, unitless, integer quantities (usually) of the same section.
# These quantities are not specifically designated as dimensions, because they represent
# quantities in their own right and are often used on their own.
# Dimensions of a specific quantity:
quantity = public.section_system.atom_labels
for dim in quantity.shape:
if isinstance(dim, str):
section = quantity.m_parent
print('%s[%s]: %s' % (, dim, m_env.resolve_definition(dim, metainfo.Quantity)))
# All quantities used as dimensions in a package:
for definition in public.m_package.m_all_contents():
if definition.m_def == metainfo.Quantity.m_def:
for dim in definition.shape:
if isinstance(dim, str) and '..' not in dim:
print('%s[%s]: %s' % (, dim, m_env.resolve_definition(dim, metainfo.Quantity)))
except KeyError:
# metainfo is still not perfect
print('does not exist: ' + dim)
# Categories are special classes, similar to sections and they Python definition is a
# subclass of MCategory or MSection:
print(public.atom_forces_type, issubclass(public.atom_forces_type, metainfo.MCategory))
print(public.section_system, issubclass(public.section_system, metainfo.MSection))
# Or the definition of the definition is Category or Section respectively:
print(public.atom_forces_type, public.atom_forces_type.m_def == metainfo.Category.m_def)
print(public.section_system, public.section_system.m_def == metainfo.Section.m_def)
# Get all sections and categories definitions in a package:
# Access the categories of a metainfo definition, e.g. quantity
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