Output Handler

Created on Sun May 1 12:06:06 2016

@author: Jonas Eschle “Mayou36”

class raredecay.tools.output.OutputHandler[source]

Bases: object

Class for output handling.

Initialize an output handler

IO_to_string()[source]

Rederict stdout (print etc.) to string.

IO_to_sys(importance=3, **add_output_kwarg)[source]

Direct stdout back to the sys.stdout and return/save string to output.

Parameters:importance (int {0, 1, 2, 3, 4, 5}) –
The importance of the output. The higher, the more likely it will
be added to the output. To not add it at all but only rederict
the output, choose 0.
Additional keyword-arguments for the
add_output() method can be
passed.
Returns:out – Returns the collected string from the redirection.
Return type:str
add_output(data_out, to_end=False, importance=3, title=None, subtitle=None, section=None, obj_separator=' ', data_separator='\n\n', force_newline=True)[source]

A method to collect the output and format it nicely.

All the objects in data_out get converted to strings and concatenated with obj_separator in between. After the objects, a data_separator is added. In the end, the whole output gets printed to a file and saved.

Available options:
  • You can add the data at the end of the output file instead of right in place.
  • You can add the data to the output “silently”, without printing.
  • Add title, subtitle and section on top of the data.
Parameters:
  • data_out (obj or list(obj, obj, obj, ..)) – The data to be added to the output. Has to be convertible to str!
  • to_end (boolean) – If True, the data will be added at the end of the file and not printed. For example all information which is not interesting in the run but maybe later, like configuration, version number etc.
  • importance (int {0, 1, 2, 3, 4, 5}) – The importance of the output. The higher, the more likely it gets printed (otherwise only saved). A 0 means “don’t print, only save”. The decisive variable is the verbosity level. The lower the verbosity level, the less likely the output will be printed.
  • title (str) – The title of the data_out, like “roc auc of different classifiers”. If None, no title will be set.
  • subtitle (str) – A subtitle which can be additional to a title or exclusive.
  • section (str) – The section title. Can be additional to the others or exclusive.
  • obj_separator (str) – The separator between the objects in data_out. Default is a new line.
  • data_separator (str) – Separates the data_outs from each other. Inserted at the end and creates a separation from the next call of add_output. Default is a blank line as separation.
  • force_newline (boolean) – If true, the data_out will be written on a new line and not just concatenated to the data written before
figure(*args, **kwargs)[source]

FUTURE: Wrapper around save_fig().

finalize(show_plots=True, play_sound_at_end=False)[source]

Finalize the run. Save everything and plot.

Parameters:
  • show_plots (boolean) – If True, show the plots. Equivalent to writing plt.show().
  • play_sound_at_end (boolean) – If True, tries to play a beep-sound at the end of a run to let you know it finished.
get_logger_path()[source]

Return the path for the log folder.

get_plots_path()[source]

Return the path for the log folder.

initialize(run_name='', prompt_for_comment=False)[source]

Initialization for external use, no folders created, config.py logger.

initialize_save(output_path, run_name='', run_message='', output_folders=None, del_existing_folders=False, logger_cfg=None)[source]

Initialize the run. Create the neccesary folders.

Parameters:
  • Practice (Best) –
  • output_path (str) – Absolute path to the folder where the run output folder will be created (named after the run) which will contain all the output folders (logfile, figures, output etc)
  • run_name (str) – The name of the run and also of the output folder.
  • run_message (str) – A message that is displayed below the titel: a further comment on what you do in the script
  • output_folders (dict) – Contain the name of the folders for the different outputs. For the available keys see __DEFAULT_OUTPUT_FOLDERS.
  • del_existing_dir (boolean) – If True, an already existing folder with the same name will be deleted. If False and the folder exists already, an exception will be raised.
  • logger_cfg (dict) – The configuration for the logger, which will be created later. If not specified (or only a few arguments), the meta_config will be taken.
make_me_a_logger()[source]

Create a logger in OutputHandler instance. Dependency “hack”.

Call after initialize_save() has ben called.

save_fig(figure, importance=3, file_format=None, to_pickle=True, **save_cfg)[source]

Advanced matplotlib.pyplot.figure(). Create and save a certain figure at the end of the run.

To create and save a figure, you just enter an already created or a new figure as a Parameters and specify the fileformats it should be saved to. The figure can also be pickled so that it can be re-plotted anytime.

Note

The figure will be saved at the end of the run (by calling finalize()) so any change you made until the end will be applied to the plot.

Parameters:
  • figure (instance of matplotlib.figure.Figure (e.g. returned by matplotlib.figure())) – The figure to be saved.
  • importance ({0, 1, 2, 3, 4, 5}) – Specify the importance level, ranging from 1 to 5, of the plot. The higher the importance level, the more important. If the importance level is higher the more it will be plotted. If it is plotted depends on the plot verbosity set (5 - importance_level < plot_verbosity). Therefore, a 0 corresponds to “no plot” and a 5 means “always plot”.
  • file_format (str or list(str, str, str,..)) – The ending of the desired format, example: ‘png’ (default). If you don’t want to save it, enter a blank list.
  • to_pickle (boolean) – If True, the plot will be saved to a pickle file.
  • **save_cfg (keyword args) – Will be used as arguments in savefig()
Returns:

out – Return the figure.

Return type:

figure