class documentation

data logger for class CMAEvolutionStrategy.

The logger is identified by its name prefix and (over-)writes or reads according data files. Therefore, the logger must be considered as global variable with unpredictable side effects, if two loggers with the same name and on the same working folder are used at the same time.

Examples

import cma

es = cma.CMAEvolutionStrategy(12 * [3], 4)
es.optimize(cma.ff.elli, callback=es.logger.plot)

plots into the current matplotlib figure (or opens one if none exists) via a generic callback. es.optimize already adds by default es.logger.add to its callback list to add data to the logger. This call:

x, es = cma.fmin2(cma.ff.elli, 12 * [3], 4, {'verb_plot': 1})

is very similar, but plots hard-coded into figure number 324:

es = cma.CMAEvolutionStrategy(...)
logger = cma.CMADataLogger().register(es)
while not es.stop():
    ...
    logger.add()  # add can also take an argument

logger.plot() # or a short cut can be used:
cma.plot()  # plot data from logger with default name into new figure

logger2 = cma.CMADataLogger('just_another_filename_prefix').load()
logger2.plot()
logger2.disp()

import cma
from matplotlib.pylab import *
res = cma.fmin(cma.ff.sphere, rand(10), 1e-0)
logger = res[-1]  # the CMADataLogger
logger.load()  # by "default" data are on disk
semilogy(logger.f[:,0], logger.f[:,5])  # plot f versus iteration, see file header
cma.s.figshow()

Details

After loading data, the logger has the attributes xmean, xrecent, std, f, D and corrspec corresponding to xmean, xrecentbest, stddev, fit, axlen and axlencorr filename trails.

See Also
disp (), plot ()
Method __init__ initialize logging of data from a CMAEvolutionStrategy instance.
Method add append some logging data from CMAEvolutionStrategy class instance es, if number_of_times_called % modulo equals to zero, never if modulo==0.
Method disp displays selected data from (files written by) the class CMADataLogger.
Method disp_header Undocumented
Method downsampling rude downsampling of a CMADataLogger data file by factor, keeping also the first first entries. This function is a stump and subject to future changes. Return self.
Method figclose Undocumented
Method initialize reset logger, overwrite original files, modulo: log only every modulo call
Method load load (or reload) data from output files, load is called in plot and disp.
Method plot plot data from a CMADataLogger (using the files written by the logger).
Method plot_all plot data from a CMADataLogger (using the files written by the logger).
Method plot_axes_scaling Undocumented
Method plot_correlations spectrum of correlation or precision matrix and percentiles of off-diagonal entries
Method plot_divers plot fitness, sigma, axis ratio...
Method plot_mean Undocumented
Method plot_sigvec plot (outer) scaling from diagonal decoding.
Method plot_stds iabscissa=1 means vs function evaluations, idx picks variables to plot
Method plot_xrecent Undocumented
Method register register a CMAEvolutionStrategy instance for logging, append=True appends to previous data logged under the same name, by default previous data are overwritten.
Method save data are saved to disk the moment they are added
Method save_to saves logger data to a different set of files, for switch=True also the loggers name prefix is switched to the new value
Method select_data keep only data of iteration_indices
Method zip write logger data to file filename [+ ...] + '.tar.gz'.
Class Variable default_prefix Undocumented
Instance Variable append append to previous data
Instance Variable counter number of calls to add
Instance Variable es Undocumented
Instance Variable expensive_modulo log also values that need an eigendecomposition to be generated every expensive iteration
Instance Variable fighandle Undocumented
Instance Variable file_names used in load, however hard-coded in add, because data must agree with name
Instance Variable key_names used in load, however hard-coded in plot
Instance Variable last_correlation_spectrum Undocumented
Instance Variable last_iteration Undocumented
Instance Variable last_precision_matrix Undocumented
Instance Variable last_skipped_iteration Undocumented
Instance Variable modulo how often to record data, allows calling add without args
Instance Variable name_prefix Undocumented
Instance Variable original_fontsize Undocumented
Instance Variable persistent_communication_dict Undocumented
Instance Variable registered Undocumented
Instance Variable relative_allowed_time_for_plotting Undocumented
Instance Variable skip_finalize_plotting Undocumented
Instance Variable timer_all Undocumented
Instance Variable timer_plot Undocumented
Instance Variable x Undocumented
Property data return dictionary with data.
Method _enter_plotting assumes that a figure is open
Method _finalize_plotting Undocumented
Method _plot_x see def plot(...
Method _unzip to unzip use "tar -xf filename" in a system shell
Method _xlabel Undocumented
Instance Variable _eigen_counter Undocumented
Instance Variable _key_names_with_annotation used in load to add one data row to be modified in plot

Inherited from BaseDataLogger:

Instance Variable filename file to save to or load from unless specified otherwise
Instance Variable optim object instance to be logging data from
Instance Variable _data dict of logged data
def __init__(self, name_prefix=default_prefix, modulo=1, append=False, expensive_modulo=1):

initialize logging of data from a CMAEvolutionStrategy instance.

Default modulo=1 means logging with each call of add. If append is True data is appended to already existing data of a logger with the same name. Additional eigendecompositions are allowed only every expensive_modulo-th logged iteration.

def add(self, es=None, more_data=(), modulo=None):

append some logging data from CMAEvolutionStrategy class instance es, if number_of_times_called % modulo equals to zero, never if modulo==0.

more_data is a list of additional data to be recorded where each data entry must have the same length.

When used for a different optimizer class, this function can be (easily?) adapted by changing the assignments under INTERFACE in the implemention.

def disp(self, idx=100):

displays selected data from (files written by) the class CMADataLogger.

Arguments

idx
indices corresponding to rows in the data file; if idx is a scalar (int), the first two, then every idx-th, and the last three rows are displayed. Too large index values are removed. If idx=='header', the header line is printed.

Example

>>> import cma, numpy as np
>>> res = cma.fmin(cma.ff.elli, 7 * [0.1], 1, {'verb_disp':1e9})  # generate data
...  #doctest: +ELLIPSIS
(4...
>>> assert res[1] < 1e-9
>>> assert res[2] < 4400
>>> l = cma.CMADataLogger()  # == res[-1], logger with default name, "points to" above data
>>> l.disp([0,-1])  # first and last
...  #doctest: +ELLIPSIS
Iterat Nfevals  function value    axis ratio maxstd  minstd...
>>> l.disp(20)  # some first/last and every 20-th line
...  #doctest: +ELLIPSIS
Iterat Nfevals  function value    axis ratio maxstd  minstd...
>>> l.disp(np.r_[0:999999:100, -1]) # every 100-th and last
...  #doctest: +ELLIPSIS
Iterat Nfevals  function value    axis ratio maxstd  minstd...
>>> l.disp(np.r_[0, -10:0]) # first and ten last
...  #doctest: +ELLIPSIS
Iterat Nfevals  function value    axis ratio maxstd  minstd...
>>> cma.disp(l.name_prefix, np.r_[0:9999999:100, -10:])  # the same as l.disp(...)
...  #doctest: +ELLIPSIS
Iterat Nfevals  function value    axis ratio maxstd  minstd...

Details

The data line with the best f-value is displayed as last line.

Use CMADataLogger.disp if the logger does not have the default name.

See Also
CMADataLogger.disp, CMADataLogger.disp
def disp_header(self):

Undocumented

def downsampling(self, factor=10, first=3, switch=True, verbose=False):

rude downsampling of a CMADataLogger data file by factor, keeping also the first first entries. This function is a stump and subject to future changes. Return self.

Arguments

  • factor -- downsampling factor
  • first -- keep first first entries
  • switch -- switch the new logger to the downsampled logger
    original_name+'down'

Details

self.name_prefix+'down' files are written

Example

import cma
cma.downsampling()  # takes outcma/cma* files
cma.plot('outcma/cmadown')
def figclose(self):

Undocumented

def initialize(self, modulo=None):

reset logger, overwrite original files, modulo: log only every modulo call

def load(self, filenameprefix=None):

load (or reload) data from output files, load is called in plot and disp.

Argument filenameprefix is the filename prefix of data to be loaded (5-8 files), by default 'outcmaes/' (where / stands for the OS-specific path seperator).

Return self with (added) attributes xrecent, xmean, f, D, std, corrspec, sigvec.

def plot(self, fig=None, iabscissa=0, iteridx=None, plot_mean=False, foffset=1e-19, x_opt=None, fontsize=7, downsample_to=10000000.0, xsemilog=False, xnormalize=False, fshift=0, addcols=None, load=True, message=None):

plot data from a CMADataLogger (using the files written by the logger).

Arguments

fig
figure number, by default starting from 325
iabscissa
0==plot versus iteration count, 1==plot versus function evaluation number
iteridx
iteration indices to plot, e.g. range(100) for the first 100 evaluations.
x_opt
If isscalar(x_opt) it is interpreted as iteration number and the difference of x to the respective iteration is plotted. If it is a negative scalar the respective index rather than the iteration is used. Namely in particular, x_opt=0 subtracts the initial solution X0 and x_opt=-1 subtracts the final solution of the data. If len(x_opt) == dimension, the difference to x_opt is plotted, otherwise the first row of x_opt are the indices of the variables to be plotted and the second row, if present, is used to take the difference.
fshift
is added to all displayed fitness values

Return CMADataLogger itself.

Examples

import cma
logger = cma.CMADataLogger()  # with default name
# try to plot the "default logging" data (e.g.
#   from previous fmin calls, which is essentially what
#   also cma.plot() does)
logger.plot()
cma.s.figsave('fig325.png')  # save current figure
logger.figclose()

Dependencies: matlabplotlib.pyplot

def plot_all(self, fig=None, iabscissa=0, iteridx=None, foffset=1e-19, x_opt=None, fontsize=7):

plot data from a CMADataLogger (using the files written by the logger).

Superseded by plot?

Arguments

fig
figure number, by default 425
iabscissa
0==plot versus iteration count, 1==plot versus function evaluation number
iteridx
iteration indices to plot
x_opt
If isscalar(x_opt) it is interpreted as iteration number and the difference of x to the respective iteration is plotted. If it is a negative scalar the respective index rather than the iteration is used. Namely in particular, x_opt=0 subtracts the initial solution X0 and x_opt=-1 subtracts the final solution of the data. If len(x_opt) == dimension, the difference to x_opt is plotted, otherwise the first row of x_opt are the indices of the variables to be plotted and the second row, if present, is used to take the difference.

Return CMADataLogger itself.

Examples

import cma
logger = cma.CMADataLogger()  # with default name
# try to plot the "default logging" data (e.g.
#   from previous fmin calls, which is essentially what
#   also cma.plot() does)
logger.plot_all()
cma.s.figsave('fig425.png')  # save current figure
logger.s.figclose()

Dependencies: matlabplotlib/pyplot.

def plot_axes_scaling(self, iabscissa=0):

Undocumented

def plot_correlations(self, iabscissa=0, name='corrspec'):

spectrum of correlation or precision matrix and percentiles of off-diagonal entries

def plot_divers(self, iabscissa=0, foffset=1e-19, fshift=0, message=None):

plot fitness, sigma, axis ratio...

Parameters
iabscissa0 means vs evaluations, 1 means vs iterations
foffsetadded to f-value after abs(f) is taken
fshiftUndocumented
messageUndocumented
See Also
plot
def plot_mean(self, iabscissa=0, x_opt=None, annotations=None, xsemilog=None, xnormalize=None):

Undocumented

def plot_sigvec(self, iabscissa=0, idx=None):

plot (outer) scaling from diagonal decoding.

iabscissa=1 plots vs function evaluations

idx picks variables to plot if len(idx) < N, otherwise it picks iteration indices (in case, after downsampling).

def plot_stds(self, iabscissa=0, idx=None):

iabscissa=1 means vs function evaluations, idx picks variables to plot

def plot_xrecent(self, iabscissa=0, x_opt=None, annotations=None, xsemilog=None, xnormalize=None):

Undocumented

def register(self, es, append=None, modulo=None):

register a CMAEvolutionStrategy instance for logging, append=True appends to previous data logged under the same name, by default previous data are overwritten.

def save(self, name=None):

data are saved to disk the moment they are added

def save_to(self, nameprefix, switch=False):

saves logger data to a different set of files, for switch=True also the loggers name prefix is switched to the new value

def select_data(self, iteration_indices):

keep only data of iteration_indices

def zip(self, filename=None, unique=True):

write logger data to file filename [+ ...] + '.tar.gz'.

filename defaults to self.name_prefix.

When unique is true, filename is furnished with a unique time stamp.

Return the path (relative or absolute) of the output file.

This function does in essence just tar -czf filename.tar.gz folder where folder defaults to the current plot data folder and filename is created to be unique.

TODO: we may want to be able to add a time stamp to the folder name too, to prevent conficts later?

default_prefix =

Undocumented

append =

append to previous data

counter: int =

number of calls to add

es =

Undocumented

expensive_modulo =

log also values that need an eigendecomposition to be generated every expensive iteration

fighandle =

Undocumented

file_names: tuple[str, ...] =

used in load, however hard-coded in add, because data must agree with name

key_names: tuple[str, ...] =

used in load, however hard-coded in plot

last_correlation_spectrum: dict =

Undocumented

last_iteration =

Undocumented

last_precision_matrix =

Undocumented

last_skipped_iteration =

Undocumented

modulo =

how often to record data, allows calling add without args

name_prefix =

Undocumented

original_fontsize =

Undocumented

persistent_communication_dict =

Undocumented

registered: bool =

Undocumented

relative_allowed_time_for_plotting =

Undocumented

skip_finalize_plotting: bool =

Undocumented

timer_all =

Undocumented

timer_plot =

Undocumented

x =

Undocumented

@property
data =

return dictionary with data.

If data entries are None or incomplete, consider calling .load().data to (re-)load the data from files first.

def _enter_plotting(self, fontsize=7):

assumes that a figure is open

def _finalize_plotting(self):

Undocumented

def _plot_x(self, iabscissa=0, x_opt=None, remark=None, annotations=None, xsemilog=None, xnormalize=False):

see def plot(...

def _unzip(self, filename):

to unzip use "tar -xf filename" in a system shell

and afterwards:

logger = cma.CMADataLogger(extacted_folder_name).plot()

in a Python shell.

def _xlabel(self, iabscissa=0):

Undocumented

_eigen_counter: int =

Undocumented

_key_names_with_annotation: tuple[str, ...] =

used in load to add one data row to be modified in plot