Skip to content

Commit 3edad73

Browse files
committed
Check whether complex types are supported by numpy and ENVI [fixes #144]
1 parent 6f72c5c commit 3edad73

File tree

3 files changed

+18
-3
lines changed

3 files changed

+18
-3
lines changed

spectral/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
BIL = 1
1717
BIP = 2
1818

19+
COMPLEX_SIZES = [64, 128, 256]
20+
1921
from .utilities.errors import SpyException
2022
from .config import SpySettings, spy_colors
2123
settings = SpySettings()

spectral/io/spyfile.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,8 @@ def load(self, **kwargs):
196196
for k in list(kwargs.keys()):
197197
if k not in ('dtype', 'scale'):
198198
raise ValueError('Invalid keyword %s.' % str(k))
199-
ctypes = [np.dtype('complex{}'.format(b)).name for b in (64, 128, 256)]
199+
cnames = ['complex{}'.format(s) for s in spy.COMPLEX_SIZES]
200+
ctypes = [np.dtype(n).name for n in cnames if hasattr(np, n)]
200201
if 'dtype' in kwargs:
201202
dtype = kwargs['dtype']
202203
elif np.dtype(self.dtype).name in ctypes:

spectral/tests/spyfile.py

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,14 @@
1717
import itertools
1818
import numpy as np
1919
import os
20-
20+
import warnings
2121

2222
import spectral as spy
2323
from spectral.io.spyfile import find_file_path, FileNotFoundError, SpyFile
2424
from spectral.tests import testdir
2525
from spectral.tests.spytest import SpyTest
2626

27+
ENVI_COMPLEX_TEST_SIZES = [64, 28]
2728

2829
assert_almost_equal = np.testing.assert_allclose
2930

@@ -326,7 +327,18 @@ def run():
326327
print('File "%s" not found. Skipping.' % fname)
327328

328329
# Run tests for complex data types
329-
dtypes = ['complex64', 'complex128']
330+
331+
# Only test complex sizes supported by numpy and ENVI.
332+
dtypes = []
333+
for s in [s for s in spy.COMPLEX_SIZES if s in ENVI_COMPLEX_TEST_SIZES]:
334+
t = 'complex{}'.format(s)
335+
if hasattr(np, t):
336+
dtypes.append(t)
337+
else:
338+
# This is unlikely to happen because numpy currently supports
339+
# more complex types than ENVI
340+
warnings.warn('numpy does not support {}. Skipping test.'.format(t))
341+
330342
tests = create_complex_test_files(dtypes)
331343
for (dtype, (fname, datum, value)) in zip(dtypes, tests):
332344
try:

0 commit comments

Comments
 (0)