Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

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

146

147

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

231

232

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

259

260

261

262

263

264

265

266

267

268

269

270

271

272

273

274

275

276

277

278

279

280

281

282

283

284

285

286

287

288

289

290

291

292

293

294

295

296

297

298

299

300

301

302

303

304

305

306

307

308

309

310

311

312

313

314

315

316

317

318

319

320

321

322

323

324

325

326

327

328

329

330

331

332

333

334

335

336

337

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

592

593

594

595

596

597

598

599

600

601

602

603

604

605

606

607

608

609

610

611

612

613

614

615

616

617

618

619

620

621

622

623

624

625

626

627

628

629

630

631

632

633

634

635

636

637

638

639

640

641

642

643

644

"""Source Parameter Estimation with LALInference and Bilby.""" 

from distutils.spawn import find_executable 

from distutils.dir_util import mkpath 

import glob 

import itertools 

import json 

import os 

import shutil 

import subprocess 

import tempfile 

import urllib 

 

from celery import group 

from gwdatafind import find_urls 

from ligo.gracedb.exceptions import HTTPError 

import numpy as np 

 

from .. import app 

from ..jinja import env 

from .core import ordered_group 

from . import condor 

from . import gracedb 

 

 

ini_name = 'online_lalinference_pe.ini' 

 

executables = {'datafind': 'gw_data_find', 

'mergeNSscript': 'lalinference_nest2pos', 

'mergeMCMCscript': 'cbcBayesMCMC2pos', 

'combinePTMCMCh5script': 'cbcBayesCombinePTMCMCh5s', 

'resultspage': 'cbcBayesPostProc', 

'segfind': 'ligolw_segment_query', 

'ligolw_print': 'ligolw_print', 

'coherencetest': 'lalinference_coherence_test', 

'lalinferencenest': 'lalinference_nest', 

'lalinferencemcmc': 'lalinference_mcmc', 

'lalinferencebambi': 'lalinference_bambi', 

'lalinferencedatadump': 'lalinference_datadump', 

'ligo-skymap-from-samples': 'true', 

'ligo-skymap-plot': 'true', 

'processareas': 'process_areas', 

'computeroqweights': 'lalinference_compute_roq_weights', 

'mpiwrapper': 'lalinference_mpi_wrapper', 

'gracedb': 'gracedb', 

'ppanalysis': 'cbcBayesPPAnalysis', 

'pos_to_sim_inspiral': 'cbcBayesPosToSimInspiral'} 

 

 

def _data_exists(end, frametype_dict): 

"""Check whether data at end time can be found with gwdatafind and return 

true it it is found. 

""" 

return min( 

len( 

find_urls(ifo[0], frametype_dict[ifo], end, end + 1) 

) for ifo in frametype_dict.keys() 

) > 0 

 

 

class NotEnoughData(Exception): 

"""Raised if found data is not enough due to the latency of data 

transfer 

""" 

 

 

@app.task(bind=True, autoretry_for=(NotEnoughData, ), default_retry_delay=1, 

max_retries=86400, retry_backoff=True, shared=False) 

def query_data(self, trigtime): 

"""Continues to query data until it is found with gwdatafind and return 

frametypes for the data. If data is not found in 86400 seconds = 1 day, 

raise NotEnoughData. 

""" 

end = trigtime + 2 

if _data_exists(end, app.conf['low_latency_frame_types']): 

return app.conf['low_latency_frame_types'] 

elif _data_exists(end, app.conf['high_latency_frame_types']): 

return app.conf['high_latency_frame_types'] 

else: 

raise NotEnoughData 

 

 

@app.task(ignore_result=True, shared=False) 

def upload_no_frame_files(request, exc, traceback, superevent_id): 

"""Upload notification when no frame files are found. 

 

Parameters 

---------- 

request : Context (placeholder) 

Task request variables 

exc : Exception 

Exception rased by condor.submit 

traceback : str (placeholder) 

Traceback message from a task 

superevent_id : str 

The GraceDB ID of a target superevent 

""" 

if isinstance(exc, NotEnoughData): 

gracedb.upload.delay( 

filecontents=None, filename=None, 

graceid=superevent_id, 

message='Frame files have not been found.', 

tags='pe' 

) 

 

 

def _find_appropriate_cal_env(trigtime, dir_name): 

"""Return the path to the calibration uncertainties estimated at the time 

before and closest to the trigger time. If there are no calibration 

uncertainties estimated before the trigger time, return the oldest one. The 

gpstimes at which the calibration uncertainties were estimated and the 

names of the files containing the uncertaintes are saved in 

[HLV]_CalEnvs.txt. 

 

Parameters 

---------- 

trigtime : float 

The trigger time of a target event 

dir_name : str 

The path to the directory where files containing calibration 

uncertainties exist 

 

Return 

------ 

path : str 

The path to the calibration uncertainties appropriate for a target 

event 

""" 

filename, = glob.glob(os.path.join(dir_name, '[HLV]_CalEnvs.txt')) 

calibration_index = np.atleast_1d( 

np.recfromtxt(filename, names=['gpstime', 'filename']) 

) 

gpstimes = calibration_index['gpstime'] 

candidate_gpstimes = gpstimes < trigtime 

if np.any(candidate_gpstimes): 

idx = np.argmax(gpstimes * candidate_gpstimes) 

appropriate_cal = calibration_index['filename'][idx] 

else: 

appropriate_cal = calibration_index['filename'][np.argmin(gpstimes)] 

return os.path.join(dir_name, appropriate_cal.decode('utf-8')) 

 

 

@app.task(shared=False) 

def prepare_ini(frametype_dict, event, superevent_id=None): 

"""Determine an appropriate PE settings for the target event and return ini 

file content for LALInference pipeline 

""" 

# Get template of .ini file 

ini_template = env.get_template('online_pe.jinja2') 

 

# fill out the ini template and return the resultant content 

singleinspiraltable = event['extra_attributes']['SingleInspiral'] 

trigtime = event['gpstime'] 

ini_settings = { 

'service_url': gracedb.client._service_url, 

'types': frametype_dict, 

'channels': app.conf['strain_channel_names'], 

'state_vector_channels': app.conf['state_vector_channel_names'], 

'webdir': os.path.join( 

app.conf['pe_results_path'], event['graceid'], 'lalinference' 

), 

'paths': [{'name': name, 'path': find_executable(executable)} 

for name, executable in executables.items()], 

'h1_calibration': _find_appropriate_cal_env( 

trigtime, 

'/home/cbc/pe/O3/calibrationenvelopes/LIGO_Hanford' 

), 

'l1_calibration': _find_appropriate_cal_env( 

trigtime, 

'/home/cbc/pe/O3/calibrationenvelopes/LIGO_Livingston' 

), 

'v1_calibration': _find_appropriate_cal_env( 

trigtime, 

'/home/cbc/pe/O3/calibrationenvelopes/Virgo' 

), 

'q': min([sngl['mass2'] / sngl['mass1'] 

for sngl in singleinspiraltable]), 

'mpirun': find_executable('mpirun') 

} 

ini_rota = ini_template.render(ini_settings) 

ini_settings.update({'use_of_ini': 'online'}) 

ini_online = ini_template.render(ini_settings) 

# upload LALInference ini file to GraceDB 

if superevent_id is not None: 

gracedb.upload.delay( 

ini_rota, filename=ini_name, graceid=superevent_id, 

message=('Automatically generated LALInference configuration file' 

' for this event.'), 

tags='pe') 

 

return ini_online 

 

 

def pre_pe_tasks(event, superevent_id): 

"""Return canvas of tasks executed before parameter estimation starts""" 

return query_data.s(event['gpstime']).on_error( 

upload_no_frame_files.s(superevent_id) 

) | prepare_ini.s(event, superevent_id) 

 

 

@app.task(shared=False) 

def _setup_dag_for_lalinference(coinc_psd, ini_contents, 

rundir, superevent_id): 

"""Create DAG for a lalinference run and return the path to DAG. 

 

Parameters 

---------- 

coinc_psd : tuple of byte contents 

Tuple of the byte contents of ``coinc.xml`` and ``psd.xml.gz`` 

ini_contents : str 

The content of online_lalinference_pe.ini 

rundir : str 

The path to a run directory where the DAG file exits 

superevent_id : str 

The GraceDB ID of a target superevent 

 

Returns 

------- 

path_to_dag : str 

The path to the .dag file 

""" 

coinc_contents, psd_contents = coinc_psd 

 

# write down coinc.xml in the run directory 

path_to_coinc = os.path.join(rundir, 'coinc.xml') 

with open(path_to_coinc, 'wb') as f: 

f.write(coinc_contents) 

 

# write down psd.xml.gz 

if psd_contents is not None: 

path_to_psd = os.path.join(rundir, 'psd.xml.gz') 

with open(path_to_psd, 'wb') as f: 

f.write(psd_contents) 

psd_arg = ['--psd', path_to_psd] 

else: 

psd_arg = [] 

 

# write down .ini file in the run directory. 

path_to_ini = os.path.join(rundir, ini_name) 

with open(path_to_ini, 'w') as f: 

f.write(ini_contents) 

 

try: 

subprocess.run( 

['lalinference_pipe', '--run-path', rundir, 

'--coinc', path_to_coinc, path_to_ini] + psd_arg, 

capture_output=True, check=True) 

except subprocess.CalledProcessError as e: 

contents = b'args:\n' + json.dumps(e.args[1]).encode('utf-8') + \ 

b'\n\nstdout:\n' + e.stdout + b'\n\nstderr:\n' + e.stderr 

gracedb.upload.delay( 

filecontents=contents, filename='pe_dag.log', 

graceid=superevent_id, 

message='Failed to prepare DAG for lalinference', tags='pe' 

) 

shutil.rmtree(rundir) 

raise 

else: 

# Remove the ini file so that people do not accidentally use this ini 

# file and hence online-PE-only nodes. 

os.remove(path_to_ini) 

 

return os.path.join(rundir, 'multidag.dag') 

 

 

@app.task(shared=False) 

def _setup_dag_for_bilby(event, rundir, preferred_event_id, superevent_id): 

"""Create DAG for a bilby run and return the path to DAG. 

 

Parameters 

---------- 

event : json contents 

The json contents retrieved from gracedb.get_event() 

rundir : str 

The path to a run directory where the DAG file exits 

preferred_event_id : str 

The GraceDB ID of a target preferred event 

superevent_id : str 

The GraceDB ID of a target superevent 

 

Returns 

------- 

path_to_dag : str 

The path to the .dag file 

""" 

path_to_json = os.path.join(rundir, 'event.json') 

with open(path_to_json, 'w') as f: 

json.dump(event, f, indent=2) 

 

path_to_webdir = os.path.join( 

app.conf['pe_results_path'], preferred_event_id, 'bilby' 

) 

 

setup_arg = ['bilby_pipe_gracedb', '--webdir', path_to_webdir, 

'--outdir', rundir, '--json', path_to_json] 

if not app.conf['sentry_environment'] == 'production': 

setup_arg += ['--channel-dict', 'o2replay', 

'--sampler-kwargs', 'FastTest'] 

try: 

subprocess.run(setup_arg, capture_output=True, check=True) 

except subprocess.CalledProcessError as e: 

contents = b'args:\n' + json.dumps(e.args[1]).encode('utf-8') + \ 

b'\n\nstdout:\n' + e.stdout + b'\n\nstderr:\n' + e.stderr 

gracedb.upload.delay( 

filecontents=contents, filename='pe_dag.log', 

graceid=superevent_id, 

message='Failed to prepare DAG for bilby', tags='pe' 

) 

shutil.rmtree(rundir) 

raise 

else: 

# Uploads bilby ini file to GraceDB 

_upload_result(rundir, 'bilby_config.ini', superevent_id, 

'Automatically generated Bilby configuration file', 

'pe', 'online_bilby_pe.ini').delay() 

 

path_to_dag, = glob.glob(os.path.join(rundir, 'submit/dag*.submit')) 

print(path_to_dag) 

return path_to_dag 

 

 

@app.task(shared=False) 

def _condor_no_submit(path_to_dag): 

"""Run 'condor_submit_dag -no_submit' and return the path to .sub file.""" 

subprocess.run(['condor_submit_dag', '-no_submit', path_to_dag], 

capture_output=True, check=True) 

return '{}.condor.sub'.format(path_to_dag) 

 

 

@app.task(shared=False) 

def dag_prepare_task(rundir, superevent_id, preferred_event_id, pe_pipeline, 

ini_contents=None): 

"""Return a canvas of tasks to prepare DAG. 

 

Parameters 

---------- 

rundir : str 

The path to a run directory where the DAG file exits 

superevent_id : str 

The GraceDB ID of a target superevent 

preferred_event_id : str 

The GraceDB ID of a target preferred event 

pe_pipeline : str 

The parameter estimation pipeline used 

Either 'lalinference' OR 'bilby' 

ini_contents : str 

The content of online_lalinference_pe.ini 

Required if pe_pipeline == 'lalinference' 

 

Returns 

------- 

canvas : canvas of tasks 

The canvas of tasks to prepare DAG 

""" 

if pe_pipeline == 'lalinference': 

canvas = ordered_group( 

gracedb.download.si('coinc.xml', preferred_event_id), 

_download_psd.si(preferred_event_id) 

) | _setup_dag_for_lalinference.s(ini_contents, rundir, superevent_id) 

elif pe_pipeline == 'bilby': 

canvas = gracedb.get_event.si(preferred_event_id) | \ 

_setup_dag_for_bilby.s(rundir, preferred_event_id, superevent_id) 

else: 

print("A PE pipeline named {} does not exist.".format(pe_pipeline)) 

raise 

canvas |= _condor_no_submit.s() 

return canvas 

 

 

def _find_paths_from_name(directory, name): 

"""Return the paths of files or directories with given name under the 

specfied directory 

 

Parameters 

---------- 

directory : string 

Name of directory under which the target file or directory is searched 

for. 

name : string 

Name of target files or directories 

 

Returns 

------- 

paths : generator 

Paths to the target files or directories 

""" 

return glob.iglob(os.path.join(directory, '**', name), recursive=True) 

 

 

@app.task(ignore_result=True, shared=False) 

def job_error_notification(request, exc, traceback, 

superevent_id, rundir, pe_pipeline): 

"""Upload notification when condor.submit terminates unexpectedly. 

 

Parameters 

---------- 

request : Context (placeholder) 

Task request variables 

exc : Exception 

Exception rased by condor.submit 

traceback : str (placeholder) 

Traceback message from a task 

superevent_id : str 

The GraceDB ID of a target superevent 

rundir : str 

The run directory for PE 

pe_pipeline : str 

The parameter estimation pipeline used 

Either lalinference OR bilby 

""" 

if isinstance(exc, condor.JobAborted): 

gracedb.upload.delay( 

filecontents=None, filename=None, graceid=superevent_id, tags='pe', 

message='The {} condor job was aborted.'.format(pe_pipeline) 

) 

elif isinstance(exc, condor.JobFailed): 

gracedb.upload.delay( 

filecontents=None, filename=None, graceid=superevent_id, tags='pe', 

message='The {} condor job failed.'.format(pe_pipeline) 

) 

# Get paths to .log files, .err files, .out files 

paths_to_log = _find_paths_from_name(rundir, '*.log') 

paths_to_err = _find_paths_from_name(rundir, '*.err') 

paths_to_out = _find_paths_from_name(rundir, '*.out') 

# Upload .log and .err files 

for path in itertools.chain(paths_to_log, paths_to_err, paths_to_out): 

with open(path, 'rb') as f: 

contents = f.read() 

if contents: 

# put .log suffix in log file names so that users can directly 

# read the contents instead of downloading them when they click 

# file names 

gracedb.upload.delay( 

filecontents=contents, 

filename=os.path.basename(path) + '.log', 

graceid=superevent_id, 

message='A log file for {} condor job.'.format(pe_pipeline), 

tags='pe' 

) 

 

 

@app.task(ignore_result=True, shared=False) 

def _upload_url(pe_results_path, graceid, pe_pipeline): 

"""Upload url of a page containing all of the plots.""" 

if pe_pipeline == 'lalinference': 

path_to_posplots, = _find_paths_from_name( 

pe_results_path, 'posplots.html' 

) 

elif pe_pipeline == 'bilby': 

path_to_posplots, = _find_paths_from_name( 

pe_results_path, 'home.html' 

) 

 

baseurl = urllib.parse.urljoin( 

app.conf['pe_results_url'], 

os.path.relpath( 

path_to_posplots, 

app.conf['pe_results_path'] 

) 

) 

gracedb.upload.delay( 

filecontents=None, filename=None, graceid=graceid, 

message=('Online {} parameter estimation finished.' 

'<a href={}>results</a>').format(pe_pipeline, baseurl), 

tags='pe' 

) 

 

 

@app.task(ignore_result=True, shared=False) 

def _get_result_contents(pe_results_path, filename): 

"""Return the contents of a PE results file by reading it from the local 

filesystem. 

""" 

path, = _find_paths_from_name(pe_results_path, filename) 

with open(path, 'rb') as f: 

contents = f.read() 

return contents 

 

 

def _upload_result(pe_results_path, filename, graceid, message, tag, 

uploaded_filename=None): 

"""Return a canvas to get the contents of a PE result file and upload it to 

GraceDB. 

""" 

if uploaded_filename is None: 

uploaded_filename = filename 

return _get_result_contents.si(pe_results_path, filename) | \ 

gracedb.upload.s(uploaded_filename, graceid, message, tag) 

 

 

@app.task(ignore_result=True, shared=False) 

def clean_up(rundir): 

"""Clean up a run directory. 

 

Parameters 

---------- 

rundir : str 

The path to a run directory where the DAG file exits 

""" 

shutil.rmtree(rundir) 

 

 

@app.task(ignore_result=True, shared=False) 

def dag_finished(rundir, preferred_event_id, superevent_id, pe_pipeline): 

"""Upload PE results and clean up run directory 

 

Parameters 

---------- 

rundir : str 

The path to a run directory where the DAG file exits 

preferred_event_id : str 

The GraceDB ID of a target preferred event 

superevent_id : str 

The GraceDB ID of a target superevent 

pe_pipeline : str 

The parameter estimation pipeline used 

Either lalinference OR bilby 

 

Returns 

------- 

tasks : canvas 

The work-flow for uploading PE results 

""" 

if pe_pipeline == 'lalinference': 

# path to lalinference pe results 

pe_results_path = os.path.join( 

app.conf['pe_results_path'], preferred_event_id, 'lalinference' 

) 

 

uploads = [ 

(rundir, 'posterior*.hdf5', 

'LALInference posterior samples', 

'LALInference.posterior_samples.hdf5'), 

(pe_results_path, 'extrinsic.png', 

'LALInference corner plot for extrinsic parameters', 

'LALInference.extrinsic.png'), 

(pe_results_path, 'sourceFrame.png', 

'LALInference corner plot for source frame parameters', 

'LALInference.intrinsic.png'), 

] 

 

elif pe_pipeline == 'bilby': 

# path to bilby pe results 

pe_results_path = os.path.join( 

app.conf['pe_results_path'], preferred_event_id, 'bilby' 

) 

 

resultdir = rundir + '/result' 

 

resultfile, = glob.glob('*ed_result.json') 

intrinsic, = glob.glob('*_intrinsic_corner.png') 

extrinsic, = glob.glob('*_extrinsic_corner.png') 

 

uploads = [ 

(resultdir, resultfile, 

'Bilby posterior samples', 

'Bilby.posterior_samples.json'), 

(resultdir, extrinsic, 

'Bilby corner plot for extrinsic parameters', 

'Bilby.extrinsic.png'), 

(resultdir, intrinsic, 

'Bilby corner plot for intrinsic parameters', 

'Bilby.intrinsic.png'), 

] 

 

upload_tasks = [ 

_upload_result( 

dir, name1, superevent_id, comment, 'pe', name2 

) for dir, name1, comment, name2 in uploads 

] 

 

# FIXME: _upload_url.si has to be out of group for 

# gracedb.create_label.si to run 

( 

_upload_url.si(pe_results_path, superevent_id, pe_pipeline) 

| 

group(upload_tasks) 

| 

clean_up.si(rundir) 

).delay() 

 

if pe_pipeline == 'lalinference': 

gracedb.create_label.delay('PE_READY', superevent_id) 

 

 

@gracedb.task(shared=False) 

def _download_psd(gid): 

"""Download ``psd.xml.gz`` and return its content. If that file does not 

exist, return None. 

""" 

try: 

return gracedb.download("psd.xml.gz", gid) 

except HTTPError: 

return None 

 

 

@app.task(ignore_result=True, shared=False) 

def start_pe(ini_contents, preferred_event_id, superevent_id, pe_pipeline): 

"""Run Parameter Estimation on a given event. 

 

Parameters 

---------- 

ini_contents : str 

The content of online_lalinference_pe.ini 

preferred_event_id : str 

The GraceDB ID of a target preferred event 

superevent_id : str 

The GraceDB ID of a target superevent 

pe_pipeline : str 

The parameter estimation pipeline used 

lalinference OR bilby 

""" 

gracedb.upload.delay( 

filecontents=None, filename=None, graceid=superevent_id, 

message=('Starting {} online parameter estimation ' 

'for {}').format(pe_pipeline, preferred_event_id), 

tags='pe' 

) 

 

# make a run directory 

pipeline_dir = os.path.expanduser('~/.cache/{}'.format(pe_pipeline)) 

mkpath(pipeline_dir) 

rundir = tempfile.mkdtemp( 

dir=pipeline_dir, prefix='{}_'.format(superevent_id) 

) 

 

# give permissions to read the files under the run directory so that PE 

# ROTA people can check the status of parameter estimation. 

os.chmod(rundir, 0o755) 

 

canvas = ( 

dag_prepare_task( 

rundir, superevent_id, preferred_event_id, 

pe_pipeline, ini_contents 

) 

| 

condor.submit.s().on_error( 

job_error_notification.s(superevent_id, rundir, pe_pipeline) 

) 

| 

dag_finished.si( 

rundir, preferred_event_id, superevent_id, pe_pipeline 

) 

) 

canvas.delay()