-
Notifications
You must be signed in to change notification settings - Fork 3
Separate forward map #428
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
Merged
Merged
Separate forward map #428
Changes from 10 commits
Commits
Show all changes
33 commits
Select commit
Hold shift + click to select a range
5c6be3d
Factored out forward map
LucaMantani 83bea26
Added tests
LucaMantani 458636d
removed pred_and_pdf from everywhere
LucaMantani 7a8c95a
New forward map class
LucaMantani 2b898bc
Refined implementation
LucaMantani 0d8a241
Changed conftest
LucaMantani 744b275
Fixed tests
LucaMantani 29dfd7e
Make sure we write pdfs with the first parameters
LucaMantani 0fd84f3
Restored doc
LucaMantani 5dcb3c3
Fixed bug in tests
LucaMantani 1e57a68
use check_pdf_model_is_linear as function rather than decorator
comane 1afebf6
Apply suggestion from @comane
comane 8f7cffd
added tests for forward map
comane 97ceb1b
merge commit
comane a4b01b4
fixed tests from merge
comane cbf5ff6
upgraded local black and formatted forward map tests
comane 74d0f4e
added line for raise not implemented in forward map
comane 7da4f04
forward model initialised with pdf parameter names
comane 054c97f
pass forward model to bayesian prior for total model params
comane 22207f3
pass pdf_model object to forward map
comane 18dc793
Update colibri/forward_map.py
LucaMantani 089110c
Update colibri/forward_map.py
LucaMantani cf02826
Update colibri/forward_map.py
LucaMantani 0cdce57
Added test
LucaMantani 20941aa
removed grid_func passing to forward map
LucaMantani 8581a03
black
LucaMantani 11641ba
Adapted tests
LucaMantani f134f71
Merge branch 'main' into separate-forward-map
LucaMantani 79c1e73
Merge branch 'main' into separate-forward-map
LucaMantani 28daeab
Adapted after merging main
LucaMantani a5cddec
Fixed bug
LucaMantani bface54
typo
vschutze-alt 177898a
typo
vschutze-alt 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
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
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,158 @@ | ||
| """ | ||
| colibri.forward_map.py | ||
|
|
||
| Forward maps: parameters → theory predictions. | ||
|
|
||
| A ``ForwardMap`` implements the final stage of the fit pipeline, turning the | ||
| fit parameter vector into theory predictions that can be compared with | ||
| data in the likelihood. It will also also return the PDF values on the fit x-grid, | ||
| which is sometimes needed for computing penalties. | ||
|
|
||
|
|
||
| Design choice: fixed call signature | ||
| ----------------------------------- | ||
| The log-likelihood calls every forward map with the same fixed signature:: | ||
|
|
||
| (pdf_grid_func, fk_tables, params) -> predictions, pdf | ||
|
|
||
| Parameter convention | ||
| -------------------- | ||
| ``params`` is a 1-D array containing *all* fit parameters. In colibri we allow | ||
| for "extra" fit parameters beyond the PDF model parameters (e.g. nuisance-like factors, | ||
| or parameters of a custom prediction function). | ||
|
|
||
| By convention: | ||
|
|
||
| ``params[:self.n_pdf_params]`` are PDF parameters consumed by ``pdf_grid_func``; | ||
| any remaining entries are "extra" parameters interpreted by the chosen | ||
| ``ForwardMap`` implementation. | ||
|
|
||
| Example - fitting a normalisation factor on top of the PDF | ||
| ---------------------------------------------------------- | ||
| :: | ||
|
|
||
| class NormForwardMap(ForwardMap): | ||
| def __init__(self, pred_func, n_pdf_params: int): | ||
| super().__init__(n_pdf_params) | ||
| self._pred_func = pred_func | ||
|
|
||
| def __call__(self, pdf_grid_func, fk_tables, params): | ||
| pdf = pdf_grid_func(params[: self.n_pdf_params]) | ||
| norm = params[self.n_pdf_params] # first extra parameter | ||
| return norm * self._pred_func(pdf, fk_tables), pdf | ||
|
|
||
| Example - fixed PDF, fitting only extra parameters | ||
| --------------------------------------------------- | ||
| :: | ||
|
|
||
| class FixedPDFForwardMap(ForwardMap): | ||
| def __init__(self, pred_func, fixed_pdf, fk_tables, n_pdf_params: int = 0): | ||
| super().__init__(n_pdf_params) | ||
| self._pred_func = pred_func | ||
| self.fixed_pdf = fixed_pdf | ||
| self._fixed_pred = self._pred_func(fixed_pdf, fk_tables) | ||
|
|
||
| def __call__(self, pdf_grid_func, fk_tables, params): | ||
| scale = params[0] | ||
| return scale * self._fixed_pred, self.fixed_pdf | ||
| """ | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| from abc import ABC, abstractmethod | ||
| from typing import Any, Callable | ||
|
|
||
| import jax.numpy as jnp | ||
|
|
||
|
|
||
| class ForwardMap(ABC): | ||
| """Abstract base class for forward maps. | ||
|
|
||
| A forward map turns fit parameters into theory predictions that can be | ||
| compared with experimental data inside the likelihood. | ||
|
|
||
| All forward maps share the same call signature: | ||
|
|
||
| ``(pdf_grid_func, fk_tables, params) -> predictions`` | ||
|
|
||
| Notes | ||
| ----- | ||
| The split point between PDF parameters and "extra" parameters is owned | ||
| by the forward map via ``self.n_pdf_params``. | ||
| """ | ||
|
|
||
| def __init__(self, n_pdf_params: int): | ||
|
|
||
| self.n_pdf_params = n_pdf_params | ||
|
|
||
| @abstractmethod | ||
| def __call__( | ||
| self, | ||
| pdf_grid_func: Callable[[jnp.ndarray], jnp.ndarray], | ||
| fk_tables: Any, | ||
| params: jnp.ndarray, | ||
| ) -> jnp.ndarray: | ||
| """Compute theory predictions from fit parameters. | ||
|
|
||
| Parameters | ||
| ---------- | ||
| pdf_grid_func : callable | ||
| Callable that evaluates PDF values on the fit x-grid from the PDF | ||
| parameters. | ||
|
|
||
| Expected call signature: | ||
| ``pdf = pdf_grid_func(pdf_params)`` | ||
| with ``pdf`` shaped ``(N_fl, N_x)``. | ||
|
|
||
| fk_tables : jnp.ndarray | ||
| Fast-kernel tables needed by the prediction function. | ||
|
|
||
| params : jnp.ndarray | ||
| 1-D array containing all fit parameters. By convention: | ||
| * ``params[:self.n_pdf_params]`` are PDF parameters | ||
| * the remaining entries are extra parameters interpreted by the | ||
| specific ``ForwardMap`` implementation. | ||
|
|
||
| Returns | ||
| ------- | ||
| jnp.ndarray | ||
| Theory predictions (1-D array with one entry per data point). | ||
| jnp.ndarray | ||
| The PDF values (2-D array with shape (N_fl, N_x)). | ||
|
|
||
| """ | ||
| raise NotImplementedError | ||
|
|
||
|
|
||
| class FKTableForwardMap(ForwardMap): | ||
| """Default forward map: params → PDF → FK-table convolution. | ||
|
|
||
| This is the standard pipeline used in colibri PDF fits. | ||
| """ | ||
|
|
||
| def __init__( | ||
| self, pred_func: Callable[[jnp.ndarray, Any], jnp.ndarray], n_pdf_params: int | ||
| ): | ||
| super().__init__(n_pdf_params) | ||
| self._pred_func = pred_func | ||
|
|
||
| def __call__(self, pdf_grid_func, fk_tables, params): | ||
| pdf_params = params[: self.n_pdf_params] | ||
| pdf = pdf_grid_func(pdf_params) | ||
| return self._pred_func(pdf, fk_tables), pdf | ||
|
|
||
|
|
||
| def forward_map(_pred_data, pdf_model): | ||
| """Reportengine provider that builds the default FK-table forward map. | ||
|
|
||
| Parameters | ||
| ---------- | ||
| _pred_data : callable | ||
| Prediction function of the form ``pred_func(pdf, fk_tables) -> predictions``. | ||
| pdf_model : optional | ||
| Used to infer ``n_pdf_params`` from ``len(pdf_model.param_names)``. | ||
|
|
||
| """ | ||
|
|
||
| n_pdf_params = len(pdf_model.param_names) | ||
| return FKTableForwardMap(_pred_data, n_pdf_params=n_pdf_params) | ||
|
comane marked this conversation as resolved.
Outdated
|
||
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
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.
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.
Uh oh!
There was an error while loading. Please reload this page.