SegmentListDict

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

Bases: ligo.segments.segmentlistdict

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

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.

clear()

coalesce(self)

Run .coalesce() on all segmentlists.

contract(self, x)

Run .contract(x) on all segmentlists.

copy(self[, keys])

Return a copy of the segmentlistdict object.

extend(self, other)

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

extent(self)

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

extent_all(self)

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

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.

find(self, 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(self, key[, default])

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

intersection(self, keys)

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

intersects(self, other)

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

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.

intersects_segment(self, seg)

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

is_coincident(self, other[, keys])

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

items()

keys()

keys_at(self, x)

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

map(self, func)

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

pop()

If key is not found, d is returned if given, otherwise KeyError is raised

popitem()

2-tuple; but raise KeyError if D is empty.

protract(self, x)

Run .protract(x) on all segmentlists.

setdefault(self, key[, default])

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

union(self, keys)

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

update()

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

Methods Documentation

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.

See also:

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

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.

See also:

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

clear()
coalesce(self)

Run .coalesce() on all segmentlists.

contract(self, x)

Run .contract(x) on all segmentlists.

copy(self, 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(self, other)

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

extent(self)

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

extent_all(self)

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

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. The offsets are preserved.

find(self, 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(self, key, default=None, /)

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

intersection(self, keys)

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

intersects(self, 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(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.

See also:

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

intersects_segment(self, seg)

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

is_coincident(self, 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()
keys()
keys_at(self, 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(self, 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()

If key is not found, d is returned if given, otherwise KeyError is raised

popitem()

2-tuple; but raise KeyError if D is empty.

protract(self, x)

Run .protract(x) on all segmentlists.

setdefault(self, 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(self, keys)

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

update()

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