utils.py

The utils module provides classes and functions that are used throughout the push interpreter and GP modules.

class pyshgp.utils.Character(char)

Holds a string of length 1.

Used to distinguish between string and char literals in Push program interpretation.

Attributes

char (str) String of length 1.
class pyshgp.utils.PushVector(lst, typ)

List where elements are all of same pysh literal type. Is a subclass of the Python list, and has access to all list methods.

Parameters:

lst : list of typ

Python list where all elements are of type typ.

typ : data-type

Python Type that denotes the type of the vector.

Attributes

typ (type) Python type that all elements must be.
append(item)

Overload of the append method to ensure that only items of the correct type are placed in the PushVector.

Parameters:

item :

The thing trying to be placed into the PushVector.

pyshgp.utils.count_parens(tree)

Returns the number of paren pairs in tree.

Parameters:

tree : list

Nested list structure equivalent to tree.

Returns:

Integer equal to the number of paren/bracket pairs.

pyshgp.utils.count_points(tree)

Returns the number of points in tree. Each atom and each pair of parentheses counts as a point.

Parameters:

tree : list

Nested list structure equivalent to tree.

Returns:

Integer equal to the number of points.

pyshgp.utils.ensure_list(thing)

Returns argument inside of a list if it is not already a list.

Parameters:

thing :

Anything!

Returns:

If thing is a list, returns thing, otherwise returns [thing].

Examples

>>> ensure_list("ABC")
["ABC"]
>>> ensure_list([1, 2, 3])
[1, 2, 3]
pyshgp.utils.flatten_all(lst)

Recursively flattens nested lists into a single list.

Parameters:lst : Nested lists.
Returns:Flattened lists.

Examples

>>> flatten_all([1, [2, 3, [4], 5]])
[1, 2, 3, 4, 5]
pyshgp.utils.gaussian_noise_factor()

Returns Gaussian noise of mean 0, std dev 1.

Returns:Float samples from Gaussian distribution.

Examples

>>> gaussian_noise_factor()
1.43412557975
>>> gaussian_noise_factor()
-0.0410900866765
pyshgp.utils.int_to_char(i)

Convert int i to chars and only get English-friendly chars

Parameters:

i : int

Any integer.

Returns:

English-friendly string of length 1.

Examples

>>> int_to_char(42)
'J'
>>> int_to_char(-42)
'v'
pyshgp.utils.is_float_type(thing)

Returns true if thing is an float, agnostic to numpy or not.

Todo

TODO: This function needs a unit test.

Parameters:thing : Any python obect or primitive.
Returns:True if thing is an float. False otherwise.

Examples

>>> is_float_type('Hello')
False
>>> is_float_type(1.23)
True
pyshgp.utils.is_int_type(thing)

Returns true if thing is an int or long, agnostic to Python version.

Todo

TODO: This function needs a unit test.

Parameters:thing : Any python obect or primitive.
Returns:True if thing is an int. False otherwise.

Examples

>>> is_int_type('Hello')
False
>>> is_int_type(7)
True
pyshgp.utils.is_str_type(thing)

Returns true if thing is a string, agnostic to Python version.

Todo

TODO: This function needs a unit test.

Parameters:thing : Any python obect or primitive.
Returns:True if thing is a str. False otherwise.

Examples

>>> is_str_type('Hello')
True
>>> is_str_type(7)
False
pyshgp.utils.keep_number_reasonable(n)

Returns a version of n that obeys the limits set in constants.

Parameters:

n : int or float

Any numeric value.

Returns:

n clamped to -max_number_magnitude < n < max_number_magnitude

pyshgp.utils.levenshtein_distance(s1, s2)

Computes the string edit distance based on the Levenshtein Distance.

All credit for implementation goes to: https://en.wikibooks.org/wiki/Algorithm_Implementation/Strings/Levenshtein_distance#Python Much appreciated.

Note

If s1 and s2 must both be strings or both be list. Cannot mix types.

Parameters:

s1 : str or list

String or list.

s1 : str or list

Other string or list.

Returns:

Integer equal to the number of edits to get from s1 to s2.

pyshgp.utils.median_absolute_deviation(a)

Returns the MAD of X.

Parameters:a : array-like, shape = (n,)
Returns:mad : float
pyshgp.utils.merge_dicts(*dict_args)

Merges arbitrary number of dicts into one dict.

Given any number of dicts, shallow copy and merge into a new dict, precedence goes to key value pairs in latter dicts. Taken From: http://stackoverflow.com/a/26853961/4297413 Thanks to Aaron Hall

Parameters:

dict_args: dicts

Arbitrary number of arguments, all must be dicts.

Returns:

Result of merging all dicts into a single dict.

pyshgp.utils.merge_sets(*set_args)

Given any number of sets, shallow copy and merge into a new set.

Parameters:

set_args: dicts

Arbitrary number of arguments, all must be sets.

Returns:

Result of union-ing all sets into a single set.

pyshgp.utils.perturb_with_gaussian_noise(sd, n)

Returns n perturbed with standard deviation.

Parameters:

sd : float

Standard deviation

n : float

Number to perturb.

Returns:

Perturbed float.

Examples

>>> perturb_with_gaussian_noise(5, 0)
5.03608878454
>>> perturb_with_gaussian_noise(1, 100)
99.9105032498
pyshgp.utils.recognize_pysh_type(thing)

Returns a string denoting the Push type of thing.

Parameters:

thing : Any python obect or primitive.

Returns:

A string with a _ as the first char. This is how Pysh types are denoted

throughout the entire package. If there is no appropriate Pysh type,

returns False.

Examples

>>> recognize_pysh_type(True)
'_bool'
>>> recognize_pysh_type(77)
'_integer'
>>> recognize_pysh_type(abs)
False
pyshgp.utils.reductions(f, l)

Returns intermediate values of the reduction of l by f.

Parameters:

f : function

Function to be reduced down l.

l : list

List to reduce f down.

Returns:

List of intermediate values.

Examples

>>> reductions(lambda x,y: x * y, [1, 3, 5, 7])
[1, 3, 15, 105]
pyshgp.utils.test_and_train_data_from_domains(domains)

Creates train and test data.

Takes a list of domains and creates a set of (random) train inputs and a set of test inputs based on the domains. Returns [train test].

Note

This will likely no longer be used once integration with scikit-learn and other libraries improves.

pyshgp.utils.type_to_pysh_type(typ) → str

Returns a string denoting the Push type version of given python/numpy type.

Parameters:

typ: A python or numpy type.

Returns:

A string with a _ as the first char. This is how Pysh types are denoted

throughout the entire package. If there is no appropriate Pysh type,

returns False.

Examples

>>> type_to_pysh_type(int)
'_integer'