Skip to content

Instantly share code, notes, and snippets.

@raphaeljolivet
Last active November 22, 2024 09:21
Show Gist options
  • Save raphaeljolivet/806a7bab89798de8bb2a42b446b3afbc to your computer and use it in GitHub Desktop.
Save raphaeljolivet/806a7bab89798de8bb2a42b446b3afbc to your computer and use it in GitHub Desktop.
Truncate / quantize numpy array of float32 to given number of signifiant bits
def strip_bits(array, significant_bits):
"""
This function truncates the mantiss of float32 values in an ndarray to a given number of significant bits.
It is useful to drop non significant data before compressing the output, to achieve better compression ratios.
This method is already available in NetCDF (https://unidata.github.io/netcdf4-python/#efficient-compression-of-netcdf-variables)
with the parmeter `significant_digits` of `createVariable` but might not be compatible with older versions.
Note that one digit (in decimal format) corresponds roughly to 3.3 bits = 10 bits for 3 digits of precision, which might be sufficient
for many use case.
"""
array = array.astype(np.float32)
# Create a mask keeping expient + significant bits of the mantiss
nbits = 9 + significant_bits
mask = (1 << nbits) - 1 << (32 - nbits) # (N x 1 bits) + 0000000
int_array = array.view(np.uint32)
int_array = int_array & np.uint32(mask)
return int_array.view(np.float32)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment