From 6fe77933f83419b50cd3360c4c9f6fc392aecb05 Mon Sep 17 00:00:00 2001 From: Zach Burnett Date: Fri, 2 Dec 2022 08:29:04 -0500 Subject: [PATCH] remove dependency on `opencv-python` (#126) * remove dependency on `opencv-python` * apply `flake8` fixes * add change log entry * update dependencies --- CHANGES.rst | 2 +- pyproject.toml | 4 +++- src/stcal/jump/jump.py | 12 +++++++++++- tests/test_jump.py | 16 +++++++++++----- 4 files changed, 26 insertions(+), 8 deletions(-) diff --git a/CHANGES.rst b/CHANGES.rst index fbc1baff..f20255ec 100644 --- a/CHANGES.rst +++ b/CHANGES.rst @@ -5,7 +5,7 @@ General ------- - Moved build configuration from ``setup.cfg`` to ``pyproject.toml`` to support PEP621 [#95] - +- made dependency on ``opencv-python`` conditional [#126] ramp_fitting ~~~~~~~~~~~~ diff --git a/pyproject.toml b/pyproject.toml index 109a2a8a..069e5bef 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -15,7 +15,6 @@ dependencies = [ 'astropy >=5.0.4', 'scipy >=1.6.0', 'numpy >=1.17', - 'opencv-python >=4.6.0.66', ] dynamic = ['version'] @@ -36,6 +35,9 @@ test = [ 'pytest-doctestplus', 'pytest-openfiles >=0.5.0', ] +opencv = [ + 'opencv-python >=4.6.0.66', +] [project.urls] 'repository' = 'https://github.com/spacetelescope/stcal' diff --git a/src/stcal/jump/jump.py b/src/stcal/jump/jump.py index 267169b3..1ab252b2 100644 --- a/src/stcal/jump/jump.py +++ b/src/stcal/jump/jump.py @@ -1,12 +1,22 @@ import time import logging +import warnings + import numpy as np -import cv2 as cv from . import twopoint_difference as twopt from . import constants import multiprocessing +try: + import cv2 as cv + + OPENCV_INSTALLED = True +except ImportError: + OPENCV_INSTALLED = False + warnings.warn('Could not import `opencv-python`; ' + 'certain snowball detection and usage of ellipses will be inoperable') + log = logging.getLogger(__name__) log.setLevel(logging.DEBUG) diff --git a/tests/test_jump.py b/tests/test_jump.py index 615f0a23..500dd7f4 100644 --- a/tests/test_jump.py +++ b/tests/test_jump.py @@ -1,17 +1,21 @@ -import pytest import numpy as np +import pytest from astropy.io import fits from stcal.jump.jump import flag_large_events, find_circles, find_ellipses +DQFLAGS = {'JUMP_DET': 4, 'SATURATED': 2, 'DO_NOT_USE': 1} +try: + import cv2 as cv -DQFLAGS = {'JUMP_DET': 4, 'SATURATED': 2, 'DO_NOT_USE': 1} + OPENCV_INSTALLED = True +except ImportError: + OPENCV_INSTALLED = False @pytest.fixture(scope='function') def setup_cube(): - def _cube(ngroups, readnoise=10): nints = 1 nrows = 204 @@ -27,6 +31,7 @@ def _cube(ngroups, readnoise=10): return _cube +@pytest.mark.skipif(not OPENCV_INSTALLED, reason="`opencv-python` not installed") def test_find_simple_circle(): plane = np.zeros(shape=(5, 5), dtype=np.uint8) plane[2, 2] = DQFLAGS['JUMP_DET'] @@ -38,6 +43,7 @@ def test_find_simple_circle(): assert circle[0][1] == pytest.approx(1.0, 1e-3) +@pytest.mark.skipif(not OPENCV_INSTALLED, reason="`opencv-python` not installed") def test_find_simple_ellipse(): plane = np.zeros(shape=(5, 5), dtype=np.uint8) plane[2, 2] = DQFLAGS['JUMP_DET'] @@ -49,9 +55,10 @@ def test_find_simple_ellipse(): plane[2, 4] = DQFLAGS['JUMP_DET'] plane[3, 3] = DQFLAGS['JUMP_DET'] ellipse = find_ellipses(plane, DQFLAGS['JUMP_DET'], 1) - assert ellipse[0][2] == pytest.approx(45.0, 1e-3) # 90 degree rotation + assert ellipse[0][2] == pytest.approx(45.0, 1e-3) # 90 degree rotation assert ellipse[0][0] == pytest.approx((2.5, 2.0)) # center + @pytest.mark.skip(reason="only for local testing") def test_single_group(): inplane = fits.getdata("jumppix.fits") @@ -61,4 +68,3 @@ def test_single_group(): min_jump_area=15, max_offset=1, expand_factor=1.1, use_ellipses=True, sat_required_snowball=False) fits.writeto("jumppix_expand.fits", indq, overwrite=True) -