test_flip_image.py 1.94 KB
Newer Older
BO ZHANG's avatar
BO ZHANG committed
1
import os
BO ZHANG's avatar
BO ZHANG committed
2
3
4
5
import unittest

import numpy as np

BO ZHANG's avatar
BO ZHANG committed
6
7
from csst_proto import flip_image, read_default_image
from csst_proto import flip_multiple_images_jl, flip_multiple_images_mp
BO ZHANG's avatar
BO ZHANG committed
8
9
10
11


class FlipImageTestCase(unittest.TestCase):
    def test_flip_image(self):
BO ZHANG's avatar
BO ZHANG committed
12
        """test flip image"""
BO ZHANG's avatar
BO ZHANG committed
13
        self.assertTrue(
BO ZHANG's avatar
BO ZHANG committed
14
            np.all(flip_image(read_default_image()) == np.array([[4, 3], [2, 1]]))
BO ZHANG's avatar
BO ZHANG committed
15
16
17
18
19
        )

        # the code fails for 1D array
        with self.assertRaises(AssertionError):
            flip_image(np.array([1, 2, 3, 4]))
BO ZHANG's avatar
BO ZHANG committed
20

BO ZHANG's avatar
BO ZHANG committed
21
22
    def test_flip_image_on_server(self):
        """test flip image on server"""
BO ZHANG's avatar
BO ZHANG committed
23
        image_input = np.loadtxt(
BO ZHANG's avatar
BO ZHANG committed
24
25
26
            os.path.join(
                os.environ["UNIT_TEST_DATA_ROOT"],
                "csst_proto/test_flip/input/image.txt",
27
28
29
            ),
            delimiter=",",
            dtype=int,
BO ZHANG's avatar
BO ZHANG committed
30
        )
BO ZHANG's avatar
BO ZHANG committed
31
        image_answer = np.loadtxt(
BO ZHANG's avatar
BO ZHANG committed
32
33
34
            os.path.join(
                os.environ["UNIT_TEST_DATA_ROOT"],
                "csst_proto/test_flip/answer/flipped_image.txt",
35
36
37
            ),
            delimiter=",",
            dtype=int,
BO ZHANG's avatar
BO ZHANG committed
38
        )
BO ZHANG's avatar
BO ZHANG committed
39
        self.assertEqual(
BO ZHANG's avatar
BO ZHANG committed
40
41
42
43
44
            flip_image(image_input),
            image_answer,
            "Test flip image on server failed",
        )

BO ZHANG's avatar
BO ZHANG committed
45
    def test_flip_multiple_images_mp(self):
BO ZHANG's avatar
BO ZHANG committed
46
        """test flip multiple images with multiprocessing"""
BO ZHANG's avatar
BO ZHANG committed
47
        n_jobs = 10
BO ZHANG's avatar
BO ZHANG committed
48
        imgs = [read_default_image() for _ in range(n_jobs)]
BO ZHANG's avatar
BO ZHANG committed
49
50
        flipped_imgs = flip_multiple_images_mp(imgs, n_jobs)
        for i_job in range(n_jobs):
BO ZHANG's avatar
BO ZHANG committed
51
            self.assertTrue(np.all(flipped_imgs[i_job] == np.array([[4, 3], [2, 1]])))
BO ZHANG's avatar
BO ZHANG committed
52
53

    def test_flip_multiple_images_jl(self):
BO ZHANG's avatar
BO ZHANG committed
54
        """test flip multiple images with joblib"""
BO ZHANG's avatar
BO ZHANG committed
55
        n_jobs = 10
BO ZHANG's avatar
BO ZHANG committed
56
        imgs = [read_default_image() for _ in range(n_jobs)]
BO ZHANG's avatar
BO ZHANG committed
57
58
        flipped_imgs = flip_multiple_images_jl(imgs, n_jobs)
        for i_job in range(n_jobs):
BO ZHANG's avatar
BO ZHANG committed
59
            self.assertTrue(np.all(flipped_imgs[i_job] == np.array([[4, 3], [2, 1]])))