Пример #1
0
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)
Пример #2
0
fig2 = plt.figure(2)
fig2.canvas.set_window_title('True pars, data from exp model')
model.plot(pars, data_exp, residuals=True)
plt.xlim(150,300)

fig3 = plt.figure(3)
fig3.canvas.set_window_title('Nominal pars, data from linear model')
model.plot(pars0, data_lin, residuals=True)
plt.xlim(150,300)

fig4 = plt.figure(4)
fig4.canvas.set_window_title('Profiled pars, data from linear model')
model.plot(min_pars, data_lin, residuals=True)
plt.xlim(150,300)

t1 = np.einsum('i,ij,i->j',model.bkg,model.c, 1 - data_lin.n/(model.n_exp(pars0))*(1 - model.bkg/model.n_exp(pars0)*model.c.dot(pars.gammas)) )
t2 = np.einsum('i,ij,i,i,i->j',model.bkg,model.c, -data_lin.n/model.n_exp(pars0), model.bkg/model.n_exp(pars0)*model.c.dot(pars.gammas), model.bkg/model.n_exp(pars0)*model.c.dot(pars.gammas) )
t3 = np.einsum('i,ij,i,i,i,i->j',model.bkg,model.c, data_lin.n/model.n_exp(pars0), model.bkg/model.n_exp(pars0)*model.c.dot(pars.gammas), model.bkg/model.n_exp(pars0)*model.c.dot(pars.gammas), model.bkg/model.n_exp(pars0)*model.c.dot(pars.gammas))
t4 = np.einsum('i,ij,i,i,i,i,i->j',model.bkg,model.c, -data_lin.n/model.n_exp(pars0), model.bkg/model.n_exp(pars0)*model.c.dot(pars.gammas), model.bkg/model.n_exp(pars0)*model.c.dot(pars.gammas), model.bkg/model.n_exp(pars0)*model.c.dot(pars.gammas), model.bkg/model.n_exp(pars0)*model.c.dot(pars.gammas))

model2 = Model().load('fastprof/models/HighMass_NW-700-log500-gammas-only.json')
model2.sig = model.s_exp(min_pars)
model2.bkg = model.b_exp(min_pars)
model.init_vars()
model2.linear_nps = True
pars02 = model.expected_pars(5)
pars02.gammas -= min_pars.gammas
pars2 = model.expected_pars(5)
pars2.gammas = pars.gammas - min_pars.gammas
data2_lin = Data(model2).set_expected(pars2)
Пример #3
0
test_mus = np.linspace(0,5,6)
n_np = 2
bkg = np.array([1.0, 10.0])  # specific to this case!
sig = np.array([1.0, 0])  # specific to this case!
a = np.array([[0.2], [0.2]]) # specific to this case!
b = np.array([[0.2], [0.2]]) # specific to this case!
#pyhf_data = np.array([ 6.00000000e+00,  9.00000000e+00,  1.29864346e-01, -5.74496450e-01 ])
pyhf_data = np.array([ 2,  10,  0, 0 ])

spec = json.load(open('fastprof/models/test1.json', 'r'))
ws = pyhf.Workspace(spec)
pyhf_model = ws.model()

for mu in test_mus :
  print('-------------------------------------')
  print('Testing the following hypothesis: ', mu)
  model = Model(sig, bkg, alphas = ['acc_sys'], betas = ['bkg_sys'], a=a, b=b)
  data = Data(model).set_from_pyhf_data(pyhf_data, pyhf_model)
  #print(model)
  npm = NPMinimizer(mu, data)
  nll = npm.profile_nll()
  pars, val  = pyhf.infer.mle.fixed_poi_fit(mu, pyhf_data, pyhf_model, return_fitted_val=True)
  print('fast pars |', npm.min_pars.array())
  print('fast nexp |', model.s_exp(npm.min_pars), model.b_exp(npm.min_pars), model.n_exp(npm.min_pars))
  print(nll)
  pyhf_pars = Parameters(pars[0], pars[1], pars[2])
  print('pyhf data |', pyhf_data)
  print('pyhf pars |', pyhf_pars.array())
  print('pyhf nexp |', model.s_exp(pyhf_pars), model.b_exp(pyhf_pars), model.n_exp(pyhf_pars))
  print(model.nll(pyhf_pars, data))