utilities

Module of common utility methods and attributes used by all the modules.

class mloop.utilities.NullQueueListener

Bases: object

Shell class with start and stop functions that do nothing. Queue listener is not implemented in python 2. Current fix is to simply use the multiprocessing class to pipe straight to the cmd line if running on python 2. This is class is just a placeholder.

start()

Does nothing

stop()

Does nothing

mloop.utilities._config_logger(log_filename='M-LOOP', file_log_level=10, console_log_level=20, **kwargs)

Configure and the root logger.

Keyword Arguments:
 
  • log_filename (Optional [string]) – Filename prefix for log. Default MLOOP run . If None, no file handler is created
  • file_log_level (Optional[int]) – Level of log output for file, default is logging.DEBUG = 10
  • console_log_level (Optional[int]) – Level of log output for console, defalut is logging.INFO = 20
Returns:

Dict with extra keywords not used by the logging configuration.

Return type:

dictionary

mloop.utilities._generate_legend_labels(param_indices, all_param_names)

Generate a list of labels for the legend of a plot.

This is a helper function for visualization methods, used to generate the labels in legends for plots that show the values for optimization parameters. The label has the parameter’s index and, if available, a colon followed by the parameter’s name e.g. ‘3: some_name’. If no name is available, then the label will simply be a string representation of the parameter’s index, e.g. ‘3’.

Parameters:
  • param_indices (list-like of int) – The indices of the parameters for which labels should be generated. Generally this should be the same as the list of indices of parameters included in the plot.
  • all_param_names (list-like of str) – The names of all parameters from the optimization. Note this this argument should be all of the names for all of the parameters, not just the ones to be included in the plot legend.
Returns:

The labels generated for use in a plot legend.

Return type:

labels (list of str)

mloop.utilities._param_names_from_file_dict(file_dict)

Extract the value for ‘param_names’ from a training dictionary.

Versions of M-LOOP <= 2.2.0 didn’t support the param_names option, so archives generated by those versions do not have an entry for param_names. This helper function takes the dict generated when get_dict_from_file() is called on an archive and returns the value for param_names if present. If there is no entry for param_names, it returns a list of empty strings. This makes it possible to use the param_names data in archives from newer versions of M-LOOP while retaining the ability to plot data from archives generated by older versions of M-LOOP.

Parameters:file_dict (dict) – A dict containing data from an archive, such as those returned by get_dict_from_file().
Returns:
List of the names of the optimization
parameters if present in file_dict. If not present, then param_names will be set to None.
Return type:param_names (list of str)
mloop.utilities.check_file_type_supported(file_type)

Checks whether the file type is supported

Returns:True if file_type is supported, False otherwise.
Return type:bool
mloop.utilities.chunk_list(list_, chunk_size)

Divide a list into sublists of length chunk_size.

All elements in list_ will be included in exactly one of the sublists and will be in the same order as in list_. If the length of list_ is not divisible by chunk_size, then the final sublist returned will have fewer than chunk_size elements.

Examples

>>> chunk_list([1, 2, 3, 4, 5], 2)
[[1, 2], [3, 4], [5]]
>>> chunk_list([1, 2, 3, 4, 5], None)
[[1, 2, 3, 4, 5]]
>>> chunk_list([1, 2, 3, 4, 5], float('inf'))
[[1, 2, 3, 4, 5]]
Parameters:
  • list (list-like) – A list (or similar) to divide up into smaller lists.
  • chunk_size (int) – The number of elements to have in each sublist. The last sublist will have fewer elements than this if the length of list_ is not divisible by chunk_size. If set to float(‘inf’) or None, then all elements will be put into one sublist.
Returns:

List of sublists, each of which contains elements from the input

list_. Each sublist has length chunk_size except for the last one which may have fewer elements.

Return type:

(list)

mloop.utilities.config_logger(**kwargs)

Wrapper for _config_logger.

mloop.utilities.datetime_to_string(datetime)

Method for changing a datetime into a standard string format used by all packages.

mloop.utilities.dict_to_txt_file(tdict, filename)

Method for writing a dict to a file with syntax similar to how files are input.

Parameters:
  • tdict (dict) – Dictionary to be written to file.
  • filename (string) – Filename for file.
mloop.utilities.generate_filename_suffix(file_type, file_datetime=None, random_bytes=False)

Method for generating a string with date and extension for end of file names.

This method returns a string such as ‘_2020-06-13_04-20.txt’ where the date and time specify when this function was called.

Parameters:
  • file_type (string) – The extension to use at the end of the filename, e.g. ‘txt’. Note that the period should NOT be included.
  • file_datetime (Optional datetime.datetime) – The date and time to use in the filename suffix, represented as an instance of the datetime class defined in the datetime module. If set to None, then this function will use the result returned by datetime.datetime.now(). Default None.
  • random_bytes (Optional bool) – If set to True, six random bytes will be added to the filename suffix. This can be useful avoid duplication if multiple filenames are created with the same datetime.
Returns:

A string giving the suffix that can be appended to a filename

prefix to give a full filename with timestamp and extension, such as ‘_2020-06-13_04-20.txt’. The date and time specify when this function was called.

Return type:

string

mloop.utilities.get_controller_type_from_learner_archive(learner_filename)

Determine the controller_type used in an optimization.

This function returns the value used for controller_type during an optimization run, determined by examining the optimization’s learner archive.

Parameters:learner_filename (String) – The file name including extension, and optionally including path, of a learner archive.
Returns:
A string specifying the value for
controller_type used during the optimization run that produced the provided learner archive.
Return type:controller_type (String)
mloop.utilities.get_dict_from_file(filename, file_type=None)

Method for getting a dictionary from a file, of a given format.

Parameters:filename (str) – The filename for the file.
Keyword Arguments:
 file_type (Optional str) – The file_type for the file. Can be ‘mat’ for matlab, ‘txt’ for text, or ‘pkl’ for pickle. If set to None, then file_type will be automatically determined from the file extension. Default None.
Returns:Dictionary of values in file.
Return type:dict
mloop.utilities.get_file_type(filename)

Get the file type of a file from the extension in its filename.

Parameters:filename (String) – The filename including extension, and optionally including path, from which to extract the file type.
Returns:
The file’s type, inferred from its extension. The
type does NOT include a leading period.
Return type:file_type (String)
mloop.utilities.safe_cast_to_array(in_array)

Attempts to safely cast the input to an array. Takes care of border cases

Parameters:in_array (array or equivalent) – The array (or otherwise) to be converted to a list.
Returns:array that has been squeezed and 0-D cases change to 1-D cases
Return type:array
mloop.utilities.safe_cast_to_list(in_array)

Attempts to safely cast a numpy array to a list, if not a numpy array just casts to list on the object.

Parameters:in_array (array or equivalent) – The array (or otherwise) to be converted to a list.
Returns:List of elements from in_array
Return type:list
mloop.utilities.save_dict_to_file(dictionary, filename, file_type=None)

Method for saving a dictionary to a file, of a given format.

Parameters:
  • dictionary – The dictionary to be saved in the file.
  • filename – The filename for the saved file.
Keyword Arguments:
 

file_type (Optional str) – The file_type for the file. Can be ‘mat’ for matlab, ‘txt’ for text, or ‘pkl’ for pickle. If set to None, then file_type will be automatically determined from the file extension. Default None.

mloop.utilities.txt_file_to_dict(filename)

Method for taking a file and changing it to a dict. Every line in file is a new entry for the dictionary and each element should be written as:

[key] = [value]

White space does not matter.

Parameters:filename (string) – Filename of file.
Returns:Dictionary of values in file.
Return type:dict