File: memmap.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 (171 lines) | stat: -rw-r--r-- 5,733 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
'''
Runs unit tests of image file interfaces using numpy memmaps.

The unit tests in this module assume the example file "92AV3C.lan" is in the
spectral data path.  After the file is opened it is saved in various formats
(different combinations of byte order, interleave, and data type) and for each
file written, the memmap interfaces are tested.

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

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

from __future__ import absolute_import, division, print_function, unicode_literals

import numpy as np
from numpy.testing import assert_almost_equal

import spectral as spy
from spectral.io.spyfile import find_file_path, FileNotFoundError
from spectral.tests import testdir
from spectral.tests.spytest import SpyTest

class SpyFileMemmapTest(SpyTest):
    '''Tests that SpyFile memmap interfaces read and write properly.'''
    def __init__(self, file, datum, value, src_inter):
        '''
        Arguments:

            `file` (str or `SpyFile`):

                The SpyFile to be tested.  This can be either the name of the
                file or a SpyFile object that has already been opened.

            `datum` (3-tuple of ints):

                (i, j, k) are the row, column and band of the datum to be
                tested. 'i' and 'j' should be at least 10 pixels away from the
                edge of the associated image and `k` should have at least 10
                bands above and below it in the image.

            `value` (int or float):

                The scalar value associated with location (i, j, k) in
                the image.

            `src_inter` (str):

                Interleave with which to save the source file
        '''
        self.file = file
        self.datum = tuple(datum)
        self.value = value
        self.src_inter = src_inter

    def setup(self):
        self.create_test_image_file()

    def create_test_image_file(self):
        import os
        import spectral
        img = spy.open_image(self.file)
        fname = os.path.join(testdir, 'memmap_test_%s.hdr' % self.src_inter)
        spy.envi.save_image(fname,
                            img,
                            dtype = img.dtype,
                            interleave = self.src_inter,
                            force=True)
        self.image = spy.open_image(fname)
        
    def test_spyfile_has_memmap(self):
        assert(self.image.using_memmap == True)

    def test_bip_memmap_read(self):
        self.create_test_image_file()
        (i, j, k) = self.datum
        mm = self.image.open_memmap(interleave='bip')
        assert_almost_equal(mm[i, j, k], self.value)

    def test_bil_memmap_read(self):
        self.create_test_image_file()
        (i, j, k) = self.datum
        mm = self.image.open_memmap(interleave='bil')
        assert_almost_equal(mm[i, k, j], self.value)

    def test_bsq_memmap_read(self):
        self.create_test_image_file()
        (i, j, k) = self.datum
        mm = self.image.open_memmap(interleave='bsq')
        assert_almost_equal(mm[k, i, j], self.value)

    def test_bip_memmap_write(self):
        from spectral import open_image
        self.create_test_image_file()
        (i, j, k) = self.datum
        mm = self.image.open_memmap(interleave='bip', writable=True)
        mm[i, j, k] = 2 * self.value
        mm.flush()
        assert_almost_equal(self.image.open_memmap()[i, j, k], 2 * self.value)

    def test_bil_memmap_write(self):
        from spectral import open_image
        self.create_test_image_file()
        (i, j, k) = self.datum
        mm = self.image.open_memmap(interleave='bil', writable=True)
        mm[i, k, j] = 3 * self.value
        mm.flush()
        assert_almost_equal(self.image.open_memmap()[i, j, k], 3 * self.value)

    def test_bsq_memmap_write(self):
        from spectral import open_image
        self.create_test_image_file()
        (i, j, k) = self.datum
        mm = self.image.open_memmap(interleave='bsq', writable=True)
        mm[k, i, j] = 3 * self.value
        mm.flush()
        assert_almost_equal(self.image.open_memmap()[i, j, k], 3 * self.value)


class SpyFileMemmapTestSuite(object):
    def __init__(self, filename, datum, value):
        '''
        Arguments:

            `filename` (str):

                Name of the image file to be tested.

            `datum` (3-tuple of ints):

                (i, j, k) are the row, column and band of the datum  to be
                tested. 'i' and 'j' should be at least 10 pixels away from the
                edge of the associated image and `k` should have at least 10
                bands above and below it in the image.

            `value` (int or float):

                The scalar value associated with location (i, j, k) in
                the image.
        '''
        self.filename = filename
        self.datum = datum
        self.value = value

    def run(self):
        import os
        import itertools
        import spectral
        print('\n' + '-' * 72)
        print('Running memmap tests.')
        print('-' * 72)
        if not os.path.isdir(testdir):
            os.mkdir(testdir)
        interleaves = ('bil', 'bip', 'bsq')
        for inter in interleaves:
            print('Testing memmaps with %s image file.' % inter.upper())
            test = SpyFileMemmapTest(self.filename, self.datum, self.value,
                                     inter)
            test.run()


def run():
    suite = SpyFileMemmapTestSuite('92AV3C.lan', (30, 40, 50), 5420.0)
    suite.run()

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