-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdo_bkg_llhs.py
218 lines (143 loc) · 5.79 KB
/
do_bkg_llhs.py
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
import numpy as np
from astropy.io import fits
import os
import argparse
import logging, traceback
from bkg_rate_estimation import rate_obj_from_sqltab
from sqlite_funcs import get_conn
from dbread_funcs import get_rate_fits_tab, guess_dbfname
from config import EBINS0, EBINS1
# need to read rate fits from DB
# and read twinds
# and read/get event, dmask, and ebins
# then get bkg_llh_obj and a minimizer
# then loop over all time windows
# minimizing nllh and recording bf params
def cli():
parser = argparse.ArgumentParser()
parser.add_argument('--evfname', type=str,\
help="Event data file",
default=None)
parser.add_argument('--dmask', type=str,\
help="Detmask fname",
default=None)
parser.add_argument('--dbfname', type=str,\
help="Name to save the database to",\
default=None)
args = parser.parse_args()
return args
def do_bkg_analysis_mp(i_proc, nprocs):
if args.dbfname is None:
db_fname = guess_dbfname()
if isinstance(db_fname, list):
db_fname = db_fname[0]
else:
db_fname = args.dbfname
logging.info('Connecting to DB')
conn = get_conn(db_fname)
info_tab = get_info_tab(conn)
logging.info('Got info table')
files_tab = get_files_tab(conn)
logging.info('Got files table')
trigtime = info_tab['trigtimeMET'][0]
evfname = files_tab['evfname'][0]
ev_data = fits.open(evfname)[1].data
dmask = files_tab['detmask'][0].data
bl_dmask = (dmask==0.0)
logging.debug('Opened up event and detmask files')
ebins0 = np.array(EBINS0)
ebins1 = np.array(EBINS1)
logging.debug("ebins0")
logging.debug(ebins0)
logging.debug("ebins1")
logging.debug(ebins1)
# probably get times from twind table
rate_fits_df = get_rate_fits_tab(conn)
bkg_rates_obj = rate_obj_from_sqltab(rate_fits_df, 0, 1)
twind_df = get_twinds_tab(conn)
logging.info("Got TimeWindows table")
logging.info("Getting rate fits from DB")
min_bin_size = np.min(twind_df['duration'])
logging.info("Smallest duration to test is %.3fs" %(min_bin_size))
llh_bkg = get_bkg_llh_obj(ev_data, ebins0, ebins1, bl_dmask,\
bkg_rates_obj, twind_df['time'].values[0],\
min_bin_size)
miner.set_llh(llh_bkg)
t_bins0 = twind_df['time'].values
t_bins1 = twind_df['time_end'].values
t_bins0 = t_bins0[i_proc:][::nprocs]
t_bins1 = t_bins1[i_proc:][::nprocs]
ntbins = len(t_bins0)
logging.debug("There are %d time0 bins" %(len(t_bins0)))
logging.debug("There are %d time1 bins" %(len(t_bins1)))
logging.debug("min(t_bins0), max(t_bins0): %.3f, %.3f" %(np.min(t_bins0),np.max(t_bins0)))
logging.debug("min(t_bins1), max(t_bins1): %.3f, %.3f" %(np.min(t_bins1),np.max(t_bins1)))
for i in xrange(ntbins):
# pretty sure I don't have to do miner.set_llh() again
dt = t_bins1[i] - t_bins0[i]
llh_bkg.set_time(t_bins0[i], dt)
bf_vals, bf_nllhs, ress = miner.minimize()
# then write the result
def main(args):
logging.basicConfig(filename='bkg_llh_analysis.log', level=logging.DEBUG,\
format='%(asctime)s-' '%(levelname)s- %(message)s')
# probably want to move this entire thing to a seperate function
# so that they can be launched as seperate processes
if args.dbfname is None:
db_fname = guess_dbfname()
if isinstance(db_fname, list):
db_fname = db_fname[0]
else:
db_fname = args.dbfname
logging.info('Connecting to DB')
conn = get_conn(db_fname)
info_tab = get_info_tab(conn)
logging.info('Got info table')
files_tab = get_files_tab(conn)
logging.info('Got files table')
trigtime = info_tab['trigtimeMET'][0]
evfname = files_tab['evfname'][0]
ev_data = fits.open(evfname)[1].data
dmask = files_tab['detmask'][0].data
bl_dmask = (dmask==0.0)
logging.debug('Opened up event and detmask files')
ebins0 = np.array(EBINS0)
ebins1 = np.array(EBINS1)
logging.debug("ebins0")
logging.debug(ebins0)
logging.debug("ebins1")
logging.debug(ebins1)
# probably get times from twind table
rate_fits_df = get_rate_fits_tab(conn)
bkg_rates_obj = rate_obj_from_sqltab(rate_fits_df, 0, 1)
twind_df = get_twinds_tab(conn)
logging.info("Got TimeWindows table")
logging.info("Getting rate fits from DB")
min_bin_size = np.min(twind_df['duration'])
logging.info("Smallest duration to test is %.3fs" %(min_bin_size))
exp_groups = twind_df.groupby('duration')
nexps = len(exp_groups)
miner = NLLH_ScipyMinimize('')
llh_bkg = get_bkg_llh_obj(ev_data, ebins0, ebins1, bl_dmask,\
bkg_rates_obj, twind_df['time'].values[0],\
min_bin_size)
miner.set_llh(llh_bkg)
for ii, exp_group in enumerate(exp_groups):
logging.info("Starting duration size %d of %d" %(ii+1, nexps))
df_twind = exp_group[1]
t_bins0 = df_twind['time'].values
t_bins1 = df_twind['time_end'].values
dt = t_bins1[0] - t_bins0[0]
ntbins = len(t_bins0)
logging.debug("There are %d time0 bins" %(len(t_bins0)))
logging.debug("There are %d time1 bins" %(len(t_bins1)))
logging.debug("min(t_bins0), max(t_bins0): %.3f, %.3f" %(np.min(t_bins0),np.max(t_bins0)))
logging.debug("min(t_bins1), max(t_bins1): %.3f, %.3f" %(np.min(t_bins1),np.max(t_bins1)))
for i in xrange(ntbins):
# pretty sure I don't have to do miner.set_llh() again
llh_bkg.set_time(t_bins0[i], dt)
bf_vals, bf_nllhs, ress = miner.minimize()
# then write the result
if __name__ == "__main__":
args = cli()
main(args)