lsys package

Submodules

lsys.algo module

lsys.algo.add_noise(num, scale=0.5)[source]

produces a random value distributed normally between +- num

lsys.algo.coords_to_xy(coords)[source]
Parameters:coords (numpy.array) – a 2D array
Returns:
  • x (numpy.array) – a 1D array
  • y
lsys.algo.midpoints(np_array)[source]

Converts np array to an array of midpoints ignoring

Parameters:np_array (array-like) – 1-dimensional data
Returns:The length of the output is one less than the length of the input array.
Return type:1-D numpy.array
lsys.algo.xy_to_coords(x, y, stride=None)[source]

lsys.bezier module

This module has functions helpful in the construction of general bezier curves, as well as fitting cubic bezier curves between vertices calculated by the :class:lsys.Lsys

In addition, this module contains the code used to determine the symetric weight factor needed to determine the position of control points that form near-circular curves between three points with an arbitrary interior angle, buiding on the work documented here.

lsys.bezier.bezier(points, segs=100)[source]

Computes nth order bezier curves through the points provided and returns the curve as a list of points to produce line segments.

The order of the bezier curve is determined by the length of the list of points. The length of the output curve numpy.array is equal to segs + 1, such that segs is equal to the number of line segments between the points of the array.

Parameters:
  • points (list-like) – 2D array
  • segs (integer, optional (default = 100)) – The number of line segments to approximate bezier curve
Returns:

  • curve (numpy.array) – 2D array of points along the curve.
  • Reference
  • ———
  • https (//gist.github.com/Juanlu001/7284462)

lsys.bezier.bezier_xy(x, y, weight=None, angle=90, segs=100, keep_ends=True)[source]

Compute cubic bezier curves between each set of three points using symetric control points offset by the weight.

Parameters:
  • x (list-like) – a 1D array of x values
  • y (list-like) – a 1D array of y values
  • weight (float, optional (default = None)) – Weight given to intermediate bezier control points. If None then the weight will be determined by the angle
  • angle (float, optional (default = 90)) – the interior angle for the bezier sections to traverse
  • segs (integer, optional (default = 100)) – the number of segments for bezier curve approzimation between each set of three points
  • keep_ends (bool, optional (default = True)) – include start and end coordinates in output
Returns:

  • tx (numpy.array) – an array of x values
  • ty (numpy.array) – an array of y values

lsys.bezier.binomial(n, k)[source]
Parameters:
  • n (integer) – the order of the binomial to expand
  • k (integer) – the zero indexed position of the binomial expansion coefficient to return
Returns:

  • coefficient (integer) – coefficient for the nth order binomial expansion, kth term
  • reference (http://pomax.github.io/bezierinfo/)

lsys.bezier.circular_weight(angle)[source]

This function utilizes the precomputed circular bezier function with a fit to a 10th order curve created by the following code block:

x = numpy.arange(.5, 180, 0.5)
y = []
for i in x:
    y.append(bezier.find_circular_weight(i, tol=1e-12, max_iters=500))
y = numpy.array(y)
z = numpy.polyfit(x, y, 10)
Parameters:angle (float) – enter the angle to be traversed in degrees
Returns:weight – Weight value for calculating bezier control points that approximate a circular curve.
Return type:float
lsys.bezier.ctrl_pts(p0, p1, w)[source]

Calculate the position of a new point along vector p0 to p1 with magnitude w.

Parameters:
  • p0 (1D array) – the starting point
  • p1 (1D array) – the end point
  • w (float) – the weight factor
Returns:

point – a point along vector p0 to p1 with magnitude w

Return type:

1D array

lsys.bezier.find_circular_weight(angle, guess=0.5, tol=1e-09, max_iters=50, r=10, segs=100)[source]

Finds weight factor for bezier control points such that the angle QRS will be filled with cubic bezier curve approximating a circle that is tangent at the points Q and S.

Parameters:
  • angle (float) – the turn angle in degrees. e.g., if turn angle is 30 degrees, then the angle that needs to be spanned by the curve is 150 degrees.
  • guess (float, optional (default=0.5)) – the initial value for the bisection search
  • tol (float, optional (default=1e-9)) – the tolerance for ceasing bisection search
  • max_iters (int, optional (default=50)) – the maximum iterations for bisection search
  • r (float, optional (default=10)) – the radius for the circle. the larger this value, the more precise the weight will be since tolerance is absolute.
  • segs (int, optional (defualt=100)) – number of points to consider on the circle, and bezier points to compute.
Returns:

  • weight (float) – the magnitude for scaling control points relative to start and end points.
  • error_np (1D numpy.array) – an array of float error values from the curve to circular radius
  • error (float) – the max error minus min error
  • iterations (integer) – the count of bisection search iterations

References

http://spencermortensen.com/articles/bezier-circle/

^ | / |/ h – – – R angle

/ /

/ | / | / o o /o | o /o | o 0 - Q o—-+—-o S – >
0
lsys.bezier.gen_new_guess(guess, pt0, pt2, ptm, kc, r, a, b, segs)[source]

Generator to produce the next guess in the bisection search sequence

lsys.bezier.poly(n, k)[source]

Expands binomial and computes value at t

Parameters:
  • n (integer) – nth order of polynomial
  • k (integer) – positional term for binomial expansion coefficient
Returns:

value

Return type:

float or list of floats

https://gist.github.com/Juanlu001/7284462

lsys.cli module

lsys.fractals module

These are pre-built fractal definitions as dictionaries, **kwargs

lsys.lsys module

class lsys.lsys.Lsys(axiom=None, rule=None, depth=0, a0=90, da=0, step=1, ds=1, unoise=0, forward=’F’, bar=’|’, right=’+’, left=’-‘, goto=’G’, ignore=”, memory_check=True)[source]

Bases: object

This object combines the basic language and directives necessary to construct a Lindenmayer system, compute the coordinates based on the directives, and plot the resulting figure.

string
depth
coord
x
y

References

a0
axiom
bar
static clean_rule(rule)[source]

Clean and validate the rule mapping.

The defaults allow syntax conventions seen in the “Algorithmic Botany”, “Computational Beauty of Nature”, and “Matters Computational: Ideas, Algorithms, Source Code”

Parameters:rule (string or dict) – a mapping for expanding characters of the string. If a string is used, the format requires a valid separator between key and value. These include “:”, “=”, “–>”, “->”, “=>”. If multiple rules are provided, they must be separated by “;” or “,”.
Returns:rule
Return type:dict
commands
compute_coords()[source]

Parse the string and convert to data coordinates for rendering.

Parameters:self (lsys.Lsys()) – uses most of the attributes from the class constructor to parse the expanded string and turn it into coordinates for a plotter.
Returns:
  • x_y (numpy.array)
  • depths (numpy.array)
coords
da
depth
depths
ds
static expand(axiom, rule, depth, bar=’|’, memory_check=True)[source]

String expansion implemting rule on axiom for depth iterations.

Parameters:
  • axiom (string) – the starting string
  • rule (dict) – the rules for expanding the characters of the string
  • depth (integer) – the number of times to perform the expansion
  • bar (string, optional (default = "|")) – the character to indicate that a calculation should be done by the parser based on the present depth of the string expansion
  • memory_check (bool, optional (default = True)) – stop expanding when things get too large
  • Returns
  • string (str) – the string expansion for the given depth
forward
goto
ignore
left
memory_check
plot(as_lc=False, pad=5, square=True, ax=None, **kwargs)[source]
plot_bezier(bezier_weight=None, segs=100, as_lc=False, pad=5, square=True, keep_ends=True, ax=None, **kwargs)[source]
right
rule
step
string
unoise
vocab
x
y

lsys.validate module

lsys.validate.axes_object(ax)[source]

Checks if a value if an Axes. If None, a new one is created. Both the figure and axes are returned (in that order).

lsys.validate.is_np(listlike)[source]

lsys.viz module

lsys.viz.get_coord_lims(coords, pad=5, square=False)[source]
lsys.viz.get_xy_lims(x, y, pad=5, square=False)[source]
lsys.viz.make_colormap(seq)[source]

Return a LinearSegmentedColormap with colors defined as the sequence.

The sequence red to blue can be defined as: make_colormap([‘red’, ‘blue’]) or equivalently make_colormap([(1,0,0), ‘blue’])

For more control over color transitions, floats can be included after pairs of colors to indicate the percentage of the color map that the transition should take place.

make_colormap([‘red’, ‘green’, .25, ‘green’, ‘blue’])

Parameters:seq (list) – a sequence of floats, strings, or RGB-tuples. The floats should be in increasing order and in the interval (0, 1).
Returns:cmap
Return type:matplotlib.colors.LinearSegmentedColormap

Examples

References

http://stackoverflow.com/questions/16834861/create-own-colormap-using-matplotlib-and-plot-color-scale

lsys.viz.pad_lim(lim, pad=5)[source]
lsys.viz.plot(x, y, pad=5, square=False, ax=None, **kwargs)[source]

Plots ‘x’ vs ‘y’ and optionally computes the limits of the axes based on the input data

Parameters:
  • x (numpy.array) – 1D numpy array of points
  • y (numpy.array) – 1D numpy array of points
  • pad (float or int, optional (default = 5)) – the percent spacing between plot data and axes lines. This can be used to zoom the plot. This is only applied if ‘square’ is ‘True’ #TODO: figure out how to make this also pan the plot.
  • square (bool, optional (default = False)) – whether to make the output plot a square with equal aspect ratios for the axes.
  • ax (matplotlib.axes.Axes, optional (default = None)) – the axes artist for plotting. If None is given, a new figure will be created.
  • kwargs (dict, optional) – all remaining keyword arguments are passed into the plot() command
Returns:

ax

Return type:

matplotlib.axes.Axes

lsys.viz.plot_line_collection(coords, pad=5, square=False, ax=None, **kwargs)[source]

Plots a 2d array of points as a matplotlib.LineCollection and computes the limits of the axes based on the input data, since this is not handled by matplotlib by default.

Parameters:
  • coords (numpy.array) – 2D numpy array of points
  • pad (float or int, optional (default = 5)) – the percent spacing between plot data and axes lines. This can be used to zoom the plot. #TODO: figure out how to make this also pan the plot.
  • square (bool, optional (default = False)) – whether to make the output plot a square with equal aspect ratios for the axes.
  • ax (matplotlib.axes.Axes, optional (default = None)) – the axes artist for plotting. If None is given, a new figure will be created.
  • kwargs (dict, optional) – all remaining keyword arguments are passed into the LineCollection
Returns:

ax

Return type:

matplotlib.axes.Axes

lsys.viz.square_aspect(xlim, ylim)[source]

Calculates the limits to produce a square plot if all axes are equal scale.

Module contents