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

from __future__ import print_function, division, absolute_import, unicode_literals 

import numpy as np 

from collections import deque 

import os 

 

from cosmoHammer.ChainContext import ChainContext 

from cosmoHammer.exceptions import LikelihoodComputationException 

from cosmoHammer import getLogger 

from cosmoHammer.util import Params 

 

class LikelihoodComputationChain(object): 

""" 

Implementation of a likelihood computation chain. 

""" 

 

def __init__(self, min=None, max=None): 

""" 

Constructor for the likelihood chain 

 

:param min: array  

lower bound for the parameters 

:param max: array 

upper bound for the parameters 

""" 

self.min = min 

self.max = max 

self._likelihoodModules = deque(); 

self._coreModules = deque(); 

 

 

def getCoreModules(self): 

"""pointer to the likelihood module list """ 

return self._coreModules 

 

def getLikelihoodModules(self): 

"""pointer to the core module list """ 

return self._likelihoodModules 

 

def addLikelihoodModule(self, module): 

""" 

adds a module to the likelihood module list 

 

:param module: callable 

the callable module to add for the likelihood computation 

""" 

self.getLikelihoodModules().append(module) 

 

def addCoreModule(self, module): 

""" 

adds a module to the likelihood module list 

 

:param module: callable 

the callable module to add for the computation of the data 

""" 

self.getCoreModules().append(module) 

 

 

def isValid(self, p): 

""" 

checks if the given parameters are valid  

""" 

if(self.min is not None): 

for i in range(len(p)): 

if (p[i]<self.min[i]): 

getLogger().debug("Params out of bounds i="+str(i)+" params "+str(p)) 

return False 

 

if(self.max is not None): 

for i in range(len(p)): 

if (p[i]>self.max[i]): 

getLogger().debug("Params out of bounds i="+str(i)+" params "+str(p)) 

return False 

 

return True 

 

 

def setup(self): 

"""sets up the chain and its modules """ 

for cModule in self.getCoreModules(): 

cModule.setup() 

 

for cModule in self.getLikelihoodModules(): 

cModule.setup() 

 

 

def __call__(self, p): 

""" 

Computes the log likelihood by calling all the core and likelihood modules. 

 

:param p: the parameter array for which the likelihood should be evaluated 

 

:return: the current likelihood and a dict with additional data 

""" 

try: 

getLogger().debug("pid: %s, processing: %s"%(os.getpid(), p)) 

if not self.isValid(p): 

raise LikelihoodComputationException() 

 

ctx = self.createChainContext(p) 

 

self.invokeCoreModules(ctx) 

 

likelihood = self.computeLikelihoods(ctx) 

getLogger().debug("pid: %s, processed. Returning: %s"%(os.getpid(), likelihood)) 

return likelihood, ctx.getData() 

except LikelihoodComputationException: 

getLogger().debug("pid: %s, processed. Returning: %s"%(os.getpid(), -np.inf)) 

return -np.inf, [] 

 

def createChainContext(self, p): 

""" 

Returns a new instance of a chain context  

""" 

try: 

p = Params(*zip(self.params.keys, p)) 

except Exception: 

# no params or params has no keys 

pass 

return ChainContext(self, p) 

 

def invokeCoreModules(self, ctx): 

""" 

Iterates thru the core modules and invokes them 

""" 

for cModule in self.getCoreModules(): 

self.invokeCoreModule(cModule, ctx) 

 

 

def invokeCoreModule(self, coreModule, ctx): 

""" 

Invokes the given module with the given ChainContext 

""" 

coreModule(ctx) 

 

 

def computeLikelihoods(self, ctx): 

""" 

Computes the likelihoods by iterating thru all the modules. 

Sums up the log likelihoods. 

""" 

likelihood = 0 

 

for lModule in self.getLikelihoodModules(): 

likelihood += self.invokeLikelihoodModule(lModule, ctx) 

 

return likelihood 

 

def invokeLikelihoodModule(self, likelihoodModule, ctx): 

""" 

Invokes the given module with the given ChainContext 

""" 

return likelihoodModule.computeLikelihood(ctx) 

 

def __str__(self, *args, **kwargs): 

s = "Core Modules: \n " 

s = s + "\n ".join([type(o).__name__ for o in self.getCoreModules()]) 

 

s = s + "\nLikelihood Modules: \n " 

s = s + "\n ".join([type(o).__name__ for o in self.getLikelihoodModules()]) 

return s