-
Notifications
You must be signed in to change notification settings - Fork 14
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
Work on CCL module for cross-correlations #16
Conversation
If ccl is going to be loaded from the global init, should probably make pyccl load dynamically (otherwise it will error if people don't have pyccl installed - e.g. anyone on Windows). There seem to be two Tester classes here, can the one in ccl.py be deleted? |
Done (I think) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
See the various inline comments-- main point is to give the likelihood in ccl.py a name, make it inherit from GaussianLikelihood, and expand the testing. See some of the guidelines here for guidance; also feel free to ask me for clarification on any of these points. I've also just today turned on the github actions CI to run on pull requests (it wasn't on before), so we need to make sure those pass.
soliket/__init__.py
Outdated
@@ -3,4 +3,4 @@ | |||
from .ps import PSLikelihood, BinnedPSLikelihood | |||
from .clusters import ClusterLikelihood | |||
from .mflike import MFLike | |||
from .ccl import CCL | |||
from .ccl import CCL, Tester |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why is Tester
imported at the package top level like this? If it's something just for testing, it should just be in the test module, not imported here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I also prefer putting a try/except block to import CCL here rather than the dynamic import in ccl.py; makes more sense to have the error caught at import time here rather than runtime.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It depends on whether you want access to static properties of CCL class without having pyccl installed (e.g. for auto-installing it from cobaya install scripts - which is why mostly dynamical imports in the Cobaya default likelihoods).
soliket/ccl.py
Outdated
|
||
def initialize(self): | ||
self.ccl = __import__("pyccl") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
see above regarding importing. I think top-level import of pyccl is fine here.
soliket/ccl.py
Outdated
|
||
def initialize(self): | ||
self.ccl = __import__("pyccl") | ||
self._var_pairs = set() | ||
self._required_results = {} | ||
|
||
def get_requirements(self): | ||
# These are currently required to construct a CCL cosmology object. | ||
# Ultimately CCL should depend only on observable not parameters | ||
# 'As' could be substituted by sigma8. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Update comment here to remove reference to 'As', since it's no longer there.
soliket/ccl.py
Outdated
# Create a CCL cosmology object. Because we are giving it background | ||
# quantities, it should not depend on the cosmology parameters given | ||
cosmo = self.ccl.CosmologyCalculator( | ||
Omega_c=Omega_c, Omega_b=Omega_b, h=h, sigma8=0.8, n_s=0.96, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would feel a bit more comfortable with sigma8
and n_s
coming from the provider, unless @damonge tells me it's 100% OK not to do so, not only for this current use case, but for plausible future ones.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If you're passing linear power spectra, sigma8 and n_s should be redundant. You're not doing that here, but see my comment below.
soliket/ccl.py
Outdated
'delta_matter:delta_matter': Pk_lin}) | ||
cosmo._init_growth({'a': a, | ||
'growth_factor': growth, | ||
'growth_rate': fgrowth}) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this kosher CCL API usage? My understanding from @damonge is that growth and pk should get set as part of the initialization of the CosmologyCalculator
. Maybe we can move the cosmo
initialization happen later on after the growth, pk stuff is figured out?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, indeed. This should be done when initializing the CosmologyCalculator
. Calling any _whatever
method is a bad sign :-). Incidentally, you no longer need to pass growth arrays to CCL if you don't want to (no core quantities depend on them anymore).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah that's great, I will change that, thanks!
soliket/ccl.py
Outdated
class Tester(Likelihood): | ||
# Cross and auto data | ||
|
||
auto_file: str = 'input/clgg_noiseless.txt' |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
what are these paths relative to? See some of the other likelihoods for how to package data. Options might be making it installable, or including the data in the package and finding it with resource_filename
.
soliket/ccl.py
Outdated
#dcl_file: str = "/Users/Pablo/Code/SOLikeT/soliket/data/simulated_ccl/dcls.npy" | ||
|
||
def initialize(self): | ||
self.ccl = __import__("pyccl") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
see above comment about global pyccl; I think we should stick with that.
soliket/ccl.py
Outdated
|
||
#ell_file: str = "/Users/Pablo/Code/SOLikeT/soliket/data/simulated_ccl/ell.npy" | ||
#cl_file: str = "/Users/Pablo/Code/SOLikeT/soliket/data/simulated_ccl/cls.npy" | ||
#dcl_file: str = "/Users/Pablo/Code/SOLikeT/soliket/data/simulated_ccl/dcls.npy" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
remove unused code, here and elsewhere
soliket/ccl.py
Outdated
sigma = np.concatenate([self.cl_auto_err, self.cl_cross_err]) | ||
chi2 = delta**2/sigma**2. | ||
#chi2 = np.dot(r, self.invcov.dot(r)) | ||
return -0.5 * np.sum(chi2) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since this is a gaussian likelihood, this likelihood should inherit from GaussianLikelihood
, and define the appropriate _get_data
, _get_cov
, and _get_theory
methods.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
(see some of the other likelihoods for examples, or ask me for more clarification)
soliket/tests/test_ccl.py
Outdated
model = get_model(info) | ||
loglikes, derived = model.loglikes({"b_hydro": 0.3}) | ||
assert loglikes[0] == 0.3 | ||
print('OK') |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This test file should contain various functions named test_...
that will get picked up and run by pytest. Including to testing this Tester
likelihood here, you should also have functions that test the cross-correlation likelihood implemented in ccl.py
. Again, see some of the other likelihoods for examples. There should also be an example test_[ ].yaml
file that contains the specification of a model for test running purposes (again, see test_*.yaml
in this dir)
I just turned on the GH actions to run on PRs; I'm hoping it will run when you add more commits here. |
BTW, I think I sent this to @Pablo-Lemos and @timothydmorton , but just in case it helps, this is a similar implementation of a CCL theory class we've been using as a prototype in DESC: https://github.com/damonge/ShCl_like/blob/master/shcl_like/clccl.py |
Regarding imports/different versions - presumably depends on whether the CCL Theory class will soon be part of the ccl package, or if there will be separate versions maintained separately for each expt and/or in Cobaya. Probably the most logical place for it is in CCL, so CCL can keep it consistent with any CCL changes; people would then just e.g. theory "pyccl.ccl" to use it directly from the pyccl package. |
In any case, likelihoods and their tests should probably be in separate files from general ccl interfaces. |
I can push for this to be part of CCL soon-ish. First it might be good to identify if the current structure here or in the repo I sent before works well for all purposes. So how about @Pablo-Lemos finishes this implementation and then I/we look into making it part of CCL (possibly generalising things a bit) |
That sounds great! Happy to work towards making it more general |
I have tried to address all the points (thanks everyone) |
Thanks - perhaps put the likelihood in a separate file, to keep it distinct from more-general and solikeit-independent CCL? Rather than sigma8=0.8, n_s=0.96 it might be safer to fix to some obviously crazy values to make sure they don't get used by mistake (like 0 and -1)? I think the suggestion about test naming was to put all the test code in a function like "test_cross_correlation_likelihood()". - that's how pytest files are normally arranged if not using classes (rather than in the .py file body) |
Yes, I agree with @cmbant - I'd put the likelihood in a separate file, add another I also agree that the natural home for the CCL theory is in pyccl, so we should eventually expect that, but happy to have this be a development sandbox for now. @damonge just let us know when it makes sense for y'all to pull this (or the other version) into pyccl, and we can make sure these likelihoods are updated appropriately, and |
I have tried, but I am not familiar with pytest at all, so do let me know if it's not right! |
Hi @timothydmorton , I tried to address all your points but there seem to be some conflicts now. Is there anything I can do to fix them? Or do you have to approve the changes? |
You can just make another push to your branch to fix conflicts |
Did that do it? |
soliket/cross_correlation.py
Outdated
|
||
class CrossCorrelationLikelihood(GaussianLikelihood): | ||
def initialize(self): | ||
self.auto_file: str = 'soliket/data/xcorr_simulated/clgg_noiseless.txt' |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
All these properties should be class-level properties or set in the .yaml file for the likelihood (not set within .initialize()
), and paths should be obtained using get_resource_filename
, if the data is to be packaged within soliket.
soliket/test_cross_correlation.yaml
Outdated
extra_args: | ||
num_massive_neutrinos: 0 | ||
soliket.ccl.CCL: | ||
python_path: /Users/Pablo/Code/SOLikeT/ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
See above.
soliket/test_cross_correlation.yaml
Outdated
sigma8: | ||
likelihood: | ||
soliket.cross_correlation.CrossCorrelationLikelihood: | ||
python_path: /Users/Pablo/Code/SOLikeT/ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should not be here. Shouldn't have to hard-code paths like this anywhere.
soliket/tests/test_runs.py
Outdated
@pytest.mark.parametrize("lhood", ["mflike", "lensing", "lensing_lite", "multi"]) | ||
def test_evaluate(lhood): | ||
@pytest.mark.parametrize("lhood", ["mflike", "lensing", "lensing_lite", "multi, cross_correlation"]) | ||
def test_run(lhood): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should be changed back to test_evaluate
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
And below, please add cross_correlation
to the test_mcmc
parameter list as well.
You will see now that the CI tests have failed. You will need to ensure that the xcorr test data is included in the package (you may need to add this to the |
Hi @timothydmorton, thanks for the comments, I think it has all been addressed |
Nice job @Pablo-Lemos, thanks! Sorry this took so long for me to get to! |
* Added CCL CosmologyCalculator functionality * Added CCL CosmologyCalculator functionality * Added notebook for testing * Added ccl likelihood * small changes * Dynamically importing ccl * Changed import back to top level * Renamed Tester likelihood and added data files * Change creation of CCL cosmo object * Change likelihood to use GaussianLikelihood structure * Removed old comments * Separate likelihood file and tests * Moved test file and other small changes * Removed python path * Updated example notebook * Updated assert statement * Changed max_tries * Increased tolerance of assert * fixed bug
No description provided.