import numpy as np import matplotlib.pyplot as plt from fastprof import Model, Data, Parameters, NPMinimizer, OptiMinimizer import copy np.set_printoptions(precision=4, suppress=False, linewidth=180) np.random.seed(4) # ========================================== model = Model().load('run/fastprof/HighMass_NW-1700-log200-noDSig.json') pars0 = model.expected_pars(0.1) data = model.generate_data(pars0) opti = OptiMinimizer(data, 0.1, (0, 20)) t = opti.tmu(0.1) print(opti.hypo_pars) print(model.closure_approx(opti.hypo_pars, data)) print(model.closure_exact(opti.hypo_pars, data)) # ========================================== model2 = model.regularize(2) data2 = copy.copy(data) data2.model = model2 data2.aux_betas = np.zeros(model2.nb) opti2 = OptiMinimizer(data2, 0.1, (0, 20)) t2 = opti2.tmu(0.1) print(opti2.hypo_pars)
import numpy as np import matplotlib.pyplot as plt from fastprof import Model, Data, Parameters, NPMinimizer, OptiMinimizer np.random.seed(7) model = Model().load('fastprof/models/HighMass_NW-700-log500-gammas-only.json') pars0 = model.expected_pars(5) pars = model.expected_pars(5) pars.gammas = np.random.normal(pars.gammas, 1) print('Randomized gammas = ', pars.gammas) model.linear_nps = False data_exp = Data(model).set_expected(pars) model.linear_nps = True data_lin = Data(model).set_expected(pars) npm = NPMinimizer(5, data_lin) print(npm.profile()) min_pars = npm.min_pars plt.ion() plt.show() fig1 = plt.figure(1) fig1.canvas.set_window_title('True pars, data from linear model') model.plot(pars, data_lin, residuals=True) plt.xlim(150,300) fig2 = plt.figure(2)
def run(argv=None): parser = make_parser() options = parser.parse_args() if not options: parser.print_help() sys.exit(0) try: bins = [int(b) for b in options.bins.split(',')] except Exception as inst: print(inst) raise ValueError( 'Invalid bin specification %s : the format should be bin1,bin2,...' % options.bins) if options.yrange != '': try: y_min, y_max = [float(p) for p in options.yrange.split(',')] except Exception as inst: print(inst) raise ValueError( 'Invalid range specification %s, expected y_min,y_max' % options.yrange) model = Model().load(options.model_file) if model is None: raise ValueError('No valid model definition found in file %s.' % options.model_file) if not options.cutoff is None: model.cutoff = options.cutoff if options.channel != None: channel = model.channel(options.channel) if not channel: raise KeyError('Channel %s not found in model.' % options.channel) else: channel = list(model.channels.values())[0] if options.sample != None: sample = channel.sample(options.sample) if not sample: raise KeyError('Sample %s not found in channel %s.' % (options.sample, channel.name)) else: sample = list(channel.samples.values())[0] if options.validation_file is not None: validation_file = options.validation_file else: split_name = os.path.splitext(options.model_file) validation_file = split_name[0] + '_validation' + split_name[1] data = ValidationData(model, validation_file) print('Validating for POI value %s = %g' % (model.poi(0).name, data.poi)) plt.ion() nplots = model.nnps nc = math.ceil(math.sqrt(nplots)) nr = math.ceil(nplots / nc) cont_x = np.linspace(data.points[0], data.points[-1], 100) pars = model.expected_pars(data.poi) channel_offset = model.channel_offsets[channel.name] sample_index = model.sample_indices[sample.name] nexp0 = model.n_exp(pars)[sample_index, channel_offset:channel_offset + channel.nbins()] ax_vars = [] ax_invs = [] def nexp_var(pars, par, x): return model.n_exp(pars.set( par, x))[sample_index, channel_offset:channel_offset + channel.nbins()] for b in bins: fig = plt.figure(figsize=(8, 8), dpi=96) fig.suptitle('Linearity checks for sample %s, bin [%g, %g]' % (sample.name, channel.bins[b]['lo_edge'], channel.bins[b]['hi_edge'])) gs = gridspec.GridSpec(nrows=nr, ncols=nc, wspace=0.3, hspace=0.3, top=0.9, bottom=0.05, left=0.1, right=0.95) for i, par in enumerate(model.nps.keys()): if options.inversion_plots: sgs = gridspec.GridSpecFromSubplotSpec(2, 1, subplot_spec=gs[i // nc, i % nc], wspace=0.1, hspace=0.1) else: sgs = [gs[i // nc, i % nc]] pars = model.expected_pars(data.poi) model.use_linear_nps = True vars_lin = [ nexp_var(pars, par, x)[b] / nexp0[b] - 1 for x in cont_x ] rvar_lin = [ -((nexp_var(pars, par, x)[b] - nexp0[b]) / nexp0[b])**2 for x in cont_x ] model.use_linear_nps = False vars_nli = [ nexp_var(pars, par, x)[b] / nexp0[b] - 1 for x in cont_x ] rvar_nli = [ -((nexp_var(pars, par, x)[b] - nexp0[b]) / nexp0[b])**2 for x in cont_x ] ax_var = fig.add_subplot(sgs[0]) ax_var.set_title(par) ax_var.plot( data.points, data.variations[channel.name][par][sample_index, b, :] - 1, 'ko') ax_var.plot([0], [0], 's', marker='o', color='purple') ax_var.plot(sample.pos_vars[par], sample.pos_imps[par][:, b], 's', marker='o', color='red') ax_var.plot(sample.neg_vars[par], sample.neg_imps[par][:, b], 's', marker='o', color='red') ax_var.plot(cont_x, vars_lin, 'r--') ax_vars.append(ax_var) if not options.no_nli: ax_var.plot(cont_x, vars_nli, 'b') if options.yrange: ax_var.set_ylim(y_min, y_max) if options.inversion_plots: ax_inv = fig.add_subplot(sgs[1], sharex=ax_var) ax_inv.plot(cont_x, rvar_lin, 'r--') ax_inv.plot(cont_x, rvar_nli, 'b') if options.inv_range: ax_inv.set_ylim(-options.inv_range, 0) ax_invs.append(ax_inv) fig.canvas.set_window_title('Linearity checks for sample %s, bin %g' % (sample.name, b)) if options.output_file != '': if options.output_file is not None: output_file = options.output_file else: split_name = os.path.splitext(options.model_file) output_file = split_name[0] + '-%s-bin_%d.png' % ( options.sample, b) plt.savefig(output_file)