aixcalibuha.utils package

Package containing utility functions used in different packages. Contains a statistics analyzer and a visualizer.

exception aixcalibuha.utils.MaxIterationsReached[source]

Bases: Exception

Exception raised for when the calibration ends because the maximum number of allowed iterations is reached.

aixcalibuha.utils.validate_cal_class_input(calibration_classes: CalibrationClass | List[CalibrationClass]) List[CalibrationClass][source]

Check if given list contains only CalibrationClass objects or is one and return a list in both cases. Else raise an error

Submodules

aixcalibuha.utils.configuration module

Module to with configs and functions to read configs for objects in this repository.

aixcalibuha.utils.configuration.get_calibration_classes_from_config(config)[source]

Read the data for a CalibrationClass object.

Parameters:

config (list) – List of dicts with configs holding the following cols for - names - start_time - stop_time - Optional: goals, tuner_paras, relevant_intervals

Returns:

TunerParas tuner_paras Loaded Goals object

aixcalibuha.utils.configuration.get_goals_from_config(config)[source]

Read the data for a Goals object.

Parameters:

config (dict) – Config holding the following cols for - meas_target_data - variable_names - Optional: weightings

Returns:

Goals goals Loaded Goals object

aixcalibuha.utils.configuration.get_tuner_paras_from_config(config)[source]

Read the data for a TunerParas object.

Parameters:

config (dict) – Config holding the following cols for - names - initial_values - bounds

Returns:

TunerParas tuner_paras Loaded Goals object

aixcalibuha.utils.configuration.read_config(filepath)[source]

Read the given file and return the toml-config

Parameters:

filepath (str,os.path.normpath) – Filepath with the config.

Returns:

dict config: Loaded config

aixcalibuha.utils.configuration.write_config(filepath, config)[source]

Write the given config to the filepath. If the file already exists, the data is recursively updated.

Parameters:

filepath (str,os.path.normpath) – Filepath with the config.

Param:

dict config: Config to be saved

aixcalibuha.utils.visualizer module

Module with classes and function to help visualize different processes inside the framework. Both plots and print-function/log-function will be implemented here. The Visualizer Class inherits the Logger class, as logging will always be used as a default.

class aixcalibuha.utils.visualizer.CalibrationLogger(cd, name, calibration_class, logger=None)[source]

Bases: object

Base class for showing the process of functions in this Framework with print-statements and saving everything relevant as a log-file.

Parameters:
  • cd (str,os.path.normpath) – Directory where to store the output of the Logger and possible child-classes. If the given directory can not be created, an error will be raised.

  • name (str) – Name of the reason of logging, e.g. classification, processing etc.

  • calibration_class (aixcalibuha.CalibrationClass) – Calibration class used in the calibration-process.

  • logger (logging.Logger) – If given, this logger is used to print and or save the messsages. Else, a new one is set up.

calibrate_new_class(calibration_class, cd=None, for_validation=False)[source]

Function to setup the figures for a new class of calibration. This function is called when instantiating this Class. If you uses continuuos calibration classes, call this function before starting the next calibration-class.

Parameters:
  • calibration_class (aixcalibuha.CalibrationClass) – Class holding information on names, tuner_paras, goals and time-intervals of calibration.

  • cd (str,os.path.normpath) – Optional change in working directory to store files

  • for_validation (bool) – If it’s only for validation, only plot the goals

calibration_callback_func(xk, obj, verbose_information, penalty=None)[source]

Logs the current values of the objective function.

Parameters:
  • xk (np.array) – Array with the current values of the calibration

  • obj (float) – Current objective value.

  • verbose_information (dict) – A dict with difference-values of for all goals and the corresponding weightings

  • penalty (float) – Penaltyfactor from current evaluation

property calibration_class: CalibrationClass

Get current calibration class object

property cd: str

Get the current working directory for storing plots.

decimal_prec = 6
error(msg)[source]

Wrapper function to directly log an error

property goals: Goals

Get current goals instance

integer_prec = 4
log(msg, level=20)[source]

Wrapper function to directly log in the internal logger

log_initial_names()[source]

Function to log the initial names and the statistical measure before calibration.

log_intersection_of_tuners(intersected_tuner_parameters, **kwargs)[source]

If an intersection for multiple classes occurs, an information about the statistics of the dataset has to be provided.

Parameters:

intersected_tuner_parameters (dict) – Dict with cols being the name of the tuner parameter and the value being the list with all the different “best” values for the tuner parameter.

save_calibration_result(best_iterate, model_name, **kwargs)[source]

Process the result, re-run the simulation and generate a logFile for the minimal quality measurement

Parameters:
  • best_iterate (dict) – Result object of the minimization

  • model_name (str) – Name of the model being calibrated

property tuner_paras: TunerParas
validation_callback_func(obj)[source]

Log the validation result information

Parameters:

obj (float) – Objective value of validation.

class aixcalibuha.utils.visualizer.CalibrationVisualizer(cd, name, calibration_class, logger=None, **kwargs)[source]

Bases: CalibrationLogger

More advanced class to not only log ongoing function evaluations but also show the process of the functions by plotting interesting causalities and saving these plots.

Parameters:
  • show_plot (boolean) – If False, all created plots are not shown during calibration but only stored at the end of the process.

  • create_tsd_plot (boolean) – If False, the plot of the time series data (goals) is not created and thus shown in during calibration. It therefore is also not stored, even if you set the save_tsd_plot keyword-argument to true.

  • save_tsd_plot (boolean) – If True, at each iteration the created plot of the time-series is saved. This may make the process much slower

  • show_plot_pause_time (float) – Set the time (in seconds) the plt.draw() pauses. May be altered if show_plot yields plot which disappear to fast. Default is 1-e3 s.

calibrate_new_class(calibration_class, cd=None, for_validation=False)[source]

Function to setup the figures for a new class of calibration. This function is called when instantiating this Class. If you uses continuuos calibration classes, call this function before starting the next calibration-class.

Parameters:
  • calibration_class (aixcalibuha.CalibrationClass) – Class holding information on names, tuner_paras, goals and time-intervals of calibration.

  • cd (str,os.path.normpath) – Optional change in working directory to store files

  • for_validation (bool) – If it’s only for validation, only plot the goals

calibration_callback_func(xk, obj, verbose_information, penalty=None)[source]

Logs the current values of the objective function.

Parameters:
  • xk (np.array) – Array with the current values of the calibration

  • obj (float) – Current objective value.

  • verbose_information (dict) – A dict with difference-values of for all goals and the corresponding weightings

log_intersection_of_tuners(intersected_tuner_parameters, **kwargs)[source]

If an intersection for multiple classes occurs, an information about the statistics of the dataset has to be provided.

Parameters:

intersected_tuner_parameters (dict) – Dict with cols being the name of the tuner parameter and the value being the list with all the different “best” values for the tuner parameter.

save_calibration_result(best_iterate, model_name, **kwargs)[source]

Process the result, re-run the simulation and generate a logFile for the minimal quality measurement

Parameters:
  • best_iterate (scipy.optimize.minimize.result) – Result object of the minimization

  • model_name (str) – Name of the model being calibrated

validation_callback_func(obj)[source]

Log the validation result information. Also plot if selected.

Parameters:

obj (float) – Objective value of validation.

aixcalibuha.utils.visualizer.short_name(ini_name: str, max_len: int)[source]

Shortens long strings to a max length from the front. long_string_name => …ing_name with len(new_name) = max_len

Parameters:
  • ini_name (str) – Long string to shorten.

  • max_len (int) – Max len of the new string.

Returns:

str The shorten string.