def test_wsireg_run_reg_reload_from_cache(data_out_dir, im_mch_np): img_fp1 = im_mch_np output_dir = str(data_out_dir) pname = gen_project_name_str() wsi_reg = WsiReg2D(pname, output_dir) wsi_reg.add_modality( "mod1", img_fp1, 0.65, channel_names=["test"], channel_colors=["red"], ) wsi_reg.add_modality( "mod2", img_fp1, 0.65, channel_names=["test"], channel_colors=["red"], ) wsi_reg.add_reg_path("mod1", "mod2", reg_params=["rigid_test"]) wsi_reg.register_images() pp_mod1_r1 = imread(wsi_reg.image_cache / "mod1_prepro.tiff") pp_mod2_r1 = imread(wsi_reg.image_cache / "mod2_prepro.tiff") # run registration again, loading data from cache wsi_reg = WsiReg2D(pname, output_dir) wsi_reg.add_modality( "mod1", img_fp1, 0.65, channel_names=["test"], channel_colors=["red"], preprocessing={"ch_indices": [1]}, ) wsi_reg.add_modality( "mod2", img_fp1, 0.65, channel_names=["test"], channel_colors=["red"], preprocessing={"ch_indices": [1]}, ) wsi_reg.add_reg_path("mod1", "mod2", reg_params=["rigid_test"]) wsi_reg.register_images() pp_mod1_r2 = imread(wsi_reg.image_cache / "mod1_prepro.tiff") pp_mod2_r2 = imread(wsi_reg.image_cache / "mod2_prepro.tiff") assert not np.array_equal(pp_mod1_r1, pp_mod1_r2) assert not np.array_equal(pp_mod2_r1, pp_mod2_r2)
def test_wsireg_run_reg_downsampling_m1m2_merge_no_prepro( data_out_dir, disk_im_gry): wsi_reg = WsiReg2D(gen_project_name_str(), str(data_out_dir)) img_fp1 = str(disk_im_gry) wsi_reg.add_modality( "mod1", img_fp1, 0.65, channel_names=["test"], channel_colors=["red"], preprocessing={"downsampling": 2}, ) wsi_reg.add_modality( "mod2", img_fp1, 0.65, channel_names=["test"], channel_colors=["red"], preprocessing={"downsampling": 2}, ) wsi_reg.add_reg_path("mod1", "mod2", reg_params=["rigid_test"]) wsi_reg.add_merge_modalities("mod12-merge", ["mod1", "mod2"]) wsi_reg.register_images() im_fps = wsi_reg.transform_images(transform_non_reg=False, remove_merged=True) regim = reg_image_loader(im_fps[0], 0.65) assert regim.shape == (2, 2048, 2048)
def test_wsireg_run_reg_downsampling_m1m2_changeores(data_out_dir, disk_im_gry): wsi_reg = WsiReg2D(gen_project_name_str(), str(data_out_dir)) img_fp1 = str(disk_im_gry) wsi_reg.add_modality( "mod1", img_fp1, 0.65, channel_names=["test"], channel_colors=["red"], preprocessing={"downsampling": 2}, output_res=(1.3, 1.3), ) wsi_reg.add_modality( "mod2", img_fp1, 0.65, channel_names=["test"], channel_colors=["red"], preprocessing={"downsampling": 2}, ) wsi_reg.add_reg_path("mod1", "mod2", reg_params=["rigid_test"]) wsi_reg.register_images() im_fps = wsi_reg.transform_images(transform_non_reg=False) regim = reg_image_loader(im_fps[0], 0.65) assert regim.shape[1:] == (1024, 1024)
def test_wsireg_run_reg_changeres(data_out_dir, disk_im_gry): wsi_reg = WsiReg2D(gen_project_name_str(), str(data_out_dir)) img_fp1 = str(disk_im_gry) wsi_reg.add_modality( "mod1", img_fp1, 0.65, channel_names=["test"], channel_colors=["red"], output_res=0.325, ) wsi_reg.add_modality( "mod2", img_fp1, 0.65, channel_names=["test"], channel_colors=["red"], ) wsi_reg.add_reg_path("mod1", "mod2", reg_params=["rigid_test", "affine_test"]) wsi_reg.register_images() im_fps = wsi_reg.transform_images(transform_non_reg=False) regim = reg_image_loader(im_fps[0], 0.325) assert regim.shape[1:] == (4096, 4096)
def test_wsireg_run_reg_shapes(data_out_dir, disk_im_gry): wsi_reg = WsiReg2D(gen_project_name_str(), str(data_out_dir)) img_fp1 = str(disk_im_gry) wsi_reg.add_modality( "mod1", img_fp1, 0.65, channel_names=["test"], channel_colors=["red"], ) wsi_reg.add_modality( "mod2", img_fp1, 0.65, channel_names=["test"], channel_colors=["red"], ) wsi_reg.add_reg_path("mod1", "mod2", reg_params=["rigid_test", "affine_test"]) wsi_reg.add_attachment_shapes("mod1", "shapeset", GEOJSON_FP) wsi_reg.register_images() wsi_reg.transform_shapes() wsi_reg.save_transformations() im_fps = wsi_reg.transform_images(transform_non_reg=False) gj_files = sorted(Path(im_fps[0]).parent.glob("*.geojson")) assert Path(im_fps[0]).exists() is True assert len(gj_files) > 0
def test_wsireg_run_reg_wmerge(data_out_dir, disk_im_gry): wsi_reg = WsiReg2D(gen_project_name_str(), str(data_out_dir)) img_fp1 = str(disk_im_gry) wsi_reg.add_modality( "mod1", img_fp1, 0.65, channel_names=["test"], channel_colors=["red"], ) wsi_reg.add_modality( "mod2", img_fp1, 0.65, channel_names=["test"], channel_colors=["red"], ) wsi_reg.add_reg_path("mod1", "mod2", reg_params=["rigid_test", "affine_test"]) wsi_reg.add_merge_modalities("test_merge", ["mod1", "mod2"]) wsi_reg.register_images() wsi_reg.save_transformations() im_fps = wsi_reg.transform_images(transform_non_reg=True) merged_im = reg_image_loader(im_fps[0], 0.65) assert Path(im_fps[0]).exists() is True assert merged_im.shape == (2, 2048, 2048)
def test_wsireg_run_reg_w_override(data_out_dir, im_mch_np): wsi_reg = WsiReg2D(gen_project_name_str(), str(data_out_dir)) img_fp1 = im_mch_np wsi_reg.add_modality( "mod1", img_fp1, 0.65, channel_names=["test"], channel_colors=["red"], ) wsi_reg.add_modality( "mod2", img_fp1, 0.65, channel_names=["test"], channel_colors=["red"], ) wsi_reg.add_modality( "mod3", img_fp1, 0.65, channel_names=["test"], channel_colors=["red"], ) wsi_reg.add_modality( "mod4", img_fp1, 0.65, channel_names=["test"], channel_colors=["red"], ) wsi_reg.add_reg_path("mod1", "mod2", reg_params=["rigid_test"]) wsi_reg.add_reg_path("mod4", "mod3", reg_params=["rigid_test"]) wsi_reg.add_reg_path( "mod2", "mod3", reg_params=["rigid_test"], override_prepro={ "source": { "ch_indices": [1] }, "target": { "ch_indices": [1] }, }, ) wsi_reg.register_images() or_mod2 = imread(wsi_reg.image_cache / "mod2-mod3-override_prepro.tiff") or_mod3 = imread(wsi_reg.image_cache / "mod3-mod2-override_prepro.tiff") pp_mod2 = imread(wsi_reg.image_cache / "mod2_prepro.tiff") pp_mod3 = imread(wsi_reg.image_cache / "mod3_prepro.tiff") assert not np.array_equal(or_mod2, pp_mod2) assert not np.array_equal(or_mod3, pp_mod3)
def config_to_WsiReg2D(config_filepath, output_dir): reg_config = parse_check_reg_config(config_filepath) reg_graph = WsiReg2D( reg_config.get("project_name"), output_dir, reg_config.get("cache_images"), ) return reg_graph
def test_wsireg_run_reg_with_crop_merge(data_out_dir, disk_im_gry): wsi_reg = WsiReg2D(gen_project_name_str(), str(data_out_dir)) img_fp1 = str(disk_im_gry) wsi_reg.add_modality( "mod1", img_fp1, 0.65, channel_names=["test"], channel_colors=["red"], ) wsi_reg.add_modality( "mod2", img_fp1, 0.65, channel_names=["test"], channel_colors=["red"], ) wsi_reg.add_modality( "mod3", img_fp1, 0.65, channel_names=["test"], channel_colors=["red"], preprocessing={ "mask_bbox": [512, 512, 512, 512], "crop_to_mask_bbox": True, }, ) wsi_reg.add_reg_path("mod1", "mod3", reg_params=["rigid_test"]) wsi_reg.add_reg_path("mod2", "mod3", reg_params=["rigid_test"]) wsi_reg.register_images() wsi_reg.add_merge_modalities("merge", ["mod1", "mod2", "mod3"]) # not cropped im_fps = wsi_reg.transform_images(transform_non_reg=True, to_original_size=True) registered_image_nocrop = reg_image_loader(im_fps[0], 1) # crop image im_fps = wsi_reg.transform_images(transform_non_reg=True, to_original_size=False) wsi_reg.save_transformations() registered_image_crop = reg_image_loader(im_fps[0], 1) assert registered_image_nocrop.shape[1:] == (2048, 2048) assert registered_image_crop.shape[1:] == (512, 512)
def test_wsireg_run_reg_downsampling_m1m2_merge_ds_attach( data_out_dir, disk_im_gry): wsi_reg = WsiReg2D(gen_project_name_str(), str(data_out_dir)) img_fp1 = str(disk_im_gry) wsi_reg.add_modality( "mod1", img_fp1, 0.65, channel_names=["test"], channel_colors=["red"], preprocessing={"downsampling": 2}, ) wsi_reg.add_modality( "mod2", img_fp1, 0.65, channel_names=["test"], channel_colors=["red"], preprocessing={"downsampling": 2}, ) wsi_reg.add_attachment_images( "mod2", "mod3", img_fp1, 0.65, channel_names=["test"], channel_colors=["red"], ) wsi_reg.add_reg_path("mod1", "mod2", reg_params=["rigid_test"]) wsi_reg.add_merge_modalities("mod12-merge", ["mod1", "mod2", "mod3"]) wsi_reg.register_images() im_fps = wsi_reg.transform_images(transform_non_reg=False, remove_merged=True) regim = reg_image_loader(im_fps[0], 0.65) ome_data = from_xml(TiffFile(im_fps[0]).ome_metadata) assert regim.shape == (3, 2048, 2048) assert ome_data.images[0].pixels.physical_size_x == 0.65 assert ome_data.images[0].pixels.physical_size_y == 0.65 assert ome_data.images[0].pixels.size_x == 2048 assert ome_data.images[0].pixels.size_y == 2048 assert ome_data.images[0].pixels.size_c == 3
def test_wsireg_run_reg_wattachment_ds2(data_out_dir, disk_im_gry): wsi_reg = WsiReg2D(gen_project_name_str(), str(data_out_dir)) im1 = np.random.randint(0, 255, (2048, 2048), dtype=np.uint16) im2 = np.random.randint(0, 255, (2048, 2048), dtype=np.uint16) wsi_reg.add_modality( "mod1", im1, 0.65, channel_names=["test"], channel_colors=["red"], preprocessing={"downsampling": 2}, ) wsi_reg.add_modality( "mod2", im2, 0.65, channel_names=["test"], channel_colors=["red"], preprocessing={"downsampling": 2}, ) wsi_reg.add_attachment_images("mod2", "attached", im2, image_res=0.65) wsi_reg.add_attachment_images("mod1", "attached2", im1, image_res=0.65) wsi_reg.add_reg_path("mod2", "mod1", reg_params=["rigid_test", "affine_test"]) wsi_reg.register_images() im_fps = wsi_reg.transform_images(transform_non_reg=False) wsi_reg.save_transformations() regim = reg_image_loader(im_fps[0], 0.65) attachim = reg_image_loader(im_fps[1], 0.65) attachim2 = reg_image_loader(im_fps[2], 0.65) assert np.array_equal( np.squeeze(regim.dask_image.compute()), np.squeeze(attachim.dask_image.compute()), ) assert np.array_equal(np.squeeze(im1), np.squeeze(attachim2.dask_image.compute()))
def test_wsireg2d_add_merge_modality_notfound(data_out_dir, data_im_fp): wsi_reg = WsiReg2D(gen_project_name_str(), str(data_out_dir)) img_fp1 = str(data_im_fp) wsi_reg.add_modality( "test_mod1", img_fp1, 0.65, channel_names=["test"], channel_colors=["red"], ) img_fp1 = str(data_im_fp) wsi_reg.add_modality( "test_mod2", img_fp1, 0.65, channel_names=["test"], channel_colors=["red"], ) with pytest.raises(ValueError): wsi_reg.add_merge_modalities("mergetest", ["test_mod1", "test_mod3"])
def test_wsireg_run_reg_downsampling_from_cache(data_out_dir, disk_im_gry): pstr = gen_project_name_str() wsi_reg = WsiReg2D(pstr, str(data_out_dir)) img_fp1 = str(disk_im_gry) wsi_reg.add_modality( "mod1", img_fp1, 0.65, channel_names=["test"], channel_colors=["red"], preprocessing={"downsampling": 2}, ) wsi_reg.add_modality( "mod2", img_fp1, 0.65, channel_names=["test"], channel_colors=["red"], preprocessing={"downsampling": 2}, ) wsi_reg.add_attachment_images( "mod2", "mod3", img_fp1, 0.65, channel_names=["test"], channel_colors=["red"], ) wsi_reg.add_reg_path("mod1", "mod2", reg_params=["rigid_test"]) wsi_reg.register_images() im_fps_nocache = wsi_reg.transform_images(transform_non_reg=True, remove_merged=True) regim_nocache = reg_image_loader(im_fps_nocache[0], 0.65) regim_nocache_attach = reg_image_loader(im_fps_nocache[1], 0.65) regim_nocache_br = reg_image_loader(im_fps_nocache[2], 0.65) wsi_reg2 = WsiReg2D(pstr, str(data_out_dir)) img_fp1 = str(disk_im_gry) wsi_reg2.add_modality( "mod1", img_fp1, 0.65, channel_names=["test"], channel_colors=["red"], preprocessing={"downsampling": 2}, ) wsi_reg2.add_modality( "mod2", img_fp1, 0.65, channel_names=["test"], channel_colors=["red"], preprocessing={"downsampling": 2}, ) wsi_reg2.add_attachment_images( "mod2", "mod3", img_fp1, 0.65, channel_names=["test"], channel_colors=["red"], ) wsi_reg2.add_reg_path("mod1", "mod2", reg_params=["rigid_test"]) wsi_reg2.register_images() im_fps_cache = wsi_reg.transform_images(transform_non_reg=True, remove_merged=True) regim_cache = reg_image_loader(im_fps_cache[0], 0.65) regim_cache_attach = reg_image_loader(im_fps_cache[1], 0.65) regim_cache_br = reg_image_loader(im_fps_cache[2], 0.65) assert regim_cache.shape == regim_nocache.shape assert regim_cache_br.shape == regim_nocache_br.shape assert regim_cache_attach.shape == regim_nocache_attach.shape
def test_WsiReg2D_instantiation(data_out_dir): pstr = gen_project_name_str() wsi_reg = WsiReg2D(pstr, str(data_out_dir)) assert wsi_reg.project_name == pstr assert wsi_reg.output_dir == Path(str(data_out_dir))