Skip to content

Commit c04b2f4

Browse files
authored
Merge pull request #16 from ksunden/subsample
Dynamic subsampling example
2 parents de25dd1 + 45fd7b4 commit c04b2f4

File tree

2 files changed

+78
-0
lines changed

2 files changed

+78
-0
lines changed

examples/subsample.py

+77
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
"""
2+
====================
3+
Dynamic Downsampling
4+
====================
5+
6+
Generates a large image with three levels of detail.
7+
8+
When zoomed out, appears as a difference of 2D Gaussians.
9+
At medium zoom, a diagonal sinusoidal pattern is apparent.
10+
When zoomed in close, noise is visible.
11+
12+
The image is dynamically subsampled using a local mean which hides the finer details.
13+
"""
14+
15+
from typing import Tuple, Dict, Any, Union
16+
17+
import matplotlib as mpl
18+
import matplotlib.pyplot as plt
19+
from matplotlib.colors import Normalize
20+
21+
import numpy as np
22+
23+
from data_prototype.wrappers import ImageWrapper
24+
from data_prototype.containers import _Transform
25+
26+
from skimage.transform import downscale_local_mean
27+
28+
29+
x = y = np.linspace(-3, 3, 3000)
30+
X, Y = np.meshgrid(x, y)
31+
Z1 = np.exp(-(X**2) - Y**2) + 0.08 * np.sin(50 * (X + Y))
32+
Z2 = np.exp(-((X - 1) ** 2) - (Y - 1) ** 2)
33+
Z = (Z1 - Z2) * 2
34+
35+
Z += np.random.random(Z.shape) - 0.5
36+
37+
38+
class Subsample:
39+
def describe(self):
40+
return {
41+
"xextent": Desc([2], float),
42+
"yextent": Desc([2], float),
43+
"image": Desc([], float),
44+
}
45+
46+
def query(
47+
self,
48+
transform: _Transform,
49+
size: Tuple[int, int],
50+
) -> Tuple[Dict[str, Any], Union[str, int]]:
51+
(x1, y1), (x2, y2) = transform.transform([[0, 0], [1, 1]])
52+
53+
xi1 = np.argmin(np.abs(x - x1))
54+
yi1 = np.argmin(np.abs(y - y1))
55+
xi2 = np.argmin(np.abs(x - x2))
56+
yi2 = np.argmin(np.abs(y - y2))
57+
58+
xscale = int(np.ceil((xi2 - xi1) / 50))
59+
yscale = int(np.ceil((yi2 - yi1) / 50))
60+
61+
return {
62+
"xextent": [x1, x2],
63+
"yextent": [y1, y2],
64+
"image": downscale_local_mean(Z[xi1:xi2, yi1:yi2], (xscale, yscale)),
65+
}, hash((x1, x2, y1, y2))
66+
67+
68+
sub = Subsample()
69+
cmap = mpl.colormaps["coolwarm"]
70+
norm = Normalize(-2.2, 2.2)
71+
im = ImageWrapper(sub, {"image": lambda image: cmap(norm(image))})
72+
73+
fig, ax = plt.subplots()
74+
ax.add_artist(im)
75+
ax.set_xlim(-3, 3)
76+
ax.set_ylim(-3, 3)
77+
plt.show()

requirements-doc.txt

+1
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,4 @@ sphinx
66
sphinx-copybutton
77
sphinx-gallery
88
ipython
9+
scikit-image

0 commit comments

Comments
 (0)