Input Parser
oqd_heisenberg_ion.common.inputs.input_parser
InputParser
Parses the provided list of inputs for a single parameter set. Converts strings to required types using the data schema and builds the input configurations for the driver
Source code in src/oqd_heisenberg_ion/common/inputs/input_parser.py
class InputParser:
"""
Parses the provided list of inputs for a single parameter set.
Converts strings to required types using the data schema and builds the input configurations for the driver
"""
def __init__(self, **config_settings):
"""
constructs an instance of InputParser. Builds the input_config settings for the driver layer
Args:
**config_settings (dict): key word arguments specifying a single parameter set with str values
"""
self.dtype_parsers = {
str: self.extract_string,
int: self.extract_integer,
float: self.extract_float,
bool: self.extract_bool,
"categorical": self.extract_categorical,
}
self.input_schema = input_schema
self.simulation_config = {}
self.build_input_config(config_settings)
def extract_integer(self, config_settings, key):
"""
converts str to int for parameter specified by key
Args:
config_settings (dict): contains the key value pairs that define the simulation parameter set
key (str): parameter key
Returns:
(int): parameter value
"""
return int(config_settings[key])
def extract_float(self, config_settings, key):
"""
converts str to float for parameter specified by key
Args:
config_settings (dict): contains the key value pairs that define the simulation parameter set
key (str): parameter key
Returns:
(float): parameter value
"""
return float(config_settings[key])
def extract_string(self, config_settings, key):
"""
extracts str parameter specified by key
Args:
config_settings (dict): contains the key value pairs that define the simulation parameter set
key (str): parameter key
Returns:
(str): parameter value
"""
return config_settings[key]
def extract_bool(self, config_settings, key):
"""
converts str to bool for parameter specified by key
Args:
config_settings (dict): contains the key value pairs that define the simulation parameter set
key (str): parameter key
Returns:
(bool): parameter value
"""
if isinstance(config_settings[key], bool):
return config_settings[key]
else:
if config_settings[key].capitalize() == "True":
return True
elif config_settings[key].capitalize() == "False":
return False
else:
raise ValueError(
f"Unrecognized entry for key {key}, value provided: {config_settings[key]}. "
"Allowed values are 'True' or 'False'\n"
)
def extract_categorical(self, config_settings, key):
"""
extracts categorical parameter specified by key. Checks if the provided value is allowed by schema
Args:
config_settings (dict): contains the key value pairs that define the simulation parameter set
key (str): parameter key
Raises:
ValueError: if value associated with key is unrecognized by schema
Returns:
(str): parameter value
"""
val = config_settings[key]
allowed_vals = self.input_schema[key]["Categories"]
if val in allowed_vals:
return convert_to_snake_case(val) if self.input_schema[key]["ConvertValCase"] else val
else:
raise ValueError(
f"Unrecognized input for key: {key}. Value provided: {val}. Allowed values: {allowed_vals}"
)
def build_input_config(self, config_settings):
"""
builds the entire input configuration corresponding to the parameter set with correct types
Args:
config_settings (dict): key word arguments specifying a single parameter set with str values
"""
for key in config_settings.keys():
dtype = self.input_schema[key]["DataType"]
param_type = self.input_schema[key]["ParameterType"]
parser = self.dtype_parsers[dtype]
val = parser(config_settings, key)
if param_type not in self.simulation_config:
self.simulation_config[param_type] = {}
self.simulation_config[param_type][key] = val
__init__
constructs an instance of InputParser. Builds the input_config settings for the driver layer
Parameters:
-
**config_settings(dict, default:{}) –key word arguments specifying a single parameter set with str values
Source code in src/oqd_heisenberg_ion/common/inputs/input_parser.py
def __init__(self, **config_settings):
"""
constructs an instance of InputParser. Builds the input_config settings for the driver layer
Args:
**config_settings (dict): key word arguments specifying a single parameter set with str values
"""
self.dtype_parsers = {
str: self.extract_string,
int: self.extract_integer,
float: self.extract_float,
bool: self.extract_bool,
"categorical": self.extract_categorical,
}
self.input_schema = input_schema
self.simulation_config = {}
self.build_input_config(config_settings)
extract_integer
converts str to int for parameter specified by key
Parameters:
-
config_settings(dict) –contains the key value pairs that define the simulation parameter set
-
key(str) –parameter key
Returns:
-
int–parameter value
Source code in src/oqd_heisenberg_ion/common/inputs/input_parser.py
extract_float
converts str to float for parameter specified by key
Parameters:
-
config_settings(dict) –contains the key value pairs that define the simulation parameter set
-
key(str) –parameter key
Returns:
-
float–parameter value
Source code in src/oqd_heisenberg_ion/common/inputs/input_parser.py
def extract_float(self, config_settings, key):
"""
converts str to float for parameter specified by key
Args:
config_settings (dict): contains the key value pairs that define the simulation parameter set
key (str): parameter key
Returns:
(float): parameter value
"""
return float(config_settings[key])
extract_string
extracts str parameter specified by key
Parameters:
-
config_settings(dict) –contains the key value pairs that define the simulation parameter set
-
key(str) –parameter key
Returns:
-
str–parameter value
Source code in src/oqd_heisenberg_ion/common/inputs/input_parser.py
extract_bool
converts str to bool for parameter specified by key
Parameters:
-
config_settings(dict) –contains the key value pairs that define the simulation parameter set
-
key(str) –parameter key
Returns:
-
bool–parameter value
Source code in src/oqd_heisenberg_ion/common/inputs/input_parser.py
def extract_bool(self, config_settings, key):
"""
converts str to bool for parameter specified by key
Args:
config_settings (dict): contains the key value pairs that define the simulation parameter set
key (str): parameter key
Returns:
(bool): parameter value
"""
if isinstance(config_settings[key], bool):
return config_settings[key]
else:
if config_settings[key].capitalize() == "True":
return True
elif config_settings[key].capitalize() == "False":
return False
else:
raise ValueError(
f"Unrecognized entry for key {key}, value provided: {config_settings[key]}. "
"Allowed values are 'True' or 'False'\n"
)
extract_categorical
extracts categorical parameter specified by key. Checks if the provided value is allowed by schema
Parameters:
-
config_settings(dict) –contains the key value pairs that define the simulation parameter set
-
key(str) –parameter key
Raises:
-
ValueError–if value associated with key is unrecognized by schema
Returns:
-
str–parameter value
Source code in src/oqd_heisenberg_ion/common/inputs/input_parser.py
def extract_categorical(self, config_settings, key):
"""
extracts categorical parameter specified by key. Checks if the provided value is allowed by schema
Args:
config_settings (dict): contains the key value pairs that define the simulation parameter set
key (str): parameter key
Raises:
ValueError: if value associated with key is unrecognized by schema
Returns:
(str): parameter value
"""
val = config_settings[key]
allowed_vals = self.input_schema[key]["Categories"]
if val in allowed_vals:
return convert_to_snake_case(val) if self.input_schema[key]["ConvertValCase"] else val
else:
raise ValueError(
f"Unrecognized input for key: {key}. Value provided: {val}. Allowed values: {allowed_vals}"
)
build_input_config
builds the entire input configuration corresponding to the parameter set with correct types
Parameters:
-
config_settings(dict) –key word arguments specifying a single parameter set with str values
Source code in src/oqd_heisenberg_ion/common/inputs/input_parser.py
def build_input_config(self, config_settings):
"""
builds the entire input configuration corresponding to the parameter set with correct types
Args:
config_settings (dict): key word arguments specifying a single parameter set with str values
"""
for key in config_settings.keys():
dtype = self.input_schema[key]["DataType"]
param_type = self.input_schema[key]["ParameterType"]
parser = self.dtype_parsers[dtype]
val = parser(config_settings, key)
if param_type not in self.simulation_config:
self.simulation_config[param_type] = {}
self.simulation_config[param_type][key] = val