def scan_llh(dom_tables_kw, hypo_kw, events_kw, scan_kw): """Script "main" function""" t00 = time.time() scan_values = [] for dim in HYPO_PARAMS_T._fields: val_str = ''.join(scan_kw.pop(dim)) val_str = val_str.lower().replace('pi', format(np.pi, '.17e')) scan_values.append(hrlist2list(val_str)) dom_tables = init_obj.setup_dom_tables(**dom_tables_kw) hypo_handler = init_obj.setup_discrete_hypo(**hypo_kw) events_generator = init_obj.get_events(**events_kw) # Pop 'outdir' from `scan_kw` since we don't want to store this info in # the metadata dict. outdir = expand(scan_kw.pop('outdir')) mkdir(outdir) print('Scanning paramters') t0 = time.time() fast_llh = True if fast_llh: get_llh = dom_tables._get_llh dom_info = dom_tables.dom_info tables = dom_tables.tables table_norm = dom_tables.table_norm t_indep_tables = dom_tables.t_indep_tables t_indep_table_norm = dom_tables.t_indep_table_norm sd_idx_table_indexer = dom_tables.sd_idx_table_indexer metric_kw = {} def metric_wrapper(hypo, hits, hits_indexer, unhit_sd_indices, time_window): sources = hypo_handler.get_sources(hypo) return get_llh(sources=sources, hits=hits, hits_indexer=hits_indexer, unhit_sd_indices=unhit_sd_indices, sd_idx_table_indexer=sd_idx_table_indexer, time_window=time_window, dom_info=dom_info, tables=tables, table_norm=table_norm, t_indep_tables=t_indep_tables, t_indep_table_norm=t_indep_table_norm) else: metric_kw = dict(dom_tables=dom_tables, tdi_table=None) get_llh = likelihood.get_llh def metric_wrapper(hypo, **metric_kw): sources = hypo_handler.get_sources(hypo) return get_llh(sources=sources, **metric_kw) n_points_total = 0 metric_vals = [] for _, event in events_generator: hits = event['hits'] hits_indexer = event['hits_indexer'] hits_summary = event['hits_summary'] metric_kw['hits'] = hits metric_kw['hits_indexer'] = hits_indexer hit_sd_indices = hits_indexer['sd_idx'] unhit_sd_indices = np.array(sorted( ALL_STRS_DOMS_SET.difference(hit_sd_indices)), dtype=np.uint32) metric_kw['unhit_sd_indices'] = unhit_sd_indices metric_kw['time_window'] = np.float32( hits_summary['time_window_stop'] - hits_summary['time_window_start']) t1 = time.time() metric_vals.append(scan(scan_values, metric_wrapper, metric_kw)) dt = time.time() - t1 n_points = metric_vals[-1].size n_points_total += n_points print(' ---> {:.3f} s, {:d} points ({:.3f} ms per LLH)'.format( dt, n_points, dt / n_points * 1e3)) dt = time.time() - t0 info = OrderedDict([ ('hypo_params', HYPO_PARAMS_T._fields), ('scan_values', scan_values), ('metric_name', 'llh'), ('metric_vals', metric_vals), ('scan_kw', sort_dict(scan_kw)), ('dom_tables_kw', sort_dict(dom_tables_kw)), ('hypo_kw', sort_dict(hypo_kw)), ('events_kw', sort_dict(events_kw)), ]) outfpath = join(outdir, 'scan.pkl') print('Saving results in pickle file, path "{}"'.format(outfpath)) pickle.dump(info, open(outfpath, 'wb'), protocol=pickle.HIGHEST_PROTOCOL) print('Total time to scan: {:.3f} s; {:.3f} ms avg per LLH'.format( time.time() - t00, dt / n_points_total * 1e3)) return metric_vals, info
spec['y_max'] = float(y_max) spec['n_y'] = int(n_y) spec['z_min'] = float(z_min) spec['z_max'] = float(z_max) spec['n_z'] = int(n_z) spec['costhetadir_min'] = -1 spec['costhetadir_max'] = 1 spec['n_costhetadir'] = int(n_costhetadir) spec['phidir_min'] = -np.pi spec['phidir_max'] = np.pi spec['n_phidir'] = int(n_phidir) specs.append(spec) hypo_handler = setup_discrete_hypo( cascade_kernel=None, track_kernel='table_e_loss', track_time_step=3, ) all_sources = hypo_handler.get_generic_sources(SIM['mc_true_params']) assert len(all_sources) > 0 print('len(all_sources):', len(all_sources)) n_costhetadir = specs[0]['n_costhetadir'] costhetadir_bin_edges = np.linspace( start=specs[0]['costhetadir_min'], stop=specs[0]['costhetadir_max'], num=n_costhetadir, )
# tables=kwargs['dom_tables_kw']['dom_tables_kind'], # norm=kwargs['dom_tables_kw']['norm_version'], # no_dir_str='no_dir_' if not kwargs['dom_tables_kw']['use_directionality'] else '', # cone_str=( # 'sigma{}deg_{}phi_'.format(kwargs['dom_tables_kw']['ckv_sigma_deg'], kwargs['dom_tables_kw']['ckv_sigma_deg']) # if (kwargs['dom_tables_kw']['use_directionality'] and # kwargs['dom_tables_kw']['dom_tables_kind'] in ['raw_uncompr', 'raw_templ_compr']) # else '' # ), # dedx_str='dedx_' if kwargs['hypo_kw']['track_kernel'] == 'table_e_loss' else '', # muon_dt=kwargs['hypo_kw']['track_time_step'] # #) dom_tables = init_obj.setup_dom_tables(**kwargs['dom_tables_kw']) hypo_handler = init_obj.setup_discrete_hypo(**kwargs['hypo_kw']) sim = SIMULATIONS[kwargs['other_kw']['sim_to_test']] outdir = expand(kwargs['other_kw'].pop('outdir')) fwd_sim_dir = '/data/icecube/retro/sims/' sim['fwd_sim_histo_file'] = join(fwd_sim_dir, sim['fwd_sim_histo_file']) fwd_sim_histo_file_md5 = None if sim['fwd_sim_histo_file'] is not None: print('Loading and hashing forward simulation histograms from "%s"...' % sim['fwd_sim_histo_file']) contents = open(expand(sim['fwd_sim_histo_file']), 'rb').read() fwd_sim_histo_file_md5 = hashlib.md5(contents).hexdigest() fwd_sim_histos = pickle.loads(contents) bin_edges = fwd_sim_histos['bin_edges'] bin_centers = 0.5 * (bin_edges[:-1] + bin_edges[1:])
t=0, x=0, y=0, z=-400, track_azimuth=0, track_zenith=np.pi, # cascade_azimuth=0, cascade_zenith=np.pi, track_energy=0, cascade_energy=20), fwd_sim_histo_file= 'EMinus_energy20_x0_y0_z-400_cz-1.0_az0_ice_spice_mie_holeice_as.h2-50cm_gcd_md5_14bd15d0_geant_false_nsims1000000_step1_photon_histos_0-4000ns_400bins.pkl' ), ) dom_tables = init_obj.setup_dom_tables(**dom_tables_kw) hypo_handler = init_obj.setup_discrete_hypo(**hypo_kw) sim = SIMULATIONS[pdf_kw['sim_to_test']] outdir = expand(pdf_kw.pop('outdir')) fwd_sim_dir = '/data/icecube/retro/sims/' sim['fwd_sim_histo_file'] = join(fwd_sim_dir, sim['fwd_sim_histo_file']) fwd_sim_histo_file_md5 = None if sim['fwd_sim_histo_file'] is not None: print('Loading and hashing forward simulation histograms from "%s"...' % sim['fwd_sim_histo_file']) t0 = time.time() contents = open(expand(sim['fwd_sim_histo_file']), 'rb').read() fwd_sim_histo_file_md5 = hashlib.md5(contents).hexdigest() fwd_sim_histos = pickle.loads(contents) bin_edges = fwd_sim_histos['bin_edges']
def reco(dom_tables_kw, hypo_kw, events_kw, reco_kw): """Script "main" function""" t00 = time.time() dom_tables = init_obj.setup_dom_tables(**dom_tables_kw) hypo_handler = init_obj.setup_discrete_hypo(**hypo_kw) events_iterator = init_obj.get_events(**events_kw) print('Running reconstructions...') spatial_prior_orig = reco_kw.pop('spatial_prior').strip() spatial_prior_name = spatial_prior_orig.lower() if spatial_prior_name == 'ic': x_prior = (PRI_UNIFORM, (-860, 870)) y_prior = (PRI_UNIFORM, (-780, 770)) z_prior = (PRI_UNIFORM, (-780, 790)) elif spatial_prior_name == 'dc': x_prior = (PRI_UNIFORM, (-150, 270)) y_prior = (PRI_UNIFORM, (-210, 150)) z_prior = (PRI_UNIFORM, (-770, 760)) elif spatial_prior_name == 'dc_subdust': x_prior = (PRI_UNIFORM, (-150, 270)) y_prior = (PRI_UNIFORM, (-210, 150)) z_prior = (PRI_UNIFORM, (-610, -60)) elif spatial_prior_name == 'spefit2': x_prior = ( PRI_SPEFIT2, ( # scipy.stats.cauchy loc, scale parameters -0.19687812829978152, 14.282171566308806, # Hard limits -600, 750)) y_prior = ( PRI_SPEFIT2, ( # scipy.stats.cauchy loc, scale parameters -0.2393645701205161, 15.049528023495354, # Hard limits -750, 650)) z_prior = ( PRI_SPEFIT2, ( # scipy.stats.cauchy loc, scale parameters -5.9170661027492546, 12.089399308036718, # Hard limits -1200, 200)) else: raise ValueError( 'Spatial prior "{}" not recognized'.format(spatial_prior_orig)) temporal_prior_orig = reco_kw.pop('temporal_prior').strip() temporal_prior_name = temporal_prior_orig.lower() if temporal_prior_name == PRI_UNIFORM: time_prior = (PRI_UNIFORM, (-4e3, 0.0)) elif temporal_prior_name == PRI_SPEFIT2: time_prior = ( PRI_SPEFIT2, ( # scipy.stats.cauchy loc (rel to SPEFit2 time), scale -82.631395081663754, 75.619895703067343, # Hard limits (relative to left, right edges of window, # respectively) -4e3, 0.0)) else: raise ValueError( 'Temporal prior "{}" not recognized'.format(temporal_prior_orig)) energy_prior_name = reco_kw.pop('energy_prior') energy_lims = reco_kw.pop('energy_lims') if energy_prior_name == PRI_UNIFORM: energy_prior = (PRI_UNIFORM, (np.min(energy_lims), np.max(energy_lims))) elif energy_prior_name == PRI_LOG_UNIFORM: energy_prior = (PRI_LOG_UNIFORM, (np.min(energy_lims), np.max(energy_lims))) elif energy_prior_name == PRI_LOG_NORMAL: energy_prior = ( PRI_LOG_NORMAL, ( # scipy.stats.lognorm 3 paramters 0.96251341305506233, 0.4175592980195757, 17.543915051586644, # hard limits np.min(energy_lims), np.max(energy_lims))) else: raise ValueError(str(energy_prior_name)) priors = OrderedDict([('time', time_prior), ('x', x_prior), ('y', y_prior), ('z', z_prior), ('track_zenith', (PRI_COSINE, (-1, 1))), ('track_azimuth', (PRI_UNIFORM, (0, TWO_PI))), ('energy', energy_prior), ('track_fraction', (PRI_UNIFORM, (0, 1)))]) if HYPO_PARAMS_T is HypoParams10D: priors['cascade_zenith'] = (PRI_COSINE, (-1, 1)) priors['cascade_azimuth'] = (PRI_UNIFORM, (0, TWO_PI)) for event_idx, event in events_iterator: # pylint: disable=unused-variable t1 = time.time() if 'mc_truth' in event: print(event['mc_truth']) llhp, _ = run_multinest(event_idx=event_idx, event=event, dom_tables=dom_tables, hypo_handler=hypo_handler, priors=priors, **reco_kw) dt = time.time() - t1 n_points = llhp.size print(' ---> {:.3f} s, {:d} points ({:.3f} ms per LLH)'.format( dt, n_points, dt / n_points * 1e3)) print('Total script run time is {:.3f} s'.format(time.time() - t00))