Overview

Overview#

The purpose of the lattice_validator is to verify that unreliable input, such as input provided by the user, adheres to the input specifications of the lattice_builder. It uses the cerberus package to validate python dictionaries against schemata defining valid input.

The basic usage scenario for the lattice_validator is the following:

  • Setup an input (python) dictionary either directly in your python script or by loading a yaml file

  • Validate the dictionary using lattice_validator

Minimal python script implementing these steps (input dictionary defined in script):

# import pretty printer for displaying dictionaries
from pprint import PrettyPrinter
# import Validator class from lattice_validator package
from lattice_validator import Validator

# initialize pretty printer
pp = PrettyPrinter(indent=2)
# STEP 1
# create minimal dictionary for 'unitcell'
uc = {
    'atoms': [
        {'id': 0,}
    ],
    'bonds': [],
}
# create minimal dictionary for 'system'
sys = {
    'site_type': 'spinless_fermions',
}
# create minimal dictionary for 'configuration'
conf = {
    'unitcell': uc,
    'system': sys,
}
# STEP 2
# initialize Validator
val = Validator()
# validate 'configuration'
valid, conf_ = val.validateConfiguration(conf)
if not valid:
    # conf_ contains information about why 'configuration' is not valid
    print('Configuration invalid:')
    pp.pprint(conf_)

else:
    # conf_ contains *normalized* 'configuration' dictionary
    print('Normalized configuration:')
    pp.pprint(conf_)
    # ... do something useful with the normalized 'configuration' dictionary

Instead of defining the input directly in the python script, one can also use a yaml file. The minimal input defined above corresponds to the following yaml file:

unitcell:
    atoms:
        - id: 0
    bonds: []
system:
    site_type: spinless_fermions

Minimal python script implementing these steps (dictionary defined in yaml file input.yaml):

# import pretty printer for displaying dictionaries
from pprint import PrettyPrinter
# import pyyaml package for reading yaml files
import yaml
# import Validator class from lattice_validator package
from lattice_validator import Validator

# initialize pretty printer
pp = PrettyPrinter(indent=2)
# STEP 1
# loading yaml file 'input.yaml'
with open('input.yaml', mode='r') as input_file:
    # load yaml input to configuration dictionary
    conf = yaml.load(input_file, Loader=yaml.SafeLoader)

    # STEP 2
    # initialize Validator
    val = Validator()
    # validate 'configuration'
    valid, conf_ = val.validateConfiguration(conf)
    if not valid:
        # conf_ contains information about why 'configuration' is not valid
        print('Configuration invalid:')
        pp.pprint(conf_)

    else:
        # conf_ contains *normalized* 'configuration' dictionary
        print('Normalized configuration:')
        pp.pprint(conf_)
        # ... do something useful with the normalized 'configuration' dictionary

For more examples have a look at the Jupyter notebooks shipped in the examples directory.