test_flip_image.py 2.88 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
13
14
15
16
17
18
19
20
21
22
23
24
        """
        Aim
        ---
        Test `flip_image` function with package data.

        Criteria
        --------
        The flipped image is consistent with answer.

        Details
        -------
        The input image is in package data.
        """
BO ZHANG's avatar
BO ZHANG committed
25
        self.assertTrue(
BO ZHANG's avatar
BO ZHANG committed
26
            np.all(flip_image(read_default_image()) == np.array([[4, 3], [2, 1]]))
BO ZHANG's avatar
BO ZHANG committed
27
28
29
30
31
        )

        # 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
32

BO ZHANG's avatar
BO ZHANG committed
33
    def test_flip_image_on_server(self):
BO ZHANG's avatar
BO ZHANG committed
34
35
36
37
38
39
40
41
42
43
44
45
46
        """
        Aim
        ---
        Test `flip_image` function with server data.

        Criteria
        --------
        The flipped image is consistent with answer at 1e-6 level.

        Details
        -------
        This is the same case with `test_flip_image` but the data is on server.
        """
BO ZHANG's avatar
BO ZHANG committed
47
        image_input = np.loadtxt(
BO ZHANG's avatar
BO ZHANG committed
48
49
50
            os.path.join(
                os.environ["UNIT_TEST_DATA_ROOT"],
                "csst_proto/test_flip/input/image.txt",
51
52
53
            ),
            delimiter=",",
            dtype=int,
BO ZHANG's avatar
BO ZHANG committed
54
        )
BO ZHANG's avatar
BO ZHANG committed
55
        image_answer = np.loadtxt(
BO ZHANG's avatar
BO ZHANG committed
56
57
58
            os.path.join(
                os.environ["UNIT_TEST_DATA_ROOT"],
                "csst_proto/test_flip/answer/flipped_image.txt",
59
60
61
            ),
            delimiter=",",
            dtype=int,
BO ZHANG's avatar
BO ZHANG committed
62
        )
BO ZHANG's avatar
BO ZHANG committed
63
64
65
        self.assertLess(
            np.linalg.norm(flip_image(image_input) - image_answer),
            1e-6,
BO ZHANG's avatar
BO ZHANG committed
66
67
68
            "Test flip image on server failed",
        )

BO ZHANG's avatar
BO ZHANG committed
69
    def test_flip_multiple_images_mp(self):
BO ZHANG's avatar
BO ZHANG committed
70
71
72
73
74
75
76
77
78
79
80
81
82
        """
        Aim
        ---
        Test `flip_multiple_images_mp` function with package data.

        Criteria
        --------
        The flipped image equals answer.

        Details
        -------
        The input image is in package data.
        """
BO ZHANG's avatar
BO ZHANG committed
83
        n_jobs = 10
BO ZHANG's avatar
BO ZHANG committed
84
        imgs = [read_default_image() for _ in range(n_jobs)]
BO ZHANG's avatar
BO ZHANG committed
85
86
        flipped_imgs = flip_multiple_images_mp(imgs, n_jobs)
        for i_job in range(n_jobs):
BO ZHANG's avatar
BO ZHANG committed
87
            self.assertTrue(np.all(flipped_imgs[i_job] == np.array([[4, 3], [2, 1]])))
BO ZHANG's avatar
BO ZHANG committed
88
89

    def test_flip_multiple_images_jl(self):
BO ZHANG's avatar
BO ZHANG committed
90
91
92
93
94
95
96
97
98
99
100
101
102
        """
        Aim
        ---
        Test `flip_multiple_images_jl` function with package data.

        Criteria
        --------
        The flipped image equals answer.

        Details
        -------
        The input image is in package data.
        """
BO ZHANG's avatar
BO ZHANG committed
103
        n_jobs = 10
BO ZHANG's avatar
BO ZHANG committed
104
        imgs = [read_default_image() for _ in range(n_jobs)]
BO ZHANG's avatar
BO ZHANG committed
105
106
        flipped_imgs = flip_multiple_images_jl(imgs, n_jobs)
        for i_job in range(n_jobs):
BO ZHANG's avatar
BO ZHANG committed
107
            self.assertTrue(np.all(flipped_imgs[i_job] == np.array([[4, 3], [2, 1]])))