calling pywavelet functions wavedecand waverec from a polars dataframe

65 Views Asked by At

I would like to call the following function ( works fine with pandas) with a polars series..

import pywt
def lowpassfilter(signal, thresh = 0.63, wavelet="db4"):
    thresh = thresh*np.nanmax(signal)
    coeff = pywt.wavedec(signal, wavelet, mode="per" )
    coeff[1:] = (pywt.threshold(i, value=thresh, mode="soft" ) for i in coeff[1:])
    reconstructed_signal = pywt.waverec(coeff, wavelet, mode="per" )
    return reconstructed_signal  

i have tried the following:

df = df.with_columns(pl.struct('close').map(lowpassfilter).alias('wavelet')

and i get a weird array "value error, buffer source aray is read only"

could someone help me, and explain how to call the fuction correctly and what i am doing wrong..

many thanks,

1

There are 1 best solutions below

5
alexp On

So telling what exactly you are doing wrong is a little bit hard without data, but maybe this example is helping you further. So assuming your column "close" is a list.

import pywt
import numpy as np
import polars as pl

df = pl.DataFrame(
    {
        "close": [
            [1, 2, 5, 1, 1, 1, 115, 51, 5651, 65, 1651, 651, 651, 156],
            [1, 252, 5, 1452, 1, 1542, 115, 51, 56, 65, 51, 61, 651, 56],
        ],
        "ind": [1, 2],
    }
)


┌────────────────┬─────┐
│ close          ┆ ind │
│ ---            ┆ --- │
│ list[i64]      ┆ i64 │
╞════════════════╪═════╡
│ [1, 2, … 156]  ┆ 1   │
│ [1, 252, … 56] ┆ 2   │
└────────────────┴─────┘

Furthermore adapt your function to return a polars series.

def lowpassfilter(signal, thresh=0.63, wavelet="db4"):
    thresh = thresh * np.nanmax(signal)
    coeff = pywt.wavedec(signal, wavelet, mode="per")
    coeff[1:] = (pywt.threshold(i, value=thresh, mode="soft") for i in coeff[1:])
    reconstructed_signal = pywt.waverec(coeff, wavelet, mode="per")
    return pl.Series(reconstructed_signal)

df.with_columns(pl.col("close").map_elements(lambda x: lowpassfilter(x)))



┌───────────────────────────────────┬─────┐
│ close                             ┆ ind │
│ ---                               ┆ --- │
│ list[f64]                         ┆ i64 │
╞═══════════════════════════════════╪═════╡
│ [12.289185, -70.788354, … 296.92… ┆ 1   │
│ [-44.438136, 35.622057, … 304.48… ┆ 2   │
└───────────────────────────────────┴─────┘