|
| 1 | +import numpy as np |
| 2 | +from matplotlib.testing.decorators import image_comparison, knownfailureif |
| 3 | +from matplotlib.delaunay.triangulate import Triangulation |
| 4 | +from matplotlib import pyplot as plt |
| 5 | +import matplotlib as mpl |
| 6 | + |
| 7 | +def constant(x, y): |
| 8 | + return np.ones(x.shape, x.dtype) |
| 9 | +constant.title = 'Constant' |
| 10 | + |
| 11 | +def xramp(x, y): |
| 12 | + return x |
| 13 | +xramp.title = 'X Ramp' |
| 14 | + |
| 15 | +def yramp(x, y): |
| 16 | + return y |
| 17 | +yramp.title = 'Y Ramp' |
| 18 | + |
| 19 | +def exponential(x, y): |
| 20 | + x = x*9 |
| 21 | + y = y*9 |
| 22 | + x1 = x+1.0 |
| 23 | + x2 = x-2.0 |
| 24 | + x4 = x-4.0 |
| 25 | + x7 = x-7.0 |
| 26 | + y1 = x+1.0 |
| 27 | + y2 = y-2.0 |
| 28 | + y3 = y-3.0 |
| 29 | + y7 = y-7.0 |
| 30 | + f = (0.75 * np.exp(-(x2*x2+y2*y2)/4.0) + |
| 31 | + 0.75 * np.exp(-x1*x1/49.0 - y1/10.0) + |
| 32 | + 0.5 * np.exp(-(x7*x7 + y3*y3)/4.0) - |
| 33 | + 0.2 * np.exp(-x4*x4 -y7*y7)) |
| 34 | + return f |
| 35 | +exponential.title = 'Exponential and Some Gaussians' |
| 36 | + |
| 37 | +def cliff(x, y): |
| 38 | + f = np.tanh(9.0*(y-x) + 1.0)/9.0 |
| 39 | + return f |
| 40 | +cliff.title = 'Cliff' |
| 41 | + |
| 42 | +def saddle(x, y): |
| 43 | + f = (1.25 + np.cos(5.4*y))/(6.0 + 6.0*(3*x-1.0)**2) |
| 44 | + return f |
| 45 | +saddle.title = 'Saddle' |
| 46 | + |
| 47 | +def gentle(x, y): |
| 48 | + f = np.exp(-5.0625*((x-0.5)**2+(y-0.5)**2))/3.0 |
| 49 | + return f |
| 50 | +gentle.title = 'Gentle Peak' |
| 51 | + |
| 52 | +def steep(x, y): |
| 53 | + f = np.exp(-20.25*((x-0.5)**2+(y-0.5)**2))/3.0 |
| 54 | + return f |
| 55 | +steep.title = 'Steep Peak' |
| 56 | + |
| 57 | +def sphere(x, y): |
| 58 | + circle = 64-81*((x-0.5)**2 + (y-0.5)**2) |
| 59 | + f = np.where(circle >= 0, np.sqrt(np.clip(circle,0,100)) - 0.5, 0.0) |
| 60 | + return f |
| 61 | +sphere.title = 'Sphere' |
| 62 | + |
| 63 | +def trig(x, y): |
| 64 | + f = 2.0*np.cos(10.0*x)*np.sin(10.0*y) + np.sin(10.0*x*y) |
| 65 | + return f |
| 66 | +trig.title = 'Cosines and Sines' |
| 67 | + |
| 68 | +def gauss(x, y): |
| 69 | + x = 5.0-10.0*x |
| 70 | + y = 5.0-10.0*y |
| 71 | + g1 = np.exp(-x*x/2) |
| 72 | + g2 = np.exp(-y*y/2) |
| 73 | + f = g1 + 0.75*g2*(1 + g1) |
| 74 | + return f |
| 75 | +gauss.title = 'Gaussian Peak and Gaussian Ridges' |
| 76 | + |
| 77 | +def cloverleaf(x, y): |
| 78 | + ex = np.exp((10.0-20.0*x)/3.0) |
| 79 | + ey = np.exp((10.0-20.0*y)/3.0) |
| 80 | + logitx = 1.0/(1.0+ex) |
| 81 | + logity = 1.0/(1.0+ey) |
| 82 | + f = (((20.0/3.0)**3 * ex*ey)**2 * (logitx*logity)**5 * |
| 83 | + (ex-2.0*logitx)*(ey-2.0*logity)) |
| 84 | + return f |
| 85 | +cloverleaf.title = 'Cloverleaf' |
| 86 | + |
| 87 | +def cosine_peak(x, y): |
| 88 | + circle = np.hypot(80*x-40.0, 90*y-45.) |
| 89 | + f = np.exp(-0.04*circle) * np.cos(0.15*circle) |
| 90 | + return f |
| 91 | +cosine_peak.title = 'Cosine Peak' |
| 92 | + |
| 93 | +allfuncs = [exponential, cliff, saddle, gentle, steep, sphere, trig, gauss, cloverleaf, cosine_peak] |
| 94 | + |
| 95 | + |
| 96 | +class LinearTester(object): |
| 97 | + name = 'Linear' |
| 98 | + def __init__(self, xrange=(0.0, 1.0), yrange=(0.0, 1.0), nrange=101, npoints=250): |
| 99 | + self.xrange = xrange |
| 100 | + self.yrange = yrange |
| 101 | + self.nrange = nrange |
| 102 | + self.npoints = npoints |
| 103 | + |
| 104 | + rng = np.random.RandomState(1234567890) |
| 105 | + self.x = rng.uniform(xrange[0], xrange[1], size=npoints) |
| 106 | + self.y = rng.uniform(yrange[0], yrange[1], size=npoints) |
| 107 | + self.tri = Triangulation(self.x, self.y) |
| 108 | + |
| 109 | + def replace_data(self, dataset): |
| 110 | + self.x = dataset.x |
| 111 | + self.y = dataset.y |
| 112 | + self.tri = Triangulation(self.x, self.y) |
| 113 | + |
| 114 | + def interpolator(self, func): |
| 115 | + z = func(self.x, self.y) |
| 116 | + return self.tri.linear_extrapolator(z, bbox=self.xrange+self.yrange) |
| 117 | + |
| 118 | + def plot(self, func, interp=True, plotter='imshow'): |
| 119 | + if interp: |
| 120 | + lpi = self.interpolator(func) |
| 121 | + z = lpi[self.yrange[0]:self.yrange[1]:complex(0,self.nrange), |
| 122 | + self.xrange[0]:self.xrange[1]:complex(0,self.nrange)] |
| 123 | + else: |
| 124 | + y, x = np.mgrid[self.yrange[0]:self.yrange[1]:complex(0,self.nrange), |
| 125 | + self.xrange[0]:self.xrange[1]:complex(0,self.nrange)] |
| 126 | + z = func(x, y) |
| 127 | + |
| 128 | + z = np.where(np.isinf(z), 0.0, z) |
| 129 | + |
| 130 | + extent = (self.xrange[0], self.xrange[1], |
| 131 | + self.yrange[0], self.yrange[1]) |
| 132 | + fig = plt.figure() |
| 133 | + plt.hot() # Some like it hot |
| 134 | + if plotter == 'imshow': |
| 135 | + plt.imshow(np.nan_to_num(z), interpolation='nearest', extent=extent, origin='lower') |
| 136 | + elif plotter == 'contour': |
| 137 | + Y, X = np.ogrid[self.yrange[0]:self.yrange[1]:complex(0,self.nrange), |
| 138 | + self.xrange[0]:self.xrange[1]:complex(0,self.nrange)] |
| 139 | + plt.contour(np.ravel(X), np.ravel(Y), z, 20) |
| 140 | + x = self.x |
| 141 | + y = self.y |
| 142 | + lc = mpl.collections.LineCollection(np.array([((x[i], y[i]), (x[j], y[j])) |
| 143 | + for i, j in self.tri.edge_db]), colors=[(0,0,0,0.2)]) |
| 144 | + ax = plt.gca() |
| 145 | + ax.add_collection(lc) |
| 146 | + |
| 147 | + if interp: |
| 148 | + title = '%s Interpolant' % self.name |
| 149 | + else: |
| 150 | + title = 'Reference' |
| 151 | + if hasattr(func, 'title'): |
| 152 | + plt.title('%s: %s' % (func.title, title)) |
| 153 | + else: |
| 154 | + plt.title(title) |
| 155 | + |
| 156 | +class NNTester(LinearTester): |
| 157 | + name = 'Natural Neighbors' |
| 158 | + def interpolator(self, func): |
| 159 | + z = func(self.x, self.y) |
| 160 | + return self.tri.nn_extrapolator(z, bbox=self.xrange+self.yrange) |
| 161 | + |
| 162 | +def make_all_testfuncs(allfuncs=allfuncs): |
| 163 | + def make_test(func): |
| 164 | + filenames = [ |
| 165 | + '%s-%s' % (func.func_name, x) for x in |
| 166 | + ['ref-img', 'nn-img', 'lin-img', 'ref-con', 'nn-con', 'lin-con']] |
| 167 | + |
| 168 | + # We only generate PNGs to save disk space -- we just assume |
| 169 | + # that any backend differences are caught by other tests. |
| 170 | + @image_comparison(filenames, extensions=['png']) |
| 171 | + def reference_test(): |
| 172 | + nnt.plot(func, interp=False, plotter='imshow') |
| 173 | + nnt.plot(func, interp=True, plotter='imshow') |
| 174 | + lpt.plot(func, interp=True, plotter='imshow') |
| 175 | + nnt.plot(func, interp=False, plotter='contour') |
| 176 | + nnt.plot(func, interp=True, plotter='contour') |
| 177 | + lpt.plot(func, interp=True, plotter='contour') |
| 178 | + |
| 179 | + tester = reference_test |
| 180 | + tester.__name__ = 'test_%s' % func.func_name |
| 181 | + return tester |
| 182 | + |
| 183 | + nnt = NNTester(npoints=1000) |
| 184 | + lpt = LinearTester(npoints=1000) |
| 185 | + for func in allfuncs: |
| 186 | + globals()['test_%s' % func.func_name] = make_test(func) |
| 187 | + |
| 188 | +make_all_testfuncs() |
0 commit comments