FIX: util.montage2d 'normalize' becomes 'rescale_intensity' and defaults to False

This commit is contained in:
Nicolas Pinto
2012-02-14 19:36:07 -05:00
parent c715adf1c0
commit 68f8c68c3f
2 changed files with 31 additions and 11 deletions
+9 -11
View File
@@ -1,11 +1,12 @@
__all__ = ['montage2d']
import numpy as np
from .. import exposure
EPSILON = 1e-6
def montage2d(arr_in, fill='mean', normalize=True):
def montage2d(arr_in, fill='mean', rescale_intensity=False):
"""Create a 2-dimensional 'montage' from a 3-dimensional input array
representing an ensemble of equally shaped 2-dimensional images.
@@ -35,8 +36,8 @@ def montage2d(arr_in, fill='mean', normalize=True):
How to fill the 2-dimensional output array when sqrt(n_images)
is not an integer. If 'mean' is chosen, then fill = arr_in.mean().
normalize: bool, optional
Whether to normalize each image with zero-mean, unit-variance.
rescale_intensity: bool, optional
Whether to rescale the intensity of each image to [0, 1].
Returns
-------
@@ -73,14 +74,11 @@ def montage2d(arr_in, fill='mean', normalize=True):
n_images, height, width = arr_in.shape
# -- normalize if necessary
if normalize:
arr_in = arr_in.T
arr_in -= arr_in.mean(0)
astd = arr_in.std(0)
astd[astd < EPSILON] = 1
arr_in /= astd
arr_in = arr_in.T
# -- rescale intensity if necessary
if rescale_intensity:
for i in xrange(n_images):
arr_in[i] = exposure.rescale_intensity(
arr_in[i], out_range=(0.0, 1.0))
# -- determine alpha
alpha = int(np.ceil(np.sqrt(n_images)))
+22
View File
@@ -53,6 +53,28 @@ def test_shape():
assert_equal(arr_out.shape, (alpha * height, alpha * width))
def test_rescale_intensity():
n_images = 4
height, width = 3, 3
arr_in = np.arange(n_images * height * width, dtype=np.float32)
arr_in = arr_in.reshape(n_images, height, width)
arr_out = montage2d(arr_in, rescale_intensity=True)
gt = np.array(
[[ 0. , 0.125, 0.25 , 0. , 0.125, 0.25 ],
[ 0.375, 0.5 , 0.625, 0.375, 0.5 , 0.625],
[ 0.75 , 0.875, 1. , 0.75 , 0.875, 1. ],
[ 0. , 0.125, 0.25 , 0. , 0.125, 0.25 ],
[ 0.375, 0.5 , 0.625, 0.375, 0.5 , 0.625],
[ 0.75 , 0.875, 1. , 0.75 , 0.875, 1. ]]
)
assert_equal(arr_out.min(), 0.0)
assert_equal(arr_out.max(), 1.0)
assert_array_equal(arr_out, gt)
@raises(AssertionError)
def test_error_ndim():
arr_error = np.random.randn(1, 2, 3, 4)