From 08466518b0341f0e702737e129bd15adbdc58d21 Mon Sep 17 00:00:00 2001 From: Tanjona Rabemananjara Date: Wed, 13 Nov 2024 22:13:08 +0100 Subject: [PATCH 1/7] Start modifications --- pyproject.toml | 2 +- validphys2/src/validphys/photon/structure_functions.py | 2 +- .../src/validphys/tests/photon/test_structurefunctions.py | 4 ++-- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/pyproject.toml b/pyproject.toml index 3b659e35a5..8b1c7dd068 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -62,7 +62,7 @@ vp-deltachi2 = "validphys.scripts.vp_deltachi2:main" # Generic dependencies (i.e., validphys) python = "^3.9" matplotlib = ">=3.3.0,<3.8" -pineappl = "^0.8.2" +pineappl = { git = "https://github.com/NNPDF/pineappl.git", branch = "v1-file-format", subdirectory = "pineappl_py" } pandas = "*" numpy = "*" validobj = "*" diff --git a/validphys2/src/validphys/photon/structure_functions.py b/validphys2/src/validphys/photon/structure_functions.py index 73b45f4746..25c24a05f3 100644 --- a/validphys2/src/validphys/photon/structure_functions.py +++ b/validphys2/src/validphys/photon/structure_functions.py @@ -43,7 +43,7 @@ def __init__(self, path_to_fktable, pdfs): self.q2_max = max(q2) - predictions = self.fktable.convolve_with_one(2212, pdfs.xfxQ2) + predictions = self.fktable.convolve(pdg_convs=self.fktable.convolutions, xfxs=[pdfs.xfxQ2]) grid2D = predictions.reshape(len(x), len(q2)) self.interpolator = RectBivariateSpline(x, q2, grid2D) diff --git a/validphys2/src/validphys/tests/photon/test_structurefunctions.py b/validphys2/src/validphys/tests/photon/test_structurefunctions.py index f3bba28d0d..92c6b1af09 100644 --- a/validphys2/src/validphys/tests/photon/test_structurefunctions.py +++ b/validphys2/src/validphys/tests/photon/test_structurefunctions.py @@ -35,7 +35,7 @@ def bin_left(self, i): else: return 0 - def convolve_with_one(self, pdgid, xfxQ2): + def convolve(self, pdg_convs, xfxs): return np.zeros((10, 10)) @@ -99,7 +99,7 @@ def test_interpolation_grid(): fktable = pineappl.fk_table.FkTable.read(path_to_fktable) x = np.unique(fktable.bin_left(1)) q2 = np.unique(fktable.bin_left(0)) - predictions = fktable.convolve_with_one(2212, pdfs.members[replica].xfxQ2) + predictions = fktable.convolve(fktable.convolutions, [pdfs.members[replica].xfxQ2]) grid2D = predictions.reshape(len(x), len(q2)) struct_func = sf.InterpStructureFunction(path_to_fktable, pdfs.members[replica]) From 2fdf99ee875f75e94a57ec2c524d607d9861b812 Mon Sep 17 00:00:00 2001 From: Tanjona Rabemananjara Date: Sun, 17 Nov 2024 15:37:31 +0100 Subject: [PATCH 2/7] Continue with minor changes --- validphys2/src/validphys/coredata.py | 20 ++++++------ validphys2/src/validphys/pineparser.py | 43 +++++++++++--------------- 2 files changed, 29 insertions(+), 34 deletions(-) diff --git a/validphys2/src/validphys/coredata.py b/validphys2/src/validphys/coredata.py index 46a1cab7c6..c9ea0114ae 100644 --- a/validphys2/src/validphys/coredata.py +++ b/validphys2/src/validphys/coredata.py @@ -48,7 +48,7 @@ class FKTableData: ``xgrid`` indicating the points in ``x`` where the PDF should be evaluated. - convolution_types: tuple[str] + convolution_types: list(pineappl.convolutions.Conv) The type of convolution that the FkTable is expecting for each of the functions to be convolved with (usually the two types of PDF from the two incoming hadrons). @@ -68,7 +68,7 @@ class FKTableData: ndata: int xgrid: np.ndarray sigma: pd.DataFrame - convolution_types: tuple[str] = None + convolution_types: list | None = None metadata: dict = dataclasses.field(default_factory=dict, repr=False) protected: bool = False @@ -206,26 +206,28 @@ def determine_pdfs(self, pdf): conv_pdfs = [] for convolution_type in self.convolution_types: - + # Check the polarization of the current convolution + polarized = convolution_type.conv_type.polarized # Check the type of convolutions that the fktable is asking for and match it to the PDF - if convolution_type == "UnpolPDF": + if not polarized: if pdf.is_polarized: if pdf.unpolarized_bc is None: raise ValueError( "The FKTable asked for an unpolarized PDF but received only polarized PDFs" ) - conv_pdfs.append(pdf.unpolarized_bc.make_only_cv()) else: conv_pdfs.append(pdf) - - elif convolution_type == "PolPDF": + elif polarized: if not pdf.is_polarized: raise ValueError( - """The FKTable asked for a polarized PDF, but the PDF received cannot be understood as polarized. - When using a polarized PDF make sure to include a boundary condition `unpolarized_bc: ` whenever needed (`t0`, `dataspecs`...).""" + """The FKTable asked for a polarized PDF, but the PDF received cannot be understood + as polarized. When using a polarized PDF make sure to include a boundary condition + `unpolarized_bc: ` whenever needed (`t0`, `dataspecs`...).""" ) conv_pdfs.append(pdf) + else: # Other scenarios (such as `time_like`) should be implemented as another `elif` statement + raise ValueError("The convolution type is not recognized!") return conv_pdfs diff --git a/validphys2/src/validphys/pineparser.py b/validphys2/src/validphys/pineparser.py index d7dd4488a8..5dfd8d2aec 100644 --- a/validphys2/src/validphys/pineparser.py +++ b/validphys2/src/validphys/pineparser.py @@ -75,17 +75,15 @@ def _pinelumi_to_columns(pine_luminosity, hadronic): ) flav_size = len(evol_basis_pids) columns = [] + # TODO: Extend this to deal with a generic number of convolutions if hadronic: for i, j in pine_luminosity: idx = evol_basis_pids.index(i) jdx = evol_basis_pids.index(j) columns.append(flav_size * idx + jdx) else: - # The proton might come from both sides - try: - columns = [evol_basis_pids.index(i) for _, i in pine_luminosity] - except ValueError: - columns = [evol_basis_pids.index(i) for i, _ in pine_luminosity] + # Now for DIS, there is only ONE single PID + columns = [evol_basis_pids.index(i[0]) for i in pine_luminosity] return columns @@ -155,26 +153,18 @@ def pineappl_reader(fkspec): # Extract metadata from the first grid pine_rep = pines[0] - # Is it hadronic? (at the moment only hadronic and DIS are considered) - try: - parton1 = pine_rep.key_values()["convolution_particle_1"] - parton2 = pine_rep.key_values()["convolution_particle_2"] - except KeyError: - # Old pineappl FKTables used `initial_state` instead of `convolution_particle` - parton1 = pine_rep.key_values()["initial_state_1"] - parton2 = pine_rep.key_values()["initial_state_2"] - hadronic = parton1 == parton2 - - # NOTE: while the following can accept any number of convolutions, at the moment only - # 1 (DIS) or 2 (hadronic) are implemented. - # In the case of DIS grids, convolution 1 refers to the hadron - conv_types = [pine_rep.key_values().get("convolution_type_1", "UnpolPDF")] - if hadronic: - # Sanity check (in case at some point we start fitting things that are not protons) - if parton1 != "2212": - raise ValueError("vp can only read hadronic fktables with 2 protons!") + # Get the convolution types for this FK table + convolutions = pine_rep.convolutions + + # Is it hadronic? For the time being, hadronic is defined with `len(convolutions) == 2`. + # Hadronic could also involve 3 convolutions in processes such as `pp->H` (with FFs). + # DIS FK table now only contains ONE single convolutions. + hadronic = len(convolutions) == 2 - conv_types.append(pine_rep.key_values().get("convolution_type_2", "UnpolPDF")) + # TODO: While now any arbittrary number of convolutions is allowed, for the time being, + # raise Errors when the number of convolutions is more than 2. + if len(convolutions) > 2: + raise ValueError("vp can only deal with fktables with 2 convolutions!") Q0 = np.sqrt(pine_rep.muf2()) xgrid = np.array([]) @@ -212,6 +202,9 @@ def pineappl_reader(fkspec): # Read the table, remove bin normalization and apply cfactors raw_fktable = (cfprod * p.table().T / p.bin_normalizations()).T + # If it is a DIS FK table then we need to expand the dimension + if not hadronic: + raw_fktable = np.expand_dims(raw_fktable, axis=-1) n = raw_fktable.shape[0] # Apply possible per-fktable fixes @@ -272,7 +265,7 @@ def pineappl_reader(fkspec): sigma=sigma, ndata=ndata, Q0=Q0, - convolution_types=tuple(conv_types), + convolution_types=convolutions, metadata=fkspec.metadata, hadronic=hadronic, xgrid=xgrid, From f00d1b09a9cd9d413b795541a94446b6cb2be672 Mon Sep 17 00:00:00 2001 From: Radonirinaunimi Date: Sun, 17 Nov 2024 19:38:21 +0100 Subject: [PATCH 3/7] Fix Photon tests --- .../tests/photon/test_structurefunctions.py | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/validphys2/src/validphys/tests/photon/test_structurefunctions.py b/validphys2/src/validphys/tests/photon/test_structurefunctions.py index 92c6b1af09..0cc9fadfbd 100644 --- a/validphys2/src/validphys/tests/photon/test_structurefunctions.py +++ b/validphys2/src/validphys/tests/photon/test_structurefunctions.py @@ -1,5 +1,6 @@ import numpy as np -import pineappl +from pineappl.convolutions import Conv, ConvType +from pineappl.fk_table import FkTable from validphys.api import API from validphys.core import PDF as PDFset @@ -35,6 +36,11 @@ def bin_left(self, i): else: return 0 + @property + def convolutions(self): + convtype = ConvType(polarized=False, time_like=False) + return [Conv(conv_type=convtype, pid=2212)] + def convolve(self, pdg_convs, xfxs): return np.zeros((10, 10)) @@ -63,7 +69,7 @@ def test_zero_pdfs(): def test_zero_grid(monkeypatch): "test that a zero grid gives a zero structure function" # patching pineappl.fk_table.FkTable to use ZeroFKTable - monkeypatch.setattr(pineappl.fk_table.FkTable, "read", ZeroFKTable) + monkeypatch.setattr(FkTable, "read", ZeroFKTable) pdfs = PDFset(PDF).load() structurefunc = sf.InterpStructureFunction("", pdfs.central_member) for x in np.geomspace(1e-4, 1.0, 10): @@ -96,7 +102,7 @@ def test_interpolation_grid(): for kind in ["F2", "FL"]: tmp = "fastkernel/FIATLUX_DIS_" + kind + ".pineappl.lz4" path_to_fktable = test_theory.path / tmp - fktable = pineappl.fk_table.FkTable.read(path_to_fktable) + fktable = FkTable.read(path_to_fktable) x = np.unique(fktable.bin_left(1)) q2 = np.unique(fktable.bin_left(0)) predictions = fktable.convolve(fktable.convolutions, [pdfs.members[replica].xfxQ2]) From 430887bc1303f73f1b4ed26ebca5232ddb52a087 Mon Sep 17 00:00:00 2001 From: Radonirinaunimi Date: Mon, 18 Nov 2024 17:40:11 +0100 Subject: [PATCH 4/7] Ignore `conda` tests for the time being --- .github/workflows/tests.yml | 5 ++++- conda-recipe/meta.yaml | 1 - 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index 11b2d41821..5a2bfdbc35 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -1,7 +1,10 @@ # A single CI script with github workflow to test NNPDF, and upload the conda package and documentation name: Test conda package -on: [push] +on: + push: + branches-ignore: + - update-pineappl-v1 concurrency: group: ${{ github.head_ref || github.ref }} diff --git a/conda-recipe/meta.yaml b/conda-recipe/meta.yaml index cb93a7be0b..4e121cc95a 100644 --- a/conda-recipe/meta.yaml +++ b/conda-recipe/meta.yaml @@ -36,7 +36,6 @@ requirements: - requests - prompt_toolkit - validobj - - pineappl >=0.8.2 - eko >=0.14.2 - fiatlux - sphinx >=5.0.2 From be0a888d315f383e1561f4f3c705cbc85dcf6cbd Mon Sep 17 00:00:00 2001 From: Radonirinaunimi Date: Mon, 18 Nov 2024 21:59:37 +0100 Subject: [PATCH 5/7] Check `pid` of convolutions and use `tuple` for `convolution_types` --- validphys2/src/validphys/coredata.py | 4 ++-- validphys2/src/validphys/pineparser.py | 7 +++++-- 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/validphys2/src/validphys/coredata.py b/validphys2/src/validphys/coredata.py index c9ea0114ae..5eb97f8a9a 100644 --- a/validphys2/src/validphys/coredata.py +++ b/validphys2/src/validphys/coredata.py @@ -48,7 +48,7 @@ class FKTableData: ``xgrid`` indicating the points in ``x`` where the PDF should be evaluated. - convolution_types: list(pineappl.convolutions.Conv) + convolution_types: tuple(pineappl.convolutions.Conv) The type of convolution that the FkTable is expecting for each of the functions to be convolved with (usually the two types of PDF from the two incoming hadrons). @@ -68,7 +68,7 @@ class FKTableData: ndata: int xgrid: np.ndarray sigma: pd.DataFrame - convolution_types: list | None = None + convolution_types: tuple | None = None metadata: dict = dataclasses.field(default_factory=dict, repr=False) protected: bool = False diff --git a/validphys2/src/validphys/pineparser.py b/validphys2/src/validphys/pineparser.py index 5dfd8d2aec..cc9afb4e3a 100644 --- a/validphys2/src/validphys/pineparser.py +++ b/validphys2/src/validphys/pineparser.py @@ -160,11 +160,14 @@ def pineappl_reader(fkspec): # Hadronic could also involve 3 convolutions in processes such as `pp->H` (with FFs). # DIS FK table now only contains ONE single convolutions. hadronic = len(convolutions) == 2 + # For the time being, allow only hadronic with identical initial-state protons + if hadronic and (convolutions[0].pid != 2212 or convolutions[1].pid != 2212): + raise ValueError("Only two identical protons in the initial-state are allowed.") # TODO: While now any arbittrary number of convolutions is allowed, for the time being, # raise Errors when the number of convolutions is more than 2. if len(convolutions) > 2: - raise ValueError("vp can only deal with fktables with 2 convolutions!") + raise ValueError("Only FK tables with maximum 2 convolutions are allowed.") Q0 = np.sqrt(pine_rep.muf2()) xgrid = np.array([]) @@ -265,7 +268,7 @@ def pineappl_reader(fkspec): sigma=sigma, ndata=ndata, Q0=Q0, - convolution_types=convolutions, + convolution_types=tuple(convolutions), metadata=fkspec.metadata, hadronic=hadronic, xgrid=xgrid, From b18f249b17e8628cd87dec4fb272a33c87df6571 Mon Sep 17 00:00:00 2001 From: Radonirinaunimi Date: Thu, 21 Nov 2024 09:18:52 +0100 Subject: [PATCH 6/7] Fix type annotation that leads `pineko` tests to fail --- validphys2/src/validphys/coredata.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/validphys2/src/validphys/coredata.py b/validphys2/src/validphys/coredata.py index 5eb97f8a9a..bf24474327 100644 --- a/validphys2/src/validphys/coredata.py +++ b/validphys2/src/validphys/coredata.py @@ -5,7 +5,7 @@ import dataclasses import logging -from typing import Optional +from typing import Optional, Union import numpy as np import pandas as pd @@ -68,7 +68,7 @@ class FKTableData: ndata: int xgrid: np.ndarray sigma: pd.DataFrame - convolution_types: tuple | None = None + convolution_types: Union[tuple, None] = None metadata: dict = dataclasses.field(default_factory=dict, repr=False) protected: bool = False From e72105816744eba6208f6b83501149858041438f Mon Sep 17 00:00:00 2001 From: Radonirinaunimi Date: Wed, 27 Nov 2024 02:46:41 +0100 Subject: [PATCH 7/7] Revert `convolution_types` to `tuple[str]` --- validphys2/src/validphys/coredata.py | 12 +++++------- validphys2/src/validphys/pineparser.py | 26 +++++++++++++++++++++++++- 2 files changed, 30 insertions(+), 8 deletions(-) diff --git a/validphys2/src/validphys/coredata.py b/validphys2/src/validphys/coredata.py index bf24474327..68d8490707 100644 --- a/validphys2/src/validphys/coredata.py +++ b/validphys2/src/validphys/coredata.py @@ -5,7 +5,7 @@ import dataclasses import logging -from typing import Optional, Union +from typing import Optional import numpy as np import pandas as pd @@ -48,7 +48,7 @@ class FKTableData: ``xgrid`` indicating the points in ``x`` where the PDF should be evaluated. - convolution_types: tuple(pineappl.convolutions.Conv) + convolution_types: tuple[str] The type of convolution that the FkTable is expecting for each of the functions to be convolved with (usually the two types of PDF from the two incoming hadrons). @@ -68,7 +68,7 @@ class FKTableData: ndata: int xgrid: np.ndarray sigma: pd.DataFrame - convolution_types: Union[tuple, None] = None + convolution_types: Optional[tuple[str]] = None metadata: dict = dataclasses.field(default_factory=dict, repr=False) protected: bool = False @@ -206,10 +206,8 @@ def determine_pdfs(self, pdf): conv_pdfs = [] for convolution_type in self.convolution_types: - # Check the polarization of the current convolution - polarized = convolution_type.conv_type.polarized # Check the type of convolutions that the fktable is asking for and match it to the PDF - if not polarized: + if convolution_type == "UnpolPDF": if pdf.is_polarized: if pdf.unpolarized_bc is None: raise ValueError( @@ -218,7 +216,7 @@ def determine_pdfs(self, pdf): conv_pdfs.append(pdf.unpolarized_bc.make_only_cv()) else: conv_pdfs.append(pdf) - elif polarized: + elif convolution_type == "PolPDF": if not pdf.is_polarized: raise ValueError( """The FKTable asked for a polarized PDF, but the PDF received cannot be understood diff --git a/validphys2/src/validphys/pineparser.py b/validphys2/src/validphys/pineparser.py index cc9afb4e3a..e8d3c9e245 100644 --- a/validphys2/src/validphys/pineparser.py +++ b/validphys2/src/validphys/pineparser.py @@ -87,6 +87,29 @@ def _pinelumi_to_columns(pine_luminosity, hadronic): return columns +def _get_convolution_types(convolutions): + """Get the type of convolutions from for the given FK table. + + Parameters + ---------- + convolutions: list[pineappl.convolutions.Conv] + a list of PineAPPL object containing the types of convolutions + + Returns + ------- + tuple(str): a tuple of string containing whose elements are either + `UnpolPDF` or `PolPDF` + """ + # TODO: Extend the following to deal with `time_like` FFs + convolution_types = [] + for convolution in convolutions: + if convolution.conv_type.polarized: + convolution_types.append("PolPDF") + else: + convolution_types.append("UnpolPDF") + return tuple(convolution_types) + + def get_yaml_information(yaml_file, theorypath): """Reads the yaml information from a yaml compound file @@ -155,6 +178,7 @@ def pineappl_reader(fkspec): # Get the convolution types for this FK table convolutions = pine_rep.convolutions + convolution_types = _get_convolution_types(convolutions) # Is it hadronic? For the time being, hadronic is defined with `len(convolutions) == 2`. # Hadronic could also involve 3 convolutions in processes such as `pp->H` (with FFs). @@ -268,7 +292,7 @@ def pineappl_reader(fkspec): sigma=sigma, ndata=ndata, Q0=Q0, - convolution_types=tuple(convolutions), + convolution_types=convolution_types, metadata=fkspec.metadata, hadronic=hadronic, xgrid=xgrid,