Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SliceAndShuffle fix #43

Merged
merged 1 commit into from
Mar 26, 2024
Merged

SliceAndShuffle fix #43

merged 1 commit into from
Mar 26, 2024

Conversation

liyiersan
Copy link
Contributor

Thanks for your great work. When I tried to use tsgm for time series data augmentation, it seemed that the SliceAndShuffle did not work.
The codes are as follows:

import tsgm
import numpy as np
from matplotlib import pyplot as plt

# init a cosine time series
n_samples = 1
n_timesteps = 1000
n_features = 4
X = np.zeros((n_samples, n_timesteps, n_features))

for i in range(n_features):
    X[:, :, i] = 100*np.cos(np.linspace(i, 2 * np.pi + i, n_timesteps))


aug_model = tsgm.models.augmentations.GaussianNoise()
x1 = aug_model.generate(X=X, n_samples=1, variance=0.2)

aug_model = tsgm.models.augmentations.Shuffle()
x2 = aug_model.generate(X=X, n_samples=1)

aug_model = tsgm.models.augmentations.SliceAndShuffle()
x3 = aug_model.generate(X=X, n_samples=1, n_segments=3)

aug_model = tsgm.models.augmentations.MagnitudeWarping()
x4 = aug_model.generate(X=X, n_samples=1, sigma=1)

aug_model = tsgm.models.augmentations.WindowWarping()
x5 = aug_model.generate(X=X, n_samples=1, scales=(0.5,), window_ratio=0.5)


# show the original and augmented time series
plt.figure(figsize=(12, 8))
plt.subplot(231)
plt.plot(X.squeeze(0))
plt.title('Original')
plt.subplot(232)
plt.plot(x1.squeeze(0))
plt.title('GaussianNoise')
plt.subplot(233)
plt.plot(x2.squeeze(0))
plt.title('Shuffle')
plt.subplot(234)
plt.plot(x3.squeeze(0))
plt.title('SliceAndShuffle')
plt.subplot(235)
plt.plot(x4.squeeze(0))
plt.title('MagnitudeWarping')
plt.subplot(236)
plt.plot(x5.squeeze(0))
plt.title('WindowWarping')
plt.savefig('augmentations.png')

The results are shown in Fig. 1. We can see that there is no difference between the original data and SliceAndShuffle.
Fig.1

I have checked the source codes of SliceAndShuffle, I found that the shuffled slices are not concatenated.

       for i in seeds_idx:
            sequence = X[i]
            if self.per_channel:
                raise NotImplementedError(
                    "SliceAndShuffle separately by feature is not supported yet."
                )
            else:
                # Randomly pick n_segments-1 points where to slice
                idxs = np.random.randint(0, sequence.shape[0], size=n_segments - 1)
                slices = []
                start_idx = 0
                for j in sorted(idxs):
                    s = sequence[start_idx:j]
                    start_idx = j
                    slices.append(s)
                slices.append(sequence[start_idx:])
                np.random.shuffle(slices)
            # sequence is still X[i]
            synthetic_data.append(sequence)

New codes are:

            sequence = np.concatenate(slices)
            synthetic_data.append(sequence)

Now the results are good as shown in Fig. 2.
Fig. 2

Copy link

codecov bot commented Mar 26, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 99.71%. Comparing base (b90e021) to head (595fed3).

Additional details and impacted files
@@            Coverage Diff             @@
##             main      #43      +/-   ##
==========================================
- Coverage   99.71%   99.71%   -0.01%     
==========================================
  Files          28       28              
  Lines        2113     2112       -1     
==========================================
- Hits         2107     2106       -1     
  Misses          6        6              

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@AlexanderVNikitin AlexanderVNikitin merged commit 903bff4 into AlexanderVNikitin:main Mar 26, 2024
4 of 5 checks passed
@AlexanderVNikitin
Copy link
Owner

@liyiersan thank you! 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants