Last active
September 9, 2019 05:38
-
-
Save khirotaka/cb5cc212a32042231e1e40d480208757 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from collections import Counter | |
import numpy as np | |
class SlidingWindow: | |
def __init__(self, window_size, overlap_rate=0.5): | |
self.window_size = window_size | |
assert 0.0 < overlap_rate and overlap_rate <= 1.0 | |
self.overlap_rate = overlap_rate | |
self.overlap = int(window_size * overlap_rate) | |
def transform(self, x): | |
seq_len = x.shape[0] | |
assert seq_len > self.window_size | |
data = [x[i:i+self.window_size] for i in range(0, seq_len-self.window_size, self.overlap)] | |
data = np.stack(data, 0) | |
return data | |
def clean(self, labels): | |
tmp = [] | |
for l in labels: | |
window_size = len(l) | |
c = Counter(l) | |
common = c.most_common() | |
values = list(c.values()) | |
if common[0][0] == 0 and values[0] == window_size // 2: | |
label = common[1][0] | |
else: | |
label = common[0][0] | |
tmp.append(label) | |
return np.array(tmp) | |
def __call__(self, x, y): | |
data = self.transform(x) | |
label = self.transform(y) | |
label = self.clean(label) | |
return data, label |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment