-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathnaive_stack.py
More file actions
219 lines (191 loc) · 7.9 KB
/
naive_stack.py
File metadata and controls
219 lines (191 loc) · 7.9 KB
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
"""
A summarizer that simple makes a histogram of a point estimate
"""
from typing import Any, Generator
import numpy as np
import qp
from ceci.config import StageParameter as Param
from rail.core.data import QPHandle, TableHandle, TableLike
from rail.core.common_params import SharedParams, TOMOGRAPHY_ALL, TOMOGRAPHY_NONE
from rail.estimation.informer import PzInformer
from rail.estimation.summarizer import PZSummarizer
class NaiveStackInformer(PzInformer):
"""Placeholder Informer"""
name = "NaiveStackInformer"
entrypoint_function = "inform" # the user-facing science function for this class
interactive_function = "naive_stack_informer"
config_options = PzInformer.config_options.copy()
def _finalize_run(self) -> None:
self.model = np.array([None])
PzInformer._finalize_run(self)
class NaiveStackSummarizer(PZSummarizer):
"""Summarizer which stacks individual P(z)"""
name = "NaiveStackSummarizer"
entrypoint_function = "summarize" # the user-facing science function for this class
interactive_function = "naive_stack_summarizer"
config_options = PZSummarizer.config_options.copy()
config_options.update(
zmin=SharedParams.copy_param("zmin"),
zmax=SharedParams.copy_param("zmax"),
nzbins=SharedParams.copy_param("nzbins"),
seed=Param(int, 87, msg="random seed"),
n_samples=Param(int, 1000, msg="Number of sample distributions to create"),
)
inputs = [("input", QPHandle)]
outputs = [("output", QPHandle), ("single_NZ", QPHandle)]
def __init__(self, args: Any, **kwargs: Any) -> None:
super().__init__(args, **kwargs)
self.zgrid: np.ndarray | None = None
def summarize(
self, input_data: qp.Ensemble, **kwargs
) -> QPHandle | dict[str, QPHandle]:
"""Summarizer for NaiveStack which returns multiple items
Parameters
----------
input_data : qp.Ensemble
Per-galaxy p(z), and any ancillary data associated with it
Returns
-------
QPHandle | dict[str, QPHandle]
Ensemble with n(z), and any ancillary data
Return type depends on `output_mode`
"""
self.set_data("input", input_data)
self.run()
self.finalize()
if len(self.outputs) == 1 or self.config.output_mode != "return":
results = self.get_handle("output")
# if there is more than one output and output_mode = return, return them all as a dictionary
elif len(self.outputs) > 1 and self.config.output_mode == "return":
results = {}
for output in self.outputs:
results[output[0]] = self.get_handle(output[0])
return results
def _setup_iterator(self) -> Generator:
itr = self.input_iterator("input")
for s, e, d in itr:
yield s, e, d, np.ones(e - s, dtype=bool)
def run(self) -> None:
handle = self.get_handle("input", allow_missing=True)
self._input_length = handle.size()
iterator = self._setup_iterator()
self.zgrid = np.linspace(
self.config.zmin, self.config.zmax, self.config.nzbins + 1
)
assert self.zgrid is not None
# Initializing the stacking pdf's
yvals = np.zeros((1, len(self.zgrid)))
bvals = np.zeros((self.config.n_samples, len(self.zgrid)))
bootstrap_matrix = self._broadcast_bootstrap_matrix()
first = True
for s, e, test_data, mask in iterator:
print(f"Process {self.rank} running estimator on chunk {s:,} - {e:,}")
self._process_chunk(
s, e, test_data, mask, first, bootstrap_matrix, yvals, bvals
)
first = False
if self.comm is not None: # pragma: no cover
bvals, yvals = self._join_histograms(bvals, yvals)
if self.rank == 0:
sample_ens = qp.Ensemble(
qp.interp, data=dict(xvals=self.zgrid, yvals=bvals)
)
qp_d = qp.Ensemble(qp.interp, data=dict(xvals=self.zgrid, yvals=yvals))
self.add_data("output", sample_ens)
self.add_data("single_NZ", qp_d)
def _process_chunk(
self,
start: int,
end: int,
data: qp.Ensemble,
mask: np.ndarray,
_first: bool,
bootstrap_matrix: np.ndarray,
yvals: np.ndarray,
bvals: np.ndarray,
) -> None:
assert self.zgrid is not None
pdf_vals = data.pdf(self.zgrid)
squeeze_mask = np.squeeze(mask)
yvals += np.expand_dims(
np.sum(
np.where(
np.isfinite(pdf_vals[squeeze_mask, :]), pdf_vals[squeeze_mask], 0.0
),
axis=0,
),
0,
)
# qp_d is the normalized probability of the stack, we need to know how many galaxies were
for i in range(self.config.n_samples):
bootstrap_draws = bootstrap_matrix[:, i]
# Neither all of the bootstrap_draws are in this chunk nor the index starts at "start"
chunk_mask = (bootstrap_draws >= start) & (bootstrap_draws < end)
bootstrap_draws = bootstrap_draws[chunk_mask] - start
zarr = np.where(squeeze_mask, pdf_vals.T, 0.0).T[bootstrap_draws]
bvals[i] += np.sum(zarr, axis=0)
class NaiveStackMaskedSummarizer(NaiveStackSummarizer):
name = "NaiveStackMaskedSummarizer"
entrypoint_function = "summarize" # the user-facing science function for this class
interactive_function = "naive_stack_masked_summarizer"
config_options = NaiveStackSummarizer.config_options.copy()
config_options.update(
selected_bin=Param(int, TOMOGRAPHY_NONE, msg=f"bin to use, or {TOMOGRAPHY_ALL} for all bins >=0 or {TOMOGRAPHY_NONE} for no masking"),
)
inputs = [("input", QPHandle), ("tomography_bins", TableHandle)]
outputs = [("output", QPHandle), ("single_NZ", QPHandle)]
def _setup_iterator(self) -> Generator:
selected_bin = self.config.selected_bin
if self.config.tomography_bins in ["none", None]:
selected_bin = TOMOGRAPHY_NONE
if selected_bin == TOMOGRAPHY_NONE:
itrs = [self.input_iterator("input")]
else:
itrs = [
self.input_iterator("input"),
self.input_iterator("tomography_bins"),
]
for it in zip(*itrs):
first = True
mask = None
for s, e, d in it:
if first:
start = s
end = e
pz_data = d
first = False
else:
if selected_bin == TOMOGRAPHY_ALL:
mask = d["class_id"] >= 0
else:
mask = d["class_id"] == selected_bin
if mask is None:
mask = np.ones(
pz_data.npdf, # pylint: disable=possibly-used-before-assignment
dtype=bool,
)
yield start, end, pz_data, mask # pylint: disable=possibly-used-before-assignment
def summarize(
self, input_data: qp.Ensemble, tomo_bins: TableLike | None = None, **kwargs
) -> QPHandle:
"""Override the Summarizer.summarize() method to take tomo bins
as an additional input
Parameters
----------
input_data : qp.Ensemble
Per-galaxy p(z), and any ancillary data associated with it
tomo_bins : TableLike | None, optional
Tomographic bins file, by default None
Returns
-------
QPHandle
Ensemble with n(z), and any ancillary data
"""
self.set_data("input", input_data)
if tomo_bins is None:
self.config.tomography_bins = None
else:
self.set_data("tomography_bins", tomo_bins)
self.run()
self.finalize()
return self.get_handle("output")