def get_template(self, params, return_stages=False): ''' Runs entire template-making chain, using parameters found in 'params' dict. If 'return_stages' is set to True, returns output from each stage as a simple tuple. ''' logging.info("STAGE 1: Getting Atm Flux maps...") with Timer() as t: flux_maps = get_flux_maps(self.flux_service, self.ebins, self.czbins, **params) tprofile.debug("==> elapsed time for flux stage: %s sec" % t.secs) logging.info("STAGE 2: Getting osc prob maps...") with Timer() as t: osc_flux_maps = get_osc_flux(flux_maps, self.osc_service, oversample_e=self.oversample_e, oversample_cz=self.oversample_cz, **params) tprofile.debug("==> elapsed time for oscillations stage: %s sec" % t.secs) logging.info("STAGE 3: Getting event rate true maps...") with Timer() as t: event_rate_maps = get_event_rates(osc_flux_maps, self.aeff_service, **params) tprofile.debug("==> elapsed time for aeff stage: %s sec" % t.secs) logging.info("STAGE 4: Getting event rate reco maps...") with Timer() as t: event_rate_reco_maps = get_reco_maps(event_rate_maps, self.reco_service, **params) tprofile.debug("==> elapsed time for reco stage: %s sec" % t.secs) logging.info("STAGE 5: Getting pid maps...") with Timer(verbose=False) as t: final_event_rate = get_pid_maps(event_rate_reco_maps, self.pid_service) tprofile.debug("==> elapsed time for pid stage: %s sec" % t.secs) if not return_stages: return final_event_rate # Otherwise, return all stages as a simple tuple return (flux_maps, osc_flux_maps, event_rate_maps, event_rate_reco_maps, final_event_rate)
def get_osc_prob_maps(self, **kwargs): """ Returns an oscillation probability map dictionary calculated at the values of the input parameters: deltam21,deltam31,theta12,theta13,theta23,deltacp for flavor_from to flavor_to, with the binning of ebins,czbins. The dictionary is formatted as: 'nue_maps': {'nue':map,'numu':map,'nutau':map}, 'numu_maps': {...} 'nue_bar_maps': {...} 'numu_bar_maps': {...} NOTES: * expects all angles in [rad] * this method doesn't calculate the oscillation probabilities itself, but calls get_osc_probLT_dict internally, to get a high resolution map of the oscillation probs, """ #Get the finely binned maps as implemented in the derived class logging.info('Retrieving finely binned maps') with Timer(verbose=False) as t: fine_maps = self.get_osc_probLT_dict(**kwargs) print " ==> elapsed time to get all fine maps: %s sec" % t.secs logging.info("Smoothing fine maps...") smoothed_maps = {} smoothed_maps['ebins'] = self.ebins smoothed_maps['czbins'] = self.czbins with Timer(verbose=False) as t: for from_nu, tomap_dict in fine_maps.items(): if 'vals' in from_nu: continue new_tomaps = {} for to_nu, pvals in tomap_dict.items(): logging.debug("Getting smoothed map %s/%s" % (from_nu, to_nu)) new_tomaps[to_nu] = get_smoothed_map( pvals, fine_maps['evals'], fine_maps['czvals'], self.ebins, self.czbins) smoothed_maps[from_nu] = new_tomaps tprofile.debug(" ==> elapsed time to smooth maps: %s sec" % t.secs) return smoothed_maps
def get_osc_prob_maps(self, **kwargs): """ Returns an oscillation probability map dictionary calculated at the values of the input parameters: deltam21,deltam31,theta12,theta13,theta23,deltacp for flavor_from to flavor_to, with the binning of ebins,czbins. The dictionary is formatted as: 'nue_maps': {'nue':map,'numu':map,'nutau':map}, 'numu_maps': {...} 'nue_bar_maps': {...} 'numu_bar_maps': {...} NOTES: * expects all angles in [rad] * this method doesn't calculate the oscillation probabilities itself, but calls get_osc_probLT_dict internally, to get a high resolution map of the oscillation probs, """ #Get the finely binned maps as implemented in the derived class logging.info('Retrieving finely binned maps') with Timer(verbose=False) as t: fine_maps = self.get_osc_probLT_dict(**kwargs) print " ==> elapsed time to get all fine maps: %s sec"%t.secs logging.info("Smoothing fine maps...") smoothed_maps = {} smoothed_maps['ebins'] = self.ebins smoothed_maps['czbins'] = self.czbins with Timer(verbose=False) as t: for from_nu, tomap_dict in fine_maps.items(): if 'vals' in from_nu: continue new_tomaps = {} for to_nu, pvals in tomap_dict.items(): logging.debug("Getting smoothed map %s/%s"%(from_nu,to_nu)) new_tomaps[to_nu] = get_smoothed_map( pvals,fine_maps['evals'],fine_maps['czvals'], self.ebins, self.czbins) smoothed_maps[from_nu] = new_tomaps tprofile.debug(" ==> elapsed time to smooth maps: %s sec"%t.secs) return smoothed_maps
def test_fbwkde(): """Test speed of fbwkde implementation""" n_samp = int(1e4) n_dct = int(2**12) n_eval = int(1e4) x = np.linspace(0, 20, n_eval) np.random.seed(0) times = [] for _ in range(3): enuerr = np.random.noncentral_chisquare(df=3, nonc=1, size=n_samp) t0 = time() fbwkde(data=enuerr, n_dct=n_dct, evaluate_at=x) times.append(time() - t0) tprofile.debug( 'median time to run fbwkde, %d samples %d dct, eval. at %d points: %f' ' ms', n_samp, n_dct, n_eval, np.median(times) * 1000) logging.info('<< PASS : test_fbwkde >>')
def test_vbwkde(): """Test speed of unweighted vbwkde implementation""" n_samp = int(1e4) n_dct = int(2**12) n_eval = int(5e3) n_addl = 0 x = np.linspace(0, 20, n_samp) np.random.seed(0) times = [] for _ in range(3): enuerr = np.random.noncentral_chisquare(df=3, nonc=1, size=n_eval) t0 = time() vbwkde(data=enuerr, n_dct=n_dct, evaluate_at=x, n_addl_iter=n_addl) times.append(time() - t0) tprofile.debug( 'median time to run vbwkde, %d samples %d dct %d addl iter, eval. at' ' %d points: %f ms', n_samp, n_dct, n_addl, n_eval, np.median(times) * 1e3) logging.info('<< PASS : test_vbwkde >>')
def get_template_no_osc(self, params): ''' Runs template making chain, but without oscillations ''' logging.info("STAGE 1: Getting Atm Flux maps...") with Timer() as t: flux_maps = get_flux_maps(self.flux_service, self.ebins, self.czbins, **params) tprofile.debug("==> elapsed time for flux stage: %s sec" % t.secs) # Skipping oscillation stage... logging.info(" >>Skipping Stage 2 in no oscillations case...") flavours = ['nutau', 'nutau_bar'] # Create the empty nutau maps: test_map = flux_maps['nue'] for flav in flavours: flux_maps[flav] = { 'map': np.zeros_like(test_map['map']), 'ebins': np.zeros_like(test_map['ebins']), 'czbins': np.zeros_like(test_map['czbins']) } logging.info("STAGE 3: Getting event rate true maps...") with Timer() as t: event_rate_maps = get_event_rates(flux_maps, self.aeff_service, **params) tprofile.debug("==> elapsed time for aeff stage: %s sec" % t.secs) logging.info("STAGE 4: Getting event rate reco maps...") with Timer() as t: event_rate_reco_maps = get_reco_maps(event_rate_maps, self.reco_service, **params) tprofile.debug("==> elapsed time for reco stage: %s sec" % t.secs) logging.info("STAGE 5: Getting pid maps...") with Timer(verbose=False) as t: final_event_rate = get_pid_maps(event_rate_reco_maps, self.pid_service) tprofile.debug("==> elapsed time for pid stage: %s sec" % t.secs) return final_event_rate
def test_gaussians(): """Test `gaussians` function""" n_gaus = [1, 10, 100, 1000, 10000] n_eval = int(1e4) x = np.linspace(-20, 20, n_eval) np.random.seed(0) mu_sigma_weight_sets = [(np.linspace(-50, 50, n), np.linspace(0.5, 100, n), np.random.rand(n)) for n in n_gaus] timings = OrderedDict() for impl in GAUS_IMPLEMENTATIONS: timings[impl] = [] for mus, sigmas, weights in mu_sigma_weight_sets: if not isinstance(mus, Iterable): mus = [mus] sigmas = [sigmas] weights = [weights] ref_unw = np.sum( [stats.norm.pdf(x, loc=m, scale=s) for m, s in zip(mus, sigmas)], axis=0) / len(mus) ref_w = np.sum([ stats.norm.pdf(x, loc=m, scale=s) * w for m, s, w in zip(mus, sigmas, weights) ], axis=0) / np.sum(weights) for impl in GAUS_IMPLEMENTATIONS: t0 = time() test_unw = gaussians(x, mu=mus, sigma=sigmas, weights=None, implementation=impl) dt_unw = time() - t0 t0 = time() test_w = gaussians(x, mu=mus, sigma=sigmas, weights=weights, implementation=impl) dt_w = time() - t0 timings[impl].append( (np.round(dt_unw * 1000, decimals=3), np.round(dt_w * 1000, decimals=3))) err_msgs = [] if not recursiveEquality(test_unw, ref_unw): err_msgs.append( 'BAD RESULT (unweighted), n_gaus=%d, implementation=' '"%s", max. abs. fract. diff.: %s' % (len(mus), impl, np.max(np.abs((test_unw / ref_unw - 1))))) if not recursiveEquality(test_w, ref_w): err_msgs.append( 'BAD RESULT (weighted), n_gaus=%d, implementation="%s"' ', max. abs. fract. diff.: %s' % (len(mus), impl, np.max(np.abs((test_w / ref_w - 1))))) if err_msgs: for err_msg in err_msgs: logging.error(err_msg) raise ValueError('\n'.join(err_msgs)) tprofile.debug( 'gaussians() timings (unweighted) (Note:OMP_NUM_THREADS=%d; evaluated' ' at %.0e points)', OMP_NUM_THREADS, n_eval) timings_str = ' '.join([format(t, '10d') for t in n_gaus]) tprofile.debug(' ' * 30 + 'Number of gaussians'.center(59)) tprofile.debug(' %15s %s', 'impl.', timings_str) timings_str = ' '.join(['-' * 10 for t in n_gaus]) tprofile.debug(' %15s %s', '-' * 15, timings_str) for impl in GAUS_IMPLEMENTATIONS: # only report timings for unweighted case timings_str = ' '.join([format(t[0], '10.3f') for t in timings[impl]]) tprofile.debug('Timings, %15s (ms): %s', impl, timings_str) logging.info('<< PASS : test_gaussians >>')