Skip to content

gh-209: implement mgclass - #302

Merged
gcanasherrera merged 74 commits into
mainfrom
290_mgclass_BZ
Jul 31, 2026
Merged

gh-209: implement mgclass#302
gcanasherrera merged 74 commits into
mainfrom
290_mgclass_BZ

Conversation

@zsirap

@zsirap zsirap commented Oct 23, 2025

Copy link
Copy Markdown
Collaborator

🚀 Pull Request Checklist

✅ Summary

closes #290

🔄 Changes

  • allows the user to call additional parameters for the different models in MGCLASS
  • allows to import separate CLASS and MGCLASS in the same python script

🛠 How to Test

📝 Documentation

  • This PR updates documentation

🏗 Related Issues

Resolves #290

📸 Screenshots (if applicable)

📌 Additional Notes


✅ PR Checklist for Developers

  • I have titled this PR before merging as "gh-#:", where "#" represents the task it closes
  • I have run locally pre-commit using pre-commit run --all-files
  • I have tested my changes locally
  • No new warnings or errors introduced
  • I have updated documentation (if applicable)
  • My changes do not introduce breaking changes (i.e: the package still gets installed)
  • I have added unit tests (if applicable)
  • I have consistently updated the GitHub information for the project, including milestones, task types, and other relevant details.

✅ PR Checklist for Reviewers

  • The next PR targets the correct branch
  • CI tests have run and passed for the latest commit on the source branch
  • Check that the code can still be installed if new packages are imported
  • If necessary, the notebooks in the playground will be updated in a corresponding follow-up PR
  • Coverage percentage is retained or increased
  • Quality of new/changed code is acceptable
  • Quality of new/changed unit tests is acceptable
  • No data files have been included in the commits
  • Implementation follows the agreed task description point by point
  • Check that there are no No newline at the end of file warnings
  • Check that any added folder/file has been added to the README.md file
  • Check that the implementation follows the contributing guidelines and style choices
  • Check that the documentation has been updated accordantly
  • Check that the corresponding branch has been deleted after merging. If not, delete it

@zsirap
zsirap requested a review from gcanasherrera October 23, 2025 01:11
@zsirap

zsirap commented Oct 23, 2025

Copy link
Copy Markdown
Collaborator Author

I am still getting style errors that I don't know the origin (though locally the style test and mglass test pass) if you can help on that @gcanasherrera

update: i see other PRs have some similar issues ... later i might recheck again to see if i manage to solve mine .. for time being i labeled the PR as draft but it can be already reviewed at your or any other reviewer ease

@gcanasherrera

Copy link
Copy Markdown
Member

Make sure this title of this PR is properly named

@gcanasherrera gcanasherrera added background-perturbations Tasks related to Background and Perturbations th-1 labels Oct 23, 2025
@gcanasherrera gcanasherrera added this to the v0.11 milestone Oct 23, 2025
@zsirap zsirap changed the title 290 mgclass bz 290 mgclass integration Oct 23, 2025
@zsirap
zsirap marked this pull request as ready for review October 29, 2025 02:24
@zsirap

zsirap commented Oct 29, 2025

Copy link
Copy Markdown
Collaborator Author

not a draft anymore ... it only fails now because MGCLASS is an external code and is not automatically installed ... otherwise it passes locally ...

@gcanasherrera

Copy link
Copy Markdown
Member

Hi @zsirap,

Before I jump into the reviewing and into addressing the issue on how to install MGCLASS into the CI environment, could you please make sure that you are following properly the requests specified in the PR Checklist for Developers?

For instance, you have clicked as done "I have titled this PR before merging as "gh-#:", where "#" represents the task it closes", however you have not done it.

Also, make sure you have a proper picture that you would like to share in your GitHub profile so that the all-contributors bot gives you visibility accordantly (if you want to keep the default GitHub picture, too, it's also fine).

@zsirap zsirap changed the title 290 mgclass integration 290 implement mgclass Oct 29, 2025
@zsirap zsirap changed the title 290 implement mgclass gh-implement mgclass Oct 29, 2025
@zsirap zsirap changed the title gh-implement mgclass gh-209: implement mgclass Oct 29, 2025
@zsirap

zsirap commented Oct 29, 2025

Copy link
Copy Markdown
Collaborator Author

For instance, you have clicked as done "I have titled this PR before merging as "gh-#:", where "#" represents the task it closes", however you have not done it.

ah i thought you meant by this previous comment changing the title to make it more suitable ...

Make sure this title of this PR is properly named

my bad .. ok done now i hope ...

Before I jump into the reviewing

no rush .. at your ease @gcanasherrera ..

if you want to keep the default GitHub picture, too, it's also fine

for the moment ... maybe later

@gcanasherrera

gcanasherrera commented Oct 29, 2025

Copy link
Copy Markdown
Member

Hi @zsirap,

Thanks for this. As you're one of the main users of MGCLASS, I was wondering: why don't you make it pip installable so it is easier for us to install, also in the CI?
CC- @matmartinelli

@gcanasherrera gcanasherrera left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Please, adapt the names to MGCLASS instead of CLASS only everywhere

Comment thread README.md Outdated
Comment thread README.md Outdated
Comment thread README.md Outdated
Comment thread cloelib/cosmology/mgclass_cosmology.py Outdated
Comment thread cloelib/cosmology/mgclass_cosmology.py Outdated
Comment thread cloelib/cosmology/mgclass_cosmology.py Outdated
Comment thread cloelib/cosmology/mgclass_cosmology.py Outdated
Comment thread cloelib/cosmology/mgclass_cosmology.py Outdated
Comment thread tests/test_mgclass_cosmology.py
Comment thread tests/test_mgclass_cosmology.py Outdated
@gcanasherrera
gcanasherrera marked this pull request as ready for review May 4, 2026 11:59
@gcanasherrera

Copy link
Copy Markdown
Member

You're assigned to this one @santiagocasas, you're more than a CLASS person than me.

@gcanasherrera

Copy link
Copy Markdown
Member

@zsirap (CC- @santiagocasas):

note how class is now interfacing in the main branch, where it requires an optional linear module to be passed to the non-linear module. This is to allow for cloelike interface. Please, follow the same approach in mgclass

@santiagocasas

Copy link
Copy Markdown
Member

@gcanasherrera yes, I am taking a look into this. But regarding your comment on the linear module being passed, I think we should make mgclass and hi_class more modular. We are currently duplicating a lot of boilerplate code for background for instance, and this is not only dangerous but difficult to maintain (cloelib/cosmology/mgclass_cosmology.py).

Let me think about this for a day.

@santiagocasas

Copy link
Copy Markdown
Member

thanks @zsirap for your work on this so far

@gcanasherrera gcanasherrera added the priority For PM purposes label May 11, 2026

@santiagocasas santiagocasas left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

all in all, quite close to finishing, but a few things need revision. Especially Protocol adherence and the physical error (double-check!) in Omega_cb and so on. See below! thanks!

Comment thread .github/workflows/ci.yaml Outdated
run: |
mamba run -n $ENV_NAME python -m pip install --upgrade pip
mamba run -n $ENV_NAME python -m pip install ".[camb,classy,euclidemu2,baccoemu]"
- name: Install external prereq using pip

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

please don't install mgclassy like that. Use the same format as for the other external codes and solve the conflict, so that all codes are there. There is no need to specify the version, this is installed via the pyproject.toml and this is set there already: mgclassy = ["mgclassy==2.9.4.5"]

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

I agree but for a reason not yet known, mgclass is not installing if i use the same format as the others (I might give it a try again to see if it works again but for the moment if there is urgency we can merge it like that)

a general comment mentioned before and related to this and subsequent comments is that there is a new version of MGCLASS, not released yet, that will be built above a newer or the latest version of CLASS ... but here we started to implement MGCLASS in CLOE as it is now based on an older version of CLASS because also we wanted to be quick and pragmatic and we wanted it to not install above CLASS like other patches do ... and so in trying to do so, one thing led to another such as had to make a pip installable version of what we have now ...

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I will comment on this below

self.Omega_cdm0 = Omega_cdm0
self.Omega_k0 = Omega_k0
self.As = As
self.ns = ns

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

please rebase to latest version of main and add here the new requirement in the Background protocol, the parameter alpha_s. See here:

alpha_s: float = 0.0,

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Done

Comment thread cloelib/cosmology/mgclass_cosmology.py Outdated
[
(self.results.Omega_b() + self.results.Omega0_cdm())
* (1 + z) ** 3.0
/ (self.hubble_parameter(z) / self.H0) ** 0.5

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

why the square root of E(z)? , it should be (E(z))^2.
If possible, simply use the exposed variables as CLASS does: return self.results.Om_b(zs) + self.results.Om_cdm(zs).
Also, avoid for-loops. These functions are usually broadcastable.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

yes you are right about Ez ... it must be that I wanted to put E(z) value here by mistake ... fixed

Also, avoid for-loops. These functions are usually broadcastable.

yes but they are not broadcastable in this version of CLASS

Comment thread tests/test_mgclass_cosmology.py Outdated
return {"Linear": mgclass_lin, "NonLinear": mgclass_non}


@pytest.mark.parametrize("key", ["Linear"])

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

change to @pytest.mark.parametrize("key", ["Linear", "NonLinear"]) so you also test the NonLinear adherence to the protocol

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

fixed and fixed everything related to this change

)

return D_z_k

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

for MGCLASSNonLinearPerturbations to adhere to the Perturbations protocol, it needs to have a growth_rate, see here:

def growth_rate(self) -> np.ndarray:

Tests were passing because you were not testing protocol adherence in NonLinear. See my comment in tests/test_mgclass_cosmology.py

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

fixed as I replied to a previous comment


# add neutrinos:
mgclass_background_instance.interface_args["MGCLASSparams"]["N_ncdm"] = 1
mgclass_background_instance.interface_args["MGCLASSparams"]["m_ncdm"] = 0.2

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

using this here, while leaving these other parameters as:
mgclass_background_instance.mnu == 0.0 mgclass_background_instance.N_mnu == 0
is inconsistent, in case someone uses another parametrization. Tests might fail or pass for the wrong reason

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Maybe but I think I followed the same as it is implemented in the plain CLASS test .. so should we change here and there ?

@gcanasherrera

Copy link
Copy Markdown
Member

@zsirap do you have any time scale for addressing these comments?

@zsirap

zsirap commented May 19, 2026

Copy link
Copy Markdown
Collaborator Author

@zsirap do you have any time scale for addressing these comments?

was in a workshop ... will start addressing them from today ... thanks for reminding me and thanks to @santiagocasas for the extra review

@santiagocasas

Copy link
Copy Markdown
Member

@gcanasherrera @zsirap very busy week unfortunately, but I will look into this in between traveling, hopefully by end of the week!

@santiagocasas

Copy link
Copy Markdown
Member

@zsirap thanks for your patience with this and apologies for the late reply.

I have a few comments still for the PR, some of them can be solved now, some of them I have patched myself and some require a larger coordination with the other classy-related EBS codes.

MGCLASSY GCC Installation Problem
mgclassy==2.9.4.5 is distributed as a source package and must compile its C code during installation.
The failure occurs with Conda’s GCC 15.2.0 because GCC 15 defaults to GNU C23. MGCLASSY contains legacy declarations such as:
extern int evolver_ndf15();
int (*generic_evolver)();
In older C standards, an empty parameter list means “parameters unspecified.” In C23, it means “takes no parameters.” MGCLASSY later calls these functions with arguments, while the headers declare them with full prototypes. GCC therefore reports errors such as:
conflicting types for 'evolver_ndf15'
assignment from incompatible function pointer type
too many arguments to function 'generic_evolver'
This is not a missing GSL or Python dependency problem. The same source builds with system GCC 9, or with GCC 15 when compiled using GNU C17:
CFLAGS="-std=gnu17"

@santiagocasas

santiagocasas commented Jul 23, 2026

Copy link
Copy Markdown
Member

@zsirap I have patched the .github/workflows/ci.yaml so that:
one can install mgclassy from the pyproject.toml dependencies as in the other code, all within the same environment

@zsirap what is the repo for mgclassy? I would recommend that in the future or current mgclassy, this is fixed by:

1. Replace empty parameter lists with explicit function prototypes.
2. Compile the project with -std=gnu17 or another supported pre-C23 standard.
3. Ensure the Makefile respects externally supplied CC, CFLAGS, and similar variables instead of overriding them.

if you point me to it, I might even make a PR myself there, once I have some time.

@santiagocasas

Copy link
Copy Markdown
Member

@zsirap there was an annoying print coming directly from the C implementation of MGCLASS. This is not good for codes that do CI, testing or when we have to run cloelib/like in production environments.
I have also patched it in another commit.
The annoying banner was not from pytest or CLOE. It came from unconditional printf() calls in MGCLASSY’s C source:
if (pba->mg_ansatz != GR) {
printf("Modified Gravity extensions enabled...");
...
}
Since this release has no verbosity parameter for that output, CLOE suppresses it around each MGCLASS compute() call.
The wrapper:

  1. Saves the current stdout file descriptor.
  2. Redirects stdout to /dev/null.
  3. Runs self.results.compute().
  4. Flushes native C streams using:
    ctypes.CDLL(None).fflush(None)
  5. Restores the original stdout descriptor.
    This is implemented in:
    cloelib/cosmology/mgclass_cosmology.py

In the future, @zsirap , please implement a verbosity parameter for MGCLASSY.

@santiagocasas santiagocasas left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

@zsirap please check that my patches work on your end. Install the cloe-org-environment and install mgclassy with the pip install of cloelib.
If this works, make sure the tests pass and that you agree with my patches.
One more thing, tests currently use: mg_E11=0 and mg_E22=0, you might want to test also some MG functionality.
Once this is checked, ready to merge!
Thanks for your work!

@santiagocasas santiagocasas left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Tests are passing locally and on the CI with the new patches. After the general update of cloe-org-environments, this can be merged,

@santiagocasas

Copy link
Copy Markdown
Member

@gcanasherrera I think you can now safely remove your changes suggestions or click on approve. I will merge after the update to the cloe-org-environments.

@zsirap

zsirap commented Jul 31, 2026

Copy link
Copy Markdown
Collaborator Author

@zsirap please check that my patches work on your end. Install the cloe-org-environment and install mgclassy with the pip install of cloelib. If this works, make sure the tests pass and that you agree with my patches. One more thing, tests currently use: mg_E11=0 and mg_E22=0, you might want to test also some MG functionality. Once this is checked, ready to merge! Thanks for your work!

Thank you actually @santiagocasas !! great fix and effort to bring this PR to completion ... Wanted but haven't had time (on the move the past week) to thoroughly check or reply to some of your comments or try to implement others you suggested ... hopefully in the coming days (from my side only since summer break is starting so everything off for the next month) ...

But let's not delay further the merge of this PR and merge it as you suggested ... Anything I want to reply, comment or implement, i can open an issue for that later ...

Thank you again :)

@santiagocasas

Copy link
Copy Markdown
Member

@gcanasherrera since #566 was merged and passed, I did one final commit here to double check. This should pass and can be merged now.

@gcanasherrera
gcanasherrera merged commit 0373cef into main Jul 31, 2026
8 checks passed
@gcanasherrera
gcanasherrera deleted the 290_mgclass_BZ branch July 31, 2026 13:03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

background-perturbations Tasks related to Background and Perturbations priority For PM purposes th-1

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Implement MGCLASS

4 participants