In [9]:
# Load Biospytial modules and etc.
%matplotlib inline
import sys
sys.path.append('/apps')
sys.path.append('..')
#sys.path.append('../../spystats')
import django
django.setup()
import pandas as pd
import matplotlib.pyplot as plt
import numpy as np
## Use the ggplot style
plt.style.use('ggplot')
from external_plugins.spystats.spystats import tools as sptools
import scipy
In [10]:
vm = sptools.ExponentialVariogram(sill=0.3,range_a=0.4)
In [388]:
%time xx,yy,z = sptools.simulatedGaussianFieldAsPcolorMesh(vm)
In [389]:
plt.imshow(z)
Out[389]:
In [13]:
x = xx[1,:]
y = yy[:,1]
In [372]:
import scipy.fftpack as fft
c_delta = lambda d : np.hstack(((2 + d),-1,np.zeros(128 - 3),-1))
c_base = c_delta(0.1)
c_base
Out[372]:
In [373]:
C = scipy.linalg.circulant(c_base)
n = C.shape[1]
C_inv = np.linalg.inv(C)
plt.imshow(C_inv, interpolation = 'None')
C_inv.max()
Out[373]:
In [374]:
plop = fft.ifft(fft.fft(c_base) ** -1)
#plop = fft.rifft(fft.rfft(c_base) ** -1) / n
plop.real.max()
Out[374]:
In [321]:
C_inv2 = scipy.linalg.circulant(plop.real)
plt.imshow(C_inv2, interpolation = 'None')
Out[321]:
In [375]:
C_chol = np.linalg.cholesky(C_inv)
z = np.random.normal(0, 1, [n])
mmm = np.matmul(C_chol, z)
plt.plot(mmm);
In [378]:
lambda_vec = fft.fft(c_base)
Lambda_aux = np.power(lambda_vec, -0.5)
z_re = np.random.normal(0, 1, [n])
z_im = np.random.normal(0, 1, [n])
z = z_re + 1j * z_im
# z = np.random.normal(0, 1, [n])
x = fft.fft(Lambda_aux.real * z).real / np.sqrt(n)
plt.plot(x);
Oke, for the moment I´ll follow the example in GMRF book. i.e. a Torus (stationary of 128x128)
In [396]:
#c_delta = lambda d : np.hstack(((4 + d),-1,np.zeros(128 - 3),-1))
#c_delta = lambda d : np.hstack(((0),-1,np.zeros(128 - 3),-1))
n = 64
N = 64
delta = 0.1
c_base = np.zeros([n, N])
c_base[0, 0] = 4 + delta
c_base[0, 1] = -1
c_base[0, 2] = -1
c_base[1, 0] = -1
c_base[2, 0] = -1
c_base[0, N-1] = -1
c_base[0, N-2] = -1
c_base[N-1, 0] = -1
c_base[N-2, 0] = -1
c_base
Out[396]:
In [397]:
%%time
lambda_mat = fft.fft2(c_base)
z_re = np.random.normal(0, 1, [n, N])
z_im = np.random.normal(0, 1, [n, N])
z = z_re + 1j * z_im
x = fft.fft2((lambda_mat ** -0.5) * z).real / np.sqrt(n *N)
plt.imshow(x, interpolation = 'None')
In [423]:
%%time
n = 64
N = 64
delta = 0.0001
c_base = np.zeros([n, N])
c_base[0, 0] = 4 + delta
c_base[0, 1] = -1
#c_base[0, 2] = -1
c_base[1, 0] = -1
#c_base[2, 0] = -1
c_base[0, N-1] = -1
#c_base[0, N-2] = -1
c_base[N-1, 0] = -1
#c_base[N-2, 0] = -1
lambda_mat = fft.fft2(c_base)
z_re = np.random.normal(0, 1, [n, N])
z_im = np.random.normal(0, 1, [n, N])
z = z_re + 1j * z_im
x = fft.fft2((lambda_mat ** -0.5) * z).real / np.sqrt(n *N)
plt.imshow(x, interpolation = 'none')
In [69]:
## Simulate random noise (Normal distributed)
zr = scipy.stats.norm.rvs(size=(C.size,2),loc=0,scale=1)
zr.dtype=np.complex_
#plt.hist(zr.real)
In [70]:
from scipy.fftpack import ifft2, fft2
In [71]:
Lm = scipy.sqrt(C.shape[0]*C.shape[0]) * fft2(C)
In [72]:
Lm.shape
Out[72]:
In [73]:
zr.shape
Out[73]:
In [74]:
Lm.size
Out[74]:
In [75]:
%time v = fft2(scipy.sqrt(Lm) * zr.reshape(Lm.shape))
In [76]:
x = v.real
In [77]:
x.shape
Out[77]:
In [78]:
plt.imshow(x,interpolation='None')
Out[78]:
In [79]:
cc = scipy.linalg.inv(C)
In [84]:
plt.plot(cc[:,0])
Out[84]:
In [85]:
n = x.shape[0]
mm = scipy.stats.multivariate_normal(np.zeros(n),cc)
In [86]:
mmm = mm.rvs()
In [87]:
plt.imshow(mmm.reshape(100,100))
Out[87]:
In [64]:
scipy.stats.multivariate_normal?
In [510]:
nn = mm.rvs()
In [513]:
Out[513]:
In [8]:
from scipy.fftpack import ifftn
import matplotlib.pyplot as plt
import matplotlib.cm as cm
N = 30
f, ((ax1, ax2, ax3), (ax4, ax5, ax6)) = plt.subplots(2, 3, sharex='col', sharey='row')
xf = np.zeros((N,N))
xf[0, 5] = 1
xf[0, N-5] = 1
Z = ifftn(xf)
ax1.imshow(xf, cmap=cm.Reds)
ax4.imshow(np.real(Z), cmap=cm.gray)
xf = np.zeros((N, N))
xf[5, 0] = 1
xf[N-5, 0] = 1
Z = ifftn(xf)
ax2.imshow(xf, cmap=cm.Reds)
ax5.imshow(np.real(Z), cmap=cm.gray)
xf = np.zeros((N, N))
xf[5, 10] = 1
xf[N-5, N-10] = 1
Z = ifftn(xf)
ax3.imshow(xf, cmap=cm.Reds)
ax6.imshow(np.real(Z), cmap=cm.gray)
plt.show()
In [ ]: