def assign_likelihood_ratios_xml(xmldoc, coinc_def_id, offset_vectors, vetoseglists, events_func, veto_func, ln_likelihood_ratio_func, likelihood_params_func, verbose = False, params_func_extra_args = ()): """ Assigns likelihood ratio values to coincidences (XML version). """ # # Iterate over all coincs, assigning likelihood ratios. # coinc_event_table = lsctables.CoincTable.get_table(xmldoc) if verbose: progressbar = ProgressBar("computing likelihood ratios", max = len(coinc_event_table)) else: progressbar = None for coinc_event in coinc_event_table: if progressbar is not None: progressbar.increment() if coinc_event.coinc_def_id != coinc_def_id: continue params = likelihood_params_func([event for event in events_func(None, coinc_event.coinc_event_id) if veto_func(event, vetoseglists)], offset_vectors[coinc_event.time_slide_id], *params_func_extra_args) coinc_event.likelihood = ln_likelihood_ratio_func(params) if params is not None else None del progressbar # # Done # return
def ligolw_inspinjfind(xmldoc, process, search, snglcomparefunc, nearcoinccomparefunc, end_time_bisect_window = 1.0, verbose = False): # # Analyze the document's contents. # if verbose: print("indexing ...", file=sys.stderr) bbdef = {"inspiral": thinca.InspiralCoincDef}[search] sbdef = {"inspiral": InspiralSICoincDef}[search] scedef = {"inspiral": InspiralSCExactCoincDef}[search] scndef = {"inspiral": InspiralSCNearCoincDef}[search] contents = DocContents(xmldoc = xmldoc, bbdef = bbdef, sbdef = sbdef, scedef = scedef, scndef = scndef, process = process, end_time_bisect_window = end_time_bisect_window) # # Find sim_inspiral <--> sngl_inspiral coincidences. # progressbar = ProgressBar(max = len(contents.siminspiraltable), textwidth = 35, text = sbdef.description) if verbose else None for sim in contents.siminspiraltable: if progressbar is not None: progressbar.increment() inspirals = find_sngl_inspiral_matches(contents, sim, snglcomparefunc) if inspirals: add_sim_inspiral_coinc(contents, sim, inspirals) del progressbar # # Find sim_inspiral <--> coinc_event coincidences. # if contents.scn_coinc_def_id: progressbar = ProgressBar(max = len(contents.siminspiraltable), textwidth = 35, text = scndef.description) if verbose else None for sim in contents.siminspiraltable: if progressbar is not None: progressbar.increment() coincs = contents.coincs_near_endtime(sim.time_geocent) exact_coinc_event_ids = find_exact_coinc_matches(coincs, sim, snglcomparefunc) near_coinc_event_ids = find_near_coinc_matches(coincs, sim, nearcoinccomparefunc) assert exact_coinc_event_ids.issubset(near_coinc_event_ids) if exact_coinc_event_ids: add_sim_coinc_coinc(contents, sim, exact_coinc_event_ids, contents.sce_coinc_def_id) if near_coinc_event_ids: add_sim_coinc_coinc(contents, sim, near_coinc_event_ids, contents.scn_coinc_def_id) del progressbar # # Restore the original event order. # if verbose: print("finishing ...", file=sys.stderr) contents.sort_triggers_by_id() # # Done. # return xmldoc
def ligolw_inspinjfind(xmldoc, process, search, snglcomparefunc, nearcoinccomparefunc, end_time_bisect_window = 1.0, verbose = False): # # Analyze the document's contents. # if verbose: print >>sys.stderr, "indexing ..." bbdef = {"inspiral": ligolw_thinca.InspiralCoincDef}[search] sbdef = {"inspiral": InspiralSICoincDef}[search] scedef = {"inspiral": InspiralSCExactCoincDef}[search] scndef = {"inspiral": InspiralSCNearCoincDef}[search] contents = DocContents(xmldoc = xmldoc, bbdef = bbdef, sbdef = sbdef, scedef = scedef, scndef = scndef, process = process, end_time_bisect_window = end_time_bisect_window) # # Find sim_inspiral <--> sngl_inspiral coincidences. # progressbar = ProgressBar(max = len(contents.siminspiraltable), textwidth = 35, text = sbdef.description) if verbose else None for sim in contents.siminspiraltable: if progressbar is not None: progressbar.increment() inspirals = find_sngl_inspiral_matches(contents, sim, snglcomparefunc) if inspirals: add_sim_inspiral_coinc(contents, sim, inspirals) del progressbar # # Find sim_inspiral <--> coinc_event coincidences. # if contents.scn_coinc_def_id: progressbar = ProgressBar(max = len(contents.siminspiraltable), textwidth = 35, text = scndef.description) if verbose else None for sim in contents.siminspiraltable: if progressbar is not None: progressbar.increment() coincs = contents.coincs_near_endtime(sim.get_end()) exact_coinc_event_ids = find_exact_coinc_matches(coincs, sim, snglcomparefunc) near_coinc_event_ids = find_near_coinc_matches(coincs, sim, nearcoinccomparefunc) assert exact_coinc_event_ids.issubset(near_coinc_event_ids) if exact_coinc_event_ids: add_sim_coinc_coinc(contents, sim, exact_coinc_event_ids, contents.sce_coinc_def_id) if near_coinc_event_ids: add_sim_coinc_coinc(contents, sim, near_coinc_event_ids, contents.scn_coinc_def_id) del progressbar # # Restore the original event order. # if verbose: print >>sys.stderr, "finishing ..." contents.sort_triggers_by_id() # # Done. # return xmldoc
def make_db(db_filename, filenames): # Make the sqlite database if one does not exist connection = sqlite3.connect(db_filename) cursor = connection.cursor() # Create Table 1 that contains: # - the filenames of the hdf5 subbank files # - their respective ID integers cursor.execute("CREATE TABLE fname (filename_id INTEGER PRIMARY KEY, filename TEXT);") # Create Table 2 that contains: # - k (integers which will be populated later, this column gives the order in which overlaps are placed), # - ID integers of the filenames (instead of saving the entire filename to the database - this is to save memory) # - m1, m2, chi1, chi2 of templates (separate column for each) # - row and column in which this overlap is found in the file (if row=None, means that the overlaps are found in the columns only) cursor.execute("CREATE TABLE bank (k INTEGER, filename_id INTEGER, m1 REAL, m2 REAL, chi1 REAL, chi2 REAL, row INTEGER, column INTEGER);") # Populate Table 1: for filename in filenames: cursor.execute("INSERT INTO fname (filename) VALUES (?)", (filename,)) connection.commit() cursor.execute("CREATE INDEX filename_index ON fname (filename)") progress = ProgressBar(max=len(filenames)) # Populate Table 2: for filename in filenames: progress.increment(text=filename) try: f = h5py.File(filename,"r") nrows = f[f.keys()[0]]['overlaps'].shape[0] # find number of rows in subbank file for i, (m1, m2, chi1, chi2) in enumerate(zip(f[f.keys()[0]]['mass1'].value, f[f.keys()[0]]['mass2'].value, f[f.keys()[0]]['spin1z'].value, f[f.keys()[0]]['spin2z'].value)): cursor.execute("INSERT INTO bank (filename_id, m1, m2, chi1, chi2, row, column) VALUES ((SELECT filename_id FROM fname WHERE filename = ?),?,?,?,?,?,?)", (filename, m1, m2, chi1, chi2, i if i <= nrows else None, i)) #FIXME: After building the database, confirm that each template only appears once in one row of the hdf5 files except: # ignore corrupted/broken subbank files print "Cannot load h5py file:", filename cursor.execute("CREATE INDEX template_index ON bank (m1, m2, chi1, chi2)") cursor.execute("CREATE INDEX filename_id_index ON bank (filename_id)") cursor.execute("CREATE TEMPORARY TABLE template AS SELECT DISTINCT m1,m2,chi1,chi2 FROM bank;") cursor.execute("CREATE INDEX tmp ON template (m1,m2,chi1,chi2);") cursor.execute("UPDATE bank SET k = (SELECT rowid-(SELECT MIN(rowid) FROM template) FROM template WHERE m1 = bank.m1 and m2 = bank.m2 and chi1 = bank.chi1 and chi2 = bank.chi2);") # populate column k cursor.execute("DROP TABLE template;") cursor.execute("CREATE INDEX k_index ON bank (k);") connection.commit() return connection
def make_db(db_filename, filenames): # Make the sqlite database if one does not exist connection = sqlite3.connect(db_filename) cursor = connection.cursor() # Create Table 1 that contains: # - the filenames of the hdf5 subbank files # - their respective ID integers cursor.execute("CREATE TABLE fname (filename_id INTEGER PRIMARY KEY, filename TEXT);") # Create Table 2 that contains: # - k (integers which will be populated later, this column gives the order in which overlaps are placed), # - ID integers of the filenames (instead of saving the entire filename to the database - this is to save memory) # - m1, m2, chi1, chi2 of templates (separate column for each) # - row and column in which this overlap is found in the file (if row=None, means that the overlaps are found in the columns only) cursor.execute("CREATE TABLE bank (k INTEGER, filename_id INTEGER, m1 REAL, m2 REAL, chi1 REAL, chi2 REAL, row INTEGER, column INTEGER);") # Populate Table 1: for filename in filenames: cursor.execute("INSERT INTO fname (filename) VALUES (?)", (filename,)) connection.commit() cursor.execute("CREATE INDEX filename_index ON fname (filename)") progress = ProgressBar(max=len(filenames)) # Populate Table 2: for filename in filenames: progress.increment(text=filename) try: f = h5py.File(filename,"r") nrows = f[f.keys()[0]]['overlaps'].shape[0] # find number of rows in subbank file for i, (m1, m2, chi1, chi2) in enumerate(zip(f[f.keys()[0]]['mass1'].value, f[f.keys()[0]]['mass2'].value, f[f.keys()[0]]['spin1z'].value, f[f.keys()[0]]['spin2z'].value)): cursor.execute("INSERT INTO bank (filename_id, m1, m2, chi1, chi2, row, column) VALUES ((SELECT filename_id FROM fname WHERE filename = ?),?,?,?,?,?,?)", (filename, m1, m2, chi1, chi2, i if i <= nrows else None, i)) #FIXME: After building the database, confirm that each template only appears once in one row of the hdf5 files except: # ignore corrupted/broken subbank files print "Cannot load h5py file:", filename cursor.execute("CREATE INDEX template_index ON bank (m1, m2, chi1, chi2)") cursor.execute("CREATE INDEX filename_id_index ON bank (filename_id)") cursor.execute("CREATE TEMPORARY TABLE template AS SELECT DISTINCT m1,m2,chi1,chi2 FROM bank;") cursor.execute("CREATE INDEX tmp ON template (m1,m2,chi1,chi2);") cursor.execute("UPDATE bank SET k = (SELECT rowid-(SELECT MIN(rowid) FROM template) FROM template WHERE m1 = bank.m1 and m2 = bank.m2 and chi1 = bank.chi1 and chi2 = bank.chi2);") # populate column k cursor.execute("DROP TABLE template;") cursor.execute("CREATE INDEX k_index ON bank (k);") connection.commit() return connection
ln_p_j, p_j_templates, p_j_indices = find_ln_p_j_voronoi( mass, f, popt ) #p_j_indices = indices of the templates for m1, m2 arrays (eg. p_j_indices[0] = 1, which means p_j_template[0] corresponds to the 1th template in m1, m2) t_k = np.array(range(len(mass))) ln_p_jk = np.log(np.zeros( (len(rho), len(t_k)))) # P(signal t_j is recovered by template t_k) #p_j_indices.tolist().sort(key=ln_p_j.__getitem__) # doing loop in order of p_j (smallest to largest) for numerical accuracy order = np.argsort(ln_p_j) ln_p_j, p_j_templates, p_j_indices = ln_p_j[order], p_j_templates[ order], p_j_indices[order] progress = ProgressBar(max=len(p_j_indices)) for i in range(len(p_j_indices)): # loop over all signal population progress.increment(text=str(p_j_templates[order][i])) ovrlp = overlap[p_j_indices[i]] for r in range(len(rho)): # loop over all rho ln_p_jk[r, :] = np.logaddexp( ln_p_jk[r, :], ln_p_j[order][i] + ln_p_k(ovrlp, rho[r], t_k)) print "ln(P_jk) computed for all templates. Time elapsed:", time.time( ) - start_time # Save data to hdf5 file if save_data: directory = "" filename = "logP_vs_rho_" + datetime f = h5py.File(directory + filename + ".hdf5", "w") f.create_dataset('rho', data=rho) f.create_dataset('ln_P_jk', data=ln_p_jk)
k = np.arange(1,n+1) rho = 30*np.cos((2*k-1)*np.pi/(2*n)) + 30 # min(rho)~0, max(rho)~80 rho.sort() rho = np.insert(rho,0,0) ln_p_j, p_j_templates, p_j_indices = find_ln_p_j_voronoi(mass, f, popt) #p_j_indices = indices of the templates for m1, m2 arrays (eg. p_j_indices[0] = 1, which means p_j_template[0] corresponds to the 1th template in m1, m2) t_k = np.array(range(len(mass))) ln_p_jk = np.log(np.zeros((len(rho), len(t_k)))) # P(signal t_j is recovered by template t_k) #p_j_indices.tolist().sort(key=ln_p_j.__getitem__) # doing loop in order of p_j (smallest to largest) for numerical accuracy order = np.argsort(ln_p_j) ln_p_j, p_j_templates, p_j_indices = ln_p_j[order], p_j_templates[order], p_j_indices[order] progress = ProgressBar(max=len(p_j_indices)) for i in range(len(p_j_indices)): # loop over all signal population progress.increment(text=str(p_j_templates[order][i])) ovrlp = overlap[p_j_indices[i]] for r in range(len(rho)): # loop over all rho ln_p_jk[r,:] = np.logaddexp(ln_p_jk[r,:], ln_p_j[order][i]+ln_p_k(ovrlp, rho[r], t_k)) print "ln(P_jk) computed for all templates. Time elapsed:", time.time()-start_time # Save data to hdf5 file if save_data: directory = "" filename = "logP_vs_rho_"+datetime f = h5py.File(directory+filename+".hdf5","w") f.create_dataset('rho', data=rho) f.create_dataset('ln_P_jk', data=ln_p_jk) f.create_dataset('masses', data=mass) f.close()
img = Image.open(filename) width, height = img.size width, height = int(round(width * options.height / float(height))), options.height if options.verbose: print("converting to %dx%d grayscale ... " % (width, height), file=sys.stderr) img = img.resize((width, height)).convert("L") progress = ProgressBar("computing pixels", max=width * height) if options.verbose else None for i in range(width): for j in range(height): if progress is not None: progress.increment() # amplitude. hrss column is ignored by waveform # generation code. it is included for convenience, # to record the desired pixel brightness. because # band- and time-limited white-noise burst # waveforms are random, the waveform's final # ampltiude (in the egw_over_rsquared column) is # determined by generating the burst at a canonical # amplitude, measuring its hrss, then rescaling to # achieve the desired value. this process requires # the final sample rate to be known. hrss = options.hrss_scale * img.getpixel( (i, height - 1 - j)) / 255.0 if hrss == 0.: # don't generate injections for black # pixels