File: detectors.py

package info (click to toggle)
python-spectral 0.22.4-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 1,064 kB
  • sloc: python: 13,161; makefile: 7
file content (176 lines) | stat: -rw-r--r-- 6,455 bytes parent folder | download
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
'''
Runs unit tests for various target detectors.

To run the unit tests, type the following from the system command line:

    # python -m spectral.tests.detectors
'''

from __future__ import absolute_import, division, print_function, unicode_literals

import numpy as np
from numpy.testing import assert_allclose

import spectral as spy
from spectral.tests.spytest import SpyTest


class MatchedFilterTest(SpyTest):
    def setup(self):
        from spectral.algorithms.detectors import MatchedFilter
        self.data = spy.open_image('92AV3C.lan').load()
        self.background = spy.calc_stats(self.data)
        self.target_ij = [33, 87]
#        self.target = self.data[33, 87]
        (i, j) = self.target_ij
        self.mf = MatchedFilter(self.background, self.data[i, j])

    def test_mf_bg_eq_zero(self):
        '''Matched Filter response of background should be zero.'''
        (i, j) = self.target_ij
        np.testing.assert_approx_equal(self.mf(self.background.mean), 0)
        
    def test_mf_target_eq_one(self):
        '''Matched Filter response of target should be one.'''
        from spectral.algorithms.detectors import matched_filter
        (i, j) = self.target_ij
        target = self.data[i, j]
        mf = matched_filter(self.data, target, self.background)
        np.testing.assert_approx_equal(mf[i, j], 1)

    def test_mf_target_no_bg_eq_one(self):
        '''Matched Filter response of target should be one.'''
        from spectral.algorithms.detectors import matched_filter
        (i, j) = self.target_ij
        target = self.data[i, j]
        mf = matched_filter(self.data, target)
        np.testing.assert_approx_equal(mf[i, j], 1)

    def test_mf_target_pixel_eq_one(self):
        '''Matched Filter response of target pixel should be one.'''
        (i, j) = self.target_ij
        np.testing.assert_approx_equal(self.mf(self.data)[i, j], 1)

    def test_mf_windowed_target_eq_one(self):
        '''Windowed Matched Filter response of target pixel should be one.'''
        X = self.data[:10, :10, :]
        ij = (3, 3)
        y = spy.matched_filter(X, X[ij], window=(3,7), cov=self.background.cov)
        np.allclose(1, y[ij])


class RXTest(SpyTest):
    def setup(self):
        self.data = spy.open_image('92AV3C.lan').load()
        self.background = spy.calc_stats(self.data)

    def test_rx_bg_eq_zero(self):
        from spectral.algorithms.detectors import rx, RX
        d = rx(self.data)
        stats = spy.calc_stats(self.data)
        np.testing.assert_approx_equal(rx(stats.mean, background=stats), 0)
        

class ACETest(SpyTest):
    def setup(self):
        self.data = spy.open_image('92AV3C.lan').load()
        self.bg = spy.calc_stats(self.data)
        self.X = self.data[:20, :20, :]

    def test_ace_bg_eq_zero(self):
        '''ACE score of background mean should be zero.'''
        ij = (10, 10)
        y = spy.ace(self.bg.mean, self.X[ij], background=self.bg)
        assert(np.allclose(0, y))
        
    def test_ace_pixel_target_eq_one(self):
        '''ACE score of target should be one for single pixel arg.'''
        ij = (10, 10)
        y = spy.ace(self.X[ij], self.X[ij], background=self.bg)
        assert(np.allclose(1, y))

    def test_ace_novec_pixel_target_eq_one(self):
        '''ACE score of target should be one for single pixel arg.'''
        ij = (10, 10)
        y = spy.ace(self.X[ij], self.X[ij], background=self.bg, vectorize=False)
        assert(np.allclose(1, y))

    def test_ace_target_eq_one(self):
        '''ACE score of target should be one.'''
        ij = (10, 10)
        y = spy.ace(self.X, self.X[ij], background=self.bg)
        assert(np.allclose(1, y[ij]))

    def test_ace_novec_target_eq_one(self):
        '''ACE score (without vectorization) of target should be one.'''
        ij = (10, 10)
        y = spy.ace(self.X, self.X[ij], background=self.bg, vectorize=False)
        assert(np.allclose(1, y[ij]))

    def test_ace_multi_targets_eq_one(self):
        '''ACE score of multiple targets should each be one.'''
        ij1 = (10, 10)
        ij2 = (3, 12)
        y = spy.ace(self.X, [self.X[ij1], self.X[ij2]], background=self.bg)
        assert(np.allclose(1, [y[ij1][0], y[ij2][1]]))

    def test_ace_novec_multi_targets_eq_one(self):
        '''ACE score of multiple targets should each be one.'''
        ij1 = (10, 10)
        ij2 = (3, 12)
        y = spy.ace(self.X, [self.X[ij1], self.X[ij2]], background=self.bg,
                    vectorize=False)
        assert(np.allclose(1, [y[ij1][0], y[ij2][1]]))

    def test_ace_multi_targets_bg_eq_zero(self):
        '''ACE score of background for multiple targets should be one.'''
        ij1 = (10, 10)
        ij2 = (3, 12)
        y = spy.ace(self.bg.mean, [self.X[ij1], self.X[ij2]],
                    background=self.bg)
        assert(np.allclose(0, y))

    def test_ace_subspace_targets_eq_one(self):
        '''ACE score of targets defining target subspace should each be one.'''
        ij1 = (10, 10)
        ij2 = (3, 12)
        y = spy.ace(self.X, np.array([self.X[ij1], self.X[ij2]]),
                    background=self.bg)
        assert(np.allclose(1, [y[ij1], y[ij2]]))

    def test_ace_novec_subspace_targets_eq_one(self):
        '''ACE score of targets defining target subspace should each be one.'''
        ij1 = (10, 10)
        ij2 = (3, 12)
        y = spy.ace(self.X, np.array([self.X[ij1], self.X[ij2]]),
                    background=self.bg, vectorize=False)
        assert(np.allclose(1, [y[ij1], y[ij2]]))

    def test_ace_subspace_bg_eq_zero(self):
        '''ACE score of background for target subspace should be zero.'''
        ij1 = (10, 10)
        ij2 = (3, 12)
        y = spy.ace(self.bg.mean, np.array([self.X[ij1], self.X[ij2]]),
                    background=self.bg)
        assert(np.allclose(0, y))

    def test_ace_windowed_target_eq_one(self):
        '''ACE score of target for windowed background should be one.'''
        ij = (10, 10)
        y = spy.ace(self.X, self.X[ij], window=(3,7), cov=self.bg.cov)
        assert(np.allclose(1, y[ij]))


def run():
    print('\n' + '-' * 72)
    print('Running target detector tests.')
    print('-' * 72)
    for T in [MatchedFilterTest, RXTest, ACETest]:
        T().run()

if __name__ == '__main__':
    from spectral.tests.run import parse_args, reset_stats, print_summary
    parse_args()
    reset_stats()
    run()
    print_summary()