glyph.utils package

Submodules

glyph.utils.argparse module

Collection of helper functions for arparse.

non_negative_int(string)[source]

Check whether string is an integer greater than -1.

np_infinity_int(string)[source]
ntuple(n, to_type=<class 'float'>)[source]

Check whether string is an n-tuple.

positive_int(string)[source]

Check whether string is an integer greater than 0.

readable_file(string)[source]

Check weather file is readable

readable_yaml_file(string)[source]

Check weather file is a .yaml file and readable

unit_interval(string)[source]

Check whether string is a float in the interval [0.0, 1.0].

glyph.utils.break_condition module

class SoftTimeOut(ttl)[source]

Bases: object

Break condition based on a soft time out.

Start a new generation as long as there is some time left.

Parameters:ttl – time to live in seconds
alive
now
break_condition(target=0, error_index=0, ttl=0, max_iter=inf)[source]

Combined breaking condition based on time to live, minimum target and maximum number of iterations.

Parameters:
  • target – value of desired error metric
  • error_index – index in fitness tuple
  • ttl – time to live in seconds
  • max_iter – maximum number of iterations
soft_max_iter(app, max_iter=inf)[source]

Soft breaking condition. Will check after each generation weather maximum number of iterations is exceeded.

Parameters:max_iter – maximum number of function evaluations
Returns:bool(iter) > max_iter
soft_target(app, target=0, error_index=0)[source]

Soft breaking condition. Will check after each generation minimum error is reached.

Parameters:
  • target – value of desired error metric
  • error_index – index in fitness tuple
Returns:

bool(min_error) <= target

glyph.utils.logging module

load_config(config_file, placeholders=None, level=20)[source]

Load logging configuration from .yaml file.

log_level(verbosity)[source]

Convert numeric verbosity to logging log levels.

print_dict(p_func, d)[source]

Pretty print a dictionary

Parameters:p_func – printer to use (print or logging)
print_params(p_func, gp_config)[source]

Pretty print a glyph app config

glyph.utils.numeric module

class SlowConversionTerminator(method, step_size=10, min_stat=10, threshold=25)[source]

Bases: object

Decorate a minimize method used in scipy.optimize.minimize to cancel non promising constant optimizations.

The stopping criteria is based on the improvement rate :math:`

rac{Delta f}[Delta fev}`.

If the improvement rate is below the q_{threshold} quantile for a given number of function evaluations, optimization is stopped. :params method: see scipy.optimize.minimize method :params step_size: number of function evaluations betweem iterations :params min_stat: minimum sample size before stopping :params threshold: quantile
cvrmse(x, y)[source]

Coefficient of variation, with respect to x, of the rmse.

hill_climb(fun, x0, args, precision=5, maxfev=100, directions=5, target=0, rng=<module 'numpy.random' from '/home/docs/checkouts/readthedocs.org/user_builds/glyph/envs/latest/lib/python3.7/site-packages/numpy/random/__init__.py'>, **kwargs)[source]

Stochastic hill climber for constant optimization. Try self.directions different solutions per iteration to select a new best individual.

Parameters:
  • fun – function to optimize
  • x0 – initial guess
  • args – additional arguments to pass to fun
  • precision – maximum precision of x0
  • maxfev – maximum number of function calls before stopping
  • directions – number of directions to explore before doing a hill climb step
  • target – stop if fun(x) <= target
  • rng – (seeded) random number generator
Returns:

scipy.optimize.OptimizeResult

nrmse(x, y)[source]

Normalized, with respect to x, root mean square error.

rms(y)[source]

Root mean square.

rmse(x, y)[source]

Root mean square error.

silent_numpy(func)[source]
strict_subtract(x, y)[source]

Module contents

class Memoize(fn)[source]

Bases: object

Memoize(fn) - an instance which acts like fn but memoizes its arguments

Will only work on functions with non-mutable arguments http://code.activestate.com/recipes/52201/

key_set(itr, key=<built-in function hash>)[source]
partition(pred, iterable)[source]

Use a predicate to partition entries into false entries and true entries.

>>> is_odd = lambda x: x % 2
>>> odd, even = partition(is_odd, range(10))
>>> list(odd)
[0, 2, 4, 6, 8]
random_state(obj, rng=<module 'random' from '/home/docs/checkouts/readthedocs.org/user_builds/glyph/envs/latest/lib/python3.7/random.py'>)[source]

Do work inside this contextmanager with a random state defined by obj.

Looks for _prev_state to seed the rng. On exit, it will write the current state of the rng as _tmp_state to the obj.

Params obj:Any object.
Params rng:Instance of a random number generator.