SegmentTable: 基本#
Tip
可視化、GravitySpy 連携、高度な操作を含むより包括的なガイドは、Table / Segment ユーザーガイド を参照してください。
GWpy の基本クラスと gwexpy の拡張#
セグメント解析は GWpy の型を土台にしています。各行の区間は引き続き gwpy.segments.Segment で表され、ペイロード列は GWpy の TimeSeries や FrequencySeries オブジェクトを直接保持できます。
その上で gwexpy は、gwpy.table.Table の拡張として SegmentTable を追加し、SegmentCell による遅延ペイロード処理や、apply()・map()・crop()・asd() といったテーブル指向のバッチヘルパーを提供します。実際には、データオブジェクト自体には GWpy の基本クラスをそのまま使い、多数のセグメントをまとめて処理するワークフロー層を gwexpy が追加する、という形になります。
import warnings
import warnings
with warnings.catch_warnings():
import numpy as np
np.random.seed(42)
from gwpy.segments import Segment
from gwexpy.table import SegmentTable
# 1. Create simple segments
segs = [Segment(0, 4), Segment(4, 8), Segment(8, 12)]
st = SegmentTable.from_segments(segs, label=["A", "B", "C"])
st
/home/runner/micromamba/envs/gwexpy/lib/python3.11/site-packages/gwpy/time/_ligotimegps.py:42: UserWarning: Wswiglal-redir-stdio:
SWIGLAL standard output/error redirection is enabled in IPython.
This may lead to performance penalties. To disable locally, use:
with lal.no_swig_redirect_standard_output_error():
...
To disable globally, use:
lal.swig_redirect_standard_output_error(False)
Note however that this will likely lead to error messages from
LAL functions being either misdirected or lost when called from
Jupyter notebooks.
To suppress this warning, use:
import warnings
warnings.filterwarnings("ignore", "Wswiglal-redir-stdio")
import lal
from lal import LIGOTimeGPS
SegmentCell による遅延ロード#
必要になるまでデータをロードしない「ペイロード列」を追加できます。これは巨大なデータのバッチ処理に非常に有効です。
def my_loader():
# Simulate loading data
print("Loading series...")
from gwpy.timeseries import TimeSeries
return TimeSeries(np.random.randn(128), sample_rate=32)
# Add a payload column with a loader (sequence of callables)
st.add_series_column("raw", loader=[my_loader]*len(st), kind="timeseries")
st
| span | label | raw | |
|---|---|---|---|
| 0 | (0, 4) | A | <lazy: timeseries> |
| 1 | (4, 8) | B | <lazy: timeseries> |
| 2 | (8, 12) | C | <lazy: timeseries> |
行単位の処理#
SegmentTable は apply() メソッドを提供し、各行を処理して新しい列として統合できます。
def process_row(row):
span = row["span"]
return {"duration": float(span[1] - span[0]), "valid": True}
st2 = st.apply(process_row)
st2.display()
| span | label | duration | valid | raw | |
|---|---|---|---|---|---|
| 0 | (0, 4) | A | 4.0 | True | <lazy: timeseries> |
| 1 | (4, 8) | B | 4.0 | True | <lazy: timeseries> |
| 2 | (8, 12) | C | 4.0 | True | <lazy: timeseries> |
明示的ロードとデータ変換#
fetch() や materialize() を使ってデータを明示的にロードできます。to_pandas() を使うと、通常の pandas DataFrame として扱えます。
st2.fetch()
df = st2.to_pandas()
df.head()
Loading series...
Loading series...
Loading series...
| span | label | duration | valid | |
|---|---|---|---|---|
| 0 | (0, 4) | A | 4.0 | True |
| 1 | (4, 8) | B | 4.0 | True |
| 2 | (8, 12) | C | 4.0 | True |