Source code for dakotathon.plugins.base
#! /usr/bin/env python
"""An abstract base class for all Dakota model plugins."""
import os
import re
import yaml
from abc import ABCMeta, abstractmethod
[docs]def write_dflt_file(tmpl_file, parameters_file, run_duration=1.0):
"""Create a model input file populated with default values.
Parameters
----------
tmpl_file : str
The path to the template file defined for the model.
parameters_file : str
The path to the parameters file defined for the model.
run_duration : str, int, or float
Simulation run length, in undetermined units (default is 1.0).
Returns
-------
str or None
The path to the new dflt file, or None on an error.
"""
with open(tmpl_file, "r") as fp:
template = fp.read().split("\n")
with open(parameters_file, "r") as fp:
parameters = yaml.safe_load(fp)
parameters["_run_duration"] = {"value": {"default": str(run_duration)}}
defaults = template
for p_name in parameters.keys():
p_default = str(parameters[p_name]["value"]["default"])
for i, line_tmpl in enumerate(template):
if re.search(p_name, line_tmpl):
line_dflt_split = defaults[i].strip().split()
for j, item in enumerate(line_tmpl.strip().split()):
if item.startswith("{" + p_name):
line_dflt_split[j] = p_default
defaults[i] = " ".join(line_dflt_split)
dflt_file = os.path.splitext(os.path.basename(tmpl_file))[0] + ".dflt"
with open(dflt_file, "w") as ofp:
ofp.write("\n".join(defaults))
return dflt_file
[docs]def write_dtmpl_file(tmpl_file, dflt_input_file, parameter_names):
"""Create a template input file for use by Dakota.
In the CSDMS framework, the tmpl file is an input file for a
model, but with the parameter values replaced by
`{parameter_name}`. Dakota uses the same idea. This function
creates a Dakota dtmpl file from a CSDMS model tmpl file. Only
the parameters used by Dakota are left in the tmpl format; the
remainder are populated with default values for the model. The
dtmpl file is written to the current directory.
Parameters
----------
tmpl_file : str
The path to the template file defined for the model.
dflt_input_file : str
An input file that contains the default parameter values for a
model.
parameter_names : list of str
A list of parameter names for the model to be evaluated by
Dakota.
Returns
-------
str or None
The path to the new dtmpl file, or None on an error.
"""
with open(tmpl_file, "r") as fp:
txt_base_tmpl = fp.read().split("\n")
with open(dflt_input_file, "r") as fp:
txt_dflt_input = fp.read().split("\n")
for p_name in parameter_names:
for i, line_tmpl in enumerate(txt_base_tmpl):
if re.search(p_name, line_tmpl):
line_input_split = txt_dflt_input[i].strip().split()
for j, item in enumerate(line_tmpl.strip().split()):
if item.startswith("{" + p_name):
line_input_split[j] = "{" + p_name + "}"
txt_dflt_input[i] = " ".join(line_input_split)
dtmpl_file = os.path.splitext(os.path.basename(tmpl_file))[0] + ".dtmpl"
with open(dtmpl_file, "w") as fp:
fp.write("\n".join(txt_dflt_input))
return dtmpl_file
[docs]class PluginBase(object):
"""Describe features common to all Dakota plugins."""
__metaclass__ = ABCMeta
@abstractmethod
def __init__(self, **kwargs):
"""Define default attributes."""
pass
[docs] @abstractmethod
def setup(self, config):
"""Configure model inputs.
Sets attributes using information from the run configuration
file. The Dakota parsing utility ``dprepro`` reads parameters
from Dakota to create a new input file from a template.
Parameters
----------
config : dict
Stores configuration settings for a Dakota experiment.
"""
pass
[docs] @abstractmethod
def call(self):
"""Call the model through the shell."""
pass
[docs] @abstractmethod
def load(self, output_file):
"""Read data from a model output file.
Parameters
----------
output_file : str
The path to a model output file.
Returns
-------
array_like
A numpy array, or None on an error.
"""
pass
[docs] @abstractmethod
def calculate(self):
"""Calculate Dakota response functions."""
pass
[docs] @abstractmethod
def write(self, params_file, results_file):
"""Write a Dakota results file.
Parameters
----------
params_file : str
A Dakota parameters file.
results_file : str
A Dakota results file.
"""
pass