forked from vicente-gonzalez-ruiz/scalar_quantization
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinformation.py
More file actions
55 lines (39 loc) · 1.58 KB
/
information.py
File metadata and controls
55 lines (39 loc) · 1.58 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
55
'''Information estimation.'''
import numpy as np
import math
import logging
import os
def energy(x):
return np.sum(x.astype(np.double)*x.astype(np.double))
def average_energy(x):
return energy(x)/np.size(x)
def entropy(sequence_of_symbols):
value, counts = np.unique(sequence_of_symbols, return_counts = True)
probs = counts / len(sequence_of_symbols)
n_classes = np.count_nonzero(probs)
logging.info("information.entropy: sequence_of_symbols.max() =", sequence_of_symbols.max())
logging.info("information.entropy: sequence_of_symbols.min() =", sequence_of_symbols.min())
logging.info("information.entropy: n_clases = ", n_classes)
#debug.print("information.entropy: probs =", probs)
if n_classes <= 1:
return 0
_entropy = 0.
for i in probs:
_entropy -= i * math.log(i, 2)
logging.debug("information.entropy: _entropy =", _entropy)
return _entropy
if __name__ == "__main__":
sequence = np.array([0,1], dtype=np.uint8)
print(sequence, entropy(sequence))
sequence = np.array([0,1,2,3], dtype=np.uint8)
print(sequence, entropy(sequence))
sequence = np.array([0,1,1,2,3], dtype=np.uint8)
print(sequence, entropy(sequence))
sequence = np.array([0,-1,1,-2,3], dtype=np.uint8)
print(sequence, entropy(sequence))
sequence = np.arange(21, dtype=np.uint8)
print(sequence, entropy(sequence))
sequence = np.arange(21, dtype=np.uint8).reshape(3,7)
print(sequence, entropy(sequence))
sequence = np.arange(21, dtype=np.uint8).reshape(3,7)
print(sequence, PNG_BPP(sequence))