-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathHuffman.py
More file actions
54 lines (48 loc) · 1.49 KB
/
Huffman.py
File metadata and controls
54 lines (48 loc) · 1.49 KB
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
# @yifan
#
import numpy as np
import huffman
class Huffman():
def __init__(self):
self.hist = None
self.dict = {}
self.inv_dict = {}
self.version = '2021.05.20'
def Hist(self, x, bins=64):
x = x.reshape(-1).astype('int32')
res = np.zeros(bins)
for i in range(bins):
res[i] = len(x[x == i])
return res
def make_dict(self):
tmp = []
for i in range(len(self.hist)):
tmp.append((str(i), self.hist[i]))
self.dict = huffman.codebook(tmp)
self.inv_dict = {v: k for k, v in self.dict.items()}
def fit(self, X):
bins = len(np.unique(X.astype('int16')))
self.hist = self.Hist(X, bins=bins)
self.make_dict()
return self
def encode(self, X):
X = X.reshape(-1).astype('int32')
stream = ''
for i in range(len(X)):
stream += self.dict[str(X[i])]
return stream
def decode(self, stream, start=0, size=-1):
if size < 0:
size = len(stream)
dX, last, ct = [], start, 0
for i in range(start, len(stream)):
if stream[last:i] in self.inv_dict:
dX.append((int)(self.inv_dict[stream[last:i]]))
last = i
ct += 1
if ct >= size:
break
if ct < size:
dX.append((int)(self.inv_dict[stream[last:]]))
last = len(stream)
return np.array(dX), last