Range

class sofia_redux.scan.utilities.range.Range(min_val=-inf, max_val=inf, include_min=True, include_max=True)[source]

Bases: ABC

Initialize a Range object.

A Range object defines a range of values that are considered “valid”, and provides a few methods to check if given values fall within this range.

Parameters:
min_valint or float or units.Quantity, optional

The minimum value of the range.

max_valint or float or units.Quantity, optional

The maximum value of the range.

include_minbool, optional

Whether the minimum value of the range is considered valid.

include_maxbool, optional

Whether the maximum value of the range is considered valid.

Examples

>>> r = Range(1, 3)
>>> print(r(np.arange(5)))
[False  True  True  True False]
>>> r = Range(1, 3, include_min=False, include_max=False)
>>> print(r(np.arange(5)))
[False False  True False False]

Attributes Summary

bounded

Return whether the Range has upper or lower bounds.

lower_bounded

Return whether the range has a lower limit.

midpoint

Return the midpoint of the Range.

span

Return the span of the range.

upper_bounded

Return whether the range has an upper limit.

Methods Summary

__call__(value)

Check whether given values fall inside the range.

copy()

Return a copy of the Range.

empty()

Remove the range values.

flip()

Swap the minimum and maximum values of the Range.

from_spec(spec[, is_positive])

Return a Range object from a string specification.

full()

Set the range to -inf -> +inf

full_range()

Return a completely unbounded Range.

grow(factor)

Grow a bounded range.

in_range(value)

Return whether a given value is inside the valid range.

include(*args)

Extend the range if necessary.

include_value(value)

Extend the range if necessary to include the provided value.

intersect_with(*args)

Intersects the current range with the new supplied range.

is_empty()

Return whether the range is empty.

is_intersecting(other)

Return whether this Range is intersecting with another.

negative_range()

Return a range valid for negative values.

positive_range()

Return a range valid for positive values.

scale(value)

Scale the range by a given factor.

Attributes Documentation

bounded

Return whether the Range has upper or lower bounds.

Returns:
bool
lower_bounded

Return whether the range has a lower limit.

Returns:
bool
midpoint

Return the midpoint of the Range.

Returns:
float or units.Quantity
span

Return the span of the range.

The span is defined as the difference between the maximum and minimum range values.

Returns:
float or units.Quantity
upper_bounded

Return whether the range has an upper limit.

Returns:
bool

Methods Documentation

__call__(value)[source]

Check whether given values fall inside the range.

Parameters:
valueint or float or units.Quantity or numpy.ndarray
Returns:
in_rangebool or np.ndarray (bool)
copy()[source]

Return a copy of the Range.

Returns:
Range
empty()[source]

Remove the range values.

Sets the (min,max) to (+inf,-inf).

Returns:
None
flip()[source]

Swap the minimum and maximum values of the Range.

Returns:
None
static from_spec(spec, is_positive=False)[source]

Return a Range object from a string specification.

Specifications should be of the form min:max or min-max if is_positive is True where min and max should be replaced by a number. min and max can also be replaced by ‘*’ indicating no minimum or maximum range limit should be placed. i.e., the spec 5:* would range from 5 to infinity.

A single value can also be supplied by just supplying a single number without any ‘:’ or ‘-’ delimiter. Note that the returned range will be inclusive and must not contain any units. i.e. min <= valid <= max. You can always set units later by using the scale method. e.g.,

  • An infinite range may be set with ‘*’

  • Unbounded ranges may be set with ‘>=#’, ‘<=#’, ‘>#’, ‘<#’ where # indicates a number.

>>> r = Range.from_spec('1:3')
>>> r.scale(units.Unit('minute'))
>>> print(r)
(1.0 min -> 3.0 min)
Parameters:
specstr or None

The range specification. Usually read from a configuration file.

is_positivebool, optional

If True, all values in the range are considered positive and any ‘-’ character in spec will be treated as a delimiter rather than a minus sign.

Returns:
Range
full()[source]

Set the range to -inf -> +inf

Returns:
None
static full_range()[source]

Return a completely unbounded Range.

The range will extend from -infinity -> +infinity.

Returns:
Range
grow(factor)[source]

Grow a bounded range.

The span is increased by the specified factor while keeping the midpoint fixed.

Parameters:
factorint or float
Returns:
None
in_range(value)[source]

Return whether a given value is inside the valid range.

Parameters:
valueint or float or units.Quantity or numpy.ndarray.
Returns:
validbool or numpy.ndarray (bool)
include(*args)[source]

Extend the range if necessary.

Parameters:
argsRange or value, value

Either one or two arguments can be supplied. If a single argument is received, it should be another Range object. Otherwise, an inclusion minimum and maximum must be supplied.

Returns:
None
include_value(value)[source]

Extend the range if necessary to include the provided value.

Note that a NaN value will set the Range to be unbounded (-inf, inf).

Parameters:
valueint or float or units.Quantity.
Returns:
None
intersect_with(*args)[source]

Intersects the current range with the new supplied range.

Parameters:
argsRange or value, value

Either one or two arguments can be supplied. If a single argument is received, it should be another Range object. Otherwise, an intersection minimum and maximum must be supplied.

Returns:
None
is_empty()[source]

Return whether the range is empty.

A Range is considered empty when the minimum is greater than the maximum.

Returns:
bool
is_intersecting(other)[source]

Return whether this Range is intersecting with another.

Parameters:
otherRange
Returns:
intersectingbool
static negative_range()[source]

Return a range valid for negative values.

Returns:
Range
static positive_range()[source]

Return a range valid for positive values.

Returns:
Range
scale(value)[source]

Scale the range by a given factor.

Parameters:
valueint or float or units.Quantity or units.Unit
Returns:
None