def test_tailcuts_clean_min_neighbors_2(): """ requiring that picture pixels have at least two neighbors above picture_thresh""" # start with simple 3-pixel camera geom = CameraGeometry.make_rectangular(3, 1, (-1, 1)) p = 15 # picture value b = 7 # boundary value testcases = { (p, p, 0): [False, False, False], (p, 0, p): [False, False, False], (p, b, p): [False, False, False], (p, b, 0): [False, False, False], (b, b, 0): [False, False, False], (p, p, p): [True, True, True], } for image, mask in testcases.items(): result = cleaning.tailcuts_clean( geom, np.array(image), picture_thresh=15, boundary_thresh=5, min_number_picture_neighbors=2, keep_isolated_pixels=False, ) assert (result == mask).all()
def test_tailcuts_clean(): # start with simple 3-pixel camera geom = CameraGeometry.make_rectangular(3, 1, (-1, 1)) p = 15 # picture value b = 7 # boundary value # for no isolated pixels: testcases = { (p, p, 0): [True, True, False], (p, 0, p): [False, False, False], (p, b, p): [True, True, True], (p, b, 0): [True, True, False], (b, b, 0): [False, False, False], (0, p, 0): [False, False, False], } for image, mask in testcases.items(): result = cleaning.tailcuts_clean( geom, np.array(image), picture_thresh=15, boundary_thresh=5, keep_isolated_pixels=False, ) assert (result == mask).all()
def test_tailcuts_clean_min_neighbors_1(): """ requiring that picture pixels have at least one neighbor above picture_thres: """ # start with simple 3-pixel camera geom = CameraGeometry.make_rectangular(3, 1, (-1, 1)) p = 15 # picture value b = 7 # boundary value testcases = {(p, p, 0): [True, True, False], (p, 0, p): [False, False, False], (p, b, p): [False, False, False], (p, b, 0): [False, False, False], (b, b, 0): [False, False, False], (0, p, 0): [False, False, False], (p, p, p): [True, True, True]} for image, mask in testcases.items(): result = cleaning.tailcuts_clean(geom, np.array(image), picture_thresh=15, boundary_thresh=5, min_number_picture_neighbors=1, keep_isolated_pixels=False) assert (result == mask).all()
def test_neighbor_pixels(): hexgeom = CameraGeometry.from_name("LSTCam") recgeom = CameraGeometry.make_rectangular() # most pixels should have 4 neighbors for rectangular geometry and 6 for # hexagonal assert int(median(recgeom.neighbor_matrix.sum(axis=1))) == 4 assert int(median(hexgeom.neighbor_matrix.sum(axis=1))) == 6
def test_neighbor_pixels(): hexgeom = CameraGeometry.from_name("LSTCam") recgeom = CameraGeometry.make_rectangular() # most pixels should have 4 neighbors for rectangular geometry and 6 for # hexagonal assert int(median(recgeom.neighbor_matrix.sum(axis=1))) == 4 assert int(median(hexgeom.neighbor_matrix.sum(axis=1))) == 6
def test_mars_cleaning_1st_pass(): """Test the MARS-like cleaning 1st pass.""" # start with simple 3-pixel camera geom = CameraGeometry.make_rectangular(3, 1, (-1, 1)) p = 10 # picture value b1 = 7 # 1st boundary value b2 = 7 # 2nd boundary value # The following test-cases are a superset of those used to test # 'tailcuts_clean': since 'mars_image_cleaning' uses it internally, so it # has to be backwards-compatible. # for no isolated pixels and min_number_picture_neighbors = 0 : testcases = { # from 'tailcuts_clean', for backwards-compatibility (p, p, p): [True, True, True], (p, p, 0): [True, True, False], (p, 0, p): [False, False, False], (0, p, 0): [False, False, False], # as in 'tailcuts_clean', but specifying 1st boundary threshold (p, b1, p): [True, True, True], (p, b1, 0): [True, True, False], (b1, b1, 0): [False, False, False], (b1, b1, b1): [False, False, False], (0, b1, 0): [False, False, False], # to put in test_tailcuts_clean! # specific for 'mars_image_cleaning' (p, b2, p): [True, True, True], (p, b2, 0): [True, True, False], (b1, b2, 0): [False, False, False], (b2, b2, 0): [False, False, False], (0, b2, 0): [False, False, False], (p, b1, b2): [True, True, True], (p, b2, b1): [True, True, True], (p, b1, b1): [True, True, True], (p, b2, b2): [True, True, True], (p, b1, b2 - 1): [True, True, False], } for image, mask in testcases.items(): result = cleaning.mars_cleaning_1st_pass( geom, np.array(image), picture_thresh=10, boundary_thresh=7, keep_isolated_pixels=False, ) assert (result == mask).all()
def test_largest_island(): """Test selection of largest island in imagea with given cleaning masks.""" from ctapipe.image import number_of_islands, largest_island # Create a simple rectangular camera made of 17 pixels camera = CameraGeometry.make_rectangular(17, 1) # Assume a simple image (flattened array) made of 0, 1 or 2 photoelectrons # [2, 1, 1, 1, 1, 2, 2, 1, 0, 2, 1, 1, 1, 0, 2, 2, 2] # Assume a virtual tailcut cleaning that requires: # - picture_threshold = 2 photoelectrons, # - boundary_threshold = 1 photoelectron, # - min_number_picture_neighbors = 0 # There will be 4 islands left after cleaning: clean_mask = np.zeros(camera.n_pixels).astype("bool") # initialization clean_mask[[0, 1]] = 1 clean_mask[[4, 5, 6, 7]] = 2 # this is the biggest clean_mask[[9, 10]] = 3 clean_mask[[14, 15, 16]] = 4 # Label islands (their number is not important here) _, islands_labels = number_of_islands(camera, clean_mask) # Create the true mask which takes into account only the biggest island # Pixels with no signal are labelled with a 0 true_mask_largest = np.zeros(camera.n_pixels).astype("bool") true_mask_largest[[4, 5, 6, 7]] = 1 # Apply the function to test mask_largest = largest_island(islands_labels) # Now the degenerate case of only one island surviving # Same process as before clean_mask_one = np.zeros(camera.n_pixels).astype("bool") clean_mask_one[[0, 1]] = 1 _, islands_labels_one = number_of_islands(camera, clean_mask_one) true_mask_largest_one = np.zeros(camera.n_pixels).astype("bool") true_mask_largest_one[[0, 1]] = 1 mask_largest_one = largest_island(islands_labels_one) # Last the case of no islands surviving clean_mask_0 = np.zeros(camera.n_pixels).astype("bool") _, islands_labels_0 = number_of_islands(camera, clean_mask_0) true_mask_largest_0 = np.zeros(camera.n_pixels).astype("bool") mask_largest_0 = largest_island(islands_labels_0) # Check if the function recovers the ground truth in all of the three cases assert (mask_largest_one == true_mask_largest_one).all() assert (mask_largest_0 == true_mask_largest_0).all() assert_allclose(mask_largest, true_mask_largest)
def test_tailcuts_clean_with_isolated_pixels(): # start with simple 3-pixel camera geom = CameraGeometry.make_rectangular(3, 1, (-1, 1)) p = 15 # picture value b = 7 # boundary value testcases = {(p, p, 0): [True, True, False], (p, 0, p): [True, False, True], (p, b, p): [True, True, True], (p, b, 0): [True, True, False], (0, p, 0): [False, True, False], (b, b, 0): [False, False, False]} for image, mask in testcases.items(): result = cleaning.tailcuts_clean(geom, np.array(image), picture_thresh=15, boundary_thresh=5, keep_isolated_pixels=True) assert (result == mask).all()
def test_make_rectangular_camera_geometry(): geom = CameraGeometry.make_rectangular() assert geom.pix_x.shape == geom.pix_y.shape
def test_make_rectangular_camera_geometry(): """ Check that we can construct a dummy camera with square geometry """ geom = CameraGeometry.make_rectangular() assert geom.pix_x.shape == geom.pix_y.shape
def test_make_rectangular_camera_geometry(): geom = CameraGeometry.make_rectangular() assert geom.pix_x.shape == geom.pix_y.shape
"""Example how to make a toymodel shower image and plot it. """ import matplotlib.pyplot as plt from ctapipe.image.toymodel import generate_2d_shower_model, \ make_toymodel_shower_image from ctapipe.instrument import CameraGeometry NX = 40 NY = 40 geom = CameraGeometry.make_rectangular(NX, NY) showermodel = generate_2d_shower_model(centroid=[0.25, 0.0], length=0.1, width=0.02, psi='40d') image, signal, noise = make_toymodel_shower_image(geom, showermodel.pdf, intensity=20, nsb_level_pe=30) # make them into 2D arrays so we can plot them with imshow image.shape = (NX, NY) signal.shape = (NX, NY) noise.shape = (NX, NY) # here we just plot the images using imshow(). For a more general # case, one should use a ctapipe.visualization.CameraDisplay plt.figure(figsize=(10, 3)) plt.subplot(1, 3, 1)
"""Example how to make a toymodel shower image and plot it. """ import matplotlib.pyplot as plt from ctapipe.image.toymodel import generate_2d_shower_model, \ make_toymodel_shower_image from ctapipe.instrument import CameraGeometry NX = 40 NY = 40 geom = CameraGeometry.make_rectangular(NX, NY) showermodel = generate_2d_shower_model(centroid=[0.25, 0.0], length=0.1, width=0.02, psi='40d') image, signal, noise = make_toymodel_shower_image(geom, showermodel.pdf, intensity=20, nsb_level_pe=30) # make them into 2D arrays so we can plot them with imshow image.shape = (NX, NY) signal.shape = (NX, NY) noise.shape = (NX, NY) # here we just plot the images using imshow(). For a more general # case, one should use a ctapipe.visualization.CameraDisplay plt.figure(figsize=(10, 3)) plt.subplot(1, 3, 1) plt.imshow(signal, interpolation='nearest', origin='lower') plt.title("Signal") plt.colorbar() plt.subplot(1, 3, 2)
) mask = tailcuts_clean( geom, image, picture_thresh=6 * image.mean(), boundary_thresh=4 * image.mean() ) cleaned = image.copy() cleaned[~mask] = 0 hillas = hillas_parameters(geom, cleaned) disp.image = image disp.add_colorbar(ax=axs[ii]) disp.set_limits_percent(95) disp.overlay_moments(hillas, linewidth=3, color='blue') if __name__ == '__main__': hexgeom = CameraGeometry.from_name("LSTCam") recgeom = CameraGeometry.make_rectangular() draw_several_cams(recgeom) draw_several_cams(hexgeom) plt.tight_layout() plt.show()
) mask = tailcuts_clean( geom, image, picture_thresh=6 * image.mean(), boundary_thresh=4 * image.mean() ) cleaned = image.copy() cleaned[~mask] = 0 hillas = hillas_parameters(geom, cleaned) disp.image = image disp.add_colorbar(ax=axs[ii]) disp.set_limits_percent(95) disp.overlay_moments(hillas, linewidth=3, color='blue') if __name__ == '__main__': hexgeom = CameraGeometry.from_name("LSTCam") recgeom = CameraGeometry.make_rectangular() draw_several_cams(recgeom) draw_several_cams(hexgeom) plt.tight_layout() plt.show()