def test_get_characters_omits_error_nodes(): with open(get_test_path(os.path.join("mathml", "x_plus_error.xml"))) as mathml_file: mathml = mathml_file.read() characters = get_characters(mathml) assert len(characters) == 1 assert Character("x", 0, 0, 1) in characters
def test_parse_equation(): with open( get_test_path(os.path.join("mathml", "x_sub_t_sub_i.xml")) ) as mathml_file: mathml = mathml_file.read() symbols = parse_equation(mathml) assert len(symbols) == 5 x_sub_t_sub_i = list( filter(lambda s: "msub" in str(s.element) and "x" in str(s.element), symbols) )[0] t_sub_i = list( filter(lambda s: "msub" in str(s.element) and s is not x_sub_t_sub_i, symbols) )[0] x = list(filter(lambda s: str(s.element) == "<mi>x</mi>", symbols))[0] t = list(filter(lambda s: str(s.element) == "<mi>t</mi>", symbols))[0] i = list(filter(lambda s: str(s.element) == "<mi>i</mi>", symbols))[0] assert len(x_sub_t_sub_i.children) == 2 assert x in x_sub_t_sub_i.children assert t_sub_i in x_sub_t_sub_i.children assert len(t_sub_i.children) == 2 assert t in t_sub_i.children assert i in t_sub_i.children
def test_get_characters(): with open(get_test_path(os.path.join("mathml", "x_plus_y.xml"))) as mathml_file: mathml = mathml_file.read() characters = get_characters(mathml) assert len(characters) == 2 assert Character("x", 0, 0, 1) in characters assert Character("y", 2, 4, 5) in characters
def load_fragment_tag(filename: str) -> Tag: " Read a MathML fragment from file and return a BeautifulSoup tag for it. " with open(get_test_path(os.path.join("mathml-fragments", filename))) as file_: mathml = file_.read() # 'body.next' is used as the parser adds in 'html' and 'body' tags; this return just the child # node of the body (the original node we were parsing) return BeautifulSoup(mathml, "lxml").body.next
def test_find_bounding_box(): image = cv2.imread( get_test_path(os.path.join("images", "rectangle_hue_40.png"))) boxes = find_boxes_with_color(image, (40 / 360.0)) assert len(boxes) == 1 assert boxes[0].left == 10 assert boxes[0].top == 10 assert boxes[0].width == 10 assert boxes[0].height == 10
def test_get_symbols_omits_error_nodes(): with open(get_test_path(os.path.join("mathml", "x_plus_error.xml"))) as mathml_file: mathml = mathml_file.read() symbols = get_symbols(mathml) assert len(symbols) == 1 x = list(filter(lambda s: s.characters == [0], symbols))[0] assert x.mathml.strip() == "<mi>x</mi>"
def test_parse_consecutive_mi(): with open(get_test_path(os.path.join("mathml", "relu.xml"))) as mathml_file: mathml = mathml_file.read() symbols = get_symbols(mathml) assert len(symbols) == 1 relu = symbols[0] assert len(relu.children) == 0 assert len(relu.characters) == 4 assert relu.mathml == "<mi>ReLU</mi>"
def test_get_symbols_for_subscript(): with open(get_test_path(os.path.join("mathml", "x_sub_i.xml"))) as mathml_file: mathml = mathml_file.read() symbols = get_symbols(mathml) assert len(symbols) == 3 x_sub_i = list(filter(lambda s: "msub" in s.mathml, symbols))[0] assert re.search(r"<msub>\s*<mi>x</mi>\s*<mi>i</mi>\s*</msub>", x_sub_i.mathml) assert len(x_sub_i.characters) == 2 assert 0 in x_sub_i.characters assert 1 in x_sub_i.characters
def test_get_symbols_for_characters(): with open(get_test_path(os.path.join("mathml", "x_plus_y.xml"))) as mathml_file: mathml = mathml_file.read() symbols = get_symbols(mathml) assert len(symbols) == 2 x = list(filter(lambda s: s.characters == [0], symbols))[0] assert x.mathml.strip() == "<mi>x</mi>" y = list(filter(lambda s: s.characters == [2], symbols))[0] assert y.mathml.strip() == "<mi>y</mi>"
def test_get_symbol_sub_super_with_numeric_child(): with open(get_test_path(os.path.join( "mathml", "x_sub_four_sub_three.xml"))) as mathml_file: mathml = mathml_file.read() symbols = get_symbols(mathml) assert len(symbols) == 3 x_sub_four_sub_three = list( filter(lambda s: "msub" in s.mathml and "x" in s.mathml, symbols))[0] x = list(filter(lambda s: s.mathml == "<mi>x</mi>", symbols))[0] four_sub_three = list( filter(lambda s: s.mathml == "<msub>\n<mn>4</mn>\n<mn>3</mn>\n</msub>", symbols))[0] assert len(x_sub_four_sub_three.children) == 2 assert x in x_sub_four_sub_three.children assert four_sub_three in x_sub_four_sub_three.children
def test_split_into_lines(): image = cv2.imread( get_test_path(os.path.join("images", "two_lines_hue_40.png"))) boxes = find_boxes_with_color(image, (40 / 360.0)) assert len(boxes) == 2 # The two boxes that are horizontally aligned but separated get merged. assert boxes[0].left == 5 assert boxes[0].top == 10 assert boxes[0].width == 30 assert boxes[0].height == 10 # The box at a different vertical location doesn't get merged. assert boxes[1].left == 5 assert boxes[1].top == 21 assert boxes[1].width == 10 assert boxes[1].height == 10
def test_find_boxes_within_masks(): image = cv2.imread( get_test_path(os.path.join("images", "two_lines_hue_40.png"))) boxes = find_boxes_with_color( image, (40 / 360.0), masks=(( Rectangle(left=5, top=10, width=10, height=10), Rectangle(left=25, top=10, width=10, height=10), )), ) # The masks will split up two colorized regions that would normally be merged. assert len(boxes) == 2 assert boxes[0].left == 5 assert boxes[0].top == 10 assert boxes[0].width == 10 assert boxes[0].height == 10 assert boxes[1].left == 25 assert boxes[1].top == 10 assert boxes[1].width == 10 assert boxes[1].height == 10
def test_detect_black_pixels(): image = cv2.imread( get_test_path(os.path.join("images", "black_letter_40.png"))) assert contains_black_pixels(image)
def test_no_detect_saturated_pixels_as_black(): image = cv2.imread( get_test_path(os.path.join("images", "yellow_letter_40.png"))) assert not contains_black_pixels(image)
def test_no_detect_black_pixels_in_blank_image(): image = cv2.imread(get_test_path(os.path.join("images", "blank_40.png"))) assert not contains_black_pixels(image)
def test_no_detect_shift_for_other_hue(): black = cv2.imread(get_test_path(os.path.join("images", "black_letter_40.png"))) pink_shifted = cv2.imread( get_test_path(os.path.join("images", "pink_letter_40_shifted.png")) ) assert not has_hue_shifted(black, pink_shifted, YELLOW_HUE)
def test_detect_shift(): black = cv2.imread(get_test_path(os.path.join("images", "black_letter_40.png"))) yellow_shifted = cv2.imread( get_test_path(os.path.join("images", "yellow_letter_40_shifted.png")) ) assert has_hue_shifted(black, yellow_shifted, YELLOW_HUE)
def test_no_detect_shift_when_pixels_aligned(): black = cv2.imread(get_test_path(os.path.join("images", "black_letter_40.png"))) yellow = cv2.imread(get_test_path(os.path.join("images", "yellow_letter_40.png"))) assert not has_hue_shifted(black, yellow, YELLOW_HUE)