Exemplo n.º 1
0
plot_max = 12.0


# Use autograph for performance
@tf.function
def nll(coeffs_, signal_events_):
    return bmf.signal.normalized_nll(coeffs_, signal_events_)


def try_nll(pos_, val):
    # Override the coeff we want to vary
    try_coeffs[pos_] = tf.constant(val)
    return nll(try_coeffs, signal_events)


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()

    # For each amplitude
    for a_idx in range(0, bmf.coeffs.amplitude_count):
        # If no coeffs for this amplitude are trainable, then skip this plot
        coeff_id_alpha = a_idx * bmf.coeffs.param_count
        if not bmf.coeffs.is_trainable(fit_coeffs[coeff_id_alpha]) \
            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])
    choices=bmf.coeffs.signal_models,
    default=bmf.coeffs.SM,
    help='signal model (default: {})'.format(bmf.coeffs.SM)
)
parser.add_argument(
    '-w',
    '--write-svg',
    dest='write_svg',
    metavar='SVG_PATH',
    help='write plots as SVGs using this filepath. this string must contain \'%%name%%\''
)
args = parser.parse_args()
if args.write_svg and '%name%' not in args.write_svg:
    parser.error('-w/--write-svg must contain \'%name%\'')

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
    from matplotlib.ticker import FuncFormatter, MultipleLocator
    import seaborn as sns

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

    if args.left_transform:
        bmf.stdout('Performing transformations: a_para_l -> -a_perp_l, a_perp_l -> -a_para_l/2')
        a_para_ls = signal_coeffs[0:6]
        a_perp_ls = signal_coeffs[12:18]

# Use autograph for performance
@tf.function
def nll(coeffs_, signal_events_):
    return bmf.signal.normalized_nll(coeffs_, signal_events_)


def try_nll(c):
    # Override the two coeffs we want to vary
    fit_coeffs[cx_idx] = tf.constant(c[0])
    fit_coeffs[cy_idx] = tf.constant(c[1])
    return nll(fit_coeffs, signal_events)


with bmf.Script():
    signal_coeffs = bmf.coeffs.signal(bmf.coeffs.SM)
    signal_events = bmf.signal.generate(signal_coeffs)
    # Set fit coeffs to our constant signal ones
    fit_coeffs = bmf.coeffs.signal(bmf.coeffs.SM)

    cx = np.linspace(grid_min, grid_max, grid_points, dtype=np.float32)
    cy = np.linspace(grid_min, grid_max, grid_points, dtype=np.float32)
    X, Y = tf.meshgrid(cx, cy)

    points_grid = tf.stack([X, Y], axis=2)
    # Turn our grid into (x, y) pairs
    points = tf.reshape(points_grid, [grid_points**2, 2])
    # Calculate likelihoods
    likelihoods = tf.map_fn(try_nll, points)
    # Convert likelihoods back into meshgrid shape