-
Notifications
You must be signed in to change notification settings - Fork 51
/
IO2.py
58 lines (50 loc) · 1.84 KB
/
IO2.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
import numpy as np
def Compress(NpArray, RateNeed=0):
if NpArray.ndim == 1:
Row = NpArray.shape
Column = 1
else:
Row, Column = NpArray.shape
ComedNpArray = np.array([Row, Column], dtype=int)
# NpArray=NpArray.flatten()
NpArray = NpArray.reshape((1, Row * Column))
ZeroCounter = 0
for iterr in range(NpArray.size):
if NpArray[0][iterr] == 0:
ZeroCounter = ZeroCounter + 1
else:
if ZeroCounter == 0:
ComedNpArray = np.append(ComedNpArray, np.array(NpArray[0, iterr]))
else:
ComedNpArray = np.append(ComedNpArray, np.array([0, ZeroCounter, NpArray[0, iterr]]))
ZeroCounter = 0
if ZeroCounter != 0:
ComedNpArray = np.append(ComedNpArray, np.array([0, ZeroCounter]))
if RateNeed == 0:
return ComedNpArray
else:
CompressRate = float(ComedNpArray.size) / (Row * Column)
print("CompressRate is :",CompressRate)
return ComedNpArray
def Decompress(NpArray):
Length = NpArray.size - 2
Row = NpArray[0]
Column = NpArray[1]
DecomedNpArray = list()
for interr in range(Length):
if NpArray[interr + 2] == 0:
for x in range(NpArray[interr + 3]):
DecomedNpArray.append(0)
elif NpArray[interr + 1] == 0: # NpArray[interr+2] != 0
pass
else: # NpArray[interr+2] != 0 && NpArray[interr+1] != 0
DecomedNpArray.append(NpArray[interr + 2])
DecomedNpArray = np.array(DecomedNpArray, dtype=int)
DecomedNpArray = DecomedNpArray.reshape(Row, Column)
return DecomedNpArray
def CompressArray(array):
assert type(array) == type(list())
return [Compress(x) for x in array]
def DecompressArray(array):
assert type(array) == type(list())
return [Decompress(x) for x in array]