cf.Comparison

class cf.Comparison(relation, value, units=None)

Bases: object

Create an object for storing a comparison expression.

A comparison expression comprises a relation (such as ‘less than’) and a value (any object capable of being related to with the standard comparison operators). These are stored in the relation and value attributes respectively.

The comparison is evaluated for an arbitrary object with the evaluate method.

Valid relations are:

Relation Description
lt Strictly less than
le Less than or equal
gt Strictly greater than
ge Greater than or equal
eq Equals (within a tolerance)
ne Not equal to (within a tolerance)
inside Inside a given range (range bounds included)
outside Outside a given range (range bounds excluded)

See also

As a convenience, for each relation in the above list there is an identically named function which returns the appropriate Comparison object.

Examples

>>> c = cf.Comparison('le', 5)
>>> c.evaluate(4)
True
>>> c.evaluate(5)
True
>>> c = lt(5)
>>> c.evaluate(4)
True
>>> c = cf.Comparison('inside', (1,2))
>>> a = numpy.arange(4)
>>> c.evaluate(a)
array([False,  True,  True, False], dtype=bool)
>>> (a >= 1) & (a <= 2)
array([False,  True,  True, False], dtype=bool)

Initialization

Parameters :
relation : str

The comparison operator type.

value

The value on the right hand side of the comparison operation.

units : str or Units, optional

The units of value. By default, the same units as the left hand side of the comparison operation are assumed.

copy()

Return a deep copy.

Examples

>>> c.copy()
dump(id=None, omit=())

Return a string containing a full description of the object.

Parameters:
  • id (str) – Optional. Set the common prefix of component names. If None then defaults to the class name.
  • omit (sequence) – Optional. Omit the given attributes from the description.
Returns:

A string containing the description.

See also

cf.dump

evaluate(x)

Evaluate the comparison expression for a given object.

Parameters :
x

The object for the left hand side of the comparison expression.

Returns :
out

The output of the comparison given by the relation attribute with x on the left hand side and the value attribute on the right hand side.

Examples

>>> c = cf.Comparison('lt', 5.5)
>>> c.evaluate(6)
False
>>> c = cf.Comparison('inside', (1,2))
>>> array = numpy.arange(4)
>>> array
array([0, 1, 2, 3])
>>> c.evaluate(array)
array([False,  True,  True, False], dtype=bool)

Previous topic

cf.Units

Next topic

cf.CoordinateList

This Page