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

Simplify TemplateSource, CombinedSource and SpectrumTemplateSource #69

Merged
merged 25 commits into from
Aug 16, 2023

Conversation

dachengx
Copy link
Collaborator

@dachengx dachengx commented Jul 31, 2023

What does the code in this PR do / what does it improve?

The functionality of TemplateSource, CombinedSource, and SpectrumTemplateSource are unchanged.

Add test of CombinedSource.

The weights can be set as shape parameters in the config. You can check it by:

config = load_yaml('unbinned_wimp_statistical_model.yaml')
blueice_model = BlueiceExtendedModel(
    parameter_definition=config['parameter_definition'],
    likelihood_config=config['likelihood_config'],
)

llh_term = blueice_model.get_likelihood_term_from_name('sr2')
zs = np.asarray([-2, 0])

blueice_model._likelihood.likelihood_list[llh_term].mus_interpolator(zs)
blueice_model._likelihood.likelihood_list[llh_term].ps_interpolator(zs).std()

But the SpectrumTemplateSource is not tested, also the configuration is too complex, especially the normalization.

Can you briefly describe how it works?

I already tested that the results are the same in main and simplify_source.

Can you give a minimal working example (or illustrate with a figure)?

Please refer to test_blueice_extended_model.py.

What are the potential drawbacks of the codes?

Please include the following if applicable:

  • Update the docstring(s)
  • Update the documentation
  • Tests to check the (new) code is working as desired.
  • Does it solve one of the open issues on github?

alea/template_source.py Show resolved Hide resolved
alea/template_source.py Outdated Show resolved Hide resolved
alea/template_source.py Outdated Show resolved Hide resolved
alea/template_source.py Show resolved Hide resolved
alea/template_source.py Show resolved Hide resolved
alea/template_source.py Outdated Show resolved Hide resolved
alea/template_source.py Outdated Show resolved Hide resolved
alea/template_source.py Outdated Show resolved Hide resolved
alea/template_source.py Outdated Show resolved Hide resolved
alea/template_source.py Outdated Show resolved Hide resolved
@coveralls
Copy link

coveralls commented Jul 31, 2023

Pull Request Test Coverage Report for Build 5718802491

Warning: This coverage report may be inaccurate.

This pull request's base commit is no longer the HEAD commit of its target branch. This means it includes changes from outside the original pull request, including, potentially, unrelated coverage changes.

Details

  • 89 of 132 (67.42%) changed or added relevant lines in 3 files are covered.
  • 47 unchanged lines in 4 files lost coverage.
  • Overall coverage increased (+13.9%) to 75.205%

Changes Missing Coverage Covered Lines Changed/Added Lines %
alea/parameters.py 0 1 0.0%
alea/utils.py 10 14 71.43%
alea/template_source.py 79 117 67.52%
Files with Coverage Reduction New Missed Lines %
alea/models/blueice_extended_model.py 3 92.05%
alea/utils.py 10 80.26%
alea/template_source.py 15 69.43%
alea/parameters.py 19 81.25%
Totals Coverage Status
Change from base Build 5696617155: 13.9%
Covered Lines: 643
Relevant Lines: 855

💛 - Coveralls

alea/template_source.py Show resolved Hide resolved
alea/utils.py Show resolved Hide resolved
alea/utils.py Outdated Show resolved Hide resolved
alea/utils.py Outdated Show resolved Hide resolved
alea/utils.py Show resolved Hide resolved
alea/utils.py Show resolved Hide resolved
alea/utils.py Outdated Show resolved Hide resolved
@dachengx dachengx marked this pull request as ready for review July 31, 2023 06:04
@dachengx dachengx requested a review from kdund July 31, 2023 06:04
@dachengx
Copy link
Collaborator Author

Maybe we also want to test SpectrumTemplateSource.

alea/template_source.py Show resolved Hide resolved
alea/template_source.py Show resolved Hide resolved
alea/template_source.py Show resolved Hide resolved
alea/template_source.py Show resolved Hide resolved
alea/template_source.py Show resolved Hide resolved
alea/template_source.py Show resolved Hide resolved
alea/template_source.py Show resolved Hide resolved
alea/template_source.py Show resolved Hide resolved
@dachengx
Copy link
Collaborator Author

This PR depends on #68.

@@ -1,5 +1,7 @@
from typing import Any, Dict, List, Optional, Tuple

import scipy
Copy link

Choose a reason for hiding this comment

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

[pep8] reported by reviewdog 🐶
F401 'scipy' imported but unused

Copy link
Collaborator

Choose a reason for hiding this comment

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

Why do you import it though @dachengx ? If there's a reason you can also just put the #noqa: F401 to make clear that it is intentionally added here and nobody accidentally removes it in the future 😊

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Added.

Copy link
Collaborator

@kdund kdund left a comment

Choose a reason for hiding this comment

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

SOme comments-- at the moment, the only way I found testing it was to import binference plotting code, is there a better way, you think?

alea/examples/configs/unbinned_wimp_statistical_model.yaml Outdated Show resolved Hide resolved
alea/examples/configs/unbinned_wimp_statistical_model.yaml Outdated Show resolved Hide resolved
# ... and finally to probability density
if 0 < self.events_per_day:
if self.events_per_day > 0:
Copy link
Collaborator

Choose a reason for hiding this comment

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

I can see how it is correct if read in english, but I have in general made many more mistakes when I did not consistently never use > (so all inequalities have the smaller number to the left)

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

We will apply a < b < c in the future.

ret[n] = t[:, i]
return ret


class CombinedSource(blueice.HistogramPdfSource):
class CombinedSource(TemplateSource, HistogramPdfSource):
"""
Source that inherits structure from TH2DSource by Jelle,
Copy link
Collaborator

Choose a reason for hiding this comment

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

HistogramPdfSource is the one in blueice originally written by Jelle

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

removed.

Copy link
Collaborator

@kdund kdund Aug 3, 2023

Choose a reason for hiding this comment

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

I meant rather in the docstring also :)

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Removed.

@dachengx
Copy link
Collaborator Author

dachengx commented Aug 3, 2023

I think histogram_multiplier is a bad name, actually it is the absolute rate.

@hammannr
Copy link
Collaborator

hammannr commented Aug 3, 2023

I think histogram_multiplier is a bad name, actually it is the absolute rate.

It depends on how you use it and I think we agreed that we try to avoid people using it. But really, the histogram is just multiplied with it, right?

alea/template_source.py Outdated Show resolved Hide resolved
Copy link
Collaborator

@hammannr hammannr left a comment

Choose a reason for hiding this comment

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

I scrolled through the changes and it looks nice, for sure useful to clean up this part. Though a bit hard to keep track and check all the changes. Maybe it would make sense to add a test for the combined source or did I just miss it?
Also, could you maybe think of a case where this source would be used so that we can make a sensible example out of it?

@dachengx
Copy link
Collaborator Author

dachengx commented Aug 4, 2023

I think histogram_multiplier is a bad name, actually it is the absolute rate.

It depends on how you use it and I think we agreed that we try to avoid people using it. But really, the histogram is just multiplied with it, right?

The histogram is normalized to one and then multiplied by it.

@dachengx
Copy link
Collaborator Author

dachengx commented Aug 4, 2023

I scrolled through the changes and it looks nice, for sure useful to clean up this part. Though a bit hard to keep track and check all the changes. Maybe it would make sense to add a test for the combined source or did I just miss it? Also, could you maybe think of a case where this source would be used so that we can make a sensible example out of it?

The test it at

class: alea.template_source.CombinedSource
. But it is not meaningful.

I would postpone the meaningful test to the future when we really used the CombinedSource. Currently, the source is just functionally runnable.

@dachengx dachengx requested review from kdund and hammannr August 4, 2023 19:18
@dachengx
Copy link
Collaborator Author

dachengx commented Aug 6, 2023

histogram_scale_factor is also multiplied on the histogram.

@hammannr
Copy link
Collaborator

hammannr commented Aug 9, 2023

Ah, right @dachengx ! The histogram_multiplier was renamed to histogram_scale_factor in the TemplateSource. For the CombinedSource it indeed is the rate before slicing the way it is implemented. I never worked with the CombinedSource, do we need/want this behaviour? Otherwise we might consider sticking to a histogram_scale_factor for both that do the same thing i.e. just being multiplied to the template. If you want to normalize things to one you should do so yourself. But in any case, the intended usage is to only use the multiplier if you very quickly want to check or vary something. For production you should update the normalization of the template according to the expectation value, at least this is what we discussed with @kdund I think.

@hammannr
Copy link
Collaborator

hammannr commented Aug 9, 2023

Ok I see that for now there is no urgency to have a meaningful test but it would be very useful to have an idea when this source could be used and how one would want to use it, I think. Maybe we could also make this a bit clearer in the docstring since rn I have no idea what to do with it tbh 😄

@dachengx
Copy link
Collaborator Author

dachengx commented Aug 10, 2023

For this to be merged, we need more example templates like #30 provided. Especially the 3D template with (E, cS2, cS2).

Unless we can live with the nonsense pytest. Currently the config .yaml files only. provide a runnable model definition. But the SpectrumTemplateSource there is nonsense.

@dachengx dachengx added the testing Works on testing code label Aug 15, 2023
@dachengx
Copy link
Collaborator Author

@kdund I moved the test of sources into another test module. They will not participate in CL calculation, to save time.

Copy link
Collaborator

@kdund kdund left a comment

Choose a reason for hiding this comment

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

Thanks, @dachengx I think from my side this looks good now

@@ -56,7 +61,7 @@ parameter_definition:
er_band_shift:
nominal_value: 0
ptype: shape
uncertainty: null # 'scipy.stats.uniform(loc=-1, scale=2)'
uncertainty: null # stats.uniform(loc=0, scale=2).logpdf
Copy link
Collaborator

Choose a reason for hiding this comment

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

I think making all our tests usig the same large config is not optimal-- could we make smaller likelihoods for each aspects of the test?

raise ValueError(
f"Uncertainty string '{self._uncertainty}'"
" must start with 'scipy.' or 'numpy.'")
NotImplementedError(
Copy link
Collaborator

Choose a reason for hiding this comment

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

What is the challenge in not including this functionality for now?

README.md Outdated

## Ackgnowledgements
Copy link
Collaborator

Choose a reason for hiding this comment

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

acknowledgements :)

@dachengx dachengx merged commit b61fffd into main Aug 16, 2023
@dachengx dachengx deleted the simplify_source branch August 16, 2023 21:09
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
testing Works on testing code
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants