"""
The :mod:`paddy.writer` module contains built in data display functions.
Module functions are used to print specific data to terminal, formating
output in a user friendly manner, and quickly generate plots via the
`matplotlib.pyplot` module. Data is often passed via methods found in
:mod:`paddy.Paddy_Runner`.
Routine Listings
----------------
box(g_data, box_type)
Function that produces box plots.
average_gen_plot(g_data)
Function that plots the average fitness for each *generation*.
average_population_plot(g_data)
Function that plots the average fitness for the *population*
during each itteration.
preformace_plotter(info,verbose)
Function that handles other module level functions for plotting.
clean_parameter_print(dirty_values)
Function that prints information regarding the top evaluated seed for
each iteration, including a list of parameter values on a single line.
single_param_print(dirty_values,value_key)
Function that prints the parameters for a user specified seed.
See Also
--------
:meth:`paddy.Paddy_Runner.PFARunner.paddy_plot_and_print` : `PFARunner` method
that handles data output functions.
matplotlib.pyplot : Plotting package dependancy.
See more at `<https://matplotlib.org/>` for specifics.
Notes
-----
The `PFARunner` method `paddy_plot_and_print` calls `preformace_plotter`
when passed relevant arguments such as 'scatter'. Refer to
`paddy_plot_and_print` documentation for information regarding how data is
generated and passed to `preformace_plotter`
"""
# Authors: Armen Beck & Jonathan Fine
# License: BSD
import numpy as np
import matplotlib.pyplot as plt
from paddy.utils import (get_param_names,
get_top_fitness,
random_propogation)
[docs]def box(g_data, box_type):
"""Plot boxplots via `preformance_plotter`.
Function that plots the desired box plot type as specified by the user.
Parameters
----------
g_data : dictionary
A dictionary with keys being the paddy iteration, with '0'
representing the random initiation step, and values being a
numpy-array containig parameter and gausian values.
box_type : integer
One of the integers: 1, 2, or 3. The number is associated with a
respective box plot type detailed in the notes section.
See Also
--------
:meth:`paddy.Paddy_Runner.PFARunner.paddy_plot_and_print` : `PFARunner`
method that handles data output functions.
preformance_plotter : Function that manages plotting arguments passed by
`paddy.Paddy_Runner.PFARunner.paddy_plot_and_print`
plt.boxplot : Function that generates boxplots as part of the
`matplotlib.pyplot` module.
Notes
-----
The `box_type` parameter is an integer assigned as a numeric
representation of a string argument passed via the `verbose` parameter of
the `preformance_plotter` function. The numeric values are related to the
string arguments as:
* 1: equivalent to 'box'.
* 2: equivalent to 'box_hidden'
* 3: equivalent to 'box_notched'
See documentation for `preformance_plotter` and `paddy_plot_and_print` for
details on how `box` is called.
"""
plot_data = []
x_val = []
for i in g_data:
temp = g_data[i]
x_val.append(int(i))
data = temp
plot_data.append(data)
if box_type == 1:
#default box plot
return plt.boxplot(plot_data, positions=x_val)
if box_type == 2:
#box plot without scatter
return plt.boxplot(plot_data, 0, '', positions=x_val)
if box_type == 3:
#box plot with indents
return plt.boxplot(plot_data, 1, positions=x_val)
[docs]def average_gen_plot(g_data):
"""Plot average fitness for each generation.
Function that plots the average fitness value for the seeds sown during
each iteration.
Parameters
----------
g_data : dictionary
A dictionary with keys being the paddy iteration, with '0'
representing the random initiation step, and values being a
numpy-array containig parameter and gausian values.
See Also
--------
:meth:`paddy.Paddy_Runner.PFARunner.paddy_plot_and_print` : `PFARunner`
method that handles data output functions.
preformance_plotter : Function that manages plotting arguments passed by
`Paddy_Runner.PFARunner.paddy_plot_and_print`.
plt.plot : Plotting function as part of the `matplotlib.pyplot` module.
Notes
-----
`average_gen_plot` is typically called by `preformance_plotter` via the
`PFARunner` method `paddy_plot_and_print`. See respective documentation
for detailes on how `average_gen_plot` is called.
"""
ave = []
counter = -1
x_list = []
for i in g_data:
counter += 1
x_list.append(counter)
ave.append(np.average(g_data[i]))
return plt.plot(x_list, ave, color='orange',
label='Average Fitness of Iteration')
[docs]def average_population_plot(g_data):
"""Plot average fitness for the population during each iteration.
Function that plots the average fitness value for the seeds within
the population of evaluated seeds at each given interation.
Parameters
----------
g_data : dictionary
A dictionary with keys being the paddy iteration, with '0'
representing the random initiation step, and values being a
numpy-array containig parameter and gausian values.
See Also
--------
:meth:`paddy.Paddy_Runner.PFARunner.paddy_plot_and_print` : `PFARunner`
method that handles data output functions.
preformance_plotter : Function that manages plotting arguments passed by
`paddy.Paddy_Runner.PFARunner.paddy_plot_and_print`
matplotlib.pyplot
Notes
-----
`average_population_plot` is typically called by `preformance_plotter` via
the `PFARunner` method `paddy_plot_and_print`. See respective
documentation for detailes on how `average_gen_plot` is called.
"""
temp = []
ave = []
counter = -1
x_list = []
for i in g_data:
counter += 1
x_list.append(counter)
temp.append(g_data[i])
ave.append(np.average(np.concatenate(temp)))
return plt.plot(x_list, ave, color='green', label='Average Fitness of Population')
[docs]def clean_parameter_print(dirty_values):
"""Prints parameter values of `PFARunner` atribute ``top_values``.
Function that prints information regarding the top seed evaluated for
sowing during each iteration of paddy.
Parameters
----------
dirty_values : dictionary
Dictionary with the same structure of `PFARunner.top_values`.
See Also
--------
:meth:`paddy.Paddy_Runner.PFARunner.run_paddy`
:meth:`paddy.Paddy_Runner.PFARunner.paddy_plot_and_print`
Notes
-----
This function prints two lines for each iteration, where the first line
contains details on what seed was most fit during the selection step and
the second line consists of a list of parameter values. Be mindfull of
this when using this function as it is intended for low iteration
implementations of paddy, such as deep learning, and a high iteration run
will result in a proportionaly long print time.
The function is typically called by `PFARunner.paddy_plot_and_print`
when passed the argument 'final_results'.
"""
for i in dirty_values:
temp = []
for j in dirty_values[i]['parameters']:
temp.append(j[0])
print(i+':seed_'+str(dirty_values[i]['seed'])
+':Fitness:'+str(dirty_values[i]['fitness'])+' Parameters:')
print(temp)
[docs]def single_param_print(dirty_values, value_key):
"""Prints the parameters for a specific seed.
Function that typically prints parameter values stored in
`PFARunner.seed_params` when called throughout a paddy run.
Parameters
----------
dirty_values : list of array-like, shape = (seed_counter,parameters,2)
A list of numpy-arrays containing the parameter and gaussian values
for each `PaddyParameter` instance in `PFARunner.space` of a given
paddy run. Typically the `PFARunner.seed_params` atribute.
value_key : integer
A integer that represents the seed number that the parameters are to
be printed for.
See Also
--------
:meth:`paddy.Paddy_Runner.PFARunner.run_paddy` : method that excecutes the
paddy field algorithm.
Notes
-----
This function is natively called when 'top_gen' or 'pop' are passed via
`paddy.Paddy_Runner.PFARunner.run_paddy` when listed as an element in the
`verbose` parameter. The list that is printed contains strictly the
parameter values of the `PFARunner.space` atributes of a given seed,
rather than the associated gaussian values.
"""
temp = []
for i in dirty_values[value_key]:
temp.append(i[0])
print(temp)