tests.test_frames
1import os 2 3import cv2 4 5from csi_images import csi_frames, csi_tiles, csi_scans 6 7if os.environ.get("DEBIAN_FRONTEND") == "noninteractive": 8 SHOW_PLOTS = False 9else: 10 # Change this to your preference for local testing, but commit as True 11 SHOW_PLOTS = True 12 13 14def test_getting_frames(): 15 scan = csi_scans.Scan.load_yaml("tests/data") 16 tile = csi_tiles.Tile(scan, 100) 17 frames = csi_frames.Frame.get_frames(tile) 18 assert len(frames) == 4 19 frames = csi_frames.Frame.get_all_frames(scan) 20 assert len(frames) == scan.roi[0].tile_rows * scan.roi[0].tile_cols 21 assert len(frames[0]) == 4 22 frames = csi_frames.Frame.get_all_frames(scan, as_flat=False) 23 assert len(frames) == scan.roi[0].tile_rows 24 assert len(frames[0]) == scan.roi[0].tile_cols 25 assert len(frames[0][0]) == 4 26 27 28def test_checking_frames(): 29 scan = csi_scans.Scan.load_yaml("tests/data") 30 tile = csi_tiles.Tile(scan, 100) 31 frames = csi_frames.Frame.get_frames(tile) 32 assert len(frames) == 4 33 for frame in frames: 34 assert frame.check_image() 35 assert csi_frames.Frame.check_all_images(scan) 36 # Manually set up a frame that shouldn't exist 37 tile.x = 100 38 for frame in csi_frames.Frame.get_frames(tile): 39 assert not frame.check_image() 40 41 42def test_make_rgb(): 43 scan = csi_scans.Scan.load_txt("tests/data") 44 tile = csi_tiles.Tile(scan, 1000) 45 frames = csi_frames.Frame.get_frames(tile) 46 47 if SHOW_PLOTS: 48 for frame in frames: 49 cv2.imshow("Frames from a tile", frame.get_image()) 50 cv2.waitKey(0) 51 cv2.destroyAllWindows() 52 53 channel_indices = scan.get_channel_indices(["TRITC", "CY5", "DAPI"]) 54 channels = { 55 channel_indices[0]: (1.0, 0.0, 0.0), 56 channel_indices[1]: (0.0, 1.0, 0.0), 57 channel_indices[2]: (0.0, 0.0, 1.0), 58 } 59 image = csi_frames.Frame.make_rgb_image(tile, channels) 60 assert image.shape == (scan.tile_height_px, scan.tile_width_px, 3) 61 62 if SHOW_PLOTS: 63 cv2.imshow("RGB tile", cv2.cvtColor(image, cv2.COLOR_RGB2BGR)) 64 cv2.waitKey(0) 65 cv2.destroyAllWindows() 66 67 # Test with a white channel 68 channel_indices = scan.get_channel_indices(["TRITC", "CY5", "DAPI", "AF488"]) 69 channels = { 70 channel_indices[0]: (1.0, 0.0, 0.0), 71 channel_indices[1]: (0.0, 1.0, 0.0), 72 channel_indices[2]: (0.0, 0.0, 1.0), 73 channel_indices[3]: (1.0, 1.0, 1.0), 74 } 75 image = csi_frames.Frame.make_rgb_image(tile, channels) 76 assert image.shape == (scan.tile_height_px, scan.tile_width_px, 3) 77 78 if SHOW_PLOTS: 79 cv2.imshow("RGBW tile", cv2.cvtColor(image, cv2.COLOR_RGB2BGR)) 80 cv2.waitKey(0) 81 cv2.destroyAllWindows()
def
test_getting_frames():
15def test_getting_frames(): 16 scan = csi_scans.Scan.load_yaml("tests/data") 17 tile = csi_tiles.Tile(scan, 100) 18 frames = csi_frames.Frame.get_frames(tile) 19 assert len(frames) == 4 20 frames = csi_frames.Frame.get_all_frames(scan) 21 assert len(frames) == scan.roi[0].tile_rows * scan.roi[0].tile_cols 22 assert len(frames[0]) == 4 23 frames = csi_frames.Frame.get_all_frames(scan, as_flat=False) 24 assert len(frames) == scan.roi[0].tile_rows 25 assert len(frames[0]) == scan.roi[0].tile_cols 26 assert len(frames[0][0]) == 4
def
test_checking_frames():
29def test_checking_frames(): 30 scan = csi_scans.Scan.load_yaml("tests/data") 31 tile = csi_tiles.Tile(scan, 100) 32 frames = csi_frames.Frame.get_frames(tile) 33 assert len(frames) == 4 34 for frame in frames: 35 assert frame.check_image() 36 assert csi_frames.Frame.check_all_images(scan) 37 # Manually set up a frame that shouldn't exist 38 tile.x = 100 39 for frame in csi_frames.Frame.get_frames(tile): 40 assert not frame.check_image()
def
test_make_rgb():
43def test_make_rgb(): 44 scan = csi_scans.Scan.load_txt("tests/data") 45 tile = csi_tiles.Tile(scan, 1000) 46 frames = csi_frames.Frame.get_frames(tile) 47 48 if SHOW_PLOTS: 49 for frame in frames: 50 cv2.imshow("Frames from a tile", frame.get_image()) 51 cv2.waitKey(0) 52 cv2.destroyAllWindows() 53 54 channel_indices = scan.get_channel_indices(["TRITC", "CY5", "DAPI"]) 55 channels = { 56 channel_indices[0]: (1.0, 0.0, 0.0), 57 channel_indices[1]: (0.0, 1.0, 0.0), 58 channel_indices[2]: (0.0, 0.0, 1.0), 59 } 60 image = csi_frames.Frame.make_rgb_image(tile, channels) 61 assert image.shape == (scan.tile_height_px, scan.tile_width_px, 3) 62 63 if SHOW_PLOTS: 64 cv2.imshow("RGB tile", cv2.cvtColor(image, cv2.COLOR_RGB2BGR)) 65 cv2.waitKey(0) 66 cv2.destroyAllWindows() 67 68 # Test with a white channel 69 channel_indices = scan.get_channel_indices(["TRITC", "CY5", "DAPI", "AF488"]) 70 channels = { 71 channel_indices[0]: (1.0, 0.0, 0.0), 72 channel_indices[1]: (0.0, 1.0, 0.0), 73 channel_indices[2]: (0.0, 0.0, 1.0), 74 channel_indices[3]: (1.0, 1.0, 1.0), 75 } 76 image = csi_frames.Frame.make_rgb_image(tile, channels) 77 assert image.shape == (scan.tile_height_px, scan.tile_width_px, 3) 78 79 if SHOW_PLOTS: 80 cv2.imshow("RGBW tile", cv2.cvtColor(image, cv2.COLOR_RGB2BGR)) 81 cv2.waitKey(0) 82 cv2.destroyAllWindows()