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:
objectA 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) – APopulationwhere 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.PlotterPlots 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
ProgressPlotterinstance.- Parameters
filename (
str) – The basename of the files. This means it should not include file extensions.property_fn (
callable) – Acallablewhich takes aEAPopulationand aMoleculeobject and returns a property value of that molecule, which is used for the plot. Thecallablemust return a valid value for eachMoleculein 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 anEAPopulationand aMoleculeas input and returnsTrueorFalse. Only molecules which returnTruewill be plotted. Default is for all molecules to be plotted.
-
plot(self, progress)¶ Plot a progress plot.
- Parameters
progress (
Population) – APopulationwhere 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.PlotterPlots which molecules a
Selectorselects.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
SelectionPlotterinstance.- Parameters
filename (
str) – The basename of the files. This means it should not include file extensions.selector (
Selector) – TheSelectorwhose selection of molecules is plotted.x_label (
str, optional) – The label use for the x axis.molecule_label (
callable, optional) – Acallablewhich takes aEAPopulationand 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 theMoleculeon the plot.heat_map_value (
callable, optional) – Acallable, which takes aEAPopulationand 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 aEAPopulationand 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
Plottercreates a plot each timeselectoris finished selecting molecules.- Parameters
progress (
Population) – APopulationwhere each generation of the GA is a subpopulation.- Returns
None
- Return type
NoneType
-