Module cma :: Class CMADataLogger
[hide private]
[frames] | no frames]

Class CMADataLogger

source code

    object --+    
             |    
BaseDataLogger --+
                 |
                CMADataLogger

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(...)
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


logger2 = cma.CMADataLogger('just_another_filename_prefix').load()
logger2.plot()
logger2.disp()
import cma
from matplotlib.pylab import *
res = cma.fmin(cma.Fcts.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
show()

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()

Instance Methods [hide private]
 
__init__(self, name_prefix=u'outcmaes', modulo=1, append=False)
initialize logging of data from a CMAEvolutionStrategy instance, default modulo=1 means logging with each call
source code
 
data(self)
return dictionary with data.
source code
 
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.
source code
 
initialize(self, modulo=None)
reset logger, overwrite original files, modulo: log only every modulo call
source code
 
load(self, filenameprefix=None)
load (or reload) data from output files, load() is called in plot() and disp().
source code
 
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.
source code
 
closefig(self) source code
 
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
source code
 
select_data(self, iteration_indices)
keep only data of iteration_indices
source code
 
plot(self, fig=None, iabscissa=1, iteridx=None, plot_mean=False, foffset=1e-19, x_opt=None, fontsize=9)
plot data from a CMADataLogger (using the files written by the logger).
source code
 
plot_all(self, fig=None, iabscissa=1, iteridx=None, foffset=1e-19, x_opt=None, fontsize=9)
plot data from a CMADataLogger (using the files written by the logger).
source code
 
plot_axes_scaling(self, iabscissa=1) source code
 
plot_stds(self, iabscissa=1) source code
 
plot_mean(self, iabscissa=1, x_opt=None, annotations=None) source code
 
plot_xrecent(self, iabscissa=1, x_opt=None, annotations=None) source code
 
plot_correlations(self, iabscissa=1)
spectrum of correlation matrix and largest correlation
source code
 
plot_divers(self, iabscissa=1, foffset=1e-19)
plot fitness, sigma, axis ratio...
source code
 
_enter_plotting(self, fontsize=9)
assumes that a figure is open
source code
 
_finalize_plotting(self) source code
 
_xlabel(self, iabscissa=1) source code
 
_plot_x(self, iabscissa=1, x_opt=None, remark=None, annotations=None)
If x_opt is not None the difference to x_opt is plotted in log scale
source code
 
downsampling(self, factor=10, first=3, switch=True, verbose=True)
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.
source code
 
disp(self, idx=100)
displays selected data from (files written by) the class CMADataLogger.
source code
 
disp_header(self) source code

Inherited from object: __delattr__, __format__, __getattribute__, __hash__, __new__, __reduce__, __reduce_ex__, __repr__, __setattr__, __sizeof__, __str__, __subclasshook__

Class Variables [hide private]
  default_prefix = u'outcmaes'
Instance Variables [hide private]
  file_names
used in load, however hard-coded in add
  key_names
used in load, however hard-coded in plot
  _key_names_with_annotation
used in load to add one data row to be modified in plot
  modulo
how often to record data, allows calling add without args
  append
append to previous data
  counter
number of calls to add
Properties [hide private]

Inherited from object: __class__

Method Details [hide private]

__init__(self, name_prefix=u'outcmaes', modulo=1, append=False)
(Constructor)

source code 
initialize logging of data from a CMAEvolutionStrategy instance, default modulo=1 means logging with each call
Overrides: object.__init__

data(self)

source code 

return dictionary with data.

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

Overrides: BaseDataLogger.data

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

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

load(self, filenameprefix=None)

source code 

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 (six files), by default 'outcmaes'.

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

add(self, es=None, more_data=[], modulo=None)

source code 

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

The sequence more_data must always 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.

Overrides: BaseDataLogger.add

plot(self, fig=None, iabscissa=1, iteridx=None, plot_mean=False, foffset=1e-19, x_opt=None, fontsize=9)

source code 

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

Arguments

fig
figure number, by default 325
iabscissa
0==plot versus iteration count, 1==plot versus function evaluation number
iteridx
iteration indices to plot

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.savefig('fig325.png')  # save current figure
logger.closefig()

Dependencies: matlabplotlib/pyplot.

Overrides: BaseDataLogger.plot

plot_all(self, fig=None, iabscissa=1, iteridx=None, foffset=1e-19, x_opt=None, fontsize=9)

source code 

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

Arguments

fig
figure number, by default 425
iabscissa
0==plot versus iteration count, 1==plot versus function evaluation number
iteridx
iteration indices to plot

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.savefig('fig425.png')  # save current figure
logger.closefig()

Dependencies: matlabplotlib/pyplot.

plot_divers(self, iabscissa=1, foffset=1e-19)

source code 
plot fitness, sigma, axis ratio...
Parameters:
  • iabscissa - 0 means vs evaluations, 1 means vs iterations
  • foffset - added to f-value

See Also: plot()

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

source code 

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 outcmaes* files
cma.plot('outcmaesdown')

disp(self, idx=100)

source code 

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.

Example

>>> import cma, numpy as np
>>> res = cma.fmin(cma.fcts.elli, 7 * [0.1], 1, {'verb_disp':1e9})  # generate data
>>> 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
>>> l.disp(20)  # some first/last and every 20-th line
>>> l.disp(np.r_[0:999999:100, -1]) # every 100-th and last
>>> l.disp(np.r_[0, -10:0]) # first and ten last
>>> cma.disp(l.name_prefix, np.r_[0::100, -10:])  # the same as l.disp(...)

Details

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

Overrides: BaseDataLogger.disp

See Also: disp()