-
Notifications
You must be signed in to change notification settings - Fork 0
/
informationGrid.py
84 lines (66 loc) · 2.07 KB
/
informationGrid.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
import numpy as np
import sys
import StringIO
import zlib
def create_grid_information(img, a=2):
lx = int(len(img)/a)
ly = int(len(img[1])/a)
kolmogorovGrid = np.zeros_like(entropyGrid)
for i in range(lx - a):
for j in range(ly-a):
imgS = img[i*a:i*a+a, j*a:j*a+a]
kolmogorovGrid[i][j] = image_kolmogorov_gzip(imgS.flatten())
return kolmogorovGrid
def image_kolmogorov_gzip(imgString):
getKbytes = lambda s: sys.getsizeof(s)/100000.
#zlib.compress('asdf')
#print(getKbytes(zlib.compress(imgString)))
#exi
return getKbytes(zlib.compress(imgString))
#extraido de ...nao lembro
def compress(uncompressed):
"""Compress a string to a list of output symbols."""
# Build the dictionary.
dict_size = 256
dictionary = dict((chr(i), i) for i in range(dict_size))
# in Python 3: dictionary = {chr(i): i for i in range(dict_size)}
w = ""
result = []
for c in uncompressed:
wc = w + c
if wc in dictionary:
w = wc
else:
result.append(dictionary[w])
# Add wc to the dictionary.
dictionary[wc] = dict_size
dict_size += 1
w = c
# Output the code for w.
if w:
result.append(dictionary[w])
return result
def decompress(compressed):
"""Decompress a list of output ks to a string."""
#
# Build the dictionary.
dict_size = 256
dictionary = dict((i, chr(i)) for i in range(dict_size))
# in Python 3: dictionary = {i: chr(i) for i in range(dict_size)}
# use StringIO, otherwise this becomes O(N^2)
# due to string concatenation in a loop
result = StringIO()
w = chr(compressed.pop(0))
result.write(w)
for k in compressed:
if k in dictionary:
entry = dictionary[k]
elif k == dict_size:
entry = w + w[0]
else:
raise ValueError('Bad compressed k: %s' % k)
result.write(entry)
dictionary[dict_size] = w + entry[0]
dict_size += 1
w = entry
return result.getvalue()