Skip to content

Commit

Permalink
Merge pull request #73 from UCL/72-delegate_compare_matrices
Browse files Browse the repository at this point in the history
Issue #72 used compare matrices from aruco
  • Loading branch information
thompson318 authored Jul 6, 2021
2 parents ae5d869 + c7d0a3e commit 2a10bf0
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 57 deletions.
63 changes: 63 additions & 0 deletions .codeclimate.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
version: "2" # required to adjust maintainability checks

checks:
argument-count:
enabled: true
config:
threshold: 4
complex-logic:
enabled: true
config:
threshold: 4
file-lines:
enabled: true
config:
threshold: 250
method-complexity:
enabled: true
config:
threshold: 5
method-count:
enabled: true
config:
threshold: 20
method-lines:
enabled: true
config:
threshold: 25
nested-control-flow:
enabled: true
config:
threshold: 4
return-statements:
enabled: true
config:
threshold: 4
similar-code:
enabled: true
config:
threshold: #language-specific defaults. overrides affect all languages.
identical-code:
enabled: true
config:
threshold: #language-specific defaults. overrides affect all languages.

# plugins:
# eslint:
# enabled: true
# channel: "eslint-6"
# rubocop:
# enabled: true
# channel: "rubocop-0-79"

exclude_patterns:
- "config/"
- "db/"
- "dist/"
- "features/"
- "**/test/"
- "**/tests/"
- "sksurgerybard/_version.py"
- "versioneer.py"


2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ scikit-surgerycalibration>=0.1.9
scikit-surgerycore>=0.6.8
scikit-surgeryutils
scikit-surgeryvtk
scikit-surgeryarucotracker>=0.2.4
scikit-surgeryarucotracker>=0.2.5
opencv-contrib-python
#scikit-surgeryspeech>=0.2.0 #uncomment this to use speech interface
#PocketSphinx # uncomment this to use speech with sphinx
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@
'scikit-surgerycalibration>=0.1.9',
'scikit-surgeryutils',
'scikit-surgeryvtk',
'scikit-surgeryarucotracker>=0.2.4',
'scikit-surgeryarucotracker>=0.2.5',
],

entry_points={
Expand Down
2 changes: 1 addition & 1 deletion tests/algorithms/test_pointer.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import pytest
import numpy as np
from sksurgerycore.transforms.transform_manager import TransformManager
import sksurgerybard.algorithms.pointer as pointer
from sksurgerybard.algorithms import pointer


def test_write_pointer_invalid_tm():
Expand Down
63 changes: 9 additions & 54 deletions tests/widgets/test_bard_overlay.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
import copy
import numpy as np
import pytest
from sksurgeryarucotracker.algorithms.compare_matrices \
import matrices_equivalent
import sksurgerybard.widgets.bard_overlay_app as boa


Expand Down Expand Up @@ -41,53 +43,6 @@

}

def _matrices_equivalent(mat_a, mat_b, cube_length = 500.0, tolerance = 5.0):
"""
Helper function to check the equivalence of 2 matrices.
Because the 3x3 rotation and the 1X3 translation can
have very different magnitudes we want something
a bit more nuances than just np.allclose for the
whole matrix.
np.allclose worked OK until opencv-contrib-python > 4.5.1
see Issue #69
Rather than comparing matrices let's look at what happens when we
apply them to point cooridinates. If we define an operating cube,
with the camera at the centre of the plane defined by the
x and y axes and z = 0, by testing the differences at the
extremities of that cube we can say that BARD is accurate to
within x mm over a volume of side length x mm. By default 5.0 mm and
500 mm.
This isn't very accurate but mainly seems to be due to
instability in opencv-contrib-python. If you wanted more
accuracy for a given application you could version
lock opencv-contrib-python
"""
cube_points = np.array(
[[-cube_length/2.0, -cube_length/2.0, cube_length, 1.],
[-cube_length/2.0, cube_length/2.0, cube_length, 1.],
[cube_length/2.0, cube_length/2.0, cube_length, 1.],
[cube_length/2.0, -cube_length/2.0, cube_length, 1.],
[-cube_length/2.0, -cube_length/2.0, 0., 1.],
[-cube_length/2.0, cube_length/2.0, 0., 1.],
[cube_length/2.0, cube_length/2.0, 0., 1.],
[cube_length/2.0, -cube_length/2.0, 0., 1.]],
dtype = np.float32)

trans_a = np.matmul(mat_a, cube_points.transpose())
trans_b = np.matmul(mat_b, cube_points.transpose())

equivalent = np.allclose(trans_a, trans_b, rtol = 0.0, atol = tolerance)

if not equivalent:
print ("trans a: " , trans_a.transpose())
print ("trans b: " , trans_b.transpose())
print ("diff: " , trans_a.transpose() - trans_b.transpose())

return equivalent


def test_valid_config():
"""
Loads a valid config file, and checks that we have retrieved the calibration
Expand Down Expand Up @@ -117,7 +72,7 @@ def test_valid_config():
[ 0.0000e+00, 0.00000e+00, 0.00000e+00, 1.00000000e+00]],
dtype=np.float32)

assert _matrices_equivalent(cam2model_regression,
assert matrices_equivalent(cam2model_regression,
bard_overlay.transform_manager.get("modelreference2camera"))

pointer2camera_regression = np.array([
Expand All @@ -127,7 +82,7 @@ def test_valid_config():
[ 0.00000000e+00, 0.00000000e+00, 0.00000000e+00, 1.00000000e+00]],
dtype=np.float32)

assert _matrices_equivalent(pointer2camera_regression,
assert matrices_equivalent(pointer2camera_regression,
bard_overlay.transform_manager.get("pointerref2camera"))

pointer2model_regression = np.array([
Expand All @@ -137,7 +92,7 @@ def test_valid_config():
[ 0.00000000e+00, 0.00000000e+00, 0.00000000e+00, 1.00000000e+00]],
dtype=np.float32)

assert _matrices_equivalent(pointer2model_regression,
assert matrices_equivalent(pointer2model_regression,
bard_overlay.transform_manager.get("pointerref2modelreference"))

#skip 5 frames
Expand All @@ -151,7 +106,7 @@ def test_valid_config():
[ 0.00000000e+00, 0.00000000e+00, 0.00000000e+00, 1.00000000e+00]],
dtype=np.float32)

assert _matrices_equivalent(cam2model_regression,
assert matrices_equivalent(cam2model_regression,
bard_overlay.transform_manager.get("modelreference2camera"))

pointer2camera_regression = np.array([
Expand All @@ -161,7 +116,7 @@ def test_valid_config():
[ 0.00000000e+00, 0.00000000e+00, 0.00000000e+00, 1.00000000e+00]],
dtype=np.float32)

assert _matrices_equivalent(pointer2camera_regression,
assert matrices_equivalent(pointer2camera_regression,
bard_overlay.transform_manager.get("pointerref2camera"))

pointer2model_regression = np.array([
Expand All @@ -171,7 +126,7 @@ def test_valid_config():
[0.00000000e+00, 0.00000000e+00, 0.00000000e+00, 1.00000000e+00]],
dtype=np.float32)

assert _matrices_equivalent(pointer2model_regression,
assert matrices_equivalent(pointer2model_regression,
bard_overlay.transform_manager.get("pointerref2modelreference"))


Expand Down Expand Up @@ -217,7 +172,7 @@ def test_with_no_pointer():
[ 0.0000e+00, 0.00000e+00, 0.00000e+00, 1.00000000e+00]],
dtype=np.float32)

assert _matrices_equivalent(cam2model_regression,
assert matrices_equivalent(cam2model_regression,
bard_overlay.transform_manager.get("modelreference2camera"))

with pytest.raises(ValueError):
Expand Down

0 comments on commit 2a10bf0

Please sign in to comment.