<commit_before># -*- coding: utf-8 -*-
from unittest import TestCase

import numpy as np

from sldc import Dispatcher
from sldc import DispatchingRule, PolygonClassifier, WorkflowBuilder, Segmenter
from .util import circularity, draw_circle, draw_square, draw_poly, NumpyImage, relative_error

__author__ = "<NAME> <<EMAIL>>"
__version__ = "0.1"


class CircleSegmenter(Segmenter):
    def segment(self, image):
        """Segment a grey circle in black image"""
        segmented = (image[:, :, 0] > 50)
        return segmented.astype("uint8") * 255


class CircleClassifier(PolygonClassifier):
    def predict(self, image, polygon):
        """A polygon classifier which always predict 1 with a probablility 1.0"""
        return 1, 1.0


class CircleRule(DispatchingRule):
    """A rule which matches circle polygons"""
    def evaluate(self, image, polygon):
        return circularity(polygon) > 0.85


class SquareRule(DispatchingRule):
    """A rule that matches square polygons"""
    def evaluate(self, image, polygon):
        return circularity(polygon) <= 0.8


class ColorClassifier(PolygonClassifier):
    """A classifier which returns the color class of the center point of the polygon"""
    GREY = 0
    WHITE = 1

    def predict(self, image, polygon):
        window = image.window_from_polygon(polygon)
        sub_image = window.np_image
        pxl = sub_image[int(polygon.centroid.y) - window.offset_y][int(polygon.centroid.x) - window.offset_x]
        if pxl == 255:
            return ColorClassifier.WHITE, 1.0
        elif 0 < pxl < 255:
            return ColorClassifier.GREY, 1.0
        else:
            return None, 1.0


class CustomSegmenter(Segmenter):
    """Every non black pixel are in object of interests"""
    def segment(self, image):
        return (image > 0).astype("uint8")


class CustomDispatcher(Dispatcher):
    """Dispatch polygons as big or small"""
    def __init__(self, thresh_area):
        super(CustomDispatcher, self).__init__()
        self._thresh_area = thresh_area

    def dispatch(self, image, polygon):
        return "BIG" if polygon.area > self._thresh_area else "SMALL"


class TestFullWorkflow(TestCase):
    def testFindCircleAndSquare(self):
        """Test the workflow on an image containing both squares and circles of different colors
        Two squares of side: 200
            - white centered in (1000, 1000)
            - grey centered in (3000, 3000)
        Two circles of side: 100
            - white centered in (1000, 3000)
            - grey centered in (3000, 1000)
        Another white square of side 300 centered in (2000,2000)
        """
        # square
        w, h = 2000, 2000
        image = np.zeros((w, h,), dtype="uint8")
        image = draw_circle(image, 100, (500, 1500), 255)
        image = draw_circle(image, 100, (1500, 600), 127)
        image = draw_square(image, 200, (500, 500), 255)
        image = draw_square(image, 200, (1500, 1500), 127)
        image = draw_square(image, 300, (1000, 1000), 255)

        # Build the workflow
        builder = WorkflowBuilder()
        builder.set_segmenter(CustomSegmenter())
        builder.add_classifier(CircleRule(), ColorClassifier(), dispatching_label="circle")
        builder.add_classifier(SquareRule(), ColorClassifier())
        workflow = builder.get()

        # Execute
        results = workflow.process(NumpyImage(image))

        image = np.zeros((h, w), dtype="uint8")
        color = 50
        for p in results.polygons:
            image = draw_poly(image, p, color)
            color += 35
        import cv2
        cv2.imwrite("image.png", image)

        # count check
        count = len(results)
        self.assertEqual(count, 5)

        # sort polygons
        sorted_idx = sorted(range(count), key=lambda i: (results.polygons[i].centroid.y, results.polygons[i].centroid.x))

        # first square
        square1 = results.polygons[sorted_idx[0]]
        self.assertEqual(relative_error(square1.area, 200 * 200) < 0.005, True)
        self.assertEqual(relative_error(square1.centroid.x, 500) < 0.005, True)
        self.assertEqual(relative_error(square1.centroid.y, 500) < 0.005, True)
        self.assertEqual(results.dispatch[sorted_idx[0]], 1)  # square
        self.assertEqual(results.classes[sorted_idx[0]], ColorClassifier.WHITE)  # white
        self.assertAlmostEquals(results.probas[sorted_idx[0]], 1.0)

        # first circle
        circle1 = results.polygons[sorted_idx[1]]
        self.assertEqual(relative_error(circle1.area, np.pi * 100 * 100) < 0.005, True)
        self.assertEqual(relative_error(circle1.centroid.x, 1500) < 0.005, True)
        self.assertEqual(relative_error(circle1.centroid.y, 600) < 0.005, True)
        self.assertEqual(results.dispatch[sorted_idx[1]], "circle")  # circle
        self.assertEqual(results.classes[sorted_idx[1]], ColorClassifier.GREY)  # grey
        self.assertAlmostEquals(results.probas[sorted_idx[1]], 1.0)

        # second square (centered)
        square2 = results.polygons[sorted_idx[2]]
        self.assertEqual(relative_error(square2.area, 300 * 300) < 0.005, True)
        self.assertEqual(relative_error(square2.centroid.x, 1000) < 0.005, True)
        self.assertEqual(relative_error(square2.centroid.y, 1000) < 0.005, True)<commit_msg>remove debug image generation<commit_after># -*- coding: utf-8 -*-
from unittest import TestCase

import numpy as np

from sldc import Dispatcher
from sldc import DispatchingRule, PolygonClassifier, WorkflowBuilder, Segmenter
from .util import circularity, draw_circle, draw_square, draw_poly, NumpyImage, relative_error

__author__ = "<NAME> <<EMAIL>>"
__version__ = "0.1"


class CircleSegmenter(Segmenter):
    def segment(self, image):
        """Segment a grey circle in black image"""
        segmented = (image[:, :, 0] > 50)
        return segmented.astype("uint8") * 255


class CircleClassifier(PolygonClassifier):
    def predict(self, image, polygon):
        """A polygon classifier which always predict 1 with a probablility 1.0"""
        return 1, 1.0


class CircleRule(DispatchingRule):
    """A rule which matches circle polygons"""
    def evaluate(self, image, polygon):
        return circularity(polygon) > 0.85


class SquareRule(DispatchingRule):
    """A rule that matches square polygons"""
    def evaluate(self, image, polygon):
        return circularity(polygon) <= 0.8


class ColorClassifier(PolygonClassifier):
    """A classifier which returns the color class of the center point of the polygon"""
    GREY = 0
    WHITE = 1

    def predict(self, image, polygon):
        window = image.window_from_polygon(polygon)
        sub_image = window.np_image
        pxl = sub_image[int(polygon.centroid.y) - window.offset_y][int(polygon.centroid.x) - window.offset_x]
        if pxl == 255:
            return ColorClassifier.WHITE, 1.0
        elif 0 < pxl < 255:
            return ColorClassifier.GREY, 1.0
        else:
            return None, 1.0


class CustomSegmenter(Segmenter):
    """Every non black pixel are in object of interests"""
    def segment(self, image):
        return (image > 0).astype("uint8")


class CustomDispatcher(Dispatcher):
    """Dispatch polygons as big or small"""
    def __init__(self, thresh_area):
        super(CustomDispatcher, self).__init__()
        self._thresh_area = thresh_area

    def dispatch(self, image, polygon):
        return "BIG" if polygon.area > self._thresh_area else "SMALL"


class TestFullWorkflow(TestCase):
    def testFindCircleAndSquare(self):
        """Test the workflow on an image containing both squares and circles of different colors
        Two squares of side: 200
            - white centered in (1000, 1000)
            - grey centered in (3000, 3000)
        Two circles of side: 100
            - white centered in (1000, 3000)
            - grey centered in (3000, 1000)
        Another white square of side 300 centered in (2000,2000)
        """
        # square
        w, h = 2000, 2000
        image = np.zeros((w, h,), dtype="uint8")
        image = draw_circle(image, 100, (500, 1500), 255)
        image = draw_circle(image, 100, (1500, 600), 127)
        image = draw_square(image, 200, (500, 500), 255)
        image = draw_square(image, 200, (1500, 1500), 127)
        image = draw_square(image, 300, (1000, 1000), 255)

        # Build the workflow
        builder = WorkflowBuilder()
        builder.set_segmenter(CustomSegmenter())
        builder.add_classifier(CircleRule(), ColorClassifier(), dispatching_label="circle")
        builder.add_classifier(SquareRule(), ColorClassifier())
        workflow = builder.get()

        # Execute
        results = workflow.process(NumpyImage(image))

        # count check
        count = len(results)
        self.assertEqual(count, 5)

        # sort polygons
        sorted_idx = sorted(range(count), key=lambda i: (results.polygons[i].centroid.y, results.polygons[i].centroid.x))

        # first square
        square1 = results.polygons[sorted_idx[0]]
        self.assertEqual(relative_error(square1.area, 200 * 200) < 0.005, True)
        self.assertEqual(relative_error(square1.centroid.x, 500) < 0.005, True)
        self.assertEqual(relative_error(square1.centroid.y, 500) < 0.005, True)
        self.assertEqual(results.dispatch[sorted_idx[0]], 1)  # square
        self.assertEqual(results.classes[sorted_idx[0]], ColorClassifier.WHITE)  # white
        self.assertAlmostEquals(results.probas[sorted_idx[0]], 1.0)

        # first circle
        circle1 = results.polygons[sorted_idx[1]]
        self.assertEqual(relative_error(circle1.area, np.pi * 100 * 100) < 0.005, True)
        self.assertEqual(relative_error(circle1.centroid.x, 1500) < 0.005, True)
        self.assertEqual(relative_error(circle1.centroid.y, 600) < 0.005, True)
        self.assertEqual(results.dispatch[sorted_idx[1]], "circle")  # circle
        self.assertEqual(results.classes[sorted_idx[1]], ColorClassifier.GREY)  # grey
        self.assertAlmostEquals(results.probas[sorted_idx[1]], 1.0)

        # second square (centered)
        square2 = results.polygons[sorted_idx[2]]
        self.assertEqual(relative_error(square2.area, 300 * 300) < 0.005, True)
        self.assertEqual(relative_error(square2.centroid.x, 1000) < 0.005, True)
        self.assertEqual(relative_error(square2.centroid.y, 1000) < 0.005, True)
{'fraction_non_alphanumeric': 0.11386186301897698, 'fraction_numerical': 0.051536460841922746, 'mean_word_length': 2.9884748367268537, 'pattern_counts': {'":': 0, '<': 33, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '871122', 'n_tokens_mistral': 3599, 'n_tokens_neox': 3090, 'n_words': 806}