nibabel
nibabel copied to clipboard
setting a tolerance for precision loss when writing
When an image is written out by nibabel, it tries to fit the range of the data into the datatype of the file. in certain instances this could result in a loss of precision. the goal would be to allow setting a tolerance (with appropriate defaults), so that users can control when the loss leads to an exception.
see discussion in #115
Hi @satra. This is old, but do you have a preferred API? A global variable, like nibabel.PRECISION_LOSS_THRESHOLD, and a parameter to nb.save and FileBasedImage.to_* methods?
One additional thing I'm thinking about is a PrecisionLossWarning. This could be noisy enough to let users know there's a potential issue, and programmers can impose strong constraints by upgrading warnings to exceptions:
def safe_save(img, fname):
with warnings.catch_warnings():
warnings.simplefilter('error', nb.PrecisionLossWarning)
nb.save(img, fname)
I'm not sure that setting the tolerance as a parameter is really conceptually compatible with a warning, as I would expect explicit thresholds to lead to errors. Would be happy to hear your thoughts.
@effigies - i would be ok with a global constant if every save method in nibabel took it into account. it can be misleading if only a few save components implement it. for every method this may get a bit complicated.
i like the idea of the warning and the ability for programmers to turn this into exceptions. in some ways this is similar to the tolerance threshold issue we get from ITK.