scikit-hep / cabinetry

design and steer profile likelihood fits
https://cabinetry.readthedocs.io/
BSD 3-Clause "New" or "Revised" License
26 stars 19 forks source link

Add rebinning functionality #412

Open alexander-held opened 1 year ago

alexander-held commented 1 year ago

Provide the possibility to re-bin histograms, which probably should happen at the post-processing step. The rebinning could be done via hist and the syntax should presumably follow UHI. Generic rebinnings with bin edges provided would be ideal but require https://github.com/scikit-hep/hist/issues/345.

alexander-held commented 1 year ago

Example for how to do string conversion to slices useable for rebinning:

import re
from typing import Optional, Union

import hist

def _str_to_bound(string: str) -> Optional[Union[int, complex]]:
    """Convert a string to be useable as a slice in UHI style.

    Currently supports indexing by bin counts (via ints), by location (via imaginary
    numbers) and rebinning (also via imaginary numbers). See https://uhi.readthedocs.io/
    for more information about UHI.

    Args:
        string (str): string to be converted

    Returns:
        Optional[Union[int, complex]]: arguments to be put into a slice()
    """
    if string == "":
        return None
    try:
        bound = float(string.rstrip("j"))
    except:
        raise ValueError(f"cannot parse rebinning string: {string}")
    if string[-1] == "j":
        return bound * 1j
    else:
        return int(bound)

h = hist.new.Regular(10, 0, 1).Double().fill([0.5, 0.9])
slice_str = "1:0.9j:2j"

print(*(_str_to_bound(s) for s in slice_str.split(":")))
h[slice(*(_str_to_bound(s) for s in slice_str.split(":")))]