Commit 7a083741 authored by Matt Clarkson's avatar Matt Clarkson

Issue #6: Add validation of camera, rotation and translation matrices from scikit-surgeryimage.

parent c12d2038
# coding=utf-8
"""
Various validation routines for checking matrices.
"""
import numpy as np
def validate_camera_matrix(matrix):
"""
Validates that a matrix is a camera (intrinsic) matrix.
1. Is a numpy array
2. Is 2D
3. Has 3 rows
4. Has 3 columns
:param matrix: camera matrix
:raises: TypeError, ValueError if it fails
:returns: True
"""
if not isinstance(matrix, np.ndarray):
raise TypeError("Camera matrix is not a numpy ndarray.")
if len(matrix.shape) != 2:
raise ValueError("Camera matrix should have 2 dimensions.")
if matrix.shape[0] != 3:
raise ValueError("Camera matrix should have 3 rows.")
if matrix.shape[1] != 3:
raise ValueError("Camera matrix should have 3 columns.")
return True
# pylint: disable=invalid-name
def validate_distortion_coefficients(matrix):
"""
Validates that a matrix is a set of OpenCV style distortion coefficients.
1. Is a numpy array
2. Is 2D
3. Has 1 row
4. Has either 4, 5, 8, 12 or 14 columns
:param matrix: set of distortion coefficients
:raises: TypeError, ValueError if if fails
:returns: True
"""
if not isinstance(matrix, np.ndarray):
raise TypeError("Distortion coefficients are not a numpy ndarray.")
if len(matrix.shape) != 2:
raise ValueError("Camera matrix should have 2 dimensions.")
if matrix.shape[0] != 1:
raise ValueError("Distortion coefficients should have 1 row.")
if matrix.shape[1] not in [4, 5, 8, 12, 14]: # See OpenCV docs
raise ValueError("Distortion coefficients should have "
+ "4, 5, 8, 12 or 14 columns.")
return True
def validate_rotation_matrix(matrix):
"""
Validates that a matrix is rotation matrix.
1. Is a numpy array
2. Is 2D
3. Has 3 rows
4. Has 3 columns
:param matrix: rotation matrix
:raises: TypeError, ValueError if it fails
:returns: True
"""
if not isinstance(matrix, np.ndarray):
raise TypeError("Rotation matrix should be a numpy ndarray.")
if len(matrix.shape) != 2:
raise ValueError("Rotation matrix should have 2 dimensions.")
if matrix.shape[0] != 3:
raise ValueError("Rotation matrix should have 3 rows.")
if matrix.shape[1] != 3:
raise ValueError("Rotation matrix should have 3 columns.")
# pylint: disable=invalid-name
def validate_translation_column_vector(matrix):
"""
Validates that a translation matrix is a column vector.
1. Is numpy array
2. Is 2D
3. Has 3 rows
4. Has 1 column
:param matrix: translation matrix
:raises: TypeError, ValueError if it fails
:return: True
"""
if not isinstance(matrix, np.ndarray):
raise TypeError("Translation matrix should be a numpy ndarray.")
if len(matrix.shape) != 2:
raise ValueError("Translation matrix should have 2 dimensions.")
if matrix.shape[0] != 3:
raise ValueError("Translation matrix should have 3 rows.")
if matrix.shape[1] != 1:
raise ValueError("Translation matrix should have 1 column.")
# coding=utf-8
"""
Tests for validate_matrix.py
"""
import pytest
import numpy as np
import sksurgerycore.utilities.validate_matrix as vm
def test_camera_matrix_invalid_because_wrong_type():
with pytest.raises(TypeError):
vm.validate_camera_matrix(1)
def test_camera_matrix_invalid_because_not_two_dimensional():
with pytest.raises(ValueError):
vm.validate_camera_matrix(np.ones((3, 3, 3)))
def test_camera_matrix_invalid_because_too_few_rows():
with pytest.raises(ValueError):
vm.validate_camera_matrix(np.ones((1, 3)))
def test_camera_matrix_invalid_because_too_many_rows():
with pytest.raises(ValueError):
vm.validate_camera_matrix(np.ones((4, 3)))
def test_camera_matrix_invalid_because_too_few_columns():
with pytest.raises(ValueError):
vm.validate_camera_matrix(np.ones((3, 1)))
def test_camera_matrix_invalid_because_too_many_columns():
with pytest.raises(ValueError):
vm.validate_camera_matrix(np.ones((3, 4)))
def test_distortion_coefficients_invalid_because_wrong_type():
with pytest.raises(TypeError):
vm.validate_distortion_coefficients(1)
def test_distortion_coefficients_invalid_because_not_two_dimensional():
with pytest.raises(ValueError):
vm.validate_distortion_coefficients(np.ones((3, 3, 3)))
def test_distortion_coefficients_invalid_because_too_many_rows():
with pytest.raises(ValueError):
vm.validate_distortion_coefficients(np.ones((3, 4)))
def test_distortion_coefficients_invalid_because_too_few_columns():
with pytest.raises(ValueError):
vm.validate_distortion_coefficients(np.ones((1, 3)))
def test_distortion_coefficients_invalid_because_number_of_columns_not_in_list():
with pytest.raises(ValueError):
# Should accept [4, 5, 8, 12, 14]
vm.validate_distortion_coefficients(np.ones((1, 6)))
def test_rotation_matrix_invalid_because_wrong_type():
with pytest.raises(TypeError):
vm.validate_rotation_matrix(1)
def test_rotation_matrix_invalid_because_not_two_dimensional():
with pytest.raises(ValueError):
vm.validate_rotation_matrix(np.ones((3, 3, 3)))
def test_rotation_matrix_invalid_because_too_many_rows():
with pytest.raises(ValueError):
vm.validate_rotation_matrix(np.ones((4, 3)))
def test_rotation_matrix_invalid_because_too_many_columns():
with pytest.raises(ValueError):
vm.validate_rotation_matrix(np.ones((3, 4)))
def test_rotation_matrix_invalid_because_too_few_rows():
with pytest.raises(ValueError):
vm.validate_rotation_matrix(np.ones((2, 3)))
def test_rotation_matrix_invalid_because_too_few_columns():
with pytest.raises(ValueError):
vm.validate_rotation_matrix(np.ones((3, 2)))
def test_translation_matrix_invalid_because_wrong_type():
with pytest.raises(TypeError):
vm.validate_translation_column_vector(1)
def test_translation_matrix_invalid_because_not_two_dimensional():
with pytest.raises(ValueError):
vm.validate_translation_column_vector(np.ones((3, 3, 3)))
def test_translation_matrix_invalid_because_too_many_rows():
with pytest.raises(ValueError):
vm.validate_translation_column_vector(np.ones((4, 1)))
def test_translation_matrix_invalid_because_too_many_columns():
with pytest.raises(ValueError):
vm.validate_translation_column_vector(np.ones((3, 4)))
def test_translation_matrix_invalid_because_too_few_rows():
with pytest.raises(ValueError):
vm.validate_translation_column_vector(np.ones((2, 1)))
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment