xdem icon indicating copy to clipboard operation
xdem copied to clipboard

Limit the number of decimal places when displaying floats with NumPy

Open adebardo opened this issue 5 months ago • 3 comments

Currently, NumPy displays too many decimal places for floating point numbers by default.

Exemple :

Image

Propositions :

  • Use printoptions as a context manager to set the values temporarily.
  • Work directly for the iterations prints
  • Other proposition ?

adebardo avatar Jul 17 '25 08:07 adebardo

I think we should use a consistent number of significant figures (3?) to ensure the number of various magnitude that we have always display correctly.

Apparently there's two ways of doing this, either Python f"{mynum:.3g}" or NumPy with np.printoptions(precision=3) you mention. However both of those will trigger scientific notation for small/large numbers (e.g. 1.234e+05).

If we want to avoid scientific notation and still have right number of significant digits, apparently there aren't many options, it's only np.format_float_positional that is made just for this!

I can deal with this in #759

rhugonnet avatar Jul 18 '25 00:07 rhugonnet

Great ! Thanks :)

adebardo avatar Jul 18 '25 06:07 adebardo

I think we should use a consistent number of significant figures (3?) to ensure the number of various magnitude that we have always display correctly.

Apparently there's two ways of doing this, either Python f"{mynum:.3g}" or NumPy with np.printoptions(precision=3) you mention. However both of those will trigger scientific notation for small/large numbers (e.g. 1.234e+05).

If we want to avoid scientific notation and still have right number of significant digits, apparently there aren't many options, it's only np.format_float_positional that is made just for this!

I can deal with this in #759

Of course, scientific notation is the only way if you want to always display 3 non 0 numbers. But if we know we wants only 3 digits, it is possible to use the format ".3f" instead of ".3g". For example, if the shift is 0.0001 pixels, it makes sense that the print is "0.000" (with .3f) instead of "1e-4" (with .3g).

adehecq avatar Jul 30 '25 09:07 adehecq