-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconvolutionF.py
More file actions
268 lines (241 loc) · 8.54 KB
/
convolutionF.py
File metadata and controls
268 lines (241 loc) · 8.54 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
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
#matplotlib inline
import matplotlib.pyplot as plt
import numpy as np
import math
def convolve(Iin,k,opt):
#str_arr_I = raw_input('insert only the values of the first 1D array with the space between them:').split(' ')
#Iin=0.0* np.ones(len(str_arr_I))
#for i,j in zip (str_arr_I, xrange(len(str_arr_I))):
# Iin[j]=i
#str_arr_k = raw_input('insert only values of the second 1D array with the space between them:').split(' ')
#k=0.0* np.ones(len(str_arr_k))
#for i,j in zip (str_arr_k, xrange(len(str_arr_k))):
# k[j]=i
kT=k[::-1]
if len(Iin)<len(k):
kT=Iin[::-1]
kT0=kT
kT1=kT
if len(k)!=len(Iin):
kT_=[0]*abs(len(k)-len(Iin))
kT0=np.concatenate((kT_,kT), axis=0)
kT1=np.concatenate((kT,kT_), axis=0)
length_s=0
if len(Iin)>len(k):
length_s=len(Iin)
else:
length_s=len(k)
if len(Iin)<len(k):
Iin=k
M,T=np.meshgrid(Iin,Iin)
il=np.tril(M[:(len(kT)-1),:])
iu=np.triu(M)
mask = il>0
mask_1 = iu>0
justified_mask = np.sort(mask,1)
justified_mask_1 = np.sort(mask_1,1)
justified_mask = justified_mask[:,::]
justified_mask_1 = justified_mask_1[:,::-1]
out = np.zeros_like(il[:,:])
out_1 = np.zeros_like(iu)
out[justified_mask] = il[:,:][mask]
out_1[justified_mask_1] = iu[mask_1]
Rr=np.concatenate((np.dot(out,kT0),np.dot(kT1,out_1)),axis=0)
#print("for full mode {}".format(Rr) )
length_f=len(Rr)
off=length_f-length_s
off_eachend=off/2
f_idx=int(off_eachend)
e_idx=f_idx+length_s
Sr=Rr[f_idx:e_idx]
#print("for same mode {}".format(Sr))
if opt=='full':
return(Rr)
if opt=='same':
return(Sr)
def convolve_VRF(Iin,k,opt):
kT=k
if len(Iin)<len(k):
kT=Iin
kT0=kT
kT1=kT
if len(k)!=len(Iin):
kT_=[0]*abs(len(k)-len(Iin))
kT0=np.concatenate((kT_,kT), axis=0)
kT1=np.concatenate((kT,kT_), axis=0)
length_s=0
if len(Iin)>len(k):
length_s=len(Iin)
else:
length_s=len(k)
if len(Iin)<len(k):
Iin=k
M,T=np.meshgrid(Iin,Iin)
il=np.tril(M[:(len(kT)-1),:])
iu=np.triu(M)
mask = il>0
mask_1 = iu>0
justified_mask = np.sort(mask,1)
justified_mask_1 = np.sort(mask_1,1)
justified_mask = justified_mask[:,::]
justified_mask_1 = justified_mask_1[:,::-1]
out = np.zeros_like(il[:,:])
out_1 = np.zeros_like(iu)
out[justified_mask] = il[:,:][mask]
out_1[justified_mask_1] = iu[mask_1]
Rr=np.concatenate((np.dot(out,kT0),np.dot(kT1,out_1)),axis=0)
#print("for full mode {}".format(Rr) )
length_f=len(Rr)
off=length_f-length_s
off_eachend=off/2
f_idx=int(off_eachend)
e_idx=f_idx+length_s
Sr=Rr[f_idx:e_idx]
#print("for same mode {}".format(Sr))
if opt=='full':
return(Rr)
if opt=='same':
return(Sr)
def convolve_VRF2(Iin,k):
return (np.dot(k,Iin))
import convolutionF as F
def deconvolve_VRF2(sig,mask,deconV,option,value):
sig0=sig
#mask_mir=mask[::-1]
mask_mir=mask
m_tst=F.convolve_VRF2(sig,mask)
deconv = deconV
def main(deconv,mask,sig0,mask_mir):
sigC=F.convolve_VRF2(deconv,mask)
relative_blur=sig0/sigC
with np.errstate(divide='ignore'):
relative_blur[np.isinf(relative_blur)] = -2
deconvP=deconv*F.convolve_VRF2(relative_blur,mask_mir)
error=np.abs(deconvP-deconv)
deconv=deconvP
return(deconv,error)
if option=='iteration':
error=0
for i in xrange(value):
deconv,error=main(deconv,mask,sig0,mask_mir)
#sigC=F.convolve(deconv,mask,conv)
#relative_blur=sig0/sigC
#deconvP=deconv*F.convolve(relative_blur,mask_mir,conv)
#error=np.abs(deconvP-deconv)
#deconv=deconvP
#print('error achieved: {}'.format(error))
if option=='error':
it=0
while True:
deconv,error=main(deconv,mask,sig0,mask_mir)
#sigC=F.convolve(deconv,mask,conv)
#relative_blur=sig0/sigC
#deconvP=deconv*F.convolve(relative_blur,mask_mir,conv)
#error=np.abs(deconvP-deconv)
#deconv=deconvP
it=it+1
#print('number of iteration: {}'.format(it))
if np.all(error<value):
break
print('number of iteration: {}'.format(it))
return(deconv)
import convolutionF as F
def deconvolve(sig,mask,deconV,conv,option,value):
sig0=sig
mask_mir=mask[::-1]
m_tst=F.convolve(sig,mask,conv)
deconv = deconV
def main(deconv,mask,sig0,mask_mir,conv):
sigC=F.convolve(deconv,mask,conv)
relative_blur=sig0/sigC
with np.errstate(divide='ignore'):
relative_blur[np.isinf(relative_blur)] = -2
deconvP=deconv*F.convolve(relative_blur,mask_mir,conv)
error=np.abs(deconvP-deconv)
deconv=deconvP
return(deconv,error)
if option=='iteration':
error=0
for i in xrange(value):
deconv,error=main(deconv,mask,sig0,mask_mir,conv)
#sigC=F.convolve(deconv,mask,conv)
#relative_blur=sig0/sigC
#deconvP=deconv*F.convolve(relative_blur,mask_mir,conv)
#error=np.abs(deconvP-deconv)
#deconv=deconvP
#print('error achieved: {}'.format(error))
if option=='error':
it=0
while True:
deconv,error=main(deconv,mask,sig0,mask_mir,conv)
#sigC=F.convolve(deconv,mask,conv)
#relative_blur=sig0/sigC
#deconvP=deconv*F.convolve(relative_blur,mask_mir,conv)
#error=np.abs(deconvP-deconv)
#deconv=deconvP
it=it+1
#print('number of iteration: {}'.format(it))
if np.all(error<value):
break
print('number of iteration: {}'.format(it))
return(deconv)
import convolutionF as F
def deconvolve_TV(sig,mask,deconV,conv,eps,rgP,option,value):
sig0=sig
mask_mir=mask[::-1]
m_tst=F.convolve(sig,mask,conv)
deconv = deconV
def main(deconv,mask,sig0,mask_mir,conv,eps,rgP):
sigC=F.convolve(deconv,mask,conv)
relative_blur=sig0/sigC
with np.errstate(divide='ignore'):
relative_blur[np.isinf(relative_blur)] = -2
grad=np.gradient(deconv)
#grad=np.diff(deconv)
#norm=np.linalg.norm(grad, ord=1)
norm=np.sqrt(grad**2)
mod_norm=np.sqrt(eps**2+norm**2)
division=(grad)/mod_norm
division[np.isnan(division)] = 0.0
with np.errstate(divide='ignore'):
division[np.isinf(division)] = -2
#divergence=np.sum(np.gradient(division))
divergence=np.gradient(division)
div_rgp=rgP*divergence
#if np.any(div_rgp>1):
#mask=(div_rgp)>1
#div_rgp[:][mask]=divergence[mask]
#mask2=(1-div_rgp)<0.02
#div_rgp[:][mask2]=(rgP/3)*(divergence[mask2])
#div_rgp[np.any(abs(div_rgp)>1)]=divergence
#rgp=divergence/divergence
#div_rgp=rgP*division
deconvP=(deconv/(1-(div_rgp)))*F.convolve(relative_blur,mask_mir,conv)
error=np.abs(deconvP-deconv)
deconv=deconvP
return(deconv,error)
if option=='iteration':
error=0
for i in xrange(value):
deconv,error=main(deconv,mask,sig0,mask_mir,conv,eps,rgP)
#sigC=F.convolve(deconv,mask,conv)
#relative_blur=sig0/sigC
#deconvP=deconv*F.convolve(relative_blur,mask_mir,conv)
#error=np.abs(deconvP-deconv)
#deconv=deconvP
#print('error achieved: {}'.format(error))
if option=='error':
it=0
while True:
deconv,error=main(deconv,mask,sig0,mask_mir,conv,eps,rgP)
#sigC=F.convolve(deconv,mask,conv)
#relative_blur=sig0/sigC
#deconvP=deconv*F.convolve(relative_blur,mask_mir,conv)
#error=np.abs(deconvP-deconv)
#deconv=deconvP
it=it+1
#print('number of iteration: {}'.format(it))
if np.all(error<value):
break
print('number of iteration: {}'.format(it))
return(deconv)