Skip to content

Commit 439c77a

Browse files
Merge pull request #237 from NeurodataWithoutBorders/fix_conda_forge
Make Image checks and tests safe
2 parents cc353d7 + 7ff6aac commit 439c77a

File tree

2 files changed

+39
-19
lines changed

2 files changed

+39
-19
lines changed

nwbinspector/checks/images.py

Lines changed: 25 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,21 +4,29 @@
44

55
from ..register_checks import register_check, Importance, InspectorMessage
66

7+
try:
8+
from pynwb.base import Images
79

8-
@register_check(importance=Importance.BEST_PRACTICE_VIOLATION, neurodata_type=Images)
9-
def check_order_of_images_unique(images: Images):
10-
if images.order_of_images is None:
11-
return
12-
if not len(set(images.order_of_images)) == len(images.order_of_images):
13-
return InspectorMessage(message="order_of_images should have unique values.")
14-
15-
16-
@register_check(importance=Importance.BEST_PRACTICE_VIOLATION, neurodata_type=Images)
17-
def check_order_of_images_len(images: Images):
18-
if images.order_of_images is None:
19-
return
20-
if not len(images.order_of_images) == len(images.images):
21-
return InspectorMessage(
22-
message=f"Length of order_of_images ({len(images.order_of_images)}) does not match the number of images ("
23-
f"{len(images.images)})."
24-
)
10+
HAVE_IMAGES = True
11+
except ImportError:
12+
HAVE_IMAGES = False
13+
14+
15+
if HAVE_IMAGES:
16+
17+
@register_check(importance=Importance.BEST_PRACTICE_VIOLATION, neurodata_type=Images)
18+
def check_order_of_images_unique(images: Images):
19+
if images.order_of_images is None:
20+
return
21+
if not len(set(images.order_of_images)) == len(images.order_of_images):
22+
return InspectorMessage(message="order_of_images should have unique values.")
23+
24+
@register_check(importance=Importance.BEST_PRACTICE_VIOLATION, neurodata_type=Images)
25+
def check_order_of_images_len(images: Images):
26+
if images.order_of_images is None:
27+
return
28+
if not len(images.order_of_images) == len(images.images):
29+
return InspectorMessage(
30+
message=f"Length of order_of_images ({len(images.order_of_images)}) does not match the number of images ("
31+
f"{len(images.images)})."
32+
)

tests/unit_tests/test_images.py

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,21 @@
1-
import numpy as np
1+
import pytest
22

3-
from pynwb.base import Images, ImageReferences
3+
import numpy as np
44
from pynwb.image import GrayscaleImage
55

66
from nwbinspector import InspectorMessage, Importance
77
from nwbinspector.checks.images import check_order_of_images_unique, check_order_of_images_len
88

9+
try:
10+
from pynwb.base import Images, ImageReferences
11+
12+
HAVE_IMAGES = True
13+
except ImportError:
14+
HAVE_IMAGES = False
15+
skip_reason = "You must have PyNWB>=v2.1.0 to run these tests!"
16+
917

18+
@pytest.mark.skipif(not HAVE_IMAGES, reason=skip_reason)
1019
def test_check_order_of_images_unique():
1120

1221
imgs = [GrayscaleImage(name=f"image{i}", data=np.random.randn(10, 10)) for i in range(5)]
@@ -23,6 +32,7 @@ def test_check_order_of_images_unique():
2332
)
2433

2534

35+
@pytest.mark.skipif(not HAVE_IMAGES, reason=skip_reason)
2636
def test_pass_check_order_of_images_unique():
2737

2838
imgs = [GrayscaleImage(name=f"image{i}", data=np.random.randn(10, 10)) for i in range(5)]
@@ -32,6 +42,7 @@ def test_pass_check_order_of_images_unique():
3242
assert check_order_of_images_unique(images) is None
3343

3444

45+
@pytest.mark.skipif(not HAVE_IMAGES, reason=skip_reason)
3546
def test_check_order_of_images_len():
3647

3748
imgs = [GrayscaleImage(name=f"image{i}", data=np.random.randn(10, 10)) for i in range(5)]
@@ -48,6 +59,7 @@ def test_check_order_of_images_len():
4859
)
4960

5061

62+
@pytest.mark.skipif(not HAVE_IMAGES, reason=skip_reason)
5163
def test_pass_check_order_of_images_len():
5264

5365
imgs = [GrayscaleImage(name=f"image{i}", data=np.random.randn(10, 10)) for i in range(5)]

0 commit comments

Comments
 (0)