コード例 #1
0
def main():
    script = os.path.splitext(os.path.basename(__file__))[0]
    log.info("[SCRIPT] {}".format(script))

    parser = argparse.ArgumentParser(
        description='Display each event in the file')
    parser.add_argument('-f',
                        '--file',
                        dest='input_path',
                        action='store',
                        default=get_path('gamma_test.simtel.gz'),
                        help='path to the input file. '
                        'Default = gamma_test.simtel.gz')
    parser.add_argument('-O',
                        '--origin',
                        dest='origin',
                        action='store',
                        default='hessio',
                        help='origin of the file: {}. Default = hessio'.format(
                            InputFile.origin_list()))
    parser.add_argument('-D',
                        dest='display',
                        action='store_true',
                        default=False,
                        help='display the camera events')
    parser.add_argument('--pdf',
                        dest='output_path',
                        action='store',
                        default=None,
                        help='path to store a pdf output of the plots')
    parser.add_argument('-t',
                        '--telescope',
                        dest='tel',
                        action='store',
                        type=int,
                        default=None,
                        help='telecope to view. '
                        'Default = All')

    calibration_arguments(parser)

    logger_detail = parser.add_mutually_exclusive_group()
    logger_detail.add_argument('-q',
                               '--quiet',
                               dest='quiet',
                               action='store_true',
                               default=False,
                               help='Quiet mode')
    logger_detail.add_argument('-v',
                               '--verbose',
                               dest='verbose',
                               action='store_true',
                               default=False,
                               help='Verbose mode')
    logger_detail.add_argument('-d',
                               '--debug',
                               dest='debug',
                               action='store_true',
                               default=False,
                               help='Debug mode')

    args = parser.parse_args()
    print('DEBUG type(args) {}'.format(type(args)))
    print('DEBUG args {}'.format(args))
    params = calibration_parameters(args)

    if args.quiet:
        log.setLevel(40)
    if args.verbose:
        log.setLevel(20)
    if args.debug:
        log.setLevel(10)

    log.debug("[file] Reading file")
    input_file = InputFile(args.input_path, args.origin)
    source = input_file.read()

    # geom_dict is a dictionary of CameraGeometry, with keys of
    # (num_pixels, focal_length), the parameters that are used to guess the
    # geometry of the telescope. By using these keys, the geometry is
    # calculated only once per telescope type as needed, reducing computation
    # time.
    # Creating a geom_dict at this point is optional, but is recommended, as
    # the same geom_dict can then be shared between the calibration and
    # CameraPlotter, again reducing computation time.
    # The dictionary becomes filled as a result of a dictionary's mutable
    # nature.
    geom_dict = {}

    # Calibrate events and fill geom_dict

    calibrated_source = calibrate_source(source, params, geom_dict)

    fig = plt.figure(figsize=(16, 7))
    if args.display:
        plt.show(block=False)
    pp = PdfPages(args.output_path) if args.output_path is not None else None
    for event in calibrated_source:
        tels = list(event.dl0.tels_with_data)
        if args.tel is None:
            tel_loop = tels
        else:
            if args.tel not in tels:
                continue
            tel_loop = [args.tel]
        log.debug(tels)
        for tel_id in tel_loop:
            display_telescope(event, tel_id, args.display, geom_dict, pp, fig)
    if pp is not None:
        pp.close()

    log.info("[COMPLETE]")
コード例 #2
0
def main():
    script = os.path.splitext(os.path.basename(__file__))[0]
    log.info("[SCRIPT] {}".format(script))

    parser = argparse.ArgumentParser(
        formatter_class=argparse.ArgumentDefaultsHelpFormatter)
    parser.add_argument('-f',
                        '--file',
                        dest='input_path',
                        action='store',
                        default=get_path('gamma_test.simtel.gz'),
                        help='path to the input file')
    parser.add_argument('-O',
                        '--origin',
                        dest='origin',
                        action='store',
                        choices=InputFile.origin_list(),
                        default='hessio',
                        help='origin of the file')
    parser.add_argument('-o',
                        '--output',
                        dest='output_dir',
                        action='store',
                        default=None,
                        help='path of the output directory to store the '
                        'images (default = input file directory)')
    parser.add_argument('-e',
                        '--event',
                        dest='event_req',
                        action='store',
                        default=0,
                        type=int,
                        help='event index to plot (not id!)')
    parser.add_argument('--id',
                        dest='event_id_f',
                        action='store_true',
                        default=False,
                        help='-e will specify event_id instead '
                        'of index')
    parser.add_argument('-t',
                        '--telescope',
                        dest='tel',
                        action='store',
                        type=int,
                        default=None,
                        help='telecope to view')
    parser.add_argument('-c',
                        '--channel',
                        dest='chan',
                        action='store',
                        type=int,
                        default=0,
                        help='channel to view (default = 0 (HG))')
    parser.add_argument('--calib-help',
                        dest='calib_help',
                        action='store_true',
                        default=False,
                        help='display the arguments used for the camera '
                        'calibration')

    logger_detail = parser.add_mutually_exclusive_group()
    logger_detail.add_argument('-q',
                               '--quiet',
                               dest='quiet',
                               action='store_true',
                               default=False,
                               help='Quiet mode')
    logger_detail.add_argument('-v',
                               '--verbose',
                               dest='verbose',
                               action='store_true',
                               default=False,
                               help='Verbose mode')
    logger_detail.add_argument('-d',
                               '--debug',
                               dest='debug',
                               action='store_true',
                               default=False,
                               help='Debug mode')

    args, excess_args = parser.parse_known_args()

    if args.quiet:
        log.setLevel(40)
    if args.verbose:
        log.setLevel(20)
    if args.debug:
        log.setLevel(10)

    telid = args.tel
    chan = args.chan

    log.debug("[file] Reading file")
    input_file = InputFile(args.input_path, args.origin)
    event = input_file.get_event(args.event_req, args.event_id_f)

    # Print event/args values
    log.info("[event_index] {}".format(event.count))
    log.info("[event_id] {}".format(event.dl0.event_id))
    log.info("[telescope] {}".format(telid))
    log.info("[channel] {}".format(chan))

    params, unknown_args = calibration_parameters(excess_args, args.origin,
                                                  args.calib_help)

    if unknown_args:
        parser.print_help()
        calibration_parameters(unknown_args, args.origin, True)
        msg = 'unrecognized arguments: %s'
        parser.error(msg % ' '.join(unknown_args))

    # Create a dictionary to store any geoms in
    geom = CameraGeometry.guess(*event.meta.pixel_pos[telid],
                                event.meta.optical_foclen[telid])
    geom_dict = {telid: geom}
    calibrated_event = calibrate_event(event, params, geom_dict)

    # Select telescope
    tels = list(calibrated_event.dl0.tels_with_data)
    if telid is None or telid not in tels:
        log.error("[event] please specify one of the following telescopes "
                  "for this event: {}".format(tels))
        exit()

    # Extract required images
    data_ped = calibrated_event.dl1.tel[telid].pedestal_subtracted_adc[chan]
    true_pe = calibrated_event.mc.tel[telid].photo_electrons
    measured_pe = calibrated_event.dl1.tel[telid].pe_charge
    max_time = np.unravel_index(np.argmax(data_ped), data_ped.shape)[1]
    max_charges = np.max(data_ped, axis=1)
    max_pixel = int(np.argmax(max_charges))
    min_pixel = int(np.argmin(max_charges))

    # Get Neighbours
    max_pixel_nei = geom.neighbors[max_pixel]
    min_pixel_nei = geom.neighbors[min_pixel]

    # Get Windows
    windows = calibrated_event.dl1.tel[telid].integration_window[chan]
    length = np.sum(windows, axis=1)
    start = np.argmax(windows, axis=1)
    end = start + length - 1

    # Draw figures
    ax_max_nei = {}
    ax_min_nei = {}
    fig_waveforms = plt.figure(figsize=(18, 9))
    fig_waveforms.subplots_adjust(hspace=.5)
    fig_camera = plt.figure(figsize=(15, 12))

    ax_max_pix = fig_waveforms.add_subplot(4, 2, 1)
    ax_min_pix = fig_waveforms.add_subplot(4, 2, 2)
    ax_max_nei[0] = fig_waveforms.add_subplot(4, 2, 3)
    ax_min_nei[0] = fig_waveforms.add_subplot(4, 2, 4)
    ax_max_nei[1] = fig_waveforms.add_subplot(4, 2, 5)
    ax_min_nei[1] = fig_waveforms.add_subplot(4, 2, 6)
    ax_max_nei[2] = fig_waveforms.add_subplot(4, 2, 7)
    ax_min_nei[2] = fig_waveforms.add_subplot(4, 2, 8)

    ax_img_nei = fig_camera.add_subplot(2, 2, 1)
    ax_img_max = fig_camera.add_subplot(2, 2, 2)
    ax_img_true = fig_camera.add_subplot(2, 2, 3)
    ax_img_cal = fig_camera.add_subplot(2, 2, 4)

    plotter = CameraPlotter(event, geom_dict)

    # Draw max pixel traces
    plotter.draw_waveform(data_ped[max_pixel], ax_max_pix)
    ax_max_pix.set_title("(Max) Pixel: {}, "
                         "True: {}, "
                         "Measured = {:.3f}".format(max_pixel,
                                                    true_pe[max_pixel],
                                                    measured_pe[max_pixel]))
    ax_max_pix.set_ylabel("Amplitude-Ped (ADC)")
    max_ylim = ax_max_pix.get_ylim()
    plotter.draw_waveform_positionline(start[max_pixel], ax_max_pix)
    plotter.draw_waveform_positionline(end[max_pixel], ax_max_pix)
    for i, ax in ax_max_nei.items():
        if len(max_pixel_nei) > i:
            pix = max_pixel_nei[i]
            plotter.draw_waveform(data_ped[pix], ax)
            ax.set_title("(Max Nei) Pixel: {}, "
                         "True: {}, "
                         "Measured = {:.3f}".format(pix, true_pe[pix],
                                                    measured_pe[pix]))
            ax.set_ylabel("Amplitude-Ped (ADC)")
            ax.set_ylim(max_ylim)
            plotter.draw_waveform_positionline(start[pix], ax)
            plotter.draw_waveform_positionline(end[pix], ax)

    # Draw min pixel traces
    plotter.draw_waveform(data_ped[min_pixel], ax_min_pix)
    ax_min_pix.set_title("(Min) Pixel: {}, "
                         "True: {}, "
                         "Measured = {:.3f}".format(min_pixel,
                                                    true_pe[min_pixel],
                                                    measured_pe[min_pixel]))
    ax_min_pix.set_ylabel("Amplitude-Ped (ADC)")
    ax_min_pix.set_ylim(max_ylim)
    plotter.draw_waveform_positionline(start[min_pixel], ax_min_pix)
    plotter.draw_waveform_positionline(end[min_pixel], ax_min_pix)
    for i, ax in ax_min_nei.items():
        if len(min_pixel_nei) > i:
            pix = min_pixel_nei[i]
            plotter.draw_waveform(data_ped[pix], ax)
            ax.set_title("(Min Nei) Pixel: {}, "
                         "True: {}, "
                         "Measured = {:.3f}".format(pix, true_pe[pix],
                                                    measured_pe[pix]))
            ax.set_ylabel("Amplitude-Ped (ADC)")
            ax.set_ylim(max_ylim)
            plotter.draw_waveform_positionline(start[pix], ax)
            plotter.draw_waveform_positionline(end[pix], ax)

    # Draw cameras
    nei_camera = np.zeros_like(max_charges, dtype=np.int)
    nei_camera[min_pixel_nei] = 2
    nei_camera[min_pixel] = 1
    nei_camera[max_pixel_nei] = 3
    nei_camera[max_pixel] = 4
    camera = plotter.draw_camera(telid, nei_camera, ax_img_nei)
    camera.cmap = plt.cm.viridis
    ax_img_nei.set_title("Neighbour Map")
    plotter.draw_camera_pixel_annotation(telid, max_pixel, min_pixel,
                                         ax_img_nei)

    camera = plotter.draw_camera(telid, data_ped[:, max_time], ax_img_max)
    camera.add_colorbar(ax=ax_img_max, label="Amplitude-Ped (ADC)")
    ax_img_max.set_title("Max Timeslice (T = {})".format(max_time))
    plotter.draw_camera_pixel_annotation(telid, max_pixel, min_pixel,
                                         ax_img_max)

    camera = plotter.draw_camera(telid, true_pe, ax_img_true)
    camera.add_colorbar(ax=ax_img_true, label="True Charge (Photo-electrons)")
    ax_img_true.set_title("True Charge")
    plotter.draw_camera_pixel_annotation(telid, max_pixel, min_pixel,
                                         ax_img_true)

    int_dict, inverted = integrator_dict()
    integrator_name = '' if 'integrator' not in params else \
        params['integrator']

    camera = plotter.draw_camera(telid, measured_pe, ax_img_cal)
    camera.add_colorbar(ax=ax_img_cal, label="Calib Charge (Photo-electrons)")
    ax_img_cal.set_title("Charge (integrator={})".format(integrator_name))
    plotter.draw_camera_pixel_annotation(telid, max_pixel, min_pixel,
                                         ax_img_cal)

    fig_waveforms.suptitle("Integrator = {}".format(integrator_name))
    fig_camera.suptitle("Camera = {}".format(geom.cam_id))

    waveform_output_name = "{}_e{}_t{}_c{}_integrator{}_waveform.pdf"\
        .format(input_file.filename, event.count, telid, chan,
                inverted[params['integrator']])
    camera_output_name = "{}_e{}_t{}_c{}_integrator{}_camera.pdf"\
        .format(input_file.filename, event.count, telid, chan,
                inverted[params['integrator']])
    output_dir = args.output_dir if args.output_dir is not None else \
        input_file.output_directory
    output_dir = os.path.join(output_dir, script)
    if not os.path.exists(output_dir):
        log.info("[output] Creating directory: {}".format(output_dir))
        os.makedirs(output_dir)

    waveform_output_path = os.path.join(output_dir, waveform_output_name)
    log.info("[output] {}".format(waveform_output_path))
    fig_waveforms.savefig(waveform_output_path,
                          format='pdf',
                          bbox_inches='tight')

    camera_output_path = os.path.join(output_dir, camera_output_name)
    log.info("[output] {}".format(camera_output_path))
    fig_camera.savefig(camera_output_path, format='pdf', bbox_inches='tight')

    log.info("[COMPLETE]")
コード例 #3
0
def argparsing():
    parser = argparse.ArgumentParser(
        formatter_class=argparse.ArgumentDefaultsHelpFormatter)
    parser.add_argument('-f', '--file', dest='input_paths',
                        default=[get_path('gamma_test.simtel.gz')], nargs='*',
                        help='path to the input files to be combined for a '
                             'single charge resolution.')
    parser.add_argument('-O', '--origin', dest='origin', action='store',
                        choices=InputFile.origin_list(),
                        default='hessio', help='origin of the file')
    parser.add_argument('-t', '--telescope', dest='tel', action='store',
                        type=int, default=None, nargs='*',
                        help='list of telecopes to be included. '
                             'Default = All')
    parser.add_argument('-o', '--output', dest='output_path', action='store',
                        default=None,
                        help='path to store a pdf output of the plots. '
                             'default = display on screen instead')
    parser.add_argument('--comparison', dest='comparison', action='store',
                        default=None,
                        help='output path for a True Charge versus Measured'
                             'Charge graph. Default = do not plot graph')
    parser.add_argument('-M', '--maxpe', dest='maxpe', action='store',
                        default=None, type=float,
                        help='maximum pe to calculate the charge resolution'
                             ' up to. Default = maximum pe in file')
    parser.add_argument('--maxpeplot', dest='maxpeplot', action='store',
                        default=None, type=float,
                        help='maximum pe to plot up to. Default = maxpe')
    parser.add_argument('-B', '--binning', dest='binning', action='store',
                        default="log", choices=['none', 'normal', 'log'],
                        help='binning of the charge resoltion graph: '
                             '"none" = no binning, "normal" = bin, '
                             '"log" = bin in log space.')
    parser.add_argument('--normalx', dest='normalx', action='store_true',
                        default=False,
                        help='Use a normal x axis instead of the defualt log'
                             'axis')
    parser.add_argument('--normaly', dest='normaly', action='store_true',
                        default=False,
                        help='Use a normal y axis instead of the defualt log'
                             'axis')
    parser.add_argument('-E', '--example', dest='example', action='store_true',
                        default=False,
                        help='print an example runcard')
    parser.add_argument('-R', '--runcard', dest='runcard', action='store',
                        default=None,
                        help='path to a runcard text file with arguments that '
                             'override command line arguments. This run card '
                             'can allow complex combinations of files and '
                             'calibrations to compare the charge resolution '
                             'against each other.')
    parser.add_argument('--chargeres-names', dest='chargeres_names',
                        default=['default'], nargs='*',
                        help='chargres calculation to include in plot. '
                             'Only used for runcards.')
    parser.add_argument('--calib-help', dest='calib_help', action='store_true',
                        default=False,
                        help='display the arguments used for the camera '
                             'calibration')

    logger_detail = parser.add_mutually_exclusive_group()
    logger_detail.add_argument('-q', '--quiet', dest='quiet',
                               action='store_true', default=False,
                               help='Quiet mode')
    logger_detail.add_argument('-v', '--verbose', dest='verbose',
                               action='store_true', default=False,
                               help='Verbose mode')
    logger_detail.add_argument('-d', '--debug', dest='debug',
                               action='store_true', default=False,
                               help='Debug mode')

    args, excess_args = parser.parse_known_args()

    if args.quiet:
        log.setLevel(40)
    if args.verbose:
        log.setLevel(20)
    if args.debug:
        log.setLevel(10)

    if args.calib_help:
        params, unknown_args = calibration_parameters(excess_args,
                                                      args.origin,
                                                      args.calib_help)

    if args.example:
        print("""
# Each charge resolution block starts with [chargeres] and the names for
# this charge resolution.
# The options in each block are equivalent to the scripts help message.
# Options that seem to apply to plotting will only have effect in a
# plotting block.

[chargeres] test_file_local
#-f gamma_test.simtel.gz
-f ~/Software/outputs/sim_telarray/simtel_run4_gcts_hnsb.gz
-O hessio
--integrator 4
--integration-window 7 3
--integration-sigamp 2 4
--integration-lwt 0
--integration-calib_scale 1.05
--comparison ~/Downloads/test_file_local.pdf


# A second charge resolution block to also calculate the resolution with
# a different integrator so the two resolutions can be plotted against
# each other.

[chargeres] test_file_nei
#-f gamma_test.simtel.gz
-f ~/Software/outputs/sim_telarray/simtel_run4_gcts_hnsb.gz
-O hessio
--integrator 5
--integration-window 7 3
--integration-sigamp 2 4
--integration-lwt 0
--integration-calib_scale 1.05
--comparison ~/Downloads/test_file_nei.pdf

# A plotting block configures an output plot

[plot] normal_plot
--chargeres-names test_file_local test_file_nei
-o ~/Downloads/normal_plot.pdf
--binning normal
--normalx
--normaly

[plot] log_plot
--chargeres-names test_file_local test_file_nei
-o ~/Downloads/log_plot.pdf""")
        exit()

    chargeres_cmdlines = {}
    plot_cmdlines = {}

    if args.runcard is None:
        name = args.chargeres_names[0]
        chargeres_cmdlines[name] = sys.argv[1:]
        plot_cmdlines[name] = sys.argv[1:]
        chargeres_args = {name: args}
        plot_args = {name: args}
    else:
        chargeres_args = {}
        plot_args = {}
        current = None
        runcard = open(args.runcard)
        for line in runcard:
            if line.strip() and not line.startswith('#'):
                argument = line.split()[0]
                if argument == '[chargeres]':
                    name = line.split()[1:][0]
                    chargeres_cmdlines[name] = []
                    current = chargeres_cmdlines[name]
                    continue
                elif argument == '[plot]':
                    name = line.split()[1:][0]
                    plot_cmdlines[name] = []
                    current = plot_cmdlines[name]
                    continue
                current.extend(line.split())

        # Temp fill for checks
        for name, cmdline in chargeres_cmdlines.items():
            chargeres_args[name], unknown = parser.parse_known_args(cmdline)
        for name, cmdline in plot_cmdlines.items():
            plot_args[name], unknown = parser.parse_known_args(cmdline)

    # Check all chargeres_names exist
    for plot_name, args in plot_args.items():
        for name in args.chargeres_names:
            try:
                if name not in chargeres_args:
                    raise IndexError
            except IndexError:
                log.exception("[chargeres_names] For plot: {}, no chargeres "
                              "has the name: {}".format(plot_name, name))
                raise 

    return parser, chargeres_cmdlines, plot_cmdlines
コード例 #4
0
def main():
    script = os.path.splitext(os.path.basename(__file__))[0]
    log.info("[SCRIPT] {}".format(script))

    parser = argparse.ArgumentParser(
        description='Display each event in the file')
    parser.add_argument('-f', '--file', dest='input_path', action='store',
                        default=get_path('gamma_test.simtel.gz'),
                        help='path to the input file. '
                             'Default = gamma_test.simtel.gz')
    parser.add_argument('-O', '--origin', dest='origin', action='store',
                        default='hessio',
                        help='origin of the file: {}. Default = hessio'
                        .format(origin_list()))
    parser.add_argument('-D', dest='display', action='store_true',
                        default=False, help='display the camera events')
    parser.add_argument('--pdf', dest='output_path', action='store',
                        default=None,
                        help='path to store a pdf output of the plots')
    parser.add_argument('-t', '--telescope', dest='tel', action='store',
                        type=int, default=None, help='telecope to view. '
                                                     'Default = All')

    calibration_arguments(parser)

    logger_detail = parser.add_mutually_exclusive_group()
    logger_detail.add_argument('-q', '--quiet', dest='quiet',
                               action='store_true', default=False,
                               help='Quiet mode')
    logger_detail.add_argument('-v', '--verbose', dest='verbose',
                               action='store_true', default=False,
                               help='Verbose mode')
    logger_detail.add_argument('-d', '--debug', dest='debug',
                               action='store_true', default=False,
                               help='Debug mode')

    args = parser.parse_args()
    params = calibration_parameters(args)

    if args.quiet:
        log.setLevel(40)
    if args.verbose:
        log.setLevel(20)
    if args.debug:
        log.setLevel(10)

    log.debug("[file] Reading file")
    input_file = InputFile(args.input_path, args.origin)
    source = input_file.read()

    # geom_dict is a dictionary of CameraGeometry, with keys of
    # (num_pixels, focal_length), the parameters that are used to guess the
    # geometry of the telescope. By using these keys, the geometry is
    # calculated only once per telescope type as needed, reducing computation
    # time.
    # Creating a geom_dict at this point is optional, but is recommended, as
    # the same geom_dict can then be shared between the calibration and
    # CameraPlotter, again reducing computation time.
    # The dictionary becomes filled as a result of a dictionary's mutable
    # nature.
    geom_dict = {}

    # Calibrate events and fill geom_dict
    calibrated_source = calibrate_source(source, params, geom_dict)

    fig = plt.figure(figsize=(16, 7))
    if args.display:
        plt.show(block=False)
    pp = PdfPages(args.output_path) if args.output_path is not None else None
    for event in calibrated_source:
        tels = list(event.dl0.tels_with_data)
        if args.tel is None:
            tel_loop = tels
        else:
            if args.tel not in tels:
                continue
            tel_loop = [args.tel]
        log.debug(tels)
        for tel_id in tel_loop:
            display_telescope(event, tel_id, args.display, geom_dict, pp, fig)
    if pp is not None:
        pp.close()

    log.info("[COMPLETE]")
コード例 #5
0
def calculate_charge_resolutions(parser, chargeres_cmdlines):

    run = 0
    num_runs = len(chargeres_cmdlines)
    chargeres_dict = {}

    for name, cmdline in chargeres_cmdlines.items():

        args, excess_args = parser.parse_known_args(cmdline)

        run += 1
        log.info("[run] Calculating charge resolution {}/{}".format(
            run, num_runs))

        # Print event/args values
        log.info("[files] {}".format(args.input_paths))
        telescopes = "All" if args.tel is None else args.tel
        log.info("[origin] {}".format(args.origin))
        log.info("[telescopes] {}".format(telescopes))

        # Obtain InputFiles (to check files exist)
        input_file_list = []
        for filepath in args.input_paths:
            tilde_filepath = os.path.expanduser(filepath)
            for globfile in glob.glob(tilde_filepath):
                input_file_list.append(InputFile(globfile, args.origin))

        # Find maxpe
        maxpe = args.maxpe
        if maxpe is None:
            maxpe = 0
            for input_file in input_file_list:
                file_maxpe = input_file.find_max_true_npe(args.tel)
                if file_maxpe > maxpe:
                    maxpe = file_maxpe
        log.info("[maxpe] {}".format(maxpe))

        params, unknown_args = calibration_parameters(excess_args, args.origin,
                                                      args.calib_help)

        chargeres = ChargeResolution(maxpe)

        for input_file in input_file_list:
            source = input_file.read()
            calibrated_source = calibrate_source(source, params)
            chargeres.add_source(calibrated_source, args.tel)

        # Plot comparison graphs
        if args.comparison is not None:
            hist = chargeres.variation_hist
            hist[hist == 0.0] = np.nan
            xedges = chargeres.variation_xedges
            yedges = chargeres.variation_yedges

            fig = plt.figure(figsize=(10, 7))
            ax = fig.add_subplot(111)
            ax.set_title(name)
            x, y = np.meshgrid(xedges, yedges)
            x = np.power(10, x)
            y = np.power(10, y)
            hist_mask = np.ma.masked_where(np.isnan(hist), hist)
            im = ax.pcolormesh(x,
                               y,
                               hist_mask,
                               norm=LogNorm(),
                               cmap=plt.cm.viridis)
            cb = plt.colorbar(im)
            ax.set_aspect('equal')
            ax.grid()
            ax.set_xscale('log')
            ax.set_yscale('log')
            ax.set_xlabel(r'True Charge $Q_T$ (p.e.)')
            ax.set_ylabel(r'Measured Charge $Q_M$ (p.e.)')
            cb.ax.set_ylabel("Count")

            line = np.linspace(*ax.get_xlim(), 100)
            ax.plot(line, line, c='0.75', ls='--')

            # Add minor ticks
            lmin = floor(log10(hist_mask.min()))
            lmax = ceil(log10(hist_mask.max()))
            logticks = np.tile(np.arange(lmin, 10), lmax) * (np.power(
                10,
                np.arange(lmax * 10) // 10))
            logticks = im.norm(
                logticks[(logticks != 0) & (logticks >= hist_mask.min()) &
                         (logticks <= hist_mask.max())])
            cb.ax.yaxis.set_ticks(logticks, minor=True)
            cb.ax.yaxis.set_tick_params(which='minor', length=5)
            cb.ax.tick_params(length=10)

            args.comparison = os.path.expanduser(args.comparison)
            output_dir = dirname(args.comparison)
            if not os.path.exists(output_dir):
                log.info("[output] Creating directory: {}".format(output_dir))
                os.makedirs(output_dir)
            log.info("[output] {}".format(args.comparison))
            warnings.filterwarnings("ignore", module="matplotlib")
            with warnings.catch_warnings():
                warnings.simplefilter("ignore")
                fig.savefig(args.comparison, bbox_inches='tight')

        chargeres_dict[name] = chargeres

    log.info('[run] Charge resolution calculations finished')

    return chargeres_dict
コード例 #6
0
def argparsing():
    parser = argparse.ArgumentParser(
        formatter_class=argparse.ArgumentDefaultsHelpFormatter)
    parser.add_argument('-f',
                        '--file',
                        dest='input_paths',
                        default=[get_path('gamma_test.simtel.gz')],
                        nargs='*',
                        help='path to the input files to be combined for a '
                        'single charge resolution.')
    parser.add_argument('-O',
                        '--origin',
                        dest='origin',
                        action='store',
                        choices=InputFile.origin_list(),
                        default='hessio',
                        help='origin of the file')
    parser.add_argument('-t',
                        '--telescope',
                        dest='tel',
                        action='store',
                        type=int,
                        default=None,
                        nargs='*',
                        help='list of telecopes to be included. '
                        'Default = All')
    parser.add_argument('-o',
                        '--output',
                        dest='output_path',
                        action='store',
                        default=None,
                        help='path to store a pdf output of the plots. '
                        'default = display on screen instead')
    parser.add_argument('--comparison',
                        dest='comparison',
                        action='store',
                        default=None,
                        help='output path for a True Charge versus Measured'
                        'Charge graph. Default = do not plot graph')
    parser.add_argument('-M',
                        '--maxpe',
                        dest='maxpe',
                        action='store',
                        default=None,
                        type=float,
                        help='maximum pe to calculate the charge resolution'
                        ' up to. Default = maximum pe in file')
    parser.add_argument('--maxpeplot',
                        dest='maxpeplot',
                        action='store',
                        default=None,
                        type=float,
                        help='maximum pe to plot up to. Default = maxpe')
    parser.add_argument('-B',
                        '--binning',
                        dest='binning',
                        action='store',
                        default="log",
                        choices=['none', 'normal', 'log'],
                        help='binning of the charge resoltion graph: '
                        '"none" = no binning, "normal" = bin, '
                        '"log" = bin in log space.')
    parser.add_argument('--normalx',
                        dest='normalx',
                        action='store_true',
                        default=False,
                        help='Use a normal x axis instead of the defualt log'
                        'axis')
    parser.add_argument('--normaly',
                        dest='normaly',
                        action='store_true',
                        default=False,
                        help='Use a normal y axis instead of the defualt log'
                        'axis')
    parser.add_argument('-E',
                        '--example',
                        dest='example',
                        action='store_true',
                        default=False,
                        help='print an example runcard')
    parser.add_argument('-R',
                        '--runcard',
                        dest='runcard',
                        action='store',
                        default=None,
                        help='path to a runcard text file with arguments that '
                        'override command line arguments. This run card '
                        'can allow complex combinations of files and '
                        'calibrations to compare the charge resolution '
                        'against each other.')
    parser.add_argument('--chargeres-names',
                        dest='chargeres_names',
                        default=['default'],
                        nargs='*',
                        help='chargres calculation to include in plot. '
                        'Only used for runcards.')
    parser.add_argument('--calib-help',
                        dest='calib_help',
                        action='store_true',
                        default=False,
                        help='display the arguments used for the camera '
                        'calibration')

    logger_detail = parser.add_mutually_exclusive_group()
    logger_detail.add_argument('-q',
                               '--quiet',
                               dest='quiet',
                               action='store_true',
                               default=False,
                               help='Quiet mode')
    logger_detail.add_argument('-v',
                               '--verbose',
                               dest='verbose',
                               action='store_true',
                               default=False,
                               help='Verbose mode')
    logger_detail.add_argument('-d',
                               '--debug',
                               dest='debug',
                               action='store_true',
                               default=False,
                               help='Debug mode')

    args, excess_args = parser.parse_known_args()

    if args.quiet:
        log.setLevel(40)
    if args.verbose:
        log.setLevel(20)
    if args.debug:
        log.setLevel(10)

    if args.calib_help:
        params, unknown_args = calibration_parameters(excess_args, args.origin,
                                                      args.calib_help)

    if args.example:
        print("""
# Each charge resolution block starts with [chargeres] and the names for
# this charge resolution.
# The options in each block are equivalent to the scripts help message.
# Options that seem to apply to plotting will only have effect in a
# plotting block.

[chargeres] test_file_local
#-f gamma_test.simtel.gz
-f ~/Software/outputs/sim_telarray/simtel_run4_gcts_hnsb.gz
-O hessio
--integrator 4
--integration-window 7 3
--integration-sigamp 2 4
--integration-lwt 0
--integration-calib_scale 1.05
--comparison ~/Downloads/test_file_local.pdf


# A second charge resolution block to also calculate the resolution with
# a different integrator so the two resolutions can be plotted against
# each other.

[chargeres] test_file_nei
#-f gamma_test.simtel.gz
-f ~/Software/outputs/sim_telarray/simtel_run4_gcts_hnsb.gz
-O hessio
--integrator 5
--integration-window 7 3
--integration-sigamp 2 4
--integration-lwt 0
--integration-calib_scale 1.05
--comparison ~/Downloads/test_file_nei.pdf

# A plotting block configures an output plot

[plot] normal_plot
--chargeres-names test_file_local test_file_nei
-o ~/Downloads/normal_plot.pdf
--binning normal
--normalx
--normaly

[plot] log_plot
--chargeres-names test_file_local test_file_nei
-o ~/Downloads/log_plot.pdf""")
        exit()

    chargeres_cmdlines = {}
    plot_cmdlines = {}

    if args.runcard is None:
        name = args.chargeres_names[0]
        chargeres_cmdlines[name] = sys.argv[1:]
        plot_cmdlines[name] = sys.argv[1:]
        chargeres_args = {name: args}
        plot_args = {name: args}
    else:
        chargeres_args = {}
        plot_args = {}
        current = None
        runcard = open(args.runcard)
        for line in runcard:
            if line.strip() and not line.startswith('#'):
                argument = line.split()[0]
                if argument == '[chargeres]':
                    name = line.split()[1:][0]
                    chargeres_cmdlines[name] = []
                    current = chargeres_cmdlines[name]
                    continue
                elif argument == '[plot]':
                    name = line.split()[1:][0]
                    plot_cmdlines[name] = []
                    current = plot_cmdlines[name]
                    continue
                current.extend(line.split())

        # Temp fill for checks
        for name, cmdline in chargeres_cmdlines.items():
            chargeres_args[name], unknown = parser.parse_known_args(cmdline)
        for name, cmdline in plot_cmdlines.items():
            plot_args[name], unknown = parser.parse_known_args(cmdline)

    # Check all chargeres_names exist
    for plot_name, args in plot_args.items():
        for name in args.chargeres_names:
            try:
                if name not in chargeres_args:
                    raise IndexError
            except IndexError:
                log.exception("[chargeres_names] For plot: {}, no chargeres "
                              "has the name: {}".format(plot_name, name))
                raise

    return parser, chargeres_cmdlines, plot_cmdlines
コード例 #7
0
def main():
    script = os.path.splitext(os.path.basename(__file__))[0]
    log.info("[SCRIPT] {}".format(script))

    parser = argparse.ArgumentParser(description='Create a gif of an event')
    parser.add_argument('-f', '--file', dest='input_path', action='store',
                        required=True, help='path to the input file')
    parser.add_argument('-O', '--origin', dest='origin', action='store',
                        required=True, help='origin of the file: {}'
                        .format(origin_list()))
    parser.add_argument('-o', '--output', dest='output_dir', action='store',
                        default=None,
                        help='path of the output directory to store the '
                             'images (default = input file directory)')
    parser.add_argument('-e', '--event', dest='event_req', action='store',
                        required=True, type=int,
                        help='event index to plot (not id!)')
    parser.add_argument('--id', dest='event_id_f', action='store_true',
                        default=False, help='-e will specify event_id instead '
                                            'of index')
    parser.add_argument('-t', '--telescope', dest='tel', action='store',
                        type=int, default=None, help='telecope to view')
    parser.add_argument('-c', '--channel', dest='chan', action='store',
                        type=int, default=0,
                        help='channel to view (default = 0 (HG))')

    calibration_arguments(parser)

    logger_detail = parser.add_mutually_exclusive_group()
    logger_detail.add_argument('-q', '--quiet', dest='quiet',
                               action='store_true', default=False,
                               help='Quiet mode')
    logger_detail.add_argument('-v', '--verbose', dest='verbose',
                               action='store_true', default=False,
                               help='Verbose mode')
    logger_detail.add_argument('-d', '--debug', dest='debug',
                               action='store_true', default=False,
                               help='Debug mode')

    args = parser.parse_args()

    if args.quiet:
        log.setLevel(40)
    if args.verbose:
        log.setLevel(20)
    if args.debug:
        log.setLevel(10)

    telid = args.tel
    chan = args.chan

    log.debug("[file] Reading file")
    input_file = InputFile(args.input_path, args.origin)
    event = input_file.get_event(args.event_req, args.event_id_f)

    # Print event/args values
    log.info("[event_index] {}".format(event.count))
    log.info("[event_id] {}".format(event.dl0.event_id))
    log.info("[telescope] {}".format(telid))
    log.info("[channel] {}".format(chan))

    params = calibration_parameters(args)

    # Create a dictionary to store any geoms in
    geom = CameraGeometry.guess(*event.meta.pixel_pos[telid],
                                event.meta.optical_foclen[telid])
    geom_dict = {telid: geom}
    calibrated_event = calibrate_event(event, params, geom_dict)

    # Select telescope
    tels = list(calibrated_event.dl0.tels_with_data)
    if telid is None or telid not in tels:
        log.error("[event] please specify one of the following telescopes "
                  "for this event: {}".format(tels))
        exit()

    # Extract required images
    data_ped = calibrated_event.dl1.tel[telid].pedestal_subtracted_adc[chan]
    true_pe = calibrated_event.mc.tel[telid].photo_electrons
    measured_pe = calibrated_event.dl1.tel[telid].pe_charge
    max_time = np.unravel_index(np.argmax(data_ped), data_ped.shape)[1]
    max_charges = np.max(data_ped, axis=1)
    max_pixel = int(np.argmax(max_charges))
    min_pixel = int(np.argmin(max_charges))

    # Get Neighbours
    max_pixel_nei = geom.neighbors[max_pixel]
    min_pixel_nei = geom.neighbors[min_pixel]

    # Get Windows
    windows = calibrated_event.dl1.tel[telid].integration_window[chan]
    length = np.sum(windows, axis=1)
    start = np.argmax(windows, axis=1)
    end = start + length - 1

    # Draw figures
    ax_max_nei = {}
    ax_min_nei = {}
    fig_waveforms = plt.figure(figsize=(24, 10))
    fig_waveforms.subplots_adjust(hspace=.5)
    fig_camera = plt.figure(figsize=(30, 24))

    ax_max_pix = fig_waveforms.add_subplot(4, 2, 1)
    ax_min_pix = fig_waveforms.add_subplot(4, 2, 2)
    ax_max_nei[0] = fig_waveforms.add_subplot(4, 2, 3)
    ax_min_nei[0] = fig_waveforms.add_subplot(4, 2, 4)
    ax_max_nei[1] = fig_waveforms.add_subplot(4, 2, 5)
    ax_min_nei[1] = fig_waveforms.add_subplot(4, 2, 6)
    ax_max_nei[2] = fig_waveforms.add_subplot(4, 2, 7)
    ax_min_nei[2] = fig_waveforms.add_subplot(4, 2, 8)

    ax_img_nei = fig_camera.add_subplot(2, 2, 1)
    ax_img_max = fig_camera.add_subplot(2, 2, 2)
    ax_img_true = fig_camera.add_subplot(2, 2, 3)
    ax_img_cal = fig_camera.add_subplot(2, 2, 4)

    plotter = CameraPlotter(event, geom_dict)

    # Draw max pixel traces
    plotter.draw_waveform(data_ped[max_pixel], ax_max_pix)
    ax_max_pix.set_title("(Max) Pixel: {}, "
                         "True: {}, "
                         "Measured = {}".format(max_pixel, true_pe[max_pixel],
                                                measured_pe[max_pixel]))
    ax_max_pix.set_ylabel("Amplitude-Ped (ADC)")
    max_ylim = ax_max_pix.get_ylim()
    plotter.draw_waveform_positionline(start[max_pixel], ax_max_pix)
    plotter.draw_waveform_positionline(end[max_pixel], ax_max_pix)
    for i, ax in ax_max_nei.items():
        if len(max_pixel_nei) > i:
            pix = max_pixel_nei[i]
            plotter.draw_waveform(data_ped[pix], ax)
            ax.set_title("(Max Nei) Pixel: {}, "
                         "True: {}, "
                         "Measured = {}".format(pix, true_pe[pix],
                                                measured_pe[pix]))
            ax.set_ylabel("Amplitude-Ped (ADC)")
            ax.set_ylim(max_ylim)
            plotter.draw_waveform_positionline(start[pix], ax)
            plotter.draw_waveform_positionline(end[pix], ax)

    # Draw min pixel traces
    plotter.draw_waveform(data_ped[min_pixel], ax_min_pix)
    ax_min_pix.set_title("(Min) Pixel: {}, "
                         "True: {}, "
                         "Measured = {}".format(min_pixel, true_pe[min_pixel],
                                                measured_pe[min_pixel]))
    ax_min_pix.set_ylabel("Amplitude-Ped (ADC)")
    ax_min_pix.set_ylim(max_ylim)
    plotter.draw_waveform_positionline(start[min_pixel], ax_min_pix)
    plotter.draw_waveform_positionline(end[min_pixel], ax_min_pix)
    for i, ax in ax_min_nei.items():
        if len(min_pixel_nei) > i:
            pix = min_pixel_nei[i]
            plotter.draw_waveform(data_ped[pix], ax)
            ax.set_title("(Min Nei) Pixel: {}, "
                         "True: {}, "
                         "Measured = {}".format(pix, true_pe[pix],
                                                measured_pe[pix]))
            ax.set_ylabel("Amplitude-Ped (ADC)")
            ax.set_ylim(max_ylim)
            plotter.draw_waveform_positionline(start[pix], ax)
            plotter.draw_waveform_positionline(end[pix], ax)

    # Draw cameras
    nei_camera = np.zeros_like(max_charges, dtype=np.int)
    nei_camera[min_pixel_nei] = 2
    nei_camera[min_pixel] = 1
    nei_camera[max_pixel_nei] = 3
    nei_camera[max_pixel] = 4
    camera = plotter.draw_camera(telid, nei_camera, ax_img_nei)
    camera.cmap = plt.cm.viridis
    ax_img_nei.set_title("Neighbour Map")
    plotter.draw_camera_pixel_annotation(telid, max_pixel, min_pixel,
                                         ax_img_nei)

    camera = plotter.draw_camera(telid, data_ped[:, max_time], ax_img_max)
    camera.add_colorbar(ax=ax_img_max, label="Amplitude-Ped (ADC)")
    ax_img_max.set_title("Max Timeslice (T = {})".format(max_time))
    plotter.draw_camera_pixel_annotation(telid, max_pixel, min_pixel,
                                         ax_img_max)

    camera = plotter.draw_camera(telid, true_pe, ax_img_true)
    camera.add_colorbar(ax=ax_img_true, label="True Charge (Photo-electrons)")
    ax_img_true.set_title("True Charge")
    plotter.draw_camera_pixel_annotation(telid, max_pixel, min_pixel,
                                         ax_img_true)

    camera = plotter.draw_camera(telid, measured_pe, ax_img_cal)
    camera.add_colorbar(ax=ax_img_cal, label="Calib Charge (Photo-electrons)")
    ax_img_cal.set_title("Charge (integrator={})".format(params['integrator']))
    plotter.draw_camera_pixel_annotation(telid, max_pixel, min_pixel,
                                         ax_img_cal)

    fig_waveforms.suptitle("Integrator = {}".format(params['integrator']))
    fig_camera.suptitle("Camera = {}".format(geom.cam_id))

    # TODO: another figure of all waveforms that have non-zero true charge

    waveform_output_name = "{}_e{}_t{}_c{}_integrator{}_waveform.pdf"\
        .format(input_file.filename, event.count, telid, chan, args.integrator)
    camera_output_name = "{}_e{}_t{}_c{}_integrator{}_camera.pdf"\
        .format(input_file.filename, event.count, telid, chan, args.integrator)
    output_dir = args.output_dir if args.output_dir is not None else \
        input_file.output_directory
    output_dir = os.path.join(output_dir, script)
    if not os.path.exists(output_dir):
        log.info("[output] Creating directory: {}".format(output_dir))
        os.makedirs(output_dir)

    waveform_output_path = os.path.join(output_dir, waveform_output_name)
    log.info("[output] {}".format(waveform_output_path))
    fig_waveforms.savefig(waveform_output_path, format='pdf')

    camera_output_path = os.path.join(output_dir, camera_output_name)
    log.info("[output] {}".format(camera_output_path))
    fig_camera.savefig(camera_output_path, format='pdf')

    log.info("[COMPLETE]")
コード例 #8
0
    def initialize(self, argv=None):
        self.log.info("Initializing the calibration pipeline...")

        # Declare and parse command line option
        parser = argparse.ArgumentParser(
            description='Display each event in the file')
        parser.add_argument('-f', '--file', dest='input_path',
                            action='store',
                            default=get_path('gamma_test.simtel.gz'),
                            help='path to the input file. '
                            ' Default = gamma_test.simtel.gz')
        parser.add_argument('-O', '--origin', dest='origin',
                            action='store',
                            default='hessio',
                            help='origin of the file: {}. '
                            'Default = hessio'
                            .format(origin_list()))
        parser.add_argument('-D', dest='display', action='store_true',
                            default=False,
                            help='display the camera events')
        parser.add_argument('--pdf', dest='output_path', action='store',
                            default=None,
                            help='path to store a pdf output of the plots')
        parser.add_argument('-t', '--telescope', dest='tel',
                            action='store',
                            type=int, default=None,
                            help='telecope to view. Default = All')

        calibration_arguments(parser)

        logger_detail = parser.add_mutually_exclusive_group()
        logger_detail.add_argument('-q', '--quiet', dest='quiet',
                                   action='store_true', default=False,
                                   help='Quiet mode')
        logger_detail.add_argument('-v', '--verbose', dest='verbose',
                                   action='store_true', default=False,
                                   help='Verbose mode')
        logger_detail.add_argument('-d', '--debug', dest='debug',
                                   action='store_true', default=False,
                                   help='Debug mode')

        self.args = parser.parse_args()
        self.params = calibration_parameters(self.args)

        if self.args.quiet:
            self.log.setLevel(40)
        if self.args.verbose:
            self.log.setLevel(20)
        if self.args.debug:
            self.log.setLevel(10)

        self.log.debug("[file] Reading file")
        input_file = InputFile(self.args.input_path, self.args.origin)
        self.source = input_file.read()

        # geom_dict is a dictionary of CameraGeometry, with keys of
        # (num_pixels, focal_length), the parameters that are used to guess the
        # geometry of the telescope. By using these keys, the geometry is
        # calculated only once per telescope type as needed, reducing
        # computation time.
        # Creating a geom_dict at this point is optional, but is recommended,
        # as the same geom_dict can then be shared between the calibration and
        # CameraPlotter, again reducing computation time.
        # The dictionary becomes filled as a result of a dictionary's mutable
        # nature.def display_telescope(event, tel_id, display,
        # geom_dict, pp, fig):
        self.geom_dict = {}