从给定的二维方差创建二维高斯随机场

Creating a 2D Gaussian random field from a given 2D variance

我一直在尝试使用计算出的方差创建物质斑点(高斯随机场)的二维地图。这个方差是一个二维数组。我尝试使用 numpy.random.normal 因为它允许方差的二维输入,但它并没有真正创建具有我期望的输入参数趋势的地图。重要的输入常量之一 lambda_c 应表现为 blob 的物理大小(直径)。但是,当我更改 lambda_c 时,blob 的大小根本没有改变。例如,如果我设置 lambda_c = 40 秒差距,地图需要直径为 40 秒差距的斑点。使用我的方差生成地图的 MWE:

import numpy as np
import random
import matplotlib.pyplot as plt
from matplotlib.pyplot import show, plot
import scipy.integrate as integrate
from scipy.interpolate import RectBivariateSpline



n = 300
c = 3e8
G = 6.67e-11
M_sun = 1.989e30
pc = 3.086e16  # parsec
Dds = 1097.07889283e6*pc 
Ds = 1726.62069147e6*pc
Dd = 1259e6*pc

FOV_arcsec_original = 5.
FOV_arcmin = FOV_arcsec_original/60.   


pix2rad = ((FOV_arcmin/60.)/float(n))*np.pi/180.
rad2pix = 1./pix2rad

x_pix = np.linspace(-FOV_arcsec_original/2/pix2rad/180.*np.pi/3600.,FOV_arcsec_original/2/pix2rad/180.*np.pi/3600.,n)
y_pix = np.linspace(-FOV_arcsec_original/2/pix2rad/180.*np.pi/3600.,FOV_arcsec_original/2/pix2rad/180.*np.pi/3600.,n)
X_pix,Y_pix = np.meshgrid(x_pix,y_pix)

conc = 10.
M = 1e13*M_sun
r_s = 18*1e3*pc

lambda_c = 40*pc  ### The important parameter that doesn't seem to manifest itself in the map when changed

rho_s = M/((4*np.pi*r_s**3)*(np.log(1+conc) - (conc/(1+conc)))) 
sigma_crit = (c**2*Ds)/(4*np.pi*G*Dd*Dds)
k_s = rho_s*r_s/sigma_crit
theta_s = r_s/Dd
Renorm = (4*G/c**2)*(Dds/(Dd*Ds))
#### Here I just interpolate and zoom into my field of view to get better resolutions
A = np.sqrt(X_pix**2 + Y_pix**2)*pix2rad/theta_s



A_1 = A[100:200,0:100]

n_x = n_y = 100

FOV_arcsec_x = FOV_arcsec_original*(100./300)
FOV_arcmin_x = FOV_arcsec_x/60.   
pix2rad_x = ((FOV_arcmin_x/60.)/float(n_x))*np.pi/180.
rad2pix_x = 1./pix2rad_x

FOV_arcsec_y = FOV_arcsec_original*(100./300)
FOV_arcmin_y = FOV_arcsec_y/60.   
pix2rad_y = ((FOV_arcmin_y/60.)/float(n_y))*np.pi/180.
rad2pix_y = 1./pix2rad_y

x1 = np.linspace(-FOV_arcsec_x/2/pix2rad_x/180.*np.pi/3600.,FOV_arcsec_x/2/pix2rad_x/180.*np.pi/3600.,n_x)
y1 = np.linspace(-FOV_arcsec_y/2/pix2rad_y/180.*np.pi/3600.,FOV_arcsec_y/2/pix2rad_y/180.*np.pi/3600.,n_y)
X1,Y1 = np.meshgrid(x1,y1)



n_x_2 = 500
n_y_2 = 500


x2 = np.linspace(-FOV_arcsec_x/2/pix2rad_x/180.*np.pi/3600.,FOV_arcsec_x/2/pix2rad_x/180.*np.pi/3600.,n_x_2)
y2 = np.linspace(-FOV_arcsec_y/2/pix2rad_y/180.*np.pi/3600.,FOV_arcsec_y/2/pix2rad_y/180.*np.pi/3600.,n_y_2)
X2,Y2 = np.meshgrid(x2,y2)

interp_spline = RectBivariateSpline(y1,x1,A_1)

A_2 = interp_spline(y2,x2)



A_3 = A_2[50:450,0:400]

n_x_3 = n_y_3 = 400

FOV_arcsec_x = FOV_arcsec_original*(100./300)*400./500.
FOV_arcmin_x = FOV_arcsec_x/60.   
pix2rad_x = ((FOV_arcmin_x/60.)/float(n_x_3))*np.pi/180.
rad2pix_x = 1./pix2rad_x

FOV_arcsec_y = FOV_arcsec_original*(100./300)*400./500.
FOV_arcmin_y = FOV_arcsec_y/60.   
pix2rad_y = ((FOV_arcmin_y/60.)/float(n_y_3))*np.pi/180.
rad2pix_y = 1./pix2rad_y

x3 = np.linspace(-FOV_arcsec_x/2/pix2rad_x/180.*np.pi/3600.,FOV_arcsec_x/2/pix2rad_x/180.*np.pi/3600.,n_x_3)
y3 = np.linspace(-FOV_arcsec_y/2/pix2rad_y/180.*np.pi/3600.,FOV_arcsec_y/2/pix2rad_y/180.*np.pi/3600.,n_y_3)
X3,Y3 = np.meshgrid(x3,y3)

n_x_4 = 1000
n_y_4 = 1000


x4 = np.linspace(-FOV_arcsec_x/2/pix2rad_x/180.*np.pi/3600.,FOV_arcsec_x/2/pix2rad_x/180.*np.pi/3600.,n_x_4)
y4 = np.linspace(-FOV_arcsec_y/2/pix2rad_y/180.*np.pi/3600.,FOV_arcsec_y/2/pix2rad_y/180.*np.pi/3600.,n_y_4)
X4,Y4 = np.meshgrid(x4,y4)

interp_spline = RectBivariateSpline(y3,x3,A_3)

A_4 = interp_spline(y4,x4)

############### Function to calculate variance

variance = np.zeros((len(A_4),len(A_4)))



def variance_fluctuations(x):
    for i in xrange(len(x)):
        for j in xrange(len(x)):
            if x[j][i] < 1.:
                variance[j][i] = (k_s**2)*(lambda_c/r_s)*((np.pi/x[j][i]) - (1./(x[j][i]**2 -1)**3.)*(((6.*x[j][i]**4. - 17.*x[j][i]**2. + 26)/3.)+ (((2.*x[j][i]**6. - 7.*x[j][i]**4. + 8.*x[j][i]**2. - 8)*np.arccosh(1./x[j][i]))/(np.sqrt(1-x[j][i]**2.)))))
            elif x[j][i] > 1.:
                variance[j][i] = (k_s**2)*(lambda_c/r_s)*((np.pi/x[j][i]) - (1./(x[j][i]**2 -1)**3.)*(((6.*x[j][i]**4. - 17.*x[j][i]**2. + 26)/3.)+ (((2.*x[j][i]**6. - 7.*x[j][i]**4. + 8.*x[j][i]**2. - 8)*np.arccos(1./x[j][i]))/(np.sqrt(x[j][i]**2.-1)))))



variance_fluctuations(A_4)

#### Creating the map 

mean = 0

delta_kappa = np.random.normal(0,variance,A_4.shape)  

xfinal = np.linspace(-FOV_arcsec_x*np.pi/180./3600.*Dd/pc/2,FOV_arcsec_x*np.pi/180./3600.*Dd/pc/2,1000)
yfinal = np.linspace(-FOV_arcsec_x*np.pi/180./3600.*Dd/pc/2,FOV_arcsec_x*np.pi/180./3600.*Dd/pc/2,1000)

Xfinal, Yfinal = np.meshgrid(xfinal,yfinal)
plt.contourf(Xfinal,Yfinal,delta_kappa,100)
plt.show()

地图看起来像这样,斑点的密度向右增加。但是,无论我使用 lambda_c = 40*pc 还是 [=33=,blob 的大小都不会改变,地图看起来几乎一样] = 400*pc.

我想知道 np.random.normal 函数是不是真的按照我的预期去做?我觉得地图的像素比例和绘制样本的方式不会 link 影响斑点的大小。也许有更好的方法来使用方差创建地图,将不胜感激任何见解。

我希望地图看起来像这样,斑点大小根据我的方差的输入参数而变化:

ThunderFlash,试试这个代码来绘制地图:

# function to produce blobs:
from scipy.stats import multivariate_normal

def blob (positions, mean=(0,0),  var=1):
    cov = [[var,0],[0,var]]
    return multivariate_normal(mean, cov).pdf(positions)
    
"""
now prepare for blobs generation.
note that I use less dense grid to pick blobs centers (regulated by `step`)
this makes blobs more pronounced and saves calculation time.

use this part instead of your code section below comment #### Creating the map 
"""
delta_kappa = np.random.normal(0,variance,A_4.shape) # same
    
step = 10 # 
dk2 = delta_kappa[::step,::step] # taking every 10th element
x2, y2 = xfinal[::step],yfinal[::step]
field = np.dstack((Xfinal,Yfinal)) 
print (field.shape, dk2.shape, x2.shape, y2.shape)

>> (1000, 1000, 2), (100, 100), (100,), (100,)

result = np.zeros(field.shape[:2]) 

for x in range (len(x2)):
    for y in range (len(y2)):
        res2 = blob(field, mean = (x2[x], y2[y]), var=10000)*dk2[x,y]
        result += res2

# the cycle above took over 20 minutes on Ryzen 2700X. It could be accelerated by vectorization presumably.

plt.contourf(Xfinal,Yfinal,result,100)
plt.show()

您可能想在 blob() 中使用 var 参数来平滑图像,并使用 step 来使其更加压缩。 这是我使用您的代码得到的图像(不知何故轴被翻转,顶部更密集的区域):

一种完全不同且更快的方法可能只是用高斯滤波器模糊 delta_kappa 数组。尝试调整 sigma 参数来改变斑点大小。

from scipy.ndimage.filters import gaussian_filter
dk_gf = gaussian_filter(delta_kappa, sigma=20)
Xfinal, Yfinal = np.meshgrid(xfinal,yfinal)
plt.contourf(Xfinal,Yfinal,dk_ma,100, cmap='jet')
plt.show();

这是 sigma=20

的图像

这是 sigma=2.5

的图像

这是(惊喜)天文学和宇宙学中一个被广泛访问的问题。

你可以使用 lenstool:https://lenstools.readthedocs.io/en/latest/examples/gaussian_random_field.html

您也可以在这里尝试:

https://andrewwalker.github.io/statefultransitions/post/gaussian-fields

更不用说:

https://github.com/bsciolla/gaussian-random-fields

我不会在这里复制代码,因为所有功劳都归于上述作者。然而,他们确实都从 google 搜索中出来了:/

最简单的可能是 python 模块 FyeldGenerator,显然是为此目的而设计的:

https://github.com/cphyc/FyeldGenerator

所以(改编自 github 示例):

pip install FyeldGenerator
from FyeldGenerator import generate_field

from matplotlib import use
use('Agg')

import matplotlib.pyplot as plt
import numpy as np



plt.figure()

# Helper that generates power-law power spectrum
def Pkgen(n):
    def Pk(k):
        return np.power(k, -n)

    return Pk

# Draw samples from a normal distribution
def distrib(shape):
    a = np.random.normal(loc=0, scale=1, size=shape)
    b = np.random.normal(loc=0, scale=1, size=shape)
    return a + 1j * b


shape = (512, 512)

field = generate_field(distrib, Pkgen(2), shape)

plt.imshow(field, cmap='jet')

plt.savefig('field.png',dpi=400)
plt.close())

这给出:

对我来说看起来很简单 :)

PS:FoV 暗示了对高斯随机场的 telescope 观察:)