首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Healpy:从数据到Healpix地图

Healpy:从数据到Healpix地图
EN

Stack Overflow用户
提问于 2015-07-23 04:39:52
回答 2查看 2.9K关注 0票数 9

我有一个数据网格,其中行表示θ(0,pi),列表示φ(0,2*pi),其中f(θ,φ)是该位置的暗物质密度。我想要计算它的功率谱,并决定使用healpy。

我不能理解的是如何格式化我的数据以供healpy使用。如果有人能提供代码(基于显而易见的原因,用python编写)或者给我提供一个教程,那就太好了!我已经用下面的代码尝试过了:

代码语言:javascript
复制
#grid dimensions are Nrows*Ncols (subject to change)
theta = np.linspace(0, np.pi, num=grid.shape[0])[:, None]
phi = np.linspace(0, 2*np.pi, num=grid.shape[1])
nside = 512
print "Pixel area: %.2f square degrees" % hp.nside2pixarea(nside, degrees=True)
pix = hp.ang2pix(nside, theta, phi)
healpix_map = np.zeros(hp.nside2npix(nside), dtype=np.double)
healpix_map[pix] = grid 

但是,当我尝试执行代码来做功率谱时。具体来说,就是:

代码语言:javascript
复制
cl = hp.anafast(healpix_map[pix], lmax=1024)

我得到了这个错误:

TypeError:像素数错误

如果有人能给我一个好的教程,或者帮我编辑代码,那就太好了。

更多规范:我的数据在2dNP数组中,如果需要,我可以更改numRow/numCols。

编辑:

我已经解决了这个问题,首先将anafast的参数改为healpix_map。我还通过制作Nrows*Ncols=12*nside*nside改进了间距。但是,我的功率谱仍然有误差。如果有人有关于如何计算功率谱( theta/phi args的条件)的良好文档/教程的链接,那将是非常有帮助的。

EN

回答 2

Stack Overflow用户

发布于 2019-10-29 09:15:22

this answer之后,有一种使用numpy.add.at进行映射初始化的更快方法。

在我的机器上,这比Daniel's excellent answer的第一部分快了好几倍

代码语言:javascript
复制
import healpy as hp
import numpy as np
import matplotlib.pyplot as plt

# Set the number of sources and the coordinates for the input
nsources = int(1e7)
nside = 64
npix = hp.nside2npix(nside)

# Coordinates and the density field f
thetas = np.random.uniform(0, np.pi, nsources)
phis = np.random.uniform(0, 2*np.pi, nsources)
fs = np.random.randn(nsources)

# Go from HEALPix coordinates to indices
indices = hp.ang2pix(nside, thetas, phis)

# Baseline, from Daniel Lenz's answer:
# time: ~5 s
hpxmap1 = np.zeros(npix, dtype=np.float)
for i in range(nsources):
    hpxmap1[indices[i]] += fs[i]

# Using numpy.add.at
# time: ~0.6 ms
hpxmap2 = np.zeros(npix, dtype=np.float)
np.add.at(hpxmap2, indices, fs)
票数 3
EN

Stack Overflow用户

发布于 2017-10-22 12:20:12

给你,希望这就是你要找的。如有疑问,请随时发表评论:)

代码语言:javascript
复制
import healpy as hp
import numpy as np
import matplotlib.pyplot as plt

# Set the number of sources and the coordinates for the input
nsources = int(1.e4)
nside = 16
npix = hp.nside2npix(nside)

# Coordinates and the density field f
thetas = np.random.random(nsources) * np.pi
phis = np.random.random(nsources) * np.pi * 2.
fs = np.random.randn(nsources)

# Go from HEALPix coordinates to indices
indices = hp.ang2pix(nside, thetas, phis)

# Initate the map and fill it with the values
hpxmap = np.zeros(npix, dtype=np.float)
for i in range(nsources):
    hpxmap[indices[i]] += fs[i]

# Inspect the map
hp.mollview(hpxmap)

由于上面的地图只包含噪声,因此功率谱应该只包含散粒噪声,即平坦。

代码语言:javascript
复制
# Get the power spectrum
Cl = hp.anafast(hpxmap)
plt.figure()
plt.plot(Cl)

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/31573572

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档