-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpadding.py
64 lines (50 loc) · 1.47 KB
/
padding.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
import abc
import numpy as np
def get_padder(method, params=dict()):
padders = {
"zero": ZeroPadder,
"same": SamePadder,
"identical": IdenticalPadder,
}
return padders[method](**params)
class BasePadder():
def __init__(self, padding_size):
self.padding_size = padding_size
@abc.abstractmethod
def transform(self, seq):
pass
@abc.abstractmethod
def inv_transform(self, seq):
pass
class ZeroPadder(BasePadder):
PAD_VALUE = 0.0
def __init__(self, padding_size):
super(ZeroPadder, self).__init__(padding_size)
def transform(self, seq):
return np.hstack([
self.PAD_VALUE * np.ones(self.padding_size),
seq,
self.PAD_VALUE * np.ones(self.padding_size),
])
def inv_transform(self, seq):
ps = self.padding_size
return seq[ps:-ps]
class SamePadder(BasePadder):
def __init__(self, padding_size):
super(SamePadder, self).__init__(padding_size)
def transform(self, seq):
start_val = seq[0]
end_val = seq[-1]
return np.hstack([
start_val * np.ones(self.padding_size),
seq,
end_val * np.ones(self.padding_size),
])
def inv_transform(self, seq):
ps = self.padding_size
return seq[ps:-ps]
class IdenticalPadder(BasePadder):
def transform(self, seq):
return seq
def inv_transform(self, seq):
return seq