urclib.fuzzylogic.fuzzycurves

Module containing various implementations of FuzzyCurve types.

Author: Patrick Wingo

Version: 0.1

Module Contents

Classes

FuzzyCurve

A partially abstract class for all Curve types to inherit from.

LinearCurve

A simple curve class for representing linearly increasing or decreasing membership functions.

GaussianCurve

A curve that conforms to the equation: \(f(x)=ae^{-\frac{(x-b)^2}{2c^2}}\).

SigmoidCurve

A curve that conforms to the equation: \(f(x) = \frac{L}{1+e^{-k(x-x0)}}\).

TriangleCurve

A curve with a singular triangle/pyramid shape.

TrapezoidCurve

A curve with a trapezoid shape.

StepCurve

A curve with a discrete-step shape.

PolynomialCurve

A curve that conforms to the equation: \(f(x) = ax^2+bx+c\).

CubicCurve

A curve that conforms to the equation: \(f(x)=ax^3+bx^2+cx+d.\)

PiecewiseCurve

A curve used to represent a membership function, result function, or implication.

FieldEntry

Description of how to represent a particular FuzzyCurve attribute/property using a user interface widget

Functions

get_curvelist()

Retrieve a list of curve types defined in the module.

get_curve_typenames()

Retrieve a list of types for all curves other than piecewise.

get_curvetype_for_name(name)

Retrieve class identifier for a provided name

_dict_iteritems2(d)

get_curvename_for_type(classtype)

Get the common-name identifier for the name of a FuzzyCurve subclass.

get_propdetails_for_obj(o)

Retrieve all supported entry attributes.

Attributes

ABC

_curveTemplates

_allProperties

urclib.fuzzylogic.fuzzycurves.ABC
class urclib.fuzzylogic.fuzzycurves.FuzzyCurve(name)

Bases: urclib.fuzzylogic.geomutils.ABC

A partially abstract class for all Curve types to inherit from.

name

The name associated with the curve.

Type:

str

Parameters:

name (str) – The name to assign to the curve.

property ctrlpoints

geomutils.Pt2D objects representing the control points of the curve.

Type:

list

property anchorpoints

geomutils.Pt2D objects representing the control points of the curve.

Type:

list

property drawpoints

Enough points to draw the segment.

Type:

list

monotonic = False
abstract __call__(inval)

Given an x-value, retrieves the appropriate Y-value.

Parameters:

inval (float) – The value to use for the query.

Returns:

The y-equivalent for inVal as a float.

Return type:

float

Raises:

ValueError – If inValue is less than 0 or greater than 1.

copy_segments()

Create a discrete copy of the segments that compose the curve.

Returns:

An explicit copy of all internal segments.

Return type:

list

overwrite_segments(segs)

Replace the curve segments with an explicit copy of the supplied segments.

Parameters:

segs (list) – The segments to copy and use.

static _ensure_end_to_end(segs)

Makes sure that all endpoints of the segments overlap, and that the entire x-range is [0,1]

Parameters:

segs (list) – Segments to be processed; on return, x endpoints will have been adjusted to properly overlap.

abstract construct_multiple(count, overlap, midpoint_to_edge)

Construct multiple copies of this FuzzyCurve, distributing based on the method arguments.

Parameters:
  • count (int) – The number of curves to produce; minimum of 2.

  • overlap (float) – Rough percentage of overlap between curves; in the range of [0,1].

  • midpoint_to_edge (boolean) – If true, the first and last curves will have their midpoints shifted to either edge of the graph space, with additional curves spaced appropriately.

Raises:

ValueError – If count is less than 2.

multiple_allow_overlap()

Flag indicating whether or not multiple instances can overlap when templated.

Returns:

True if overlapping is allowed; False otherwise.

Return type:

bool

class urclib.fuzzylogic.fuzzycurves.LinearCurve(name)

Bases: FuzzyCurve

A simple curve class for representing linearly increasing or decreasing membership functions.

name

The name associated with the curve.

Type:

str

Parameters:

name (str) – The name to assign to the curve.

property xleft

X-coordinate of leftmost point of the curve.

Type:

float

property xright

X-coordinate of the rightmost point of the curve.

Type:

float

property yleft

Y-coordinate of leftmost point of the curve.

Type:

float

property yright

Y-coordinate of the rightmost point of the curve.

Type:

float

property anchorpoints

geomutils.Pt2D objects representing the control points of the curve.

Type:

list

property drawpoints

geomutils.Pt2D objects that can be used to trace the curve in a visual display.

Type:

list

monotonic = True
__repr__()

Return repr(self).

__call__(inval)

Given an x-value, retrieves the appropriate Y-value.

Parameters:

inval (float) – The value to use for the query.

Returns:

The y-equivalent for inVal as a float.

Return type:

float

Raises:

ValueError – If inValue is less than 0 or greater than 1.

construct_multiple(count, overlap, midpoint_to_edge)

Construct multiple copies of this FuzzyCurve, distributing based on the method arguments.

Parameters:
  • count (int) – The number of curves to produce; minimum of 2.

  • overlap (float) – Rough percentage of overlap between curves; in the range of [0,1].

  • midpoint_to_edge (boolean) – If true, the first and last curves will have their midpoints shifted to either edge of the graph space, with additional curves spaced appropriately.

Raises:

ValueError – If count is less than 2.

class urclib.fuzzylogic.fuzzycurves.GaussianCurve(name)

Bases: FuzzyCurve

A curve that conforms to the equation: \(f(x)=ae^{-\frac{(x-b)^2}{2c^2}}\).

Where:
  • a is the height of the bell.

  • b is the center of the bell.

  • c is the “width” of the bell.

name

The name associated with the curve.

Type:

str

Parameters:

name (str) – The name to assign to the curve.

property xmidpoint

The midpoint/apex of the “bell” of the functions.

Type:

float

property spread

Value that controls the width of the “bell”.

Type:

float

property ymin

The lower y-axis value boundary.

Type:

float

property ymax

The upper y-axis value boundary.

Type:

float

property ctrlpoints

geomutils.Pt2D objects representing the control points of the curve.

Type:

list

property anchorpoints

geomutils.Pt2D objects representing the control points of the curve.

Type:

list

__repr__()

Return repr(self).

__str__()

Return str(self).

_refresh_c_denom()
__call__(inval)

Given an x-value, retrieves the appropriate Y-value.

Parameters:

inval (float) – The value to use for the query.

Returns:

The y-equivalent for inVal as a float.

Return type:

float

Raises:

ValueError – If inValue is less than 0 or greater than 1.

construct_multiple(count, overlap, midpoint_to_edge)

Construct multiple copies of this FuzzyCurve, distributing based on the method arguments.

Parameters:
  • count (int) – The number of curves to produce; minimum of 2.

  • overlap (float) – Rough percentage of overlap between curves; in the range of [0,1].

  • midpoint_to_edge (boolean) – If true, the first and last curves will have their midpoints shifted to either edge of the graph space, with additional curves spaced appropriately.

Raises:

ValueError – If count is less than 2.

class urclib.fuzzylogic.fuzzycurves.SigmoidCurve(name)

Bases: FuzzyCurve

A curve that conforms to the equation: \(f(x) = \frac{L}{1+e^{-k(x-x0)}}\).

Where:
  • x0 is the x-coordinate of the midpoint between upper and lower boundaries.

  • L is the maximum y-value.

  • k is the “steepness” of the slope of the curve.

name

The name associated with the curve.

Type:

str

Parameters:

name (str) – The name to assign to the curve.

property xmidpoint

The x-coordinate of the point halfway between the y-coordinate boundaries.

Type:

float

property slope

The “steepness” of the increase in the equation.

Type:

float

property ymin

The lower y-axis value boundary.

Type:

float

property ymax

The upper y-axis value boundary.

Type:

float

property ctrlpoints

geomutils.Pt2D objects representing the control points of the curve.

Type:

list

property anchorpoints

geomutils.Pt2D objects representing the control points of the curve.

Type:

list

monotonic = True
__repr__()

Return repr(self).

__str__()

Return str(self).

__call__(inval)

Given an x-value, retrieves the appropriate Y-value.

Parameters:

inval (float) – The value to use for the query.

Returns:

The y-equivalent for inVal as a float.

Return type:

float

Raises:

ValueError – If inValue is less than 0 or greater than 1.

construct_multiple(count, overlap, midpoint_to_edge)

Construct multiple copies of this FuzzyCurve, distributing based on the method arguments.

Parameters:
  • count (int) – The number of curves to produce; minimum of 2.

  • overlap (float) – Rough percentage of overlap between curves; in the range of [0,1].

  • midpoint_to_edge (boolean) – If true, the first and last curves will have their midpoints shifted to either edge of the graph space, with additional curves spaced appropriately.

Raises:

ValueError – If count is less than 2.

class urclib.fuzzylogic.fuzzycurves.TriangleCurve(name)

Bases: FuzzyCurve

A curve with a singular triangle/pyramid shape.

name

The name associated with the curve.

Type:

str

Parameters:

name (str) – The name to assign to the curve.

property xmidpoint

X-coordinate of the apex of the triangle curve.

Type:

float

property spread

The width of the triangle base.

Type:

float

property ymin

The lower y-axis value boundary.

Type:

float

property ymax

The upper y-axis value boundary.

Type:

float

property anchorpoints

geomutils.Pt2D objects representing the control points of the curve.

Type:

list

property drawpoints

geomutils.Pt2D objects that can be used to trace the curve in a visual display.

Type:

list

__repr__()

Return repr(self).

__str__()

Return str(self).

__call__(inval)

Given an x-value, retrieves the appropriate Y-value.

Parameters:

inval (float) – The value to use for the query.

Returns:

The y-equivalent for inVal as a float.

Return type:

float

Raises:

ValueError – If inValue is less than 0 or greater than 1.

construct_multiple(count, overlap, midpoint_to_edge)

Construct multiple copies of this FuzzyCurve, distributing based on the method arguments.

Parameters:
  • count (int) – The number of curves to produce; minimum of 2.

  • overlap (float) – Rough percentage of overlap between curves; in the range of [0,1].

  • midpoint_to_edge (boolean) – If true, the first and last curves will have their midpoints shifted to either edge of the graph space, with additional curves spaced appropriately.

Raises:

ValueError – If count is less than 2.

class urclib.fuzzylogic.fuzzycurves.TrapezoidCurve(name)

Bases: FuzzyCurve

A curve with a trapezoid shape.

name

The name associated with the curve.

Type:

str

Parameters:

name (str) – The name to assign to the curve.

property xmidpoint

X-coordinate of the midpoint of the trapezoid.

Type:

float

property lowspread

Width of the base of the trapezoid.

Type:

float

property highspread

width of the upper portion of the trapezoid.

Type:

float

property ymin

The lower y-axis value boundary.

Type:

float

property ymax

The upper y-axis value boundary.

Type:

float

property anchorpoints

geomutils.Pt2D objects representing the control points of the curve.

Type:

list

property drawpoints

geomutils.Pt2D objects that can be used to trace the curve in a visual display.

Type:

list

__repr__()

Return repr(self).

__str__()

Return str(self).

__call__(inval)

Given an x-value, retrieves the appropriate Y-value.

Parameters:

inval (float) – The value to use for the query.

Returns:

The y-equivalent for inVal as a float.

Return type:

float

Raises:

ValueError – If inValue is less than 0 or greater than 1.

construct_multiple(count, overlap, midpoint_to_edge)

Construct multiple copies of this FuzzyCurve, distributing based on the method arguments.

Parameters:
  • count (int) – The number of curves to produce; minimum of 2.

  • overlap (float) – Rough percentage of overlap between curves; in the range of [0,1].

  • midpoint_to_edge (boolean) – If true, the first and last curves will have their midpoints shifted to either edge of the graph space, with additional curves spaced appropriately.

Raises:

ValueError – If count is less than 2.

class urclib.fuzzylogic.fuzzycurves.StepCurve(name)

Bases: FuzzyCurve

A curve with a discrete-step shape.

name

The name associated with the curve.

Type:

str

yleft

Height of the left end of the steps.

Type:

float

yright

Height of the right end of the steps.

Type:

float

xMin

Where the steps begin along the x-axis.

Type:

float

xMax

Where the steps end along the x-axis.

Type:

float

Parameters:

name (str) – The name to assign to the curve.

property steps

The number of steps to include in the curve.

Type:

int

property anchorpoints

geomutils.Pt2D objects representing the control points of the curve.

Type:

list

property drawpoints

geomutils.Pt2D objects that can be used to trace the curve in a visual display.

Type:

list

monotonic = True
__call__(inval)

Given an x-value, retrieves the appropriate Y-value.

Parameters:

inval (float) – The value to use for the query.

Returns:

The y-equivalent for inVal as a float.

Return type:

float

Raises:

ValueError – If inValue is less than 0 or greater than 1.

construct_multiple(count, overlap, midpoint_to_edge)

Construct multiple copies of this FuzzyCurve, distributing based on the method arguments.

Parameters:
  • count (int) – The number of curves to produce; minimum of 2.

  • overlap (float) – Rough percentage of overlap between curves; in the range of [0,1].

  • midpoint_to_edge (boolean) – If true, the first and last curves will have their midpoints shifted to either edge of the graph space, with additional curves spaced appropriately.

Raises:

ValueError – If count is less than 2.

class urclib.fuzzylogic.fuzzycurves.PolynomialCurve(name)

Bases: FuzzyCurve

A curve that conforms to the equation: \(f(x) = ax^2+bx+c\).

name

The name associated with the curve.

Type:

str

monotonic

Class attribute; specifies if the curve is monotonic when processed.

Type:

boolean

Parameters:

name (str) – The name to assign to the curve.

property xmidpoint

midpoint along x-axis.

Type:

float

property anchorpoints

geomutils.Pt2D objects representing the control points of the curve.

Type:

list

__repr__()

Return repr(self).

__str__()

Return str(self).

__call__(inval)

Given an x-value, retrieves the appropriate Y-value.

Parameters:

inval (float) – The value to use for the query.

Returns:

The y-equivalent for inVal as a float.

Return type:

float

Raises:

ValueError – If inValue is less than 0 or greater than 1.

construct_multiple(count, overlap, midpoint_to_edge)

Construct multiple copies of this FuzzyCurve, distributing based on the method arguments.

Parameters:
  • count (int) – The number of curves to produce; minimum of 2.

  • overlap (float) – Not used.

  • midpoint_to_edge (boolean) – If true, the first and last curves will have their midpoints shifted to either edge of the graph space, with additional curves spaced appropriately.

Raises:

ValueError – If count is less than 2.

multiple_allow_overlap()

Flag indicating whether or not multiple instances can overlap when templated.

Returns:

True if overlapping is allowed; False otherwise.

Return type:

bool

class urclib.fuzzylogic.fuzzycurves.CubicCurve(name)

Bases: FuzzyCurve

A curve that conforms to the equation: \(f(x)=ax^3+bx^2+cx+d.\)

name

The name associated with the curve.

Type:

str

monotonic

Class attribute; specifies if the curve is monotonic when processed.

Type:

boolean

Parameters:

name (str) – The name to assign to the curve.

property xmidpoint

midpoint along x-axis.

Type:

float

property ctrlpoints

geomutils.Pt2D objects representing the control points of the curve.

Type:

list

property anchorpoints

geomutils.Pt2D objects representing the control points of the curve.

Type:

list

__repr__()

Return repr(self).

__str__()

Return str(self).

__call__(inval)

Given an x-value, retrieves the appropriate Y-value.

Parameters:

inval (float) – The value to use for the query.

Returns:

The y-equivalent for inVal as a float.

Return type:

float

Raises:

ValueError – If inValue is less than 0 or greater than 1.

construct_multiple(count, overlap, midpoint_to_edge)

Construct multiple copies of this FuzzyCurve, distributing based on the method arguments.

Parameters:
  • count (int) – The number of curves to produce; minimum of 2.

  • overlap (float) – Not used.

  • midpoint_to_edge (boolean) – If true, the first and last curves will have their midpoints shifted to either edge of the graph space, with additional curves spaced appropriately.

Raises:

ValueError – If count is less than 2.

multiple_allow_overlap()

Flag indicating whether or not multiple instances can overlap when templated.

Returns:

True if overlapping is allowed; False otherwise.

Return type:

bool

class urclib.fuzzylogic.fuzzycurves.PiecewiseCurve(name, segments=None)

Bases: FuzzyCurve

A curve used to represent a membership function, result function, or implication.

A curve is composed of segments, which themselves are represented as math functions. Curves are used when choosing values in response to inputs, and for representing the final volume for combined implications.

name

The name associated with the curve.

Type:

str

Parameters:
  • name (str) – The name to assign to the curve.

  • segments (list,optional) – List of segments. If omitted, will be initialized with a single linear segment from (0.0,0.0) to (1.0,0.0).

property anchorpoints

geomutils.Pt2D objects representing the control points of the curve.

Type:

list

property ctrlpoints

geomutils.Pt2D objects representing the control points of the curve.

Type:

list

property drawpoints

geomutils.Pt2D objects that can be used to trace the curve in a visual display.

Type:

list

__repr__()

Return repr(self).

__call__(inval)

Given an x-value, retrieves the appropriate Y-value.

Parameters:

inval (float) – The value to use for the query.

Returns:

The y-equivalent for inVal as a float.

Return type:

float

Raises:

ValueError – If inValue is less than 0 or greater than 1.

copy_segments()

Create a discrete copy of the segments that compose the curve.

Returns:

An explicit copy of all internal segments.

Return type:

list

overwrite_segments(segs)

Replace the curve segments with an explicit copy of the supplied segments.

Parameters:

segs (list) – The segments to copy and use.

segments()

Iterator for internal segments

Yields:

BaseSegment – The next segment in the curve

static _ensure_end_to_end(segs)

Makes sure that all endpoints of the segments overlap, and that the entire x-range is [0,1]

Parameters:

segs (list) – Segments to be processed; on return, x endpoints will have been adjusted to properly overlap.

abstract construct_multiple(count, overlap, midpoint_to_edge)

This method is not supported by this subclass, but is included as a safetycheck

Parameters:
  • count (int) – Not used.

  • overlap (float) – Not used.

  • midpoint_to_edge (boolean) – Not used.

Raises:

NotImplementedError – On call.

class urclib.fuzzylogic.fuzzycurves.FieldEntry(lbl, key, min=-inf, max=inf, **kwargs)

Bases: object

Description of how to represent a particular FuzzyCurve attribute/property using a user interface widget

(typically a spinner).

label

The dlg_label to display for the property in the UI.

Type:

str

prop

The actual property/attribute being represented.

Type:

str

min

The minimum allowed value.

Type:

float or int

max

The maximum allowed value.

Type:

float or int

valType

Pattern denoting type of value represented; currently only ‘i’ (int) and ‘f’ (float) are supported.

Type:

str

zeroAllow

Flag indicating whether or not zero is a valid value for the represented property.

Type:

bool

lowerProp

The property of the object that restricts the lower boundary of values.

Type:

str

upperProp

The property of the object that restricts the upper boundary of values.

Type:

str

isX

Flag indicating whether or not this property/attribute represents a value along the x-axis. If so, it may be subject to transformation in the view model.

Type:

bool

stepSize

Increment to apply to property when represented in spinner widget.

Type:

float or int

Parameters:
  • lbl (str) – The dlg_label to display for the property in the UI.

  • key (str) – The actual property/attribute being represented.

  • min (float or int, optional) – The minimum allowed value; defaults to negative infinity.

  • max (float or int, optional) – The maximum allowed value; defaults to positive infinity.

Keyword Arguments:
  • valType (str, optional) – Pattern denoting type of value represented; either ‘i’ (int) or ‘f’ (float); defaults to ‘f’

  • zeroAllow (bool, optional) – Flag indicating whether or not zero is a valid value; defaults to True.

  • lowerProp (str, optional) – The property of the object that restricts the lower boundary of values; default is None.

  • upperProp (str, optional) – The property of the object that restricts the upper boundary of values; default is None.

  • isX (bool, optional) – Flag indicating whether or not this property/attribute represents a value along the x-axis; defaults to False

  • stepSize (float or int, optional) – Increment to apply to property when represented in spinner widget; defaults to None.

urclib.fuzzylogic.fuzzycurves._curveTemplates
urclib.fuzzylogic.fuzzycurves._allProperties = ()
urclib.fuzzylogic.fuzzycurves.get_curvelist()

Retrieve a list of curve types defined in the module.

Returns:

List of string of curve titles to use in menus

Return type:

list

urclib.fuzzylogic.fuzzycurves.get_curve_typenames()

Retrieve a list of types for all curves other than piecewise.

Returns:

List of strings of class names.

Return type:

list

urclib.fuzzylogic.fuzzycurves.get_curvetype_for_name(name)

Retrieve class identifier for a provided name

Parameters:

name (str) – The name of the string to return.

Returns:

The type of the curve class object.

Return type:

type

urclib.fuzzylogic.fuzzycurves._dict_iteritems2(d)
urclib.fuzzylogic.fuzzycurves.get_curvename_for_type(classtype)

Get the common-name identifier for the name of a FuzzyCurve subclass.

Parameters:

classtype (type) – The object type identifier to query.

Returns:

The name of the supplied classType.

Return type:

str

Raises:

ValueError – If type does not have a corresponding name.

urclib.fuzzylogic.fuzzycurves.get_propdetails_for_obj(o)

Retrieve all supported entry attributes.

Parameters:

o (object) – The object to query for curve-related attributes

Returns:

A collection of FieldEntry values that match attributes supported by the queried object.

Return type:

list