Skip to content

covmat-by-reference-bug#2463

Merged
scarlehoff merged 5 commits into
masterfrom
comat-by-reference-bug
Apr 30, 2026
Merged

covmat-by-reference-bug#2463
scarlehoff merged 5 commits into
masterfrom
comat-by-reference-bug

Conversation

@jacoterh
Copy link
Copy Markdown
Collaborator

@jacoterh jacoterh commented Apr 29, 2026

This PR addresses a bug in the construction of the total covariance matrix entering the fit:

def dataset_inputs_t0_total_covmat(dataset_inputs_t0_exp_covmat, loaded_theory_covmat):
"""
Function to compute the covmat to be used for the sampling by make_replica and for the chi2
by fitting_data_dict. In this case the t0 prescription is used for the experimental covmat
and the multiplicative errors are included in it. Moreover, the theory covmat is added to experimental covmat.
"""
covmat = dataset_inputs_t0_exp_covmat
covmat += loaded_theory_covmat
return covmat

The line

covmat = dataset_inputs_t0_exp_covmat

links covmat to dataset_inputs_t0_exp_covmat by reference, i.e. creates an alias, such that any subsequent changes to covmat also mutate the original object dataset_inputs_t0_exp_covmat .

  • We have checked that n3fit calls/resolves the provider dataset_inputs_t0_total_covmat twice before starting the fit, which means that dataset_inputs_t0_exp_covmat already includes the theory covmat after the first call such that the returned object that enters the fit after the second call is the experimental covmat plus twice the theory covmat.
  • We have run an agent and it turns out this behaviour shows up in covmats.py in 3 other places. This PR fixes this too. It also added a dedicated test to check whether things are safe now.
  • The filter utils function has also been modified for safety, because also here we modify the argument directly. I don't think this matters too much because the filter utils are not part of report engine, but better safe than sorry.

Report comparing the impact of the bug before and after the fix: https://vp.nnpdf.science/Gx02F1SqSaKSvTtOLhXFZg==/

@jacoterh jacoterh requested a review from achiefa April 29, 2026 09:49
@jacoterh jacoterh changed the title comat-by-reference-bug covmat-by-reference-bug Apr 29, 2026
@achiefa achiefa added the bug Something isn't working label Apr 29, 2026
@achiefa
Copy link
Copy Markdown
Contributor

achiefa commented Apr 29, 2026

Hi @jacoterh, thanks for opening this PR. I'll have a look asap!

Copy link
Copy Markdown
Contributor

@achiefa achiefa left a comment

Choose a reason for hiding this comment

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

It looks good to me. I just want to hear whether @scarlehoff has something to say on this matter.

"""
for i in range(len(list_of_matrices)):
list_of_matrices[i] = np.array(list_of_matrices[i])
matrices = [np.array(matrix) for matrix in list_of_matrices]
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I'm OK with this. I just want to check if this affects any of the datasets. regenerate_commondata should tell us if there's any difference.

assert ld.ndata == 1
ld.systematic_errors(t0_predictions)


Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

The test is fine per se, but I'd like to hear @scarlehoff's opinion. In my view, the test prevents us from repeating the same mistake, although now that we are aware of the issue it might seem a bit superfluous. Still, I think we should keep it as a reminder for the future and as a signal for new members.

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.

Many of our tests are built on the blood of our previous mistakes. I'm happy with superflous tests.

@jacoterh
Copy link
Copy Markdown
Collaborator Author

Why do we get a failing test because of some eko incompatibility?

@scarlehoff
Copy link
Copy Markdown
Member

It looks like the macos workers in github have changed (and have python 3.14 now? we should not be testing that)

@achiefa
Copy link
Copy Markdown
Contributor

achiefa commented Apr 30, 2026

Does this mean that we can merge this? Is it possible to change the Python version un the MacOS runners?

@scarlehoff
Copy link
Copy Markdown
Member

No, it means that we need to look over the github docs to understand what changed (or what are we doing wrong in the workflow for mac os)

shell: bash -l {0}
run: |
conda -n base install conda-build --yes
conda install -n base conda-build --yes
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.

If I was both clever and handsome I would be too powerful for this world.
Anyway, this can be merged I guess.

(I've done this change because reading https://github.com/conda-incubator/setup-miniconda#important I thought macos, which has its own flavour of bash due to licence issues, might be messing with the environment activation, but I don't fully know why this fixes it other than "apple bad")

Copy link
Copy Markdown
Member

@scarlehoff scarlehoff left a comment

Choose a reason for hiding this comment

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

Thanks for this. Please make sure to put here also the report you did comparing the fit with and without the bugfix.

Comment thread nnpdf_data/nnpdf_data/filter_utils/utils.py Outdated
Copy link
Copy Markdown
Contributor

@achiefa achiefa left a comment

Choose a reason for hiding this comment

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

Also green light from me. Thanks @jacoterh !

@jacoterh
Copy link
Copy Markdown
Collaborator Author

Somehow I'm not authorised to merge? I see "Merging is blocked. You're not authorised to push to this branch". Weird.

@scarlehoff scarlehoff merged commit bb30db4 into master Apr 30, 2026
12 checks passed
@scarlehoff scarlehoff deleted the comat-by-reference-bug branch April 30, 2026 14:13
@scarlehoff
Copy link
Copy Markdown
Member

I wanted to put strong protections to master. Perhaps I exaggerated :__ I'll double check

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working regenerate-data

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants