SegmentListDict

class gwpy.segments.SegmentListDict(*args)[source]

A dict of SegmentLists

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.

Examples

>>> 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)]}

Methods Summary

all_intersects(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.

all_intersects_all(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.

clear()

coalesce()

Run .coalesce() on all segmentlists.

contract(x)

Run .contract(x) on all segmentlists.

copy([keys])

Return a copy of the segmentlistdict object.

extend(other)

Appends the segmentlists from other to the corresponding segmentlists in self, adding new segmentslists to self as needed.

extent()

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

extent_all()

Return the result of running .extent() on the union of all lists in the dictionary.

extract_common(keys[, n])

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

find(item)

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

fromkeys(iterable[, value])

Create a new dictionary with keys from iterable and values set to value.

get(key[, default])

Return the value for key if key is in the dictionary, else default.

intersection(keys)

Return the intersection of the segmentlists associated with the keys in keys.

intersects(other)

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

intersects_all(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.

intersects_segment(seg)

Returns True if any segmentlist in self intersects the segment, otherwise returns False.

is_coincident(other[, keys])

Return True if any segment in any list in self intersects any segment in any list in other.

items()

keys()

keys_at(x)

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

map(func)

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

pop(key[, default])

If the key is not found, return the default if given; otherwise, raise a KeyError.

popitem(/)

Remove and return a (key, value) pair as a 2-tuple.

protract(x)

Run .protract(x) on all segmentlists.

setdefault(key[, default])

Insert key with a value of default if key is not in the dictionary.

union(keys)

Return the union of the segmentlists associated with the keys in keys.

update([E, ]**F)

If E is present and has a .keys() method, then does: for k in E: D[k] = E[k] If E is present and lacks a .keys() method, then does: for k, v in E: D[k] = v In either case, this is followed by: for k in F: D[k] = F[k]

values()

vote(keys, n)

Return the intervals when n or more of the segment lists identified by keys are on.

Methods Documentation

all_intersects(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.

See also:

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

all_intersects_all(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.

See also:

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

clear() None.  Remove all items from D.
coalesce()

Run .coalesce() on all segmentlists.

contract(x)

Run .contract(x) on all segmentlists.

copy(keys=None)

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.

extend(other)

Appends the segmentlists from other to the corresponding segmentlists in self, adding new segmentslists to self as needed.

extent()

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

extent_all()

Return the result of running .extent() on the union of all lists in the dictionary.

extract_common(keys, n=None)

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. If n is not None then instead of their mutual intersection, compute the intervals in which n or more of the segmentlists named in keys intersect. n = len(keys) is equivalent to n = None. If keys contains repeated entries, those segmentlists are still only considered once for the purpose of counting n.

find(item)

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)])
>>> assert x.find(7) == {'H2': 0, 'H1': 0}

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

fromkeys(iterable, value=None, /)

Create a new dictionary with keys from iterable and values set to value.

get(key, default=None, /)

Return the value for key if key is in the dictionary, else default.

intersection(keys)

Return the intersection of the segmentlists associated with the keys in keys.

See also .vote(). This method is equivalent to .vote(keys, len(keys)) with keys uniquified.

intersects(other)

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(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.

See also:

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

intersects_segment(seg)

Returns True if any segmentlist in self intersects the segment, otherwise returns False.

is_coincident(other, keys=None)

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.

items() a set-like object providing a view on D's items
keys() a set-like object providing a view on D's keys
keys_at(x)

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(func)

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)])
>>> assert x.map(lambda l: 12 in l) == {'H2': True, 'H1': False}
pop(key, default=<unrepresentable>, /)

If the key is not found, return the default if given; otherwise, raise a KeyError.

popitem(/)

Remove and return a (key, value) pair as a 2-tuple.

Pairs are returned in LIFO (last-in, first-out) order. Raises KeyError if the dict is empty.

protract(x)

Run .protract(x) on all segmentlists.

setdefault(key, default=None, /)

Insert key with a value of default if key is not in the dictionary.

Return the value for key if key is in the dictionary, else default.

union(keys)

Return the union of the segmentlists associated with the keys in keys.

update([E, ]**F) None.  Update D from dict/iterable E and F.

If E is present and has a .keys() method, then does: for k in E: D[k] = E[k] If E is present and lacks a .keys() method, then does: for k, v in E: D[k] = v In either case, this is followed by: for k in F: D[k] = F[k]

values() an object providing a view on D's values
vote(keys, n)

Return the intervals when n or more of the segment lists identified by keys are on. Each segment list casts as many votes as the number of times it appears in keys.

See also .intersection().