-
Notifications
You must be signed in to change notification settings - Fork 2
/
Bk.py
94 lines (82 loc) · 2.48 KB
/
Bk.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
import numpy as np
import xarray as xr
from typing import List, Union
from summarizer.data import BoxCatalogue, SurveyCatalogue
from summarizer.base import BaseSummary
import PolyBin3D as pb
#TODO: make sure los works as expected
class Bk(BaseSummary):
def __init__(
self,
k_bins: Union[str, List],
n_mesh: int = 360,
lmax: int = 2,
mask = None,
los:str = 'global',
ells: List[int] = [0,2],
):
"""
"""
self.k_bins = k_bins
self.n_mesh = n_mesh
self.lmax = lmax
self.mask = mask
self.ells = ells
self.los = los
def __str__(
self,
):
return "bk"
def __call__(
self,
catalogue: Union[BoxCatalogue, SurveyCatalogue],
) -> np.array:
"""Given a catalogue, compute its bispectrum
Args:
catalogue (Catalogue): catalogue to summarize
Returns:
np.array:
"""
galaxies_mesh = catalogue.get_mesh(self.n_mesh)
base = pb.PolyBin3D(
sightline=self.los,
gridsize=self.n_mesh,
boxsize=[catalogue.boxsize, catalogue.boxsize, catalogue.boxsize],
boxcenter=(0.,0.,0.) if catalogue.boxsize is not None else None,
pixel_window='interlaced-tsc' if not catalogue.is_periodic_box else 'tsc',
)
bspec = pb.BSpec(
base,
self.k_bins, # k-bin edges
lmax=self.lmax, # Legendre multipoles
mask=self.mask, # real-space mask
applySinv=None, # filter to apply to data
k_bins_squeeze = None,
include_partial_triangles=False,
)
print('COMPUTING Bk')
bk_corr = bspec.Bk_ideal(
galaxies_mesh,
discreteness_correction=True,
)
k123 = bspec.get_ks()
weight = k123.prod(axis=0)
return np.array(
[bk_corr[f'b{ell}'] * weight for ell in self.ells]
)
def to_dataset(self, summary: np.array) -> xr.DataArray:
"""Convert a power spectrum array into an xarray dataset
with coordinates
Args:
summary (np.array): summary to convert
Returns:
xr.DataArray: dataset array
"""
return xr.DataArray(
summary,
dims=("ells", "k_index"),
coords={
"ells": self.ells,
"k_index": np.arange(len(summary[0])),
},
)