agentlib_flexquant.utils package

Submodules

agentlib_flexquant.utils.config_management module

agentlib_flexquant.utils.config_management.get_class_from_file(file_path: str, class_name: str) ABCMeta[source]
agentlib_flexquant.utils.config_management.get_module(config: AgentConfig, module_type: str) T[source]

Extracts a module from a config based on its name.

agentlib_flexquant.utils.config_management.get_module_type_matching_dict(dictionary: dict) -> (<class 'dict'>, <class 'dict'>)[source]

Create two dictionaries, which map the modules types of the agentlib_mpc modules to those of the flexquant modules.

This is done by using the MODULE_TYPE_DICT.

agentlib_flexquant.utils.config_management.get_orig_module_type(config: AgentConfig) str[source]

Return the config type of the original MPC.

agentlib_flexquant.utils.config_management.to_dict_and_remove_unnecessary_fields(module: BaseModuleConfig) dict[source]

Remove unnecessary fields from the module to keep the created json simple.

agentlib_flexquant.utils.data_handling module

agentlib_flexquant.utils.data_handling.convert_timescale_of_index(df: DataFrame, from_unit: Literal['seconds', 'minutes', 'hours', 'days'], to_unit: {'seconds': 1, 'minutes': 60, 'hours': 3600, 'days': 86400}) DataFrame[source]

Convert the timescale of a dataframe index (from seconds) to the given time unit.

Parameters:
  • from_unit – the time unit of the original index

  • to_unit – the time unit to convert the index to

Returns:

A DataFrame with the converted index

agentlib_flexquant.utils.data_handling.fill_nans(series: Series, method: Literal['mean', 'interpolate']) Series[source]

Fill NaN values in the series with the given method.

Parameters:
  • series – the series to be filled

  • method – the method to be applied, there are two predefined - mean: fill NaN values with the mean of the following values. - interpolate: interpolate missing values.

Returns:

A pd.Series with nan filled.

agentlib_flexquant.utils.data_handling.strip_multi_index(series: Series) Series[source]

agentlib_flexquant.utils.interactive module

class agentlib_flexquant.utils.interactive.CustomBound(for_variable: str, lb_name: str | None = None, ub_name: str | None = None)[source]

Bases: object

Dataclass to let the user define custom bounds for the mpc variables.

for_variable – The name of the variable to plot the bounds into lower bound – The lower bound of the variable as the name of the lower bound variable in the MPC upper bound – The upper bound of the variable as the name of the upper bound variable in the MPC

for_variable: str
lower_bound: str | None
upper_bound: str | None
class agentlib_flexquant.utils.interactive.Dashboard(flex_config: str | Path | FlexQuantConfig | None = None, simulator_agent_config: str | Path | AgentConfig | None = None, generated_flex_files_base_path: str | Path | None = None, results: str | Path | dict[str, dict[str, pandas.core.frame.DataFrame]] = None, to_timescale: Literal['seconds', 'minutes', 'hours', 'days'] = 'hours', port: int = None)[source]

Bases: Results

Class for the dashboard of flexquant

MPC_ITERATIONS: str = 'iter_count'
bounds_key: str = 'bounds'
characteristic_times_accepted_key: str = 'characteristic_times_accepted'
characteristic_times_current_key: str = 'characteristic_times_current'
custom_bounds: list[agentlib_flexquant.utils.interactive.CustomBound] = []
label_negative: str = 'negative'
label_positive: str = 'positive'
show(custom_bounds: CustomBound | list[agentlib_flexquant.utils.interactive.CustomBound] = None)[source]
Show the dashboard in a web browser containing:

– Statistics of the MPCs solver – The states, controls, and the power variable of the MPCs and the simulator – KPIs of the flexibility quantification – Markings of the characteristic flexibility times

Parameters:

custom_bounds – optional arguments to show the comfort bounds

agentlib_flexquant.utils.parsing module

class agentlib_flexquant.utils.parsing.SetupSystemModifier(mpc_data: BaseMPCData, controls: List[MPCVariable], binary_controls: List[MPCVariable] | None)[source]

Bases: NodeTransformer

A custom AST transformer for modifying the MPC model file.

This class traverses the AST of the input file, identifies the relevant classes and methods, and performs the necessary modifications.

get_leftmost_list(node: Tuple | BinOp | List) List | None[source]

Recursively traverse binary operations to get the leftmost list.

Parameters:

node – An AST node (could be a BinOp or directly a List)

Returns:

The leftmost List node found

modify_config_class_baseline(node: ClassDef)[source]

Modify the config class of the baseline mpc.

Parameters:

node – The class definition node of the config.

modify_config_class_shadow(node: ClassDef)[source]

Modify the config class of the shadow mpc.

Parameters:

node – The class definition node of the config.

modify_setup_system_baseline(node: FunctionDef)[source]

Modify the setup_system method of the baseline mpc model class.

This method changes the return statement of the setup_system method and adds all necessary new lines of code.

Parameters:

node – The function definition node of setup_system.

modify_setup_system_shadow(node: FunctionDef)[source]

Modify the setup_system method of the shadow mpc model class.

This method changes the return statement of the setup_system method and adds all necessary new lines of code.

Parameters:

node – The function definition node of setup_system.

visit_ClassDef(node: ClassDef) ClassDef[source]

Visit a class definition in the AST.

This method is called for each class definition in the AST. It identifies the BaselineMPCModelConfig and BaselineMPCModel classes and performs the necessary actions.

Parameters:

node – The class definition node in the AST.

Returns:

The possibly modified class definition node.

visit_Module(module: Module) Module[source]

Visit a module definition in the AST.

Append or delete the import statements at the top of the module.

Parameters:

module – The module definition node in the AST.

Returns:

The possibly modified module definition node.

agentlib_flexquant.utils.parsing.add_import_to_tree(name: str, alias: str, tree: Module) Module[source]

Add import to the module.

The statement ‘import name as alias’ will be added.

Parameters:
  • name – name of the module to be imported

  • alias – alias of the module

  • tree – the tree to be imported

Returns:

The tree updated with the import statement

agentlib_flexquant.utils.parsing.add_input(name: str, value: bool | str | int, unit: str, description: str, type: str) Call[source]

Create an AST node for an input definition.

Parameters:
  • name – The name of the input.

  • value – The default value for the input. Can be a boolean, string, or integer.

  • unit – The unit associated with the input value.

  • description – A human-readable description of the input.

  • type – The data type of the input (e.g., “float”, “int”, “string”).

Returns:

An abstract syntax tree (AST) call node representing the input definition.

Return type:

ast.Call

agentlib_flexquant.utils.parsing.add_output(name: str, unit: str, type: str, value: str | float, description: str) Call[source]

Create an AST node for an output definition.

Parameters:
  • name – The name of the output.

  • unit – The unit associated with the output value.

  • type – The data type of the output (e.g., “float”, “string”).

  • value – The value of the output. Can be a string or float.

  • description – A human-readable description of the output.

Returns:

An abstract syntax tree (AST) call node representing the output definition.

Return type:

ast.Call

agentlib_flexquant.utils.parsing.add_parameter(name: str, value: int | float, unit: str, description: str) Call[source]

Create an AST node for a parameter definition.

Parameters:
  • name – The name of the parameter.

  • value – The value of the parameter. Can be an integer or float.

  • unit – The unit associated with the parameter value.

  • description – A human-readable description of the parameter.

Returns:

An abstract syntax tree (AST) call node representing the parameter definition.

Return type:

ast.Call

agentlib_flexquant.utils.parsing.create_ast_element(template_string: str) Call[source]

Convert a template string into an AST call node.

Parameters:

template_string – A Python code template string to parse.

Returns:

An abstract syntax tree (AST) call node parsed from the template string.

Return type:

ast.Call

agentlib_flexquant.utils.parsing.remove_all_imports_from_tree(tree: Module) Module[source]