Page MenuHomec4science

test_matrix.py
No OneTemporary

File Metadata

Created
Sun, May 5, 14:30

test_matrix.py

import unittest
import lxml.etree as ET
from os import sep, path
from os.path import isdir, dirname
import sys
sys.path.append('svgscripts')
from datatypes.matrix import Matrix
from datatypes.transkriptionField import TranskriptionField
class FakeTF:
def __init__(self):
self.xmin = 297.6379999999997
self.xmax = 765.354
self.ymin = 157.328
self.ymax = 752.6040160033832
class TestMatrix(unittest.TestCase):
def setUp(self):
self.x = 219.4058
self.y = 106.4634
self.matrix_string = 'matrix(1 0 0 1 {} {})'.format(str(self.x), str(self.y))
self.test_data_dir = dirname(__file__) + sep + 'test_data'
if not isdir(self.test_data_dir):
self.test_data_dir = dirname(dirname(__file__)) + sep + 'test_data'
self.test_file = self.test_data_dir + sep + 'test_ai.svg'
self.rotation_angle = 20
self.rotation_matrix_string = 'matrix(0.94 0.342 -0.342 0.94 0 0)'
self.test_margin_field_file = self.test_data_dir + sep + 'W_I_8_neu_125-01.svg'
self.test_place_printing_verso = self.test_data_dir + sep + 'N_VII_1_xp5_4_page5.svg'
self.test_place_printing_recto = self.test_data_dir + sep + 'N_VII_1_xp5_4_page6.svg'
self.multipage = f'{self.test_data_dir}{sep}pdfsvg{sep}csv{sep}15.svg'
self.marginals_extra = f'{self.test_data_dir}{sep}pdfsvg{sep}csv{sep}45.svg'
self.marginals_extra_fn = f'{self.test_data_dir}{sep}pdfsvg{sep}csv{sep}44.svg'
def test_Matrix(self):
matrix = Matrix(self.matrix_string)
self.assertEqual(matrix.getX(), self.x)
self.assertEqual(matrix.add2X(1), self.x + 1)
self.assertEqual(matrix.getY(), self.y)
matrix = Matrix('matrix(0.98966578,0.1433933,-0.0913015,0.9958233,0,0)')
self.assertEqual(matrix.getX(), 0)
matrix = Matrix('matrix(1 2.998719e-04 -2.998719e-04 1 415.3643 476.7988)')
def test_Matrix_rotation(self):
rotation_string = 'rotate({})'.format(self.rotation_angle)
rotation_stringC = 'rotate(-{})'.format(self.rotation_angle)
matrixA = Matrix(rotation_string)
matrixB = Matrix(self.rotation_matrix_string)
matrixC = Matrix(rotation_stringC)
self.assertEqual(matrixA.matrix[Matrix.A], matrixB.matrix[Matrix.A])
self.assertEqual(matrixA.matrix[Matrix.B], matrixB.matrix[Matrix.B])
self.assertEqual(matrixA.matrix[Matrix.C], matrixB.matrix[Matrix.C])
self.assertEqual(matrixA.matrix[Matrix.D], matrixB.matrix[Matrix.D])
self.assertEqual(matrixA.matrix[Matrix.E], matrixB.matrix[Matrix.E])
self.assertEqual(matrixA.matrix[Matrix.F], matrixB.matrix[Matrix.F])
self.assertEqual(matrixA.toString(), self.rotation_matrix_string)
self.assertEqual(matrixC.toCSSTransformString(), 'rotate(-{}deg)'.format(self.rotation_angle))
def test_get_rotation_direction(self):
rotation_string = 'rotate(-{})'.format(self.rotation_angle)
matrixA = Matrix(rotation_string)
matrixB = Matrix(self.rotation_matrix_string)
matrixC = Matrix(self.matrix_string)
self.assertEqual(matrixA.get_rotation_direction(), Matrix.UP)
self.assertEqual(matrixB.get_rotation_direction(), Matrix.DOWN)
self.assertEqual(matrixC.get_rotation_direction(), Matrix.STRAIGHT)
def test_isRotationMatrix(self):
rotation_string = 'rotate({})'.format(self.rotation_angle)
matrixA = Matrix(rotation_string)
self.assertEqual(matrixA.isRotationMatrix(), True)
matrixB = Matrix(self.matrix_string)
self.assertEqual(matrixB.isRotationMatrix(), False)
def test_toCSSTransformString(self):
rotation_string = 'rotate({})'.format(self.rotation_angle)
matrixA = Matrix(rotation_string)
self.assertEqual(matrixA.toCSSTransformString(), 'rotate({}deg)'.format(self.rotation_angle))
matrixB = Matrix(self.rotation_matrix_string)
self.assertEqual(matrixB.toCSSTransformString(), 'rotate({}deg)'.format(self.rotation_angle))
def test_Matrix_Exception(self):
with self.assertRaises(Exception):
Matrix('matrix({})'.format(' '.join([ '0.0' for i in range(5)])))
def test_Matrix_TranskriptionField(self):
tf = TranskriptionField(self.test_file)
matrix = Matrix(self.matrix_string, transkription_field=tf)
self.assertEqual(round(matrix.getX(), 3) , 28.706)
self.assertEqual(round(matrix.getY(), 3) , 31.563)
def test_get_transformed_positions(self):
# Test relies on the example from "https://developer.mozilla.org/en-US/docs/Web/SVG/Attribute/transform"
x = 10
y = 10
width = 30
height = 20
matrix = Matrix(transform_matrix_string='matrix(3 1 -1 3 30 40)')
new_x, new_y, new_width, new_height = matrix.get_transformed_positions(x=x, y=y, width=width, height=height)
self.assertEqual(new_x, 50)
self.assertEqual(new_y, 80)
self.assertEqual(new_width, 90)
self.assertEqual(new_height, 60)
def test_is_matrix_horizontal(self):
matrix = Matrix(transform_matrix_string='matrix(3 1 -1 3 30 40)')
self.assertEqual(matrix.is_matrix_horizontal(), False)
matrix = Matrix(transform_matrix_string='matrix(1 0 0 1 30 40)')
self.assertEqual(matrix.is_matrix_horizontal(), True)
def test_is_part_of_transkription_field(self):
tf = TranskriptionField(self.test_file)
text_node = ET.Element('text', attrib={'transform': 'matrix(1 0 0 1 244.1211 91.7134)'})
self.assertEqual(Matrix.IS_PART_OF_TRANSKRIPTION_FIELD(tf, text_node=text_node), True)
text_node = ET.Element('text', attrib={'transform': 'matrix(1 0 0 1 244.1211 51.7134)'})
self.assertEqual(Matrix.IS_PART_OF_TRANSKRIPTION_FIELD(tf, text_node=text_node), False)
text_node = ET.Element('text', attrib={'transform': 'matrix(1 0 0 1 44.1211 91.7134)'})
self.assertEqual(Matrix.IS_PART_OF_TRANSKRIPTION_FIELD(tf, text_node=text_node), False)
text_node = ET.Element('text', attrib={'transform': 'matrix(1 0 0 1 244.1211 891.7134)'})
self.assertEqual(Matrix.IS_PART_OF_TRANSKRIPTION_FIELD(tf, text_node=text_node), False)
text_node = ET.Element('text', attrib={'transform': 'matrix(1 0 0 1 844.1211 91.7134)'})
self.assertEqual(Matrix.IS_PART_OF_TRANSKRIPTION_FIELD(tf, text_node=text_node), False)
text_node = ET.Element('text', attrib={'transform': 'matrix(0.866 -0.5 0.5 0.866 356.4303 753.4836)'})
tspan_node = ET.SubElement(text_node, 'tspan', attrib={'x': '41.82', 'y': '0'})
tspan_node.text = 'De'
fake_tf = FakeTF()
self.assertEqual(Matrix.IS_PART_OF_TRANSKRIPTION_FIELD(fake_tf, text_node=text_node), True)
def test_is_nearx_tf(self):
tf = TranskriptionField(self.test_file)
matrix_string = 'matrix(1 0 0 1 180.8755 315.9131)'
self.assertEqual(Matrix.IS_NEARX_TRANSKRIPTION_FIELD(matrix_string, tf), True)
matrix_string = 'matrix(1 0 0 1 100.8755 315.9131)'
self.assertEqual(Matrix.IS_NEARX_TRANSKRIPTION_FIELD(matrix_string, tf), False)
def test_do_conversion_factors_differ(self):
self.assertEqual(Matrix.DO_CONVERSION_FACTORS_DIFFER(None, None), False)
matrix_a = Matrix('matrix(1 0 0 1 180.8755 315.9131)')
self.assertEqual(Matrix.DO_CONVERSION_FACTORS_DIFFER(matrix_a, None), True)
matrix_b = Matrix('matrix(1 0 0 1 100.8755 315.9131)')
self.assertEqual(Matrix.DO_CONVERSION_FACTORS_DIFFER(matrix_a, matrix_b), False)
matrix_b = Matrix('matrix(0 0 0 1 100.8755 315.9131)')
self.assertEqual(Matrix.DO_CONVERSION_FACTORS_DIFFER(matrix_a, matrix_b), True)
matrix_b = Matrix('matrix(1 1 0 1 100.8755 315.9131)')
self.assertEqual(Matrix.DO_CONVERSION_FACTORS_DIFFER(matrix_a, matrix_b), True)
matrix_b = Matrix('matrix(1 0 1 1 100.8755 315.9131)')
self.assertEqual(Matrix.DO_CONVERSION_FACTORS_DIFFER(matrix_a, matrix_b), True)
matrix_b = Matrix('matrix(1 0 0 0 100.8755 315.9131)')
self.assertEqual(Matrix.DO_CONVERSION_FACTORS_DIFFER(matrix_a, matrix_b), True)
def test_clone_transformation_matrix(self):
matrix_a = Matrix(matrix_list=[ 1, 0, 0, 1, 180.8755, 315.9131 ])
matrix_b = matrix_a.clone_transformation_matrix()
self.assertEqual(Matrix.DO_CONVERSION_FACTORS_DIFFER(matrix_a, matrix_b), False)
self.assertEqual(matrix_b.matrix[Matrix.E], 0)
self.assertEqual(matrix_b.matrix[Matrix.F], 0)
def test_toString(self):
matrix_string = 'matrix(1.0 0.0 0.0 1.0 180.8755 315.9131)'
matrix = Matrix(matrix_string)
self.assertEqual(matrix.toString(), matrix_string)
def test_get_semanticAndDataDict(self):
matrix = Matrix('rotate(20)')
#self.assertEqual(matrix.get_data_dictionary()['body'].get('matrix'), matrix.matrix)
def test_is_in_margin_field(self):
tf = TranskriptionField(self.test_margin_field_file)
text_node = ET.Element('text', attrib={'transform': 'matrix(1 0 0 1 178.8916 182.0127)'})
self.assertEqual(Matrix.IS_IN_MARGIN_FIELD(text_node.get('transform'), tf), True)
text_node = ET.Element('text', attrib={'transform': 'matrix(1 0 0 1 357.7339 818.3276)'})
self.assertEqual(Matrix.IS_IN_MARGIN_FIELD(text_node.get('transform'), tf), False)
tf = TranskriptionField(self.marginals_extra)
text_node = ET.Element('text', attrib={'transform': 'matrix(1 0 0 1 778.519 407.1094)'})
self.assertEqual(Matrix.IS_IN_MARGIN_FIELD(text_node.get('transform'), tf, marginals_on_extra_page=True), True)
text_node = ET.Element('text', attrib={'transform': 'matrix(1 0 0 1 51.8503 1056.1182)'})
self.assertEqual(Matrix.IS_IN_MARGIN_FIELD(text_node.get('transform'), tf, marginals_on_extra_page=True), False)
text_node = ET.Element('text', attrib={'transform': 'matrix(1 0 0 1 552.9165 1072.1025)'})
self.assertEqual(Matrix.IS_IN_MARGIN_FIELD(text_node.get('transform'), tf, marginals_on_extra_page=True), False)
def test_is_in_place_of_printing_area(self):
tf = TranskriptionField(self.test_place_printing_verso)
text_node = ET.Element('text', attrib={'transform': 'matrix(1 0 0 1 42.5195 575.8736)'})
self.assertEqual(Matrix.IS_IN_PLACE_OF_PRINTING_AREA(text_node.get('transform'), tf), True)
text_node = ET.Element('text', attrib={'transform': 'matrix(1 0 0 1 109.145 575.8736)'})
self.assertEqual(Matrix.IS_IN_PLACE_OF_PRINTING_AREA(text_node.get('transform'), tf), True)
text_node = ET.Element('text', attrib={'transform': 'matrix(1 0 0 1 191.0571 575.8736)'})
self.assertEqual(Matrix.IS_IN_PLACE_OF_PRINTING_AREA(text_node.get('transform'), tf), False)
tf = TranskriptionField(self.test_place_printing_recto)
text_node = ET.Element('text', attrib={'transform': 'matrix(1 0 0 1 28.3462 575.8736)'})
self.assertEqual(Matrix.IS_IN_PLACE_OF_PRINTING_AREA(text_node.get('transform'), tf), True)
text_node = ET.Element('text', attrib={'transform': 'matrix(1 0 0 1 28.3462 583.8736)'})
self.assertEqual(Matrix.IS_IN_PLACE_OF_PRINTING_AREA(text_node.get('transform'), tf), True)
text_node = ET.Element('text', attrib={'transform': 'matrix(1 0 0 1 170.0791 575.8736)'})
self.assertEqual(Matrix.IS_IN_PLACE_OF_PRINTING_AREA(text_node.get('transform'), tf), False)
def test_is_in_footnote_area(self):
tf = TranskriptionField(self.test_place_printing_verso)
text_node = ET.Element('text', attrib={'transform': 'matrix(1 0 0 1 42.5195 575.8736)'})
self.assertEqual(Matrix.IS_IN_FOOTNOTE_AREA(text_node.get('transform'), tf), False)
text_node = ET.Element('text', attrib={'transform': 'matrix(1 0 0 1 109.145 575.8736)'})
self.assertEqual(Matrix.IS_IN_FOOTNOTE_AREA(text_node.get('transform'), tf), False)
text_node = ET.Element('text', attrib={'transform': 'matrix(1 0 0 1 191.0571 575.8736)'})
self.assertEqual(Matrix.IS_IN_FOOTNOTE_AREA(text_node.get('transform'), tf), True)
tf = TranskriptionField(self.test_place_printing_recto)
text_node = ET.Element('text', attrib={'transform': 'matrix(1 0 0 1 28.3462 575.8736)'})
self.assertEqual(Matrix.IS_IN_FOOTNOTE_AREA(text_node.get('transform'), tf), False)
text_node = ET.Element('text', attrib={'transform': 'matrix(1 0 0 1 28.3462 583.8736)'})
self.assertEqual(Matrix.IS_IN_FOOTNOTE_AREA(text_node.get('transform'), tf), False)
text_node = ET.Element('text', attrib={'transform': 'matrix(1 0 0 1 170.0791 575.8736)'})
self.assertEqual(Matrix.IS_IN_FOOTNOTE_AREA(text_node.get('transform'), tf), True)
tf = TranskriptionField(self.multipage, multipage_index=0)
text_node = ET.Element('text', attrib={'transform': 'matrix(1 0 0 1 395.7141 463.6953)'})
self.assertEqual(Matrix.IS_IN_FOOTNOTE_AREA(text_node.get('transform'), tf), False)
text_node = ET.Element('text', attrib={'transform': 'matrix(1 0 0 1 395.7141 453.6953)'})
self.assertEqual(Matrix.IS_IN_FOOTNOTE_AREA(text_node.get('transform'), tf), True)
tf = TranskriptionField(self.marginals_extra)
text_node = ET.Element('text', attrib={'transform': 'matrix(1 0 0 1 552.9165 1072.1025)'})
self.assertEqual(Matrix.IS_IN_FOOTNOTE_AREA(text_node.get('transform'), tf, marginals_on_extra_page=True), True)
text_node = ET.Element('text', attrib={'transform': 'matrix(1 0 0 1 51.8503 1056.1182)'})
self.assertEqual(Matrix.IS_IN_FOOTNOTE_AREA(text_node.get('transform'), tf, x=5.352, marginals_on_extra_page=True), False)
text_node = ET.Element('text', attrib={'transform': 'matrix(1 0 0 1 215.5483 1056.1182)'})
self.assertTrue(Matrix.IS_IN_FOOTNOTE_AREA(text_node.get('transform'), tf, x=24.732, marginals_on_extra_page=True))
svg_tree = ET.parse(self.marginals_extra_fn)
tf = TranskriptionField(self.marginals_extra)
namespaces = { k if k is not None else 'ns': v for k, v in svg_tree.getroot().nsmap.items() }
node = svg_tree.xpath('//ns:text[@transform="matrix(1 0 0 1.0101 698.1499 85.3594)"]', namespaces=namespaces)[0]
self.assertFalse(Matrix.NODE_HAS_CONTENT_IN_FOOTNOTE_AREA(node, tf, marginals_on_extra_page=True))
self.assertFalse(Matrix.IS_IN_FOOTNOTE_AREA(node.get('transform'), tf, marginals_on_extra_page=True))
node = svg_tree.xpath('//ns:text[@transform="matrix(1 0 0 1 215.5483 1056.1182)"]', namespaces=namespaces)[0]
self.assertTrue(Matrix.NODE_HAS_CONTENT_IN_FOOTNOTE_AREA(node, tf, marginals_on_extra_page=True))
if __name__ == "__main__":
unittest.main()

Event Timeline