diff --git a/brainrender/atlas_specific/allen_brain_atlas/streamlines.py b/brainrender/atlas_specific/allen_brain_atlas/streamlines.py index 80156e1b..29fc42f1 100644 --- a/brainrender/atlas_specific/allen_brain_atlas/streamlines.py +++ b/brainrender/atlas_specific/allen_brain_atlas/streamlines.py @@ -92,8 +92,8 @@ def get_streamlines_data(eids, force_download=False): def get_streamlines_for_region(region, force_download=False): """ - Using the Allen Mouse Connectivity data and corresponding API, this function finds expeirments whose injections - were targeted to the region of interest and downloads the corresponding streamlines data. By default, experiements + Using the Allen Mouse Connectivity data and corresponding API, this function finds experiments whose injections + were targeted to the region of interest and downloads the corresponding streamlines data. By default, experiments are selected for only WT mice and only when the region was the primary injection target. :param region: str with region to use for research diff --git a/examples/brain_regions.py b/examples/brain_regions.py index 0e4a64a0..9df88ef8 100644 --- a/examples/brain_regions.py +++ b/examples/brain_regions.py @@ -8,13 +8,12 @@ print(f"[{orange}]Running example: {Path(__file__).name}") # Create a brainrender scene -scene = Scene(title="brain regions") +scene = Scene(title="brain regions", atlas_name="allen_human_500um") # Add brain regions -scene.add_brain_region("TH") +scene.add_brain_region("FGM") # You can specify color, transparency... -scene.add_brain_region("MOs", "CA1", alpha=0.2, color="green") # Render! scene.render() diff --git a/examples/screenshot.py b/examples/screenshot.py index b8b67a91..45378477 100644 --- a/examples/screenshot.py +++ b/examples/screenshot.py @@ -45,6 +45,6 @@ # Take a screenshot - passing no name uses current time # Screenshots can be also created during runtime by pressing "s" -scene.screenshot(name="example_brainrender_shot", scale=scale) +scene.screenshot(name="example_brainrender_shot.pdf", scale=scale) scene.close() diff --git a/tests/data/screenshot.jpg b/tests/data/screenshot.jpg new file mode 100644 index 00000000..5358b62e Binary files /dev/null and b/tests/data/screenshot.jpg differ diff --git a/tests/data/screenshot.png b/tests/data/screenshot.png new file mode 100644 index 00000000..acd56f67 Binary files /dev/null and b/tests/data/screenshot.png differ diff --git a/tests/test_scene.py b/tests/test_scene.py index 8ddf3cee..d99fce48 100644 --- a/tests/test_scene.py +++ b/tests/test_scene.py @@ -1,8 +1,3 @@ -import shutil -from pathlib import Path - -import pytest - from brainrender import Scene from brainrender.actor import Actor @@ -101,38 +96,6 @@ def test_scene_slice(): del s -@pytest.mark.parametrize( - "name, scale, expected_suffix", - [ - ("test", 2, ".png"), - (None, None, ".png"), - (None, 1, ".png"), - ("test2", None, ".png"), - ("test.jpg", 1, ".jpg"), - ("test.eps", 1, ".eps"), - ("test.svg", 1, ".svg"), - ("test.pdf", 1, ".pdf"), - ("test.tiff", 1, ".png"), - ], -) -def test_scene_screenshot(name, scale, expected_suffix): - screenshot_folder = Path.home() / "test_screenshots" - s = Scene(screenshots_folder=screenshot_folder) - out_path = s.screenshot(name=name, scale=scale) - - assert Path(out_path).suffix == expected_suffix - - # Vedo exports eps and svg files as gzipped files - # Append the .gz suffix to the expected path to check if file exists - if expected_suffix in [".eps", ".svg"]: - out_path += ".gz" - - assert Path(out_path).exists() - - shutil.rmtree(screenshot_folder) - del s - - def test_actor_removal(): s = Scene() th = s.add_brain_region("TH") diff --git a/tests/test_screenshot.py b/tests/test_screenshot.py new file mode 100644 index 00000000..4c7ef948 --- /dev/null +++ b/tests/test_screenshot.py @@ -0,0 +1,48 @@ +import platform +from pathlib import Path + +import pytest +from skimage.color import rgb2gray +from skimage.io import imread +from skimage.metrics import structural_similarity as ssim + +from brainrender import Scene + +validate_directory = Path(__file__).parent / "data" + + +@pytest.mark.parametrize("extension", ["png", "jpg", "svg", "eps", "pdf"]) +def test_screenshot(tmp_path, extension, similarity_threshold=0.75): + filename = "screenshot." + extension + scene = Scene( + screenshots_folder=tmp_path, + ) + scene.add_brain_region("TH") + scene.render(interactive=False, zoom=2) + scene.screenshot(name=filename) + scene.close() + + test_filepath = tmp_path / filename + + # These are saved compressed + if extension in ["eps", "svg"]: + test_filepath = test_filepath.parent / (test_filepath.name + ".gz") + + assert test_filepath.exists() + + # These are the only raster formats + if extension in ["png", "jpg"]: + test_image = rgb2gray(imread(test_filepath)) + validate_filepath = validate_directory / filename + validate_image = rgb2gray(imread(validate_filepath)) + + assert test_image.shape == validate_image.shape + + if platform.system() != "Linux": + # The screenshots are not produced correctly on Linux in CI + data_range = validate_image.max() - validate_image.min() + similarity_index, _ = ssim( + test_image, validate_image, data_range=data_range, full=True + ) + + assert similarity_index > similarity_threshold