-
Notifications
You must be signed in to change notification settings - Fork 14
Crash when combining point prescriptions with a user covmat #2504
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
Open
achiefa
wants to merge
4
commits into
master
Choose a base branch
from
fix-total-theory-covmat-check
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+93
−42
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,87 @@ | ||
| """ | ||
| test_theorycovariance.py | ||
|
|
||
| Tests for the theory covariance matrices built in | ||
| :py:mod:`validphys.theorycovariance.construction`, and in particular for the | ||
| ``nnfit_theory_covmat`` production rule, which is what ``vp-setupfit`` asks for | ||
| whenever a fit runcard contains a ``theorycovmatconfig``. | ||
| """ | ||
|
|
||
| import numpy as np | ||
| import pytest | ||
|
|
||
| from reportengine.table import savetable | ||
| from validphys.api import API | ||
|
|
||
| # Basename the fixture looks up when reading a user covmat from disk. | ||
| USER_COVMAT_FILENAME = "test_user_covmat.csv" | ||
| POINT_PRESCRIPTION = "3 point" | ||
|
|
||
|
|
||
| @pytest.fixture(scope="module") | ||
| def user_covmat_on_disk(tmp_path_factory, thcovmat_config): | ||
| """Write out a "user" theory covmat and return ``(directory, matrix)``. | ||
|
|
||
| The matrix written out is the ``3 point`` scale variation covmat for the very | ||
| same data, so that once it has been read back by ``fromfile_covmat`` the | ||
| resulting ``user_covmat`` is numerically identical to ``theory_covmat_custom``. | ||
| This exercises the real cut and index handling of ``fromfile_covmat`` and | ||
| gives the tests below an expected value with no hardcoded numbers in it. | ||
|
|
||
| ``theory_covmat_custom`` is float32; it is promoted to float64 before being | ||
| written, because the shortest repr of a float32 only round trips to float32 | ||
| and would introduce a ~1e-8 relative error. | ||
| """ | ||
| covmat = API.theory_covmat_custom( | ||
| point_prescriptions=[POINT_PRESCRIPTION], **thcovmat_config | ||
| ).astype(np.float64) | ||
|
|
||
| # Path must be relative to cwd because ``Loader.check_vp_output_file`` rejects absolute paths. | ||
| path = tmp_path_factory.mktemp("user_covmat") | ||
| savetable(covmat, path / USER_COVMAT_FILENAME) | ||
| return path, covmat | ||
|
|
||
|
|
||
| @pytest.mark.parametrize( | ||
| ("covmat_config", "expected_factor"), | ||
| [ | ||
| # Scale variations *and* a user covmat. | ||
| ( | ||
| {"point_prescriptions": [POINT_PRESCRIPTION], "user_covmat_path": USER_COVMAT_FILENAME}, | ||
| 2.0, | ||
| ), | ||
| # Scale variations only. | ||
| ({"point_prescriptions": [POINT_PRESCRIPTION]}, 1.0), | ||
| # User covmat only. | ||
| ({"user_covmat_path": USER_COVMAT_FILENAME}, 1.0), | ||
| # User covmat only, rescaled by ``mult_factor``. | ||
| ({"user_covmat_path": USER_COVMAT_FILENAME, "mult_factor": 2.5}, 2.5), | ||
| ], | ||
| ids=["scalevar_and_user", "scalevar_only", "user_only", "user_only_rescaled"], | ||
| ) | ||
| def test_nnfit_theory_covmat( | ||
| monkeypatch, thcovmat_config, user_covmat_on_disk, covmat_config, expected_factor | ||
| ): | ||
| """Every branch of ``produce_nnfit_theory_covmat`` must resolve and return a | ||
| covmat indexed in runcard order on both axes to match with the experimental | ||
| covariance matrix. | ||
| """ | ||
| covmat_dir, scalevar_covmat = user_covmat_on_disk | ||
| monkeypatch.chdir(covmat_dir) | ||
| covmat = API.nnfit_theory_covmat(**covmat_config, **thcovmat_config) | ||
|
|
||
| runcard_index = API.data_index(**thcovmat_config).droplevel(0) | ||
| process_index = API.procs_index(**thcovmat_config).droplevel(0) | ||
|
|
||
| # This test only has an effect if process grouping actually reorders the runcard. | ||
| assert not runcard_index.equals(process_index), ( | ||
| "Grouping DATA_THCOVMAT by process no longer reorders it, so the " | ||
| "assertions below would hold even if the covmat were written out in " | ||
| "process order instead of runcard order." | ||
| ) | ||
|
|
||
| assert covmat.index.droplevel(0).equals(runcard_index) | ||
| assert covmat.columns.equals(covmat.index) | ||
|
|
||
| expected = scalevar_covmat.reindex(index=covmat.index, columns=covmat.columns) | ||
| np.testing.assert_allclose(covmat.to_numpy(), expected_factor * expected.to_numpy(), rtol=1e-8) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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 see this check is from the time the fitting code was c++ so I'm ready to believe it is obsolete.
However, I also know that two different PhD students three year apart spent many hours on a misordered sum of covmats.
Now it would be about 3 years later from last time... are you absolutely sure this is safe to remove?
Btw, a way to check whether the hypothesis is correct from what you say would be to simply add groups_index to data_input_matched_procs_index as a dummy argument. That should be enough to trigger the production.
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, I tried to add
groups_indexas a dummy argument and it worked. However, I'm not in favour of this solution for two reasons. First, it's very hard to understand the reason why the dummy variable has been included. However, a comment may help in this case. Second, as you said we'd have an important part of the code (checking ordering of covmats) that rely on obselete code. Moreover, if we were to keep this obselete code, we'd have two functions checking the same thing in two not-so-different branches of the code.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.
No, I don't think it should be added as an argument of course.
But it is a way to check whether it was a possible issue or the actual issue.
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.
Good. I reach these conclusions:
check_fit_dataset_order_matches_groupedchecks ifgroup_dataset_inputs_by_metadatafollows or not runcard order.group_dataset_inputs_by_metadatais never used to construct an index in the theory covmat pipeline.nnpdf/validphys2/src/validphys/config.py
Lines 1928 to 1929 in abee4f0
but it's a different thing.
theory_covmat_custom_fitting/total_theory_covmat_fitting/user_covmat_fittingpush the covmat through_reindex_covmat_to_fitting_orderwithdata_input_matched_procs_index. This latter matches with the order of the runcard, which is the same as the exp. covmat. On the reading side,produce_loaded_theory_covmatalways reindexes.nnpdf/validphys2/src/validphys/theorycovariance/construction.py
Lines 544 to 549 in abee4f0
where both matrices are added. However,
fromfile_covmatuses the same index as intheory_covmat_custom_per_prescription, namelyprocs_index. If their index mismatches, then the sum would complain rasing an error.Given these points, I think it's safe to remove this function.