Commit 330b17a5 authored by Matt Clarkson's avatar Matt Clarkson

Merge branch '6-merge-utilities-from-scikit-surgeryimage'

parents c12d2038 6be4c2ff
......@@ -15,7 +15,7 @@ Design principles:
import json
import copy
import sksurgerycore.utilities.file_utilities as f
import sksurgerycore.utilities.validate_file as f
class ConfigurationManager:
......
# coding=utf-8
"""
Various validation functions.
"""
def validate_is_string_or_number(var):
"""
Checks if the input is either a str, int or float.
:param var: input
:raises: TypeError if not
:returns: True
"""
valid_types = (str, int, float)
if not isinstance(var, valid_types):
raise TypeError("Input is not a string or a number:" + str(var))
return True
def validate_width_height(dims):
"""
Checks if dims (width, height) is a valid specification,
meaning width and height are both integers above zero.
:param dims: (width, height)
:raises: TypeError, ValueError
:returns: True
"""
if dims is None:
raise ValueError("Null input for (width, height).")
width, height = dims
if not isinstance(width, int):
raise TypeError("Width should be an integer.")
if not isinstance(height, int):
raise TypeError("Height should be an integer.")
if width < 1:
raise ValueError("Width should be >= 1.")
if height < 1:
raise ValueError("Height should be >= 1.")
return True
......@@ -11,6 +11,10 @@ import os
def validate_is_file(file_name):
"""
Check if file_name is a file.
:param file_name: string containing path name
:raises: ValueError if not
:returns: True
"""
if not file_name:
raise ValueError("Empty parameter file_name")
......@@ -24,6 +28,10 @@ def validate_is_file(file_name):
def validate_is_writable_file(file_name):
"""
Check if file_name is a writable file.
:param file_name: string containing path name
:raises: ValueError if not
:returns: True
"""
validate_is_file(file_name)
......
# 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 not
: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 not
: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 not
: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.")
return True
# 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 not
: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.")
return True
# coding=utf-8
"""
Tests for validate.py
"""
import pytest
from sksurgerycore.utilities import validate as v
def test_is_string_or_number():
string = "test"
integer = 1
fp = 1.01
list_invalid = [1, 2, 3]
assert v.validate_is_string_or_number(string)
assert v.validate_is_string_or_number(integer)
assert v.validate_is_string_or_number(fp)
with pytest.raises(TypeError):
v.validate_is_string_or_number(list_invalid)
def test_width_height_invalid_because_none_input():
with pytest.raises(ValueError):
v.validate_width_height(None)
def test_width_height_invalid_because_empty_input():
with pytest.raises(ValueError):
v.validate_width_height(())
def test_width_height_invalid_because_width_wrong_type():
with pytest.raises(TypeError):
v.validate_width_height(("", 2))
def test_width_height_invalid_because_width_too_low():
with pytest.raises(ValueError):
v.validate_width_height((0, 2))
def test_width_height_invalid_because_height_wrong_type():
with pytest.raises(TypeError):
v.validate_width_height((1, ""))
def test_width_height_invalid_because_height_too_low():
with pytest.raises(ValueError):
v.validate_width_height((1, 0))
......@@ -5,7 +5,7 @@ Tests for file_utilities.py
"""
import pytest
from sksurgerycore.utilities import file_utilities as f
from sksurgerycore.utilities import validate_file as f
def test_invalid_file_name_because_non_existent():
......
# 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