Segmentsο
Stability: Stable
Classes and utilities for managing time segments (data quality flags, active/inactive intervals).
|
A representation of a named set of segments. |
|
An dict of (key, DataQualityFlag) pairs. |
|
A tuple defining a semi-open interval |
|
A list of Segments <Segment>. |
- class gwexpy.segments.DataQualityDict[source]
Bases:
dictAn dict of (key, DataQualityFlag) pairs.
- read = <gwpy.segments.connect.DataQualityDictRead object>
- write = <gwpy.segments.connect.DataQualityDictWrite object>
- classmethod query_dqsegdb(flags: list[str], *args: SupportsToGps | Segment | SegmentList, host: str | None = 'https://segments.ligo.org', on_error: str = 'raise', parallel: int = 10, **kwargs) Self[source]
Query the advanced LIGO DQSegDB for a list of flags.
- Parameters:
flags (iterable) β A list of flag names for which to query.
args β Either, two float-like numbers indicating the GPS [start, stop) interval, or a SegmentList defining a number of summary segments.
host (str, optional) β Name or URL of the DQSegDB instance to talk to. Defaults to
dqsegdb2.utils.get_default_host().on_error (str, optional) β
how to handle an error querying for one flag, one of
βraiseβ (default): raise the Exception
βwarnβ: print a warning
βignoreβ: move onto the next flag as if nothing happened
parallel (int, optional) β Maximum number of threads to use for parallel connections to the DQSegDB host.
kwargs β All other keyword arguments are passed to
dqsegdb2.query.query_segments().
- Returns:
flagdict β An ordered DataQualityDict of (name, DataQualityFlag) pairs.
- Return type:
DataQualityDict
Examples
The GPS interval(s) of interest can be passed as two arguments specifing the start and end of a single interval:
>>> DataQualityDict.query_dqsegdb(["X1:OBSERVING:1", "Y1:OBSERVING:1"], start, end)
Or, as a single Segment:
>>> DataQualityDict.query_dqsegdb(["X1:OBSERVING:1", "Y1:OBSERVING:1"], interval)
Or, as a SegmentList specifying multiple intervals.
>>> DataQualityDict.query_dqsegdb(["X1:OBSERVING:1", "Y1:OBSERVING:1"], intervals)
- classmethod query(flags: list[str], *args: SupportsToGps | Segment | SegmentList, host: str | None = 'https://segments.ligo.org', on_error: str = 'raise', parallel: int = 10, **kwargs) Self
Query the advanced LIGO DQSegDB for a list of flags.
- Parameters:
flags (iterable) β A list of flag names for which to query.
args β Either, two float-like numbers indicating the GPS [start, stop) interval, or a SegmentList defining a number of summary segments.
host (str, optional) β Name or URL of the DQSegDB instance to talk to. Defaults to
dqsegdb2.utils.get_default_host().on_error (str, optional) β
how to handle an error querying for one flag, one of
βraiseβ (default): raise the Exception
βwarnβ: print a warning
βignoreβ: move onto the next flag as if nothing happened
parallel (int, optional) β Maximum number of threads to use for parallel connections to the DQSegDB host.
kwargs β All other keyword arguments are passed to
dqsegdb2.query.query_segments().
- Returns:
flagdict β An ordered DataQualityDict of (name, DataQualityFlag) pairs.
- Return type:
DataQualityDict
Examples
The GPS interval(s) of interest can be passed as two arguments specifing the start and end of a single interval:
>>> DataQualityDict.query_dqsegdb(["X1:OBSERVING:1", "Y1:OBSERVING:1"], start, end)
Or, as a single Segment:
>>> DataQualityDict.query_dqsegdb(["X1:OBSERVING:1", "Y1:OBSERVING:1"], interval)
Or, as a SegmentList specifying multiple intervals.
>>> DataQualityDict.query_dqsegdb(["X1:OBSERVING:1", "Y1:OBSERVING:1"], intervals)
- classmethod from_veto_definer_file(source: str, start: SupportsToGps | None = None, end: SupportsToGps | None = None, ifo: str | None = None, **read_kw) Self[source]
Read a DataQualityDict from a LIGO_LW XML VetoDefinerTable.
- Parameters:
source (str, Path, file) β Path or URL of veto definer file to read, or open file.
start (~gwpy.time.LIGOTimeGPS, float, str) β GPS start time of required data, any input parseable by ~gwpy.time.to_gps is fine
end (~gwpy.time.LIGOTimeGPS, float, str) β GPS end time of required data, any input parseable by ~gwpy.time.to_gps is fine
ifo (str, optional) β Interferometer prefix whose flags you want to read. Default is None (read all flags).
read_kw β Other keyword arguments are passed to ~astropy.table.Table.read when reading the veto definer file.
- Returns:
flags β A DataQualityDict of flags parsed from the veto_def_table of the input file.
- Return type:
DataQualityDict
Notes
This method does not automatically ~DataQualityDict.populate the active segment list of any flags, a separate call should be made for that as follows
>>> flags = DataQualityDict.from_veto_definer_file('/path/to/file.xml') >>> flags.populate()
- classmethod from_ligolw_tables(segmentdeftable: igwn_ligolw.lsctables.SegmentDefTable, segmentsumtable: igwn_ligolw.lsctables.SegmentSumTable, segmenttable: igwn_ligolw.lsctables.SegmentTable, names: list[str] | None = None, gpstype: type[SupportsFloat] | Callable[[float], SupportsFloat] = <class 'lal.LIGOTimeGPS'>, on_missing: str = 'error') Self[source]
Build a DataQualityDict from a set of LIGO_LW segment tables.
- Parameters:
segmentdeftable (
SegmentDefTable) β Thesegment_definertable to read.segmentsumtable (
SegmentSumTable) β Thesegment_summarytable to read.segmenttable (
SegmentTable) β Thesegmenttable to read.names (list of str, optional) β A list of flag names to read. Default is to read all names.
gpstype (type, callable, optional) β Class to use for GPS times in returned objects, can be a function to convert GPS time to something else. Default is ~gwpy.time.LIGOTimeGPS.
on_missing (str, optional) β
Action to take when a one or more
namesare not found in thesegment_definertable, one of'ignore': do nothing'warn': print a warningerror': raise a ValueError
- Returns:
dqdict β A dict of DataQualityFlag objects populated from the LIGO_LW tables.
- Return type:
DataQualityDict
- to_ligolw_tables(**attrs) tuple[igwn_ligolw.lsctables.SegmentDefTable, igwn_ligolw.lsctables.SegmentSumTable, igwn_ligolw.lsctables.SegmentTable][source]
Convert this DataQualityDict into a trio of LIGO_LW segment tables.
- Parameters:
attrs β Other attributes to add to all rows in all tables (e.g.
'process_id').- Returns:
segmentdeftable (
SegmentDefTable) β Thesegment_definertable.segmentsumtable (
SegmentSumTable) β Thesegment_summarytable.segmenttable (
SegmentTable) β Thesegmenttable.
- coalesce() Self[source]
Coalesce all segments lists in this DataQualityDict.
This method modifies this object in-place.
- Returns:
A view of this flag, not a copy.
- Return type:
self
- populate(source: str | None = 'https://segments.ligo.org', segments: SegmentList | None = None, *, pad: bool = True, on_error: str = 'raise', **kwargs) Self[source]
Query the segment database for each flagβs active segments.
This method assumes all of the metadata for each flag have been filled. Minimally, the following attributes must be filled
nameThe name associated with this flag.
knownThe segments during which this flag was known.
Segments will be fetched from the database, with any
paddingadded on-the-fly.Entries in this dict will be modified in-place.
- Parameters:
source (str) β Source of segments for this flag. This must be either a URL for a segment database or a path to a file on disk.
segments (SegmentList, optional) β A list of known segments during which to query, if not given, existing known segments for flags will be used.
pad (bool, optional, default: True) β Apply the ~DataQualityFlag.padding associated with each flag, default: True.
on_error (str) β
How to handle an error querying for one flag, one of
βraiseβ (default): raise the Exception
βwarnβ: print a warning
βignoreβ: move onto the next flag as if nothing happened
kwargs β Any other keyword arguments to be passed to
DataQualityFlag.query()orDataQualityFlag.read().
- Returns:
self β A reference to the modified DataQualityDict
- Return type:
DataQualityDict
- copy(*, deep: bool = False) Self[source]
Build a copy of this dictionary.
- Parameters:
deep (bool, optional, default: False) β perform a deep copy of the original dictionary with a fresh memory address
- Returns:
flag2 β a copy of the original dictionary
- Return type:
DataQualityFlag
- union() DataQualityFlag[source]
Return the union of all flags in this dict.
- Returns:
union β a new DataQualityFlag whoβs active and known segments are the union of those of the values of this dict
- Return type:
DataQualityFlag
- intersection() DataQualityFlag[source]
Return the intersection of all flags in this dict.
- Returns:
intersection β a new DataQualityFlag whoβs active and known segments are the intersection of those of the values of this dict
- Return type:
DataQualityFlag
- plot(label: str = 'key', **kwargs) Plot[source]
Plot this dict on a segments projection.
- Parameters:
label (str, optional) β
Labelling system to use, or fixed label for all flags, special values include
'key': use the key of the DataQualityDict,'name': use thenameof the flag
If anything else, that fixed label will be used for all lines.
kwargs β All keyword arguments are passed to the
Plotconstructor.
- Returns:
figure β the newly created figure, with populated Axes.
- Return type:
~matplotlib.figure.Figure
See also
matplotlib.pyplot.figureFor documentation of keyword arguments used to create the figure.
matplotlib.figure.Figure.add_subplotFor documentation of keyword arguments used to create the axes.
gwpy.plot.SegmentAxes.plot_segmentlistFor documentation of keyword arguments used in rendering the data.
- class gwexpy.segments.DataQualityFlag(name: str | None = None, active: SegmentListLike | None = None, known: SegmentListLike | None = None, *, label: str | None = None, category: int | None = None, description: str | None = None, isgood: bool = True, padding: tuple[float | None, float | None] | None = None)[source]
Bases:
objectA representation of a named set of segments.
- Parameters:
name (str, optional) β The name of this flag. This should be of the form {ifo}:{tag}:{version}, e.g. βH1:DMT-SCIENCE:1β. Use label for human-readable names.
active (SegmentList, optional) β A list of active segments for this flag
known (SegmentList, optional) β A list of known segments for this flag
label (str, optional) β Human-readable name for this flag, e.g.
'Science-mode'category (int, optional) β Veto category for this flag.
description (str, optional) β Human-readable description of why this flag was created.
isgood (bool, optional) β Do active segments mean the IFO was in a good state?
- property name: str | None
The name associated with this flag.
This normally takes the form {ifo}:{tag}:{version}. If found, each component is stored separately the associated attributes.
- Type:
str
- property ifo: str | None
The interferometer associated with this flag.
This should be a single uppercase letter and a single number, e.g.
'H1'.
- property tag: str | None
The tag (name) associated with this flag.
This should take the form
'AAA-BBB_CCC_DDD', i.e. where each component is an uppercase acronym of alphanumeric characters only, e.g.'DCH-IMC_BAD_CALIBRATION'or'DMT-SCIENCE'.
- property version: int | None
The version number of this flag.
Each flag in the segment database is stored with a version integer, with each successive version representing a more accurate dataset for its known segments than any previous.
- property active: SegmentList
The set of segments during which this flag was active.
- property known: SegmentList
The segments during which this flag was known.
- property isgood: bool
Whether active segments mean the instrument was in a good state.
- property extent: Segment
The GPS
[start, stop)enclosing segment of this DataQualityFlag.
- property livetime: float
Amount of time this flag was active.
- property regular: bool
True if the active segments are a proper subset of the known.
- read = <gwpy.segments.connect.DataQualityFlagRead object>
- write = <gwpy.segments.connect.DataQualityFlagWrite object>
- classmethod query_dqsegdb(flag: str, *args: SupportsToGps | Segment | SegmentList, host: str | None = 'https://segments.ligo.org', **kwargs) Self[source]
Query a DQSegDB server for a flag.
- Parameters:
flag (str) β The name of the flag for which to query
args β Either, two float-like numbers indicating the GPS [start, stop) interval, or a SegmentList defining a number of summary segments
host (str, optional) β Name or URL of the DQSegDB instance to talk to. Defaults to
dqsegdb2.utils.get_default_host().kwargs β All other keyword arguments are passed to
dqsegdb2.query.query_segments().
- Returns:
flag β A new DataQualityFlag, with the known and active lists filled appropriately.
- Return type:
DataQualityFlag
Examples
The GPS interval(s) of interest can be passed as two arguments specifing the start and end of a single interval:
>>> DataQualityDict.query_dqsegdb(["X1:OBSERVING:1", "Y1:OBSERVING:1"], start, end)
Or, as a single Segment:
>>> DataQualityDict.query_dqsegdb(["X1:OBSERVING:1", "Y1:OBSERVING:1"], interval)
Or, as a SegmentList specifying multiple intervals.
>>> DataQualityDict.query_dqsegdb(["X1:OBSERVING:1", "Y1:OBSERVING:1"], intervals)
- classmethod query(flag: str, *args: SupportsToGps | Segment | SegmentList, host: str | None = 'https://segments.ligo.org', **kwargs) Self
Query a DQSegDB server for a flag.
- Parameters:
flag (str) β The name of the flag for which to query
args β Either, two float-like numbers indicating the GPS [start, stop) interval, or a SegmentList defining a number of summary segments
host (str, optional) β Name or URL of the DQSegDB instance to talk to. Defaults to
dqsegdb2.utils.get_default_host().kwargs β All other keyword arguments are passed to
dqsegdb2.query.query_segments().
- Returns:
flag β A new DataQualityFlag, with the known and active lists filled appropriately.
- Return type:
DataQualityFlag
Examples
The GPS interval(s) of interest can be passed as two arguments specifing the start and end of a single interval:
>>> DataQualityDict.query_dqsegdb(["X1:OBSERVING:1", "Y1:OBSERVING:1"], start, end)
Or, as a single Segment:
>>> DataQualityDict.query_dqsegdb(["X1:OBSERVING:1", "Y1:OBSERVING:1"], interval)
Or, as a SegmentList specifying multiple intervals.
>>> DataQualityDict.query_dqsegdb(["X1:OBSERVING:1", "Y1:OBSERVING:1"], intervals)
- classmethod fetch_open_data(flag: str, start: SupportsToGps, end: SupportsToGps, **kwargs) Self[source]
Fetch Open Data timeline segments into a flag.
- flagstr
The name of the flag to query
- start~gwpy.time.LIGOTimeGPS, float, str
GPS start time of required data, any input parseable by ~gwpy.time.to_gps is fine
- end~gwpy.time.LIGOTimeGPS, float, str
GPS end time of required data, any input parseable by ~gwpy.time.to_gps is fine
- timeoutint, optional
Timeout for download (seconds).
- hoststr, optional
URL of GWOSC host, default:
'https://gwosc.org'.
- Returns:
flag β a new flag with active segments filled from Open Data
- Return type:
DataQualityFlag
Examples
>>> from gwpy.segments import DataQualityFlag >>> print(DataQualityFlag.fetch_open_data( ... "H1_DATA", ... "Sep 14 2015", ... "Sep 15 2015", ... )) <DataQualityFlag('H1:DATA', known=[[1126224017 ... 1126310417)] active=[[1126251604 ... 1126252133) [1126252291 ... 1126274322) [1126276234 ... 1126281754) ... [1126308670 ... 1126309577) [1126309637 ... 1126309817) [1126309877 ... 1126310417)] description=None)>
- classmethod from_veto_def(veto: igwn_ligolw.lsctables.VetoDef | astropy.table.Row) Self[source]
Define a DataQualityFlag from a VetoDef.
- Parameters:
veto (~igwn_ligolw.lsctables.VetoDef, ~astropy.table.Row.) β Veto definition to convert from.
- populate(source: str | None = 'https://segments.ligo.org', segments: SegmentList | None = None, *, pad: bool = True, **kwargs) Self[source]
Query the segment database for this flagβs active segments.
This method assumes all of the metadata for each flag have been filled. Minimally, the following attributes must be filled
nameThe name associated with this flag.
knownThe segments during which this flag was known.
Segments will be fetched from the database, with any
paddingadded on-the-fly.This DataQualityFlag will be modified in-place.
- Parameters:
source (str) β Source of segments for this flag. This must be either a URL for a segment database or a path to a file on disk.
segments (SegmentList, optional) β A list of segments during which to query, if not given, existing known segments for this flag will be used.
pad (bool, optional, default: True) β apply the ~DataQualityFlag.padding associated with this flag, default: True.
**kwargs β any other keyword arguments to be passed to
DataQualityFlag.query()orDataQualityFlag.read().
- Returns:
self β a reference to this flag
- Return type:
DataQualityFlag
- contract(x: float) SegmentList[source]
Contract each of the
activesegments byxseconds.This method adds
xto each segmentβs lower bound, and subtractsxfrom the upper bound.The
activeSegmentList is modified in place.- Parameters:
x (float) β Number of seconds by which to contract each Segment.
- protract(x: float) SegmentList[source]
Protract each of the
activesegments byxseconds.This method subtracts
xfrom each segmentβs lower bound, and addsxto the upper bound, while maintaining that each Segment stays within the known bounds.The
activeSegmentList is modified in place.- Parameters:
x (float) β Number of seconds by which to protact each Segment.
- pad(*args: float, inplace: bool = False) Self[source]
Apply a padding to each segment in this DataQualityFlag.
This method either takes no arguments, in which case the value of the
paddingattribute will be used, or two values representing the padding for the start and end of each segment.For both the start and end paddings, a positive value means pad forward in time, so that a positive start pad or negative end padding will contract a segment at one or both ends, and vice-versa.
This method will apply the same padding to both the ~DataQualityFlag.known and ~DataQualityFlag.active lists, but will not
coalesce()the result.- Parameters:
args (float, optional) β Two floats giving the start and end padding to apply. If not given, self.padding will be used.
inplace (bool, optional) β Modify this object in-place. Default is False, i.e. return a copy of the original object with padded segments.
- Returns:
paddedflag β A view of the modified flag.
- Return type:
DataQualityFlag
- round(*, contract: bool = False) Self[source]
Round this flag to integer segments.
- Parameters:
contract (bool, optional) β If False (default) expand each segment to the containing integer boundaries, otherwise contract each segment to the contained boundaries.
- Returns:
roundedflag β A copy of the original flag with the active and known segments padded out to integer boundaries..
- Return type:
DataQualityFlag
- coalesce() Self[source]
Coalesce the segments for this flag.
This method does two things:
coalesces <SegmentList.coalesce> the ~DataQualityFlag.known and ~DataQualityFlag.active segment lists
forces the active segments to be a proper subset of the known segments
Note
This operation is performed in-place.
- Returns:
A view of this flag, not a copy.
- Return type:
self
- copy() Self[source]
Build an exact copy of this flag.
- Returns:
flag β A deepcopy of the original flag.
- Return type:
DataQualityFlag
- plot(figsize: tuple[float, float] = (12, 4), xscale: str = 'auto-gps', **kwargs) Plot[source]
Plot this flag on a segments projection.
- Parameters:
figsize (tuple of float) β The size (width, height) of the figure to create.
xscale (str) β The scaling to use for the X-axis (time axis). Default is
"auto-gps"to dynamically choose the right scaling based on how much time is covered by the visible span.kwargs β Other keyword arguments are passed to the
Plotconstructor.
- Returns:
figure β The newly created figure, with populated Axes.
- Return type:
~matplotlib.figure.Figure
See also
matplotlib.pyplot.figureFor documentation of keyword arguments used to create the figure.
matplotlib.figure.Figure.add_subplotFor documentation of keyword arguments used to create the axes
gwpy.plot.SegmentAxes.plot_segmentlistFor documentation of keyword arguments used in rendering the data.
- class gwexpy.segments.Segment(iterable=(), /)[source]
Bases:
segment,Generic[T]A tuple defining a semi-open interval
[start, end).Each Segment represents the range of values in a given interval, with general arithmetic supported for combining/comparing overlapping segments.
- Parameters:
start (float) β The start value of this Segment.
end (float) β The end value of this Segment.
Examples
>>> Segment(0, 10) & Segment(5, 15) Segment(5, 10) >>> Segment(0, 10) | Segment(5, 15) Segment(0, 15) >>> Segment(0, 10) - Segment(5, 15) Segment(0, 5) >>> Segment(0, 10) < Segment(5, 15) True >>> Segment(1, 2) in Segment(0, 10) True >>> Segment(1, 11) in Segment(0, 10) False >>> Segment(0, 1) Segment(0, 1) >>> Segment(1, 0) Segment(0, 1) >>> bool(Segment(0, 1)) True
- property start: T
The beginning of this segment.
- property end: T
The end of this segment.
- class gwexpy.segments.SegmentList(iterable=(), /)[source]
Bases:
segmentlistA list of Segments <Segment>.
The SegmentList provides additional methods that assist in the manipulation of lists of Segments <Segment>. In particular, arithmetic operations such as union and intersection are provided. Unlike the Segment, the SegmentList is 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 SegmentList to be in what is refered to as a βcoalescedβ state - consisting solely of disjoint Segments <Segment> listed in ascending order. Using the standard Python sequence-like operations, a SegmentList can be easily constructed that is not in this state; for example by simply appending a Segment to the end of the list that overlaps some other Segment already in the list. The class provides a
coalesce()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 a SegmentList exclusively 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)]
- extent()
Return the segment whose end-points denote the maximum and minimum extent of the segmentlist. Does not require the segmentlist to be coalesced.
- 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).
- to_table() Table[source]
Convert this SegmentList to a ~astropy.table.Table.
The resulting Table has four columns: index, start, end, and duration, 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 SegmentList objects in comma-separated value (CSV) format, via the
write()method.
- read = <gwpy.segments.connect.SegmentListRead object>
- write = <gwpy.segments.connect.SegmentListWrite object>
- class gwexpy.segments.SegmentListDict(*args)[source]
Bases:
segmentlistdictA dict of SegmentLists <SegmentList>.
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 <Segment> 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)]}