-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdata_extraction_funcs.py
More file actions
511 lines (411 loc) · 12.5 KB
/
Copy pathdata_extraction_funcs.py
File metadata and controls
511 lines (411 loc) · 12.5 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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
"""A set of utility functions to extract data for plotting from rail files"""
from __future__ import annotations
from typing import Any
import numpy as np
import qp
import tables_io
from rail.utils import catalog_utils
from rail.projects import RailProject, path_funcs
from . import utility_functions
def extract_z_true(
filepath: str,
colname: str = "redshift",
) -> np.ndarray:
"""Extract the true redshifts from a file
Parameters
----------
filepath: str
Path to file with tabular data
colname: str
Name of the column with redshfits ['redshift']
Returns
-------
redshifts: np.ndarray
Redshifts in question
Notes
-----
This assumes the redshifts are in a file that can be read by tables_io
"""
truth_table = tables_io.read(filepath)
return truth_table[colname]
def extract_z_point(
filepath: str,
colname: str = "zmode",
) -> np.ndarray:
"""Extract the point estimates of redshifts from a file
Parameters
----------
filepath: str
Path to file with tabular data
colname: str
Name of the column with point estimates ['zmode']
Returns
-------
z_estimates: np.ndarray
Redshift estimates in question
Notes
-----
This assumes the point estimates are in a qp file
"""
qp_ens = qp.read(filepath)
z_estimates = np.squeeze(qp_ens.ancil[colname])
return z_estimates
def extract_mag(
filepath: str,
colname: str = "LSST_obs_i",
) -> np.ndarray:
"""Extract the i-mag from a file
Parameters
----------
filepath: str
Path to file with tabular data
colname: str
Name of the column with redshfits ['redshift']
Returns
-------
magnitude: np.ndarray
Magnitude in question
Notes
-----
This assumes the magnitude are in a file that can be read by tables_io
"""
magnitude_table = tables_io.read(filepath)
return magnitude_table[colname]
def extract_magnitudes(
filepath: str,
template: str,
bands: list[str],
) -> np.ndarray:
"""Extract the magntidues from a file
Parameters
----------
filepath: str
Path to file with tabular data
template:
Template to make the names
bands:
List of the bands to apply to the template
Returns
-------
magnitudes: np.ndarray
Magnitudes in question
Notes
-----
This assumes the magnitude are in a file that can be read by tables_io
"""
magnitude_table = tables_io.read(filepath)
magnitudes = utility_functions.get_band_values(magnitude_table, template, bands)
return magnitudes
def extract_z_pdf(
filepath: str,
) -> qp.ensemble:
"""Extract the pdf estimates of redshifts from a file
Parameters
----------
filepath: str
Path to file with tabular data
Returns
-------
z_pdf: qp.ensemble
Redshift pdf in question
Notes
-----
This assumes the point estimates are in a qp file
"""
z_pdf = qp.read(filepath)
return z_pdf
def extract_multiple_z_point(
filepaths: dict[str, str],
colname: str = "zmode",
) -> dict[str, np.ndarray]: # pragma: no cover
"""Extract the point estimates of redshifts from several files
Parameters
----------
filepaths: dict[str, str]
Path to file with tabular data, keys will be associatd with the various
extracted point estimates
colname: str
Name of the column with point estimates ['zmode']
Returns
-------
z_estimates: dict[str, np.ndarray]
Redshift estimates in question, key by the key from input argument
Notes
-----
This assumes the point estimates are in a qp file
"""
ret_dict = {key: extract_z_point(val, colname) for key, val in filepaths.items()}
return ret_dict
def make_z_true_z_point_dict(
z_true: np.ndarray,
z_estimate: np.ndarray,
mags: np.ndarray,
) -> dict[str, np.ndarray]:
"""Build a dictionary with true redshifts and a point_estimates
Parameters
----------
z_true:
True Redshifts
z_estimate:
Point estimates
mags:
Magnitdues
Returns
-------
out_dict: dict[str, np.ndarray]
Dictionary with true redshift and a point estimate of the redshift
"""
out_dict: dict[str, Any] = dict(
truth=z_true,
pointEstimate=z_estimate,
magnitude=mags,
)
return out_dict
def make_z_true_multi_z_point_dict(
z_true: np.ndarray,
z_estimates: dict[str, np.ndarray],
) -> dict[str, Any]:
"""Build a single dictionary with true redshifts and several point_estimates
Parameters
----------
z_true: np.ndarray
True Redshifts
z_estimates: dict[str, np.ndarray]
Point estimates
Returns
-------
out_dict: dict[str, Any]
Dictionary with true redshift and all the point estimate of the redshift
"""
out_dict: dict[str, Any] = dict(
truth=z_true,
pointEstimates=z_estimates,
)
return out_dict
def get_pz_pdf_data(
project: RailProject,
selection: str,
flavor: str,
tag: str,
algo: str,
) -> dict[str, Any] | None:
"""Get the true redshifts and point estimates
for a particualar analysis selection and flavor
Parameters
----------
project: RailProject
Object with information about the structure of the current project
selection: str
Data selection in question, e.g., 'gold', or 'blended'
flavor: str
Analysis flavor in question, e.g., 'baseline' or 'zCosmos'
algo: str
Algorithm we want the estimates for, e.g., 'knn', 'bpz'], etc...
tag: str
File tag, e.g., 'test' or 'train', or 'train_zCosmos'
Returns
-------
pz_data: dict[str, Any] | None
Data in question or None if a file is missing
"""
z_true_path = path_funcs.get_z_true_path(project, selection, flavor, tag)
z_estimate_path = path_funcs.get_ceci_pz_output_path(
project, selection, flavor, algo
)
if z_estimate_path is None: # pragma: no cover
return None
z_true_data = extract_z_true(z_true_path)
z_pdf_data = extract_z_pdf(z_estimate_path)
pz_data = dict(truth=z_true_data, pz=z_pdf_data)
return pz_data
def get_pz_point_estimate_data(
project: RailProject,
selection: str,
flavor: str,
tag: str,
algo: str,
) -> dict[str, np.ndarray] | None:
"""Get the true redshifts and point estimates
for a particualar analysis selection and flavor
Parameters
----------
project: RailProject
Object with information about the structure of the current project
selection: str
Data selection in question, e.g., 'gold', or 'blended'
flavor: str
Analysis flavor in question, e.g., 'baseline' or 'zCosmos'
algo: str
Algorithm we want the estimates for, e.g., 'knn', 'bpz'], etc...
tag: str
File tag, e.g., 'test' or 'train', or 'train_zCosmos'
Returns
-------
pz_data: dict[str, np.ndarray] | None
Data in question or None if a file is missing
"""
z_true_path = path_funcs.get_z_true_path(project, selection, flavor, tag)
z_estimate_path = path_funcs.get_ceci_pz_output_path(
project, selection, flavor, algo
)
if z_estimate_path is None: # pragma: no cover
return None
z_true_data = extract_z_true(z_true_path)
z_estimate_data = extract_z_point(z_estimate_path)
flavor_info = project.get_flavor(flavor)
catalog_tag = flavor_info["catalog_tag"]
catalog_utils.apply(catalog_tag)
active_catalog = catalog_utils.get_active_tag()
ref_band = active_catalog.config.mag_column_template.format(
band=active_catalog.config.ref_band
)
mag_data = extract_mag(z_true_path, colname=ref_band)
pz_data = make_z_true_z_point_dict(z_true_data, z_estimate_data, mag_data)
return pz_data
def get_ztrue_and_magntidues(
project: RailProject,
selection: str,
flavor: str,
tag: str,
) -> dict[str, np.ndarray] | None:
"""Get the true redshifts and observed magntidues
for a particualar analysis selection and flavor
Parameters
----------
project: RailProject
Object with information about the structure of the current project
selection: str
Data selection in question, e.g., 'gold', or 'blended'
flavor: str
Analysis flavor in question, e.g., 'baseline' or 'zCosmos'
tag: str
File tag, e.g., 'test' or 'train', or 'train_zCosmos'
Returns
-------
out_data: dict[str, np.ndarray] | None
Data in question or None if a file is missing
"""
flavor_info = project.get_flavor(flavor)
catalog_tag = flavor_info["catalog_tag"]
catalog_utils.apply(catalog_tag)
active_catalog = catalog_utils.get_active_tag()
z_true_path = path_funcs.get_z_true_path(project, selection, flavor, tag)
z_true_data = extract_z_true(z_true_path, active_catalog.config.redshift_col)
mag_data = extract_magnitudes(
z_true_path,
active_catalog.config.mag_column_template,
active_catalog.config.band_list,
)
out_data = dict(
truth=z_true_data,
magnitudes=mag_data,
bands=active_catalog.config.band_list,
)
return out_data
def get_multi_pz_point_estimate_data(
point_estimate_infos: dict[str, dict[str, Any]],
) -> dict[str, Any] | None:
"""Get the true redshifts and point estimates
for several analysis variants
This checks that they all have the same redshifts
Parameters
----------
point_estimate_infos: dict[str, dict[str, Any]]
Information about how to get point estimates
Returns
-------
pz_data: dict[str, Any] | None
Data in question or None
"""
point_estimates: dict[str, np.ndarray] = {}
ztrue_data: np.ndarray | None = None
ztrue_key: str | None = None
for key, val in point_estimate_infos.items():
the_data = get_pz_point_estimate_data(**val)
if the_data is None: # pragma: no cover
continue
if ztrue_data is None:
ztrue_data = the_data["truth"]
ztrue_key = key
else:
if not np.allclose(ztrue_data, the_data["truth"]): # pragma: no cover
raise ValueError(
f"Mismatch in truth data. data({key}) != data({ztrue_key})"
)
point_estimates[key] = the_data["pointEstimate"]
if ztrue_data is None: # pragma: no cover
return None
pz_data = make_z_true_multi_z_point_dict(ztrue_data, point_estimates)
return pz_data
def get_tomo_bins_nz_estimate_data(
project: RailProject,
selection: str,
flavor: str,
algo: str,
classifier: str,
summarizer: str,
) -> qp.Ensemble:
"""Get the tomographic bin n(z) estimates
Parameters
----------
project: RailProject
Object with information about the structure of the current project
selection: str
Data selection in question, e.g., 'gold', or 'blended'
flavor: str
Analysis flavor in question, e.g., 'baseline' or 'zCosmos'
algo: str
Algorithm we want the estimates for, e.g., 'knn', 'bpz'], etc...
classifier: str
Algorithm we use to make tomograpic bin
summarizer: str
Algorithm we use to go from p(z) to n(z)
Returns
-------
nz_data: qp.Ensemble
Tomographic bin n(z) data
"""
paths = path_funcs.get_ceci_nz_output_paths(
project,
selection,
flavor,
algo,
classifier,
summarizer,
)
data = qp.concatenate([extract_z_pdf(path_) for path_ in paths])
return data
def get_tomo_bins_true_nz_data(
project: RailProject,
selection: str,
flavor: str,
algo: str,
classifier: str,
) -> qp.Ensemble:
"""Get the tomographic bin true n(z)
Parameters
----------
project: RailProject
Object with information about the structure of the current project
selection: str
Data selection in question, e.g., 'gold', or 'blended'
flavor: str
Analysis flavor in question, e.g., 'baseline' or 'zCosmos'
algo: str
Algorithm we want the estimates for, e.g., 'knn', 'bpz'], etc...
classifier: str
Algorithm we use to make tomograpic bin
Returns
-------
nz_data: qp.Ensemble
Tomographic bin n(z) data
"""
paths = path_funcs.get_ceci_true_nz_output_paths(
project,
selection,
flavor,
algo,
classifier,
)
data = qp.concatenate([extract_z_pdf(path_) for path_ in paths])
return data