IGWN Segments
IGWN Segments (igwn-segments) provides a robust Pythonic representation of half-open intervals and lists of such intervals. It offers tools for creating, manipulating, and performing set-like operations (union, intersection, subtraction) on time segments, crucial for gravitational-wave data analysis and other scientific applications. Currently at version 2.1.1, the library follows an irregular release cadence, driven by the needs of the IGWN collaboration.
Common errors
-
TypeError: unsupported operand type(s) for +: 'Segment' and 'Segment'
cause Attempting to use Python's `+` operator for segment union, intersection, or other set-like operations.fixUse the dedicated methods provided by `SegmentList` for set-like operations, such as `.union()`, `.intersection()`, or `.subtraction()`. Example: `SegmentList([s1, s2]).union()` -
ValueError: Invalid segment string format: '[[10,20], [30,40]]' (or similar)
cause The string representation passed to `Segment.from_str()` or `SegmentList.from_str()` does not conform to the expected syntax, typically due to incorrect bracket usage or missing commas.fixEnsure the string format strictly adheres to the library's expectations for half-open intervals, typically `[start, end)` for single segments and `[[start1, end1), [start2, end2))` for lists. For example: `SegmentList.from_str('[[10, 20), [30, 40))')`. -
AttributeError: 'Segment' object has no attribute 'union'
cause Attempting to call a list-oriented method like `union()` directly on a single `Segment` object instead of a `SegmentList` object.fixEnsure that methods for combining or manipulating multiple segments are called on a `SegmentList`. If you have a single segment and want to combine it with others, first wrap it in a `SegmentList`: `SegmentList([my_segment]).union(other_list)`.
Warnings
- breaking As of v2.0.0, `Segment` and `SegmentList` objects strictly represent half-open intervals `[start, end)`. The `segments.Segment.is_right_open` parameter has been removed, and intervals are always right-open.
- gotcha Individual `Segment` objects are immutable. Operations like `union()`, `intersection()`, or `subtraction()` on a `SegmentList` return a *new* `SegmentList` object. Attempting to modify a `Segment` in place will fail.
- gotcha Direct arithmetic operators (`+`, `-`, `*`, `/`) are not defined for `Segment` objects or for performing set-like operations on `SegmentList` objects. Instead, use explicit methods like `.union()`, `.intersection()`, `.subtraction()`.
Install
-
pip install igwn-segments
Imports
- Segment
from segments import Segment
- SegmentList
from segments import SegmentList
Quickstart
from segments import Segment, SegmentList
# Create individual segments
s1 = Segment(100.0, 200.0)
s2 = Segment(150.0, 250.0)
s3 = Segment(300.0, 350.0)
print(f"Segment 1: {s1}")
print(f"Segment 2: {s2}")
print(f"Duration of s1: {s1.duration}")
print(f"s1 intersects s2: {s1.intersects(s2)}")
# Create a list of segments
sl = SegmentList([s1, s2, s3])
print(f"Initial SegmentList: {sl}")
# Perform union operation
union_sl = sl.union()
print(f"Union of SegmentList: {union_sl}")
# Add a segment to a SegmentList
sl_plus_s4 = sl.union(Segment(0, 50))
print(f"SegmentList with an added segment: {sl_plus_s4}")
# Create from string representation
sl_from_str = SegmentList.from_str('[[10, 20), [30, 40))')
print(f"SegmentList from string: {sl_from_str}")