Fix a typo & add option to be faster

This commit is contained in:
linarphy 2025-07-11 19:54:02 +02:00
parent 7a645f6572
commit a3cc8ce1d5
Signed by: linarphy
GPG key ID: 0CBF02E039B4FFFF
4 changed files with 119 additions and 57 deletions

View file

@ -17,6 +17,7 @@ class ComponentFactory:
start_time: Time, start_time: Time,
duration: Quantity[time], duration: Quantity[time],
metadata: MetadataComponent, metadata: MetadataComponent,
force_virgotools: bool = False,
) -> Component: ) -> Component:
""" """
Fetch a component from online data Fetch a component from online data
@ -31,11 +32,18 @@ class ComponentFactory:
Duration Duration
metadata : MetadataComponent metadata : MetadataComponent
Component's metadata Component's metadata
force_virgotools : bool
Force the use of the virgotools library in place of gwpy
first to be faster. Default to False
Returns
-------
Component
""" """
sensor = download_timeseries( sensor = download_timeseries(
channel="V1:Sc_{}_MIR_Z".format(metadata.name), channel="V1:Sc_{}_MIR_Z".format(metadata.name),
start_time=start_time, start_time=start_time,
duration=duration, duration=duration,
unit=uV, # in um, but the calibration factor is considered as m/V unit=uV, # in um, but the calibration factor is considered as m/V
force_virgotools=force_virgotools,
) )
return Component(metadata=metadata, sensor=sensor) return Component(metadata=metadata, sensor=sensor)

View file

@ -36,6 +36,7 @@ class MeasurementFactory:
start_time: Time, start_time: Time,
duration: Quantity[time], duration: Quantity[time],
initializer_metadata: list[InitializerScatterer], initializer_metadata: list[InitializerScatterer],
force_virgotools: bool = False,
) -> Measurement: ) -> Measurement:
""" """
Fetch a measurement from online data Fetch a measurement from online data
@ -55,6 +56,32 @@ class MeasurementFactory:
initializer_metadata : list[InitializerScatterer] initializer_metadata : list[InitializerScatterer]
List of component's and scatterer's pre-initialized List of component's and scatterer's pre-initialized
metadata that will be used in the created measurement metadata that will be used in the created measurement
force_virgotools : bool
Force the use of the virgotools library in place of gwpy
first to be faster. Default to False
Examples
--------
>>> data = MeasurementFactory.fetch(
... name="a name",
... description="a detailed description",
... start_time=Time(
... "2025-01-01T00:00:00.000000000",
... format="isot",
... scale="utc",
... ),
... duration=10 * s,
... initializer_metadata=[
... InitializerScatterer(
... name="SDB1",
... calibration_factor=1.0 * m / V,
... associated_component=InitializerComponent(
... name="SR",
... calibration_factor=1.0 * m / V,
... ),
... injection=None,
... ),
... ],
... )
Returns Returns
------- -------
Measurement : constructed Measurement from these metadata Measurement : constructed Measurement from these metadata
@ -70,6 +97,7 @@ class MeasurementFactory:
duration=duration, duration=duration,
units=list(channels_map.values()), units=list(channels_map.values()),
frame="trend", frame="trend",
force_virgotools=force_virgotools,
) )
if sum(data["V1:META_ITF_LOCK_index"].diff().abs()) != 0: if sum(data["V1:META_ITF_LOCK_index"].diff().abs()) != 0:
raise Exception( raise Exception(
@ -101,6 +129,7 @@ class MeasurementFactory:
name=initializer_scatterer.associated_component.name, name=initializer_scatterer.associated_component.name,
calibration_factor=initializer_scatterer.associated_component.calibration_factor, calibration_factor=initializer_scatterer.associated_component.calibration_factor,
), ),
force_virgotools=force_virgotools,
) )
components.append(component) components.append(component)
scatterers.append( scatterers.append(
@ -120,6 +149,7 @@ class MeasurementFactory:
start_time=start_time, start_time=start_time,
duration=duration, duration=duration,
unit=None, unit=None,
force_virgotools=force_virgotools,
) )
return Measurement( return Measurement(
metadata=metadata, metadata=metadata,

View file

@ -17,6 +17,7 @@ class ScattererFactory:
start_time: Time, start_time: Time,
duration: Quantity[time], duration: Quantity[time],
metadata: MetadataScatterer, metadata: MetadataScatterer,
force_virgotools: bool = False,
) -> Scatterer: ) -> Scatterer:
""" """
Fetch a component from online data Fetch a component from online data
@ -31,11 +32,18 @@ class ScattererFactory:
Duration Duration
metadata : MetadataComponent metadata : MetadataComponent
Component's metadata Component's metadata
force_virgotools : bool
Force the use of the virgotools library in place of gwpy
first to be faster. Default to False
Returns
-------
Scatterer
""" """
sensor = download_timeseries( sensor = download_timeseries(
channel="V1:{}_LC_Z".format(metadata.name), channel="V1:{}_LC_Z".format(metadata.name),
start_time=start_time, start_time=start_time,
duration=duration, duration=duration,
unit=uV, # in um, but the calibration factor is considered as m/V unit=uV, # in um, but the calibration factor is considered as m/V
force_virgotools=force_virgotools,
) )
return Scatterer(metadata=metadata, sensor=sensor) return Scatterer(metadata=metadata, sensor=sensor)

View file

@ -11,9 +11,9 @@ from gwpy.timeseries import TimeSeries, TimeSeriesDict
try: try:
from virgotools.frame_lib import FrameFile, FrVect # pyright: ignore[reportMissingImports] from virgotools.frame_lib import FrameFile, FrVect # pyright: ignore[reportMissingImports]
virgotool = True virgotools = True
except ImportError: except ImportError:
virgotool = False virgotools = False
pass pass
@ -51,6 +51,7 @@ def download_timeseries(
duration: Quantity[time], duration: Quantity[time],
unit: Unit | None = None, unit: Unit | None = None,
frame: Literal["raw"] | Literal["trend"] = "raw", frame: Literal["raw"] | Literal["trend"] = "raw",
force_virgotools: bool = False,
) -> TimeSeries: ) -> TimeSeries:
""" """
Download a TimeSeries from an online channel Download a TimeSeries from an online channel
@ -66,37 +67,44 @@ def download_timeseries(
Unit of the TimeSeries (default ot None) Unit of the TimeSeries (default ot None)
frame : raw|trend frame : raw|trend
Which frame to take the data from. Default to raw Which frame to take the data from. Default to raw
force_virgotools : bool
Force the use of the virgotools library in place of gwpy first
to be faster. Default to False
Notes Notes
----- -----
It first try to use gwpy, and fallback to virgotools It first try to use gwpy, and fallback to virgotools
""" """
try: if not force_virgotools:
return TimeSeries.get( try:
channel=channel, return TimeSeries.get(
start=to_gps(start_time), channel=channel,
end=to_gps(start_time + duration), start=to_gps(start_time),
) end=to_gps(start_time + duration),
except HTTPError:
if virgotool:
vect: FrVect | None = None # pyright: ignore[reportPossiblyUnboundVariable]
with FrameFile(frame) as ffl: # pyright: ignore[reportPossiblyUnboundVariable]
vect = ffl.getChannel( # pyright: ignore[reportPossiblyUnboundVariable]
channel, to_gps(start_time), duration.to(s)
)
if vect is None: # pyright: ignore[reportPossiblyUnboundVariable]
raise RuntimeError(
"cannot get data vector from online data"
)
return TimeSeries(
data=vect.data, # pyright: ignore[reportPossiblyUnboundVariable]
unit=unit,
t0=start_time,
sample_rate=vect.fsample * Hz, # pyright: ignore[reportPossiblyUnboundVariable]
) )
else: except HTTPError:
pass
if virgotools:
vect: FrVect | None = None # pyright: ignore[reportPossiblyUnboundVariable]
with FrameFile(frame) as ffl: # pyright: ignore[reportPossiblyUnboundVariable]
vect = ffl.getChannel( # pyright: ignore[reportPossiblyUnboundVariable]
channel,
to_gps(start_time),
duration.to(s).value,
)
if vect is None: # pyright: ignore[reportPossiblyUnboundVariable]
raise RuntimeError( raise RuntimeError(
"virgotools not available in this environment, cannot download online Virgo data" "cannot get data vector from online data"
) )
return TimeSeries(
data=vect.data, # pyright: ignore[reportPossiblyUnboundVariable]
unit=unit,
t0=start_time,
sample_rate=vect.fsample * Hz, # pyright: ignore[reportPossiblyUnboundVariable]
)
else:
raise RuntimeError(
"virgotools not available in this environment, cannot download online Virgo data"
)
def download_dicttimeseries( def download_dicttimeseries(
@ -105,6 +113,7 @@ def download_dicttimeseries(
duration: Quantity[time], duration: Quantity[time],
units: list[Unit | None] | None = None, units: list[Unit | None] | None = None,
frame: Literal["raw"] | Literal["trend"] = "raw", frame: Literal["raw"] | Literal["trend"] = "raw",
force_virgotools: bool = False,
) -> TimeSeriesDict: ) -> TimeSeriesDict:
""" """
Download multiple TimeSeries at the same time to be more efficient Download multiple TimeSeries at the same time to be more efficient
@ -121,39 +130,46 @@ def download_dicttimeseries(
channels. Default to None channels. Default to None
frame : raw|trend frame : raw|trend
Which frame to take the data from. Default to raw Which frame to take the data from. Default to raw
force_virgotools : bool
Force the use of the virgotools library in place of gwpy first
to be faster. Default to False
Notes Notes
----- -----
It first try to use gwpy, and fallback to virgotools It first try to use gwpy, and fallback to virgotools
""" """
try: if not force_virgotools:
return TimeSeriesDict.get( try:
channels=channels, return TimeSeriesDict.get(
start=to_gps(start_time), channels=channels,
end=to_gps(start_time + duration), start=to_gps(start_time),
) end=to_gps(start_time + duration),
except HTTPError:
if virgotool:
vects: list[FrVect] = [] # pyright: ignore[reportPossiblyUnboundVariable]
with FrameFile(frame) as ffl: # pyright: ignore[reportPossiblyUnboundVariable]
for channel in channels:
vects.append(
ffl.getChannel(
channel, to_gps(start_time), duration.to(s)
)
)
timeseries_dict: dict[str, TimeSeries] = dict()
for i in range(len(vects)):
unit = None
if units is not None:
unit = units[i]
timeseries_dict[channels[i]] = TimeSeries(
data=vects[i].data,
unit=unit,
t0=start_time,
sample_rate=vects[i].fsample * Hz,
)
return TimeSeriesDict(timeseries_dict)
else:
raise RuntimeError(
"virgotools not available in this environment, cannot download online Virgo data"
) )
except HTTPError:
pass
if virgotools:
vects: list[FrVect] = [] # pyright: ignore[reportPossiblyUnboundVariable]
with FrameFile(frame) as ffl: # pyright: ignore[reportPossiblyUnboundVariable]
for channel in channels:
vects.append(
ffl.getChannel(
channel,
to_gps(start_time),
duration.to(s).value,
)
)
timeseries_dict: dict[str, TimeSeries] = dict()
for i in range(len(vects)):
unit = None
if units is not None:
unit = units[i]
timeseries_dict[channels[i]] = TimeSeries(
data=vects[i].data,
unit=unit,
t0=start_time,
sample_rate=vects[i].fsample * Hz,
)
return TimeSeriesDict(timeseries_dict)
else:
raise RuntimeError(
"virgotools not available in this environment, cannot download online Virgo data"
)