tests.test_frames
1import os 2 3import pytest 4 5import cv2 6 7from csi_images.csi_scans import Scan 8from csi_images.csi_tiles import Tile 9from csi_images.csi_frames import Frame 10 11if os.environ.get("DEBIAN_FRONTEND") == "noninteractive": 12 # For Docker testing; do not try to show plots 13 SHOW_PLOTS = False 14else: 15 # Change this to your preference for local testing, but commit as True 16 SHOW_PLOTS = True 17 18 19@pytest.fixture 20def scan(): 21 return Scan.load_yaml("tests/data") 22 23 24@pytest.fixture 25def tile(scan): 26 return Tile(scan, 100) 27 28 29def test_paths(scan, tile): 30 frames = Frame.get_frames(tile) 31 # Check that the paths exist 32 for frame in frames: 33 # Manually creating the path 34 file_path = os.path.join(scan.path, frame.get_file_name()) 35 assert os.path.exists(file_path) 36 # Using the build-in method 37 assert os.path.exists(frame.get_file_path()) 38 # Check that the images are "valid" 39 assert frame.check_image() 40 # Check that all images are valid 41 assert Frame.check_all_images(scan) 42 # Manually set up a frame that shouldn't exist 43 tile.x = 100 44 for frame in Frame.get_frames(tile): 45 assert not frame.check_image() 46 47 48def test_getting_frames(scan, tile): 49 # Get frames for a single tile 50 frames = Frame.get_frames(tile) 51 assert len(frames) == 4 52 53 # Get all frames for the scan 54 frames = Frame.get_all_frames(scan) 55 assert len(frames) == scan.roi[0].tile_rows * scan.roi[0].tile_cols 56 assert len(frames[0]) == 4 57 # Get all frames in a grid 58 frames = Frame.get_all_frames(scan, as_flat=False) 59 assert len(frames) == scan.roi[0].tile_rows 60 assert len(frames[0]) == scan.roi[0].tile_cols 61 assert len(frames[0][0]) == 4 62 63 64def test_make_rgb(scan, tile): 65 frames = Frame.get_frames(tile) 66 67 if SHOW_PLOTS: 68 for frame in frames: 69 cv2.imshow("Frames from a tile", frame.get_image()) 70 cv2.waitKey(0) 71 cv2.destroyAllWindows() 72 73 channel_indices = scan.get_channel_indices(["TRITC", "CY5", "DAPI"]) 74 channels = { 75 channel_indices[0]: (1.0, 0.0, 0.0), 76 channel_indices[1]: (0.0, 1.0, 0.0), 77 channel_indices[2]: (0.0, 0.0, 1.0), 78 } 79 image = Frame.make_rgb_image(tile, channels) 80 real_image_size = scan.get_image_size() + (3,) 81 assert image.shape == real_image_size 82 83 if SHOW_PLOTS: 84 cv2.imshow("RGB tile", cv2.cvtColor(image, cv2.COLOR_RGB2BGR)) 85 cv2.waitKey(0) 86 cv2.destroyAllWindows() 87 88 # Test with a white channel 89 channel_indices = scan.get_channel_indices(["TRITC", "CY5", "DAPI", "AF488"]) 90 channels = { 91 channel_indices[0]: (1.0, 0.0, 0.0), 92 channel_indices[1]: (0.0, 1.0, 0.0), 93 channel_indices[2]: (0.0, 0.0, 1.0), 94 channel_indices[3]: (1.0, 1.0, 1.0), 95 } 96 image = Frame.make_rgb_image(tile, channels) 97 assert image.shape == real_image_size 98 99 if SHOW_PLOTS: 100 cv2.imshow("RGBW tile", cv2.cvtColor(image, cv2.COLOR_RGB2BGR)) 101 cv2.waitKey(0) 102 cv2.destroyAllWindows()
@pytest.fixture
def
scan():
@pytest.fixture
def
tile(scan):
def
test_paths(scan, tile):
30def test_paths(scan, tile): 31 frames = Frame.get_frames(tile) 32 # Check that the paths exist 33 for frame in frames: 34 # Manually creating the path 35 file_path = os.path.join(scan.path, frame.get_file_name()) 36 assert os.path.exists(file_path) 37 # Using the build-in method 38 assert os.path.exists(frame.get_file_path()) 39 # Check that the images are "valid" 40 assert frame.check_image() 41 # Check that all images are valid 42 assert Frame.check_all_images(scan) 43 # Manually set up a frame that shouldn't exist 44 tile.x = 100 45 for frame in Frame.get_frames(tile): 46 assert not frame.check_image()
def
test_getting_frames(scan, tile):
49def test_getting_frames(scan, tile): 50 # Get frames for a single tile 51 frames = Frame.get_frames(tile) 52 assert len(frames) == 4 53 54 # Get all frames for the scan 55 frames = Frame.get_all_frames(scan) 56 assert len(frames) == scan.roi[0].tile_rows * scan.roi[0].tile_cols 57 assert len(frames[0]) == 4 58 # Get all frames in a grid 59 frames = Frame.get_all_frames(scan, as_flat=False) 60 assert len(frames) == scan.roi[0].tile_rows 61 assert len(frames[0]) == scan.roi[0].tile_cols 62 assert len(frames[0][0]) == 4
def
test_make_rgb(scan, tile):
65def test_make_rgb(scan, tile): 66 frames = Frame.get_frames(tile) 67 68 if SHOW_PLOTS: 69 for frame in frames: 70 cv2.imshow("Frames from a tile", frame.get_image()) 71 cv2.waitKey(0) 72 cv2.destroyAllWindows() 73 74 channel_indices = scan.get_channel_indices(["TRITC", "CY5", "DAPI"]) 75 channels = { 76 channel_indices[0]: (1.0, 0.0, 0.0), 77 channel_indices[1]: (0.0, 1.0, 0.0), 78 channel_indices[2]: (0.0, 0.0, 1.0), 79 } 80 image = Frame.make_rgb_image(tile, channels) 81 real_image_size = scan.get_image_size() + (3,) 82 assert image.shape == real_image_size 83 84 if SHOW_PLOTS: 85 cv2.imshow("RGB tile", cv2.cvtColor(image, cv2.COLOR_RGB2BGR)) 86 cv2.waitKey(0) 87 cv2.destroyAllWindows() 88 89 # Test with a white channel 90 channel_indices = scan.get_channel_indices(["TRITC", "CY5", "DAPI", "AF488"]) 91 channels = { 92 channel_indices[0]: (1.0, 0.0, 0.0), 93 channel_indices[1]: (0.0, 1.0, 0.0), 94 channel_indices[2]: (0.0, 0.0, 1.0), 95 channel_indices[3]: (1.0, 1.0, 1.0), 96 } 97 image = Frame.make_rgb_image(tile, channels) 98 assert image.shape == real_image_size 99 100 if SHOW_PLOTS: 101 cv2.imshow("RGBW tile", cv2.cvtColor(image, cv2.COLOR_RGB2BGR)) 102 cv2.waitKey(0) 103 cv2.destroyAllWindows()