Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TVB-2719 Add 100% test coverage #58

Merged
merged 6 commits into from
Aug 3, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions .coveragerc
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[run]
plugins = Cython.Coverage
6 changes: 5 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,10 @@ __pycache__/

# Distribution / packaging
dist/
*.egg-info

# coverage file
.coverage
.coverage*

# PyCharm generated configuration
.idea/
Expand All @@ -30,3 +31,6 @@ env
# Shared object files
*.so
*.pyd

# generated file during compilation
gdist.cpp
10 changes: 7 additions & 3 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,20 @@ notifications:

install:
- pip3 install .
# - python setup.py build_ext --inplace
script:
- pip3 install pytest~=3.6.1
- pytest
- pip3 install pytest
- if [ "${TRAVIS_OS_NAME}" = "linux" ]; then pytest --cov=gdist; else pytest; fi

jobs:
include:
- name: "Python 3.8 on Xenial Linux"
language: python
os: linux
python: 3.8
env: COVERAGE=1
before_install: pip3 install pytest-cov codecov
before_script: python3 setup.py clean --all build_ext --force --inplace
after_success: codecov
- name: "Python 3.8.0 on Windows"
os: windows
language: shell
Expand Down
121 changes: 121 additions & 0 deletions data/flat_triangular_mesh_no_target.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
0.000000000000000000e+00
1.999999999999999556e-01
3.999999999999999667e-01
5.999999999999998668e-01
7.999999999999998224e-01
9.999999999999997780e-01
1.199999999999999734e+00
1.399999999999999689e+00
1.599999999999999645e+00
1.799999999999999378e+00
1.999999999999999334e+00
1.999999999999999001e-01
2.828427124746188404e-01
4.472135954999578167e-01
6.324555320336756603e-01
8.246211251235316952e-01
1.019803902718556365e+00
1.216552506059643601e+00
1.414213562373094479e+00
1.612451549659709160e+00
1.811077027627482572e+00
2.009975124224177456e+00
3.999999999999998002e-01
4.472135954999576501e-01
5.656854249492377917e-01
7.211102550927973587e-01
8.944271909999156334e-01
1.077032961426900526e+00
1.264911064067351321e+00
1.456021977856102989e+00
1.649242250247063168e+00
1.843908891458576171e+00
2.039607805437112731e+00
5.999999999999999778e-01
6.324555320336757713e-01
7.211102550927979138e-01
8.485281374238569096e-01
9.999999999999996669e-01
1.166190378969059749e+00
1.341640786499873395e+00
1.523154621172781553e+00
1.708800749063505764e+00
1.897366596101026870e+00
2.088061301782109247e+00
7.999999999999997113e-01
8.246211251235318063e-01
8.944271909999155223e-01
9.999999999999997780e-01
1.131370849898475361e+00
1.280624847486569129e+00
1.442220510185595161e+00
1.612451549659709160e+00
1.788854381999831267e+00
1.969771560359220519e+00
2.154065922853801052e+00
1.000000000000000000e+00
1.019803902718556810e+00
1.077032961426900526e+00
1.166190378969059749e+00
1.280624847486569795e+00
1.414213562373094701e+00
1.562049935181330396e+00
1.720465053408524847e+00
1.886796226411320054e+00
2.059126028197399538e+00
2.236067977499788917e+00
1.200000000000000178e+00
1.216552506059644045e+00
1.264911064067351321e+00
1.341640786499873839e+00
1.442220510185595383e+00
1.562049935181331062e+00
1.697056274847713597e+00
1.843908891458577060e+00
1.999999999999999556e+00
2.163330765278392853e+00
2.332380757938119498e+00
1.399999999999999911e+00
1.414213562373094923e+00
1.456021977856103433e+00
1.523154621172781553e+00
1.612451549659709160e+00
1.720465053408524847e+00
1.843908891458577282e+00
1.979898987322332271e+00
2.126029162546929197e+00
2.280350850198275214e+00
2.441311123146739703e+00
1.600000000000000089e+00
1.612451549659709826e+00
1.649242250247064057e+00
1.708800749063505986e+00
1.788854381999831267e+00
1.886796226411320054e+00
1.999999999999999778e+00
2.126029162546929641e+00
2.262741699796951611e+00
2.408318915758458356e+00
2.561249694973138702e+00
1.799999999999999600e+00
1.811077027627482794e+00
1.843908891458577060e+00
1.897366596101026648e+00
1.969771560359220741e+00
2.059126028197399094e+00
2.163330765278392853e+00
2.280350850198275214e+00
2.408318915758458356e+00
2.545584412271570063e+00
2.690724809414740903e+00
1.999999999999999778e+00
2.009975124224177456e+00
2.039607805437113619e+00
2.088061301782109247e+00
2.154065922853801052e+00
2.236067977499788917e+00
2.332380757938119498e+00
2.441311123146739703e+00
2.561249694973138702e+00
2.690724809414741792e+00
2.828427124746188959e+00
5 changes: 2 additions & 3 deletions gdist.pyx
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
# -*- coding: utf-8 -*-
# cython: language_level=3
#
#
# TheVirtualBrain-Framework Package. This package holds all Data Management, and
Expand Down Expand Up @@ -171,10 +170,10 @@ def compute_gdist(numpy.ndarray[numpy.float64_t, ndim=2] vertices,
cdef vector[unsigned] faces

if source_indices is None:
source_indices = numpy.arange(0, dtype=numpy.int32) # default to 0
source_indices = numpy.arange(1, dtype=numpy.int32) # default to 0
if target_indices is None:
propagate_on_max_distance = True
target_indices = numpy.arange(vertices.size(), dtype=numpy.int32)
target_indices = numpy.arange(vertices.shape[0], dtype=numpy.int32)

for k in vertices.flatten():
points.push_back(k)
Expand Down
40 changes: 31 additions & 9 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,24 @@
"""

import os
import numpy
import shutil
import setuptools

import numpy
from Cython.Distutils import build_ext
from Cython.Build.Dependencies import cythonize


compiler_directives = {
'language_level': 3,
}

# Disable assertions; one is failing geodesic_mesh.h:405
define_macros = [('NDEBUG', 1)]

if 'COVERAGE' in os.environ:
compiler_directives['linetrace'] = True
define_macros.append(("CYTHON_TRACE_NOGIL", "1"))

GEODESIC_NAME = "gdist"

Expand All @@ -53,8 +67,7 @@
name=GEODESIC_NAME, # Name of extension
sources=["gdist.pyx"], # Filename of Cython source
language="c++", # Cython create C++ source
# Disable assertions; one is failing geodesic_mesh.h:405
define_macros=[('NDEBUG', 1)],
define_macros=define_macros,
extra_compile_args=['--std=c++14'],
extra_link_args=['--std=c++14'],
include_dirs=[numpy.get_include(), "geodesic_library"],
Expand All @@ -73,12 +86,26 @@
with open(os.path.join(os.path.dirname(__file__), 'README.rst')) as fd:
DESCRIPTION = fd.read()

class new_build_ext(build_ext):
def finalize_options(self):
self.distribution.ext_modules = cythonize(
self.distribution.ext_modules,
compiler_directives=compiler_directives,
annotate=False,
)
if not self.include_dirs:
self.include_dirs = []
elif isinstance(self.include_dirs, str):
self.include_dirs = [self.include_dirs]
self.include_dirs.append(numpy.get_include())
super().finalize_options()

setuptools.setup(
name="tvb-" + GEODESIC_NAME,
version='2.1.0',
ext_modules=GEODESIC_MODULE,
include_dirs=INCLUDE_DIRS,
cmdclass={'build_ext': build_ext},
cmdclass={"build_ext": new_build_ext},
install_requires=INSTALL_REQUIREMENTS,
description="Compute geodesic distances",
long_description=DESCRIPTION,
Expand All @@ -88,8 +115,3 @@
url='https://github.com/the-virtual-brain/tvb-gdist',
keywords="gdist geodesic distance geo tvb"
)

shutil.rmtree('tvb_gdist.egg-info', True)
if os.path.exists(GEODESIC_NAME + '.cpp'):
os.remove(GEODESIC_NAME + '.cpp')
shutil.rmtree('build', True)
15 changes: 15 additions & 0 deletions tests/test_gdist.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,21 @@ def test_flat_triangular_mesh_1_indexed(self):
)
np.testing.assert_array_almost_equal(distance, [0.2])

def test_flat_triangular_mesh_no_target(self):
data = np.loadtxt("data/flat_triangular_mesh.txt", skiprows=1)
vertices = data[0:121].astype(np.float64)
triangles = data[121:].astype(np.int32)
source = None
target = None
distance = gdist.compute_gdist(
vertices,
triangles,
source,
target,
)
expected = np.loadtxt("data/flat_triangular_mesh_no_target.txt")
np.testing.assert_array_almost_equal(distance, expected)

def test_hedgehog_mesh(self):
data = np.loadtxt("data/hedgehog_mesh.txt", skiprows=1)
vertices = data[0:300].astype(np.float64)
Expand Down