In [118]:
%pylab inline
In [119]:
import numpy as np
import scipy
import scipy.ndimage
import sed3
import plotly.plotly as py
In [120]:
# [mm]
sample_spacing = [1.0, 1.0, 1.0]
# [mm]
lambda_start = 1.0
lambda_stop = 15.0
exponent = 0.0
data_shape = [100,100,100]
In [121]:
lambda0 = lambda_start * np.asarray(sample_spacing)
lambda1 = lambda_stop * np.asarray(sample_spacing)
In [122]:
def noise_normalization(data, std_factor=1.0):
data0n = (data - np.mean(data)) * 1.0 / (std_factor * np.var(data)**0.5)
return data0n
In [123]:
data = np.random.rand(*data_shape)
In [124]:
data0 = scipy.ndimage.filters.gaussian_filter(data, sigma=lambda0)
data0 = noise_normalization(data0)
plt.imshow(data0[:,:,50], cmap="gray")
plt.colorbar()
print np.mean(data0)
print np.var(data0)
In [125]:
plt.hist(data0.ravel(), 20)
Out[125]:
In [126]:
data1 = scipy.ndimage.filters.gaussian_filter(data, sigma=lambda1)
data1 = noise_normalization(data1)
plt.imshow(data1[:,:,50], cmap="gray")
plt.colorbar()
print np.mean(data1)
print np.var(data1)
In [127]:
plt.hist(data1.ravel())
Out[127]:
In [128]:
x = np.linspace(0,10)
y = np.exp(-0.0 * x)
plt.plot(x,y)
Out[128]:
In [129]:
w0 = np.exp(exponent * lambda_start)
w1 = np.exp(exponent * lambda_stop)
wsum = w0 + w1
w0 = w0 / wsum
w1 = w1 / wsum
print w0, w1
In [130]:
data = ( data0 * w0 +
data1 * w1)
plt.imshow(data[:,:,50], cmap="gray")
plt.colorbar()
print np.mean(data)
print np.var(data)
In [131]:
plt.hist(data.ravel(), 20)
Out[131]:
In [132]:
def noises_fast(shape, sample_spacing=None, exponent=0.0,
lambda_start=0, lambda_stop=1, **kwargs):
data0 = 0
data1 = 0
w0 = 0
w1 = 0
lambda1 = lambda_stop * np.asarray(sample_spacing)
if lambda_start is not None:
lambda0 = lambda_start * np.asarray(sample_spacing)
data0 = np.random.rand(*shape)
data0 = scipy.ndimage.filters.gaussian_filter(data0, sigma=lambda0)
data0 = noise_normalization(data0)
w0 = np.exp(exponent * lambda_start)
if lambda_stop is not None:
lambda1 = lambda_stop * np.asarray(sample_spacing)
data1 = np.random.rand(*shape)
data1 = scipy.ndimage.filters.gaussian_filter(data1, sigma=lambda1)
data1 = noise_normalization(data1)
w1 = np.exp(exponent * lambda_stop)
wsum = w0 + w1
if wsum > 0:
w0 = w0 / wsum
w1 = w1 / wsum
print w0, w1
print np.mean(data0), np.var(data0)
print np.mean(data1), np.var(data1)
data = ( data0 * w0 + data1 * w1)
# plt.figure()
# plt.imshow(data0[:,:,50], cmap="gray")
# plt.colorbar()
# plt.figure()
# plt.imshow(data1[:,:,50], cmap="gray")
# plt.colorbar()
return data
In [133]:
noise = noises_fast(
shape=data_shape,
sample_spacing=sample_spacing,
exponent=exponent,
lambda_start=lambda_start,
lambda_stop=lambda_stop
)
plt.figure()
plt.imshow(noise[:,:,50], cmap="gray")
plt.colorbar()
print "var ", np.var(noise.ravel())
print "mean ", np.mean(noise.ravel())