Skip to content

Commit

Permalink
Merge pull request #351 from brainglobe/screenshot
Browse files Browse the repository at this point in the history
Add screenshot tests
  • Loading branch information
alessandrofelder authored May 21, 2024
2 parents 48f655f + cbeee3b commit 72b994a
Show file tree
Hide file tree
Showing 7 changed files with 53 additions and 43 deletions.
4 changes: 2 additions & 2 deletions brainrender/atlas_specific/allen_brain_atlas/streamlines.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 2 additions & 3 deletions examples/brain_regions.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
2 changes: 1 addition & 1 deletion examples/screenshot.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Binary file added tests/data/screenshot.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added tests/data/screenshot.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
37 changes: 0 additions & 37 deletions tests/test_scene.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,3 @@
import shutil
from pathlib import Path

import pytest

from brainrender import Scene
from brainrender.actor import Actor

Expand Down Expand Up @@ -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")
Expand Down
48 changes: 48 additions & 0 deletions tests/test_screenshot.py
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 72b994a

Please sign in to comment.