def update(frame): centroid = np.random.uniform(-0.5, 0.5, size=2) width = np.random.uniform(0, 0.01) length = np.random.uniform(0, 0.03) + width angle = np.random.uniform(0, 360) intens = np.random.exponential(2) * 50 model = mock.generate_2d_shower_model(centroid=centroid, width=width, length=length, psi=angle * u.deg) image, sig, bg = mock.make_mock_shower_image(geom, model.pdf, intensity=intens, nsb_level_pe=5000) # alternate between cleaned and raw images if self._counter > 20: plt.suptitle("Image Cleaning ON") cleanmask = reco.cleaning.tailcuts_clean(geom, image, pedvars=80) for ii in range(3): reco.cleaning.dilate(geom, cleanmask) image[cleanmask == 0] = 0 # zero noise pixels if self._counter >= 40: plt.suptitle("Image Cleaning OFF") self._counter = 0 disp.image = image disp.set_limits_percent(100) self._counter += 1
def update(frame): global _counter centroid = np.random.uniform(-0.5, 0.5, size=2) width = np.random.uniform(0, 0.01) length = np.random.uniform(0, 0.03) + width angle = np.random.uniform(0, 360) intens = np.random.exponential(2) * 50 model = mock.generate_2d_shower_model(centroid=centroid, width=width, length=length, psi=angle * u.deg) image, sig, bg = mock.make_mock_shower_image(geom, model.pdf, intensity=intens, nsb_level_pe=5000) # alternate between cleaned and raw images if _counter > 20: plt.suptitle("Image Cleaning ON") cleanmask = reco.cleaning.tailcuts_clean(geom, image, pedvars=80) for ii in range(3): reco.cleaning.dilate(geom, cleanmask) image[cleanmask == 0] = 0 # zero noise pixels if _counter >= 40: plt.suptitle("Image Cleaning OFF") _counter = 0 disp.image = image disp.set_limits_percent(100) _counter += 1
def draw_several_cams(geom, ncams=4): cmaps = ['jet', 'afmhot', 'terrain', 'autumn'] fig, axs = plt.subplots(1, ncams, figsize=(15, 4), sharey=True, sharex=True) for ii in range(ncams): disp = visualization.CameraDisplay( geom, ax=axs[ii], title="CT{}".format(ii + 1), ) disp.cmap = cmaps[ii] model = mock.generate_2d_shower_model( centroid=(0.2 - ii * 0.1, -ii * 0.05), width=0.005 + 0.001 * ii, length=0.1 + 0.05 * ii, psi=ii * 20 * u.deg, ) image, sig, bg = mock.make_mock_shower_image( geom, model.pdf, intensity=50, nsb_level_pe=1000, ) clean = image.copy() clean[image <= 3.0 * image.mean()] = 0.0 hillas = hillas_parameters(geom.pix_x, geom.pix_y, clean) disp.image = image disp.add_colorbar(ax=axs[ii]) disp.set_limits_percent(95) disp.overlay_moments(hillas, linewidth=3, color='blue')
def draw_several_cams(geom): ncams = 4 cmaps = [plt.cm.jet, plt.cm.afmhot, plt.cm.terrain, plt.cm.autumn] fig, ax = plt.subplots(1, ncams, figsize=(15, 4), sharey=True, sharex=True) for ii in range(ncams): disp = visualization.CameraDisplay(geom, axes=ax[ii], title="CT{}".format(ii + 1)) model = mock.generate_2d_shower_model(centroid=(0.2 - ii * 0.1, -ii * 0.05), width=0.005 + 0.001 * ii, length=0.1 + 0.05 * ii, psi=np.radians(ii * 20)) image, sig, bg = mock.make_mock_shower_image(geom, model.pdf, intensity=50, nsb_level_pe=1000) clean = image.copy() clean[image <= 3.0 * image.mean()] = 0.0 hillas = hillas_parameters(geom.pix_x.value, geom.pix_y.value, clean) disp.set_cmap(cmaps[ii]) disp.set_image(image) disp.overlay_moments(hillas, linewidth=3, color='blue')
def draw_several_cams(geom): ncams = 4 cmaps = [plt.cm.jet, plt.cm.afmhot, plt.cm.terrain, plt.cm.autumn] fig, ax = plt.subplots(1, ncams, figsize=(15, 4), sharey=True, sharex=True) for ii in range(ncams): disp = visualization.CameraDisplay(geom, ax=ax[ii], title="CT{}".format(ii + 1)) disp.cmap = cmaps[ii] model = mock.generate_2d_shower_model(centroid=(0.2 - ii * 0.1, -ii * 0.05), width=0.005 + 0.001 * ii, length=0.1 + 0.05 * ii, psi=ii * 20 * u.deg) image, sig, bg = mock.make_mock_shower_image(geom, model.pdf, intensity=50, nsb_level_pe=1000) clean = image.copy() clean[image <= 3.0 * image.mean()] = 0.0 hillas = hillas_parameters(geom.pix_x.value, geom.pix_y.value, clean) disp.image = image disp.set_limits_percent(95) disp.overlay_moments(hillas, linewidth=3, color='blue')
def update(frame): centroid = np.random.uniform(-0.5, 0.5, size=2) width = np.random.uniform(0, 0.01) length = np.random.uniform(0, 0.03) + width angle = np.random.uniform(0, 360) intens = np.random.exponential(2) * 50 model = mock.generate_2d_shower_model(centroid=centroid, width=width, length=length, psi=np.radians(angle)) image, sig, bg = mock.make_mock_shower_image(geom, model.pdf, intensity=intens, nsb_level_pe=5000) image /= image.max() disp.set_image(image)
def update(frame): centroid = np.random.uniform(-0.5, 0.5, size=2) width = np.random.uniform(0, 0.01) length = np.random.uniform(0, 0.03) + width angle = np.random.uniform(0, 360) intens = np.random.exponential(2) * 50 model = mock.generate_2d_shower_model(centroid=centroid, width=width, length=length, psi=angle * u.deg) image, sig, bg = mock.make_mock_shower_image(geom, model.pdf, intensity=intens, nsb_level_pe=5000) image /= image.max() disp.image = image
from ctapipe.reco import mock 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 = mock.generate_2d_shower_model(centroid=(0.05, 0.0), width=0.005, length=0.025, psi='35d') image, sig, bg = mock.make_mock_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 mock_event_source( geoms, events=100, single_tel=False, n_channels=1, n_samples=25, p_trigger=0.3, ): """ An event source that produces array Parameters ---------- geoms : list of CameraGeometry instances Geometries for the telescopes to simulate events : int, default: 100 maximum number of events to create n_channels : int how many channels per telescope n_samples : int how many adc samples per pixel p_trigger : float mean trigger probability for the telescopes """ n_telescopes = len(geoms) container = Container("mock_container") container.meta.add_item('mock__max_events', events) container.meta.add_item('pixel_pos', dict()) container.add_item("dl0", RawData()) container.add_item("count") tel_ids = np.arange(n_telescopes) for event_id in range(events): n_triggered = np.random.poisson(n_telescopes * 0.3) if n_triggered > n_telescopes: n_triggered = n_telescopes triggered_tels = np.random.choice(tel_ids, n_triggered, replace=False) container.dl0.event_id = event_id container.dl0.tels_with_data = triggered_tels container.count = event_id # handle single-telescope case (ignore others: if single_tel: if single_tel not in container.dl0.tels_with_data: continue container.dl0.tels_with_data = [single_tel, ] container.dl0.tel = dict() # clear the previous telescopes t = np.arange(n_samples) for tel_id in container.dl0.tels_with_data: geom = geoms[tel_id] # fill pixel position dictionary, if not already done: if tel_id not in container.meta.pixel_pos: container.meta.pixel_pos[tel_id] = ( geom.pix_x.value, geom.pix_y.value, ) centroid = np.random.uniform(geom.pix_x.min(), geom.pix_y.max(), 2) length = np.random.uniform(0.02, 0.2) width = np.random.uniform(0.01, length) psi = np.random.randint(0, 360) intensity = np.random.poisson(int(10000 * width * length)) model = mock.generate_2d_shower_model( centroid, width, length, '{}d'.format(psi) ) image, _, _ = mock.make_mock_shower_image( geom, model.pdf, intensity, ) container.dl0.tel[tel_id] = RawCameraData(tel_id) container.dl0.tel[tel_id].num_channels = n_channels n_pix = len(geom.pix_id) samples = np.empty((n_pix, n_samples)) means = np.random.normal(15, 1, (n_pix, 1)) stds = np.random.uniform(3, 6, (n_pix, 1)) samples = image[:, np.newaxis] * norm.pdf(t, means, stds) for chan in range(n_channels): container.dl0.tel[tel_id].adc_samples[chan] = samples container.dl0.tel[tel_id].adc_sums[chan] = image yield container
for nn in neigh: nx, ny = geom.pix_x[nn].value, geom.pix_y[nn].value plt.plot([x, nx], [y, ny], color=color, **kwargs) if __name__ == '__main__': # Load the camera geom = io.CameraGeometry.from_name("hess", 1) disp = visualization.CameraDisplay(geom) disp.set_limits_minmax(0, 300) disp.add_colorbar() # Create a fake camera image to display: model = mock.generate_2d_shower_model(centroid=(0.2, 0.0), width=0.01, length=0.1, psi='35d') image, sig, bg = mock.make_mock_shower_image(geom, model.pdf, intensity=50, nsb_level_pe=1000) # Apply really stupid image cleaning (single threshold): clean = image.copy() clean[image <= 3.0 * image.mean()] = 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
import numpy as np # LOAD THE CAMERA ############################################################# geom = io.make_rectangular_camera_geometry() # GENERATE A (RANDOM) 2D SHOWER MODEL ######################################### centroid = np.random.uniform(-0.5, 0.5, size=2) width = np.random.uniform(0, 0.01) length = np.random.uniform(0, 0.03) + width angle = np.random.uniform(0, 360) intens = np.random.exponential(2) * 50 model = mock.generate_2d_shower_model(centroid=centroid, width=width, length=length, psi=angle * u.deg) # MAKE THE IMAGE ############################################################## # image = c * (sig + bg) # - "image" is the noisy image (a 1D Numpy array with one value per pixel). # - "sig" is the clean image (a 1D Numpy array with one value per pixel). # - "bg" is the background noise (a 1D Numpy array with one value per pixel). image, sig, bg = mock.make_mock_shower_image(geom, model.pdf, intensity=intens, nsb_level_pe=5000) # NORMALIZE PIXELS VALUE ######################################################
from ctapipe.visualization import CameraDisplay from ctapipe.reco import mock from matplotlib.colors import PowerNorm if __name__ == '__main__': use('ggplot') # load the camera fig, axs = plt.subplots(1, 3, figsize=(15, 5)) geom = CameraGeometry.from_name("hess", 1) titles = 'Linear Scale', 'Log-Scale', 'PowerNorm(gamma=2)' model = mock.generate_2d_shower_model( centroid=(0.2, 0.0), width=0.01, length=0.1, psi='35d', ) image, sig, bg = mock.make_mock_shower_image( geom, model.pdf, intensity=50, nsb_level_pe=1000, ) disps = [] for ax, title in zip(axs, titles): disps.append(CameraDisplay(geom, ax=ax, image=image, title=title)) disps[0].norm = 'lin'
""" draw lines between a pixel and its neighbors""" neigh = geom.neighbors[pixel_index] # neighbor indices (not pixel ids) x, y = geom.pix_x[pixel_index].value, geom.pix_y[pixel_index].value for nn in neigh: nx, ny = geom.pix_x[nn].value, geom.pix_y[nn].value plt.plot([x, nx], [y, ny], color=color, **kwargs) if __name__ == '__main__': # load the camera geom = io.get_camera_geometry("hess", 1) disp = visualization.CameraDisplay(geom) # create a fake camera image to display: model = mock.generate_2d_shower_model(centroid=(0.2, 0.0), width=0.01, length=0.1, psi=np.radians(35)) image, sig, bg = mock.make_mock_shower_image(geom, model.pdf, intensity=50, nsb_level_pe=1000) # apply really stupid image cleaning (single threshold): clean = image.copy() clean[image <= 3.0 * image.mean()] = 0.0 # calculate image parameters hillas = hillas_parameters(geom.pix_x.value, geom.pix_y.value, clean) print(hillas) # show the camera image and overlay Hillas ellipse disp.set_image(image)