-
Notifications
You must be signed in to change notification settings - Fork 4
/
huffman.py
87 lines (63 loc) · 2.49 KB
/
huffman.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
import os
from collections import defaultdict, namedtuple
from heapq import heappush, heappop, heapify
import struct
from pathlib import Path
import numpy as np
import torch
Node = namedtuple('Node', 'freq value left right')
Node.__lt__ = lambda x, y: x.freq < y.freq
def huffman_encode(imProj, drawTree = False):
"""
Encodes numpy array 'arr' and saves to `save_dir`
The names of binary files are prefixed with `prefix`
returns the number of bytes for the tree and the data after the compression
"""
int_img = torch.round(imProj).long().flatten()
counts = torch.bincount(int_img)
# counts = counts.float() / torch.sum(counts)
freq_map = list(counts.cpu().numpy())
# Make heap
heap = [Node(frequency, value, None, None) for value, frequency in enumerate(freq_map)]
heapify(heap)
if drawTree:
import networkx as nx
from networkx.drawing.nx_agraph import write_dot, graphviz_layout
import matplotlib.pyplot as plt
G = nx.DiGraph()
# Merge nodes
while(len(heap) > 1):
node1 = heappop(heap)
node2 = heappop(heap)
# merged = Node(node1.freq + node2.freq, '(' + str(node1.value) + ',' + str(node2.value) + ')',
# node1, node2)
merged = Node(node1.freq + node2.freq, None, node1, node2)
heappush(heap, merged)
if drawTree:
G.add_node(str(node1.freq))
G.add_node(str(node1.freq))
G.add_node(str(merged.freq))
G.add_edge(str(merged.freq), str(node2.freq))
G.add_edge(str(merged.freq), str(node1.freq))
# write dot file to use with graphviz
# run "dot -Tpng test.dot >test.png"
if drawTree:
write_dot(G, 'test.dot')
# same layout using matplotlib with no labels
pos = graphviz_layout(G, prog='dot')
nx.draw(G, pos, with_labels=False, arrows=True)
plt.savefig('nx_test.png')
# Generate code value mapping
value2code = {}
def generate_code(node, code):
if node is None:
return
if node.value is not None:
value2code[node.value] = code
return
generate_code(node.left, code + '0')
generate_code(node.right, code + '1')
root = heappop(heap)
generate_code(root, '')
bit_countH = np.sum([len(value2code[i]) * freq_map[i] for i in range(len(freq_map))])
return bit_countH