Skip to content

Commit

Permalink
Exposes Scales in the Grid creation of the Python API
Browse files Browse the repository at this point in the history
  • Loading branch information
Radonirinaunimi committed Oct 24, 2024
1 parent 2a36294 commit b6abfbe
Show file tree
Hide file tree
Showing 7 changed files with 140 additions and 26 deletions.
99 changes: 94 additions & 5 deletions pineappl_py/src/boc.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//! Interface for bins, orders and channels.
use numpy::{IntoPyArray, PyArray1};
use pineappl::boc::{Channel, Kinematics, Order};
use pineappl::boc::{Channel, Kinematics, Order, ScaleFuncForm, Scales};
use pyo3::prelude::*;

/// PyO3 wrapper to :rustdoc:`pineappl::boc::Channel <boc/struct.Channel.html>`.
Expand Down Expand Up @@ -63,10 +63,10 @@ impl PyKinematics {
///
/// Parameters
/// ----------
/// kinematic: int
/// an integer representing the kinematic. 0 represents the scale,
/// 1 represents the momentum fraction of the first parton, and 2
/// represents the momentum fraction of the second parton.
/// kintype: str
/// the type of the kinematics, can be either `Scale` or `X`
/// value: int
/// the index mapping to the value, eg. for `X` the integer `0` => `X1`
#[new]
#[must_use]
pub fn new_kin(kintype: &str, value: usize) -> Self {
Expand All @@ -79,6 +79,93 @@ impl PyKinematics {
}
}

/// PyO3 wrapper to :rustdoc:`pineappl::boc::ScaleFuncForm <boc/enum.ScaleFuncForm.html>`.
#[pyclass(name = "ScaleFuncForm")]
#[repr(transparent)]
pub struct PyScaleFuncForm {
pub(crate) scale_func: ScaleFuncForm,
}

impl PyScaleFuncForm {
pub(crate) const fn new(scale_func: ScaleFuncForm) -> Self {
Self { scale_func }
}
}

#[pymethods]
impl PyScaleFuncForm {
/// Constructor.
///
/// Parameters
/// ----------
/// scaletype: str
/// the type of the scales, can be `NoScale`, `Scale(int)`, etc.
/// value: list(int)
/// list containing the index mapping to the corresponding value
#[new]
#[must_use]
pub fn new_scale(scaletype: &str, value: Vec<usize>) -> Self {
let scale = match scaletype {
"NoScale" => ScaleFuncForm::NoScale,
"Scale" => ScaleFuncForm::Scale(value[0]),
"QuadraticSum" => ScaleFuncForm::QuadraticSum(value[0], value[1]),
"QuadraticMean" => ScaleFuncForm::QuadraticMean(value[0], value[1]),
"QuadraticSumOver4" => ScaleFuncForm::QuadraticSumOver4(value[0], value[1]),
"LinearMean" => ScaleFuncForm::LinearMean(value[0], value[1]),
"LinearSum" => ScaleFuncForm::LinearSum(value[0], value[1]),
"ScaleMax" => ScaleFuncForm::ScaleMax(value[0], value[1]),
"ScaleMin" => ScaleFuncForm::ScaleMin(value[0], value[1]),
"Prod" => ScaleFuncForm::Prod(value[0], value[1]),
"S2plusS1half" => ScaleFuncForm::S2plusS1half(value[0], value[1]),
"Pow4Sum" => ScaleFuncForm::Pow4Sum(value[0], value[1]),
"WgtAvg" => ScaleFuncForm::WgtAvg(value[0], value[1]),
"S2plusS1fourth" => ScaleFuncForm::S2plusS1fourth(value[0], value[1]),
"ExpProd2" => ScaleFuncForm::ExpProd2(value[0], value[1]),
_ => todo!(),
};
Self::new(scale)
}
}

/// PyO3 wrapper to :rustdoc:`pineappl::boc::Scales <boc/struct.Scales.html>`.
#[pyclass(name = "Scales")]
pub struct PyScales {
pub(crate) scales: Scales,
}

impl PyScales {
pub(crate) const fn new(scales: Scales) -> Self {
Self { scales }
}
}

impl Default for PyScales {
fn default() -> Self {
Self::new(Scales {
ren: ScaleFuncForm::Scale(0),
fac: ScaleFuncForm::Scale(0),
frg: ScaleFuncForm::NoScale,
})
}
}

#[pymethods]
impl PyScales {
/// Constructor for `Scales`
#[new]
#[must_use]
pub fn news_scales(
ren: PyRef<PyScaleFuncForm>,
fac: PyRef<PyScaleFuncForm>,
frg: PyRef<PyScaleFuncForm>,
) -> Self {
let ren = ren.scale_func.clone();
let fac = fac.scale_func.clone();
let frg = frg.scale_func.clone();
Self::new(Scales { ren, fac, frg })
}
}

/// PyO3 wrapper to :rustdoc:`pineappl::boc::Order <boc/struct.Order.html>`.
#[pyclass(name = "Order")]
#[repr(transparent)]
Expand Down Expand Up @@ -189,5 +276,7 @@ pub fn register(parent_module: &Bound<'_, PyModule>) -> PyResult<()> {
m.add_class::<PyChannel>()?;
m.add_class::<PyOrder>()?;
m.add_class::<PyKinematics>()?;
m.add_class::<PyScaleFuncForm>()?;
m.add_class::<PyScales>()?;
parent_module.add_submodule(&m)
}
12 changes: 5 additions & 7 deletions pineappl_py/src/grid.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//! Grid interface.
use super::bin::PyBinRemapper;
use super::boc::{PyChannel, PyKinematics, PyOrder};
use super::boc::{PyChannel, PyKinematics, PyOrder, PyScales};
use super::convolutions::PyConv;
use super::evolution::{PyEvolveInfo, PyOperatorSliceInfo};
use super::fk_table::PyFkTable;
Expand All @@ -10,7 +10,6 @@ use super::pids::PyPidBasis;
use super::subgrid::PySubgridEnum;
use ndarray::CowArray;
use numpy::{IntoPyArray, PyArray1, PyReadonlyArray4};
use pineappl::boc::{ScaleFuncForm, Scales};
use pineappl::convolutions::ConvolutionCache;
use pineappl::evolution::AlphasTable;
use pineappl::grid::Grid;
Expand Down Expand Up @@ -55,6 +54,8 @@ impl PyGrid {
/// types of interpolations required by each kinematic
/// kinematics : list(PyKinematics)
/// list of kinematics
/// scale_funcs : PyScales
/// `Scales` object
#[new]
#[must_use]
pub fn new_grid(
Expand All @@ -65,6 +66,7 @@ impl PyGrid {
convolutions: Vec<PyRef<PyConv>>,
interpolations: Vec<PyRef<PyInterp>>,
kinematics: Vec<PyRef<PyKinematics>>,
scale_funcs: PyRef<PyScales>,
) -> Self {
Self {
grid: Grid::new(
Expand All @@ -81,11 +83,7 @@ impl PyGrid {
.map(|pyi| pyi.interp.clone())
.collect(),
kinematics.into_iter().map(|pyk| pyk.kinematics).collect(),
Scales {
ren: ScaleFuncForm::Scale(0),
fac: ScaleFuncForm::Scale(0),
frg: ScaleFuncForm::NoScale,
},
scale_funcs.scales.clone(),
),
}
}
Expand Down
2 changes: 1 addition & 1 deletion pineappl_py/tests/test_boc.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ def test_init(self):

class TestKinematics:
def test_init(self):
kin = Kinematics("Scale", 0)
kin = Kinematics(kintype="Scale", value=0)
assert isinstance(kin, Kinematics)


Expand Down
15 changes: 11 additions & 4 deletions pineappl_py/tests/test_evolution.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from typing import List

import numpy as np
from pineappl.boc import Channel, Kinematics
from pineappl.boc import Channel, Kinematics, ScaleFuncForm, Scales
from pineappl.convolutions import Conv, ConvType
from pineappl.evolution import EvolveInfo, OperatorSliceInfo
from pineappl.grid import Grid, Order
Expand All @@ -25,9 +25,9 @@ def fake_grid(
bins: List[float] = [1e-7, 1e-3, 1],
) -> Grid:
kinematics = [
Kinematics("Scale", 0), # Scale
Kinematics("X", 0), # x1 momentum fraction
Kinematics("X", 1), # x2 momentum fraction
Kinematics(kintype="Scale", value=0), # Scale
Kinematics(kintype="X", value=0), # x1 momentum fraction
Kinematics(kintype="X", value=1), # x2 momentum fraction
]
# Define the interpolation specs for each item of the Kinematics
interpolations = [
Expand All @@ -50,7 +50,13 @@ def fake_grid(
order=3,
), # Interpolation on x2 momentum fraction
]
# Define the bin limits
bin_limits = np.array(bins)
# Construct the `Scales` object
w_scale = ScaleFuncForm(scaletype="Scale", value=[0])
no_scale = ScaleFuncForm(scaletype="NoScale", value=[0])
scale_funcs = Scales(ren=w_scale, fac=w_scale, frg=no_scale)

return Grid(
pid_basis=PidBasis.Evol,
channels=channels,
Expand All @@ -59,6 +65,7 @@ def fake_grid(
convolutions=convolutions,
interpolations=interpolations,
kinematics=kinematics,
scale_funcs=scale_funcs,
)

def test_evolveinfo(self):
Expand Down
9 changes: 8 additions & 1 deletion pineappl_py/tests/test_fk_table.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from typing import List

import numpy as np
from pineappl.boc import Channel, Kinematics
from pineappl.boc import Channel, Kinematics, ScaleFuncForm, Scales
from pineappl.convolutions import Conv, ConvType
from pineappl.fk_table import FkAssumptions, FkTable
from pineappl.grid import Grid, Order
Expand Down Expand Up @@ -65,7 +65,13 @@ def fake_grid(
interpolation_meth=InterpolationMethod.Lagrange,
), # Interpolation on x2 momentum fraction
]
# Define the bin limits
bin_limits = np.array(bins)
# Construct the `Scales` object
w_scale = ScaleFuncForm(scaletype="Scale", value=[0])
no_scale = ScaleFuncForm(scaletype="NoScale", value=[0])
scale_funcs = Scales(ren=w_scale, fac=w_scale, frg=no_scale)

return Grid(
pid_basis=PidBasis.Evol,
channels=channels,
Expand All @@ -74,6 +80,7 @@ def fake_grid(
convolutions=convolutions,
interpolations=interpolations,
kinematics=kinematics,
scale_funcs=scale_funcs,
)

def test_convolve(self):
Expand Down
15 changes: 11 additions & 4 deletions pineappl_py/tests/test_grid.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import numpy as np
import pytest
from pineappl.bin import BinRemapper
from pineappl.boc import Channel, Kinematics
from pineappl.boc import Channel, Kinematics, ScaleFuncForm, Scales
from pineappl.convolutions import Conv, ConvType
from pineappl.grid import Grid, Order
from pineappl.import_subgrid import ImportSubgridV1
Expand Down Expand Up @@ -105,9 +105,9 @@ def fake_grid(
# 2nd item: parton momentum fraction of the 1st convolution
# 3rd tiem: parton momentum fraction of the 2nd convolution
kinematics = [
Kinematics("Scale", 0), # Scale
Kinematics("X", 0), # x1 momentum fraction
Kinematics("X", 1), # x2 momentum fraction
Kinematics(kintype="Scale", value=0), # Scale
Kinematics(kintype="X", value=0), # x1 momentum fraction
Kinematics(kintype="X", value=1), # x2 momentum fraction
]
# Define the interpolation specs for each item of the Kinematics
interpolations = [
Expand All @@ -130,7 +130,13 @@ def fake_grid(
order=3,
), # Interpolation on x2 momentum fraction
]
# Define the bin limits
bin_limits = np.array(bins)
# Construct the `Scales` object
w_scale = ScaleFuncForm(scaletype="Scale", value=[0])
no_scale = ScaleFuncForm(scaletype="NoScale", value=[0])
scale_funcs = Scales(ren=w_scale, fac=w_scale, frg=no_scale)

return Grid(
pid_basis=PidBasis.Evol,
channels=channels,
Expand All @@ -139,6 +145,7 @@ def fake_grid(
convolutions=convolutions,
interpolations=interpolations,
kinematics=kinematics,
scale_funcs=scale_funcs,
)

def test_init(self):
Expand Down
14 changes: 10 additions & 4 deletions pineappl_py/tests/test_subgrid.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import numpy as np
import pytest
from pineappl.boc import Channel, Kinematics, Order
from pineappl.boc import Channel, Kinematics, Order, ScaleFuncForm, Scales
from pineappl.convolutions import Conv, ConvType
from pineappl.grid import Grid
from pineappl.import_subgrid import ImportSubgridV1
Expand All @@ -24,9 +24,9 @@ def fake_dis_grid(
# on the meaning of the following parameters
convolutions = [CONVOBJECT] # Consider DIS-case
kinematics = [
Kinematics("Scale", 0), # Scale
Kinematics("X", 0), # x1 momentum fraction
Kinematics("X", 1), # x2 momentum fraction
Kinematics(kintype="Scale", value=0), # Scale
Kinematics(kintype="X", value=0), # x1 momentum fraction
Kinematics(kintype="X", value=1), # x2 momentum fraction
]
interpolations = [
Interp(
Expand All @@ -48,6 +48,11 @@ def fake_dis_grid(
order=3,
), # Interpolation on x2 momentum fraction
]
# Construct the `Scales` object
w_scale = ScaleFuncForm(scaletype="Scale", value=[0])
no_scale = ScaleFuncForm(scaletype="NoScale", value=[0])
scale_funcs = Scales(ren=w_scale, fac=w_scale, frg=no_scale)

return Grid(
pid_basis=PidBasis.Evol,
channels=channels,
Expand All @@ -56,6 +61,7 @@ def fake_dis_grid(
convolutions=convolutions,
interpolations=interpolations,
kinematics=kinematics,
scale_funcs=scale_funcs,
)


Expand Down

0 comments on commit b6abfbe

Please sign in to comment.