Skip to content

Commit

Permalink
Added docstring. Changes made in tests and image.py.
Browse files Browse the repository at this point in the history
  • Loading branch information
bvnayak committed Apr 1, 2016
1 parent b2c6ba6 commit ffb1115
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 28 deletions.
37 changes: 15 additions & 22 deletions planetaryimage/image.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,28 +20,21 @@ def open(cls, filename):
Name of file to read as an image file. This file may be gzip
(``.gz``) or bzip2 (``.bz2``) compressed.
"""
if isinstance(filename, numpy.ndarray):
error_msg = (
'A file like object is expected for stream. '
'Use PDS3Image(numpy_array) to create a PDS3Image object.'
)
raise TypeError(error_msg)
if filename.endswith('.gz'):
fp = gzip.open(filename, 'rb')
try:
return cls(fp, filename, compression='gz')
finally:
fp.close()
elif filename.endswith('.bz2'):
fp = bz2.BZ2File(filename, 'rb')
try:
return cls(fp, filename, compression='bz2')
finally:
fp.close()
else:
if filename.endswith('.gz'):
fp = gzip.open(filename, 'rb')
try:
return cls(fp, filename, compression='gz')
finally:
fp.close()
elif filename.endswith('.bz2'):
fp = bz2.BZ2File(filename, 'rb')
try:
return cls(fp, filename, compression='bz2')
finally:
fp.close()
else:
with open(filename, 'rb') as fp:
return cls(fp, filename)
with open(filename, 'rb') as fp:
return cls(fp, filename)

def __init__(self, stream_string_or_array, filename=None, compression=None):
"""Create an Image object.
Expand All @@ -66,7 +59,7 @@ def __init__(self, stream_string_or_array, filename=None, compression=None):
raise TypeError(error_msg % type(self).__name__)

if isinstance(stream_string_or_array, numpy.ndarray):
self.filename = 'numpy_array'
self.filename = None
self.compression = None
self.data = stream_string_or_array
self.label = self._create_label(stream_string_or_array)
Expand Down
37 changes: 37 additions & 0 deletions planetaryimage/pds3image.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,21 @@ class PDS3Image(PlanetaryImage):
}

def _save(self, file_to_write, overwrite):
"""Save PDS3Image object as PDS3 file.
Parameters
----------
Overwrite: Use this keyword to save image with same filename.
Examples
--------
>>> from planetaryimage import PDS3Image
>>> image = PDS3Image.open('tests/mission_data/2p129641989eth0361p2600r8m1.img')
>>> image.save('temp.IMG')
>>> image.save('temp.IMG', overwrite=True)
"""
if overwrite:
file_to_write = self.filename
elif os.path.isfile(file_to_write):
Expand Down Expand Up @@ -167,6 +182,15 @@ def _save(self, file_to_write, overwrite):
stream.close()

def _create_label(self, array):
"""Create sample PDS3 label for NumPy Array.
It is called by 'image.py' to create PDS3Image object
from Numpy Array.
Examples
--------
>>> self.label = _create_label(array)
"""
if len(array.shape) == 3:
bands = array.shape[0]
lines = array.shape[1]
Expand Down Expand Up @@ -197,6 +221,19 @@ def _create_label(self, array):
return self._update_label(label_module, array)

def _update_label(self, label, array):
"""Update PDS3 label for NumPy Array.
It is called by '_create_label' to update label values.
e.g.
- ^IMAGE, RECORD_BYTES
- STANDARD_DEVIATION
- MAXIMUM, MINIMUM
- MEDIAN, MEAN
Examples
--------
>>> self.label = self._update_label(label, array)
"""
maximum = float(numpy.max(array))
mean = float(numpy.mean(array))
median = float(numpy.median(array))
Expand Down
10 changes: 4 additions & 6 deletions tests/test_pds3file.py
Original file line number Diff line number Diff line change
Expand Up @@ -183,9 +183,8 @@ def test_image_save_float_to_int():


def test_numpy_array_save_i2():
image = PDS3Image.open(filename)
array = numpy.arange(100, dtype='>i2')
array = array.reshape(10, 10)
array = array.reshape(1, 10, 10)
temp = PDS3Image(array)
temp.save('Temp_Image.IMG')
image_temp = PDS3Image.open('Temp_Image.IMG')
Expand All @@ -196,14 +195,13 @@ def test_numpy_array_save_i2():
assert image_temp.dtype == '>i2'
assert image_temp.shape == (1, 10, 10)
assert image_temp.size == 100
assert_almost_equal(image_temp.data, image.data)
assert_almost_equal(image_temp.data, array)
os.remove('Temp_Image.IMG')


def test_numpy_array_save_f4():
image = PDS3Image.open(filename_float)
array = numpy.arange(100)
array = array.reshape(10, 10)
array = array.reshape(1, 10, 10)
array = array * 1.5
array = array.astype('>f4')
temp = PDS3Image(array)
Expand All @@ -216,7 +214,7 @@ def test_numpy_array_save_f4():
assert image_temp.dtype == '>f4'
assert image_temp.shape == (1, 10, 10)
assert image_temp.size == 100
assert_almost_equal(image_temp.data, image.data)
assert_almost_equal(image_temp.data, array)
os.remove('Temp_Image.IMG')


Expand Down

0 comments on commit ffb1115

Please sign in to comment.