trixi.logger¶
experiment¶
ExperimentLogger¶
-
class
trixi.logger.experiment.experimentlogger.
ExperimentLogger
(exp_name, base_dir, folder_format='%Y%m%d-%H%M%S_{experiment_name}', resume=False, text_logger_args=None, plot_logger_args=None, **kwargs)[source]¶ Bases:
trixi.logger.abstractlogger.AbstractLogger
A single class for logging your experiments to file.
It creates a experiment folder in your base folder and a folder structure to store your experiment files. The folder structure is:
base_dir/ new_experiment_folder/ checkpoint/ config/ img/ log/ plot/ result/ save/
-
load_config
(name, **kwargs)[source]¶ Loads a config from a json file from the experiment config dir
Parameters: name – the name of the config file Returns: A Config/ dict filled with the json file content
-
load_dict
(path)[source]¶ Loads a json file as dict from a sub path in the experiment save dir
Parameters: path – sub path to the file (starting from the experiment save dir) Returns: The restored data as a dict
-
load_numpy_data
(path)[source]¶ Loads a numpy file from a sub path in the experiment save dir
Parameters: path – sub path to the file (starting from the experiment save dir) Returns: The restored numpy array
-
load_pickle
(path)[source]¶ Loads a object via pickle from a sub path in the experiment save dir
Parameters: path – sub path to the file (starting from the experiment save dir) Returns: The restored object
-
resolve_format
(input_, resume)[source]¶ - Given some input pattern, tries to find the best matching folder name by resolving the format. Options are:
- Run-number: {run_number}
- Time: “%Y%m%d-%H%M%S
- Member variables (e.g experiment_name) : {variable_name} (e.g. {experiment_name})
Parameters: - input – The format to be resolved
- resume – Flag if folder should be resumed
Returns: The resolved folder name
-
save_config
(data, name, **kwargs)[source]¶ Saves a config as a json file in the experiment config dir
Parameters: - data – The data to be stored as config json
- name – The name of the json file in which the data will be stored
-
save_dict
(data, path, indent=4, separators=(', ', ': '), encoder_cls=<class 'trixi.util.util.MultiTypeEncoder'>, **kwargs)[source]¶ Saves a dict as a json file in the experiment save dir
Parameters: - data – The data to be stored as save file
- path – sub path in the save folder (or simply filename)
- indent – Indent for the json file
- separators – Separators for the json file
- encoder_cls – Encoder Class for the encoding to json
-
save_file
(filepath, path=None)[source]¶ Copies a file to the experiment save dir
Parameters: - filepath – Path to the file to be copied to the experiment save dir
- path – sub path to the target file (starting from the experiment save dir, does not have to exist yet)
-
save_numpy_data
(data, path)[source]¶ Saves a numpy array in the experiment save dir
Parameters: - data – The array to be stored as a save file
- path – sub path in the save folder (or simply filename)
-
save_pickle
(data, path)[source]¶ Saves a object data in the experiment save dir via pickle
Parameters: - data – The data to be stored as a save file
- path – sub path in the save folder (or simply filename)
-
save_result
(data, name, indent=4, separators=(', ', ': '), encoder_cls=<class 'trixi.util.util.MultiTypeEncoder'>, **kwargs)[source]¶ Saves data as a json file in the experiment result dir
Parameters: - data – The data to be stored as result json
- name – name of the result json file
- indent – Indent for the json file
- separators – Separators for the json file
- encoder_cls – Encoder Class for the encoding to json
-
show_barplot
(array, name, file_format='.png', **kwargs)[source]¶ This function saves a barplot in the experiment plot folder.
Parameters:
-
show_boxplot
(array, name, file_format='.png', **kwargs)[source]¶ This function saves a boxplot in the experiment plot folder.
Parameters:
-
show_image
(image, name, file_format='.png', **kwargs)[source]¶ This function saves an image in the experiment img folder.
Parameters:
-
show_lineplot
(y_vals, x_vals=None, name='lineplot', file_format='.png', **kwargs)[source]¶ This function saves a line plot in the experiment plot folder.
Parameters:
-
show_matplot_plt
(figure, name, file_format='.png', *args, **kwargs)[source]¶ This function saves a custom matplotlib figure in the experiment plot folder.
Parameters:
-
show_piechart
(array, name, file_format='.png', **kwargs)[source]¶ This function saves a piechart in the experiment plot folder.
Parameters:
-
show_scatterplot
(array, name, file_format='.png', **kwargs)[source]¶ This function saves a scatterplot in the experiment plot folder.
Parameters:
-
show_text
(text, name=None, logger='default', **kwargs)[source]¶ Logs a text to a log file.
Parameters: - text – The text to be logged
- name – Name of the text
- logger – log file (in the experiment log folder) in which the text will be logged.
- **kwargs –
-
PytorchExperimentLogger¶
-
class
trixi.logger.experiment.pytorchexperimentlogger.
PytorchExperimentLogger
(*args, **kwargs)[source]¶ Bases:
trixi.logger.experiment.experimentlogger.ExperimentLogger
A single class for logging your pytorch experiments to file. Extends the ExperimentLogger also also creates a experiment folder with a file structure:
- The folder structure is :
- base_dir/
- new_experiment_folder/
- checkpoint/ config/ img/ log/ plot/ result/ save/
-
static
get_classification_metrics
(tensor, labels, name='', metric=('roc-auc', 'pr-score'), use_sub_process=False, tag_name=None, results_fn=<function PytorchExperimentLogger.<lambda>>)[source]¶ Displays some classification metrics as line plots in a graph (similar to show value (also uses show value for the caluclated values))
Parameters: - tensor – Tensor with scores (e.g class probability )
- labels – Labels of the samples to which the scores match
- name – The name of the window
- metric – List of metrics to calculate. Options are: roc-auc, pr-auc, pr-score, mcc, f1
- tag_name – Name for the tag, if no given use name
- use_sub_process – Use a sub process to do the processing, if true nothing is returned
- results_fn – function which is called with the results/ return values. Expected f(val, name, tag)
Returns:
-
static
get_input_gradient
(model, inpt, err_fn, grad_type='vanilla', n_runs=20, eps=0.1, abs=False, results_fn=<function PytorchExperimentLogger.<lambda>>)[source]¶ Given a model creates calculates the error and backpropagates it to the image and saves it (saliency map).
Parameters: - model – The model to be evaluated
- inpt – Input to the model
- err_fn – The error function the evaluate the output of the model on
- grad_type – Gradient calculation method, currently supports (vanilla, vanilla-smooth, guided,
- guided-smooth) (the guided backprob can lead to segfaults -.-) –
- n_runs – Number of runs for the smooth variants
- eps – noise scaling to be applied on the input image (noise is drawn from N(0,1))
- abs (bool) – Flag, if the gradient should be a absolute value
- results_fn – function which is called with the results/ return values. Expected f(grads)
-
static
get_pr_curve
(tensor, labels, reduce_to_n_samples=None, use_sub_process=False, results_fn=<function PytorchExperimentLogger.<lambda>>)[source]¶ Displays a precision recall curve given a tensor with scores and the coresponding labels
Parameters: - tensor – Tensor with scores (e.g class probability )
- labels – Labels of the samples to which the scores match
- reduce_to_n_samples – Reduce/ downsample to to n samples for fewer data points
- use_sub_process – Use a sub process to do the processing, if true nothing is returned
- results_fn – function which is called with the results/ return values. Expected f(precision, recall)
-
static
get_roc_curve
(tensor, labels, reduce_to_n_samples=None, use_sub_process=False, results_fn=<function PytorchExperimentLogger.<lambda>>)[source]¶ Displays a roc curve given a tensor with scores and the coresponding labels
Parameters: - tensor – Tensor with scores (e.g class probability )
- labels – Labels of the samples to which the scores match
- reduce_to_n_samples – Reduce/ downsample to to n samples for fewer data points
- use_sub_process – Use a sub process to do the processing, if true nothing is returned
- results_fn – function which is called with the results/ return values. Expected f(tpr, fpr)
-
get_save_checkpoint_fn
(name='checkpoint', **kwargs)[source]¶ A function which returns a function which takes n_iter as arguments and saves the current values of the variables given as kwargs as a checkpoint file.
Parameters: - name – Base-name of the checkpoint file
- **kwargs – dict which is actually saved, when the returned function is called
Returns: Function which takes n_iter as arguments and saves a checkpoint file
-
load_checkpoint
(name, exclude_layer_dict=None, warnings=True, **kwargs)[source]¶ Loads a checkpoint from the checkpoint directory of the experiment folder
Parameters: - name – The name of the checkpoint file
- exclude_layer_dict – A dict with key ‘model_name’ and a list of all layers of ‘model_name’ which should
- be restored (not) –
- warnings – Flag which indicates if method should warn if not everything went perfectlys
- **kwargs – dict which is actually loaded (key=name (used to save the checkpoint) , value=variable to be
- overwritten) (loaded/) –
Returns: The kwargs dict with the loaded/ overwritten values
-
static
load_checkpoint_static
(checkpoint_file, exclude_layer_dict=None, warnings=True, **kwargs)[source]¶ Loads a checkpoint/dict in a given directory (using pytorch)
Parameters: - checkpoint_file – The checkpoint from which the checkpoint/dict should be loaded
- exclude_layer_dict – A dict with key ‘model_name’ and a list of all layers of ‘model_name’ which should
- be restored (not) –
- warnings – Flag which indicates if method should warn if not everything went perfectlys
- **kwargs – dict which is actually loaded (key=name (used to save the checkpoint) , value=variable to be
- overwritten) (loaded/) –
Returns: The kwargs dict with the loaded/ overwritten values
-
load_last_checkpoint
(**kwargs)[source]¶ Loads the (alphabetically) last checkpoint file in the checkpoint directory in the experiment folder
Parameters: - **kwargs – dict which is actually loaded (key=name (used to save the checkpoint) , value=variable to be
- overwritten) (loaded/) –
Returns: The kwargs dict with the loaded/ overwritten values
-
static
load_last_checkpoint_static
(dir_, name=None, **kwargs)[source]¶ Loads the (alphabetically) last checkpoint file in a given directory
Parameters: - dir – The directory to look for the (alphabetically) last checkpoint
- name – String pattern which indicates the files to look form
- **kwargs – dict which is actually loaded (key=name (used to save the checkpoint) , value=variable to be
- overwritten) (loaded/) –
Returns: The kwargs dict with the loaded/ overwritten values
-
load_model
(model, name, exclude_layers=(), warnings=True)[source]¶ Loads a pytorch model from the model directory of the experiment folder
Parameters: - model – The model to be loaded (whose parameters should be restored)
- name – The file name of the model file
- exclude_layers – List of layer names which should be excluded from restoring
- warnings – Flag which indicates if method should warn if not everything went perfectlys
-
static
load_model_static
(*args, **kwargs)¶
-
print
(*args)[source]¶ Prints the given arguments using the text logger print function
Parameters: *args – Things to be printed
-
save_at_exit
(name='checkpoint_end', **kwargs)[source]¶ Saves a dict as checkpoint if the program exits (not garanteed to work 100%)
Parameters: - name – Name of the checkpoint file
- **kwargs – dict which is actually saved (key=name, value=variable to be stored)
-
save_checkpoint
(name, n_iter=None, iter_format='{:05d}', prefix=False, **kwargs)[source]¶ Saves a checkpoint in the checkpoint directory of the experiment folder
Parameters: - name – The file name of the checkpoint file
- n_iter – The iteration number, formatted with the iter_format and added to the checkpoint name (if not None)
- iter_format – The format string, which indicates how n_iter will be formated as a string
- prefix – If True, the formated n_iter will be appended as a prefix, otherwise as a suffix
- **kwargs – dict which is actually saved (key=name, value=variable to be stored)
-
static
save_checkpoint_static
(*args, **kwargs)¶
-
save_model
(model, name, n_iter=None, iter_format='{:05d}', prefix=False)[source]¶ Saves a pytorch model in the model directory of the experiment folder
Parameters: - model – The model to be stored
- name – The file name of the model file
- n_iter – The iteration number, formatted with the iter_format and added to the model name (if not None)
- iter_format – The format string, which indicates how n_iter will be formated as a string
- prefix – If True, the formated n_iter will be appended as a prefix, otherwise as a suffix
-
static
save_model_static
(*args, **kwargs)¶
-
show_gif
(frame_list=None, name='frames', scale=1.0, fps=25)[source]¶ Saves gif in the img folder. Should be a list of arrays with dimension HxWxC.
Parameters: - frame_list – The list of image tensors/arrays to be saved as a gif
- name – Filename of the gif
- scale – Scaling factor of the individual frames
- fps – FPS of the gif
-
show_image_gradient
(name, *args, **kwargs)[source]¶ Given a model creates calculates the error and backpropagates it to the image and saves it.
Parameters: - name – Name of the file
- model – The model to be evaluated
- inpt – Input to the model
- err_fn – The error function the evaluate the output of the model on
- grad_type – Gradient calculation method, currently supports (vanilla, vanilla-smooth, guided,
- guided-smooth) (the guided backprob can lead to segfaults -.-) –
- n_runs – Number of runs for the smooth variants
- eps – noise scaling to be applied on the input image (noise is drawn from N(0,1))
- abs (bool) – Flag, if the gradient should be a absolute value
-
show_image_grid
(image, name, **kwargs)[source]¶ Saves images in the img folder as a image grid
Parameters: - images – The images to be saved
- name – file name of the new image file
-
show_image_grid_heatmap
(heatmap, background=None, name='heatmap', **kwargs)[source]¶ Saves images in the img folder as a image grid
Parameters: - heatmap – The images to be converted to a heatmap
- background – Context of the heatmap (to be underlayed)
- name – file name of the new image file
-
show_images
(images, name, **kwargs)[source]¶ Saves images in the img folder
Parameters: - images – The images to be saved
- name – file name of the new image file
-
show_video
(frame_list=None, name='video', dim='LxHxWxC', scale=1.0, fps=25, extension='.mp4', codec='THEO')[source]¶ Saves video in the img folder. Should be a list of arrays with dimension HxWxC.
Parameters: - frame_list – The list of image tensors/arrays to be saved as a video
- name – Filename of the video
- dim – Dimension of the tensor - should be either LxHxWxC or LxCxHxW
- fps – FPS of the video
- extension – File extension - should be mp4, ogc, avi or webm
file¶
NumpyPlotFileLogger¶
-
class
trixi.logger.file.numpyplotfilelogger.
NumpyPlotFileLogger
(img_dir, plot_dir, switch_backend=True, **kwargs)[source]¶ Bases:
trixi.logger.plt.numpyseabornplotlogger.NumpySeabornPlotLogger
NumpyPlotFileLogger is a logger, which can plot/ interpret numpy array as different types (images, lineplots, …) into an image and plot directory. For the plotting it builds up on the NumpySeabornPlotLogger.
-
show_barplot
(array, name, file_format='.png', *args, **kwargs)[source]¶ Method which creates and stores a barplot
Parameters: - array – Array of values you want to plot
- name – file-name
- file_format – output-image (plot) file format
-
show_boxplot
(array, name, file_format='.png', *args, **kwargs)[source]¶ Method which creates and stores a boxplot
Parameters: - array – Array of values you want to plot
- name – file-name
- file_format – output-image (plot) file format
-
show_image
(image, name, file_format='.png', *args, **kwargs)[source]¶ Method which stores an image as a image file
Parameters: - image – Numpy array-image
- name – file-name
- file_format – output-image file format
-
show_lineplot
(y_vals, x_vals, name, file_format='.png', *args, **kwargs)[source]¶ Method which creates and stores a lineplot
Parameters: - y_vals – Array of y values
- x_vals – Array of corresponding x-values
- name – file-name
- file_format – output-image (plot) file format
-
show_matplot_plt
(figure, name, file_format='.png', *args, **kwargs)[source]¶ Method to save a custom matplotlib figure
Parameters: - figure – Figure you want to plot
- name – file name
- file_format – output image (plot) file format
-
show_piechart
(array, name, file_format='.png', *args, **kwargs)[source]¶ Method which creates and stores a piechart
Parameters: - array – Array of values you want to plot
- name – file-name
- file_format – output-image (plot) file format
-
show_scatterplot
(array, name, file_format='.png', *args, **kwargs)[source]¶ Method which creates and stores a scatter
Parameters: - array – Array of values you want to plot
- name – file-name
- file_format – output-image (plot) file format
-
show_value
(value, name, counter=None, tag=None, file_format='.png', *args, **kwargs)[source]¶ Method which logs a value as a line plot
Parameters: - value – Value (y-axis value) you want to display/ plot/ store
- name – Name of the value (will also be the filename if no tag is given)
- counter – counter, which tells the number of the sample (with the same name –> filename) (x-axis value)
- tag – Tag, grouping similar values. Values with the same tag will be plotted in the same plot
- file_format – output-image file format
Returns:
-
PytorchPlotFileLogger¶
-
class
trixi.logger.file.pytorchplotfilelogger.
PytorchPlotFileLogger
(*args, **kwargs)[source]¶ Bases:
trixi.logger.file.numpyplotfilelogger.NumpyPlotFileLogger
Visual logger, inherits the NumpyPlotLogger and plots/ logs pytorch tensors and variables as files on the local file system.
-
process_params
(f, *args, **kwargs)[source]¶ Inherited “decorator”: convert Pytorch variables and Tensors to numpy arrays
-
save_image
(tensor, name, n_iter=None, iter_format='{:05d}', prefix=False, image_args=None)[source]¶ Saves an image into the image directory of the PytorchPlotFileLogger
Parameters: - tensor – Tensor containing the image
- name – file-name of the image file
- n_iter – The iteration number, formatted with the iter_format and added to the model name (if not None)
- iter_format – The format string, which indicates how n_iter will be formated as a string
- prefix – If True, the formated n_iter will be appended as a prefix, otherwise as a suffix
- image_args – Arguments for the tensorvision save image method
-
save_image_grid
(tensor, name, n_iter=None, prefix=False, iter_format='{:05d}', image_args=None)[source]¶ Saves images of a 4d- tensor (N, C, H, W) as a image grid into an image file in the image directory of the PytorchPlotFileLogger
Parameters: - tensor – 4d- tensor (N, C, H, W)
- name – file-name of the image file
- n_iter – The iteration number, formatted with the iter_format and added to the model name (if not None)
- iter_format – The format string, which indicates how n_iter will be formated as a string
- prefix – If True, the formated n_iter will be appended as a prefix, otherwise as a suffix
- image_args – Arguments for the tensorvision save image method
-
static
save_image_grid_static
(*args, **kwargs)¶
-
static
save_image_static
(*args, **kwargs)¶
-
save_images
(tensors, n_iter=None, iter_format='{:05d}', prefix=False, image_args=None)[source]¶ Saves an image tensors into the image directory of the PytorchPlotFileLogger
Parameters: - tensors – A dict with file-name-> tensor to plot as image
- n_iter – The iteration number, formatted with the iter_format and added to the model name (if not None)
- iter_format – The format string, which indicates how n_iter will be formated as a string
- prefix – If True, the formated n_iter will be appended as a prefix, otherwise as a suffix
- image_args – Arguments for the tensorvision save image method
-
static
save_images_static
(*args, **kwargs)¶
-
show_image
(image, name, n_iter=None, iter_format='{:05d}', prefix=False, image_args=None, **kwargs)[source]¶ Calls the save image method (for abstract logger combatibility)
Parameters: - image – Tensor containing the image
- name – file-name of the image file
- n_iter – The iteration number, formatted with the iter_format and added to the model name (if not None)
- iter_format – The format string, which indicates how n_iter will be formated as a string
- prefix – If True, the formated n_iter will be appended as a prefix, otherwise as a suffix
- image_args – Arguments for the tensorvision save image method
-
show_image_grid
(images, name, n_iter=None, prefix=False, iter_format='{:05d}', image_args=None, **kwargs)[source]¶ Calls the save image grid method (for abstract logger combatibility)
Parameters: - images – 4d- tensor (N, C, H, W)
- name – file-name of the image file
- n_iter – The iteration number, formatted with the iter_format and added to the model name (if not None)
- iter_format – The format string, which indicates how n_iter will be formated as a string
- prefix – If True, the formated n_iter will be appended as a prefix, otherwise as a suffix
- image_args – Arguments for the tensorvision save image method
-
show_image_grid_heatmap
(heatmap, background=None, ratio=0.3, normalize=True, colormap=<sphinx.ext.autodoc.importer._MockObject object>, name='heatmap', n_iter=None, prefix=False, iter_format='{:05d}', image_args=None, **kwargs)[source]¶ Creates heat map from the given map and if given combines it with the background and then displays results with as image grid.
Parameters: - heatmap – 4d- tensor (N, C, H, W) to be converted to a heatmap
- background – 4d- tensor (N, C, H, W) background/ context of the heatmap (to be underlayed)
- name – The name of the window
- ratio – The ratio to mix the map with the background (0 = only background, 1 = only map)
- n_iter – The iteration number, formatted with the iter_format and added to the model name (if not None)
- iter_format – The format string, which indicates how n_iter will be formated as a string
- prefix – If True, the formated n_iter will be appended as a prefix, otherwise as a suffix
- image_args – Arguments for the tensorvision save image method
-
show_images
(images, name, n_iter=None, iter_format='{:05d}', prefix=False, image_args=None, **kwargs)[source]¶ Calls the save images method (for abstract logger combatibility)
Parameters: - images – List of Tensors
- name – List of file names (corresponding to the images list)
- n_iter – The iteration number, formatted with the iter_format and added to the model name (if not None)
- iter_format – The format string, which indicates how n_iter will be formated as a string
- prefix – If True, the formated n_iter will be appended as a prefix, otherwise as a suffix
- image_args – Arguments for the tensorvision save image method
-
TextFileLogger¶
-
class
trixi.logger.file.textfilelogger.
TextFileLogger
(base_dir=None, logging_level=10, logging_stream=<_io.TextIOWrapper name='<stdout>' mode='w' encoding='UTF-8'>, default_stream_handler=True, **kwargs)[source]¶ Bases:
trixi.logger.abstractlogger.AbstractLogger
A Logger for logging text into different text files and output streams (using the python logging framework)
-
add_file_handler
(name, logger='default')[source]¶ Adds a file handler to a logger, thus the logger will log into a log file with a given name
Parameters: - name – File name of the log file (in which the logger will log now)
- logger – Name of the logger to add the file-hander/ logging file to
-
add_handler
(handler, logger='default')[source]¶ Adds an additional handler to a logger with a name :param handler: Logging handler to be added to a given logger :param logger: Name of the logger to add the hander to
-
add_logger
(name, logging_level=None, file_handler=True, stream_handler=True)[source]¶ Adds a new logger
Parameters: - name – Name of the new logger
- logging_level – Logging level of the new logger
- file_handler – Flag, if it should use a file_handler, if yes creates a new file with the given name in the logging directory
- stream_handler – Flag, if the logger should also log to the default stream
Returns:
-
add_stream_handler
(logger='default')[source]¶ Adds a stream handler to a logger, thus the logger will log into the default logging stream
Parameters: logger – Name of the logger to add the stream-hander to
-
debug
(msg, logger='default')[source]¶ Prints and logs a message with the level debug
Parameters: - msg – Message to print/ log
- logger – Logger which should log
-
error
(msg, logger='default')[source]¶ Prints and logs a message with the level error
Parameters: - msg – Message to print/ log
- logger – Logger which should log
-
info
(msg, logger='default')[source]¶ Prints and logs a message with the level info
Parameters: - msg – Message to print/ log
- logger – Logger which should log
-
log
(msg, logger='default')[source]¶ Prints and logs a message with the level info
Parameters: - msg – Message to print/ log
- logger – Logger which should log
-
log_to
(msg, name, log_to_default=False)[source]¶ Logs to an existing logger or if logger does not exists creates new one
Parameters: - msg – Message to be logged
- name – Name of the logger to log to (usually also the logfile-name)
- log_to_default – Flag if it should in addition to the logger given by name, log to the default logger
-
print
(*args, logger='default')[source]¶ Prints and logs objects
Parameters: - *args – Object to print/ log
- logger – Logger which should log
-
message¶
SlackMessageLogger¶
-
class
trixi.logger.message.slackmessagelogger.
SlackMessageLogger
(token, user_email, exp_name=None, *args, **kwargs)[source]¶ Bases:
trixi.logger.plt.numpyseabornimageplotlogger.NumpySeabornImagePlotLogger
Slack logger, inherits the NumpySeabornImagePlotLogger and sends plots/logs to a chat via a Slack bot.
-
delete_message
(ts)[source]¶ Deletes a direct message from the bot with the given timestamp (ts)
Parameters: ts – Time stamp the message was sent
-
static
find_cid_for_user
(slack_client, uid)[source]¶ Returns the chat/channel id for a direct message of the bot with the given user
Parameters: - slack_client – Slack client (already authorized)
- uid – User id of the user
Returns: chat/channel id for a direct message
-
static
find_uid_for_email
(slack_client, email)[source]¶ Returns the slack user id for a given email
Parameters: - slack_client – Slack client (already authorized)
- email – Workspace email address to get the user id for
Returns: Slack workspace user id
-
print
(text, *args, **kwargs)[source]¶ Just calls
show_text()
-
process_params
(f, *args, **kwargs)[source]¶ Inherited “decorator”: convert PyTorch variables and Tensors to numpy arrays
-
send_message
(message='', file=None)[source]¶ Sends a message and a file if one is given
Parameters: - message – Message to be sent
- file – File to be sent
Returns: The timestamp (ts) of the message
-
show_barplot
(array, name='barplot', delete_last=True, *args, **kwargs)[source]¶ Sends a barplot to a chat using an existing slack bot.
Parameters: - array – array of shape NxM where N is the number of rows and M is the number of elements in the row.
- name – The name of the figure
- delete_last – If a message with the same name was sent, delete it beforehand
-
show_image
(image, *args, **kwargs)[source]¶ Sends an image file to a chat using an existing slack bot.
Parameters: image (str or np array) – Path to the image file to be sent to the chat.
-
show_image_grid
(image_array, name=None, nrow=8, padding=2, normalize=False, range=None, scale_each=False, pad_value=0, delete_last=True, *args, **kwargs)[source]¶ Sends an array of images to a chat using an existing Slack bot. (Requires torch and torchvision)
Parameters: - image_array (np.narray / torch.tensor) – Image array/tensor which will be sent as an image grid
- make_grid_kargs – Key word arguments for the torchvision make grid method
- delete_last – If a message with the same name was sent, delete it beforehand
-
show_lineplot
(y_vals, x_vals=None, name='lineplot', delete_last=True, *args, **kwargs)[source]¶ Sends a lineplot to a chat using an existing slack bot.
Parameters: - y_vals – Array of shape MxN , where M is the number of points and N is the number of different line
- x_vals – Has to have the same shape as Y: MxN. For each point in Y it gives the corresponding X value (if not set the points are assumed to be equally distributed in the interval [0, 1])
- name – The name of the figure
- delete_last – If a message with the same name was sent, delete it beforehand
-
show_piechart
(array, name='piechart', delete_last=True, *args, **kwargs)[source]¶ Sends a piechart to a chat using an existing slack bot.
Parameters: - array – Array of positive integers. Each integer will be presented as a part of the pie (with the total as the sum of all integers)
- name – The name of the figure
- delete_last – If a message with the same name was sent, delete it beforehand
-
show_scatterplot
(array, name='scatterplot', delete_last=True, *args, **kwargs)[source]¶ Sends a scatterplot to a chat using an existing slack bot.
Parameters: - array – An array with size N x dim, where each element i in N at X[i] results in a 2D (if dim = 2) or 3D (if dim = 3) point.
- name – The name of the figure
- delete_last – If a message with the same name was sent, delete it beforehand
-
show_text
(text, *args, **kwargs)[source]¶ Sends a text to a chat using an existing slack bot.
Parameters: text (str) – Text message to be sent to the bot.
-
show_value
(value, name, counter=None, tag=None, delete_last=True, *args, **kwargs)[source]¶ Sends a value to a chat using an existing slack bot.
Parameters: - value – Value to be plotted sent to the chat.
- name – Name for the plot.
- counter – Optional counter to be sent in conjunction with the value.
- tag – Tag to be used as a label for the plot.
- delete_last – If a message with the same name was sent, delete it beforehand
-
TelegramMessageLogger¶
-
class
trixi.logger.message.telegrammessagelogger.
TelegramMessageLogger
(token, chat_id, exp_name=None, *args, **kwargs)[source]¶ Bases:
trixi.logger.plt.numpyseabornimageplotlogger.NumpySeabornImagePlotLogger
Telegram logger, inherits the NumpySeabornImagePlotLogger and sends plots/logs to a chat via a Telegram bot.
-
process_params
(f, *args, **kwargs)[source]¶ Inherited “decorator”: convert PyTorch variables and Tensors to numpy arrays
-
show_barplot
(array, name=None, *args, **kwargs)[source]¶ Sends a barplot to a chat using an existing Telegram bot.
Parameters: - array – array of shape NxM where N is the number of rows and M is the number of elements in the row.
- name – The name of the figure
-
show_image
(image, *args, **kwargs)[source]¶ Sends an image file to a chat using an existing Telegram bot.
Parameters: image (str or np array) – Path to the image file to be sent to the chat.
-
show_image_grid
(image_array, name=None, nrow=8, padding=2, normalize=False, range=None, scale_each=False, pad_value=0, *args, **kwargs)[source]¶ Sends an array of images to a chat using an existing Telegram bot. (Requires torch and torchvision)
Parameters: - image_array (np.narray / torch.tensor) – Image array/ tensor which will be sent as an image grid
- make_grid_kargs – Key word arguments for the torchvision make grid method
-
show_lineplot
(y_vals, x_vals=None, name=None, *args, **kwargs)[source]¶ Sends a lineplot to a chat using an existing Telegram bot.
Parameters: - y_vals – Array of shape MxN , where M is the number of points and N is the number of different line
- x_vals – Has to have the same shape as Y: MxN. For each point in Y it gives the corresponding X value (if
- set the points are assumed to be equally distributed in the interval [0, 1] ) (not) –
- name – The name of the figure
-
show_piechart
(array, name=None, *args, **kwargs)[source]¶ Sends a piechart to a chat using an existing Telegram bot.
Parameters: - array – Array of positive integers. Each integer will be presented as a part of the pie (with the total
- the sum of all integers) (as) –
- name – The name of the figure
-
show_scatterplot
(array, name=None, *args, **kwargs)[source]¶ Sends a scatterplot to a chat using an existing Telegram bot.
Parameters: - array – A 2d array with size N x dim, where each element i in N at X[i] results in a a 2d (if dim = 2)/
- 3d (if dim = 3) –
- name – The name of the figure
-
show_text
(text, *args, **kwargs)[source]¶ Sends a text to a chat using an existing Telegram bot.
Parameters: text (str) – Text message to be sent to the bot.
-
show_value
(value, name, counter=None, tag=None, *args, **kwargs)[source]¶ Sends a value to a chat using an existing Telegram bot.
Parameters: - value – Value to be plotted sent to the chat.
- name – Name for the plot.
- counter – Optional counter to be sent in conjunction with the value.
- tag – Tag to be used as a label for the plot.
-
plt¶
NumpySeabornPlotLogger¶
-
class
trixi.logger.plt.numpyseabornplotlogger.
NumpySeabornPlotLogger
(**kwargs)[source]¶ Bases:
trixi.logger.abstractlogger.AbstractLogger
Visual logger, inherits the AbstractLogger and plots/ logs numpy arrays/ values as matplotlib / seaborn plots.
-
get_figure
(name)[source]¶ Returns a figure with a given name as identifier.
If no figure yet exists with the name a new one is created. Otherwise the existing one is returned
Parameters: name – Name of the figure Returns: A figure with the given name
-
show_barplot
(array, name=None, show=True, *args, **kwargs)[source]¶ Creates a bar plot figure from an array
Parameters: - array – array of shape NxM where N is the number of rows and M is the number of elements in the row.
- name – The name of the figure
- show – Flag if it should also display the figure (result might also depend on the matplotlib backend )
Returns: A matplotlib figure
-
show_boxplot
(array, name, show=True, *args, **kwargs)[source]¶ Creates a box plot figure from an array
Parameters: - array – array of shape NxM where N is the number of rows and M is the number of elements in the row.
- name – The name of the figure
- show – Flag if it should also display the figure (result might also depend on the matplotlib backend )
Returns: A matplotlib figure
-
show_image
(image, name=None, show=True, *args, **kwargs)[source]¶ Create an image figure
Parameters: - image – The image array to be displayed
- name – The name of the image window
- show – Flag if it should also display the figure (result might also depend on the matplotlib backend )
Returns: A matplotlib figure
-
show_lineplot
(y_vals, x_vals=None, name=None, show=True, *args, **kwargs)[source]¶ Creates a line plot figure with (multiple) lines plot, given values Y (and optional the corresponding X values)
Parameters: - y_vals – Array of shape MxN , where M is the number of points and N is the number of different line
- x_vals – Has to have the same shape as Y: MxN. For each point in Y it gives the corresponding X value (if
- set the points are assumed to be equally distributed in the interval [0, 1] ) (not) –
- name – The name of the figure
- show – Flag if it should also display the figure (result might also depend on the matplotlib backend )
Returns: A matplotlib figure
-
show_piechart
(array, name=None, show=True, *args, **kwargs)[source]¶ Creates a scatter plot figure
Parameters: - array – Array of positive integers. Each integer will be presented as a part of the pie (with the total
- the sum of all integers) (as) –
- name – The name of the figure
- show – Flag if it should also display the figure (result might also depend on the matplotlib backend )
Returns: A matplotlib figure
-
show_scatterplot
(array, name=None, show=True, *args, **kwargs)[source]¶ Creates a scatter plot figure with the points given in array
Parameters: - array – A 2d array with size N x dim, where each element i in N at X[i] results in a a 2d (if dim = 2)/
- 3d (if dim = 3) –
- name – The name of the figure
- show – Flag if it should also display the figure (result might also depend on the matplotlib backend )
Returns: A matplotlib figure
-
show_value
(value, name, counter=None, tag=None, show=True, *args, **kwargs)[source]¶ Creates a line plot that is automatically appended with new values and returns it as a figure.
Parameters: - value – Value to be plotted / appended to the graph (y-axis value)
- name – The name of the window
- counter – counter, which tells the number of the sample (with the same name) (x-axis value)
- tag – Tag, grouping similar values. Values with the same tag will be plotted in the same plot
- show –
Flag if it should also display the figure (result might also depend on the matplotlib backend )
- Returns:
- A matplotlib figure
-
NumpySeabornImagePlotLogger¶
-
class
trixi.logger.plt.numpyseabornimageplotlogger.
NumpySeabornImagePlotLogger
(**kwargs)[source]¶ Bases:
trixi.logger.plt.numpyseabornplotlogger.NumpySeabornPlotLogger
Wrapper around
NumpySeabornPlotLogger
that renders figures into numpy arrays.-
show_barplot
(array, name=None, *args, **kwargs)[source]¶ Creates a bar plot figure from an array
Parameters: - array – array of shape NxM where N is the number of rows and M is the number of elements in the row.
- name – The name of the figure
Returns: A numpy array image of the figure
-
show_image
(image, name=None, *args, **kwargs)[source]¶ Create an image figure
Parameters: - image – The image array to be displayed
- name – The name of the image window
Returns: A numpy array image of the figure
-
show_lineplot
(y_vals, x_vals=None, name=None, *args, **kwargs)[source]¶ Creates a line plot figure with (multiple) lines plot, given values Y (and optional the corresponding X values)
Parameters: - y_vals – Array of shape MxN , where M is the number of points and N is the number of different line
- x_vals – Has to have the same shape as Y: MxN. For each point in Y it gives the corresponding X value (if
- set the points are assumed to be equally distributed in the interval [0, 1] ) (not) –
- name – The name of the figure
Returns: A numpy array image of the figure
-
show_piechart
(array, name=None, *args, **kwargs)[source]¶ Creates a scatter plot figure
Parameters: - array – Array of positive integers. Each integer will be presented as a part of the pie (with the total
- the sum of all integers) (as) –
- name – The name of the figure
Returns: A numpy array image of the figure
-
show_scatterplot
(array, name=None, *args, **kwargs)[source]¶ Creates a scatter plot figure with the points given in array
Parameters: - array – A 2d array with size N x dim, where each element i in N at X[i] results in a a 2d (if dim = 2)/
- 3d (if dim = 3) –
- name –
The name of the figure
- Returns:
- A numpy array image of the figure
-
show_value
(value, name, counter=None, tag=None, *args, **kwargs)[source]¶ Creates a line plot that is automatically appended with new values and returns it as a figure.
Parameters: - value – Value to be plotted / appended to the graph (y-axis value)
- name – The name of the window
- counter – counter, which tells the number of the sample (with the same name) (x-axis value)
- tag –
Tag, grouping similar values. Values with the same tag will be plotted in the same plot
- Returns:
- A numpy array image of the figure
-
visdom¶
NumpyVisdomLogger¶
-
class
trixi.logger.visdom.numpyvisdomlogger.
NumpyVisdomLogger
(exp_name='main', server='http://localhost', port=8080, auto_close=True, auto_start=False, auto_start_ports=(8080, 8000), **kwargs)[source]¶ Bases:
trixi.logger.abstractlogger.AbstractLogger
Visual logger, inherits the AbstractLogger and plots/ logs numpy arrays/ values on a Visdom server.
-
show_values
(val_dict)[source]¶ A util function for multiple values. Simple plots all values in a dict, there the window name is the key in the dict and the plotted value is the value of a dict (Simply calls the show_value function).
Parameters: val_dict – Dict with key, values pairs which will be plotted
-
PytorchVisdomLogger¶
-
class
trixi.logger.visdom.pytorchvisdomlogger.
PytorchVisdomLogger
(*args, **kwargs)[source]¶ Bases:
trixi.logger.visdom.numpyvisdomlogger.NumpyVisdomLogger
Visual logger, inherits the NumpyVisdomLogger and plots/ logs pytorch tensors and variables on a Visdom server.
-
plot_model_gradient_flow
(model, name='model', title=None)[source]¶ Plots statstics (mean, std, abs(max)) of the weights or the corresponding gradients of a model as a barplot.
Parameters: - model – Model with the weights.
- env_appendix – Visdom environment name appendix, if none is given, it uses “-histogram”.
- model_name – Name of the model (is used as window name).
- plot_grad – If false plots weight statistics, if true plot the gradients of the weights.
-
plot_model_statistics_grads
(model, env_appendix='', model_name='', **kwargs)[source]¶ Plots statstics (mean, std, abs(max)) of the gradients of a model as a barplot (uses plot model statistics with plot_grad=True).
Parameters: - model – Model with the weights and the corresponding gradients (have to calculated previously).
- env_appendix – Visdom environment name appendix
- model_name – Name of the model (is used as window name).
-
plot_model_statistics_weights
(model, env_appendix='', model_name='', **kwargs)[source]¶ Plots statstics (mean, std, abs(max)) of the weights of a model as a barplot (uses plot model statistics with plot_grad=False).
Parameters: - model – Model with the weights.
- env_appendix – Visdom environment name appendix
- model_name – Name of the model (is used as window name).
-
plot_model_structure
(model, input_size, name='model_structure', use_cuda=True, delete_tmp_on_close=False, forward_kwargs=None, **kwargs)[source]¶ Plots the model structure/ model graph of a pytorch module (this only works correctly with pytorch 0.2.0).
Parameters: - model – The graph of this model will be plotted.
- input_size – Input size of the model (with batch dim).
- name – The name of the window in the visdom env.
- use_cuda – Perform model dimension calculations on the gpu (cuda).
- delete_tmp_on_close – Determines if the tmp file will be deleted on close. If set true, can cause problems due to the multi threadded plotting.
-
plot_mutliple_models_statistics_grads
(model_dict, env_appendix='', **kwargs)[source]¶ Given models in a dict, plots the gradient statistics of the models.
Parameters: - model_dict – Dict with models, the key is assumed to be the name, while the value is the model.
- env_appendix – Visdom environment name appendix
-
plot_mutliple_models_statistics_weights
(model_dict, env_appendix=None, **kwargs)[source]¶ Given models in a dict, plots the weight statistics of the models.
Parameters: - model_dict – Dict with models, the key is assumed to be the name, while the value is the model.
- env_appendix – Visdom environment name appendix
-
process_params
(f, *args, **kwargs)[source]¶ Inherited “decorator”: convert Pytorch variables and Tensors to numpy arrays.
-
show_classification_metrics
(tensor, labels, name, metric=('roc-auc', 'pr-score'), use_sub_process=False, tag_name=None)[source]¶ Displays some classification metrics as line plots in a graph (similar to show value (also uses show value for the caluclated values))
Parameters: - tensor – Tensor with scores (e.g class probability )
- labels – Labels of the samples to which the scores match
- name – The name of the window
- metric – List of metrics to calculate. Options are: roc-auc, pr-auc, pr-score, mcc, f1
Returns:
-
show_embedding
(tensor, labels=None, name=None, method='tsne', n_dims=2, n_neigh=30, meth_args=None, *args, **kwargs)[source]¶ Displays a tensor a an embedding
Parameters: - tensor – Tensor to be embedded an then displayed
- labels – Labels of the entries in the tensor (first dimension)
- name – The name of the window
- method – Method used for embedding, options are: tsne, standard, ltsa, hessian, modified, isomap, mds,
- umap (spectral,) –
- n_dims – dimensions to embed the data into
- n_neigh – Neighbour parameter to kind of determin the embedding (see t-SNE for more information)
- meth_args – Further arguments which can be passed to the embedding method
-
show_image_gradient
(model, inpt, err_fn, grad_type='vanilla', n_runs=20, eps=0.1, abs=False, **image_grid_params)[source]¶ Given a model creates calculates the error and backpropagates it to the image and saves it (saliency map).
Parameters: - model – The model to be evaluated
- inpt – Input to the model
- err_fn – The error function the evaluate the output of the model on
- grad_type – Gradient calculation method, currently supports (vanilla, vanilla-smooth, guided,
- guided-smooth) (the guided backprob can lead to segfaults -.-) –
- n_runs – Number of runs for the smooth variants
- eps – noise scaling to be applied on the input image (noise is drawn from N(0,1))
- abs (bool) – Flag, if the gradient should be a absolute value
- **image_grid_params – Params for make image grid.
-
show_image_grid_heatmap
(*args, **kwargs)¶
-
show_pr_curve
(tensor, labels, name, reduce_to_n_samples=None, use_sub_process=False)[source]¶ Displays a precision recall curve given a tensor with scores and the coresponding labels
Parameters: - tensor – Tensor with scores (e.g class probability )
- labels – Labels of the samples to which the scores match
- name – The name of the window
- reduce_to_n_samples – Reduce/ downsample to to n samples for fewer data points
- use_sub_process – Use a sub process to do the processing
-
show_roc_curve
(tensor, labels, name, reduce_to_n_samples=None, use_sub_process=False)[source]¶ Displays a roc curve given a tensor with scores and the coresponding labels
Parameters: - tensor – Tensor with scores (e.g class probability )
- labels – Labels of the samples to which the scores match
- name – The name of the window
- reduce_to_n_samples – Reduce/ downsample to to n samples for fewer data points
- use_sub_process – Use a sub process to do the processing
-
AbstractLogger¶
-
class
trixi.logger.abstractlogger.
AbstractLogger
(*args, **kwargs)[source]¶ Bases:
object
Abstract interface for visual logger.
-
process_params
(f, *args, **kwargs)[source]¶ Implement this to handle data conversions in your logger.
Example: Implement logger for numpy data, then implement torch logger as child of numpy logger and just use the process_params method to convert from torch to numpy.
-
show_barplot
(*args, **kwargs)[source]¶ Abstract method which should handle and somehow log/ store a barplot
-
show_image
(*args, **kwargs)[source]¶ Abstract method which should handle and somehow log/ store an image
-
show_lineplot
(*args, **kwargs)[source]¶ Abstract method which should handle and somehow log/ store a lineplot
-
show_piechart
(*args, **kwargs)[source]¶ Abstract method which should handle and somehow log/ store a piechart
-
show_scatterplot
(*args, **kwargs)[source]¶ Abstract method which should handle and somehow log/ store a scatterplot
-