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

API restoration #1083

Merged
merged 6 commits into from
May 9, 2023
Merged

API restoration #1083

merged 6 commits into from
May 9, 2023

Conversation

damonge
Copy link
Collaborator

@damonge damonge commented May 9, 2023

The current version of the code implements the v3 API while attempting to preserve the v2 API. In this PR, I made a copy of the unit tests and benchmarks (not part of the PR) as they were before any of these modifications took place (in the latest released version, v2.7). I then ran the tests, which use the pure v2 API, and ensured that they pass.

In doing so, I weeded out a couple of bugs and restored some pieces of code that strictly speaking are part of the v2 API.

Now the caveats. Not all old tests passed, but the reasons behind those that didn't pass are all reasonable:

  • Some function calls now raise errors with a different error type than in v2. The new error types are more appropriate, and hence this should be interpreted as a bugfix.
  • Some function calls don't raise an error or a warning anymore. These were again cases where we realised this was inappropriate (and in fact it helped close a few open issues), so it should be understood as a bugfix.
  • Base classes for things like MassFunctions , HaloBiass etc. cannot be instantiated anymore. It is doubtful that anyone was using them (as they are completely useless), so I think it's worth living with it as a very mild API breakage.
  • The array of neutrino masses that Cosmologys hold now always holds the right number of masses (before it had a 3-element array, regardless of how many massive neutrinos there were).
  • Some parameters that had a default value of None now have a default value. In all cases the default value attempts to mimic the behaviour of it being None in the old API.

I'll explain some of the changes as comments below.

@@ -694,7 +694,7 @@ def _get_halo_model_nonlin_power(self):
prf = hal.HaloProfileNFW(concentration=c)
hmc = hal.HMCalculator(mass_function=hmf, halo_bias=hbf,
mass_def=mdef)
return hal.halomod_Pk2D(self, hmc, prf)
return hal.halomod_Pk2D(self, hmc, prf, normprof1=True)
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

without this, the halo model power spectrum as a Cosmology power spectrum option (which will be deprecated in v3) gave the wrong answer.

if len(out) != 3:
raise ValueError("A valid mass function and halo bias is "
"needed")
self.mass_def, self.mass_function, self.halo_bias = out
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

This is just here to throw an actually useful error if any of these arguments are set to None (the corresponding python-level error was extremely obscure)

warnings.warn("In v3 all profiles will need an associated "
"mass definition.", CCLDeprecationWarning)
prof.mass_def = self.mass_def

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

This was necessary in order to ensure that we could use profiles that were instantiated without a mass definition attached to them from scratch (this will not be possible in v3, but it was in v2).

bA = i11_A / nA if isinstance(pA, ProfNC) else np.zeros_like(k_use)
bB = i11_B / nB if isinstance(pB, ProfNC) else np.zeros_like(k_use)
bA = i11_A / nA if pA.is_number_counts else np.zeros_like(k_use)
bB = i11_B / nB if pB.is_number_counts else np.zeros_like(k_use)
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Using profile class type broke the API.

"``None`` will be deprecated in future versions.",
CCLDeprecationWarning)
name = DEFAULT_POWER_SPECTRUM
elif isinstance(p_of_k_a, str):
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

This was to throw an actually useful warning.

if (prof, prof2, prof12_2pt) == (prof3, prof4, prof34_2pt):
dpk34[ia] -= dpk12[ia]
dpk34[ia] = dpk12[ia]
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

this was a huge bug

assert issubclass(w[0].category, wtype), \
"Warning raised was the wrong type (got %s, expected %s)" % (
w[0].category, wtype)
return res
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

formally speaking, this was part of the API, even though we don't really use it.

@@ -128,3 +130,119 @@ def test_tkkssc_linear_bias_raises():
"""Test that it raises if the profile is not NFW."""
with pytest.raises(TypeError):
ccl.halos.halomod_Tk3D_SSC_linear_bias(COSMO, HMC, prof=GNFW)

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

This restores a test that was lost in the last few PRs, but which is quite useful (it actually uncovered the bug mentioned above)

@damonge damonge marked this pull request as ready for review May 9, 2023 11:06
@damonge damonge requested a review from carlosggarcia May 9, 2023 11:06
@coveralls
Copy link

Pull Request Test Coverage Report for Build 4925502085

  • 39 of 52 (75.0%) changed or added relevant lines in 7 files are covered.
  • No unchanged relevant lines lost coverage.
  • Overall coverage decreased (-0.1%) to 97.159%

Changes Missing Coverage Covered Lines Changed/Added Lines %
pyccl/pk2d.py 2 4 50.0%
pyccl/halos/halo_model.py 15 19 78.95%
pyccl/pyutils.py 3 10 30.0%
Totals Coverage Status
Change from base Build 4898423591: -0.1%
Covered Lines: 5847
Relevant Lines: 6018

💛 - Coveralls

Copy link
Contributor

@carlosggarcia carlosggarcia left a comment

Choose a reason for hiding this comment

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

Just a few comments regarding the mass definitions. Other than that, it looks good.

Copy link
Contributor

@carlosggarcia carlosggarcia left a comment

Choose a reason for hiding this comment

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

LGTM!

@damonge damonge merged commit 7f019f5 into master May 9, 2023
@damonge damonge deleted the restore_api branch May 9, 2023 15:59
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants