Skip to content

Commit

Permalink
Factory function framework (#207)
Browse files Browse the repository at this point in the history
* First version of the factory function framework for firecrown likelihoods.

* Added missing section name in the firecrown likelihood configuration section.

* Remove unused parameters

Co-authored-by: Marc Paterno <paterno@fnal.gov>
  • Loading branch information
vitenti and marcpaterno authored Nov 28, 2022
1 parent 2c6855f commit f2772ba
Show file tree
Hide file tree
Showing 18 changed files with 284 additions and 218 deletions.
3 changes: 2 additions & 1 deletion examples/cosmicshear/cosmicshear.ini
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
sampler = test
root = ${PWD}

[default]
[DEFAULT]
fatal_errors = T

[output]
Expand Down Expand Up @@ -48,6 +48,7 @@ file = ${FIRECROWN_DIR}/firecrown/connector/cosmosis/likelihood.py
;; code.
firecrown_config = ${FIRECROWN_DIR}/examples/cosmicshear/cosmicshear.py
require_nonlinear_pk = True
sampling_parameters_sections = firecrown_two_point

[test]
fatal_errors = T
Expand Down
114 changes: 56 additions & 58 deletions examples/cosmicshear/cosmicshear.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,73 +9,71 @@

import sacc

# Sources

def build_likelihood(_):
"""
Creating sources, each one maps to a specific section of a SACC file. In
this case trc0, trc1 describe weak-lensing probes. The sources are saved
in a dictionary since they will be used by one or more two-point function.
"""
sources = {}

"""
Creating sources, each one maps to a specific section of a SACC file. In
this case trc0, trc1 describe weak-lensing probes. The sources are saved
in a dictionary since they will be used by one or more two-point function.
"""
sources = {}
for i in range(2):
"""
We include a photo-z shift bias (a constant shift in dndz). We also
have a different parameter for each bin, so here again we use the
src{i}_ prefix.
"""
pzshift = wl.PhotoZShift(sacc_tracer=f"trc{i}")

"""
Now we can finally create the weak-lensing source that will compute the
theoretical prediction for that section of the data, given the
systematics.
"""
sources[f"trc{i}"] = wl.WeakLensing(sacc_tracer=f"trc{i}", systematics=[pzshift])

for i in range(2):
"""
We include a photo-z shift bias (a constant shift in dndz). We also
have a different parameter for each bin, so here again we use the
src{i}_ prefix.
Now that we have all sources we can instantiate all the two-point
functions. For each one we create a new two-point function object.
"""
pzshift = wl.PhotoZShift(sacc_tracer=f"trc{i}")
stats = {}

"""
Now we can finally create the weak-lensing source that will compute the
theoretical prediction for that section of the data, given the
systematics.
Creating all auto/cross-correlations two-point function objects for
the weak-lensing probes.
"""
sources[f"trc{i}"] = wl.WeakLensing(sacc_tracer=f"trc{i}", systematics=[pzshift])

"""
Now that we have all sources we can instantiate all the two-point
functions. For each one we create a new two-point function object.
"""
stats = {}
for i in range(2):
for j in range(i, 2):
stats[f"trc{i}_trc{j}"] = TwoPoint(
source0=sources[f"trc{i}"],
source1=sources[f"trc{j}"],
sacc_data_type="galaxy_shear_cl_ee",
)

"""
Creating all auto/cross-correlations two-point function objects for
the weak-lensing probes.
"""
for i in range(2):
for j in range(i, 2):
stats[f"trc{i}_trc{j}"] = TwoPoint(
source0=sources[f"trc{i}"],
source1=sources[f"trc{j}"],
sacc_data_type="galaxy_shear_cl_ee",
)

"""
Here we instantiate the actual likelihood. The statistics argument carry
the order of the data/theory vector.
"""
lk = ConstGaussian(statistics=list(stats.values()))
"""
Here we instantiate the actual likelihood. The statistics argument carry
the order of the data/theory vector.
"""
lk = ConstGaussian(statistics=list(stats.values()))

"""
We load the correct SACC file.
"""
saccfile = os.path.expanduser(
os.path.expandvars("${FIRECROWN_DIR}/examples/cosmicshear/cosmicshear.fits")
)
sacc_data = sacc.Sacc.load_fits(saccfile)
"""
We load the correct SACC file.
"""
saccfile = os.path.expanduser(
os.path.expandvars("${FIRECROWN_DIR}/examples/cosmicshear/cosmicshear.fits")
)
sacc_data = sacc.Sacc.load_fits(saccfile)

"""
The read likelihood method is called passing the loaded SACC file, the
two-point functions will receive the appropriated sections of the SACC
file and the sources their respective dndz.
"""
lk.read(sacc_data)
"""
The read likelihood method is called passing the loaded SACC file, the
two-point functions will receive the appropriated sections of the SACC
file and the sources their respective dndz.
"""
lk.read(sacc_data)

"""
This script will be loaded by the appropriated connector. The framework
then looks for the `likelihood` variable to find the instance that will
be used to compute the likelihood.
"""
likelihood = lk
"""
This script will be loaded by the appropriated connector. The framework
will call the factory function that should return a Likelihood instance.
"""
return lk
3 changes: 2 additions & 1 deletion examples/des_y1_3x2pt/des_y1_3x2pt.ini
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
sampler = test
root = ${PWD}

[default]
[DEFAULT]
fatal_errors = T

[output]
Expand Down Expand Up @@ -45,6 +45,7 @@ file = ${CSL_DIR}/utility/sample_sigma8/sigma8_rescale.py
file = ${FIRECROWN_DIR}/firecrown/connector/cosmosis/likelihood.py
likelihood_source = ${FIRECROWN_DIR}/examples/des_y1_3x2pt/des_y1_3x2pt.py
require_nonlinear_pk = True
sampling_parameters_sections = firecrown_two_point

[test]
fatal_errors = T
Expand Down
205 changes: 103 additions & 102 deletions examples/des_y1_3x2pt/des_y1_3x2pt.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,123 +14,124 @@
# Sources


lai_systematic = wl.LinearAlignmentSystematic(sacc_tracer="", alphag=None)
def build_likelihood(_):

"""
Creating sources, each one maps to a specific section of a SACC file. In
this case src0, src1, src2 and src3 describe weak-lensing probes. The
sources are saved in a dictionary since they will be used by one or more
two-point function.
"""
sources = {}
lai_systematic = wl.LinearAlignmentSystematic(sacc_tracer="", alphag=None)

for i in range(4):
"""
Each weak-lensing section has its own multiplicative bias. Parameters
reflect this by using src{i}_ prefix.
Creating sources, each one maps to a specific section of a SACC file. In
this case src0, src1, src2 and src3 describe weak-lensing probes. The
sources are saved in a dictionary since they will be used by one or more
two-point function.
"""
mbias = wl.MultiplicativeShearBias(sacc_tracer=f"src{i}")
sources = {}

"""
We also include a photo-z shift bias (a constant shift in dndz). We
also have a different parameter for each bin, so here again we use the
src{i}_ prefix.
"""
pzshift = wl.PhotoZShift(sacc_tracer=f"src{i}")
for i in range(4):
"""
Each weak-lensing section has its own multiplicative bias. Parameters
reflect this by using src{i}_ prefix.
"""
mbias = wl.MultiplicativeShearBias(sacc_tracer=f"src{i}")

"""
We also include a photo-z shift bias (a constant shift in dndz). We
also have a different parameter for each bin, so here again we use the
src{i}_ prefix.
"""
pzshift = wl.PhotoZShift(sacc_tracer=f"src{i}")

"""
Now we can finally create the weak-lensing source that will compute the
theoretical prediction for that section of the data, given the
systematics.
"""
sources[f"src{i}"] = wl.WeakLensing(
sacc_tracer=f"src{i}", systematics=[lai_systematic, mbias, pzshift]
)

"""
Now we can finally create the weak-lensing source that will compute the
theoretical prediction for that section of the data, given the
systematics.
"""
sources[f"src{i}"] = wl.WeakLensing(
sacc_tracer=f"src{i}", systematics=[lai_systematic, mbias, pzshift]
)
Creating the number counting sources. There are five sources each one
labeled by lens{i}.
"""
for i in range(5):
"""
We also include a photo-z shift for the dndz.
"""
pzshift = nc.PhotoZShift(sacc_tracer=f"lens{i}")

"""
The source is created and saved (temporarely in the sources dict).
"""
sources[f"lens{i}"] = nc.NumberCounts(sacc_tracer=f"lens{i}", systematics=[pzshift], derived_scale=True)

"""
Now that we have all sources we can instantiate all the two-point
functions. The weak-lensing sources have two "data types", for each one we
create a new two-point function.
"""
stats = {}
for stat, sacc_stat in [
("xip", "galaxy_shear_xi_plus"),
("xim", "galaxy_shear_xi_minus"),
]:
"""
Creating all auto/cross-correlations two-point function objects for the
weak-lensing probes.
"""
for i in range(4):
for j in range(i, 4):
stats[f"{stat}_src{i}_src{j}"] = TwoPoint(
source0=sources[f"src{i}"],
source1=sources[f"src{j}"],
sacc_data_type=sacc_stat,
)
"""
The following two-point function objects compute the cross correlations
between the weak-lensing and number count sources.
"""
for j in range(5):
for i in range(4):
stats[f"gammat_lens{j}_src{i}"] = TwoPoint(
source0=sources[f"lens{j}"],
source1=sources[f"src{i}"],
sacc_data_type="galaxy_shearDensity_xi_t",
)

"""
Creating the number counting sources. There are five sources each one
labeled by lens{i}.
"""
for i in range(5):
"""
We also include a photo-z shift for the dndz.
"""
pzshift = nc.PhotoZShift(sacc_tracer=f"lens{i}")
"""
Finally the instances for the lensing auto-correlations are created.
"""
for i in range(5):
stats[f"wtheta_lens{i}_lens{i}"] = TwoPoint(
source0=sources[f"lens{i}"],
source1=sources[f"lens{i}"],
sacc_data_type="galaxy_density_xi",
)

"""
The source is created and saved (temporarely in the sources dict).
"""
sources[f"lens{i}"] = nc.NumberCounts(sacc_tracer=f"lens{i}", systematics=[pzshift], derived_scale=True)

"""
Now that we have all sources we can instantiate all the two-point
functions. The weak-lensing sources have two "data types", for each one we
create a new two-point function.
"""
stats = {}
for stat, sacc_stat in [
("xip", "galaxy_shear_xi_plus"),
("xim", "galaxy_shear_xi_minus"),
]:
Here we instantiate the actual likelihood. The statistics argument carry
the order of the data/theory vector.
"""
Creating all auto/cross-correlations two-point function objects for the
weak-lensing probes.
"""
for i in range(4):
for j in range(i, 4):
stats[f"{stat}_src{i}_src{j}"] = TwoPoint(
source0=sources[f"src{i}"],
source1=sources[f"src{j}"],
sacc_data_type=sacc_stat,
)
lk = ConstGaussian(statistics=list(stats.values()))

"""
The following two-point function objects compute the cross correlations
between the weak-lensing and number count sources.
We load the correct SACC file.
"""
for j in range(5):
for i in range(4):
stats[f"gammat_lens{j}_src{i}"] = TwoPoint(
source0=sources[f"lens{j}"],
source1=sources[f"src{i}"],
sacc_data_type="galaxy_shearDensity_xi_t",
saccfile = os.path.expanduser(
os.path.expandvars(
"${FIRECROWN_DIR}/examples/des_y1_3x2pt/des_y1_3x2pt_sacc_data.fits"
)
)
sacc_data = sacc.Sacc.load_fits(saccfile)

"""
Finally the instances for the lensing auto-correlations are created.
The read likelihood method is called passing the loaded SACC file, the
two-point functions will receive the appropriated sections of the SACC
file and the sources their respective dndz.
"""
for i in range(5):
stats[f"wtheta_lens{i}_lens{i}"] = TwoPoint(
source0=sources[f"lens{i}"],
source1=sources[f"lens{i}"],
sacc_data_type="galaxy_density_xi",
)
lk.read(sacc_data)

"""
Here we instantiate the actual likelihood. The statistics argument carry
the order of the data/theory vector.
"""
lk = ConstGaussian(statistics=list(stats.values()))

"""
We load the correct SACC file.
"""
saccfile = os.path.expanduser(
os.path.expandvars(
"${FIRECROWN_DIR}/examples/des_y1_3x2pt/des_y1_3x2pt_sacc_data.fits"
)
)
sacc_data = sacc.Sacc.load_fits(saccfile)

"""
The read likelihood method is called passing the loaded SACC file, the
two-point functions will receive the appropriated sections of the SACC
file and the sources their respective dndz.
"""
lk.read(sacc_data)

"""
This script will be loaded by the appropriated connector. The framework
then looks for the `likelihood` variable to find the instance that will
be used to compute the likelihood.
"""
likelihood = lk
"""
This script will be loaded by the appropriated connector. The framework
will call the factory function that should return a Likelihood instance.
"""
return lk
Loading

0 comments on commit f2772ba

Please sign in to comment.