In [1]:
import os
from nbodykit import style
import matplotlib.pyplot as plt
plt.style.use(style.notebook)
In [2]:
from nbodykit.binned_statistic import BinnedStatistic
data_dir = os.path.join(os.path.abspath('.'), 'data')
power_1d = BinnedStatistic.from_json(os.path.join(data_dir, 'dataset_1d.json'))
power_2d = BinnedStatistic.from_json(os.path.join(data_dir, 'dataset_2d.json'))
In [3]:
print("1D shape = ", power_1d.shape)
print("2D shape = ", power_2d.shape)
In [4]:
print("1D dims = ", power_1d.dims)
print("2D dims = ", power_2d.dims)
In [5]:
print("2D edges = ", power_2d.coords)
In [6]:
print("2D edges = ", power_2d.edges)
In [7]:
print("1D variables = ", power_1d.variables)
print("2D variables = ", power_2d.variables)
In [8]:
# the real component of the 1D power
Pk = power_1d['power'].real
print(type(Pk), Pk.shape, Pk.dtype)
# complex power array
Pkmu = power_2d['power']
print(type(Pkmu), Pkmu.shape, Pkmu.dtype)
In [9]:
print("attrs = ", power_2d.attrs)
In [10]:
# select the first mu bin
print(power_2d[:,0])
In [11]:
# select the first and last mu bins
print(power_2d[:, [0, -1]])
In [12]:
# select the first 5 k bins
print(power_1d[:5])
In [13]:
from matplotlib import pyplot as plt
# the shot noise is volume / number of objects
shot_noise = power_2d.attrs['volume'] / power_2d.attrs['N1']
# plot each mu bin separately
for i in range(power_2d.shape[1]):
pk = power_2d[:,i]
label = r"$\mu = %.1f$" % power_2d.coords['mu'][i]
plt.loglog(pk['k'], pk['power'].real - shot_noise, label=label)
plt.legend()
plt.xlabel(r"$k$ [$h$/Mpc]", fontsize=14)
plt.ylabel(r"$P(k,\mu)$ $[\mathrm{Mpc}/h]^3$", fontsize=14)
plt.show()
In [14]:
# get all mu bins for the k bin closest to k=0.1
print(power_2d.sel(k=0.1, method='nearest'))
In [15]:
# slice from k=0.01-0.1 for mu = 0.5
print(power_2d.sel(k=slice(0.01, 0.1), mu=0.5, method='nearest'))
In [16]:
# get all mu bins for the k bin closest to k=0.1, but keep k dimension
sliced = power_2d.sel(k=[0.1], method='nearest')
print(sliced)
In [17]:
# and then squeeze to remove the k dimension
print(sliced.squeeze())
In [18]:
# re-index into wider k bins
print(power_2d.reindex('k', 0.02))
In [19]:
# re-index into wider mu bins
print(power_2d.reindex('mu', 0.4))
In [20]:
# compute P(k) from P(k,mu)
print(power_2d.average('mu'))