In [1]:
%pylab inline
from mba import *
seed(13)


Populating the interactive namespace from numpy and matplotlib

An example of layered data interpolation

Source function and scattered data locations


In [2]:
def foo(c):
    return sin(c[0]/100) + sin(c[1]*3)

cmin = [0.0, 0.0]
cmax = [1000.0, 10.0]

C = mgrid[0:cmax[0]:1e-1,0:cmax[1]:1e-1]
F = foo(C)

coo = uniform(cmin, cmax, (128,2))
val = foo(coo.transpose())

figure(figsize=(13,4))
pcolormesh(C[0], C[1], F)
scatter(coo[:,0], coo[:,1], c='k', s=1)
xlim([cmin[0], cmax[0]])
ylim([cmin[1], cmax[1]])
tight_layout()


Size of the target grid:


In [3]:
C.shape[1:]


Out[3]:
(10000, 100)

Anysotropy in target data should be reflected in the initial grid size

Reduction in horizontal grid size leads to better results:


In [4]:
ny = 16
for nx in (16, 8, 4):
    interp = mba2(cmin, cmax, [nx,ny], coo, val)
    error = amax(abs(val - interp(coo))) / amax(abs(val))
    print(interp)

    G = interp(C.transpose((1,2,0)).copy())
    figure(figsize=(12,4))
    pcolormesh(C[0], C[1], G)
    scatter(coo[:,0], coo[:,1], c='k', s=1)
    xlim([cmin[0], cmax[0]])
    ylim([cmin[1], cmax[1]])
    title(f'$n_0=[{nx},{ny}], \\quad \\varepsilon={error:e}$')
    tight_layout()


level 1: initial approximation
level 2: dense  [63, 63] (31752 bytes)
level 3: sparse [123, 123] (45360 bytes, compression: 0.37)
level 4: sparse [243, 243] (48120 bytes, compression: 0.10)
level 5: sparse [483, 483] (48912 bytes, compression: 0.03)
level 6: sparse [963, 963] (49152 bytes, compression: 0.01)

level 1: initial approximation
level 2: dense  [59, 123] (58056 bytes)
level 3: sparse [115, 243] (47136 bytes, compression: 0.21)
level 4: sparse [227, 483] (48600 bytes, compression: 0.06)
level 5: sparse [451, 963] (49056 bytes, compression: 0.01)

level 1: initial approximation
level 2: dense  [27, 123] (26568 bytes)
level 3: sparse [51, 243] (44928 bytes, compression: 0.45)
level 4: sparse [99, 483] (47832 bytes, compression: 0.13)
level 5: sparse [195, 963] (48624 bytes, compression: 0.03)
level 6: sparse [387, 1923] (49056 bytes, compression: 0.01)

Increase in vertical grid size may lead to the initial grid being too sparse:


In [5]:
nx = 16
for ny in (16, 32, 64):
    interp = mba2(cmin, cmax, [nx,ny], coo, val)
    error = amax(abs(val - interp(coo))) / amax(abs(val))
    print(interp)

    G = interp(C.transpose((1,2,0)).copy())
    figure(figsize=(12,4))
    pcolormesh(C[0], C[1], G)
    scatter(coo[:,0], coo[:,1], c='k', s=1)
    xlim([cmin[0], cmax[0]])
    ylim([cmin[1], cmax[1]])
    title(f'$n_0=[{nx},{ny}], \\quad \\varepsilon={error:e}$')
    tight_layout()


level 1: initial approximation
level 2: dense  [63, 63] (31752 bytes)
level 3: sparse [123, 123] (45360 bytes, compression: 0.37)
level 4: sparse [243, 243] (48120 bytes, compression: 0.10)
level 5: sparse [483, 483] (48912 bytes, compression: 0.03)
level 6: sparse [963, 963] (49152 bytes, compression: 0.01)

level 1: initial approximation
level 2: dense  [63, 127] (64008 bytes)
level 3: sparse [123, 251] (47304 bytes, compression: 0.19)
level 4: sparse [243, 499] (48480 bytes, compression: 0.05)
level 5: sparse [483, 995] (49056 bytes, compression: 0.01)
level 6: sparse [963, 1987] (49152 bytes, compression: 0.00)

level 1: initial approximation
level 2: dense  [33, 129] (34056 bytes)
level 3: sparse [63, 255] (45600 bytes, compression: 0.35)
level 4: sparse [123, 507] (48072 bytes, compression: 0.10)
level 5: sparse [243, 1011] (48792 bytes, compression: 0.02)
level 6: sparse [483, 2019] (49080 bytes, compression: 0.01)
level 7: sparse [963, 4035] (49152 bytes, compression: 0.00)