Skip to content

Commit

Permalink
TST: Option to disable using C-extension
Browse files Browse the repository at this point in the history
for debugging
  • Loading branch information
pllim committed May 15, 2024
1 parent 898f364 commit eb3c6eb
Show file tree
Hide file tree
Showing 7 changed files with 52 additions and 23 deletions.
8 changes: 8 additions & 0 deletions .github/workflows/spherical_geometry.yml
Original file line number Diff line number Diff line change
Expand Up @@ -51,3 +51,11 @@ jobs:
- name: Python 3.12 with dev version of dependencies
linux: py312-test-devdeps
posargs: -v
- name: Python 3.11 without C-ext
linux: py311-test-disablecufuncs
posargs: -v
- name: Python 3.12 with dev version of dependencies without C-ext
linux: py312-test-disablecufuncs-devdeps
posargs: -v
15 changes: 15 additions & 0 deletions conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,18 @@
PYTEST_HEADER_MODULES.pop('h5py', None)

TESTED_VERSIONS['spherical-geometry'] = version


# This has to be in the root dir or it will not display in CI.
def pytest_report_header(config):
import os

from spherical_geometry import DISABLE_C_UFUNCS, HAS_C_UFUNCS

# This gets added after the pytest-astropy-header output.
return (
f'CI: {os.environ.get("CI", "undefined")}\n'
f'DISABLE_SPHR_GEOM_C_UFUNCS: {os.environ.get("DISABLE_SPHR_GEOM_C_UFUNCS", "undefined")}\n'
f'DISABLE_C_UFUNCS: {DISABLE_C_UFUNCS}\n'
f'HAS_C_UFUNCS: {HAS_C_UFUNCS}\n'
)
15 changes: 15 additions & 0 deletions spherical_geometry/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,19 @@
import os

__all__ = ["__version__", "HAS_C_UFUNCS"]

try:
from ._version import version as __version__
except ImportError:
__version__ = ''


DISABLE_C_UFUNCS = os.environ.get("DISABLE_SPHR_GEOM_C_UFUNCS", "false") == "true"
if DISABLE_C_UFUNCS:
HAS_C_UFUNCS = False

Check warning on line 13 in spherical_geometry/__init__.py

View check run for this annotation

Codecov / codecov/patch

spherical_geometry/__init__.py#L13

Added line #L13 was not covered by tests
else:
try:
from . import math_util # noqa: F401
HAS_C_UFUNCS = True
except ImportError:
HAS_C_UFUNCS = False
10 changes: 3 additions & 7 deletions spherical_geometry/great_circle_arc.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,9 @@

# LOCAL
from spherical_geometry.vector import two_d

# C versions of the code have been written to speed up operations
# HAS_C_UFUNCS: C versions of the code have been written to speed up operations
# the python versions are a fallback if the C cannot be used
try:
from spherical_geometry import math_util
HAS_C_UFUNCS = True
except ImportError:
HAS_C_UFUNCS = False
from spherical_geometry import HAS_C_UFUNCS

__all__ = ['angle', 'interpolate', 'intersection', 'intersects',
'intersects_point', 'length', 'midpoint']
Expand All @@ -32,6 +27,7 @@ def _inner1d_np(x, y):


if HAS_C_UFUNCS:
from spherical_geometry import math_util
inner1d = math_util.inner1d
else:
inner1d = _inner1d_np
Expand Down
16 changes: 6 additions & 10 deletions spherical_geometry/tests/test_basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,13 @@
import pytest
from numpy.testing import assert_almost_equal, assert_allclose

from spherical_geometry import graph, great_circle_arc, polygon, vector
from spherical_geometry import graph, great_circle_arc, polygon, vector, HAS_C_UFUNCS
from spherical_geometry.tests.helpers import (
ROOT_DIR,
get_point_set,
resolve_imagename
)

try:
from spherical_geometry import math_util
except ImportError:
math_util = None

graph.DEBUG = True


Expand Down Expand Up @@ -522,18 +517,18 @@ def test_convex_hull(repeat_pts):
assert b == r, "Polygon boundary has correct points"


def test_math_util_angle_domain():
def test_gca_angle_domain():
assert not np.isfinite(
great_circle_arc.angle([[0, 0, 0]], [[0, 0, 0]], [[0, 0, 0]])[0]
)


def test_math_util_length_domain():
def test_gca_length_domain():
with pytest.raises(ValueError):
great_circle_arc.length([[np.nan, 0, 0]], [[0, 0, np.inf]])


def test_math_util_angle_nearly_coplanar_vec():
def test_gca_angle_nearly_coplanar_vec():
# test from issue #222 + extra values
vectors = [
5 * [[1.0, 1.0, 1.0]],
Expand All @@ -559,8 +554,9 @@ def test_inner1d():
assert_allclose(lengths, [3.0, 1.0, 2.25, 2.0225, 2.01], rtol=0, atol=1e-15)


@pytest.mark.skipif(math_util is None, reason="math_util C-ext is missing")
@pytest.mark.skipif(not HAS_C_UFUNCS, reason="math_util C-ext is missing")
def test_math_util_inner1d():
from spherical_geometry import math_util
vectors = [
[1.0, 1.0, 1.0],
3 * [1.0 / np.sqrt(3)],
Expand Down
7 changes: 2 additions & 5 deletions spherical_geometry/vector.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,7 @@
# THIRD-PARTY
import numpy as np

try:
from . import math_util
HAS_C_UFUNCS = True
except ImportError:
HAS_C_UFUNCS = False
from spherical_geometry import HAS_C_UFUNCS

__all__ = ['two_d', 'lonlat_to_vector', 'vector_to_lonlat',
'normalize_vector', 'radec_to_vector', 'vector_to_radec',
Expand Down Expand Up @@ -149,6 +145,7 @@ def normalize_vector(xyz, output=None):
output = np.empty(xyz.shape, dtype=np.float64)

if HAS_C_UFUNCS:
from spherical_geometry import math_util
math_util.normalize(xyz, output)
return output

Expand Down
4 changes: 3 additions & 1 deletion tox.ini
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tox]
envlist =
py{39,310,311,312}-test{,-devdeps}{,-cov}
py{39,310,311,312}-test{,-disablecufuncs}{,-devdeps}{,-cov}
codestyle
twine
bandit
Expand All @@ -11,6 +11,8 @@ passenv = HOME,WINDIR,CC,CI

setenv =
devdeps: PIP_EXTRA_INDEX_URL = https://pypi.anaconda.org/astropy/simple https://pypi.anaconda.org/scientific-python-nightly-wheels/simple https://pypi.anaconda.org/liberfa/simple
disablecufuncs: DISABLE_SPHR_GEOM_C_UFUNCS=true
!disablecufuncs: DISABLE_SPHR_GEOM_C_UFUNCS=false

# Run the tests in a temporary directory to make sure that we don't import
# package from the source tree
Expand Down

0 comments on commit eb3c6eb

Please sign in to comment.