Use PyWavelets from Java/Scala

201 views
Skip to first unread message

Nevo

unread,
Mar 7, 2012, 6:15:26 AM3/7/12
to PyWavelets
Hi!
I'm trying to use wavelet packet transform as well as stationary
wavelet transform from my Java/Scala application, so far the most
complete implementation of wavelets I think may be PyWavelets :) If
only PyWavelets isn't implemented in CPython C extension, I might be
able to use it directly via Jython. But right now, the only feasible
option for me is take out the C implementation and compile it into JNI
lib to be called from my code. I read an old discussion over
it :http://groups.google.com/group/pywavelets/browse_thread/thread/
ff489461404a77c8 , but the last words mentioning rewriting python part
require a large substantial amount of work was scary. I have quite
limited knowledge of Python so better for me is to ask for some
suggestions here. Will it cost a lot if I really want to remove Python
dependency (or maybe rewrite it in Scala/Java) so I may use the lib
from my app? Thanks!

Nevo

Filip Wasilewski

unread,
Mar 11, 2012, 6:44:32 PM3/11/12
to PyWavelets
Hi Nevo,

On Mar 7, 12:15 pm, Nevo <sakur.dea...@gmail.com> wrote:
> I read an old discussion over
> it :http://groups.google.com/group/pywavelets/browse_thread/thread/
> ff489461404a77c8 , but the last words mentioning rewriting python part
> require a large substantial amount of work was scary. I have quite
> limited knowledge of Python so better for me is to ask for some
> suggestions here. Will it cost a lot if I really want to remove Python
> dependency (or maybe rewrite it in Scala/Java) so I may use the lib
> from my app? Thanks!

Well, it depends. Let me give you an overview of the code structure to
illustrate how it is done for Python.

The underlying C code provides core routines to perform decimated and
upsampling convolutions for DWT, IDWT and SWT, as well as some helpers
to compute output buffer lengths and maximum decomposition levels. It
covers the hard part of number crunching and exposes a simple API that
takes pointers to input/output C arrays. There's no memory allocation
(well, except for the wavelet structure bit) at this level so custom
memory management for data arrays can be done in the upper layer (in
this case Numpy arrays and Python garbage collection).

The layer that interfaces with the C code is a Cython file (a dialect
of Python that compiles to C extension). The dwt/idwt code in the
https://bitbucket.org/nigma/pywt/src/50c6b94ccd29/src/_pywt.pyx#cl-459
file does mostly maintenance work in order to call the convolution
routines from the C code. It prepares input parameters, converts input
to Numpy/C arrays if necessary, creates buffers for output, looks up
wavelets by name, calls convolution functions and returns results as
simple data structures where necessary.
This is very similar to the mentioned example from the other thread
(https://bitbucket.org/nigma/pywt/src/tip/demo/dwt_decompose.c),
except for some additional sugar to make the API more Pythonic and
easier to use.
If you choose to use the JNI, I think this is more or less what you
will have to implement to expose the forward and inverse transforms
for use in Java.

The dwt/idwt/swt transform functions from the C/Cython code are then
used to provide additional functionality like multilevel,
multidimensional and wavelet packet transforms (https://bitbucket.org/
nigma/pywt/src/tip/pywt). This is coded in Python because it is mostly
about managing data structures like lists and trees of compute-on-
demand coefficient nodes, which is way easier to implement in a high-
level language rather than coding it in C and exposing it using a
native API (this is what the remark in the other thread was about).

To sum up, I don't have extensive experience in JNI, but I think it
should be pretty straightforward to create a wrapper for the C code
that would handle argument conversion and possibly add a thin layer to
make the API more friendly. That should solve the dwt/swt part.

As far as wavelet packets transform is concerned, you would have to
implement it in Java or Scala from scratch. The good thing is that
once you have a working DWT, the WP algorithm is very simple and you
don't have to follow PyWavelets' approach to get it working.

Filip Wasilewski
http://en.ig.ma/
Reply all
Reply to author
Forward
0 new messages