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

Add rebinning functionality #412

Open
alexander-held opened this issue Jun 20, 2023 · 1 comment · May be fixed by #418
Open

Add rebinning functionality #412

alexander-held opened this issue Jun 20, 2023 · 1 comment · May be fixed by #418
Labels
config Affects configuration schema enhancement New feature or request

Comments

@alexander-held
Copy link
Member

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 scikit-hep/hist#345.

@alexander-held
Copy link
Member Author

alexander-held commented Jul 1, 2023

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(":")))]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
config Affects configuration schema enhancement New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant