Mulit-dimensional Wavelet transform

121 views
Skip to first unread message

Andrew Gindi

unread,
Oct 15, 2022, 4:12:54 AM10/15/22
to PyWavelets
Hi, 

I currently have extremely large microscopy files saved in the .czi format. 
I am using the czifile python library to extract the relevant information from the files. These files have 2 channels. 

I am trying to perform a lossless compression on these files using py.wavedecn. How would I go about doing this? 

I was able to get the coefficients out but this is too large. How can I compress the size of the coefficients? 

Thank you. 

grl...@gmail.com

unread,
Oct 16, 2022, 8:57:43 AM10/16/22
to PyWavelets
Hi Andrew,

We do not have any compression algorithms implemented in PyWavelets, only the wavelet transforms themselves. A better starting point for your use case is likely the Zarr library and its underlying Numcodecs compression library. I don't think any of these are wavelet-based methods, but using Zarr to store arrays in a chunked format will likely be helpful. For purposes of processing chunked arrays you can look at Dask and for visualization there is Napari. Both Dask and Napari can work directly with Zarr files and in the case of Napari it can be done as a multi-resolution hierarchy so that you only need to load the chunks visible in the viewer at an appropriate level of detail.  Here is a Napari tutorial on working with large datasets that covers these tools

Hopefully this helps!

Cheers,
Greg

Deepu

unread,
May 22, 2023, 2:16:36 AM5/22/23
to PyWavelets
To compress the size of the coefficients obtained from the wavelet decomposition using `pywt.wavedecn`, you can apply various compression techniques. Here are a few approaches you can consider:

1. Thresholding: Set small coefficients below a certain threshold to zero. This is known as "hard thresholding" or "soft thresholding" depending on the method used. By discarding insignificant coefficients, you can achieve compression. You can experiment with different thresholding methods and thresholds to find a suitable balance between compression and reconstruction quality.

2. Quantization: Reduce the precision of the coefficients by quantizing them. This involves mapping the coefficient values to a smaller set of discrete values. The level of quantization determines the compression ratio and the level of detail preserved.

3. Coding: Apply entropy coding techniques such as Huffman coding or arithmetic coding to further compress the coefficients. These techniques exploit the statistical properties of the coefficient values to represent them more efficiently.

Here's an example code snippet that demonstrates thresholding using soft thresholding:

```python
import pywt
import numpy as np

# Perform wavelet decomposition on your data
coeffs = pywt.wavedecn(data, wavelet='db4', level=3)

# Set threshold value
threshold = 0.1

# Apply soft thresholding to the coefficients
thresholded_coeffs = pywt.threshold(coeffs, threshold, mode='soft')

# Reconstruct the compressed data
reconstructed_data = pywt.waverecn(thresholded_coeffs, wavelet='db4')

# Measure the compression ratio
original_size = data.nbytes
compressed_size = sum(coef.nbytes for coef in thresholded_coeffs.values())
compression_ratio = original_size / compressed_size

# Calculate the reconstruction error
reconstruction_error = np.linalg.norm(data - reconstructed_data)

# Print compression information
print("Compression ratio:", compression_ratio)
print("Reconstruction error:", reconstruction_error)
```

In this example, the `threshold` value is set to control the amount of compression. Smaller values result in more aggressive compression by removing more coefficients. You can adjust the threshold value to achieve the desired compression ratio and reconstruction quality.

Note that the example provided demonstrates thresholding as one approach to compression. You can explore other techniques such as quantization or coding to further improve the compression efficiency or adapt to the characteristics of your specific data.

Remember to evaluate the trade-off between compression ratio and the fidelity of the reconstructed data. Experiment with different compression techniques and parameters to find the optimal balance for your application.

I hope this helps you get started with compressing the coefficients obtained from wavelet decomposition. Let me know if you have further questions!
Reply all
Reply to author
Forward
0 new messages