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

Don't save more decimals than necessary in the uncertainty (or data) files #2095

Open
scarlehoff opened this issue May 30, 2024 · 0 comments · May be fixed by #2099
Open

Don't save more decimals than necessary in the uncertainty (or data) files #2095

scarlehoff opened this issue May 30, 2024 · 0 comments · May be fixed by #2099
Assignees

Comments

@scarlehoff
Copy link
Member

scarlehoff commented May 30, 2024

At the moment due to the way in which the uncertainties are computed in the filter files, running the same file twice with two different computers might results in differences in the ~10 digit.
These differences are completely spurious and I believe it makes more sense to round the values to the point they are more or less stable.

In order to make sure everybody is using the same function it would be a good idea to have a custom_yaml_dumper or something as part of the filter file with a custom representer for floats.

Something like this would achieve the desired value (where I set the number of digits to 8).

import yaml
def prettify_float(dumper, value):
    """Override the default yaml representer: https://github.com/yaml/pyyaml/blob/48838a3c768e3d1bcab44197d800145cfd0719d6/lib/yaml/representer.py#L189"""
    ret = dumper.represent_float(value)
    if len(ret.value) > 8:
        ret_str = f"{value:.8e}"
        ret = dumper.represent_scalar('tag:yaml.org,2002:float', ret_str)
    return ret

yaml.add_representer(float, prettify_float)

Here I'm passing every float that yaml.dump will see to prettify_float, which then delegates to the default represent_float function. If the result has a length greater than 8 (this is looking at the length of the string so it is taking into account things like -, . or e4. Then it rounds the value to 8 digits. Otherwise it uses the default oen.

(assigning this to @comane since he was taking care of organizing a bit the filter utils)

@Radonirinaunimi Radonirinaunimi linked a pull request Jun 19, 2024 that will close this issue
3 tasks
@Radonirinaunimi Radonirinaunimi linked a pull request Jul 17, 2024 that will close this issue
3 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants