Skip to content

Commit

Permalink
Merge pull request #144 from aaren/import-images
Browse files Browse the repository at this point in the history
Add importable images
  • Loading branch information
grlee77 committed Dec 18, 2015
2 parents f177245 + e8d244d commit b21ff46
Show file tree
Hide file tree
Showing 17 changed files with 228 additions and 18 deletions.
Binary file removed demo/data/aero.png
Binary file not shown.
4 changes: 2 additions & 2 deletions demo/dwt2_dwtn_image.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@

import numpy as np
import matplotlib.pyplot as plt
from scipy import ndimage

import pywt
import pywt.data


# Load image
original = ndimage.imread('data/aero.png', mode='L')
original = pywt.data.aero()

# Wavelet transform of image, and plot approximation and details
titles = ['Approximation', ' Horizontal detail',
Expand Down
5 changes: 2 additions & 3 deletions demo/dwt_signal_decomposition.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-

import os

import numpy as np
import matplotlib.pyplot as plt

import pywt
import pywt.data


ecg = np.load(os.path.join('data', 'ecg.npy'))
ecg = pywt.data.ecg()

data1 = np.concatenate((np.arange(1, 400),
np.arange(398, 600),
Expand Down
6 changes: 3 additions & 3 deletions demo/dwt_swt_show_coeffs.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-

import os

import numpy as np
import matplotlib.pyplot as plt

import pywt
import pywt.data


ecg = pywt.data.ecg()

ecg = np.load(os.path.join('data', 'ecg.npy'))
data1 = np.concatenate((np.arange(1, 400),
np.arange(398, 600),
np.arange(601, 1024)))
Expand Down
7 changes: 2 additions & 5 deletions demo/swt2.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,13 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-

import numpy as np
import matplotlib.pyplot as plt
from PIL import Image

import pywt
import pywt.data


im = Image.open("data/aero.png").convert('L')
arr = np.fromstring(im.tostring(), np.uint8)
arr.shape = (im.size[1], im.size[0])
arr = pywt.data.aero()

plt.imshow(arr, interpolation="nearest", cmap=plt.cm.gray)

Expand Down
6 changes: 2 additions & 4 deletions demo/wp_2d.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,12 @@

import numpy as np
import matplotlib.pyplot as plt
from PIL import Image

from pywt import WaveletPacket2D
import pywt.data


im = Image.open("data/aero.png").convert('L')
arr = np.fromstring(im.tostring(), np.uint8)
arr.shape = (im.size[1], im.size[0])
arr = pywt.data.aero()

wp2 = WaveletPacket2D(arr, 'db2', 'symmetric', maxlevel=2)

Expand Down
4 changes: 3 additions & 1 deletion demo/wp_visualize_coeffs_distribution.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,11 @@
import matplotlib.pyplot as plt

from pywt import WaveletPacket
import pywt.data


ecg = np.load(os.path.join('data', 'ecg.npy'))
ecg = pywt.data.ecg()

wp = WaveletPacket(ecg, 'sym5', maxlevel=4)

fig = plt.figure()
Expand Down
1 change: 1 addition & 0 deletions pywt/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
from ._thresholding import *
from ._wavelet_packets import *

from . import data

__all__ = [s for s in dir() if not s.startswith('_')]
try:
Expand Down
1 change: 1 addition & 0 deletions pywt/data/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from ._readers import ascent, aero, ecg, camera
135 changes: 135 additions & 0 deletions pywt/data/_readers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
import os

import numpy as np


def ascent():
"""
Get an 8-bit grayscale bit-depth, 512 x 512 derived image for
easy use in demos
The image is derived from accent-to-the-top.jpg at
http://www.public-domain-image.com/people-public-domain-images-pictures/
Parameters
----------
None
Returns
-------
ascent : ndarray
convenient image to use for testing and demonstration
Examples
--------
>>> import pywt.data
>>> ascent = pywt.data.ascent()
>>> ascent.shape
(512, 512)
>>> ascent.max()
255
>>> import matplotlib.pyplot as plt
>>> plt.gray()
>>> plt.imshow(ascent)
>>> plt.show()
"""
fname = os.path.join(os.path.dirname(__file__), 'ascent.npz')
ascent = np.load(fname)['data']
return ascent


def aero():
"""
Get an 8-bit grayscale bit-depth, 512 x 512 derived image for
easy use in demos
Parameters
----------
None
Returns
-------
aero : ndarray
convenient image to use for testing and demonstration
Examples
--------
>>> import pywt.data
>>> aero = pywt.data.ascent()
>>> aero.shape
(512, 512)
>>> aero.max()
255
>>> import matplotlib.pyplot as plt
>>> plt.gray()
>>> plt.imshow(aero)
>>> plt.show()
"""
fname = os.path.join(os.path.dirname(__file__), 'aero.npz')
aero = np.load(fname)['data']
return aero


def camera():
"""
Get an 8-bit grayscale bit-depth, 512 x 512 derived image for
easy use in demos
Parameters
----------
None
Returns
-------
camera : ndarray
convenient image to use for testing and demonstration
Examples
--------
>>> import pywt.data
>>> camera = pywt.data.ascent()
>>> camera.shape
(512, 512)
>>> import matplotlib.pyplot as plt
>>> plt.gray()
>>> plt.imshow(camera)
>>> plt.show()
"""
fname = os.path.join(os.path.dirname(__file__), 'camera.npz')
camera = np.load(fname)['data']
return camera


def ecg():
"""
Get 1024 points of an ECG timeseries.
Parameters
----------
None
Returns
-------
ecg : ndarray
convenient timeseries to use for testing and demonstration
Examples
--------
>>> import pywt.data
>>> ecg = pywt.data.ecg()
>>> ecg.shape
(1024,)
>>> import matplotlib.pyplot as plt
>>> plt.plot(ecg)
>>> plt.show()
"""
fname = os.path.join(os.path.dirname(__file__), 'ecg.npy')
ecg = np.load(fname)
return ecg
Binary file added pywt/data/aero.npz
Binary file not shown.
Binary file added pywt/data/ascent.npz
Binary file not shown.
Binary file added pywt/data/camera.npz
Binary file not shown.
32 changes: 32 additions & 0 deletions pywt/data/create_dat.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#!/usr/bin/env python

"""Helper script for creating image .dat files by numpy.save
Usage:
python create_dat.py <name of image file> <name of dat file>
Example (to create aero.dat):
python create_dat.py aero.png aero.dat
Requires Scipy and PIL.
"""

from __future__ import print_function

import sys

import numpy as np
from scipy.misc import imread

if len(sys.argv) != 3:
print(__doc__)
exit()

image_fname = sys.argv[1]
dat_fname = sys.argv[2]

data = imread(image_fname)

np.savez_compressed(dat_fname, data=data)
File renamed without changes.
42 changes: 42 additions & 0 deletions pywt/tests/test_data.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import numpy as np
from numpy.testing import assert_allclose

import pywt.data


def test_data_aero():
aero = pywt.data.aero()

ref = np.array([[178, 178, 179],
[170, 173, 171],
[185, 174, 171]])

assert_allclose(aero[:3, :3], ref)


def test_data_ascent():
ascent = pywt.data.ascent()

ref = np.array([[83, 83, 83],
[82, 82, 83],
[80, 81, 83]])

assert_allclose(ascent[:3, :3], ref)


def test_data_camera():
ascent = pywt.data.camera()

ref = np.array([[156, 157, 160],
[156, 157, 159],
[158, 157, 156]])

assert_allclose(ascent[:3, :3], ref)


def test_data_ecg():
ecg = pywt.data.ecg()

ref = np.array([-86, -87, -87])

assert_allclose(ecg[:3], ref)
3 changes: 3 additions & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,9 @@ def configuration(parent_package='',top_path=None):
quiet=True)

config.add_subpackage('pywt')
config.add_subpackage('pywt/data')
config.add_data_files('pywt/data/*npz')
config.add_data_files('pywt/data/*npy')

config.get_version('pywt/version.py')
return config
Expand Down

0 comments on commit b21ff46

Please sign in to comment.