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

#!/usr/local/bin/python 

# encoding: utf-8 

""" 

*Annotate transients with supplimentary information resulting from the gravity events* 

 

:Author: 

David Young 

 

:Date Created: 

March 14, 2017 

""" 

################# GLOBAL IMPORTS #################### 

import sys 

import os 

os.environ['TERM'] = 'vt100' 

from fundamentals import tools 

import healpy as hp 

import numpy as np 

import math 

 

 

class annotator(): 

""" 

*Annotate transients with supplimentary information resulting from the gravity events* 

 

**Key Arguments:** 

- ``log`` -- logger 

- ``settings`` -- the settings dictionary 

- ``gwid`` -- the ID of the gravity event to annotate the transients against (matched in settings file)  

 

**Usage:** 

 

To setup your logger, settings and database connections, please use the ``fundamentals`` package (`see tutorial here <http://fundamentals.readthedocs.io/en/latest/#tutorial>`_).  

 

To initiate a annotator object, use the following: 

 

.. todo:: 

 

- add a tutorial about ``annotator`` to documentation 

- create a blog post about what ``annotator`` does 

 

.. code-block:: python  

 

from breaker.transients import annotator 

an = annotator( 

log=log, 

settings=settings, 

gwid="G211117" 

) 

transientNames, probs = an.annotate_transients(transients)  

""" 

 

def annotate_transients( 

self, 

transients 

): 

""" 

*generate a list of the likihood contours the transients lie within* 

 

**Key Arguments:** 

- ``transients`` -- a dictionary of transients with the unique transient IDs as keys and (ra, dec) as tuple value. 

 

**Return:** 

- ``transientNames`` -- a list of the transient names as they appear in the original input transient dictionary 

- ``prosb`` -- a list of the nearest 10% likihood conntour the transient falls within (syncs with `transientNames` list) 

 

**Usage:** 

 

See class docstring 

""" 

self.log.info('starting the ``annotate_transients`` method') 

 

transientNames = [] 

ra = [] 

dec = [] 

 

transientNames = [] 

transientNames[:] = [t for t in transients.keys()] 

ra = [] 

dec = [] 

ra[:] = [r[0] for r in transients.values()] 

dec[:] = [d[1] for d in transients.values()] 

 

probs = self._generate_probability_distribution(ra, dec) 

 

self.log.info('completed the ``annotate_transients`` method') 

return transientNames, probs 

 

def _generate_probability_distribution( 

self, 

ra, 

dec): 

"""* generate probability distribution* 

 

**Key Arguments:** 

- ``ra`` -- a list of RA values for transients. 

- ``dec`` -- a list of DEC values for transients. 

 

**Return:** 

- ``prob`` -- a list of the probabilty contours the transients lie within (indices synced with RA and DEC lists) 

""" 

self.log.info( 

'starting the ``_generate_probability_distribution`` method') 

 

pathToProbMap = self.settings[ 

"gravitational waves"][self.gwid]["mapPath"] 

 

# READ HEALPIX MAPS FROM FITS FILE 

# THIS FILE IS A ONE COLUMN FITS BINARY, WITH EACH CELL CONTAINING AN 

# ARRAY OF PROBABILITIES 

# READ IN THE HEALPIX FITS FILE 

aMap, mapHeader = hp.read_map(pathToProbMap, 0, h=True, verbose=False) 

# DETERMINE THE SIZE OF THE HEALPIXELS 

nside = hp.npix2nside(len(aMap)) 

totalProb = sum(aMap) 

 

# CONTOURS - NEED TO ADD THE CUMMULATIVE PROBABILITY 

# GET THE INDEXES ORDERED BY PROB OF PIXELS; HIGHEST TO LOWEST 

i = np.flipud(np.argsort(aMap)) 

cumsum = np.cumsum(aMap[i]) 

cls = np.empty_like(aMap) 

cls[i] = cumsum * 100 

 

healpixId = hp.pixelfunc.ang2pix( 

nside, ra, dec, nest=False, lonlat=True) 

 

vals = cls[healpixId] 

 

probs = [] 

probs[:] = [roundup(p, 10) if p < 100. else 100 for p in vals] 

 

self.log.info( 

'completed the ``_generate_probability_distribution`` method') 

return probs 

 

# use the tab-trigger below for new method 

# xt-class-method 

 

 

def roundup(val, resolution): 

return int(math.ceil(val / float(resolution))) * int(resolution)