Skip to content

Commit 516f956

Browse files
committed
Restructured code to clean up imports.
1 parent 85b73c6 commit 516f956

21 files changed

+246
-992
lines changed

spectral/io/aviris.py

Lines changed: 17 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,23 @@
1-
#########################################################################
2-
#
3-
# aviris.py - This file is part of the Spectral Python (SPy) package.
4-
#
5-
# Copyright (C) 2001-2010 Thomas Boggs
6-
#
7-
# Spectral Python is free software; you can redistribute it and/
8-
# or modify it under the terms of the GNU General Public License
9-
# as published by the Free Software Foundation; either version 2
10-
# of the License, or (at your option) any later version.
11-
#
12-
# Spectral Python is distributed in the hope that it will be useful,
13-
# but WITHOUT ANY WARRANTY; without even the implied warranty of
14-
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15-
# GNU General Public License for more details.
16-
#
17-
# You should have received a copy of the GNU General Public License
18-
# along with this software; if not, write to
19-
#
20-
# Free Software Foundation, Inc.
21-
# 59 Temple Place, Suite 330
22-
# Boston, MA 02111-1307
23-
# USA
24-
#
25-
#########################################################################
26-
#
27-
# Send comments to:
28-
# Thomas Boggs, [email protected]
29-
#
30-
311
'''
322
Functions for handling AVIRIS image files.
333
'''
344

35-
from __future__ import division, print_function, unicode_literals
5+
from __future__ import absolute_import, division, print_function, unicode_literals
6+
7+
import numpy as np
8+
import glob
9+
import os
10+
11+
import spectral as spy
12+
from ..spectral import BandInfo
13+
from ..utilities.python23 import IS_PYTHON3
14+
from .bipfile import BipFile
15+
from .spyfile import find_file_path, InvalidFileError
16+
17+
if IS_PYTHON3:
18+
import builtins
19+
else:
20+
import __builtin__ as builtins
3621

3722
def open(file, band_file=None):
3823
'''
@@ -56,13 +41,6 @@ def open(file, band_file=None):
5641
5742
spectral.io.spyfile.InvalidFileError
5843
'''
59-
import numpy as np
60-
from spectral.io.bipfile import BipFile
61-
import os
62-
import glob
63-
from .spyfile import find_file_path, InvalidFileError
64-
import spectral
65-
6644
class Params:
6745
pass
6846
p = Params()
@@ -76,7 +54,7 @@ class Params:
7654
p.nrows = int(fileSize / 275072)
7755
p.byte_order = 1
7856
p.dtype = np.dtype('i2').str
79-
if spectral.byte_order != 1:
57+
if spy.byte_order != 1:
8058
p.dtype = np.dtype(p.dtype).newbyteorder().str
8159
metadata = {'default bands': ['29', '18', '8']}
8260
p.offset = 0
@@ -104,13 +82,6 @@ def read_aviris_bands(cal_filename):
10482
10583
A :class:`spectral.BandInfo` object
10684
'''
107-
from spectral.utilities.python23 import IS_PYTHON3
108-
if IS_PYTHON3:
109-
import builtins
110-
else:
111-
import __builtin__ as builtins
112-
from spectral import BandInfo
113-
from .spyfile import find_file_path
11485
bands = BandInfo()
11586
bands.band_quantity = 'Wavelength'
11687
bands.band_unit = 'nm'

spectral/io/bilfile.py

Lines changed: 26 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,18 @@
1-
#########################################################################
2-
#
3-
# bilfile.py - This file is part of the Spectral Python (SPy) package.
4-
#
5-
# Copyright (C) 2001-2010 Thomas Boggs
6-
#
7-
# Spectral Python is free software; you can redistribute it and/
8-
# or modify it under the terms of the GNU General Public License
9-
# as published by the Free Software Foundation; either version 2
10-
# of the License, or (at your option) any later version.
11-
#
12-
# Spectral Python is distributed in the hope that it will be useful,
13-
# but WITHOUT ANY WARRANTY; without even the implied warranty of
14-
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15-
# GNU General Public License for more details.
16-
#
17-
# You should have received a copy of the GNU General Public License
18-
# along with this software; if not, write to
19-
#
20-
# Free Software Foundation, Inc.
21-
# 59 Temple Place, Suite 330
22-
# Boston, MA 02111-1307
23-
# USA
24-
#
25-
#########################################################################
26-
#
27-
# Send comments to:
28-
# Thomas Boggs, [email protected]
29-
#
30-
311
'''
32-
Tools for handling files that are band interleaved by line (BIL).
2+
Code for handling files that are band interleaved by line (BIL).
333
'''
344

35-
from __future__ import division, print_function, unicode_literals
5+
from __future__ import absolute_import, division, print_function, unicode_literals
366

7+
import array
378
import logging
389
import numpy as np
10+
import os
11+
import sys
12+
13+
import spectral as spy
14+
from ..utilities.python23 import typecode, tobytes, frombytes
3915
from .spyfile import SpyFile, MemmapFile
40-
from spectral.utilities.python23 import typecode, tobytes, frombytes
4116

4217
byte_typecode = typecode('b')
4318

@@ -49,17 +24,14 @@ class BilFile(SpyFile, MemmapFile):
4924
'''
5025

5126
def __init__(self, params, metadata=None):
52-
import spectral
53-
self.interleave = spectral.BIL
27+
self.interleave = spy.BIL
5428
if metadata is None:
5529
metadata = {}
5630
SpyFile.__init__(self, params, metadata)
5731

5832
self._memmap = self._open_memmap('r')
5933

6034
def _open_memmap(self, mode):
61-
import os
62-
import sys
6335
logger = logging.getLogger('spectral')
6436
if (os.path.getsize(self.filename) < sys.maxsize):
6537
try:
@@ -93,17 +65,13 @@ def read_band(self, band, use_memmap=True):
9365
9466
An `MxN` array of values for the specified band.
9567
'''
96-
97-
from array import array
98-
import numpy
99-
10068
if self._memmap is not None and use_memmap is True:
10169
data = np.array(self._memmap[:, band, :])
10270
if self.scale_factor != 1:
10371
data = data / float(self.scale_factor)
10472
return data
10573

106-
vals = array(byte_typecode)
74+
vals = array.array(byte_typecode)
10775
offset = self.offset + band * self.sample_size * self.ncols
10876

10977
f = self.fid
@@ -114,7 +82,7 @@ def read_band(self, band, use_memmap=True):
11482
self.ncols, 0)
11583
vals.fromfile(f, self.ncols * self.sample_size)
11684

117-
arr = numpy.fromstring(tobytes(vals), dtype=self.dtype)
85+
arr = np.fromstring(tobytes(vals), dtype=self.dtype)
11886
arr = arr.reshape((self.nrows, self.ncols))
11987

12088
if self.scale_factor != 1:
@@ -144,10 +112,6 @@ def read_bands(self, bands, use_memmap=True):
144112
are the number of rows & columns in the image and `L` equals
145113
len(`bands`).
146114
'''
147-
148-
from array import array
149-
import numpy
150-
151115
if self._memmap is not None and use_memmap is True:
152116
data = np.array(self._memmap[:, bands, :]).transpose((0, 2, 1))
153117
if self.scale_factor != 1:
@@ -156,10 +120,10 @@ def read_bands(self, bands, use_memmap=True):
156120

157121
f = self.fid
158122

159-
arr = numpy.empty((self.nrows, self.ncols, len(bands)), self.dtype)
123+
arr = np.empty((self.nrows, self.ncols, len(bands)), self.dtype)
160124

161125
for i in range(self.nrows):
162-
vals = array(byte_typecode)
126+
vals = array.array(byte_typecode)
163127
row_offset = self.offset + i * (self.sample_size * self.nbands *
164128
self.ncols)
165129

@@ -168,7 +132,7 @@ def read_bands(self, bands, use_memmap=True):
168132
f.seek(row_offset + bands[j] * self.sample_size * self.ncols, 0)
169133
vals.fromfile(f, self.ncols * self.sample_size)
170134

171-
frame = numpy.fromstring(tobytes(vals), dtype=self.dtype)
135+
frame = np.fromstring(tobytes(vals), dtype=self.dtype)
172136
arr[i, :, :] = frame.reshape((len(bands), self.ncols)).transpose()
173137

174138
if self.scale_factor != 1:
@@ -197,17 +161,13 @@ def read_pixel(self, row, col, use_memmap=True):
197161
198162
A length-`B` array, where `B` is the number of image bands.
199163
'''
200-
201-
from array import array
202-
import numpy
203-
204164
if self._memmap is not None and use_memmap is True:
205165
data = np.array(self._memmap[row, :, col])
206166
if self.scale_factor != 1:
207167
data = data / float(self.scale_factor)
208168
return data
209169

210-
vals = array(byte_typecode)
170+
vals = array.array(byte_typecode)
211171
delta = self.sample_size * (self.nbands - 1)
212172
offset = self.offset + row * self.nbands * self.ncols \
213173
* self.sample_size + col * self.sample_size
@@ -220,7 +180,7 @@ def read_pixel(self, row, col, use_memmap=True):
220180
f.seek(offset + i * sample_size * ncols, 0)
221181
vals.fromfile(f, sample_size)
222182

223-
pixel = numpy.fromstring(tobytes(vals), dtype=self.dtype)
183+
pixel = np.fromstring(tobytes(vals), dtype=self.dtype)
224184

225185
if self.scale_factor != 1:
226186
return pixel / float(self.scale_factor)
@@ -258,18 +218,14 @@ def read_subregion(self, row_bounds, col_bounds, bands=None,
258218
259219
An `MxNxL` array.
260220
'''
261-
262-
from array import array
263-
import numpy
264-
265221
if self._memmap is not None and use_memmap is True:
266222
if bands is None:
267223
data = np.array(self._memmap[row_bounds[0]: row_bounds[1], :,
268224
col_bounds[0]: col_bounds[1]])
269225
else:
270-
data = np.array(
271-
self._memmap[row_bounds[0]: row_bounds[1], bands,
272-
col_bounds[0]: col_bounds[1]])
226+
data = np.array(self._memmap[row_bounds[0]: row_bounds[1],
227+
bands,
228+
col_bounds[0]: col_bounds[1]])
273229
data = data.transpose((0, 2, 1))
274230
if self.scale_factor != 1:
275231
data = data / float(self.scale_factor)
@@ -288,7 +244,7 @@ def read_subregion(self, row_bounds, col_bounds, bands=None,
288244
# Read all bands.
289245
bands = list(range(self.nbands))
290246

291-
arr = numpy.empty((nSubRows, nSubCols, len(bands)), self.dtype)
247+
arr = np.empty((nSubRows, nSubCols, len(bands)), self.dtype)
292248

293249
offset = self.offset
294250
ncols = self.ncols
@@ -299,13 +255,13 @@ def read_subregion(self, row_bounds, col_bounds, bands=None,
299255
for i in range(row_bounds[0], row_bounds[1]):
300256
f.seek(offset + i * d_row + colStartPos, 0)
301257
rowPos = f.tell()
302-
vals = array(byte_typecode)
258+
vals = array.array(byte_typecode)
303259
for j in bands:
304260
f.seek(rowPos + j * ncols * sampleSize, 0)
305261
vals.fromfile(f, nSubCols * sampleSize)
306-
subArray = numpy.fromstring(tobytes(vals), dtype=self.dtype)
262+
subArray = np.fromstring(tobytes(vals), dtype=self.dtype)
307263
subArray = subArray.reshape((nSubBands, nSubCols))
308-
arr[i - row_bounds[0], :, :] = numpy.transpose(subArray)
264+
arr[i - row_bounds[0], :, :] = np.transpose(subArray)
309265

310266
if self.scale_factor != 1:
311267
return arr / float(self.scale_factor)
@@ -343,10 +299,6 @@ def read_subimage(self, rows, cols, bands=None, use_memmap=False):
343299
An `MxNxL` array, where `M` = len(`rows`), `N` = len(`cols`),
344300
and `L` = len(bands) (or # of image bands if `bands` == None).
345301
'''
346-
347-
from array import array
348-
import numpy
349-
350302
if self._memmap is not None and use_memmap is True:
351303
if bands is None:
352304
data = np.array(self._memmap.take(rows, 0).take(cols, 2))
@@ -373,10 +325,10 @@ def read_subimage(self, rows, cols, bands=None, use_memmap=False):
373325
bands = list(range(self.nbands))
374326
nSubBands = len(bands)
375327

376-
arr = numpy.empty((nSubRows, nSubCols, nSubBands), self.dtype)
328+
arr = np.empty((nSubRows, nSubCols, nSubBands), self.dtype)
377329

378330
offset = self.offset
379-
vals = array(byte_typecode)
331+
vals = array.array(byte_typecode)
380332
sample_size = self.sample_size
381333

382334
# Pixel format is BIL
@@ -388,7 +340,7 @@ def read_subimage(self, rows, cols, bands=None, use_memmap=False):
388340
j * d_col +
389341
k * d_band, 0)
390342
vals.fromfile(f, sample_size)
391-
subArray = numpy.fromstring(tobytes(vals), dtype=self.dtype)
343+
subArray = np.fromstring(tobytes(vals), dtype=self.dtype)
392344
subArray = subArray.reshape((nSubRows, nSubCols, nSubBands))
393345

394346
if self.scale_factor != 1:
@@ -413,8 +365,6 @@ def read_datum(self, i, j, k, use_memmap=True):
413365
Using this function is not an efficient way to iterate over bands or
414366
pixels. For such cases, use readBands or readPixel instead.
415367
'''
416-
import array
417-
418368
if self._memmap is not None and use_memmap is True:
419369
datum = self._memmap[i, k, j]
420370
if self.scale_factor != 1:

0 commit comments

Comments
 (0)