def make_camera_binary_image(image, sigma, clean_bound, death_pixel_ids_list): fig, ax = plt.subplots(1, 2, figsize=(14, 7)) geom = CameraGeometry.from_name('LSTCam-003') disp0 = CameraDisplay(geom, ax=ax[0]) disp0.image = image disp0.cmap = plt.cm.gnuplot2 disp0.add_colorbar(ax=ax[0]) ax[0].set_title( "Cleaning threshold from interleaved pedestal. \n sigma = {}".format( sigma), fontsize=15) disp1 = CameraDisplay(geom, ax=ax[1]) disp1.image = image cmap = matplotlib.colors.ListedColormap(['black', 'red']) bounds = [0, clean_bound, 2 * clean_bound] norm = matplotlib.colors.BoundaryNorm(bounds, cmap.N) disp1.cmap = cmap disp1.add_colorbar(norm=norm, boundaries=bounds, ticks=[0, clean_bound, 2 * clean_bound]) disp1.set_limits_minmax(0, 2 * clean_bound) disp1.highlight_pixels(death_pixel_ids_list, linewidth=3) ax[1].set_title("Red pixels - above cleaning tailcut threshold", fontsize=15) plt.tight_layout() plt.show()
def create(self, df, geom): super().save() camera = CameraDisplay(geom, ax=self.ax, image=np.ma.zeros(2048), cmap='viridis') camera.add_colorbar(pad=-0.2) camera.colorbar.set_label("Peak Time (ns)", fontsize=20) with PdfPages(self.output_path) as pdf: n_rows = len(df.index) desc = "Saving image pages" for index, row in tqdm(df.iterrows(), total=n_rows, desc=desc): event_id = row['id'] tel = row['tel'] image = row['peak_time'] tc = row['tc'] hillas = row['hillas'] cleaned_image = np.ma.masked_array(image, mask=~tc) cleaned_image.fill_value = 0 max_ = np.percentile(cleaned_image.compressed(), 99) min_ = np.percentile(cleaned_image.compressed(), 1) camera.image = cleaned_image camera.set_limits_minmax(min_, max_) camera.highlight_pixels(np.arange(2048), 'black', 1, 0.2) # camera.overlay_moments_update(hillas, color='red') self.ax.set_title("Event: {}, Tel: {}".format(event_id, tel)) self.ax.axis('off') camera.colorbar.ax.tick_params(labelsize=30) pdf.savefig(self.fig)
def test_camera_display_single(): """ test CameraDisplay functionality """ from ..mpl_camera import CameraDisplay geom = CameraGeometry.from_name("LSTCam") disp = CameraDisplay(geom) image = np.random.normal(size=len(geom.pix_x)) disp.image = image disp.add_colorbar() disp.cmap = "nipy_spectral" disp.set_limits_minmax(0, 10) disp.set_limits_percent(95) disp.enable_pixel_picker() disp.highlight_pixels([1, 2, 3, 4, 5]) disp.norm = "log" disp.norm = "symlog" disp.cmap = "rainbow" with pytest.raises(ValueError): disp.image = np.ones(10) with pytest.raises(ValueError): disp.add_colorbar() disp.add_ellipse(centroid=(0, 0), width=0.1, length=0.1, angle=0.1) disp.clear_overlays()
def remove_star_and_run(self, list_of_file, max_events, noise_pixels_id_list): signal_place_after_clean = np.zeros(1855) sum_ped_ev = 0 alive_ped_ev = 0 for input_file in list_of_file: print(input_file) r0_r1_calibrator = LSTR0Corrections(pedestal_path=None, r1_sample_start=3, r1_sample_end=39) reader = LSTEventSource(input_url=input_file, max_events=max_events) for i, ev in enumerate(reader): r0_r1_calibrator.calibrate(ev) if i % 10000 == 0: print(ev.r0.event_id) if ev.lst.tel[1].evt.tib_masked_trigger == 32: sum_ped_ev += 1 self.r1_dl1_calibrator(ev) img = ev.dl1.tel[1].image img[noise_pixels_id_list] = 0 geom = ev.inst.subarray.tel[1].camera clean = tailcuts_clean(geom, img, **self.cleaning_parameters) cleaned = img.copy() cleaned[~clean] = 0.0 signal_place_after_clean[np.where(clean == True)] += 1 if np.sum(cleaned > 0) > 0: alive_ped_ev += 1 fig, ax = plt.subplots(figsize=(10, 8)) geom = ev.inst.subarray.tel[1].camera disp0 = CameraDisplay(geom, ax=ax) disp0.image = signal_place_after_clean / sum_ped_ev disp0.highlight_pixels(noise_pixels_id_list, linewidth=3) disp0.add_colorbar(ax=ax, label="N times signal remain after cleaning [%]") disp0.cmap = 'gnuplot2' ax.set_title("{} \n {}/{}".format( input_file.split("/")[-1][8:21], alive_ped_ev, sum_ped_ev), fontsize=25) print("{}/{}".format(alive_ped_ev, sum_ped_ev)) ax.set_xlabel(" ") ax.set_ylabel(" ") plt.tight_layout() plt.show()
def plot_event(event, reco, pdf): cams = [ event.inst.subarray.tels[i].camera for i in event.r0.tels_with_data ] cams = [c for c in cams if c.cam_id in allowed_cameras] n_tels = len(cams) p = 1 params = {} pointing_azimuth = {} pointing_altitude = {} for telescope_id, dl1 in event.dl1.tel.items(): camera = event.inst.subarray.tels[telescope_id].camera if camera.cam_id not in allowed_cameras: continue nn = int(np.ceil(np.sqrt(n_tels))) ax = plt.subplot(nn, nn, p) p += 1 boundary_thresh, picture_thresh = cleaning_level[camera.cam_id] mask = tailcuts_clean(camera, dl1.image[0], boundary_thresh=boundary_thresh, picture_thresh=picture_thresh, min_number_picture_neighbors=1) # if mask.sum() < 3: # only two pixel remaining. No luck anyways. continue h = hillas_parameters( camera[mask], dl1.image[0, mask], ) disp = CameraDisplay(camera, ax=ax, title="CT{0}".format(telescope_id)) disp.pixels.set_antialiaseds(False) disp.autoupdate = False disp.add_colorbar() # Show the camera image and overlay Hillas ellipse and clean pixels disp.image = dl1.image[0] disp.cmap = 'viridis' disp.highlight_pixels(mask, color='white') disp.overlay_moments(h, color='red', linewidth=5) pointing_azimuth[ telescope_id] = event.mc.tel[telescope_id].azimuth_raw * u.rad pointing_altitude[ telescope_id] = event.mc.tel[telescope_id].altitude_raw * u.rad params[telescope_id] = h return reco.predict(params, event.inst, pointing_altitude, pointing_azimuth)
def test_pixel_shapes(pix_type): """ test CameraDisplay functionality """ from ..mpl_camera import CameraDisplay geom = CameraGeometry.from_name("LSTCam") geom.pix_type = pix_type disp = CameraDisplay(geom) image = np.random.normal(size=len(geom.pix_x)) disp.image = image disp.add_colorbar() disp.highlight_pixels([1, 2, 3, 4, 5]) disp.add_ellipse(centroid=(0, 0), width=0.1, length=0.1, angle=0.1)
def main(filename, config_file=None): geom = read_camera_geometries(filename)["LSTCam"] dl1_parameters_table = Table.read(filename, path=dl1_params_lstcam_key) images_table = Table.read(filename, path=dl1_images_lstcam_key) dl1_table = join( dl1_parameters_table, images_table, keys=["event_id", "tel_id", "obs_id"] ) params_cleaning = get_cleaning_config(config_file) selected_table = dl1_table[np.isfinite(dl1_table["intensity"])] selected_table = dl1_table[dl1_table["intensity"] > 500] with PdfPages("images_examples.pdf") as pp: for ii, row in enumerate(selected_table[:10]): h = get_hillas_container(row) image = row["image"] peak_time = row["peak_time"] clean_mask = tailcuts_clean(geom, image, **params_cleaning) fig, axes = plt.subplots(1, 2, figsize=(12, 6)) fig.suptitle(f"event id : {row['event_id']}") ax = axes[0] display = CameraDisplay(geom, image, ax=ax) display.add_colorbar(ax=ax) ax.set_title("charges") display.highlight_pixels(clean_mask, color="red", alpha=0.33) display.overlay_moments(h) ax = axes[1] display = CameraDisplay(geom, peak_time, ax=ax) display.highlight_pixels(clean_mask, color="red", alpha=0.2) display.add_colorbar(ax=ax) ax.set_title("peak time") pp.savefig(dpi=100)
def plot_camera_display(self, image, input_file, noise_pixels_id_list, alive_ped_ev, sum_ped_ev): fig, ax = plt.subplots(figsize=(10, 8)) geom = CameraGeometry.from_name('LSTCam-003') disp0 = CameraDisplay(geom, ax=ax) disp0.image = image disp0.highlight_pixels(noise_pixels_id_list, linewidth=3) disp0.add_colorbar(ax=ax, label="N times signal remain after cleaning [%]") disp0.cmap = 'gnuplot2' ax.set_title("{} \n {}/{}".format( input_file.split("/")[-1][8:21], alive_ped_ev, sum_ped_ev), fontsize=25) print("{}/{}".format(alive_ped_ev, sum_ped_ev)) ax.set_xlabel(" ") ax.set_ylabel(" ") plt.tight_layout() plt.show()
def main(): args = parser.parse_args() event_generator = fact_event_generator( args.inputfile, args.drsfile, allowed_triggers={4}, ) fig = plt.figure(figsize=(12, 6)) ax1 = fig.add_axes([0, 0, 0.4, 1]) ax1.set_axis_off() divider = make_axes_locatable(ax1) cax1 = divider.append_axes('right', size="5%", pad=0.05) ax2 = fig.add_axes([0.5, 0.0, 0.4, 1]) ax2.set_axis_off() divider = make_axes_locatable(ax2) cax2 = divider.append_axes('right', size="5%", pad=0.05) geom = CameraGeometry.from_name('FACT') disp1 = CameraDisplay(geom, ax=ax1) disp1.add_colorbar(cax=cax1, label='Photons') disp2 = CameraDisplay(geom, ax=ax2) disp2.add_colorbar(cax=cax2, label='ArrivalTime') ax1.set_title('Photons') ax2.set_title('Peak Position') for e in event_generator: dl1_calibrator.calibrate(e) image = e.dl1.tel[0].image[0] cleaning_mask = tailcuts_clean(geom, image, 5, 3.5) if sum(cleaning_mask) < 15: continue hillas_container = hillas_parameters( geom.pix_x[cleaning_mask], geom.pix_y[cleaning_mask], image[cleaning_mask], ) disp1.overlay_moments(hillas_container, linewidth=1.5, color='c', with_label=False) disp1.highlight_pixels(cleaning_mask) disp1.image = e.dl1.tel[0].image[0] disp2.image = e.dl1.tel[0].peakpos[0] for disp in (disp1, disp2): disp.highlight_pixels(cleaning_mask, color='r', linewidth=1.5) fig.suptitle('FACT Event {}'.format(e.trig.gps_time.iso)) plt.pause(0.01) input('Press enter for next event')
from ctapipe.image import toymodel from ctapipe.instrument import CameraGeometry from ctapipe.visualization import CameraDisplay if __name__ == '__main__': plt.style.use('ggplot') fig = plt.figure(figsize=(12, 8)) ax = fig.add_subplot(1, 1, 1) geom = CameraGeometry.from_name('NectarCam') disp = CameraDisplay(geom, ax=ax) disp.add_colorbar() model = toymodel.generate_2d_shower_model( centroid=(0.05, 0.0), width=0.05, length=0.15, psi='35d' ) image, sig, bg = toymodel.make_toymodel_shower_image( geom, model.pdf, intensity=1500, nsb_level_pe=5 ) disp.image = image mask = disp.image > 10 disp.highlight_pixels(mask, linewidth=2, color='crimson') plt.show()
def plot_event(fitter, image, geometry, n_sigma=3, init=False, clean_mask=None, show_ellipsis=True, save=False, ids=''): """ Plot the image of the event in the camera along with the extracted ellipsis before or after the fitting procedure. Parameters ---------- image: Distribution of signal for the event in number of p.e. n_sigma: float Multiplicative factor on the extracted width and length used for the displayed ellipsis init: boolean If True, use the starting parameters for the ellipsis If False, use the ending parameters for the ellipsis clean_mask: boolean array cleaning selected pixels for the Hillas parameters extraction show_ellipsis: boolean If True, display the ellipsis save: bool Save and close the figure if True, return it otherwise ids: string Can be used to modify the save location Returns ------- cam_display: `ctapipe.visualization.CameraDisplay` Camera image using matplotlib """ fig, axes = plt.subplots(figsize=(10, 8)) cam_display = CameraDisplay(geometry, image, ax=axes) cam_display.add_colorbar(ax=axes) if init: params = fitter.start_parameters else: params = fitter.end_parameters length = n_sigma * params['length'] psi = params['psi'] if show_ellipsis: cam_display.add_ellipse(centroid=(params['x_cm'], params['y_cm']), width=n_sigma * params['wl'] * params['length'], length=length, angle=psi, linewidth=6, color='r', linestyle='--', label=r'{} $\sigma$ contour'.format(n_sigma)) cam_display.axes.legend(loc='best') if init and clean_mask is not None: cam_display.highlight_pixels(clean_mask, color='r') if save: cam_display.axes.get_figure().savefig('event/' + ids + '_init' + str(init) + '.png') plt.close() return None if save else cam_display
def nsb_rate( baseline_histo_file, dark_histo_file, param_file, template_filename, plot="show", plot_nsb_range=None, norm="log", bias_resistance=1e4 * u.Ohm, cell_capacitance=5e-14 * u.Farad ): baseline_histo = Histogram1D.load(baseline_histo_file) dark_histo = Histogram1D.load(dark_histo_file) baseline_shift = baseline_histo.mean()-dark_histo.mean() n_pixel = len(DigiCam.geometry.neighbors) pixels = np.arange(n_pixel, dtype=int) with open(param_file) as file: pulse_template = NormalizedPulseTemplate.load(template_filename) pulse_area = pulse_template.integral() * u.ns charge_to_amplitude = pulse_template.compute_charge_amplitude_ratio(7, 4) calibration_parameters = yaml.load(file) gain_integral = np.array(calibration_parameters['gain']) gain_amplitude = gain_integral * charge_to_amplitude crosstalk = np.array(calibration_parameters['mu_xt']) rate = _compute_nsb_rate( baseline_shift=baseline_shift, gain=gain_amplitude, pulse_area=pulse_area, crosstalk=crosstalk, bias_resistance=bias_resistance, cell_capacitance=cell_capacitance ) bad_pixels = get_bad_pixels( calib_file=param_file, nsigma_gain=5, nsigma_elecnoise=5, dark_histo=dark_histo_file, nsigma_dark=8, plot=None, output=None ) bad_pixels = np.unique(np.hstack( ( bad_pixels, pixels[rate < 0], pixels[rate > 5 * u.GHz] ) )) avg_matrix = _get_average_matrix_bad_pixels(DigiCam.geometry, bad_pixels) good_pixels_mask = np.ones(n_pixel, dtype=bool) good_pixels_mask[bad_pixels] = False good_pixels = pixels[good_pixels_mask] rate[bad_pixels] = avg_matrix[bad_pixels, :].dot(rate[good_pixels]) if plot is None: return rate fig1, ax = plt.subplots(1, 1) display = CameraDisplay(DigiCam.geometry, ax=ax, norm=norm, title='NSB rate [GHz]') rate_ghz = rate.to(u.GHz).value display.image = rate_ghz if plot_nsb_range is None: plot_nsb_range = (np.min(rate_ghz), np.max(rate_ghz)) display.set_limits_minmax(*plot_nsb_range) display.add_colorbar(ax=ax) display.highlight_pixels(bad_pixels, color='r', linewidth=2) plt.tight_layout() output_path = os.path.dirname(plot) if plot == "show" or \ (output_path != "" and not os.path.isdir(output_path)): if not plot == "show": print('WARNING: Path ' + output_path + ' for output trigger ' + 'uniformity does not exist, displaying the plot instead.\n') plt.show() else: plt.savefig(plot) print(plot, 'created') plt.close(fig1) return rate
def plot_pedestals(data_file, pedestal_file, run=0, plot_file=None, tel_id=1, offset_value=400, sample_size=1000): """ plot pedestal quantities quantities Parameters ---------- data_file: pedestal run pedestal_file: file with drs4 corrections run: run number of data to be corrected plot_file: name of output pdf file tel_id: id of the telescope offset_value: baseline off_set """ config = { "LSTEventSource": { "allowed_tels": [1], "LSTR0Corrections": { "drs4_pedestal_path": pedestal_file, }, } } # event_reader reader = EventSource(data_file, config=Config(config), max_events=None) t = np.linspace(2, 37, 36) # configuration for the charge integrator charge_config = Config( { "FixedWindowSum": { "window_shift": 6, "window_width": 12, "peak_index": 18, } } ) # declare the pedestal component pedestal = PedestalIntegrator( tel_id=tel_id, time_sampling_correction_path=None, sample_size=sample_size, sample_duration=1000000, charge_median_cut_outliers=[-10, 10], charge_std_cut_outliers=[-10, 10], charge_product="FixedWindowSum", config=charge_config, subarray=reader.subarray, ) for i, event in enumerate(reader): if tel_id != event.trigger.tels_with_trigger[0]: raise Exception( f"Given wrong telescope id {tel_id}, files has id {event.trigger.tels_with_trigger[0]}" ) are_pedestals_calculated = pedestal.calculate_pedestals(event) if are_pedestals_calculated: ped_data = event.mon.tel[tel_id].pedestal break camera_geometry = reader.subarray.tels[tel_id].camera.geometry camera_geometry = camera_geometry.transform_to(EngineeringCameraFrame()) if are_pedestals_calculated and plot_file is not None: with PdfPages(plot_file) as pdf: plt.rc("font", size=15) # first figure fig = plt.figure(1, figsize=(12, 24)) plt.tight_layout() n_samples = charge_config["FixedWindowSum"]["window_width"] fig.suptitle(f"Run {run}, integration on {n_samples} samples", fontsize=25) pad = 420 image = ped_data.charge_median mask = ped_data.charge_median_outliers for chan in np.arange(2): pad += 1 plt.subplot(pad) plt.tight_layout() disp = CameraDisplay(camera_geometry) mymin = np.median(image[chan]) - 2 * np.std(image[chan]) mymax = np.median(image[chan]) + 2 * np.std(image[chan]) disp.set_limits_minmax(mymin, mymax) disp.highlight_pixels(mask[chan], linewidth=2) disp.image = image[chan] disp.cmap = plt.cm.coolwarm # disp.axes.text(lposx, 0, f'{channel[chan]} pedestal [ADC]', rotation=90) plt.title(f"{channel[chan]} pedestal [ADC]") disp.add_colorbar() image = ped_data.charge_std mask = ped_data.charge_std_outliers for chan in np.arange(2): pad += 1 plt.subplot(pad) plt.tight_layout() disp = CameraDisplay(camera_geometry) mymin = np.median(image[chan]) - 2 * np.std(image[chan]) mymax = np.median(image[chan]) + 2 * np.std(image[chan]) disp.set_limits_minmax(mymin, mymax) disp.highlight_pixels(mask[chan], linewidth=2) disp.image = image[chan] disp.cmap = plt.cm.coolwarm # disp.axes.text(lposx, 0, f'{channel[chan]} pedestal std [ADC]', rotation=90) plt.title(f"{channel[chan]} pedestal std [ADC]") disp.add_colorbar() # histograms for chan in np.arange(2): mean_ped = ped_data.charge_mean[chan] ped_std = ped_data.charge_std[chan] # select good pixels select = np.logical_not(mask[chan]) # fig.suptitle(f"Run {run} channel: {channel[chan]}", fontsize=25) pad += 1 # pedestal charge plt.subplot(pad) plt.tight_layout() plt.ylabel("pixels") plt.xlabel(f"{channel[chan]} pedestal") median = np.median(mean_ped[select]) rms = np.std(mean_ped[select]) label = f"{channel[chan]} Median {median:3.2f}, std {rms:3.2f}" plt.hist(mean_ped[select], bins=50, label=label) plt.legend() pad += 1 # pedestal std plt.subplot(pad) plt.ylabel("pixels") plt.xlabel(f"{channel[chan]} pedestal std") median = np.median(ped_std[select]) rms = np.std(ped_std[select]) label = f" Median {median:3.2f}, std {rms:3.2f}" plt.hist(ped_std[select], bins=50, label=label) plt.legend() plt.subplots_adjust(top=0.94, bottom=0.04, right=0.96) pdf.savefig() plt.close() # event_reader # reader = EventSource(data_file, config=Config(config), max_events=1000) pix = 0 pad = 420 offset_value = reader.r0_r1_calibrator.offset.tel[tel_id] # plot corrected waveforms of first 8 events for i, ev in enumerate(reader): for chan in np.arange(2): if pad == 420: # new figure fig = plt.figure(ev.index.event_id * 1000, figsize=(12, 24)) fig.suptitle(f"Run {run}, pixel {pix}", fontsize=25) plt.tight_layout() pad += 1 plt.subplot(pad) # remove samples at beginning / end of waveform start = reader.r0_r1_calibrator.r1_sample_start.tel[tel_id] end = reader.r0_r1_calibrator.r1_sample_end.tel[tel_id] plt.subplots_adjust(top=0.92) label = f"event {ev.index.event_id}, {channel[chan]}: R0" plt.step( t, ev.r0.tel[tel_id].waveform[chan, pix, start:end], color="blue", label=label, ) label = "baseline correction \n + dt corr + interp. spikes" plt.step( t, ev.r1.tel[tel_id].waveform[chan, pix] + offset_value, alpha=0.5, color="green", label=label, ) plt.plot([0, 40], [offset_value, offset_value], "k--", label="offset") plt.xlabel("time sample [ns]") plt.ylabel("counts [ADC]") plt.legend() plt.ylim(200, 600) if pad == 428: pad = 420 plt.subplots_adjust(top=0.92) pdf.savefig() plt.close() if i == 8: break elif not are_pedestals_calculated: log.error("Not able to calculate pedestals or output pdf file not especified.") elif plot_file is None: log.warning("Not PDF outputfile specified.")
from ctapipe.image import toymodel from ctapipe.io import CameraGeometry from ctapipe.visualization import CameraDisplay from matplotlib import pyplot as plt if __name__ == '__main__': plt.style.use('ggplot') fig = plt.figure(figsize=(12, 8)) ax = fig.add_subplot(1, 1, 1) geom = CameraGeometry.from_name('hess', 1) disp = CameraDisplay(geom, ax=ax) disp.add_colorbar() model = toymodel.generate_2d_shower_model( centroid=(0.05, 0.0), width=0.005, length=0.025, psi='35d' ) image, sig, bg = toymodel.make_toymodel_shower_image( geom, model.pdf, intensity=50, nsb_level_pe=20 ) disp.image = image mask = disp.image > 15 disp.highlight_pixels(mask, linewidth=3) plt.show()
# ax1.set_yticks([]) ax1.set_xlabel('$x$-position / \\si{\\metre}') ax1.set_ylabel('$y$-position / \\si{\\metre}') ax1.set_title('') # # ax.set_axis('off') # mask = disp.image > 10 # disp.highlight_pixels(mask, linewidth=1, color='crimson') waveforms = event.dl0.tel[tel_id].waveform.T # from IPython import embed; embed() m = (waveforms.max(axis=0) > 5) & (waveforms.max(axis=0) < 10) selected_pixel = np.argmax(m) mask = np.zeros_like(m).astype(np.bool) mask[selected_pixel] = True disp.highlight_pixels(mask, linewidth=1, color='C0', alpha=1) t = np.arange(0, waveforms.shape[0], 1) ax2.plot(t, waveforms[:, selected_pixel], color='C0', lw=1) # from IPython import embed; embed() ax2.set_xlabel('Time / \\si{\\nano\\second}') ax2.set_ylabel('Voltage / a.u.') ax2.set_title('') ax2.set_xlim([0, 29]) plt.tight_layout(pad=0, rect=(0.0, 0, 1.007, 1)) plt.subplots_adjust(wspace=0.35) plt.savefig('build/preprocessing.pdf') with open('build/preprocessing_energy.txt', 'w') as f: energy = event.mc.energy.to_value('TeV')
def plot_all(ped_data, ff_data, calib_data, run=0, plot_file="none"): """ plot camera calibration quantities Parameters ---------- ped_data: pedestal container PedestalContainer() ff_data: flat-field container FlatFieldContainer() calib_data: calibration container WaveformCalibrationContainer() """ camera = CameraGeometry.from_name("LSTCam", 2) # plot open pdf if plot_file != "none": pp = PdfPages(plot_file) plt.rc('font', size=15) ### first figure fig = plt.figure(1, figsize=(12, 24)) plt.tight_layout() fig.suptitle(f"Run {run}", fontsize=25) pad = 420 image = ff_data.charge_median mask = ff_data.charge_median_outliers for chan in (np.arange(2)): pad += 1 plt.subplot(pad) plt.tight_layout() disp = CameraDisplay(camera) mymin = np.median(image[chan]) - 2 * np.std(image[chan]) mymax = np.median(image[chan]) + 2 * np.std(image[chan]) disp.set_limits_minmax(mymin, mymax) disp.highlight_pixels(mask[chan], linewidth=2) disp.image = image[chan] disp.cmap = plt.cm.coolwarm #disp.axes.text(lposx, 0, f'{channel[chan]} signal charge (ADC)', rotation=90) plt.title(f'{channel[chan]} signal charge [ADC]') disp.add_colorbar() image = ff_data.charge_std mask = ff_data.charge_std_outliers for chan in (np.arange(2)): pad += 1 plt.subplot(pad) plt.tight_layout() disp = CameraDisplay(camera) mymin = np.median(image[chan]) - 2 * np.std(image[chan]) mymax = np.median(image[chan]) + 2 * np.std(image[chan]) disp.set_limits_minmax(mymin, mymax) disp.highlight_pixels(mask[chan], linewidth=2) disp.image = image[chan] disp.cmap = plt.cm.coolwarm #disp.axes.text(lposx, 0, f'{channel[chan]} signal std [ADC]', rotation=90) plt.title(f'{channel[chan]} signal std [ADC]') disp.add_colorbar() image = ped_data.charge_median mask = ped_data.charge_median_outliers for chan in (np.arange(2)): pad += 1 plt.subplot(pad) plt.tight_layout() disp = CameraDisplay(camera) mymin = np.median(image[chan]) - 2 * np.std(image[chan]) mymax = np.median(image[chan]) + 2 * np.std(image[chan]) disp.set_limits_minmax(mymin, mymax) disp.highlight_pixels(mask[chan], linewidth=2) disp.image = image[chan] disp.cmap = plt.cm.coolwarm #disp.axes.text(lposx, 0, f'{channel[chan]} pedestal [ADC]', rotation=90) plt.title(f'{channel[chan]} pedestal [ADC]') disp.add_colorbar() image = ped_data.charge_std mask = ped_data.charge_std_outliers for chan in (np.arange(2)): pad += 1 plt.subplot(pad) plt.tight_layout() disp = CameraDisplay(camera) mymin = np.median(image[chan]) - 2 * np.std(image[chan]) mymax = np.median(image[chan]) + 2 * np.std(image[chan]) disp.set_limits_minmax(mymin, mymax) disp.highlight_pixels(mask[chan], linewidth=2) disp.image = image[chan] disp.cmap = plt.cm.coolwarm #disp.axes.text(lposx, 0, f'{channel[chan]} pedestal std [ADC]', rotation=90) plt.title(f'{channel[chan]} pedestal std [ADC]') disp.add_colorbar() plt.subplots_adjust(top=0.92) if plot_file != "none": pp.savefig() ### second figure fig = plt.figure(2, figsize=(12, 24)) plt.tight_layout() fig.suptitle(f"Run {run}", fontsize=25) pad = 420 # time image = ff_data.time_median mask = ff_data.time_median_outliers for chan in (np.arange(2)): pad += 1 plt.subplot(pad) plt.tight_layout() disp = CameraDisplay(camera) disp.highlight_pixels(mask[chan], linewidth=2) disp.image = image[chan] disp.cmap = plt.cm.coolwarm #disp.axes.text(lposx, 0, f'{channel[chan]} time', rotation=90) plt.title(f'{channel[chan]} time') disp.add_colorbar() image = ff_data.relative_gain_median mask = calib_data.unusable_pixels for chan in (np.arange(2)): pad += 1 plt.subplot(pad) plt.tight_layout() disp = CameraDisplay(camera) disp.highlight_pixels(mask[chan], linewidth=2) mymin = np.median(image[chan]) - 2 * np.std(image[chan]) mymax = np.median(image[chan]) + 2 * np.std(image[chan]) disp.set_limits_minmax(mymin, mymax) disp.image = image[chan] disp.cmap = plt.cm.coolwarm disp.set_limits_minmax(0.7, 1.3) plt.title(f'{channel[chan]} relative gain') #disp.axes.text(lposx, 0, f'{channel[chan]} relative gain', rotation=90) disp.add_colorbar() # pe image = calib_data.n_pe mask = calib_data.unusable_pixels image = np.where(np.isnan(image), 0, image) for chan in (np.arange(2)): pad += 1 plt.subplot(pad) plt.tight_layout() disp = CameraDisplay(camera) disp.highlight_pixels(mask[chan], linewidth=2) disp.image = image[chan] mymin = np.median(image[chan]) - 2 * np.std(image[chan]) mymax = np.median(image[chan]) + 2 * np.std(image[chan]) disp.set_limits_minmax(mymin, mymax) disp.cmap = plt.cm.coolwarm plt.title(f'{channel[chan]} photon-electrons') #disp.axes.text(lposx, 0, f'{channel[chan]} photon-electrons', rotation=90) disp.add_colorbar() # pe histogram pad += 1 plt.subplot(pad) plt.tight_layout() for chan in np.arange(2): n_pe = calib_data.n_pe[chan] # select good pixels select = np.logical_not(mask[chan]) median = int(np.median(n_pe[select])) rms = np.std(n_pe[select]) mymin = median - 4 * rms mymax = median + 4 * rms label = f"{channel[chan]} Median {median:3.2f}, std {rms:5.2f}" plt.hist(n_pe[select], label=label, histtype='step', range=(mymin, mymax), bins=50, stacked=True, alpha=0.5, fill=True) plt.legend() plt.xlabel(f'pe', fontsize=20) plt.ylabel('pixels', fontsize=20) # pe scatter plot pad += 1 plt.subplot(pad) plt.tight_layout() HG = calib_data.n_pe[0] LG = calib_data.n_pe[1] HG = np.where(np.isnan(HG), 0, HG) LG = np.where(np.isnan(LG), 0, LG) mymin = np.median(LG) - 2 * np.std(LG) mymax = np.median(LG) + 2 * np.std(LG) plt.hist2d(LG, HG, bins=[100, 100]) plt.xlabel("LG", fontsize=20) plt.ylabel("HG", fontsize=20) x = np.arange(mymin, mymax) plt.plot(x, x) plt.ylim(mymin, mymax) plt.xlim(mymin, mymax) plt.subplots_adjust(top=0.92) if plot_file != "none": pp.savefig() ### figures 3 and 4 : histograms for chan in np.arange(2): n_pe = calib_data.n_pe[chan] gain_median = ff_data.relative_gain_median[chan] #charge_median = ff_data.charge_median[chan] charge_mean = ff_data.charge_mean[chan] charge_std = ff_data.charge_std[chan] #median_ped = ped_data.charge_median[chan] mean_ped = ped_data.charge_mean[chan] ped_std = ped_data.charge_std[chan] # select good pixels select = np.logical_not(mask[chan]) fig = plt.figure(chan + 10, figsize=(12, 18)) fig.tight_layout(rect=[0, 0.03, 1, 0.95]) fig.suptitle(f"Run {run} channel: {channel[chan]}", fontsize=25) # charge plt.subplot(321) plt.tight_layout() median = int(np.median(charge_mean[select])) rms = np.std(charge_mean[select]) label = f"Median {median:3.2f}, std {rms:5.0f}" plt.xlabel('charge (ADC)', fontsize=20) plt.ylabel('pixels', fontsize=20) plt.hist(charge_mean[select], bins=50, label=label) plt.legend() plt.subplot(322) plt.tight_layout() plt.ylabel('pixels', fontsize=20) plt.xlabel('charge std', fontsize=20) median = np.median(charge_std[select]) rms = np.std(charge_std[select]) label = f"Median {median:3.2f}, std {rms:3.2f}" plt.hist(charge_std[select], bins=50, label=label) plt.legend() # pedestal charge plt.subplot(323) plt.tight_layout() plt.ylabel('pixels', fontsize=20) plt.xlabel('pedestal', fontsize=20) median = np.median(mean_ped[select]) rms = np.std(mean_ped[select]) label = f"Median {median:3.2f}, std {rms:3.2f}" plt.hist(mean_ped[select], bins=50, label=label) plt.legend() # pedestal std plt.subplot(324) plt.ylabel('pixels', fontsize=20) plt.xlabel('pedestal std', fontsize=20) median = np.median(ped_std[select]) rms = np.std(ped_std[select]) label = f"Median {median:3.2f}, std {rms:3.2f}" plt.hist(ped_std[select], bins=50, label=label) plt.legend() # relative gain plt.subplot(325) plt.tight_layout() plt.ylabel('pixels', fontsize=20) plt.xlabel('relative gain', fontsize=20) median = np.median(gain_median[select]) rms = np.std(gain_median[select]) label = f"Relative gain {median:3.2f}, std {rms:5.2f}" plt.hist(gain_median[select], bins=50, label=label) plt.legend() # photon electrons plt.subplot(326) plt.tight_layout() plt.ylabel('pixels', fontsize=20) plt.xlabel('pe', fontsize=20) median = np.median(n_pe[select]) rms = np.std(n_pe[select]) label = f"Median {median:3.2f}, std {rms:3.2f}" plt.hist(n_pe[select], bins=50, label=label) plt.legend() plt.subplots_adjust(top=0.92) if plot_file != "none": pp.savefig(plt.gcf()) if plot_file != "none": pp.close()
if __name__ == '__main__': # Load the camera geom = CameraGeometry.from_name("LSTCam") disp = CameraDisplay(geom) disp.add_colorbar() # Create a fake camera image to display: model = toymodel.generate_2d_shower_model( centroid=(0.2, 0.0), width=0.05, length=0.15, psi='35d' ) image, sig, bg = toymodel.make_toymodel_shower_image( geom, model.pdf, intensity=1500, nsb_level_pe=3 ) # Apply image cleaning cleanmask = tailcuts_clean( geom, image, picture_thresh=10, boundary_thresh=5 ) # Calculate image parameters hillas = hillas_parameters(geom[cleanmask], image[cleanmask]) # Show the camera image and overlay Hillas ellipse and clean pixels disp.image = image disp.highlight_pixels(cleanmask, color='crimson') disp.overlay_moments(hillas, color='cyan', linewidth=3) plt.show()
def start(self): # Get first event information first_event = self.reader.get_event(0) n_pixels = first_event.inst.num_pixels[0] n_samples = first_event.r0.tel[0].num_samples pos = first_event.inst.pixel_pos[0] foclen = first_event.inst.optical_foclen[0] geom = CameraGeometry.guess(*pos, foclen) # Setup Output output_dir = self.reader.output_directory title = self.reader.filename title = title[:title.find("_")] # Prepare Output if not exists(output_dir): self.log.info("Creating directory: {}".format(output_dir)) makedirs(output_dir) output_path = join(output_dir, title + "_events.pdf") # Setup plot fig = plt.figure(figsize=(10, 10)) ax_camera = fig.add_subplot(1, 1, 1) fig.patch.set_visible(False) ax_camera.axis('off') camera = CameraDisplay(geom, ax=ax_camera, image=np.zeros(2048), cmap='viridis') camera.add_colorbar() cb = camera.colorbar camera.colorbar.set_label("Amplitude (p.e.)") fig.suptitle(title) source = self.reader.read() desc = "Looping through file" with PdfPages(output_path) as pdf: for event in tqdm(source, desc=desc): ev = event.count event_id = event.r0.event_id self.r1.calibrate(event) self.dl0.reduce(event) self.dl1.calibrate(event) for t in event.r0.tels_with_data: dl1 = event.dl1.tel[t].image[0] # Cleaning tc = tailcuts_clean(geom, dl1, 20, 10) if not tc.any(): continue cleaned_dl1 = np.ma.masked_array(dl1, mask=~tc) try: # hillas = hillas_parameters(*pos, cleaned_tc) hillas = hillas_parameters_4(*pos, cleaned_dl1) except HillasParameterizationError: continue ax_camera.cla() camera = CameraDisplay(geom, ax=ax_camera, image=np.zeros(2048), cmap='viridis') camera.colorbar = cb camera.image = dl1 max_ = cleaned_dl1.max() # np.percentile(dl1, 99.9) min_ = np.percentile(dl1, 0.1) camera.set_limits_minmax(min_, max_) camera.highlight_pixels(tc, 'white') camera.overlay_moments(hillas, color='red') camera.update(True) ax_camera.set_title("Event: {}".format(event_id)) ax_camera.axis('off') pdf.savefig(fig) self.log.info("Created images: {}".format(output_path))
def plot_all(ped_data, ff_data, calib_data, run=0, plot_file=None): """ plot camera calibration quantities Parameters ---------- ped_data: pedestal container PedestalContainer() ff_data: flat-field container FlatFieldContainer() calib_data: calibration container WaveformCalibrationContainer() run: run number plot_file: name of the output PDF file. No file is produced if name is not provided """ # read geometry camera = load_camera_geometry() camera = camera.transform_to(EngineeringCameraFrame()) # plot open pdf if plot_file is not None: with PdfPages(plot_file) as pdf: plt.rc("font", size=15) # first figure fig = plt.figure(1, figsize=(12, 24)) plt.tight_layout() fig.suptitle(f"Run {run}", fontsize=25) pad = 420 image = ff_data.charge_median mask = ff_data.charge_median_outliers for chan in np.arange(2): pad += 1 plt.subplot(pad) plt.tight_layout() disp = CameraDisplay(camera) mymin = np.median(image[chan]) - 2 * np.std(image[chan]) mymax = np.median(image[chan]) + 2 * np.std(image[chan]) disp.set_limits_minmax(mymin, mymax) disp.highlight_pixels(mask[chan], linewidth=2) disp.image = image[chan] disp.cmap = plt.cm.coolwarm # disp.axes.text(lposx, 0, f'{channel[chan]} signal charge (ADC)', rotation=90) plt.title(f"{channel[chan]} signal charge [ADC]") disp.add_colorbar() image = ff_data.charge_std mask = ff_data.charge_std_outliers for chan in np.arange(2): pad += 1 plt.subplot(pad) plt.tight_layout() disp = CameraDisplay(camera) mymin = np.median(image[chan]) - 2 * np.std(image[chan]) mymax = np.median(image[chan]) + 2 * np.std(image[chan]) disp.set_limits_minmax(mymin, mymax) disp.highlight_pixels(mask[chan], linewidth=2) disp.image = image[chan] disp.cmap = plt.cm.coolwarm # disp.axes.text(lposx, 0, f'{channel[chan]} signal std [ADC]', rotation=90) plt.title(f"{channel[chan]} signal std [ADC]") disp.add_colorbar() image = ped_data.charge_median mask = ped_data.charge_median_outliers for chan in np.arange(2): pad += 1 plt.subplot(pad) plt.tight_layout() disp = CameraDisplay(camera) mymin = np.median(image[chan]) - 2 * np.std(image[chan]) mymax = np.median(image[chan]) + 2 * np.std(image[chan]) disp.set_limits_minmax(mymin, mymax) disp.highlight_pixels(mask[chan], linewidth=2) disp.image = image[chan] disp.cmap = plt.cm.coolwarm # disp.axes.text(lposx, 0, f'{channel[chan]} pedestal [ADC]', rotation=90) plt.title(f"{channel[chan]} pedestal [ADC]") disp.add_colorbar() image = ped_data.charge_std mask = ped_data.charge_std_outliers for chan in np.arange(2): pad += 1 plt.subplot(pad) plt.tight_layout() disp = CameraDisplay(camera) mymin = np.median(image[chan]) - 2 * np.std(image[chan]) mymax = np.median(image[chan]) + 2 * np.std(image[chan]) disp.set_limits_minmax(mymin, mymax) disp.highlight_pixels(mask[chan], linewidth=2) disp.image = image[chan] disp.cmap = plt.cm.coolwarm # disp.axes.text(lposx, 0, f'{channel[chan]} pedestal std [ADC]', rotation=90) plt.title(f"{channel[chan]} pedestal std [ADC]") disp.add_colorbar() plt.subplots_adjust(top=0.92) pdf.savefig() plt.close() # second figure fig = plt.figure(2, figsize=(12, 24)) plt.tight_layout() fig.suptitle(f"Run {run}", fontsize=25) pad = 420 # time image = ff_data.time_median mask = ff_data.time_median_outliers for chan in np.arange(2): pad += 1 plt.subplot(pad) plt.tight_layout() disp = CameraDisplay(camera) disp.highlight_pixels(mask[chan], linewidth=2) disp.image = image[chan] disp.cmap = plt.cm.coolwarm # disp.axes.text(lposx, 0, f'{channel[chan]} time', rotation=90) plt.title(f"{channel[chan]} time") disp.add_colorbar() image = ff_data.relative_gain_median mask = calib_data.unusable_pixels for chan in np.arange(2): pad += 1 plt.subplot(pad) plt.tight_layout() disp = CameraDisplay(camera) disp.highlight_pixels(mask[chan], linewidth=2) mymin = np.median(image[chan]) - 2 * np.std(image[chan]) mymax = np.median(image[chan]) + 2 * np.std(image[chan]) disp.set_limits_minmax(mymin, mymax) disp.image = image[chan] disp.cmap = plt.cm.coolwarm disp.set_limits_minmax(0.7, 1.3) plt.title(f"{channel[chan]} relative signal") # disp.axes.text(lposx, 0, f'{channel[chan]} relative gain', rotation=90) disp.add_colorbar() # pe image = calib_data.n_pe mask = calib_data.unusable_pixels image = np.where(np.isnan(image), 0, image) for chan in np.arange(2): pad += 1 plt.subplot(pad) plt.tight_layout() disp = CameraDisplay(camera) disp.highlight_pixels(mask[chan], linewidth=2) disp.image = image[chan] mymin = np.median(image[chan]) - 2 * np.std(image[chan]) mymax = np.median(image[chan]) + 2 * np.std(image[chan]) disp.set_limits_minmax(mymin, mymax) disp.cmap = plt.cm.coolwarm plt.title(f"{channel[chan]} photon-electrons") # disp.axes.text(lposx, 0, f'{channel[chan]} photon-electrons', rotation=90) disp.add_colorbar() # pe histogram pad += 1 plt.subplot(pad) plt.tight_layout() for chan in np.arange(2): n_pe = calib_data.n_pe[chan] # select good pixels select = np.logical_not(mask[chan]) median = int(np.median(n_pe[select])) rms = np.std(n_pe[select]) mymin = median - 4 * rms mymax = median + 4 * rms label = f"{channel[chan]} Median {median:3.2f}, std {rms:5.2f}" plt.hist( n_pe[select], label=label, histtype="step", range=(mymin, mymax), bins=50, stacked=True, alpha=0.5, fill=True, ) plt.legend() plt.xlabel("pe", fontsize=20) plt.ylabel("pixels", fontsize=20) # pe scatter plot pad += 1 plt.subplot(pad) plt.tight_layout() HG = calib_data.n_pe[0] LG = calib_data.n_pe[1] HG = np.where(np.isnan(HG), 0, HG) LG = np.where(np.isnan(LG), 0, LG) mymin = np.median(LG) - 2 * np.std(LG) mymax = np.median(LG) + 2 * np.std(LG) plt.hist2d(LG, HG, bins=[100, 100]) plt.xlabel("LG", fontsize=20) plt.ylabel("HG", fontsize=20) x = np.arange(mymin, mymax) plt.plot(x, x) plt.ylim(mymin, mymax) plt.xlim(mymin, mymax) plt.subplots_adjust(top=0.92) pdf.savefig() plt.close() # figures 3 and 4: histograms for chan in np.arange(2): n_pe = calib_data.n_pe[chan] gain_median = ff_data.relative_gain_median[chan] charge_median = ff_data.charge_median[chan] #charge_mean = ff_data.charge_mean[chan] charge_std = ff_data.charge_std[chan] n_ff = ff_data.n_events median_ped = ped_data.charge_median[chan] #mean_ped = ped_data.charge_mean[chan] ped_std = ped_data.charge_std[chan] n_ped = ped_data.n_events dc_to_pe = calib_data.dc_to_pe[chan] time_correction = calib_data.time_correction[chan] # select good pixels select = np.logical_not(mask[chan]) fig = plt.figure(chan + 10, figsize=(12, 24)) fig.tight_layout(rect=[0, 0.0, 1, 0.95]) fig.suptitle(f"Run {run} channel: {channel[chan]}", fontsize=25) # charge plt.subplot(421) plt.title(f"FF sample of {n_ff} events") plt.tight_layout() median = int(np.median(charge_median[select])) rms = np.std(charge_median[select]) label = f"Median {median:3.2f}, std {rms:5.0f}" plt.xlabel("charge (ADC)", fontsize=20) plt.ylabel("pixels", fontsize=20) plt.hist(charge_median[select], bins=50, label=label) plt.legend() plt.subplot(422) plt.tight_layout() plt.ylabel("pixels", fontsize=20) plt.xlabel("charge std", fontsize=20) median = np.median(charge_std[select]) rms = np.std(charge_std[select]) label = f"Median {median:3.2f}, std {rms:3.2f}" plt.hist(charge_std[select], bins=50, label=label) plt.legend() # pedestal charge plt.subplot(423) plt.tight_layout() plt.title(f"pedestal sample of {n_ped} events") plt.ylabel("pixels", fontsize=20) plt.xlabel("pedestal", fontsize=20) median = np.median(median_ped[select]) rms = np.std(median_ped[select]) label = f"Median {median:3.2f}, std {rms:3.2f}" plt.hist(median_ped[select], bins=50, label=label) plt.legend() # pedestal std plt.subplot(424) plt.ylabel("pixels", fontsize=20) plt.xlabel("pedestal std", fontsize=20) median = np.median(ped_std[select]) rms = np.std(ped_std[select]) label = f"Median {median:3.2f}, std {rms:3.2f}" plt.hist(ped_std[select], bins=50, label=label) plt.legend() # relative gain plt.subplot(425) plt.tight_layout() plt.ylabel("pixels", fontsize=20) plt.xlabel("relative signal", fontsize=20) median = np.median(gain_median[select]) rms = np.std(gain_median[select]) label = f"Relative gain {median:3.2f}, std {rms:5.2f}" plt.hist(gain_median[select], bins=50, label=label) plt.legend() # photon electrons plt.subplot(426) plt.tight_layout() plt.ylabel("pixels", fontsize=20) plt.xlabel("time corrections [ns]", fontsize=20) median = np.median(time_correction[select]) rms = np.std(time_correction[select]) label = f"Median {median:3.2f}, std {rms:3.2f}" plt.hist(time_correction[select].value, bins=50, label=label) plt.legend() plt.subplots_adjust(top=0.92) # photon electrons plt.subplot(427) plt.tight_layout() plt.ylabel("pixels", fontsize=20) plt.xlabel("pe", fontsize=20) median = np.median(n_pe[select]) rms = np.std(n_pe[select]) label = f"Median {median:3.2f}, std {rms:3.2f}" plt.hist(n_pe[select], bins=50, label=label) plt.legend() plt.subplots_adjust(top=0.92) # gain plt.subplot(428) plt.tight_layout() plt.ylabel("pixels", fontsize=20) plt.xlabel("flat-fielded gain [ADC/pe]", fontsize=20) denominator = dc_to_pe[select] numerator = 1. gain = np.divide(numerator, denominator, out=np.zeros_like(denominator), where=denominator != 0) median = np.median(gain) rms = np.std(gain) label = f"Median {median:3.2f}, std {rms:3.2f}" plt.hist(gain, bins=50, label=label) plt.legend() plt.subplots_adjust(top=0.92) pdf.savefig(plt.gcf()) plt.close()
from ctapipe.instrument import CameraGeometry from ctapipe.visualization import CameraDisplay if __name__ == '__main__': plt.style.use('ggplot') fig = plt.figure(figsize=(12, 8)) ax = fig.add_subplot(1, 1, 1) geom = CameraGeometry.from_name('NectarCam') disp = CameraDisplay(geom, ax=ax) disp.add_colorbar() model = toymodel.generate_2d_shower_model(centroid=(0.05, 0.0), width=0.05, length=0.15, psi='35d') image, sig, bg = toymodel.make_toymodel_shower_image(geom, model.pdf, intensity=1500, nsb_level_pe=5) disp.image = image mask = disp.image > 10 disp.highlight_pixels(mask, linewidth=2, color='crimson') plt.show()
psi='35d') image, sig, bg = toymodel.make_toymodel_shower_image(geom, model.pdf, intensity=50, nsb_level_pe=1000) # Apply image cleaning cleanmask = tailcuts_clean(geom, image, picture_thresh=200, boundary_thresh=100) clean = image.copy() clean[~cleanmask] = 0.0 # Calculate image parameters hillas = hillas_parameters(geom.pix_x, geom.pix_y, clean) print(hillas) # Show the camera image and overlay Hillas ellipse and clean pixels disp.image = image disp.cmap = 'PuOr' disp.highlight_pixels(cleanmask, color='black') disp.overlay_moments(hillas, color='cyan', linewidth=3) # Draw the neighbors of pixel 100 in red, and the neighbor-neighbors in # green for ii in geom.neighbors[130]: draw_neighbors(geom, ii, color='green') draw_neighbors(geom, 130, color='cyan', lw=2) plt.show()
def nsb_rate( files, aux_basepath, dark_histo_file, param_file, template_filename, output=None, plot="show", plot_nsb_range=None, norm="log", plot_baselines=False, disable_bar=False, max_events=None, n_skip=10, stars=True, bias_resistance=1e4 * u.Ohm, cell_capacitance=5e-14 * u.Farad ): files = np.atleast_1d(files) if len(files) == 1 and not files[0].endswith('.fz'): table = Table.read(files[0])[:max_events] data = dict(table) data['nsb_rate'] = np.array(data['nsb_rate']) * u.GHz else: dark_histo = Histogram1D.load(dark_histo_file) n_pixel = len(DigiCam.geometry.neighbors) pixels = np.arange(n_pixel, dtype=int) with open(param_file) as file: pulse_template = NormalizedPulseTemplate.load(template_filename) pulse_area = pulse_template.integral() * u.ns charge_to_amplitude = pulse_template.compute_charge_amplitude_ratio(7, 4) calibration_parameters = yaml.load(file) gain_integral = np.array(calibration_parameters['gain']) gain_amplitude = gain_integral * charge_to_amplitude crosstalk = np.array(calibration_parameters['mu_xt']) events = calibration_event_stream(files, max_events=max_events, disable_bar=disable_bar) events = add_slow_data_calibration( events, basepath=aux_basepath, aux_services=('DriveSystem', ) ) data = { "baseline": [], "nsb_rate": [], "good_pixels_mask": [], "timestamp": [], "event_id": [], "az": [], "el": [], } bad_pixels = get_bad_pixels( calib_file=param_file, nsigma_gain=5, nsigma_elecnoise=5, dark_histo=dark_histo_file, nsigma_dark=8, plot=None, output=None ) events_skipped = 0 for event in events: if event.event_type.INTERNAL not in event.event_type: continue events_skipped += 1 if events_skipped < n_skip: continue events_skipped = 0 data['baseline'].append(event.data.digicam_baseline) baseline_shift = event.data.digicam_baseline - dark_histo.mean() rate = _compute_nsb_rate( baseline_shift=baseline_shift, gain=gain_amplitude, pulse_area=pulse_area, crosstalk=crosstalk, bias_resistance=bias_resistance, cell_capacitance=cell_capacitance ) bad_pixels_event = np.unique(np.hstack( ( bad_pixels, pixels[rate < 0], pixels[rate > 5 * u.GHz] ) )) avg_matrix = _get_average_matrix_bad_pixels( DigiCam.geometry, bad_pixels_event ) good_pixels_mask = np.ones(n_pixel, dtype=bool) good_pixels_mask[bad_pixels_event] = False good_pixels = pixels[good_pixels_mask] rate[bad_pixels_event] = avg_matrix[bad_pixels_event, :].dot( rate[good_pixels] ) data['good_pixels_mask'].append(good_pixels_mask) data['timestamp'].append(event.data.local_time) data['event_id'].append(event.event_id) data['nsb_rate'].append(rate) data['az'].append(event.slow_data.DriveSystem.current_position_az) data['el'].append(event.slow_data.DriveSystem.current_position_el) data['nsb_rate'] = np.array(data['nsb_rate']) * u.GHz if output is not None: table = Table(data) if os.path.isfile(output): os.remove(output) table.write(output, format='fits') time_obs = Time( np.array(data['timestamp'], dtype=np.float64) * 1e-9, format='unix' ) if plot_baselines: fig2, (ax1, ax2) = plt.subplots(2, 1, figsize=(16, 8), dpi=50) baseline_std = np.std(data['baseline'], axis=0) ax1.hist(baseline_std, 100) ax1.set_xlabel('std(baseline) [LSB]') # pixels_shown = np.arange(len(baseline_std))[baseline_std > 10] pixels_shown = [834,] ax2.plot_date( time_obs.to_datetime(), data['baseline'][:, pixels_shown], '-' ) ax2.set_xlabel('time') ax2.set_ylabel('baseline [LSB]') plt.tight_layout() plt.show() plt.close(fig2) az_obs = np.array(data['az']) * u.deg el_obs = np.array(data['el']) * u.deg n_event = len(data['timestamp']) fig1, ax = plt.subplots(1, 1, figsize=(16, 12), dpi=50) date = datetime.fromtimestamp(data['timestamp'][0]*1e-9) date_str = date.strftime("%H:%M:%S") display = CameraDisplay( DigiCam.geometry, ax=ax, norm=norm, title='NSB rate [GHz], t=' + date_str ) rate_ghz = np.array(data['nsb_rate'][0].to(u.GHz).value) display.image = rate_ghz if plot_nsb_range is None: min_range_rate = np.max([np.min(rate_ghz), 50e-3]) plot_nsb_range = (min_range_rate, np.max(rate_ghz)) display.set_limits_minmax(*plot_nsb_range) display.add_colorbar(ax=ax) bad_pixels = np.arange( len(data['good_pixels_mask'][0]) )[~data['good_pixels_mask'][0]] display.highlight_pixels(bad_pixels, color='r', linewidth=2) display.axes.set_xlim([-500., 500.]) display.axes.set_ylim([-500., 500.]) plt.tight_layout() if stars is True: stars_az, stars_alt, stars_pmag = get_stars_in_fov( az_obs[0], el_obs[0], time_obs ) stars_x, stars_y = transform_azel_to_xy( stars_az, stars_alt, az_obs, el_obs ) point_stars = [] for index_star in range(len(stars_pmag)): point_star, = ax.plot( stars_x[index_star, 0], stars_y[index_star, 0], 'ok', ms=20-2*stars_pmag[index_star], mew=3, mfc='None' ) point_stars.append(point_star) def update(i, display): print('frame', i, '/', len(data['timestamp'])) display.image = data['nsb_rate'][i].to(u.GHz).value date = datetime.fromtimestamp(data['timestamp'][i] * 1e-9) date_str = date.strftime("%H:%M:%S") display.axes.set_title('NSB rate [GHz], t=' + date_str) bad_pixels = np.arange( len(data['good_pixels_mask'][i]) )[~data['good_pixels_mask'][i]] display.highlight_pixels( bad_pixels, color='r', linewidth=2 ) if stars is True: for index_star in range(len(stars_pmag)): point_stars[index_star].set_xdata( stars_x[index_star, i] ) point_stars[index_star].set_ydata( stars_y[index_star, i] ) anim = FuncAnimation( fig1, update, frames=len(data['timestamp']), interval=20, fargs=(display, ) ) Writer = animation.writers['ffmpeg'] writer = Writer(fps=50, metadata=dict(artist='Y. Renier'), bitrate=4000, codec='h263p') output_path = os.path.dirname(plot) if plot == "show" or \ (output_path != "" and not os.path.isdir(output_path)): if not plot == "show": print('WARNING: Path ' + output_path + ' for output trigger ' + 'uniformity does not exist, displaying the plot instead.\n') display.enable_pixel_picker() plt.show() else: anim.save(plot, writer=writer) print(plot, 'created') plt.close(fig1)
def plot_pedestals(data_file, pedestal_file, run=0, plot_file="none", tel_id=1, offset_value=400): """ plot pedestal quantities quantities Parameters ---------- data_file: pedestal run pedestal_file: file with drs4 corrections run: run number of data to be corrected plot_file: name of output pdf file tel_id: id of the telescope offset_value: baseline off_set """ # plot open pdf if plot_file != "none": pp = PdfPages(plot_file) plt.rc('font', size=15) # r0 calibrator r0_calib = LSTR0Corrections(pedestal_path=pedestal_file, offset=offset_value, tel_id=tel_id) # event_reader reader = event_source(data_file, max_events=1000) t = np.linspace(2, 37, 36) # configuration for the charge integrator charge_config = Config( {"FixedWindowSum": { "window_start": 12, "window_width": 12, }}) # declare the pedestal component pedestal = PedestalIntegrator(tel_id=tel_id, sample_size=1000, sample_duration=1000000, charge_median_cut_outliers=[-10, 10], charge_std_cut_outliers=[-10, 10], charge_product="FixedWindowSum", config=charge_config, subarray=reader.subarray) for i, event in enumerate(reader): if tel_id != event.r0.tels_with_data[0]: raise Exception( f"Given wrong telescope id {tel_id}, files has id {event.r0.tels_with_data[0]}" ) # move from R0 to R1 r0_calib.calibrate(event) ok = pedestal.calculate_pedestals(event) if ok: ped_data = event.mon.tel[tel_id].pedestal break camera_geometry = reader.subarray.tels[tel_id].camera.geometry camera_geometry = camera_geometry.transform_to(EngineeringCameraFrame()) # plot open pdf if plot_file != "none": pp = PdfPages(plot_file) plt.rc('font', size=15) ### first figure fig = plt.figure(1, figsize=(12, 24)) plt.tight_layout() n_samples = charge_config["FixedWindowSum"]['window_width'] fig.suptitle(f"Run {run}, integration on {n_samples} samples", fontsize=25) pad = 420 image = ped_data.charge_median mask = ped_data.charge_median_outliers for chan in (np.arange(2)): pad += 1 plt.subplot(pad) plt.tight_layout() disp = CameraDisplay(camera_geometry) mymin = np.median(image[chan]) - 2 * np.std(image[chan]) mymax = np.median(image[chan]) + 2 * np.std(image[chan]) disp.set_limits_minmax(mymin, mymax) disp.highlight_pixels(mask[chan], linewidth=2) disp.image = image[chan] disp.cmap = plt.cm.coolwarm # disp.axes.text(lposx, 0, f'{channel[chan]} pedestal [ADC]', rotation=90) plt.title(f'{channel[chan]} pedestal [ADC]') disp.add_colorbar() image = ped_data.charge_std mask = ped_data.charge_std_outliers for chan in (np.arange(2)): pad += 1 plt.subplot(pad) plt.tight_layout() disp = CameraDisplay(camera_geometry) mymin = np.median(image[chan]) - 2 * np.std(image[chan]) mymax = np.median(image[chan]) + 2 * np.std(image[chan]) disp.set_limits_minmax(mymin, mymax) disp.highlight_pixels(mask[chan], linewidth=2) disp.image = image[chan] disp.cmap = plt.cm.coolwarm # disp.axes.text(lposx, 0, f'{channel[chan]} pedestal std [ADC]', rotation=90) plt.title(f'{channel[chan]} pedestal std [ADC]') disp.add_colorbar() ### histograms for chan in np.arange(2): mean_ped = ped_data.charge_mean[chan] ped_std = ped_data.charge_std[chan] # select good pixels select = np.logical_not(mask[chan]) #fig.suptitle(f"Run {run} channel: {channel[chan]}", fontsize=25) pad += 1 # pedestal charge plt.subplot(pad) plt.tight_layout() plt.ylabel('pixels') plt.xlabel(f'{channel[chan]} pedestal') median = np.median(mean_ped[select]) rms = np.std(mean_ped[select]) label = f"{channel[chan]} Median {median:3.2f}, std {rms:3.2f}" plt.hist(mean_ped[select], bins=50, label=label) plt.legend() pad += 1 # pedestal std plt.subplot(pad) plt.ylabel('pixels') plt.xlabel(f'{channel[chan]} pedestal std') median = np.median(ped_std[select]) rms = np.std(ped_std[select]) label = f" Median {median:3.2f}, std {rms:3.2f}" plt.hist(ped_std[select], bins=50, label=label) plt.legend() plt.subplots_adjust(top=0.94) if plot_file != "none": pp.savefig() pix = 0 pad = 420 # plot corrected waveforms of first 8 events for i, ev in enumerate(reader): for chan in np.arange(2): if pad == 420: # new figure fig = plt.figure(ev.index.event_id, figsize=(12, 24)) fig.suptitle(f"Run {run}, pixel {pix}", fontsize=25) plt.tight_layout() pad += 1 plt.subplot(pad) plt.subplots_adjust(top=0.92) label = f"event {ev.index.event_id}, {channel[chan]}: R0" plt.step(t, ev.r0.tel[tel_id].waveform[chan, pix, 2:38], color="blue", label=label) r0_calib.subtract_pedestal(ev, tel_id) label = "+ pedestal substraction" plt.step(t, ev.r1.tel[tel_id].waveform[chan, pix, 2:38], color="red", alpha=0.5, label=label) r0_calib.time_lapse_corr(ev, tel_id) r0_calib.interpolate_spikes(ev, tel_id) label = "+ dt corr + interp. spikes" plt.step(t, ev.r1.tel[tel_id].waveform[chan, pix, 2:38], alpha=0.5, color="green", label=label) plt.plot([0, 40], [offset_value, offset_value], 'k--', label="offset") plt.xlabel("time sample [ns]") plt.ylabel("counts [ADC]") plt.legend() plt.ylim([-50, 500]) if plot_file != "none" and pad == 428: pad = 420 plt.subplots_adjust(top=0.92) pp.savefig() if i == 8: break if plot_file != "none": pp.close()
def finish(self): """ write fit results in h5 file and the check-plots in pdf file """ gain = np.ma.array(self.fit_parameters.T[0], mask=self.fit_error.T) quadratic_term = np.ma.array(self.fit_parameters.T[1], mask=self.fit_error.T) # give to the badly fitted pixel a median value for the B term median_quadratic_term = np.ma.median(quadratic_term, axis=0) fill_array = np.ones((constants.N_PIXELS, constants.N_GAINS)) * median_quadratic_term quadratic_term_corrected = np.ma.filled(quadratic_term, fill_array) with h5py.File(self.output_path, 'w') as hf: hf.create_dataset('gain', data=gain.T) hf.create_dataset('B_term', data=quadratic_term_corrected.T) hf.create_dataset('covariance_matrix', data=self.fit_cov_matrix) hf.create_dataset('bad_fit_mask', data=self.fit_error) # remember the camera median and the variance per run channel = ["HG", "LG"] for chan in [0, 1]: if self.signal[chan] is not None: hf.create_dataset(f'median_signal_{channel[chan]}', data=np.median(self.signal[chan], axis=0)) hf.create_dataset(f'median_variance_{channel[chan]}', data=np.median(self.variance[chan], axis=0)) hf.create_dataset(f'runs_{channel[chan]}', data=self.selected_runs[chan]) hf.create_dataset('runs', data=self.run_list) hf.create_dataset('sub_run', data=self.sub_run) # plot open pdf with PdfPages(self.plot_path) as pdf: plt.rc("font", size=15) for chan in self.gain_channels: # plot the used runs and their median camera charge fig = plt.figure((chan + 1), figsize=(8, 20)) fig.suptitle(f"{channel[chan]} channel", fontsize=25) ax = plt.subplot(2, 1, 1) ax.grid(True) ax.yaxis.set_major_locator(MaxNLocator(integer=True)) ax.xaxis.set_major_locator(MaxNLocator(integer=True)) ax.yaxis.set_major_locator(plt.MultipleLocator(1)) plt.plot(np.median(self.signal[chan], axis=0), self.selected_runs[chan], "o") plt.xlabel(r'$\mathrm{\overline{Q}-\overline{ped}}$ [ADC]') plt.ylabel(r'Runs used in the fit') plt.subplot(2, 1, 2) camera = load_camera_geometry() camera = camera.transform_to(EngineeringCameraFrame()) disp = CameraDisplay(camera) image = self.fit_parameters.T[1].T * 100 mymin = np.median(image[chan]) - 3 * np.std(image[chan]) mymax = np.median(image[chan]) + 3 * np.std(image[chan]) disp.set_limits_minmax(mymin, mymax) mask = np.where(self.fit_error[chan] == 1)[0] disp.highlight_pixels(mask, linewidth=2.5, color="green") disp.image = image[chan] disp.cmap = plt.cm.coolwarm plt.title(f"{channel[chan]} Fitted B values [%]") disp.add_colorbar() plt.tight_layout() pdf.savefig() # plot the fit results and residuals for four arbitrary pixels fig = plt.figure((chan + 1) * 10, figsize=(11, 22)) fig.suptitle(f"{channel[chan]} channel", fontsize=25) pad = 0 for pix in [0, 600, 1200, 1800]: pad += 1 plt.subplot(4, 2, pad) plt.grid(which='minor') mask = self.unusable_pixels[chan][pix] sig = np.ma.array(self.signal[chan][pix], mask=mask).compressed() var = np.ma.array(self.variance[chan][pix], mask=mask).compressed() popt = self.fit_parameters[chan, pix] # plot points plt.plot(sig, var, 'o', color="C0") # plot fit min_x = min(1000, np.min(sig) * 0.9) max_x = max(10000, np.max(sig) * 1.1) x = np.arange(np.min(sig), np.max(sig)) plt.plot(x, quadratic_fit(x, *popt), '--', color="C1", label=f'Pixel {pix}:\ng={popt[0]:5.2f} [ADC/pe] , B={popt[1]:5.3f}') plt.xlim(min_x, max_x) plt.xlabel('Q-ped [ADC]') plt.ylabel(r'$\mathrm{\sigma_Q^2-\sigma_{ped}^2}$ [$ADC^2$]') plt.xscale('log') plt.yscale('log') plt.legend() # plot residuals pad += 1 plt.subplot(4, 2, pad) plt.grid(which='both', axis='both') popt = self.fit_parameters[chan, pix] plt.plot(sig, (quadratic_fit(sig, *popt) - var) / var * 100, 'o', color="C0") plt.xlim(min_x, max_x) plt.xscale('log') plt.ylabel('fit residuals %') plt.xlabel('Q-ped [ADC]') plt.hlines(0, 0, np.max(sig), linestyle='dashed', color="black") plt.tight_layout() pdf.savefig()
if __name__ == "__main__": # Load the camera geom = CameraGeometry.from_name("LSTCam") disp = CameraDisplay(geom) disp.add_colorbar() # Create a fake camera image to display: model = toymodel.Gaussian( x=0.2 * u.m, y=0.0 * u.m, width=0.05 * u.m, length=0.15 * u.m, psi="35d" ) image, sig, bg = model.generate_image(geom, intensity=1500, nsb_level_pe=2) # Apply image cleaning cleanmask = tailcuts_clean(geom, image, picture_thresh=10, boundary_thresh=5) clean = image.copy() clean[~cleanmask] = 0.0 # Calculate image parameters hillas = hillas_parameters(geom, clean) print(hillas) # Show the camera image and overlay Hillas ellipse and clean pixels disp.image = image disp.cmap = "inferno" disp.highlight_pixels(cleanmask, color="crimson") disp.overlay_moments(hillas, color="cyan", linewidth=1) plt.show()
import astropy.units as u import numpy as np np.random.seed(1337) geom = CameraGeometry.from_name('FACT') geom = geom.transform_to(EngineeringCameraFrame()) model = SkewedGaussian( width=0.01 * u.m, length=0.03 * u.m, x=0.14 * u.m, y=0.07 * u.m, skewness=0.05, psi=30 * u.deg, ) img, signal, noise = model.generate_image(geom, 1000, nsb_level_pe=2) fig = plt.figure(figsize=(5, 5)) ax = fig.add_axes([0, 0, 1, 1]) ax.set_xticks([]) ax.set_yticks([]) ax.set_xlabel('') ax.set_ylabel('') ax.set_axis_off() disp = CameraDisplay(geom, image=img, ax=ax, cmap='inferno') disp.highlight_pixels(geom.get_border_pixel_mask(2), color='C0') ax.set_title('') fig.savefig('build/leakage.pdf', bbox_inches='tight')
from ctapipe.instrument import CameraGeometry from ctapipe.visualization import CameraDisplay if __name__ == '__main__': plt.style.use('ggplot') fig = plt.figure(figsize=(12, 8)) ax = fig.add_subplot(1, 1, 1) geom = CameraGeometry.from_name('NectarCam') disp = CameraDisplay(geom, ax=ax) disp.add_colorbar() model = toymodel.generate_2d_shower_model(centroid=(0.05, 0.0), width=0.005, length=0.025, psi='35d') image, sig, bg = toymodel.make_toymodel_shower_image(geom, model.pdf, intensity=50, nsb_level_pe=20) disp.image = image mask = disp.image > 15 disp.highlight_pixels(mask, linewidth=3) plt.show()
def main(dl1_hipecta_filename, dl1_lstchain_filename): geom = CameraGeometry.from_name('LSTCam') dl1_hipecta = tables.open_file(dl1_hipecta_filename) dl1_lstchain = tables.open_file(dl1_lstchain_filename) with tables.open_file(dl1_hipecta_filename) as dl1_hipecta: hipecta_images = Table(dl1_hipecta.root.dl1.Tel_1.calib_pic.read()) hipecta_parameters = Table(dl1_hipecta.root.dl1.Tel_1.parameters.read()) with tables.open_file(dl1_lstchain_filename) as dl1_lstchain: simu_table = Table(dl1_lstchain.root.dl1.event.simulation.LST_LSTCam.read()) lstchain_images = Table(dl1_lstchain.root[dl1_images_lstcam_key].read()) hipecta = join(hipecta_images, hipecta_parameters, keys='event_id') lstchain_table = hstack([lstchain_images, simu_table], join_type='exact') lstchain_table.rename_column('tel_id_1', 'tel_id') lstchain_table.remove_column('tel_id_2') mega_table = join(lstchain_table[lstchain_table['tel_id']==1], hipecta, uniq_col_name='{table_name}_{col_name}', table_names = ['lstchain', 'hipecta'], keys='event_id' ) selected_table = mega_table[:30] params_cleaning = dict(picture_thresh=6, boundary_thresh=3, keep_isolated_pixels=False, min_number_picture_neighbors=2) lstchain_cleaning = np.apply_along_axis(tailcuts_clean_teltype, selected_table['image'], **params_cleaning) selected_table.add_column(Column(lstchain_cleaning, dtype=int), name='lstchain_clean_mask') with PdfPages(f'compare_lstchain_hipecta_images_{date.today()}.pdf') as pp: for ii, row in enumerate(selected_table[:10]): print(f"{ii}. event id : {row['event_id']}") # print(row) h = get_hillas_container(row) image_lstchain = row['image'] image_hipecta = row['signal'] clean_mask_ctapipe_on_lstchain = row['lstchain_clean_mask'] clean_mask_ctapipe_on_hipecta = tailcuts_clean(geom, image_hipecta, **params_cleaning) clean_mask_hipecta = row['clean_mask'].astype(bool) fig, axes = plt.subplots(2,3, figsize=(12,6)) # axes[0,2].remove() display = CameraDisplay(geom, image_lstchain, ax=axes[0,0]) display.add_colorbar(ax=axes[0,0]) axes[0,0].set_title('lstchain image') display = CameraDisplay(geom, clean_mask_ctapipe_on_lstchain, ax=axes[0,1]) # display.add_colorbar(ax=axes[0,1]) display.highlight_pixels(clean_mask_ctapipe_on_lstchain.astype(bool), color='red') axes[0,1].set_title('lstchain clean mask') display = CameraDisplay(geom, image_hipecta, ax=axes[1,0]) display.add_colorbar(ax=axes[1,0]) axes[1,0].set_title('hipecta image') display = CameraDisplay(geom, clean_mask_hipecta, ax=axes[1,1]) # display.add_colorbar(ax=axes[1,1]) display.highlight_pixels(clean_mask_ctapipe_on_hipecta, color='red') axes[1,1].set_title('hipecta clean mask') axes[1,1].text(0.88,0.88,s='cleaning mask\nfrom ctapipe',color='red') axes[1,1].text(-1.5, 0.88, s=f'n_islands={row["n_islands"]}', color='black') display.overlay_moments(h) display = CameraDisplay(geom, row['photo_electron_image'], ax=axes[0,2]) display.add_colorbar(ax=axes[0,2]) axes[0,2].set_title('true pe image') display.highlight_pixels(clean_mask_ctapipe_on_lstchain.astype(bool), color='red') axes[0,2].text(0.88, 0.88, s='cleaning mask\nfrom ctapipe', color='red') display = CameraDisplay(geom, row['photo_electron_image'], ax=axes[1,2]) display.add_colorbar(ax=axes[1,2]) axes[1,2].set_title('true pe image') display.highlight_pixels(clean_mask_hipecta, color='red') axes[1,2].text(0.88,0.88,s='cleaning mask\nfrom hipecta',color='red') plt.tight_layout() pp.savefig(dpi=100)