1- """Utilities for creating combinations of photo-z bins and measurements."""
1+ """Utilities for creating combinations of tomographic bins and measurements.
2+
3+ This module provides functions to generate two-point correlation combinations from
4+ tomographic redshift bins. It supports:
5+ - All possible galaxy-galaxy correlations
6+ - CMB-galaxy cross-correlations
7+ - Filtered combinations based on bin pair selectors
8+ """
29
310from itertools import product , chain
411
714import firecrown .metadata_types as mdt
815
916
17+ def _validate_list_of_inferred_galaxy_zdists (
18+ inferred_galaxy_zdists : list [mdt .InferredGalaxyZDist ],
19+ ) -> None :
20+ """Validate that tomographic bin names are unique.
21+
22+ :param inferred_galaxy_zdists: List of tomographic bins to validate.
23+
24+ :raises ValueError: If any bin names appear more than once in the list.
25+ """
26+ bin_names_set = set ()
27+ # Produce a list of duplicates
28+ bin_names = []
29+ for igz in inferred_galaxy_zdists :
30+ if igz .bin_name in bin_names_set :
31+ bin_names .append (igz .bin_name )
32+ else :
33+ bin_names_set .add (igz .bin_name )
34+
35+ if bin_names :
36+ raise ValueError (
37+ f"Duplicate inferred galaxy z distribution bin names found: { bin_names } "
38+ )
39+
40+
1041def make_all_photoz_bin_combinations (
1142 inferred_galaxy_zdists : list [mdt .InferredGalaxyZDist ],
1243) -> list [mdt .TwoPointXY ]:
13- """Extract the two-point function metadata from a sacc file."""
14- bin_combinations = [
15- mdt .TwoPointXY (
16- x = igz1 , y = igz2 , x_measurement = x_measurement , y_measurement = y_measurement
17- )
18- for igz1 , igz2 in product (inferred_galaxy_zdists , repeat = 2 )
19- for x_measurement , y_measurement in product (
20- igz1 .measurements , igz2 .measurements
21- )
22- if mdt .measurement_is_compatible (x_measurement , y_measurement )
44+ """Create all possible two-point correlation combinations for galaxy bins.
45+
46+ This function generates all possible pairs of (bin, measurement) combinations,
47+ keeping only those where the measurements are compatible. For auto-correlations
48+ (same measurement type), only unique pairs are kept to avoid duplicates
49+ (e.g., only bin0-bin1, not both bin0-bin1 and bin1-bin0).
50+
51+ :param inferred_galaxy_zdists: List of tomographic redshift bins with their
52+ associated measurement types.
53+
54+ :return: List of all valid TwoPointXY combinations.
55+
56+ :raises ValueError: If duplicate bin names are found in inferred_galaxy_zdists.
57+ """
58+ _validate_list_of_inferred_galaxy_zdists (inferred_galaxy_zdists )
59+ expanded = [
60+ (igz , m ) for igz in inferred_galaxy_zdists for m in igz .measurement_list
61+ ]
62+
63+ # Create all combinations of the expanded list, keeping only compatible ones
64+ # and avoiding duplicates in the case of correlations of the same type
65+ all_xy = [
66+ mdt .TwoPointXY (x = igz1 , y = igz2 , x_measurement = m1 , y_measurement = m2 )
67+ for (igz1 , m1 ), (igz2 , m2 ) in product (expanded , repeat = 2 )
68+ if mdt .measurement_is_compatible (m1 , m2 )
69+ and ((m1 != m2 ) or (igz2 .bin_name >= igz1 .bin_name ))
2370 ]
2471
25- return bin_combinations
72+ # Reorder expanded to have alphabetical order considering first measurements, then
73+ # bin names.
74+ return sorted (
75+ all_xy ,
76+ key = lambda xy : (
77+ xy .x_measurement ,
78+ xy .y_measurement ,
79+ xy .x .bin_name ,
80+ xy .y .bin_name ,
81+ ),
82+ )
2683
2784
2885def make_all_photoz_bin_combinations_with_cmb (
2986 inferred_galaxy_zdists : list [mdt .InferredGalaxyZDist ],
3087 cmb_tracer_name : str = "cmb_convergence" ,
3188 include_cmb_auto : bool = False ,
3289) -> list [mdt .TwoPointXY ]:
33- """Create all galaxy combinations plus mdt. CMB-galaxy cross-correlations .
90+ """Create all galaxy-galaxy and CMB-galaxy correlation combinations .
3491
35- :param inferred_galaxy_zdists: List of galaxy redshift bins
36- :param cmb_tracer_name: Name of the mdt.CMB tracer
37- :param include_cmb_auto: Whether to include mdt.CMB auto-correlation
38- (default: False)
39- :return: List of all XY combinations including mdt.CMB-galaxy crosses
92+ This function generates all possible two-point correlations including both
93+ galaxy-galaxy auto/cross-correlations and CMB-galaxy cross-correlations.
94+
95+ :param inferred_galaxy_zdists: List of galaxy redshift bins with their associated
96+ measurement types.
97+ :param cmb_tracer_name: Name to assign to the CMB tracer (default:
98+ "cmb_convergence").
99+ :param include_cmb_auto: Whether to include CMB auto-correlation (default: False).
100+
101+ :return: Combined list of galaxy-galaxy and CMB-galaxy correlation combinations.
102+
103+ :raises ValueError: If duplicate bin names are found in inferred_galaxy_zdists.
40104 """
105+ _validate_list_of_inferred_galaxy_zdists (inferred_galaxy_zdists )
41106 # Get all galaxy-galaxy combinations first
42107 galaxy_combinations = make_all_photoz_bin_combinations (inferred_galaxy_zdists )
43108 all_combinations = galaxy_combinations + make_cmb_galaxy_combinations_only (
@@ -52,12 +117,24 @@ def make_cmb_galaxy_combinations_only(
52117 cmb_tracer_name : str = "cmb_convergence" ,
53118 include_cmb_auto : bool = False ,
54119) -> list [mdt .TwoPointXY ]:
55- """Create only mdt.CMB-galaxy cross-correlations.
120+ """Create only CMB-galaxy cross-correlations.
121+
122+ This function generates cross-correlations between CMB convergence and galaxy
123+ measurements, optionally including the CMB auto-correlation. It does NOT include
124+ any galaxy-galaxy correlations.
125+
126+ :param inferred_galaxy_zdists: List of galaxy redshift bins with their
127+ associated measurement types.
128+ :param cmb_tracer_name: Name to assign to the CMB tracer (default:
129+ "cmb_convergence").
130+ :param include_cmb_auto: Whether to include CMB auto-correlation (default: False).
56131
57- :param inferred_galaxy_zdists: List of galaxy redshift bins
58- :param cmb_tracer_name: Name of the mdt.CMB tracer
59- :return: List of mdt.CMB-galaxy cross-correlation XY combinations only
132+ :return: List of CMB-galaxy cross-correlation combinations (and optionally CMB
133+ auto).
134+
135+ :raises ValueError: If duplicate bin names are found in inferred_galaxy_zdists.
60136 """
137+ _validate_list_of_inferred_galaxy_zdists (inferred_galaxy_zdists )
61138 # Create a mock mdt.CMB "bin"
62139 cmb_bin = mdt .InferredGalaxyZDist (
63140 bin_name = cmb_tracer_name ,
@@ -94,3 +171,35 @@ def make_cmb_galaxy_combinations_only(
94171 )
95172
96173 return cmb_galaxy_combinations
174+
175+
176+ def make_binned_two_point_filtered (
177+ inferred_galaxy_zdists : list [mdt .InferredGalaxyZDist ],
178+ bin_pair_selector : mdt .BinPairSelector ,
179+ ) -> list [mdt .TwoPointXY ]:
180+ """Create two-point correlations filtered by a bin pair selector.
181+
182+ This function generates all possible bin combinations and then filters them using
183+ the provided selector, keeping only pairs that satisfy the selection criteria
184+ (e.g., auto-correlations only, specific measurements, neighboring bins).
185+
186+ :param inferred_galaxy_zdists: List of tomographic redshift bins with their
187+ associated measurement types.
188+ :param bin_pair_selector: Selector defining which bin pairs to include.
189+
190+ :return: List of TwoPointXY combinations that pass the selector's criteria.
191+
192+ :raises ValueError: If duplicate bin names are found in inferred_galaxy_zdists.
193+
194+ Example:
195+ # Get only auto-correlations of source measurements
196+ selector = AutoNameBinPairSelector() & SourceBinPairSelector()
197+ combinations = make_binned_two_point_filtered(bins, selector)
198+ """
199+ _validate_list_of_inferred_galaxy_zdists (inferred_galaxy_zdists )
200+ all_bin_combinations = make_all_photoz_bin_combinations (inferred_galaxy_zdists )
201+ return [
202+ xy
203+ for xy in all_bin_combinations
204+ if bin_pair_selector .keep ((xy .x , xy .y ), (xy .x_measurement , xy .y_measurement ))
205+ ]
0 commit comments