Skip to content

sgnl.sinks.gracedb_sink

GraceDBSink dataclass

Bases: HTTPControlSinkElement

A sink that will turn the lowest FAR event into a gracedb upload, if it passes the threshold

Source code in sgnl/sinks/gracedb_sink.py
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
@dataclass
class GraceDBSink(HTTPControlSinkElement):
    """A sink that will turn the lowest FAR event into a gracedb upload, if it passes
    the threshold
    """

    far_thresh: float = -1
    output_kafka_server: str = None
    gracedb_service_url: str = None
    gracedb_group: str = "Test"
    gracedb_pipeline: str = "SGNL"
    gracedb_search: str = "MOCK"
    gracedb_label: list[str] = None
    template_sngls: list = None
    analysis_tag: str = "mockery"
    job_tag: str = None
    analysis_ifos: list = None
    process_params: dict = None
    event_pad: str = None
    spectrum_pads: list[str] = None
    delta_t: float = 0.005
    strike_object: StrikeObject = None
    channel_dict: dict[str, str] = None

    def __post_init__(self):
        self.sink_pad_names = (self.event_pad,) + self.spectrum_pads
        super().__post_init__()
        self.events = None
        self.psds = {}
        self.state = {"far-threshold": self.far_thresh}
        self.sngls_dict = {}
        for sub in self.template_sngls:
            for tid, sngl in sub.items():
                self.sngls_dict[tid] = sngl

        assert not (
            self.output_kafka_server is not None
            and self.gracedb_service_url is not None
        )

        self.start_time = time.time()

        # set up kafka producer or a gracedb client
        if self.output_kafka_server is not None:
            print("GraceDBSink: sending events to kafka", flush=True, file=sys.stderr)
            self.client = Producer(
                {
                    "bootstrap.servers": self.output_kafka_server,
                    "message.max.bytes": 20971520,
                }
            )
        elif self.gracedb_service_url is not None:
            self.client = GraceDb(self.gracedb_service_url)
        else:
            self.client = None

    def pull(self, pad, frame):
        if frame.EOS:
            self.mark_eos(pad)

        if self.rsnks[pad] == self.event_pad:
            self.events = frame
        else:
            if frame.metadata["psd"] is not None:
                self.psds[self.rsnks[pad]] = frame.metadata["psd"]

    def internal(self):
        HTTPControlSinkElement.exchange_state(self.name, self.state)

        # send event data to kafka or gracedb
        if self.client and self.state["far-threshold"] >= 0:

            event, trigs, snr_ts = min_far_event(
                self.events["event"].data,
                self.events["trigger"].data,
                self.events["snr_ts"].data,
                self.state["far-threshold"],
            )
            if event is not None:
                xmldoc = event_trigs_to_coinc_xmldoc(
                    event,
                    trigs,
                    snr_ts,
                    self.sngls_dict,
                    self.analysis_ifos,
                    self.process_params,
                    self.delta_t,
                    self.channel_dict,
                )

                # add psd frequeny series
                lal.series.make_psd_xmldoc(self.psds, xmldoc.childNodes[-1])

                if self.output_kafka_server:
                    publish_kafka(
                        self.client,
                        xmldoc,
                        self.job_tag,
                        self.analysis_tag,
                        "".join(sorted(self.analysis_ifos)),
                        self.gracedb_group,
                        self.gracedb_search,
                        self.strike_object,
                    )
                else:
                    publish_gracedb(
                        self.client,
                        xmldoc,
                        self.gracedb_group,
                        self.gracedb_pipeline,
                        self.gracedb_search,
                        self.gracedb_label,
                    )

                self.start_time = time.time()

        if self.at_eos and self.output_kafka_server is not None:
            print("shutdown: flush kafka client", flush=True, file=sys.stderr)
            self.client.flush()

add_table_with_n_rows(xmldoc, tblcls, n)

Given an xmldoc tree and a table class, add the table with n rows initialized to 0 for numeric types and "" for string types

Source code in sgnl/sinks/gracedb_sink.py
294
295
296
297
298
299
300
301
302
303
304
305
def add_table_with_n_rows(xmldoc, tblcls, n):
    """Given an xmldoc tree and a table class, add the table with n rows
    initialized to 0 for numeric types and "" for string types"""
    table = lsctables.New(tblcls)
    for _ in range(n):
        row = table.RowType()
        for col, _type in table.validcolumns.items():
            col = col.split(":")[-1]
            setattr(row, col, "" if _type == "lstring" else 0)
        table.append(row)
    xmldoc.childNodes[-1].appendChild(table)
    return table

col_map(row, datadict, mapdict, funcdict)

A function to map between SGN columns and ligolw columns. datadict has what the data you want to insert into the ligolw "row". mapdict allows you to map between different names (if needed) or exclude data by setting the value to None. funcdict lets you call simple functions to e.g., convert the type

Source code in sgnl/sinks/gracedb_sink.py
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
def col_map(row, datadict, mapdict, funcdict):
    """A function to map between SGN columns and ligolw columns. datadict has
    what the data you want to insert into the ligolw "row".  mapdict allows you to
    map between different names (if needed) or exclude data by setting the value to
    None.  funcdict lets you call simple functions to e.g., convert the type"""
    for col, value in datadict.items():
        oldcol = col
        if col in mapdict:
            col = mapdict[col]
            if col is None:
                continue
        if oldcol in funcdict:
            value = funcdict[oldcol](value)

        setattr(row, col, value)

event_trigs_to_coinc_xmldoc(event, trigs, snr_ts, sngls_dict, analysis_ifos, process_params, delta_t, channel_dict)

Given an event dict and a trigs list of dicts corresponding to single event as well as the parameters in the sngls_dict corresponding, construct an xml doc suitable for upload to gracedb

Source code in sgnl/sinks/gracedb_sink.py
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
def event_trigs_to_coinc_xmldoc(
    event,
    trigs,
    snr_ts,
    sngls_dict,
    analysis_ifos,
    process_params,
    delta_t,
    channel_dict,
):
    """Given an event dict and a trigs list of dicts corresponding to single
    event as well as the parameters in the sngls_dict corresponding, construct an
    xml doc suitable for upload to gracedb"""

    # Initialize the main document
    xmldoc = ligolw.Document()
    xmldoc.appendChild(ligolw.LIGO_LW())
    # FIXME bayestar needs the program name
    ligolw_process.register_to_xmldoc(
        xmldoc,
        "sgnl-inspiral",
        process_params,
        instruments=analysis_ifos,
        is_online=True,
    )

    # Add tables that depend on the number of triggers
    # NOTE: tables are initilize to 0 or null values depending on the type.
    time_slide_table = add_table_with_n_rows(
        xmldoc, lsctables.TimeSlideTable, len(analysis_ifos)
    )
    process_id = lsctables.ProcessTable.get_table(xmldoc)[0].process_id

    found_ifos = []

    # Add subthreshold trigger
    triggerless_ifos = set(snr_ts.keys()) - set([t["ifo"] for t in trigs])
    if triggerless_ifos:
        subthresh_trigs = []
        # FIXME: handle the case of multiple trigs, pick the highest network snr one
        for ifo in sorted(triggerless_ifos):
            coinc_segment = ligolw_segments.segments.segment(
                ligolw_segments.segments.NegInfinity,
                ligolw_segments.segments.PosInfinity,
            )
            for trig in trigs:
                trigger_time = trig["time"] / 1_000_000_000
                trigger_ifo = trig["ifo"]
                filter_id = trig["_filter_id"]
                coincidence_window = light_travel_time(ifo, trigger_ifo) + delta_t
                coinc_segment &= ligolw_segments.segments.segment(
                    trigger_time - coincidence_window, trigger_time + coincidence_window
                )
                half_autocorr_length = (snr_ts[trigger_ifo].data.length - 1) // 2

            for trig in subthresh_trigs:
                trigger_time = trig["time"] / 1_000_000_000
                trigger_ifo = trig["ifo"]
                coincidence_window = light_travel_time(ifo, trigger_ifo) + delta_t
                coinc_segment &= ligolw_segments.segments.segment(
                    trigger_time - coincidence_window, trigger_time + coincidence_window
                )

            snr_ts_this_ifo = snr_ts[ifo]
            snr_ts_this_ifo_array = snr_ts_this_ifo.data.data
            t0 = snr_ts_this_ifo.epoch
            dt = snr_ts_this_ifo.deltaT
            idx0 = int((coinc_segment[0] - t0) / dt)
            idxf = int(math.ceil((coinc_segment[1] - t0) / dt))
            length = snr_ts_this_ifo_array.shape[-1]
            # if idx0 < 0:
            #    # FIXME: there are cases where the trigger is at the edge of the
            #    # trigger finding window, so there is not half_autocorr_length
            #    # of samples, so pad zeros in front
            #    snr_ts_this_ifo_array = numpy.concatenate(numpy.zeros(-idx0),
            #       snr_ts_this_ifo_array[:idxf])
            #    seq = abs(snr_ts_this_ifo_array)
            #    t0 = coinc_segment[0]
            #    idx0 = 0
            #    idxf = seq.shape[-1]
            #    print('idx0 < 0', idx0, file=sys.stderr)
            # elif idxf > length:
            #    snr_ts_this_ifo_array = numpy.concatenate(snr_ts_this_ifo_array[idx0:],
            #       numpy.zeros(idxf-length))
            #    seq = abs(snr_ts_this_ifo_array)
            #    t0 = coinc_segment[0]
            #    idx0 = 0
            #    idxf = seq.shape[-1]
            #    print('idxf > length', idxf, file=sys.stderr)
            # else:

            if idx0 < 0 or idxf > length:
                # FIXME: should we actually skip if we don't have enough samples?
                print("warning: not enougth samples to find coincidence")
                idx0 = max(0, idx0)
                idxf = min(idxf, length)

            seq = abs(snr_ts_this_ifo_array[idx0:idxf])

            maxid = numpy.argmax(seq)

            maxsnr = abs(snr_ts_this_ifo_array[idx0 + maxid])
            phase = math.atan2(
                snr_ts_this_ifo_array[idx0 + maxid].imag,
                snr_ts_this_ifo_array[idx0 + maxid].real,
            )
            peakt = float(t0) + (idx0 + maxid) * dt

            deltaT = snr_ts_this_ifo.deltaT

            snip0 = idx0 + maxid - half_autocorr_length
            snipf = idx0 + maxid + half_autocorr_length + 1
            sniplength = snr_ts_this_ifo_array.shape[-1]

            # if snip0 < 0:
            #    print('pad zeros', )
            #    snr_ts_snippet = numpy.concatenate(numpy.zeros(-snip),
            #       snr_ts_this_ifo_array[:snipf])
            # elif snipf > sniplength:
            #    snr_ts_snippet = numpy.concatenate(snr_ts_this_ifo_array[snip0:],
            #       numpy.zeros(snipf-sniplength))
            # else:

            if snip0 < 0 or snipf > sniplength:
                # FIXME: in gstlal it says Bayestar needs at least 26.3ms
                print(
                    ifo,
                    "not enough samples to produce snr snippet",
                    f"{snip0=} {snipf=} {sniplength=}",
                )
                continue

            snr_ts_snippet = snr_ts_this_ifo_array[snip0:snipf]
            if snr_ts_snippet.shape[-1] == 0:
                print(
                    f"{ifo=} {idx0=} {idxf=} {maxid=} {half_autocorr_length=}",
                    f"{coinc_segment=} {trigger_time=} {coincidence_window=} {t0=}",
                    f"{dt=} {snr_ts_this_ifo_array.shape=}",
                    file=sys.stderr,
                )
                raise ValueError("no snr ts snippet")

            snr_ts_snippet_out = lal.CreateCOMPLEX8TimeSeries(
                name="snr",
                epoch=peakt - half_autocorr_length * deltaT,
                f0=0.0,
                deltaT=deltaT,
                sampleUnits=lal.DimensionlessUnit,
                length=snr_ts_snippet.shape[-1],
            )
            snr_ts_snippet_out.data.data = snr_ts_snippet
            snr_ts[ifo] = snr_ts_snippet_out

            subthresh_trigs.append(
                {
                    "_filter_id": filter_id,
                    "ifo": ifo,
                    "time": int(peakt * 1e9),
                    "phase": phase,
                    "chisq": None,
                    "snr": float(maxsnr),
                }
            )

        trigs.extend(subthresh_trigs)
        event["network_snr"] = sum(trig["snr"] ** 2 for trig in trigs) ** 0.5
        event["time"] = min(trig["time"] for trig in trigs)

    for n, ifo in enumerate(analysis_ifos):
        time_slide_table[n].instrument = ifo
        time_slide_table[n].process_id = process_id

    sngl_inspiral_table = add_table_with_n_rows(
        xmldoc, lsctables.SnglInspiralTable, len(trigs)
    )
    coinc_map_table = add_table_with_n_rows(xmldoc, lsctables.CoincMapTable, len(trigs))
    coinc_def_table = add_table_with_n_rows(xmldoc, lsctables.CoincDefTable, 1)
    coinc_def_table[0].description = "sngl_inspiral<-->sngl_inspiral coincidences"
    coinc_def_table[0].search = "inspiral"
    for n, row in enumerate(sngl_inspiral_table):
        sngl_map(row, sngls_dict, trigs[n]["_filter_id"], exclude=())
        row.event_id = n
        row.channel = channel_dict[row.ifo]
        row.chisq_dof = 1
        row.eff_distance = float("nan")
        row.process_id = process_id
        col_map(
            row,
            trigs[n],
            {
                "_filter_id": "template_id",
                "time": "end",
                "phase": "coa_phase",
                "epoch_start": None,
                "epoch_end": None,
            },
            {"time": ns_to_gps},
        )
        coinc_map_table[n].event_id = row.event_id
        coinc_map_table[n].table_name = "sngl_inspiral"
        found_ifos.append(row.ifo)

        # Add snr time series
        ts = snr_ts[row.ifo]
        snr_time_series_element = lalseries.build_COMPLEX8TimeSeries(ts)
        snr_time_series_element.appendChild(
            param.Param.from_pyvalue("event_id", row.event_id)
        )
        xmldoc.childNodes[-1].appendChild(snr_time_series_element)

    # The remaining tables will all have ids set to integer zeros which
    # actually makes this single event document just magically have the right
    # relationships
    coinc_inspiral_table = add_table_with_n_rows(
        xmldoc, lsctables.CoincInspiralTable, 1
    )
    col_map(
        coinc_inspiral_table[0],
        event,
        {
            "time": "end",
            "network_snr": "snr",
            "combined_far": "combined_far",
            "false_alarm_probability": "false_alarm_rate",
            "likelihood": None,
        },
        {"time": ns_to_gps},
    )
    coinc_inspiral_table[0].ifos = ",".join(sorted(found_ifos))

    sngl_row = sngls_dict[trigs[0]["_filter_id"]]
    mass = sngl_row.mass1 + sngl_row.mass2
    mchirp = sngl_row.mchirp

    coinc_inspiral_table[0].mass = mass
    coinc_inspiral_table[0].mchirp = mchirp

    coinc_event_table = add_table_with_n_rows(xmldoc, lsctables.CoincTable, 1)
    col_map(
        coinc_event_table[0],
        event,
        {
            "time": None,
            "network_snr": None,
            "combined_far": None,
        },
        {},
    )
    coinc_event_table[0].instruments = ",".join(sorted(found_ifos))
    coinc_event_table[0].process_id = process_id

    # coinc_def_table = add_table_with_n_rows(xmldoc, lsctables.CoincDefTable, 1)

    return xmldoc

publish_gracedb(client, xmldoc, group, pipeline, search, labels)

Send the xml doc to gracedb directly

Source code in sgnl/sinks/gracedb_sink.py
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
def publish_gracedb(client, xmldoc, group, pipeline, search, labels):
    """Send the xml doc to gracedb directly"""
    message = io.BytesIO()
    ligolw_utils.write_fileobj(xmldoc, message)
    resp = client.createEvent(
        group=group,
        pipeline=pipeline,
        filename="coinc.xml",
        search=search,
        labels=labels,
        offline=False,
        filecontents=message.getvalue(),
    )
    print("Created Event:", resp.json())

    # Write a log for the created event
    graceid = resp.json()["graceid"]
    log = client.writeLog(
        object_id=graceid,
        message="This is a test log.",
        filename="sgn.png",
        filecontents=base64.b64decode(logo_data()),
        tag_name=["plot"],
        displayName=["plot"],
    )
    print("Written Log:", log)

publish_kafka(client, xmldoc, job_tag, analysis_tag, instruments, group, search, strike_object)

Send the xmldoc to kafka, where it will eventually be uploaded to gracedb

Source code in sgnl/sinks/gracedb_sink.py
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
def publish_kafka(
    client,
    xmldoc,
    job_tag,
    analysis_tag,
    instruments,
    group,
    search,
    strike_object,
):
    """Send the xmldoc to kafka, where it will eventually be uploaded to gracedb"""

    # coinc_table_row = lsctables.CoincTable.get_table(xmldoc)[0]
    coinc_inspiral_row = lsctables.CoincInspiralTable.get_table(xmldoc)[0]
    sngl_inspiral_table = lsctables.SnglInspiralTable.get_table(xmldoc)

    message = io.BytesIO()
    ligolw_utils.write_fileobj(xmldoc, message)

    topic_prefix = "" if "noninj" in job_tag else "inj_"
    client.produce(
        topic=f"sgnl.{analysis_tag}.{topic_prefix}events",
        value=json.dumps(
            {
                "far": coinc_inspiral_row.combined_far,
                "snr": coinc_inspiral_row.snr,
                "time": coinc_inspiral_row.end_time,
                "time_ns": coinc_inspiral_row.end_time_ns,
                "coinc": message.getvalue().decode("utf-8"),
                "job_tag": job_tag,
            }
        ),
        key=job_tag,
    )
    client.poll(0)

    background_bin = int(sngl_inspiral_table[0].Gamma1)
    description = "%s_%s_%s_%s_%s" % (
        "SGNL",
        job_tag,
        ("%.4g" % coinc_inspiral_row.mass).replace(".", "_").replace("-", "_"),
        group,
        search,
    )
    end_time = int(coinc_inspiral_row.end)

    #
    # make sure the directory where we will write the files to disk exists
    #

    gracedb_uploads_gps_dir = os.path.join("gracedb_uploads", str(end_time)[:5])
    if not os.path.exists(gracedb_uploads_gps_dir):
        # set exist_ok = True in case multiple jobs try
        # to create the directory at the same time;
        # this is more likely to happen with injection
        # jobs due to the high injection rate
        os.makedirs(gracedb_uploads_gps_dir, exist_ok=True)

    rankingstat_filename = os.path.join(
        gracedb_uploads_gps_dir,
        "%s-%s_RankingData-%d-%d.xml.gz" % (instruments, description, end_time, 1),
    )
    write_rankingstat_xmldoc_gracedb(
        strike_object.likelihood_ratio_uploads[str(background_bin)],
        rankingstat_filename,
    )

    ligolw_utils.write_filename(
        xmldoc, rankingstat_filename.replace("_RankingData", "")
    )

    client.produce(
        topic=f"sgnl.{analysis_tag}.{topic_prefix}ranking_stat",
        value=json.dumps(
            {
                "ranking_data_path": os.path.realpath(rankingstat_filename),
                "time": coinc_inspiral_row.end_time,
                "time_ns": coinc_inspiral_row.end_time_ns,
                "coinc": message.getvalue().decode("utf-8"),
            }
        ),
    )
    client.poll(0)