crops.elements.intervals module

Integer interval objects are defined here.

class intinterval(description='intinterval', subint=None)[source]

Bases: object

An integer interval object.

The crops.elements.intervals.intinterval class represents a data structure to hold all non-connected sub-intervals making it up, extra information tags, and set operations for the intervals.

It contains functions to organise intervals and make operations on them.

Parameters:
  • description (str, optional) – An interval ID, defaults to ‘intinterval’.
  • subint (list [int], optional) – A list of two-integer lists, defaults to [].
Variables:
Example:
>>> from crops.elements import intervals as cei
>>> my_interval = cei.intinterval('an interval')
>>> my_interval2 = cei.intinterval('another interval')
>>> my_interval = my_interval.union(15)
>>> my_interval2 = my_interval2.union([2, 7])
>>> my_interval = my_interval.union(my_interval2)
>>> print(my_interval)
Integer interval object: (id="an interval", subsets=[[2, 7], [15, 15]])
>>> print(my_interval.intersection([3,19])) # Just printing, not setting the values
Integer interval object: (id="an interval", subsets=[[3, 7], [15,15]])
>>> my_interval.n_elements()
7
>>> my_interval.terminals()
[2,15]
>>> my_interval.contains(16)
False
>>> my_interval.contains(my_interval2)
True
>>> my_interval.contains([5, 7])
True
>>> my_interval = my_interval.subtract([2, 6])
Integer interval object: (id="an interval", subsets=[[7, 7], [15, 15]])
>>> my_interval = my_interval.symdiff([10, 20])
Integer interval object: (id="an interval", subsets=[[7, 7], [10, 14], [16, 20]])
addtag(tag, value)[source]

Add a new tag to crops.elements.intervals.intinterval.

Parameters:
  • tag (str) – Key argument.
  • value (Any) – Value argument.
Raises:

TypeError – If tag is not a string.

contains(other)[source]

Check if input interval is fully contained in self. B ⊂ A : A = self, B = other.

Parameters:other (int, list [int], crops.elements.intervals.intinterval) – Another interval.
Returns:Whether self contains other or not.
Return type:bool
copy()[source]
deepcopy()[source]
deltag(tag)[source]

Delete a tag from crops.elements.intervals.intinterval.

Parameters:tag (str) – Key argument.
Raises:TypeError – If argument is not a string.
description(newdescription=None)[source]

Return or modify the ‘description’ tag.

Parameters:newdescription (str, optional) – If given, the value of ‘description’ in tags is replaced by ‘newdescription’, defaults to None.
Returns:If newdescription not given, the value of ‘description’ in tags is returned.
Return type:str
intersection(other, newdesc=None)[source]

Compute A ⋂ B : A = self, B = other.

Parameters:
Returns:

The Intersection of both intervals.

Return type:

crops.elements.intervals.intinterval

kind = 'Integer interval'
n_elements(other=None)[source]

Return the number of elements in the interval, subinterval, or any other set.

Parameters:other (int, list [int], crops.elements.intervals.intinterval, optional) – Input argument, defaults to None.
Returns:Number of elements in the interval (self if other is None, other otherwise).
Return type:int
subint
subtract(other, newdesc=None)[source]

Compute A \ B : A = self, B = other

Parameters:
Returns:

The result of subtracting other from self.

Return type:

crops.elements.intervals.intinterval

symdiff(other, newdesc=None)[source]

Compute A Δ B : A = self, B = other

Parameters:
Returns:

The Symmetric difference of both intervals.

Return type:

crops.elements.intervals.intinterval

tags
terminals(other=None)[source]

Return the first and last element in the crops.elements.intervals.intinterval.

Parameters:other (int, list [int], crops.elements.intervals.intinterval, optional) – If not self.subint but another interval is to be trimmed, defaults to None.
Returns:A list of two integers indicating lower and higher limits of the interval (self if other is None, other otherwise). If input is an empty interval, this function will return an empty list.
Return type:list [int]
union(other, newdesc=None)[source]

Compute A ⋃ B : A = self, B = other.

Parameters:
Returns:

The Union of both intervals.

Return type:

crops.elements.intervals.intinterval