Package glue :: Module segments :: Class segmentlistdict
[hide private]
[frames] | no frames]

Class segmentlistdict

source code

object --+    
         |    
      dict --+
             |
            segmentlistdict

A dictionary associating a unique label and numeric offset with each of a set of segmentlist objects.

This class implements a standard mapping interface, with additional features added to assist with the manipulation of a collection of segmentlist objects. In particular, methods for taking unions and intersections of the lists in the dictionary are available, as well as the ability to record and apply numeric offsets to the boundaries of the segments in each list.

The numeric offsets are stored in the "offsets" attribute, which itself is a dictionary, associating a number with each key in the main dictionary. Assigning to one of the entries of the offsets attribute has the effect of shifting the corresponding segmentlist from its original position (not its current position) by the given amount.

Example:

>>> x = segmentlistdict()
>>> x["H1"] = segmentlist([segment(0, 10)])
>>> print(x)
{'H1': [segment(0, 10)]}
>>> x.offsets["H1"] = 6
>>> print(x)
{'H1': [segment(6.0, 16.0)]}
>>> x.offsets.clear()
>>> print(x)
{'H1': [segment(0.0, 10.0)]}
>>> x["H2"] = segmentlist([segment(5, 15)])
>>> x.intersection(["H1", "H2"])
[segment(5, 10.0)]
>>> x.offsets["H1"] = 6
>>> x.intersection(["H1", "H2"])
[segment(6.0, 15)]
>>> c = x.extract_common(["H1", "H2"])
>>> c.offsets.clear()
>>> c
{'H2': [segment(6.0, 15)], 'H1': [segment(0.0, 9.0)]}
Instance Methods [hide private]
 
__abs__(self)
Return a dictionary of the results of running .abs() on each of the segmentlists.
source code
 
__add__(self, other) source code
 
__and__(self, other) source code
 
__delitem__(self, key)
del x[y]
source code
 
__iadd__(self, other) source code
 
__iand__(self, other) source code
new empty dictionary

__init__(self, *args)
x.__init__(...) initializes x; see help(type(x)) for signature
source code
 
__invert__(self) source code
 
__ior__(self, other) source code
 
__isub__(self, other) source code
 
__ixor__(self, other) source code
 
__or__(self, other) source code
 
__setitem__(self, key, value)
Set the segmentlist associated with a key.
source code
 
__sub__(self, other) source code
 
__xor__(self, other) source code
 
all_intersects(self, other)
Returns True if each segmentlist in self intersects the corresponding segmentlist in other; returns False if this is not the case or if self is empty.
source code
 
all_intersects_all(self, other)
Returns True if self and other have the same keys, and each segmentlist intersects the corresponding segmentlist in the other; returns False if this is not the case or if either dictionary is empty.
source code
 
coalesce(self)
Run .coalesce() on all segmentlists.
source code
 
contract(self, x)
Run .contract(x) on all segmentlists.
source code
a shallow copy of D
copy(self, keys=None)
Return a copy of the segmentlistdict object.
source code
 
extend(self, other)
Appends the segmentlists from other to the corresponding segmentlists in self, adding new segmentslists to self as needed.
source code
 
extent(self)
Return a dictionary of the results of running .extent() on each of the segmentlists.
source code
 
extent_all(self)
Return the result of running .extent() on the union of all lists in the dictionary.
source code
 
extract_common(self, keys)
Return a new segmentlistdict containing only those segmentlists associated with the keys in keys, with each set to their mutual intersection.
source code
 
find(self, item)
Return a dictionary of the results of running .find() on each of the segmentlists.
source code
 
intersection(self, keys)
Return the intersection of the segmentlists associated with the keys in keys.
source code
 
intersects(self, other)
Returns True if there exists a segmentlist in self that intersects the corresponding segmentlist in other; returns False otherwise.
source code
 
intersects_all(self, other)
Returns True if each segmentlist in other intersects the corresponding segmentlist in self; returns False if this is not the case, or if other is empty.
source code
 
intersects_segment(self, seg)
Returns True if any segmentlist in self intersects the segment, otherwise returns False.
source code
 
is_coincident(self, other, keys=None)
Return True if any segment in any list in self intersects any segment in any list in other.
source code
 
keys_at(self, x)
Return a list of the keys for the segment lists that contain x.
source code
 
map(self, func)
Return a dictionary of the results of func applied to each of the segmentlist objects in self.
source code
 
protract(self, x)
Run .protract(x) on all segmentlists.
source code
 
union(self, keys)
Return the union of the segmentlists associated with the keys in keys.
source code

Inherited from dict: __cmp__, __contains__, __eq__, __ge__, __getattribute__, __getitem__, __gt__, __iter__, __le__, __len__, __lt__, __ne__, __repr__, __sizeof__, clear, fromkeys, get, has_key, items, iteritems, iterkeys, itervalues, keys, pop, popitem, setdefault, update, values, viewitems, viewkeys, viewvalues

Inherited from object: __delattr__, __format__, __reduce__, __reduce_ex__, __setattr__, __str__, __subclasshook__

Static Methods [hide private]
a new object with type S, a subtype of T
__new__(cls, *args) source code
Class Variables [hide private]

Inherited from dict: __hash__

Properties [hide private]

Inherited from object: __class__

Method Details [hide private]

__delitem__(self, key)
(Index deletion operator)

source code 

del x[y]

Overrides: dict.__delitem__
(inherited documentation)

__init__(self, *args)
(Constructor)

source code 

x.__init__(...) initializes x; see help(type(x)) for signature

Returns:
new empty dictionary

Overrides: object.__init__
(inherited documentation)

__new__(cls, *args)
Static Method

source code 
Returns: a new object with type S, a subtype of T
Overrides: object.__new__
(inherited documentation)

__setitem__(self, key, value)
(Index assignment operator)

source code 

Set the segmentlist associated with a key. If key is not already in the dictionary, the corresponding offset is initialized to 0.0, otherwise it is left unchanged.

Overrides: dict.__setitem__

all_intersects(self, other)

source code 

Returns True if each segmentlist in self intersects the corresponding segmentlist in other; returns False if this is not the case or if self is empty.

See also:

.intersects, .intersects_all(), .all_intersects_all()

all_intersects_all(self, other)

source code 

Returns True if self and other have the same keys, and each segmentlist intersects the corresponding segmentlist in the other; returns False if this is not the case or if either dictionary is empty.

See also:

.intersects(), .all_intersects(), .intersects_all()

copy(self, keys=None)

source code 

Return a copy of the segmentlistdict object. The return value is a new object with a new offsets attribute, with references to the original keys, and shallow copies of the segment lists. Modifications made to the offset dictionary or segmentlists in the object returned by this method will not affect the original, but without using much memory until such modifications are made. If the optional keys argument is not None, then should be an iterable of keys and only those segmentlists will be copied (KeyError is raised if any of those keys are not in the segmentlistdict).

More details. There are two "built-in" ways to create a copy of a segmentlist object. The first is to initialize a new object from an existing one with

>>> old = segmentlistdict()
>>> new = segmentlistdict(old)

This creates a copy of the dictionary, but not of its contents. That is, this creates new with references to the segmentlists in old, therefore changes to the segmentlists in either new or old are reflected in both. The second method is

>>> new = old.copy()

This creates a copy of the dictionary and of the segmentlists, but with references to the segment objects in the original segmentlists. Since segments are immutable, this effectively creates a completely independent working copy but without the memory cost of a full duplication of the data.

Returns: a shallow copy of D
Overrides: dict.copy

extract_common(self, keys)

source code 

Return a new segmentlistdict containing only those segmentlists associated with the keys in keys, with each set to their mutual intersection. The offsets are preserved.

find(self, item)

source code 

Return a dictionary of the results of running .find() on each of the segmentlists.

Example:

>>> x = segmentlistdict()
>>> x["H1"] = segmentlist([segment(0, 10)])
>>> x["H2"] = segmentlist([segment(5, 15)])
>>> x.find(7)
{'H2': 0, 'H1': 0}

NOTE: all segmentlists must contain the item or KeyError is raised.

intersects(self, other)

source code 

Returns True if there exists a segmentlist in self that intersects the corresponding segmentlist in other; returns False otherwise.

See also:

.intersects_all(), .all_intersects(), .all_intersects_all()

intersects_all(self, other)

source code 

Returns True if each segmentlist in other intersects the corresponding segmentlist in self; returns False if this is not the case, or if other is empty.

See also:

.intersects(), .all_intersects(), .all_intersects_all()

is_coincident(self, other, keys=None)

source code 

Return True if any segment in any list in self intersects any segment in any list in other. If the optional keys argument is not None, then it should be an iterable of keys and only segment lists for those keys will be considered in the test (instead of raising KeyError, keys not present in both segment list dictionaries will be ignored). If keys is None (the default) then all segment lists are considered.

This method is equivalent to the intersects() method, but without requiring the keys of the intersecting segment lists to match.

keys_at(self, x)

source code 

Return a list of the keys for the segment lists that contain x.

Example:

>>> x = segmentlistdict()
>>> x["H1"] = segmentlist([segment(0, 10)])
>>> x["H2"] = segmentlist([segment(5, 15)])
>>> x.keys_at(12)
['H2']

map(self, func)

source code 

Return a dictionary of the results of func applied to each of the segmentlist objects in self.

Example:

>>> x = segmentlistdict()
>>> x["H1"] = segmentlist([segment(0, 10)])
>>> x["H2"] = segmentlist([segment(5, 15)])
>>> x.map(lambda l: 12 in l)
{'H2': True, 'H1': False}