Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SSI DES inputs #358

Merged
merged 13 commits into from
Oct 11, 2024
49 changes: 49 additions & 0 deletions examples/ssi/config_des_mag.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
# Values in this section are accessible to all the different stages.
# They can be overridden by individual stages though.
global:
# This is read by many stages that read complete
# catalog data, and tells them how many rows to read
# at once
chunk_rows: 10000
# These mapping options are also read by a range of stages
pixelization: healpix
nside: 256
sparse: true # Generate sparse maps - faster if using small areas


TXIngestSSIMatchedDESBalrogMag:
name: TXIngestSSIMatchedDESBalrogMag

TXMeanLensSelectorSSI:
name: TXMeanLensSelectorSSI
# Mag cuts
lens_zbin_edges: [0.20, 0.40, 0.55, 0.70, 0.85, 0.95, 1.05]
selection_type: DESmaglim
maglim_band: i
bright_limit: 17.5
a: 4
b: 18
extra_cols: [EXTENDED_CLASS_SOF, FLAGS_GOLD]

TXMeanLensSelectorSSIMag:
name: TXMeanLensSelectorSSIMag
# Mag cuts
lens_zbin_edges: [0.20, 0.40, 0.55, 0.70, 0.85, 0.95, 1.05]
selection_type: DESmaglim
maglim_band: i
bright_limit: 17.5
a: 4
b: 18
extra_cols: [EXTENDED_CLASS_SOF, FLAGS_GOLD]

TXLensCatalogSplitterSSI:
name: TXLensCatalogSplitterSSI

TXLensCatalogSplitterSSIMag:
name: TXLensCatalogSplitterSSIMag

TXSSIMagnification:
name: TXSSIMagnification
applied_magnification: 1.02
n_patches: 500
bootstrap_error: true
74 changes: 74 additions & 0 deletions examples/ssi/pipeline_des_mag.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
# Stages to run
stages:
- name: TXIngestSSIMatchedDESBalrog # process the unmagnified SSI inputs
- name: TXIngestSSIMatchedDESBalrogMag # process the magnified SSI inputs
classname: TXIngestSSIMatchedDESBalrog
aliases:
balrog_matched_catalog: balrog_matched_catalog_mag
matched_ssi_photometry_catalog: matched_ssi_photometry_catalog_mag

- name: TXMeanLensSelectorSSI #make unmagnified SSI lens samples
classname: TXMeanLensSelector
aliases:
photometry_catalog: matched_ssi_photometry_catalog
lens_photoz_pdfs: balrog_lens_photoz_pdfs
- name: TXLensCatalogSplitterSSI
classname: TXLensCatalogSplitter
aliases:
photometry_catalog: matched_ssi_photometry_catalog
lens_photoz_pdfs: balrog_lens_photoz_pdfs

- name: TXMeanLensSelectorSSIMag #make magnified SSI lens samples
classname: TXMeanLensSelector
aliases:
photometry_catalog: matched_ssi_photometry_catalog_mag
lens_tomography_catalog_unweighted: lens_tomography_catalog_unweighted_mag
lens_photoz_pdfs: balrog_lens_photoz_pdfs_mag
- name: TXLensCatalogSplitterSSIMag
classname: TXLensCatalogSplitter
aliases:
photometry_catalog: matched_ssi_photometry_catalog_mag
lens_tomography_catalog_unweighted: lens_tomography_catalog_unweighted_mag
binned_lens_catalog_unweighted: binned_lens_catalog_unweighted_mag
lens_photoz_pdfs: balrog_lens_photoz_pdfs_mag

- name: TXSSIMagnification # compute magnification coefficients for lens sample
aliases:
binned_lens_catalog_nomag: binned_lens_catalog_unweighted
binned_lens_catalog_mag: binned_lens_catalog_unweighted_mag


modules: txpipe

# Where to put outputs
output_dir: data/example/outputs_ssi_des_mag/

# How to run the pipeline: mini, parsl, or cwl
launcher:
name: mini
interval: 1.0

# Where to run the pipeline: cori-interactive, cori-batch, or local
site:
name: local
max_threads: 2

# configuration settings
config: examples/ssi/config_des_mag.yml

inputs:
# See README for paths to download these files
balrog_matched_catalog: /global/cfs/cdirs/lsst/groups/WL/users/jelvinpo/balrog_txpipe_example/balrog_matched_catalog_sof_run2a_v1.4.fits
balrog_matched_catalog_mag: /global/cfs/cdirs/lsst/groups/WL/users/jelvinpo/balrog_txpipe_example/balrog_matched_catalog_sof_run2a-mag_v1.4.fits
balrog_lens_photoz_pdfs: /global/cfs/cdirs/lsst/groups/WL/users/jelvinpo/balrog_txpipe_example/balrog_matched_catalog_sof_run2a_v1.4_dnf.h5
balrog_lens_photoz_pdfs_mag: /global/cfs/cdirs/lsst/groups/WL/users/jelvinpo/balrog_txpipe_example/balrog_matched_catalog_sof_run2a-mag_v1.4_dnf.h5

fiducial_cosmology: data/fiducial_cosmology.yml

# if supported by the launcher, restart the pipeline where it left off
# if interrupted
resume: false
# where to put output logs for individual stages
log_dir: data/example/outputs_ssi_des_mag/logs/
# where to put an overall parsl pipeline log
pipeline_log: data/example/outputs_ssi_des_mag/log.txt
2 changes: 1 addition & 1 deletion txpipe/ingest/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@
from .mocks import TXCosmoDC2Mock, TXGaussianSimsMock
from .gcr import TXMetacalGCRInput, TXIngestStars
from .redmagic import TXIngestRedmagic
from .ssi import TXIngestSSIGCR, TXMatchSSI
from .ssi import TXIngestSSIGCR, TXMatchSSI, TXIngestSSIMatched, TXIngestSSIMatchedDESBalrog
108 changes: 108 additions & 0 deletions txpipe/ingest/ssi.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from ..base_stage import PipelineStage
from ..data_types import (
HDFFile,
FitsFile,
)
from ..utils import (
nanojansky_to_mag_ab,
Expand Down Expand Up @@ -274,3 +275,110 @@ def finalize_output(self, outfile, group, ntot):
col.resize((ntot,))
outfile.close()
return



class TXIngestSSIMatched(PipelineStage):
"""
Base-stage for ingesting a matched SSI catalog

This stage will just read in a file in a given format and output to a
HDF file that TXPIPE can use

"""

name = "TXIngestSSIMatched"

outputs = [
("matched_ssi_photometry_catalog", HDFFile),
]

config_options = {
"chunk_rows": 100_000,
"magnification":0, # magnification label
}


class TXIngestSSIMatchedDESBalrog(TXIngestSSIMatched):
"""
Class for ingesting a matched "SSI" catalog from DES (AKA Balrog)
"""

name = "TXIngestSSIMatchedDESBalrog"

inputs = [
("balrog_matched_catalog", FitsFile),
]

def run(self):
"""
Run the analysis for this stage.
"""
print('Ingesting DES Balrog matched catalog')

#get some basic onfo about the input file
f = self.open_input("balrog_matched_catalog")
n = f[1].get_nrows()
dtypes = f[1].get_rec_dtype()[0]
f.close()

print(f'{n} objects in matched catalog')

chunk_rows = self.config["chunk_rows"]

#we will only load a subset of columns to save space
column_names = {
joezuntz marked this conversation as resolved.
Show resolved Hide resolved
"bal_id": "bal_id",
"true_bdf_mag_deredden": "inj_mag",
"true_id": "inj_id",
"meas_id": "id",
"meas_ra": "ra",
"meas_dec": "dec",
"meas_cm_mag_deredden": "mag",
"meas_cm_T": "cm_T",
"meas_EXTENDED_CLASS_SOF": "EXTENDED_CLASS_SOF",
"meas_FLAGS_GOLD_SOF_ONLY": "FLAGS_GOLD",
}
cols = list(column_names.keys())

#set up the output file columns
output = self.open_output("matched_ssi_photometry_catalog")
g = output.create_group("photometry")
for col in cols:
dtype = dtypes[col]

if "_mag" in col:
#per band
dtype = dtype.subdtype[0]
for b in "griz":
g.create_dataset(column_names[col]+f"_{b}", (n,), dtype=dtype)

#also create an empty array for the mag errors
#TO DO: compute mag errors from flux errors
joezuntz marked this conversation as resolved.
Show resolved Hide resolved
g.create_dataset(column_names[col]+f"_err_{b}", (n,), dtype=dtype)
else:
g.create_dataset(column_names[col], (n,), dtype=dtype)

#iterate over the input file and save to the output columns
for (s, e, data) in self.iterate_fits("balrog_matched_catalog", 1, cols, chunk_rows):
print(s,e,n)
for col in cols:
if "_mag" in col:
for iband,b in enumerate("griz"):
g[column_names[col]+f"_{b}"][s:e] = data[col][:,iband]
else:
g[column_names[col]][s:e] = data[col]

# Set up any dummy columns with sentinal values
# that were not in the original files
dummy_columns = {
"redshift_true":10.0,
}
for col_name in dummy_columns.keys():
g.create_dataset(col_name, data=np.full(n,dummy_columns[col_name]))

output.close()




32 changes: 31 additions & 1 deletion txpipe/lens_selector.py
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To get the DES maglim selection from Balrog catalogs, I included the EXTENDED_CLASS_SOF (the star galaxy separator) and FLAGS_GOLD columns.

Alternatively, we could rename these columns in the ingestion stage to something more Rubin-like and use these. Depends on how generic we want the lens selector options to be

Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ class TXBaseLensSelector(PipelineStage):
"selection_type": "boss",
"maglim_band": "i",
"maglim_limit": 24.1,
"extra_cols": [""],

}

Expand Down Expand Up @@ -215,6 +216,8 @@ def select_lens(self, phot_data):
s = self.select_lens_boss(phot_data)
elif t == "maglim":
s= self.select_lens_maglim(phot_data)
elif t == "DESmaglim":
s= self.select_lens_DESmaglim(phot_data)
else:
raise ValueError(f"Unknown lens selection type {t} - expected boss or maglim")
ntot = s.size
Expand Down Expand Up @@ -277,6 +280,26 @@ def select_lens_maglim(self, phot_data):
s = (mag_i < limit).astype(np.int8)
return s

def select_lens_DESmaglim(self, phot_data):
band = self.config["maglim_band"]
bright_limit = self.config["bright_limit"]
# mag < a*zphot + b
a = self.config["a"]
b = self.config["b"]

z = phot_data["z"]
mag_i = phot_data[f"mag_{band}"]
sg = phot_data["EXTENDED_CLASS_SOF"]
flags = phot_data["FLAGS_GOLD"]

cut1 = (mag_i > bright_limit) #cut very bright gaalxies
cut2 = (mag_i < a*z + b) #the z-dependant mag cut
cut3 = (sg == 3) #star/galaxy separator
cut4 = (np.bitwise_and(flags,120) == 0) #flags
joezuntz marked this conversation as resolved.
Show resolved Hide resolved

s = (cut1 & cut2 & cut3 & cut4).astype(np.int8)
return s

def calculate_tomography(self, pz_data, phot_data, lens_gals):

nbin = len(self.config["lens_zbin_edges"]) - 1
Expand Down Expand Up @@ -314,6 +337,8 @@ def data_iterator(self):
print(f"We are cheating and using the true redshift.")
chunk_rows = self.config["chunk_rows"]
phot_cols = ["mag_i", "mag_r", "mag_g", "redshift_true"]
extra_cols = [c for c in self.config["extra_cols"] if c]
phot_cols += extra_cols
# Input data. These are iterators - they lazily load chunks
# of the data one by one later when we do the for loop.
# This code can be run in parallel, and different processes will
Expand Down Expand Up @@ -343,6 +368,8 @@ def data_iterator(self):
phot_cols = ["mag_i", "mag_r", "mag_g"]
z_cols = ["zmean"]
rename = {"zmean": "z"}
extra_cols = [c for c in self.config["extra_cols"] if c]
phot_cols += extra_cols

it = self.combined_iterators(
chunk_rows,
Expand All @@ -356,7 +383,6 @@ def data_iterator(self):

return rename_iterated(it, rename)


class TXModeLensSelector(TXBaseLensSelector):
"""
Select lens objects based on best-fit redshifts and BOSS criteria
Expand All @@ -375,6 +401,8 @@ def data_iterator(self):
phot_cols = ["mag_i", "mag_r", "mag_g"]
z_cols = ["zmode"]
rename = {"zmode": "z"}
extra_cols = [c for c in self.config["extra_cols"] if c]
phot_cols += extra_cols

it = self.combined_iterators(
chunk_rows,
Expand Down Expand Up @@ -406,6 +434,8 @@ class TXRandomForestLensSelector(TXBaseLensSelector):
def data_iterator(self):
chunk_rows = self.config["chunk_rows"]
phot_cols = ["mag_u", "mag_g", "mag_r", "mag_i", "mag_z", "mag_y"]
extra_cols = [c for c in self.config["extra_cols"] if c]
phot_cols += extra_cols

for s, e, data in self.iterate_hdf(
"photometry_catalog", "photometry", phot_cols, chunk_rows
Expand Down
Loading