Example #1
0
            args.opt_params[idx][1] = float(args.opt_params[idx][1])
        except ValueError:
            pass
        opt_params[args.opt_params[idx][0]] = args.opt_params[idx][1]

iteration = 0
with bmf.Script(device=args.device) as script:
    if args.log:
        log = bmf.Log(script.name)

    signal_coeffs = bmf.coeffs.signal(args.signal_model)

    if args.csv_file is not None:
        writer = bmf.FitWriter(args.csv_file, signal_coeffs)
        if writer.current_id > 0:
            bmf.stdout('{} already contains {} iteration(s)'.format(
                args.csv_file, writer.current_id))
            bmf.stdout('')
            if writer.current_id >= args.iterations:
                bmf.stderr('Nothing to do')
                exit(0)
            iteration = writer.current_id

    # Show progress bar for fits
    for iteration in tqdm.trange(iteration + 1,
                                 args.iterations + 1,
                                 initial=iteration,
                                 total=args.iterations,
                                 unit='fit'):
        # Time each iteration for CSV writing
        script.timer_start('fit')
Example #2
0
            and not bmf.coeffs.is_trainable(fit_coeffs[coeff_id_alpha + 1]) \
                and not bmf.coeffs.is_trainable(fit_coeffs[coeff_id_alpha + 2]):
            continue

        fig, axes = plt.subplots(bmf.coeffs.param_count)
        fig.suptitle(bmf.coeffs.amplitude_latex_names[a_idx])

        # For each param in this amplitude
        for p_idx in range(0, bmf.coeffs.param_count):
            c_idx = a_idx * bmf.coeffs.param_count + p_idx

            # If this param coeff is not trainable then skip this subplot
            if not bmf.coeffs.is_trainable(fit_coeffs[c_idx]):
                continue

            bmf.stdout('Processing {} ({})'.format(bmf.coeffs.names[c_idx],
                                                   c_idx))

            # Set all coeffs to the constant signal ones
            try_coeffs = bmf.coeffs.signal(bmf.coeffs.SM)

            c_range = np.linspace(plot_min,
                                  plot_max,
                                  plot_points,
                                  dtype=np.float32)

            axes[p_idx].plot(
                c_range,
                list(map(lambda c_val: try_nll(c_idx, c_val).numpy(),
                         c_range)))

            # Add the param's greek letter on the Y-axis
Example #3
0
        colors = itertools.cycle(sns.color_palette())

        for name, points in data_points[c_name].items():
            if not all(elem == 0.0 for elem in points):
                mean = np.mean(points)
                std_err = scipy.stats.sem(points, axis=None)
                pull = list(
                    map(lambda p: (p - signal_coeffs[name][c_name]) / std_err,
                        points))
                pull_mean = np.mean(pull)

                bmf.stdout(
                    '{}/{} signal: {} mean: {} std err: {} pull mean: {}'.
                    format(
                        c_name,
                        name,
                        signal_coeffs[name][c_name],
                        mean,
                        std_err,
                        pull_mean,
                    ))
                color = next(colors)
                sns.kdeplot(pull, cut=0, color=color, label=name)
                # Draw a dotted line to represent the pull mean
                plt.gca().axvline(pull_mean, color=color, linestyle=':')

        plt.xlabel('Pull')
        plt.ylabel('Density')

        if len(data_points[c_name]) > 1:
            plt.legend()
        else:
    # Histogram bins to use
    bins = np.linspace(-x_max, x_max, args.bins)

    # Bin midpoints for x-axis
    x_list = (bins[1:] + bins[:-1]) / 2

    sm_hist = np.histogram(sm_data, bins=bins, density=True)
    np_hist = np.histogram(np_data, bins=bins, density=True)
    np_median = np.median(np_data)
    sm_gaussian = gaussian(x_list, sm_hist[0])

    # Calculate sigma confidence level
    sm_mean = np.mean(sm_data)
    sm_stddev = np.std(sm_data)
    sigma_level = (sm_mean - np_median) / sm_stddev
    bmf.stdout('mean: {} stddev: {} sigma level: {}'.format(
        sm_mean, sm_stddev, sigma_level))

    plt.figure()
    # Set style as well as font to Computer Modern Roman to match LaTeX output
    sns.set(style='ticks',
            font='cmr10',
            rc={
                'mathtext.fontset': 'cm',
                'axes.unicode_minus': False
            })

    # Blue open circles for SM data. Don't plot 0 values
    plt.scatter(x_list, [np.nan if x == 0 else x for x in sm_hist[0]],
                facecolors='none',
                edgecolors='b',
                s=15,
                q2_at_max = q2.numpy()
                diff_at_max = diff_abs_max
                diff_at_max_plus = diff_plus
                diff_at_max_minus = diff_minus
            if diff_at_min == 0.0 or diff_abs_max < diff_at_min:
                q2_at_min = q2.numpy()
                diff_at_min = diff_abs_max
                diff_at_min_plus = diff_plus
                diff_at_min_minus = diff_minus

        bmf.stdout(
            'Processed {}. 95% min +{}/-{} @ {}. 95% max +{}/-{} @ {}'.format(
                amplitude_name,
                diff_at_min_plus,
                diff_at_min_minus,
                q2_at_min,
                diff_at_max_plus,
                diff_at_max_minus,
                q2_at_max,
            )
        )

        plt.fill_between(q2_range.numpy(), min_95, max_95, label='95%', color='lightblue')
        plt.fill_between(q2_range.numpy(), min_68, max_68, label='68%', color='lightgreen')

        plt.plot(
            q2_range.numpy(),
            signal.numpy(),
            color='black', label='signal', linestyle=':'
        )
Example #6
0
#!/usr/bin/env python
"""
Benchmark time taken to run key functions.

Used to check for performance regressions.
"""
import tensorflow.compat.v2 as tf
import timeit

import b_meson_fit as bmf

tf.enable_v2_behavior()

times = [10, 100, 1000]
functions = {
    "nll": lambda: bmf.signal.nll(fit_coeffs, signal_events),
    "minimize": lambda: optimizer.minimize()
}

with bmf.Script() as script:
    signal_coeffs = bmf.coeffs.signal(bmf.coeffs.SM)
    signal_events = bmf.signal.generate(signal_coeffs)
    fit_coeffs = bmf.coeffs.fit()
    optimizer = bmf.Optimizer(fit_coeffs, signal_events)

    for n, f in functions.items():
        for t in times:
            time_taken = timeit.timeit(f, number=t)
            bmf.stdout("{}() x {}: ".format(n, t), time_taken)
                    metavar='SVG_PATH',
                    help='write plot as SVG using this filepath')
args = parser.parse_args()

with bmf.Script(device=args.device) as script:
    if args.write_svg is not None:
        matplotlib.use('SVG')

    # Import these after we optionally set SVG backend - otherwise matplotlib may bail on a missing TK backend when
    #  running from the CLI
    import matplotlib.pylab as plt
    import seaborn as sns

    bmf.stdout(
        'Integrated values between +/- 100 MeV of K892 mass: K892: {} K700: {} Mix: {}'
        .format(bmf.breit_wigner.k892_distribution_integrated(),
                bmf.breit_wigner.k700_distribution_integrated(),
                bmf.breit_wigner.k700_k892_distribution_integrated()))

    masses = tf.linspace(
        bmf.breit_wigner.mass_k_plus + bmf.breit_wigner.mass_pi_minus + 0.01,
        2.0, 150)

    k700 = bmf.breit_wigner.k700_distribution(masses)
    k892 = bmf.breit_wigner.k892_distribution(masses)
    mix = tf.math.abs(bmf.breit_wigner.k700_k892_distribution(masses))

    plt.figure()
    # Set style as well as font to Computer Modern Roman to match LaTeX output
    sns.set(style='ticks',
            font='cmr10',
Example #8
0
        plt.figure()
        # Set style as well as font to Computer Modern Roman to match LaTeX output
        sns.set(style='ticks', font='cmr10', rc={'mathtext.fontset': 'cm', 'axes.unicode_minus': False})

        plt.title(bmf.coeffs.latex_names[bmf.coeffs.names.index(c_name)])

        colors = itertools.cycle(sns.color_palette())

        for name, points in data_points[c_name].items():
            if not all(elem == 0.0 for elem in points):
                mean = np.mean(points)

                bmf.stdout(
                    '{}/{} signal: {} mean: {}'.format(
                        c_name,
                        name,
                        signal_coeffs[name][c_name],
                        mean,
                    )
                )
                color = next(colors)
                sns.kdeplot(points, cut=0, color=color, label=name)
                # Draw a dotted line to represent the mean
                plt.gca().axvline(mean, color=color, linestyle=':')

        # Draw a magenta line to represent the signal
        plt.gca().axvline(signal_coeffs[name][c_name], ymax=0.25, color='magenta')

        plt.xlabel('Fitted value')
        plt.ylabel('Density')

        if len(data_points[c_name]) > 1:
    x_list.append(val)
    y_list.append(nll)

    for step_sign in [-1, +1]:
        fit_coeffs2 = []
        for coeff in fit_coeffs:
            if bmf.coeffs.is_trainable(coeff):
                fit_coeffs2.append(tf.Variable(coeff.numpy()))
            else:
                fit_coeffs2.append(tf.constant(coeff.numpy()))

        for step in range(1, 11):
            fit_coeffs2[c_idx] = tf.constant(fit_coeffs[c_idx].numpy() +
                                             (step * step_size * step_sign),
                                             dtype=tf.float32)
            bmf.stdout(bmf.coeffs.to_str(fit_coeffs2))
            val, nll = fit(fit_coeffs2, signal_events, 0.001)
            x_list.append(val)
            y_list.append(nll)

            # if len(y_list) > 0 and nll >= y_list[0] + 0.1:
            #     break

    print(x_list)
    print(y_list)
    lists = zip(x_list, y_list)
    y_list = [x for _, x in sorted(lists)]
    x_list = sorted(x_list)
    print(x_list)
    print(y_list)