-
Notifications
You must be signed in to change notification settings - Fork 0
/
measured_stats.py
134 lines (103 loc) · 3.95 KB
/
measured_stats.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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
from __future__ import print_function, division
from collections import namedtuple
import numpy as np
class MeasuredPower(object):
"""
"""
def __init__(self, info, info_id1, info_id2):
self.info = info
self.info_id1 = info_id1
self.info_id2 = info_id2
class MeasuredPower1D(MeasuredPower):
"""
Measured 1D power spectrum.
This is basically the same as nbodykit binned statistic, but
for convenience can access k and power simply as Pk.k and Pk.P.
Also save some additional information of the fields whose power
is measured.
"""
def __init__(self,
k=None,
P=None,
num_summands=None,
nbk_binned_stat=None,
info=None,
info_id1=None,
info_id2=None):
"""
Init either with k, P, num_summands, or with nbk_binned_stat.
Parameters
----------
k, P, num_summands : array_like, (N,)
nbk_binned_stat : None, nbodykit BinnedStatistic
Contains 1D power spectrum. Must be None if k, P, num_summands are specified.
"""
super(MeasuredPower1D, self).__init__(info, info_id1, info_id2)
self.bstat = nbk_binned_stat
if nbk_binned_stat is None:
self.k = k
self.P = P
self.num_summands = num_summands
else:
assert k is None
assert P is None
assert num_summands is None
assert len(self.bstat.power.shape) == 1
# 1d power, save k, P, Nmodes for convenience/backward compatibility of code
self.k = self.bstat.power['k']
self.P = self.bstat.power['power'].real
self.Nmodes = self.bstat.power['modes'].real
self.Nk = self.k.shape[0]
class MeasuredPower2D(MeasuredPower):
"""
Measured 2D power spectrum in Nk x Nmu bins.
An instance has the following attributes:
self.k2d : array_like, (Nk, Nmu)
k2d[i,j] is the value of k in the i-th k bin and j-th mu bin.
self.k : array_like, (Nk*Nmu, )
Flattened copy of k2d. Useful for code that assumes 1D k bins.
self.mu2d : array_like, (Nk, Nmu)
mu2d[i,j] is the value of mu in the i-th k bin and j-th mu bin.
self.mu : array_like, (Nk*Nmu, )
Flattened copy of mu2d.
Can always access original nbk_binned_stat, e.g.through Pk.bstat.power.k
and Pk.bstat.power.power.real.
Note: Do not modify any attributes by hand because will lead to
inconsistent k2d, k, Pk.bstat.power.k etc.
For multipoles, use Pk.bstat.poles.k and Pk.bstat.poles.power_0,
Pk.bstat.poles.power_2, etc.
"""
def __init__(self,
nbk_binned_stat=None,
info=None,
info_id1=None,
info_id2=None):
"""
Init with nbk_binned_stat which contains a measured 2D power spectrum.
Parameters
----------
nbk_binned_stat : nbodykit BinnedStatistic
Contains measured 2D power spectrum.
"""
super(MeasuredPower2D, self).__init__(info, info_id1, info_id2)
self.bstat = nbk_binned_stat
#print('Store 2D power with attrs: ', self.bstat.power.attrs)
#print('dir(bstat): ', dir(self.bstat))
#print('dir(bstat.power): ', dir(self.bstat.power))
# Check that we really got 2d power P(k,mu).
assert self.bstat.power.dims == ['k', 'mu']
assert len(self.bstat.power.shape) == 2
self.Nk = self.bstat.power.shape[0]
self.Nmu = self.bstat.power.shape[1]
# store 2d arrays, with shape (Nk, Nmu)
self.k2d = self.bstat.power['k']
self.mu2d = self.bstat.power['mu']
self.P2d = self.bstat.power['power'].real
self.Nmodes2d = self.bstat.power['modes'].real
for a in [self.k2d, self.mu2d, self.P2d]:
assert a.shape == (self.Nk, self.Nmu)
# To use 1d code, store flattened arrays
self.k = self.k2d.flatten()
self.mu = self.mu2d.flatten()
self.P = self.P2d.flatten()
self.Nmodes = self.Nmodes2d.flatten()