예제 #1
0
    def update(self, data: SparseMatrix):
        if not data.values():
            return

        peak = max(data.values())
        for (row, column), level in data.items():
            # SparseMatrix row 0 is at the top, PIL y=0 is at the bottom
            y = (data.rows - 1) - row
            x = column
            self.dr.point((x, y), self.fill(level, peak))
예제 #2
0
    def setUp(self):
        self.rows = 2
        self.columns = 2

        self.matrix = SparseMatrix(self.rows, self.columns)
        self.other  = SparseMatrix(self.rows, self.columns)

        self.other[0, 0] = 1
        self.other[0, 1] = 2
        self.other[1, 0] = 3
        self.other[1, 1] = 4
예제 #3
0
class SparseMatrixTest(unittest.TestCase):
    def setUp(self):
        self.rows = 2
        self.columns = 2
        self.matrix = SparseMatrix(self.rows, self.columns)

    def test_assignment(self):
        assigned_value = 1
        assigned_coord = (0, 0)
        self.matrix[assigned_coord] = assigned_value
        self.assertEqual(self.matrix[assigned_coord], assigned_value)

    def test_addition(self):
        assigned_value = 1
        assigned_coord = (0, 0)
        assignments = 2
        for i in range(assignments):
            self.matrix[assigned_coord] += assigned_value
        self.assertEqual(self.matrix[assigned_coord],
                         assigned_value*assignments)

    def test_sparsity(self):
        assignments = {(0, 0): 1,
                       (0, 1): 2}
        for coord, value in assignments.items():
            self.matrix[coord] = value
        self.assertEqual(len(self.matrix.keys()), len(assignments.keys()))

    def test_addition_sparsity(self):
        assignments = {(0, 0): 1,
                       (0, 1): 2}
        for coord, value in assignments.items():
            for i in range(value):
                self.matrix[coord] += value
        self.assertEqual(len(self.matrix.keys()), len(assignments.keys()))

    def test_str(self):
        self.matrix[0, 0] = 1
        self.matrix[0, 1] = 2
        self.matrix[1, 0] = 3
        self.matrix[1, 1] = 4
        self.assertEqual(str(self.matrix), '1 2\n3 4')

    def test_invalid_coord(self):
        with self.assertRaises(KeyError):
            self.matrix[-1, 0]
        with self.assertRaises(KeyError):
            self.matrix[0, -1]

    def test_out_of_bounds(self):
        with self.assertRaises(KeyError):
            self.matrix[0, self.columns+1]
        with self.assertRaises(KeyError):
            self.matrix[self.rows+1, 0]
예제 #4
0
    def test_inplace_add(self):
        original = SparseMatrix(self.rows, self.columns)
        original.add_entries(self.matrix.entries)
        self.matrix += self.other

        for row in range(self.rows):
            for column in range(self.columns):
                with self.subTest(row=row, column=column):
                    self.assertEqual(self.matrix[row, column],
                                     original[row, column] \
                                     + self.other[row, column])
예제 #5
0
    def setUp(self):
        self.rows = 3
        self.columns = 3

        self.adapter = RGBAdapter(self.columns, self.rows)

        self.data = SparseMatrix(self.rows, self.columns)
        for row in range(self.rows):
            for column in range(self.columns):
                self.data[row, column] = 1

        self.r = SparseMatrix(self.rows, self.columns)
        self.r.merge(self.data, row_offset = -1, column_offset = -1)

        self.g = SparseMatrix(self.rows, self.columns)
        self.g.merge(self.data, row_offset = -1, column_offset = 1)

        self.b = SparseMatrix(self.rows, self.columns)
        self.b.merge(self.data, row_offset = 1)

        self.reference = Image.new('RGB', (self.columns, self.rows))
        draw = ImageDraw.Draw(self.reference)
        draw.point((0, 0),  (255,   0,   0))
        draw.point((1, 0),  (255, 255,   0))
        draw.point((2, 0),  (  0, 255,   0))
        draw.point((0, 1),  (255,   0, 255))
        draw.point((1, 1),  (255, 255, 255))
        draw.point((2, 1),  (  0, 255, 255))
        draw.point((0, 2),  (  0,   0, 255))
        draw.point((1, 2),  (  0,   0, 255))
        draw.point((2, 2),  (  0,   0, 255))
예제 #6
0
    def test_update(self):
        matrix = SparseMatrix(2, 2)
        matrix[0, 1] = 1
        matrix[1, 0] = 2
        matrix[1, 1] = 4

        self.channel.update(matrix)

        self.assertEqual(self.channel.im.getpixel((0, 0)),   0)
        self.assertEqual(self.channel.im.getpixel((1, 0)),  64)
        self.assertEqual(self.channel.im.getpixel((0, 1)), 128)
        self.assertEqual(self.channel.im.getpixel((1, 1)), 255)
예제 #7
0
class RGBAdapterTests(unittest.TestCase):
    def setUp(self):
        self.rows = 3
        self.columns = 3

        self.adapter = RGBAdapter(self.columns, self.rows)

        self.data = SparseMatrix(self.rows, self.columns)
        for row in range(self.rows):
            for column in range(self.columns):
                self.data[row, column] = 1

        self.r = SparseMatrix(self.rows, self.columns)
        self.r.merge(self.data, row_offset = -1, column_offset = -1)

        self.g = SparseMatrix(self.rows, self.columns)
        self.g.merge(self.data, row_offset = -1, column_offset = 1)

        self.b = SparseMatrix(self.rows, self.columns)
        self.b.merge(self.data, row_offset = 1)

        self.reference = Image.new('RGB', (self.columns, self.rows))
        draw = ImageDraw.Draw(self.reference)
        draw.point((0, 0),  (255,   0,   0))
        draw.point((1, 0),  (255, 255,   0))
        draw.point((2, 0),  (  0, 255,   0))
        draw.point((0, 1),  (255,   0, 255))
        draw.point((1, 1),  (255, 255, 255))
        draw.point((2, 1),  (  0, 255, 255))
        draw.point((0, 2),  (  0,   0, 255))
        draw.point((1, 2),  (  0,   0, 255))
        draw.point((2, 2),  (  0,   0, 255))

    def test_update(self):
        self.adapter.update(self.r, self.g, self.b)

        for column in range(self.columns):
            for row in range(self.rows):
                with self.subTest(row = row, column = column):
                    self.assertEqual(self.adapter.get_image()
                                     .getpixel((row, column)),
                                     self.reference.getpixel((row, column)))

    def test_save(self):
        filename = '/tmp/adapter.png'
        self.adapter.update(self.r, self.g, self.b).save(filename)

        image = Image.open(filename)

        for column in range(self.columns):
            for row in range(self.rows):
                with self.subTest(row = row, column = column):
                    self.assertEqual(image.getpixel((row, column)),
                                     self.reference.getpixel((row, column)))
예제 #8
0
    def setUp(self):
        self.rows = 2
        self.columns = 2

        self.adapter = GrayscaleAdapter(self.columns, self.rows)

        self.data = SparseMatrix(self.rows, self.columns)
        self.data[0, 0] = 1
        self.data[0, 1] = 2
        self.data[1, 0] = 3
        self.data[1, 1] = 4

        self.reference = Image.new('L', (self.columns, self.rows))
        draw = ImageDraw.Draw(self.reference)
        draw.point((0, 0),  64)
        draw.point((1, 0), 128)
        draw.point((0, 1), 192)
        draw.point((1, 1), 255)
예제 #9
0
class SparseMatrixOperationsTest(unittest.TestCase):
    def setUp(self):
        self.rows = 2
        self.columns = 2

        self.matrix = SparseMatrix(self.rows, self.columns)
        self.other  = SparseMatrix(self.rows, self.columns)

        self.other[0, 0] = 1
        self.other[0, 1] = 2
        self.other[1, 0] = 3
        self.other[1, 1] = 4

    def test_merge(self):
        self.matrix.merge(self.other)

        for row in range(self.rows):
            for column in range(self.columns):
                with self.subTest(row=row, column=column):
                    self.assertEqual(self.matrix[row, column],
                                     self.other[row, column])

    def test_merge_row_offset(self):
        self.matrix.merge(self.other, row_offset=1)

        for row in range(self.rows):
            for column in range(self.columns):
                with self.subTest(row=row, column=column):
                    if row == 0:
                        self.assertEqual(self.matrix[row, column], 0)
                    else:
                        self.assertEqual(self.matrix[row, column],
                                         self.other[row - 1, column])
        self.assertEqual(len(self.matrix.entries), 2)

    def test_merge_column_offset(self):
        self.matrix.merge(self.other, column_offset=1)

        for row in range(self.rows):
            for column in range(self.columns):
                with self.subTest(row=row, column=column):
                    if column == 0:
                        self.assertEqual(self.matrix[row, column], 0)
                    else:
                        self.assertEqual(self.matrix[row, column],
                                         self.other[row, column - 1])
        self.assertEqual(len(self.matrix.entries), 2)

    def test_merge_row_offset_negative(self):
        self.matrix.merge(self.other, row_offset=-1)

        for row in range(self.rows):
            for column in range(self.columns):
                with self.subTest(row=row, column=column):
                    if row == 1:
                        self.assertEqual(self.matrix[row, column], 0)
                    else:
                        self.assertEqual(self.matrix[row, column],
                                         self.other[row + 1, column])
        self.assertEqual(len(self.matrix.entries), 2)

    def test_merge_column_offset_negative(self):
        self.matrix.merge(self.other, column_offset=-1)

        for row in range(self.rows):
            for column in range(self.columns):
                with self.subTest(row=row, column=column):
                    if column == 1:
                        self.assertEqual(self.matrix[row, column], 0)
                    else:
                        self.assertEqual(self.matrix[row, column],
                                         self.other[row, column + 1])
        self.assertEqual(len(self.matrix.entries), 2)

    def test_add(self):
        result = self.matrix + self.other

        for row in range(self.rows):
            for column in range(self.columns):
                with self.subTest(row=row, column=column):
                    self.assertEqual(result[row, column],
                                     self.matrix[row, column] \
                                     + self.other[row, column])

    def test_inplace_add(self):
        original = SparseMatrix(self.rows, self.columns)
        original.add_entries(self.matrix.entries)
        self.matrix += self.other

        for row in range(self.rows):
            for column in range(self.columns):
                with self.subTest(row=row, column=column):
                    self.assertEqual(self.matrix[row, column],
                                     original[row, column] \
                                     + self.other[row, column])
예제 #10
0
 def setUp(self):
     self.rows = 2
     self.columns = 2
     self.matrix = SparseMatrix(self.rows, self.columns)