1

My dataset involves scalograms found from applying CWT on a timetrace. The shape is currently (n, 100,100) where n is the number of records. I'm trying to make the (100,100,1) scalograms to RGB images so it has to be (100,100,3). This is because I'm applying transfer learning for it and would require it to be RGB.

My question is how should I undertake this transformation? I've looked around but am still a bit confused. I was considering applying CWT with 3 different wavelets to get (100,100,3) however I don't think that's very practical.

1 Answer 1

0

I had the same problem as you. To solve it, I wrote two custom Sklearn transformers (see below). As I am still a beginner, the implementation may not be 100% accurate.

Information about my dataset: I have a dataset with time series data. Each row of my dataset corresponds to a time series. With the fm_cwt_transform_into_scalogramm class, I convert each row of my dataset using pywt.cwt with a single wavelet. With fm_transform_into_image, I convert the data that was transformed with fm_cwt_transform_into_scalogramm into an image with 4 channels. Please note: After transforming the data with fm_cwt_transform_into_scalogramm, you should normalize the transformed data, I believe. I did this with my custom Sklearn transformer fm_normalize. The fm_cwt_transform_into_scalogramm Sklearn Transformer:

class fm_cwt_transform_into_scalogramm(BaseEstimator, TransformerMixin):
def __init__(self, wavelet, motherwavelet_is_complex, scale_bis=None):
    # motherwavelet_is_complex == "reel" oder "komplex" --> z.B. mexh=komplex (set motherwavelet_is_complex=True); gaus=reel  (set motherwavelet_is_complex=False)      
    self.scale_bis = scale_bis # ACHTUNG: MUSS EIN SKALAR sein!!!!!!!!
    self.wavelet = wavelet
    self.motherwavelet_is_complex= motherwavelet_is_complex


def fit(self, X, y=None):
    return self

def transform(self, X, y=None):
    if not isinstance(X, np.ndarray):
        X = np.array(X)
    
    if X.ndim < 2:
        raise ValueError("Input data must have at least two dimensions.")
        
    if self.scale_bis is None:
        self.scale_bis=X.shape[1]+1 #damit hat scalogramm gleich höhe und breite; X.shape[1]=X-achselänge; self.scale_bis=Y-achselänge
        
    return_coefs = np.empty(shape=(X.shape[0],X.shape[1], self.scale_bis-1))
    
    if self.motherwavelet_is_complex:           
        for sample_index in range(X.shape[0]):
            coef, _ = cwt(X[sample_index,:], np.arange(1,self.scale_bis), self.wavelet)  
            return_coefs[sample_index] = np.abs(coef)
    else:
        for sample_index in range(X.shape[0]):
            return_coefs[sample_index], _ = cwt(X[sample_index,:], np.arange(1,self.scale_bis), self.wavelet)            
    return return_coefs

The "fm_transform_into_image" Sklearn Transformer:

class fm_transform_into_image(BaseEstimator, TransformerMixin):
# ACHTUNG!!: Die Daten müssen davor mit fm_normalizer normalisiert werden.
def __init__(self, colormap, scale_bis=None):
    self.colormap = colormap
    self.scale_bis = scale_bis # muss den selben wert wie bei fm_cwt_transform_into_scalogramm haben
    
def fit(self, X, y=None):
    return self
    
def transform(self, X, y=None): # X sind die cwt transformeireten Daten 
    if not isinstance(X, np.ndarray):
        X = np.array(X)
    
    if X.ndim < 2:
        raise ValueError("Input data must have at least two dimensions.")
        
    if self.scale_bis is None:
        self.scale_bis=X.shape[1]+1 #damit hat scalogramm gleich höhe und breite; X.shape[1]=X-achselänge; self.scale_bis=Y-achselänge
        
    return_image = np.empty(shape=(X.shape[0],X.shape[1], self.scale_bis-1, 4))
    
    for sample_index in range(X.shape[0]):
        return_image[sample_index] = ScalarMappable(cmap=self.colormap, norm = NoNorm()).to_rgba(X[sample_index])

    return return_image

The "fm_normalizer" Sklearn Transformer:

class fm_normalizer(BaseEstimator, TransformerMixin):
def __init__(self):
    self.min = None
    self.max = None

def fit(self, X, y=None):    
    self.min = np.min(X)
    self.max = np.max(X)
    return self
    
def transform(self, X, y=None):      
    return (X - self.min) / (self.max - self.min) 

My Sklearn pipeline for fully transforming my data into an image looks as follows:

transf_into_image_pipeline = make_pipeline(
fm_cwt_transform_into_scalogramm(wavelet="gaus2", motherwavelet_is_complex=False),
fm_normalizer(),
fm_transform_into_image(colormap="Pastel2"))
1
  • Can you give an example, please?
    – Alex
    May 10, 2023 at 10:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.