-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgipae.py
330 lines (260 loc) · 8.92 KB
/
gipae.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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
'''
#to be done
Try to come up with an EOF char and include it, doing which you would be able to mark the end of line and file
10 is used for EOL
then merge the multiple lists into one, convert to a square and append zeroes
write to image
Decode in the similar way, taking the steps backwards
'''
from math import *
from numpy import *
from PIL import Image
import sys
import re
def comp_square(num):
'''This method completes the squares of the list given, returns l,m,zero
input: list - list of numbers to be converted into a matrix
output: int - l: The number of rows the matrix should have
int - m: The number of columns the matrix should have
int - zero: The number of zeros to be added to the list in order to reshape it to a matrix
'''
#transforming the array into a square matrix and adding the extra number of zeros to complete the square
l=sqrt(len(num))
l=int(l)
m=l+1
elem=l*m
zero=elem-len(num)
if zero>=0:
m=l+1
else:
l=l+1
elem=l*m
zero=elem-len(num)
#print elem
#print zero
return l,m,zero
def encode_wrapper(fl,mx,zero):
'''This function returns the file name and path separately for further processing
This function also returns the path and filename where the output after decoding should be stored
input: string - fl: The path to file with the filename
float - mx: The maximum of the float values encoded in the image
int - zero: The number of zeros added for completing the matrix
output: string - fname: The name of the file separate from the path
string - path: The path of the file, after removing the filename from it
string - store: The path and file name where the output should be stored
'''
b=fl.split('/')
fname=b[-1]
path='/'.join(b[:-1])
b=fname.split('.')
fn='_'.join(b)
if path:
store=path+'/'+fn+'_'+str(mx)+'_'+str(zero)+'.tiff'
else:
store=fn+'_'+str(mx)+'_'+str(zero)+'.tiff'
return fname,path,store
def encoder(num,l,m,zero,mx,precision=1):
'''
This is the low precision encoder
This method encdes the floating point values as is in the image, which as a result loose precision and are accurate only upto 6 or 7 decimal places
input: list - num: The list containing the floating point values
int - l: The number of rows of the matrix
int - m: The number of columns of matrix
int - zero: The number of zeros to be appended to the matrix for completing the square
output: numpy.ndarray - num: The input list num reshaped into a matrix form
'''
#append the number of zeros in 'zero' variable to num
num.extend([0]*zero)
if precision:
mx=float(mx)
num=[num[i]/float(mx) for i in range(2,len(num))]
#inserting the precision bit and delimiter bit again as num leaves out the first two bits when dividing
num.insert(0,precision)
num.insert(1,ord(delim)/255.0)
#reshaping the list to form a matrix
num=reshape(num,(l,m))
#reducing the decimal precision
#num=around(num, decimals=8)
return num
def print_help():
start = "\033[1m"
end = "\033[0;0m"
try:
f=open('gipa_man.txt')
#p=f.readlines()
p=f.read()
f.close()
except:
p="Documentation not found!"
p=p.split('<b>')
p=start.join(p)
p=p.split('</b>')
p=end.join(p)
print p
def arg_parser(argv):
'''
This is an argument parser for the command line arguments, which can be used to interpret the delimiter and precision
'''
#if the first argument is help, display help, else
#if no argument, use defaults, delim=' ', precision=1
if len(argv)==2:
if re.match("^--h|^-h",argv[1]):
print_help()
exit(0)
else:
delim=' '
precision=1
return delim,precision
#if only 1 argument is specified
if len(argv)==3:
#if the 3rd argument is delimiter
if re.match("^--d|^-d",argv[2]):
#a=argv[2].split('=')[1]
#delim=a.split("'")[1]
delim=argv[2].split('=')[1]
#use default precision=1
precision=1
return delim,precision
#if the 3rd argument is precision
elif re.match("^--p|^-p",argv[2]):
a=argv[2].split('=')[1]
high=['high',1]
low=['low',0]
try:
e=int(a)
except:
a=a.replace('"','')
e=a.replace("'",'')
#e=a.split("'")[1]
if e in high:
precision=1
elif e in low:
precision=0
else:
print "Precision not recognized!"
exit(0)
#use default delimiter ' '
delim=' '
return delim,precision
else:
print_help()
exit(0)
return delim,precision
#if both the arguments are present
if len(argv)==4:
#if 3rd arg is delimiter and 4th precision
if re.match("^--d|^-d",argv[2]):
#a=argv[2].split('=')[1]
#delim=a.split("'")[1]
delim=argv[2].split('=')[1]
#if the 4th argument is precision
if re.match("^--p|^-p",argv[3]):
a=argv[3].split('=')[1]
high=['high',1]
low=['low',0]
try:
e=int(a)
except:
a=a.replace('"','')
e=a.replace("'",'')
#e=a.split("'")[1]
if e in high:
precision=1
elif e in low:
precision=0
else:
print "Precision not recognized!"
print_help()
exit(0)
return delim,precision
#if 3rd arg is precision and 4th delimiter
elif re.match("^--p|^-p",argv[2]):
a=argv[2].split('=')[1]
high=['high',1]
low=['low',0]
try:
e=int(a)
except:
a=a.replace('"','')
e=a.replace("'",'')
#e=a.split("'")[1]
if e in high:
precision=1
elif e in low:
precision=0
else:
print "Precision not recognized!"
exit(0)
#if the 4th argument is delimiter
if re.match("^--d|^-d",argv[3]):
#a=argv[3].split('=')[1]
#delim=a.split("'")[1]
delim=argv[3].split('=')[1]
return delim,precision
else:
print "Arguments not recognized"
print_help()
exit(0)
if len(argv)>4:
print "Number of arguments greater than expected"
print_help()
exit(0)
if __name__ == '__main__':
if len(sys.argv) < 2:
print "Usage: python <name> <filename> <delimiter*> <precision*>"
print_help()
exit(0)
#delim=' '
#precision=1
delim,precision=arg_parser(sys.argv)
fl=sys.argv[1]
print "Encoding "+fl
#Enter the file name with path to be opened here
#fl='/Users/gmalik9/Desktop/Genome Designer/Data Compresion/input-copy-2.txt'
try:
f=open(fl)
#p=f.readlines()
p=f.readlines()
f.close()
except:
print("No such file or directory")
print_help()
exit(0)
#merging the array into a single one read from multiple lines in the file
p=delim.join(p)
#splitting the merged array based on the delimiter and converting into float
num=p.split(delim)
num=[float(num[i]) for i in range(0,len(num))]
mx=max(num)
#add precision bit and ASCII of delimiter/255.0 in the first two pixels of the image respectively
num.insert(0,precision)
num.insert(1,ord(delim)/255.0)
#completing the square
l,m,zero=comp_square(num)
#Return the filename, path and the path+filename for the image to be stored
fname,path,store=encode_wrapper(fl,mx,zero)
num=encoder(num,l,m,zero,mx,precision)
#save it to the path from where it was taken and store it with the new name accordingly
#try writing it to an image file now
img1=Image.fromarray(num.astype('float'))
#img1 = Image.fromarray(num)
#img1.save('test.tiff')
img1.save(store)
print "Encoding Complete. File stored as "+store
#remove everything under this before deployment
#
#
# |||||
# |||
# |||
# |||
# ||| omment this
# |||
# |||
# |||||
#
#
savetxt('test.txt',num)
#f1 is basically the equivalent of num, it is exactly num, just not in a matrix form
#remove f1 before deployment, here only for testing
f1 = list(img1.getdata())