|
25 | 25 | from spectral.tests.spytest import SpyTest
|
26 | 26 |
|
27 | 27 |
|
28 |
| -def assert_almost_equal(a, b, **kwargs): |
29 |
| - if not np.allclose(a, b, **kwargs): |
30 |
| - raise Exception('NOPE') |
| 28 | +assert_almost_equal = np.testing.assert_allclose |
| 29 | + |
| 30 | +def assert_allclose (a, b, **kwargs): |
| 31 | + np.testing.assert_allclose(np.array(a), np.array(b), **kwargs) |
31 | 32 |
|
32 | 33 | class SpyFileTest(SpyTest):
|
33 | 34 | '''Tests that SpyFile methods read data correctly from files.'''
|
@@ -144,14 +145,14 @@ def test_load(self):
|
144 | 145 | data = self.image.load()
|
145 | 146 | spyf = self.image
|
146 | 147 |
|
147 |
| - load_assert = np.allclose |
| 148 | + load_assert = assert_allclose |
148 | 149 | load_assert(data[i, j, k], self.value)
|
149 | 150 | first_band = spyf[:, :, 0]
|
150 | 151 | load_assert(data[:, :, 0], first_band)
|
151 | 152 | # This is checking if different ImageArray and SpyFile indexing
|
152 | 153 | # results are the same shape, so we can't just reuse the already
|
153 | 154 | # loaded first band.
|
154 |
| - load_assert(data[:, 0, 0], spyf[:, 0, 0]) |
| 155 | + load_assert(data[:, 0, 0].squeeze(), spyf[:, 0, 0].squeeze()) |
155 | 156 | load_assert(data[0, 0, 0], spyf[0, 0, 0])
|
156 | 157 | load_assert(data[0, 0], spyf[0, 0])
|
157 | 158 | load_assert(data[-1, -1, -1], spyf[-1, -1, -1])
|
@@ -267,7 +268,7 @@ def run(self):
|
267 | 268 | os.mkdir(testdir)
|
268 | 269 | image = spy.open_image(self.filename)
|
269 | 270 | basename = os.path.join(testdir,
|
270 |
| - os.path.splitext(self.filename)[0]) |
| 271 | + os.path.splitext(os.path.split(self.filename)[-1])[0]) |
271 | 272 | interleaves = ('bil', 'bip', 'bsq')
|
272 | 273 | ends = ('big', 'little')
|
273 | 274 | cases = itertools.product(interleaves, self.dtypes, ends)
|
@@ -298,17 +299,40 @@ def run(self):
|
298 | 299 | test = SpyFileTest(testimg, self.datum, self.value)
|
299 | 300 | test.run()
|
300 | 301 |
|
| 302 | +def create_complex_test_files(dtypes): |
| 303 | + '''Create test files with complex data''' |
| 304 | + if not os.path.isdir(testdir): |
| 305 | + os.mkdir(testdir) |
| 306 | + tests = [] |
| 307 | + shape = (100, 200, 64) |
| 308 | + datum = (33, 44, 25) |
| 309 | + for t in dtypes: |
| 310 | + X = np.array(np.random.rand(*shape) + 1j * np.random.rand(*shape), |
| 311 | + dtype=t) |
| 312 | + fname = os.path.join(testdir, f'test_{t}.hdr') |
| 313 | + spy.envi.save_image(fname, X) |
| 314 | + tests.append((fname, datum, X[datum])) |
| 315 | + return tests |
301 | 316 |
|
302 | 317 | def run():
|
303 | 318 | tests = [('92AV3C.lan', (99, 99, 99), 2057.0)]
|
304 |
| -# tests = [('92AV3C.lan', (99, 99, 99), 2057.0), |
305 |
| -# ('f970619t01p02_r02_sc04.a.rfl', (99, 99, 99), 0.2311), |
306 |
| -# ('cup95eff.int.hdr', (99, 99, 33), 0.1842)] |
307 | 319 | for (fname, datum, value) in tests:
|
308 | 320 | try:
|
309 | 321 | check = find_file_path(fname)
|
310 | 322 | suite = SpyFileTestSuite(fname, datum, value,
|
311 |
| - dtypes=('i2', 'i4', 'f4', 'f8')) |
| 323 | + dtypes=('i2', 'i4', 'f4', 'f8', 'c8', 'c16')) |
| 324 | + suite.run() |
| 325 | + except FileNotFoundError: |
| 326 | + print('File "%s" not found. Skipping.' % fname) |
| 327 | + |
| 328 | + # Run tests for complex data types |
| 329 | + dtypes = ['complex64', 'complex128'] |
| 330 | + tests = create_complex_test_files(dtypes) |
| 331 | + for (dtype, (fname, datum, value)) in zip(dtypes, tests): |
| 332 | + try: |
| 333 | + check = find_file_path(fname) |
| 334 | + suite = SpyFileTestSuite(fname, datum, value, |
| 335 | + dtypes=(dtype,)) |
312 | 336 | suite.run()
|
313 | 337 | except FileNotFoundError:
|
314 | 338 | print('File "%s" not found. Skipping.' % fname)
|
|
0 commit comments