Plotting¶
Plotting is done by Plotter
objects. Plotters are calculators
which produce graphs. They generally do this
by decorating other calculators to hook into them, collect data from
them and then plot the results. To see examples of how to use plotters
work, look at the documentation of the individual plotters. For example
SelectionPlotter
and ProgressPlotter
.
Making New Plotters¶
New plotters should inherit Plotter
and define a
plot()
method. Note that plotters do no necessarily have
to make a plot when plot()
is made. For example,
SelectionPlotter
plots every time the
Selector
it is used with becomes exhausted and it’s
plot()
does nothing.
-
class
Plotter
¶ Bases:
object
A base class for plotters.
Methods
plot
(self, progress)Plots a graph.
-
__init__
(self, /, *args, **kwargs)¶ Initialize self. See help(type(self)) for accurate signature.
-
plot
(self, progress)¶ Plots a graph.
- Parameters
progress (
Population
) – APopulation
where each generation of the GA is a subpopulation.- Returns
None
- Return type
NoneType
-
-
class
ProgressPlotter
(filename, property_fn, y_label, progress_fn=None, filter=<function ProgressPlotter.<lambda>>)¶ Bases:
stk.calculators.ea.plotters.Plotter
Plots how a property changes during a GA run.
The produced plot will show the GA generations on the x axis and the min, mean and max values of an attribute on the y axis.
Examples
Plot how fitness value changes with GA generations
import stk # progress has subpopulations where each subpopulation is a # generation of a GA. progress = stk.Population(...) # Make the plotter which plots the fitness change across # generations. plotter = stk.ProgressPlotter( filename='fitness_plot', property_fn=lambda mol: mol.fitness, y_label='Fitness' ) plotter.plot(progress)
Plot how the number of atoms changes with GA generations
plotter = ProgressPlotter( filename='atom_plot', property_fn=lambda mol: len(mol.atoms), y_label='Number of Atoms' ) plotter.plot(progress)
Methods
plot
(self, progress)Plot a progress plot.
-
__init__
(self, filename, property_fn, y_label, progress_fn=None, filter=<function ProgressPlotter.<lambda> at 0x7fcf9a683b00>)¶ Initialize a
ProgressPlotter
instance.- Parameters
filename (
str
) – The basename of the files. This means it should not include file extensions.property_fn (
callable
) – Acallable
which takes aEAPopulation
and aMolecule
object and returns a property value of that molecule, which is used for the plot. Thecallable
must return a valid value for eachMolecule
in the population.y_label (
str
) – The y label for the produced graph.progress_fn (
callable
, optional) – Takes the population passed toplot()
and excutes a computation on it. This may be useful if you want to apply a normalization to the fitness values in the progress population, for example.filter (
callable
, optional) – Takes anEAPopulation
and aMolecule
as input and returnsTrue
orFalse
. Only molecules which returnTrue
will be plotted. Default is for all molecules to be plotted.
-
plot
(self, progress)¶ Plot a progress plot.
- Parameters
progress (
Population
) – APopulation
where each generation of the GA is a subpopulation.- Returns
None
- Return type
NoneType
-
-
class
SelectionPlotter
(filename, selector, x_label='Molecule: name - fitness value', molecule_label=<function SelectionPlotter.<lambda>>, heat_map_value=<function SelectionPlotter.<lambda>>, heat_map_label='Fitness', order_by=<function SelectionPlotter.<lambda>>)¶ Bases:
stk.calculators.ea.plotters.Plotter
Plots which molecules a
Selector
selects.Examples
import stk # Make a population of molecules. pop = stk.Population(...) # Make a selector. roulette = stk.Roulette(num=10) # Make a plotter. stk.SelectionPlotter('roulette_counter', roulette) # Select the molecules. selected = list(roulette.select(pop)) # There should now be a file called "roulette_counter_1.png" # which shows a graph of all the selected molecules. # Make another population. pop2 = stk.Population(...) # Select molecules from this other population. selected2 = list(roulette.select(pop2)) # There should now be a file called "roulette_counter_2.png" # which shows a graph of all the selected molecules. # Select from the original population again. selected3 = list(roulette.select(pop)) # There should now be a file called "roulette_counter_3.png" # which shows a graph of all the selected molecules. # And so on every time you use "roulette.select()".
Methods
plot
(self, progress)Does nothing.
-
__init__
(self, filename, selector, x_label='Molecule: name - fitness value', molecule_label=<function SelectionPlotter.<lambda> at 0x7fcf96cb7830>, heat_map_value=<function SelectionPlotter.<lambda> at 0x7fcf96cb78c0>, heat_map_label='Fitness', order_by=<function SelectionPlotter.<lambda> at 0x7fcf96cb7950>)¶ Initialize a
SelectionPlotter
instance.- Parameters
filename (
str
) – The basename of the files. This means it should not include file extensions.selector (
Selector
) – TheSelector
whose selection of molecules is plotted.x_label (
str
, optional) – The label use for the x axis.molecule_label (
callable
, optional) – Acallable
which takes aEAPopulation
and aMolecule
, for each molecule which is to be included on the x-axis of the counter plot. It should return a string, which is the label used for theMolecule
on the plot.heat_map_value (
callable
, optional) – Acallable
, which takes aEAPopulation
and aMolecule
, for each molecule which is to be included on the x-axis, and returns a value. The value is used for coloring the heat map used in the plot.heat_map_label (
str
, optional) – The label used for the heat map key.order_by (
callable
, optional) – Acallable
, which takes aEAPopulation
and aMolecule
, for each molecule which is to be included on the x-axis, and returns a value. The value is used to sort the plotted molecules along the x-axis in descending order.
-
plot
(self, progress)¶ Does nothing.
This
Plotter
creates a plot each timeselector
is finished selecting molecules.- Parameters
progress (
Population
) – APopulation
where each generation of the GA is a subpopulation.- Returns
None
- Return type
NoneType
-