PyshGP API¶
There are two main sub-packages that make up PyshGP’s functionality.
The push package provides a Push interpreter and a Push program abstraction.
Other Modules¶
There are a handful of modules that are used throughout both the Push sub-package and the genetic programming sub-package.
pyshgp.tap module¶
The Tap and TapManager abstractions allow for users to inject side effects into PyshGP runs.
These side effects could be print statements for monitoring, writing to log files, or sending messages to
external systems when certain events occur. A Tap can inject a side effect before a function call, after a
function call, or both. Any function or method defined in PyshGP or your application code can be tapped with the
@tap decorator defined in the module.
The taps provided in PyshGP are sufficient for most use cases, but user can define and register their
own Tap objects via inheritance and method overrides.
-
class
pyshgp.tap.CsvTap(root: str, column_names: Sequence[str])[source]¶ Bases:
pyshgp.tap.LogFileTapA
Tapfor writing CSV files.- Parameters
root (str) – The root directory for putting log files (and directories).
column_names (Sequence[str]) – The column names to put in the header of the CSV file.
-
log(id: str, filename: str, row: Dict)[source]¶ Write a to a CSV file.
- Parameters
id (str) – The ID of the function being tapped. This is used to generate a directory for the log file.
filename (str) – The name of the CSV file.
row (dict) – A dictionary that will be written as a row in the CSV. The keys should be in the CsvTap’s list of
column_names.
-
class
pyshgp.tap.JsonLinesTap(root: str)[source]¶ Bases:
pyshgp.tap.LogFileTapA
Tapfor writing JSON lines files.- Parameters
root (str) – The root directory for putting log files (and directories).
-
log(id: str, filename: str, row: Dict)[source]¶ Write a JSON object to a line of a JSON lines file.
- Parameters
id (str) – The ID of the function being tapped. This is used to generate a directory for the log file.
filename (str) – The name of the JSON file.
row (dict) – A dictionary that will be written as a row in the file.
-
class
pyshgp.tap.LogFileTap(root: str)[source]¶ Bases:
pyshgp.tap.TapA
Tapfor writing log files.- Parameters
root (str) – The root directory for putting log files (and directories).
-
dir(id: str) → str[source]¶ Generate the directory for log files associated with the given function ID.
-
log(id: str, filename: str, line: str)[source]¶ Write a line to a log file. Includes a timestamp for each log entry.
- Parameters
id (str) – The ID of the function being tapped. This is used to generate a directory for the log file.
filename (str) – The name of the log file.
line (str) – The line of text to write to the log file.
-
class
pyshgp.tap.StdOutRun(*, pre_print_config: bool = False, pre_print_atoms: bool = False, post_print_best: bool = False)[source]¶ Bases:
pyshgp.tap.TapA
Tapthat prints information from aSearchAlgorithmrun to stdout.- Parameters
pre_print_config (bool, optional) – If True, print the PushConfig before each run. Default False.
pre_print_atoms (bool, optional) – If True, print all the atoms used to generate programs before each run. Default False.
post_print_best (bool, optional) – If True, print a summary about the best program found at the end of each run. Default False.
-
class
pyshgp.tap.StdOutSearchStepTap(every_n_steps: int)[source]¶ Bases:
pyshgp.tap.TapA
Tapthat prints stats from a step (aka generation) of aSearchAlgorithmrun.
-
class
pyshgp.tap.StdOutSimplification[source]¶ Bases:
pyshgp.tap.TapA
Tapthat prints a summary of Genome simplification.
-
class
pyshgp.tap.StdOutSimplificationStep[source]¶ Bases:
pyshgp.tap.TapA
Tapthat prints a notification of a successful Genome simplification step.
-
class
pyshgp.tap.Tap[source]¶ Bases:
abc.ABCA debugging/logging abstraction to a function and collects its arguments and returned.
The methods of a
Tapshould be side effects. It is not recommended to make state changes from with aTapif that state will change code behavior.-
do(id: str, *args, **kwargs)[source]¶ Perform a particular side-effect when called.
- Parameters
id (str) – The ID of the tap.
-
post(id: str, args: Tuple, kwargs: Dict, returned)[source]¶ Perform a particular side-effect directly before the associated function/method is called.
- Parameters
id (str) – The ID of the tap, generated based off of the name qualified name of the function.
args (Tuple) – The positional of the function call. For methods,
args[0]is the class instance.kwargs (Dict) – The keyword args of the function call.
returned (Any) – The returned value of the function call.
-
pre(id: str, args: Tuple, kwargs: Dict)[source]¶ Perform a particular side-effect directly before the associated function/method is called.
- Parameters
id (str) – The ID of the tap, generated based off of the name qualified name of the function.
args (Tuple) – The positional of the function call. For methods,
args[0]is the class instance.kwargs (Dict) – The keyword args of the function call.
-
-
class
pyshgp.tap.TapManager[source]¶ Bases:
objectStores a mapping of function ID to
Tapobject than can be used to inject side effects around functions.The
TapMangerclass is treated as a singleton and should not be instanced. Its methods are static and the state they manage is shared between all usages ofTaps.Function IDs are a fully qualified identifier for a function. They are generated as the concatenation of the module name the function is defined in (ie.
my_package.my_module) and the qualified name of the function definition (ie.MyClass.my_method). The final function ID would bemy_package.my_module.MyClass.my_method.-
static
do(id: str, *args, **kwargs)[source]¶ Perform the
domethod of the tap registered under the given ID.This can be called from any location.
-
static
get(id: str) → Optional[pyshgp.tap.Tap][source]¶ Return the
Tapassociated with given ID orNoneif no tap is registered.
-
static
register(id: str, tap: pyshgp.tap.Tap)[source]¶ Register a
Tapto be performed when the function with the associated ID is called.
-
static
-
pyshgp.tap.set_verbosity(level: int)[source]¶ Register some
Tapobjects in theTapMangerthat print to stdout during an evolutionary run.Verbosity level 0 prints nothing to stdout.
Verbosity level 1 prints minimal run configuration as well as population statistics every 5 generations.
Verbosity level 2+ prints full run configuration, population statics every generation, and traces the genome simplification process.
pyshgp.utils module¶
Utility classes and functions used throughout pyshgp.
-
class
pyshgp.utils.DiscreteProbDistrib[source]¶ Bases:
objectDiscrete Probability Distribution.
-
elements¶
-
-
class
pyshgp.utils.Saveable[source]¶ Bases:
objectAllows a pickle-able class to be written and loaded from a file.
-
class
pyshgp.utils.Token(value)[source]¶ Bases:
enum.EnumEnum class of all Tokens.
-
no_stack_item= 1¶
-
revert= 2¶
-
whole_state= 3¶
-
pyshgp.validation module¶
Module for validating data and raising informative errors.
-
exception
pyshgp.validation.PushError[source]¶ Bases:
ExceptionError raised during Push program execution.
-
classmethod
failed_coerce(thing, push_type)[source]¶ Raise PushError when something fails to coerce to a PushType.
-
classmethod
-
pyshgp.validation.check_1d(seq: MutableSequence) → Sequence[source]¶ Check given data is one-dimensional. Raise error if can’t be easily transformed.
-
pyshgp.validation.check_2d(data)[source]¶ Check given data is two-dimensional. Raise error if can’t be easily transformed.
-
pyshgp.validation.check_X_y(X, y) → Tuple[Sequence, Sequence, int, Sequence][source]¶ Check the given X and y datasets are prepared to be passed to a PushEstimator.
Inspired by the scikit-learn function with the same name. https://scikit-learn.org/stable/modules/generated/sklearn.utils.check_X_y.html#sklearn.utils.check_X_y
-
pyshgp.validation.check_column_types(seq, certainty_proportion: float = 0.2) → Sequence[type][source]¶ Check all elements of each column are of the same type.
-
pyshgp.validation.check_is_fitted(estimator, attribute)[source]¶ Check the given estimator has already been fit.
Inspired by the scikit-learn function with the same name. https://scikit-learn.org/stable/modules/generated/sklearn.utils.validation.check_is_fitted.html#sklearn.utils.validation.check_is_fitted