-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathutils.py
219 lines (178 loc) · 5.3 KB
/
utils.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
import os
import math
import numpy as np
import bitstring
import sys
from PIL import Image
#Return inverse module m of a
def imodule(a, m):
if m == 0: return 0
a = a % m
for x in range(m):
if (a * x) % m == 1:
return x
return 1
#Return first or second number coprime with m
def coprime(m, mode="first"):
if mode is not "first" and mode is not "second":
sys.exit("COPRIME: Mode must be first or second!")
found = 0
for x in range(2,m):
if math.gcd(x,m) == 1:
if mode is "first":
return x
elif mode is "second":
if found < 1:
found += 1
else:
return x
return 1
#Routine to create path for file to save
def makeFileName(prefix, path):
fileName = os.path.basename(path)
dirName = os.path.dirname(path)
fileName = prefix + "-" + fileName
nPath = os.path.join(dirName, fileName)
return nPath
#Routine to extract name of the file from its path without its extension
def withoutExtensionFile(pathname, sufix=""):
pathname = os.path.basename(pathname).split(".")
return pathname[0]+sufix
def splitFloat(number):
if type(number) not in (float, np.float64):
sys.exit("SPLIT FLOAT: number must be float!")
return
whole = int(number)
dec = number - whole
return whole, dec
def joinFloat(whole, dec):
return whole + dec
def setLastBit(number, bit):
if type(number) in (int, np.int16, np.int64):
return ((number >> 1) << 1) | bit
elif type(number) in (float, np.float64):
whole, dec = splitFloat(number)
number = setLastBit(whole, bit)
return joinFloat(number, dec)
"""
binary = decToBinary(number)
binary = binary[:23] + str(bit) + binary[24:]
return binaryToDec(binary)
"""
def getLastBit(number):
if type(number) in (int, np.int16, np.int64):
return int(number % 2)
elif type(number) in (float, np.float64):
whole, dec = splitFloat(number)
return getLastBit(whole)
"""
binary = decToBinary(number)
return int(binary[23])
"""
#Return a string containing the number written in binary notation with bits bit
def decToBinary(number, bits=16):
if type(number) in (int, np.int16):
if number > 2**bits:
sys.exit("DEC TO BINARY: Insufficient number of bit!")
binary = ""
for i in range(bits):
if number != 0:
bit = number % 2
number = number >> 1
binary += str(bit)
else:
binary += str(0)
return binary[::-1]
elif type(number) in (float, np.float64):
b = bitstring.BitArray(float=number, length=32).bin
return b[:9] + " " + b[9:]
#Return a int from a string containing the number written in binary notation
def binaryToDec(number):
if number.find(" ") == -1:
somma = 0
number = number[::-1]
for i in range(len(number)):
somma += int(number[i]) * 2**i
return somma
else:
return bitstring.BitArray(bin=number).float
#Normalize numbers with a range [0,range] in [0,1]
def normalize(number, range):
return number/range
def inormalize(number, range):
return int(number*255)
#Process the Image data for metrics
def ImageToFlattedArray(image):
return np.ravel(np.asarray(image))
#Procedure to have two images of same size
def fixSizeImg(img, toFixImg, imgMode):
mode = ("L" if imgMode == 1 else "1")
width, heigth = img.size
fWidth, fHeigth = toFixImg.size
nImage = Image.new(mode, (width, heigth))
for i in range(width):
if i >= fWidth: break
for j in range(heigth):
if j >= fHeigth: break
value = toFixImg.getpixel(xy=(i,j))
nImage.putpixel(xy=(i,j),value=value)
return nImage
def setBinary(coeff, bit):
if bit == 255:
coeff[-1] = 10
else:
coeff[-1] = -10
return coeff
def getBinary(coeff):
if coeff[-1] > 0:
return 255
else:
return 0
def setGray(coeff, bit):
coeff[-1] = bit - 127
return coeff
def getGray(coeff):
return int(coeff[-1] + 127)
def setDelta(norm, delta, value):
if value == 255:
return (norm + delta), (norm - delta)
else:
return (norm - delta), (norm + delta)
def getDelta(norm1 , norm2):
if norm1 > norm2:
return 255
else:
return 0
def subVectors(coeff):
coeffsLen = len(coeff) // 2
v1 = np.zeros(coeffsLen)
v2 = np.zeros(coeffsLen)
for i in range(coeffsLen):
v1[i] = coeff[2*(i+1) - 1]
v2[i] = coeff[2*(i+1) - 2]
return v1, v2
def isubVectors(v1, v2):
coeffsLen = len(v1)
v = np.zeros(coeffsLen * 2)
for i in range(coeffsLen):
v[2*(i+1) - 1] = v1[i]
v[2*(i+1) - 2] = v2[i]
return v
def inormCalc(norm1, u1):
vLen = len(u1)
v1 = np.zeros(vLen)
for i in range(vLen):
v1[i] = norm1 * u1[i]
return v1
def normCalc(v):
norm = 0
for coeff in v:
norm += coeff**2
norm = math.sqrt(norm)
u = v/norm
return norm, u
#swap value in array
def swap(coeff, a, b):
data = coeff.copy()
data[a], data[b] = data[b], data[a]
return data