johannfaouzi / pyts

A Python package for time series classification
https://pyts.readthedocs.io
BSD 3-Clause "New" or "Revised" License
1.76k stars 164 forks source link

Generate multiple image from multivariate time series #95

Open hvsw opened 3 years ago

hvsw commented 3 years ago

Description

I was wondering, is fitted.shape equal to (6, 6, 6) because it fitted and transformed for each feature and generated an image for each one, so I have 6 6x6 images?

Steps/Code to Reproduce

This code shows what I mean. fitted.shape is (6, 6, 6).

from pyts.image import GramianAngularField
gasf = GramianAngularField(image_size=6, method='summation')
cueSamples = numpy.zeros((6, 314)) # shape = (6, 314)

fitted = gasf.fit_transform(cueSamples)
fitted.shape # (6, 6, 6)

Versions

NumPy 1.20.2 SciPy 1.6.2 Scikit-Learn 0.24.1 Numba 0.53.1 Pyts 0.11.0

johannfaouzi commented 3 years ago

Indeed, each "row" of cueSamples is independently transformed into an image. In the article introducing this transformation, the authors worked on univariate time series only. The Gramian Angular Field relies on the polar coordinate representation of a univariate time series, so I don't think it would be trivial to generalize for multivariate time series: you can't have a single scalar to define the "angle" of a vector in N dimensions (with N > 2). You could use the cosine similarity to have an angle between two vectors (a vector would represent the different features of a multivariate time series at a given time point):

Capture d’écran 2021-03-29 à 10 31 45

GramianAngularField actually expects a 2D-array as input to represent several univariate time series and not a multivariate time series, thus the transformation of each row independently into an image.

Hope this helps you a bit. If you would like to try the cosine similarity for multivariate time series, I could help you with the code if needed.

hvsw commented 3 years ago

Thank you @johannfaouzi, that's super helpful!

I'm interested in the cosine similarity you mentioned - some context: I'm working on training CNNs with EEG data for BCI systems for my undergrad thesis. Multivariate Time Series Data Transformation for Convolutional Neural Network describes a simple way of getting 1 single image by simply appending the variables images(in my case possibly channels/trials images).

So, what you're suggesting is another option to "join the data", but instead of appending images, we generate a single matrix having the angles, i.e. instead of having N trials/channels images we'd have a matrix with the angle between the trials/channels as a single scalar and generate 1 single image from that matrix "having all the information"?

johannfaouzi commented 3 years ago

Indeed I think that there are two main approaches:

You will find attached a notebook in which I tried several approaches based on cosine similarity (or linear kernel). Here is also a Google Colab link.

Hope this helps you a bit.

Transforming multivariate time series into images.ipynb.zip

Fiwzea commented 11 months ago

@johannfaouzi Hi dear, I am also working on time series classification. I have my 6 input time series data and I want to converted into images using GAF and MTF, as you know that these two methods were designed for univariate input but mine I have 6 input (accelerometer in x,y & z and gyroscope x,y & z), so I need to modify the GAF method to but for multi-variate . I read your suggestion above but i am wondering of how did you reach that, like did you use the average or Concatenation? Also, which package or library did you use? Really appreciate your helping

Fiwzea commented 11 months ago

@hvsw Hi dear, I am also working on time series classification. I have my 6 input time series data and I want to converted into images using GAF and MTF, as you know that these two methods were designed for univariate input but mine I have 6 input (accelerometer in x,y & z and gyroscope x,y & z), so I need to modify the GAF method to but for multi-variate . I read your suggestion above but i am wondering of how did you reach that, like did you use the average or Concatenation? Also, which package or library did you use? Did you use concatenation or average. Really appreciate your helping