SegmentList#
- class gwpy.segments.SegmentList(iterable=(), /)[source]#
Bases:
segmentlistThe
SegmentListprovides additional methods that assist in the manipulation of lists ofSegments. In particular, arithmetic operations such as union and intersection are provided. Unlike theSegment, theSegmentListis closed under all supported arithmetic operations.All standard Python sequence-like operations are supported, like slicing, iteration and so on, but the arithmetic and other methods in this class generally expect the
SegmentListto be in what is refered to as a “coalesced” state - consisting solely of disjointSegmentslisted in ascending order. Using the standard Python sequence-like operations, aSegmentListcan be easily constructed that is not in this state; for example by simply appending aSegmentto the end of the list that overlaps some otherSegmentalready in the list. The class provides acoalesce()method that can be called to put it in the coalesced state. Following application of the coalesce method, all arithmetic operations will function reliably. All arithmetic methods themselves return coalesced results, so there is never a need to call the coalesce method when manipulating aSegmentListexclusively via the arithmetic operators.Examples
>>> x = SegmentList([Segment(-10, 10)]) >>> x |= SegmentList([Segment(20, 30)]) >>> x -= SegmentList([Segment(-5, 5)]) >>> print(x) [Segment(-10, -5), Segment(5, 10), Segment(20, 30)] >>> print(~x) [Segment(-infinity, -10), Segment(-5, 5), Segment(10, 20), Segment(30, infinity)]
Attributes Summary
Read segments from file into a
SegmentList.Write this
SegmentListto a file.Methods Summary
coalesce()Sort the elements of the list into ascending order, and merge continuous segments into single segments.
extent()Return the segment whose end-points denote the maximum and minimum extent of the segmentlist.
to_table()Convert this
SegmentListto aTable.Attributes Documentation
- read#
Read segments from file into a
SegmentList.- Parameters:
- filename
str Path of file to read.
- format
str, optional Source format identifier. If not given, the format will be detected if possible. See below for list of acceptable formats.
- coalesce
bool, optional If
True, coalesce the segment list before returning, otherwise return exactly as contained in file(s).- kwargs
Other keyword arguments depend on the format, see
SegmentList.read.help(format=<format>)for details, or see the online documentation (Reading/writing segments and flags).
- filename
- Returns:
- segmentlist
SegmentList SegmentListactive and known segments read from file.
- segmentlist
- Raises:
IndexErrorif
sourceis an empty list
- write#
Write this
SegmentListto a file.Arguments and keywords depend on the output format, see the online documentation for full details for each format.
- Parameters:
- target
str Output filename.
- target
Methods Documentation
- coalesce() Self[source]#
Sort the elements of the list into ascending order, and merge continuous segments into single segments. Segmentlist is modified in place. This operation is O(n log n).
- extent()#
Return the segment whose end-points denote the maximum and minimum extent of the segmentlist. Does not require the segmentlist to be coalesced.
- to_table() Table[source]#
Convert this
SegmentListto aTable.The resulting
Tablehas four columns:index,start,end, andduration, corresponding to the zero-counted list index, GPS start and end times, and total duration in seconds, respectively.This method exists mainly to provide a way to write
SegmentListobjects in comma-separated value (CSV) format, via thewrite()method.