summaryrefslogtreecommitdiff
path: root/silx/image/test/test_bilinear.py
blob: 1789aca0287bf36689dddbbe9b2520c63762039a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
# -*- coding: utf-8 -*-
#
#    Project: silx (originally pyFAI)
#             https://github.com/silx-kit/silx
#
#    Copyright (C) 2012-2016  European Synchrotron Radiation Facility, Grenoble, France
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.

__authors__ = ["J. Kieffer"]
__license__ = "MIT"
__date__ = "02/08/2016"

import unittest
import numpy
import logging
logger = logging.getLogger("test_bilinear")
from ..bilinear import BilinearImage


class TestBilinear(unittest.TestCase):
    """basic maximum search test"""
    N = 1000

    def test_max_search_round(self):
        """test maximum search using random points: maximum is at the pixel center"""
        a = numpy.arange(100) - 40.
        b = numpy.arange(100) - 60.
        ga = numpy.exp(-a * a / 4000)
        gb = numpy.exp(-b * b / 6000)
        gg = numpy.outer(ga, gb)
        b = BilinearImage(gg)
        ok = 0
        for s in range(self.N):
            i, j = numpy.random.randint(100), numpy.random.randint(100)
            k, l = b.local_maxi((i, j))
            if abs(k - 40) > 1e-4 or abs(l - 60) > 1e-4:
                logger.warning("Wrong guess maximum (%i,%i) -> (%.1f,%.1f)", i, j, k, l)
            else:
                logger.debug("Good guess maximum (%i,%i) -> (%.1f,%.1f)", i, j, k, l)
                ok += 1
        logger.debug("Success rate: %.1f", 100. * ok / self.N)
        self.assertEqual(ok, self.N, "Maximum is always found")

    def test_max_search_half(self):
        """test maximum search using random points: maximum is at a pixel edge"""
        a = numpy.arange(100) - 40.5
        b = numpy.arange(100) - 60.5
        ga = numpy.exp(-a * a / 4000)
        gb = numpy.exp(-b * b / 6000)
        gg = numpy.outer(ga, gb)
        b = BilinearImage(gg)
        ok = 0
        for s in range(self.N):
            i, j = numpy.random.randint(100), numpy.random.randint(100)
            k, l = b.local_maxi((i, j))
            if abs(k - 40.5) > 0.5 or abs(l - 60.5) > 0.5:
                logger.warning("Wrong guess maximum (%i,%i) -> (%.1f,%.1f)", i, j, k, l)
            else:
                logger.debug("Good guess maximum (%i,%i) -> (%.1f,%.1f)", i, j, k, l)
                ok += 1
        logger.debug("Success rate: %.1f", 100. * ok / self.N)
        self.assertEqual(ok, self.N, "Maximum is always found")

    def test_map(self):
        N = 100
        y, x = numpy.ogrid[:N, :N + 10]
        img = x + y
        b = BilinearImage(img)
        x2d = numpy.zeros_like(y) + x
        y2d = numpy.zeros_like(x) + y
        res1 = b.map_coordinates((y2d, x2d))
        self.assertEquals(abs(res1 - img).max(), 0, "images are the same (corners)")

        x2d = numpy.zeros_like(y) + (x[:, :-1] + 0.5)
        y2d = numpy.zeros_like(x[:, :-1]) + y
        res1 = b.map_coordinates((y2d, x2d))
        self.assertEquals(abs(res1 - img[:, :-1] - 0.5).max(), 0, "images are the same (middle)")

        x2d = numpy.zeros_like(y[:-1, :]) + (x[:, :-1] + 0.5)
        y2d = numpy.zeros_like(x[:, :-1]) + (y[:-1, :] + 0.5)
        res1 = b.map_coordinates((y2d, x2d))
        self.assertEquals(abs(res1 - img[:-1, 1:]).max(), 0, "images are the same (center)")

    def test_profile_grad(self):
        N = 100
        img = numpy.arange(N * N).reshape(N, N)
        b = BilinearImage(img)
        res1 = b.profile_line((0, 0), (N - 1, N - 1))
        l = numpy.ceil(numpy.sqrt(2) * N)
        self.assertEquals(len(res1), l, "Profile has correct length")
        self.assertLess((res1[:-2] - res1[1:-1]).std(), 1e-3, "profile is linear (excluding last point)")

    def test_profile_gaus(self):
        N = 100
        x = numpy.arange(N) - N // 2.0
        g = numpy.exp(-x * x / (N * N))
        img = numpy.outer(g, g)
        b = BilinearImage(img)
        res_hor = b.profile_line((N // 2, 0), (N // 2, N - 1))
        res_ver = b.profile_line((0, N // 2), (N - 1, N // 2))
        self.assertEquals(len(res_hor), N, "Profile has correct length")
        self.assertEquals(len(res_ver), N, "Profile has correct length")
        self.assertLess(abs(res_hor - g).max(), 1e-5, "correct horizontal profile")
        self.assertLess(abs(res_ver - g).max(), 1e-5, "correct vertical profile")

        # Profile with linewidth=3
        expected_profile = img[:, N // 2 - 1:N // 2 + 2].mean(axis=1)
        res_hor = b.profile_line((N // 2, 0), (N // 2, N - 1), linewidth=3)
        res_ver = b.profile_line((0, N // 2), (N - 1, N // 2), linewidth=3)

        self.assertEquals(len(res_hor), N, "Profile has correct length")
        self.assertEquals(len(res_ver), N, "Profile has correct length")
        self.assertLess(abs(res_hor - expected_profile).max(), 1e-5,
                        "correct horizontal profile")
        self.assertLess(abs(res_ver - expected_profile).max(), 1e-5,
                        "correct vertical profile")


def suite():
    testsuite = unittest.TestSuite()
    testsuite.addTest(TestBilinear("test_max_search_round"))
    testsuite.addTest(TestBilinear("test_max_search_half"))
    testsuite.addTest(TestBilinear("test_map"))
    testsuite.addTest(TestBilinear("test_profile_grad"))
    testsuite.addTest(TestBilinear("test_profile_gaus"))
    return testsuite