Skip to content

Commit

Permalink
Issue #28 added note re smoothing in docs and a test to check it's wo…
Browse files Browse the repository at this point in the history
…rking
  • Loading branch information
thompson318 committed Oct 4, 2023
1 parent 222ec6d commit 98ace2c
Show file tree
Hide file tree
Showing 3 changed files with 162 additions and 9 deletions.
5 changes: 4 additions & 1 deletion sksurgerynditracker/nditracker.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,10 @@ def __init__(self, configuration):
ports to probe:
use quaternions:
use quaternions: default is false
smoothing buffer: specify a buffer over which to average the
tracking, defaults to 1
:raises Exception: IOError, KeyError, OSError
"""
Expand Down
15 changes: 7 additions & 8 deletions tests/polaris_mocks.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,13 @@
"data/8700339.rom"]
}

SETTINGS_POLARIS_QUAT = {
"tracker type": "polaris",
"ports to probe": 20,
"romfiles" : [
"data/something_else.rom",
"data/8700339.rom"],
"use quaternions": "true"
}
SETTINGS_POLARIS_QUAT = SETTINGS_POLARIS.copy()
SETTINGS_POLARIS_QUAT["use quaternions"] = "true"

SETTINGS_POLARIS_SMOOTH = SETTINGS_POLARIS.copy()
SETTINGS_POLARIS_QUAT_SMOOTH = SETTINGS_POLARIS_QUAT.copy()
SETTINGS_POLARIS_SMOOTH["smoothing buffer"] = 2
SETTINGS_POLARIS_QUAT_SMOOTH["smoothing buffer"] = 2

class MockPort:
"""A fake serial port for ndi"""
Expand Down
151 changes: 151 additions & 0 deletions tests/test_sksurgerynditracker_mockndi_getframe.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from sksurgerynditracker.nditracker import NDITracker

from tests.polaris_mocks import SETTINGS_POLARIS, SETTINGS_POLARIS_QUAT, \
SETTINGS_POLARIS_SMOOTH, SETTINGS_POLARIS_QUAT_SMOOTH, \
mockndiProbe, \
mockndiOpen, mockndiGetError, mockComports, \
mockndiGetPHSRHandle, mockndiVER, \
Expand Down Expand Up @@ -160,3 +161,153 @@ def test_getframe_missing(mocker):
assert np.all(np.isnan(tracking_quality))

del tracker

def test_getframe_smooth_mock(mocker):
"""
connects and configures, mocks ndicapy.ndiProbe to pass
reqs: 03, 04
"""
tracker = None
bxsource = MockBXFrameSource()
ndidevice = MockNDIDevice()
mocker.patch('serial.tools.list_ports.comports', mockComports)
mocker.patch('ndicapy.ndiProbe', mockndiProbe)
mocker.patch('ndicapy.ndiOpen', mockndiOpen)
mocker.patch('ndicapy.ndiCommand', ndidevice.mockndiCommand)
mocker.patch('ndicapy.ndiGetError', mockndiGetError)
mocker.patch('ndicapy.ndiClose')
mocker.patch('ndicapy.ndiGetPHSRNumberOfHandles',
ndidevice.mockndiGetPHSRNumberOfHandles)
mocker.patch('ndicapy.ndiGetPHRQHandle', ndidevice.mockndiGetPHRQHandle)
mocker.patch('ndicapy.ndiPVWRFromFile')
mocker.patch('ndicapy.ndiGetPHSRHandle', mockndiGetPHSRHandle)
mocker.patch('ndicapy.ndiVER', mockndiVER)
mocker.patch('ndicapy.ndiGetBXFrame', bxsource.mockndiGetBXFrame)
mocker.patch('ndicapy.ndiGetBXTransform', bxsource.mockndiGetBXTransform)

tracker = NDITracker(SETTINGS_POLARIS_SMOOTH)

bxsource.setdevice(ndidevice)

(port_handles, time_stamps, frame_numbers, tracking,
tracking_quality ) = tracker.get_frame()

assert len(port_handles) == 2
assert len(time_stamps) == 2
assert frame_numbers.count(1) == 2
expected_tracking_0 = np.array([[1.,0.,0.,10.],
[0.,1.,0.,-20.],
[0.,0.,1.,5.],
[0.,0.,0.,1.]])
assert np.array_equal(expected_tracking_0, tracking[0])
expected_tracking_1 = np.array([[1.,0.,0.,0.],
[0.,1.,0.,0.],
[0.,0.,1.,0.],
[0.,0.,0.,1.]])
assert np.array_equal(expected_tracking_1, tracking[1])
assert tracking_quality.count(1.) == 2

(port_handles, time_stamps, frame_numbers, tracking,
tracking_quality ) = tracker.get_frame()

assert len(port_handles) == 2
assert len(time_stamps) == 2
assert frame_numbers.count(2) == 2
expected_tracking_0 = np.array([[1.,0.,0.,15.],
[0.,1.,0.,-30.],
[0.,0.,1.,7.5],
[0.,0.,0.,1.]])
assert np.array_equal(expected_tracking_0, tracking[0])
assert np.array_equal(expected_tracking_1, tracking[1])
assert tracking_quality.count(1.) == 2

del tracker

def test_getframe_smooth_mock_quat(mocker):
"""
Checks that get frame works with quaternions
"""
tracker = None
bxsource = MockBXFrameSource()
ndidevice = MockNDIDevice()
mocker.patch('serial.tools.list_ports.comports', mockComports)
mocker.patch('ndicapy.ndiProbe', mockndiProbe)
mocker.patch('ndicapy.ndiOpen', mockndiOpen)
mocker.patch('ndicapy.ndiCommand', ndidevice.mockndiCommand)
mocker.patch('ndicapy.ndiGetError', mockndiGetError)
mocker.patch('ndicapy.ndiClose')
mocker.patch('ndicapy.ndiGetPHSRNumberOfHandles',
ndidevice.mockndiGetPHSRNumberOfHandles)
mocker.patch('ndicapy.ndiGetPHRQHandle', ndidevice.mockndiGetPHRQHandle)
mocker.patch('ndicapy.ndiPVWRFromFile')
mocker.patch('ndicapy.ndiGetPHSRHandle', mockndiGetPHSRHandle)
mocker.patch('ndicapy.ndiVER', mockndiVER)
mocker.patch('ndicapy.ndiGetBXFrame', bxsource.mockndiGetBXFrame)
mocker.patch('ndicapy.ndiGetBXTransform', bxsource.mockndiGetBXTransform)

tracker = NDITracker(SETTINGS_POLARIS_QUAT_SMOOTH)

bxsource.setdevice(ndidevice)

(port_handles, time_stamps, frame_numbers, tracking,
tracking_quality ) = tracker.get_frame()

assert len(port_handles) == 2
assert len(time_stamps) == 2
assert frame_numbers.count(1) == 2
expected_tracking_0 = np.array([[1.,0.,0.,0.,10.,-20,5.]])
assert np.array_equal(expected_tracking_0, tracking[0])
expected_tracking_1 = np.array([[1.,0.,0.,0.,0.,0.,0.]])
assert np.array_equal(expected_tracking_1, tracking[1])
assert tracking_quality.count(1.) == 2

(port_handles, time_stamps, frame_numbers, tracking,
tracking_quality ) = tracker.get_frame()

assert len(port_handles) == 2
assert len(time_stamps) == 2
assert frame_numbers.count(2) == 2
expected_tracking_0 = np.array([[1.,0.,0.,0.,15.,-30,7.5]])
assert np.array_equal(expected_tracking_0, tracking[0])
assert np.array_equal(expected_tracking_1, tracking[1])
assert tracking_quality.count(1.) == 2

del tracker

def test_getframe_missing_smooth(mocker):
"""
connects and configures, mocks ndicapy.ndiProbe to pass
reqs: 03, 04
"""
tracker = None
bxsource = MockBXFrameSource()
ndidevice = MockNDIDevice()
mocker.patch('serial.tools.list_ports.comports', mockComports)
mocker.patch('ndicapy.ndiProbe', mockndiProbe)
mocker.patch('ndicapy.ndiOpen', mockndiOpen)
mocker.patch('ndicapy.ndiCommand', ndidevice.mockndiCommand)
mocker.patch('ndicapy.ndiGetError', mockndiGetError)
mocker.patch('ndicapy.ndiClose')
mocker.patch('ndicapy.ndiGetPHSRNumberOfHandles',
ndidevice.mockndiGetPHSRNumberOfHandles)
mocker.patch('ndicapy.ndiGetPHRQHandle', ndidevice.mockndiGetPHRQHandle)
mocker.patch('ndicapy.ndiPVWRFromFile')
mocker.patch('ndicapy.ndiGetPHSRHandle', mockndiGetPHSRHandle)
mocker.patch('ndicapy.ndiVER', mockndiVER)
mocker.patch('ndicapy.ndiGetBXFrame', bxsource.mockndiGetBXFrame)
mocker.patch('ndicapy.ndiGetBXTransform',
bxsource.mockndiGetBXTransformMissing)

tracker = NDITracker(SETTINGS_POLARIS_SMOOTH)

bxsource.setdevice(ndidevice)
(port_handles, time_stamps, frame_numbers, tracking,
tracking_quality ) = tracker.get_frame()

assert len(port_handles) == 2
assert len(time_stamps) == 2
assert frame_numbers.count(1) == 2
assert np.all(np.isnan(tracking))
assert np.all(np.isnan(tracking_quality))

del tracker

0 comments on commit 98ace2c

Please sign in to comment.