Skip to content

Commit 71b2b2b

Browse files
committed
tests passing, cov 65%
1 parent 0fadf71 commit 71b2b2b

File tree

2 files changed

+33
-42
lines changed

2 files changed

+33
-42
lines changed

tests/test_unmap.py

Lines changed: 24 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from io import Strinunmap
1+
from io import StringIO
22

33
from PIL import Image
44
import numpy as np
@@ -12,49 +12,47 @@ def kernel(sizex, sizey):
1212
g = np.exp(-0.333*(x**2/float(sizex)+y**2/float(sizey)))
1313
return g / g.sum()
1414

15-
def make_map(n, nx=64, ny=64, kernel_size=None, seed=None):
16-
imgs = []
17-
for i in range(n):
18-
rng = np.random.RandomState(seed=seed)
19-
if kernel_size is None:
20-
kx, ky = (7, 7)
21-
else:
22-
kx, ky = kernel_size
23-
f = kernel(kx, ky)
24-
z = rng.rand(nx+2*kx, ny+2*ky)
25-
26-
z = scipy.signal.convolve(z, f, mode='valid')
27-
z = (z - z.min())/(z.max() - z.min())
28-
imgs.append(z)
15+
def make_map(nx=64, ny=64, kernel_size=None, seed=None):
16+
rng = np.random.RandomState(seed=seed)
17+
if kernel_size is None:
18+
kx, ky = (7, 7)
19+
else:
20+
kx, ky = kernel_size
21+
f = kernel(kx, ky)
22+
z = rng.rand(nx+2*kx, ny+2*ky)
23+
z = scipy.signal.convolve(z, f, mode='valid')
24+
z = (z - z.min())/(z.max() - z.min())
2925

30-
return np.stack(imgs)
26+
return z
3127

3228
def get_cbar(cmap, n=32, pixels=5):
33-
arr = cm.get_cmap()(np.arange(0, n, 1)/(n-1))[..., :3]
29+
arr = cm.get_cmap(cmap)(np.arange(0, n, 1)/(n-1))[..., :3]
3430
return np.tile(arr, (pixels, 1)).reshape(pixels, n, 3)
3531

3632
def make_image_and_cbar(cmap, seed=None):
37-
data = np.squeeze(make_map(1))
38-
cmap = cm.get_cmap(cmap)
39-
cbar = get_cbar(cmap, 128, pixels=5)
40-
return cmap(data)[..., :3], cbar
33+
data = make_map()
34+
c = cm.get_cmap(cmap)
35+
return c(data)[..., :3], get_cbar(cmap, 256, pixels=5)
4136

4237

4338
def test_unmap():
4439
"""Test the basics.
4540
"""
4641
img, cbar = make_image_and_cbar('jet', seed=42)
4742

43+
# Add some white pixels as 'background'; will become NaNs.
44+
img[:3, :3, :] = 1
45+
4846
# Using a cbar image.
49-
data = unmap.unmap(img, cmap=cbar, threshold=0.05, vrange=(100, 200), background=(1, 1, 1), levels=256)
50-
assert data.shape == (231, 231)
47+
data = unmap.unmap(img, cmap=cbar, vrange=(100, 200))
48+
assert data.shape == (64, 64)
5149
assert np.nanmax(data) == 200
52-
assert np.nanmean(data) - 150.60721435278933 < 1e-6
50+
assert np.nanmean(data) - 161.4341107536765 < 1e-6
5351
assert np.any(np.isnan(data))
5452

5553
# Using a matplotlib cmap.
56-
data = unmap.unmap(img, cmap='jet', threshold=0.05, vrange=(200, 300))
57-
assert np.nanmean(data) - 250.6121076492208 < 1e-6
54+
data = unmap.unmap(img, cmap='jet', vrange=(200, 300))
55+
assert np.nanmean(data) - 261.4341107536765 < 1e-6
5856

5957

6058
def is_greyscale():

unmap/unmap.py

Lines changed: 9 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -96,11 +96,9 @@ def remove_hillshade(img):
9696
return hsv_to_rgb(hsv_im), hillshade
9797

9898

99-
def crop_out(arr, rect, force_landscape=False, reduce=None, reverse='auto'):
99+
def crop_out(arr, rect):
100100
"""
101101
Crop pixels out of a larger image.
102-
103-
If reduce is a function, it is applied across the short axis.
104102
"""
105103
l, t, r, b = rect
106104
assert (r >= l) and (b >= t), "Right and bottom must be greater than left and top respectively; the origin is at the top left of the image."
@@ -109,18 +107,6 @@ def crop_out(arr, rect, force_landscape=False, reduce=None, reverse='auto'):
109107
b += 1 if t == b else 0
110108
pixels = arr[t:b, l:r]
111109

112-
if force_landscape and (r - l < b - t):
113-
# Then it is a vertical rectangle.
114-
pixels = np.swapaxes(pixels, 0, 1)
115-
if reverse == 'auto':
116-
reverse = True
117-
118-
if reduce is not None:
119-
pixels = reduce(pixels, axis=0)
120-
121-
if reverse:
122-
pixels = pixels[::-1]
123-
124110
return pixels
125111

126112

@@ -162,7 +148,14 @@ def get_cmap(cmap, arr=None, levels=256, quantize=False):
162148
cmap = crop_out(arr, cmap, force_landscape=True, reduce=np.mean)
163149

164150
if quantize:
165-
cmap, _ = kmeans(cmap, levels)
151+
cmap, _ = kmeans(cmap, levels)
152+
153+
if (cmap.ndim == 3) and (cmap.shape[0] > cmap.shape[1]):
154+
# Then it is a vertical rectangle.
155+
cmap = np.swapaxes(cmap, 0, 1)
156+
157+
if (cmap.ndim == 3):
158+
cmap = np.mean(cmap, axis=0)
166159

167160
if (cmap.ndim == 2) and (cmap.shape[1] in [3, 4]):
168161
cmap = LinearSegmentedColormap.from_list('', cmap[:, :3], N=levels)

0 commit comments

Comments
 (0)