Coverage for pesummary/gw/gracedb.py: 82.1%

28 statements  

« prev     ^ index     » next       coverage.py v7.4.4, created at 2024-05-02 08:42 +0000

1# Licensed under an MIT style license -- see LICENSE.md 

2 

3from ligo.gracedb.rest import GraceDb 

4from ligo.gracedb.exceptions import HTTPError 

5from pesummary.utils.utils import logger 

6from pesummary import conf 

7 

8__author__ = ["Charlie Hoy <charlie.hoy@ligo.org>"] 

9 

10 

11def get_gracedb_data( 

12 gracedb_id, superevent=False, info=None, json=None, 

13 service_url=conf.gracedb_server 

14): 

15 """Grab data from GraceDB for a specific event. 

16 

17 Parameters 

18 ---------- 

19 gracedb_id: str 

20 the GraceDB id of the event you wish to retrieve the data for 

21 superevent: Bool, optional 

22 True if the gracedb_id you are providing is a superevent 

23 info: str/list, optional 

24 either a string or list of strings for information you wish to 

25 retrieve 

26 json: dict, optional 

27 data that you have already downloaded from gracedb 

28 service_url: str, optional 

29 service url you wish to use when accessing data from GraceDB 

30 """ 

31 client = GraceDb(service_url=service_url) 

32 if json is None and superevent: 

33 json = client.superevent(gracedb_id).json() 

34 elif json is None: 

35 try: 

36 json = client.superevent(gracedb_id).json() 

37 except HTTPError: 

38 json = client.event(gracedb_id).json() 

39 

40 if isinstance(info, str) and info in json.keys(): 

41 return str(json[info]) 

42 elif isinstance(info, str): 

43 raise AttributeError( 

44 "Could not find '{}' in the gracedb dictionary. Available entries " 

45 "are: {}".format(info, ", ".join(json.keys())) 

46 ) 

47 elif isinstance(info, list): 

48 data = {} 

49 for _info in info: 

50 if _info in json.keys(): 

51 data[_info] = json[_info] 

52 else: 

53 logger.warning( 

54 "Unable to find any information for '{}'".format(_info) 

55 ) 

56 return data 

57 elif info is None: 

58 return json 

59 else: 

60 raise ValueError( 

61 "info data not understood. Please provide either a list or string" 

62 )