Skip to content

Commit 7ef02c8

Browse files
author
kahil_dell
committed
PD on patches added to the main code
1 parent 1296d81 commit 7ef02c8

20 files changed

+165
-110
lines changed

PD.py

+2-10
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,10 @@
11

2-
3-
42
import numpy as np
5-
import matplotlib.pyplot as plt
63
import scipy
74
from scipy.fftpack import fftshift, ifftshift, fft2, ifft2
8-
from mpl_toolkits.axes_grid1 import make_axes_locatable, axes_size
9-
from scipy.signal import correlate2d as correlate
10-
from scipy.signal import general_gaussian
11-
from astropy.io import fits
12-
from scipy import ndimage
135
from functools import partial
14-
import time
15-
import imreg_dft
6+
7+
168
import pyfits
179

1810

README.md

+11-1
Original file line numberDiff line numberDiff line change
@@ -46,13 +46,23 @@ The modules of this class:
4646
2. `plot_results`: takes in the returned Zernike coefficients and plots the wavefront error, the 2D MTF, the 1D MTF
4747
3. `restored_scene`: perform the deconvolution of a blurred image with an input of Zernike Coefficients. The user can use between two filter `Wiener` and `richardsonLucy`
4848

49+
## The `patch_pd` class:
50+
For fitting the wavefront error is sub-regions of the full FOV of the PD dataset. The user can enter the size of the subregion (it has to be quadratic) plus the number of Zernike polynomials to be fit and the names of the output files (one for the wavefront error and one for the 2D MTF).
4951
# How to use the code?
50-
The code returns the best-fit zernike polynomials, a visualisation of the results (wavefront error+MTF), and the restored scene (in this case the focused image of the PD dataset). To get these specific results, type in the shell terminal:
52+
The `minimization` class returns the best-fit zernike polynomials, a visualisation of the results (wavefront error+MTF), and the restored scene (in this case the focused image of the PD dataset). To get these specific results, type in the shell terminal:
5153
```
5254
python3 minimization.py -i 'path/input.fits' -s 150 -w 617.3e-6 -a 140 -f 4125.3 -p 0.5 -c 0.5 -r 1e-10 -ap 10 -x1 500 -x2 650 -y1 500 -y2 650 -z 10 -del 0.5 -o path/reduced.fits -fl 'Wiener'
5355
```
5456

5557
The specific description of the parsers can be found inside [the main code](https://github.com/fakahil/PyPD/blob/master/minimization.py). The values given above are for an example PD dataset taken by the PHI/HRT telescope. You can change the values according to your telescope.
5658

59+
To use the `patch_pd` class:
60+
61+
```
62+
python3 patch_pd.py -i 'path/input.fits' -z 10 -d 265 -ow 'path/output_wf.fits' -om 'path/output_wf.fits'
63+
64+
```
65+
The parsers description can be found in the [main code](https://github.com/fakahil/PyPD/blob/master/patch_pd.py)
66+
5767

5868

__pycache__/PD.cpython-38.pyc

-337 Bytes
Binary file not shown.

__pycache__/aperture.cpython-38.pyc

-464 Bytes
Binary file not shown.

__pycache__/cost_func.cpython-38.pyc

1.37 KB
Binary file not shown.
-391 Bytes
Binary file not shown.

__pycache__/noise.cpython-38.pyc

-396 Bytes
Binary file not shown.

__pycache__/telescope.cpython-38.pyc

-510 Bytes
Binary file not shown.

__pycache__/tools.cpython-38.pyc

-129 Bytes
Binary file not shown.

__pycache__/wavefront.cpython-38.pyc

-305 Bytes
Binary file not shown.

__pycache__/zernike.cpython-38.pyc

-510 Bytes
Binary file not shown.

aperture.py

+1-11
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,7 @@
11

22
import numpy as np
33
import matplotlib.pyplot as plt
4-
import scipy
5-
from scipy.fftpack import fftshift, ifftshift, fft2, ifft2
6-
from mpl_toolkits.axes_grid1 import make_axes_locatable, axes_size
7-
from scipy.signal import correlate2d as correlate
8-
from scipy.signal import general_gaussian
9-
from astropy.io import fits
10-
from scipy import ndimage
11-
from functools import partial
12-
import time
13-
import imreg_dft
14-
import pyfits
4+
155

166

177
def mask_pupil(rpupil, size):

cost_func.py

+24-29
Original file line numberDiff line numberDiff line change
@@ -2,34 +2,30 @@
22
import matplotlib.pyplot as plt
33
import scipy
44
from scipy.fftpack import fftshift, ifftshift, fft2, ifft2
5-
from mpl_toolkits.axes_grid1 import make_axes_locatable, axes_size
6-
from scipy.signal import correlate2d as correlate
7-
from scipy.signal import general_gaussian
8-
from astropy.io import fits
9-
from scipy import ndimage
10-
from functools import partial
11-
#import time
12-
import imreg_dft
13-
import pyfits
5+
6+
147
import tools
158
import aperture
169
import PD
1710
import noise
1811
import wavefront
12+
import telescope
13+
from telescope import *
14+
from noise import *
15+
from wavefront import *
16+
from tools import *
17+
from PD import *
1918

2019

21-
class fitting:
22-
def __init__(self,size,cut_off,reg,ap,x1,x2,y1,y2,co_num,del_z,lam, diameter,focal_length,platescale):
20+
class cost_func:
21+
def __init__(self,size,cut_off,reg,ap,co_num,del_z,lam, diameter,focal_length,platescale):
2322

2423
self.cut_off = cut_off
2524
self.reg = reg
2625
self.size = size
2726

2827
self.co_num = co_num
29-
self.x1 = x1
30-
self.x2 = x2
31-
self.y1 = y1
32-
self.y2 = y2
28+
3329
self.del_z = del_z
3430
self.lam = lam
3531
self.diameter = diameter
@@ -39,21 +35,20 @@ def __init__(self,size,cut_off,reg,ap,x1,x2,y1,y2,co_num,del_z,lam, diameter,foc
3935

4036

4137

42-
def Minimise(coefficients):
43-
A_f = wavefront.pupil_foc(coefficients,self.size,rpupil,self.co_num)
44-
A_def = wavefront.pupil_defocus(coefficients,size,del_z,rpupil,co_num)
4538

46-
psf_foc = wavefront.PSF(Mask,A_f,False)
47-
psf_defoc = wavefront.PSF(Mask,A_def,False)
48-
t0 = wavefront.OTF(psf_foc)
49-
tk = wavefront.OTF(psf_defoc)
5039

51-
q,q2 = PD.Q_matrix(t0,tk,reg,gam)
52-
53-
#noise_filter = sch_filter(N0, t0, tk, d0, dk,reg)
54-
F_m = PD.F_M(q2,d0, dk,t0,tk,noise_filter,gam)
40+
def Minimize_res(self,coefficients):
41+
42+
noise_filter = fftshift(noise_mask_high(self.size,self.cut_off))
43+
44+
ph = wavefront.phase_embed(coefficients,self.telescope.pupil_size(),self.size,self.co_num)
45+
A_f = wavefront.pupil_foc(coefficients,self.size,self.telescope.pupil_size(),self.co_num)
46+
47+
48+
psf_foc = wavefront.PSF( aperture.mask_pupil(self.telescope.pupil_size(),self.size),A_f,False)
5549

56-
E_metric = PD.Error_metric(t0,tk,d0,dk,q,noise_filter)
57-
L_m = PD.L_M(E_metric,size)
58-
return L_m
50+
t0 = wavefront.OTF(psf_foc)
51+
52+
53+
return t0,ph
5954

deconvolution.py

+1-8
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,16 @@
11

22
import numpy as np
3-
import matplotlib.pyplot as plt
3+
44
import scipy
55
from scipy.fftpack import fftshift, ifftshift, fft2, ifft2
6-
from mpl_toolkits.axes_grid1 import make_axes_locatable, axes_size
7-
from scipy.signal import correlate2d as correlate
8-
from scipy.signal import general_gaussian
96
from astropy.io import fits
10-
from scipy import ndimage
11-
from functools import partial
12-
import time
137
import imreg_dft
148
import pyfits
159
import tools
1610
import aperture
1711
import PD
1812
import noise
1913
import wavefront
20-
from scipy.optimize import minimize, minimize_scalar
2114
import wavefront
2215
from wavefront import *
2316
import tools

noise.py

+4-10
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,10 @@
11
import numpy as np
2-
import matplotlib.pyplot as plt
2+
33
import scipy
44
from scipy.fftpack import fftshift, ifftshift, fft2, ifft2
5-
from mpl_toolkits.axes_grid1 import make_axes_locatable, axes_size
6-
from scipy.signal import correlate2d as correlate
7-
from scipy.signal import general_gaussian
8-
from astropy.io import fits
9-
from scipy import ndimage
10-
from functools import partial
11-
import time
12-
import imreg_dft
13-
import pyfits
5+
6+
7+
148

159

1610
# compute the gamma parameter

patch_pd.py

+115
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
import time
2+
import imreg_dft
3+
import pyfits
4+
import tools
5+
import aperture
6+
import PD
7+
import noise
8+
import wavefront
9+
import deconvolution
10+
import cost_func
11+
from scipy.optimize import minimize, minimize_scalar
12+
import astropy
13+
from astropy.io import fits
14+
import argparse
15+
import telescope
16+
from telescope import *
17+
18+
from tools import imreg, apo2d
19+
from aperture import *
20+
from PD import *
21+
from noise import *
22+
from wavefront import *
23+
from deconvolution import *
24+
from cost_func import *
25+
26+
27+
class patch_pd(object):
28+
def __init__(self,pd_data,Del,co_num,output_wf,output_mtf):
29+
30+
31+
self.data= fits.getdata(pd_data)
32+
self.Del = Del
33+
self.co_num = co_num
34+
self.output_wf = output_wf
35+
self.output_mtf = output_mtf
36+
37+
38+
self.mean_im0 = self.data[0,500:1000,500:1000].mean()
39+
self.mean_imk = self.data[1,500:1000,500:1000].mean()
40+
41+
self.Im0 = self.data[0,:,:]/self.mean_im0
42+
self.Imk = self.data[1,:,:]/self.mean_imk
43+
44+
self.output_WF = np.zeros((2048,2048))
45+
self.output_mtf = np.zeros((2048,2048))
46+
47+
48+
49+
def fit_patch(self):
50+
51+
upper = 1700
52+
Nx = np.arange(300,upper,self.Del)
53+
Ny = np.arange(300,upper,self.Del)
54+
for n1 in Nx :
55+
for n2 in Ny:
56+
57+
print('fitting the area in:'+str(n1)+','+str(n2))
58+
im0 = self.Im0[n2:n2+self.Del,n1:n1+self.Del]
59+
imk = self.Imk[n2:n2+self.Del,n1:n1+self.Del]
60+
im0 = im0/im0.mean()
61+
imk = imk/imk.mean()
62+
imk = imreg(im0,imk)
63+
64+
65+
66+
im0 = apo2d(im0,10)
67+
imk = apo2d(imk,10)
68+
69+
d0,dk = FT(im0,imk)
70+
gam =1# Gamma(d0,dk,M_gamma)
71+
72+
p0 = np.zeros(self.co_num)
73+
fit = cost_func(self.Del,0.5,1e-10,10,self.co_num,0.5,617.3e-6, 140,4125.3,0.5)
74+
Mask = aperture.mask_pupil(fit.telescope.pupil_size(),fit.size)
75+
noise_temp = noise_mask_high(fit.size,fit.cut_off)
76+
noise_filter = fftshift(noise_temp)
77+
def Minimise(coefficients):
78+
A_f = wavefront.pupil_foc(coefficients,fit.size,fit.telescope.pupil_size(),self.co_num)
79+
A_def = wavefront.pupil_defocus(coefficients,fit.size,fit.del_z,fit.telescope.pupil_size(),self.co_num)
80+
psf_foc = wavefront.PSF(Mask,A_f,False)
81+
psf_defoc = wavefront.PSF(Mask,A_def,False)
82+
t0 = wavefront.OTF(psf_foc)
83+
tk = wavefront.OTF(psf_defoc)
84+
85+
q,q2 = PD.Q_matrix(t0,tk,fit.reg,gam)
86+
87+
88+
F_m = PD.F_M(q2,d0, dk,t0,tk,noise_filter,gam)
89+
90+
E_metric = PD.Error_metric(t0,tk,d0,dk,q,noise_filter)
91+
L_m = PD.L_M(E_metric,fit.size)
92+
return L_m
93+
94+
95+
Minimise_partial = partial(Minimise)
96+
mini = scipy.optimize.minimize(Minimise_partial,p0,method= 'L-BFGS-B')
97+
98+
result = fit.Minimize_res(mini.x)
99+
self.output_WF[n2:n2+self.Del,n1:n1+self.Del] = result[1]
100+
self.output_mtf[n2:n2+self.Del,n1:n1+self.Del] = MTF(fftshift(result[0]))
101+
hdu = fits.PrimaryHDU(self.output_WF)
102+
hdu.writeto(self.output_wf,overwrite=True)
103+
104+
105+
if (__name__ == '__main__'):
106+
parser = argparse.ArgumentParser(description='PD on sub-fields')
107+
parser.add_argument('-i','--input', help='input')
108+
parser.add_argument('-z','--Z', help='Zernike',default=10)
109+
parser.add_argument('-d','--Del', help='Del',default=265)
110+
parser.add_argument('-ow','--ow', help='output_WFE')
111+
parser.add_argument('-om','--om', help='output MTF')
112+
parsed = vars(parser.parse_args())
113+
st = patch_pd(pd_data='{0}'.format(parsed['input']),Del=int(parsed['Del']),co_num=int(parsed['Z']),output_wf='{0}'.format(parsed['ow']),output_mtf='{0}'.format(parsed['om']))
114+
st.fit_patch()
115+

telescope.py

+2-12
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,7 @@
11

22
import numpy as np
3-
import matplotlib.pyplot as plt
4-
import scipy
5-
from scipy.fftpack import fftshift, ifftshift, fft2, ifft2
6-
from mpl_toolkits.axes_grid1 import make_axes_locatable, axes_size
7-
from scipy.signal import correlate2d as correlate
8-
from scipy.signal import general_gaussian
9-
from astropy.io import fits
10-
from scipy import ndimage
11-
from functools import partial
12-
import time
13-
import imreg_dft
14-
import pyfits
3+
4+
155

166

177
#from . import detector

tools.py

+3-5
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,10 @@
55
from mpl_toolkits.axes_grid1 import make_axes_locatable, axes_size
66
from scipy.signal import correlate2d as correlate
77
from scipy.signal import general_gaussian
8-
from astropy.io import fits
98
from scipy import ndimage
10-
from functools import partial
11-
import time
12-
import imreg_dft
13-
import pyfits
9+
10+
11+
1412

1513
def GetPSD1D(psd2D):
1614
h = psd2D.shape[0]

wavefront.py

+2-9
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,9 @@
11
import numpy as np
2-
import matplotlib.pyplot as plt
2+
33
import scipy
44
from scipy.fftpack import fftshift, ifftshift, fft2, ifft2
5-
from mpl_toolkits.axes_grid1 import make_axes_locatable, axes_size
65
from scipy.signal import correlate2d as correlate
7-
from scipy.signal import general_gaussian
8-
from astropy.io import fits
96
from scipy import ndimage
10-
from functools import partial
11-
import time
12-
import imreg_dft
13-
import pyfits
147
import zernike
158

169
def phase(coefficients,rpupil, co_num):
@@ -42,7 +35,7 @@ def phase_aberr(del_z,rpupil):
4235

4336
# Embed the phase in the center of a quadratic map of dimensions sizexsize
4437
def phase_embed(coefficients,rpupil,size,co_num):
45-
ph = phase(coefficients,rpupil)
38+
ph = phase(coefficients,rpupil,co_num)
4639
A = np.zeros([size,size])
4740
A[size//2-rpupil+1:size//2+rpupil+1,size//2-rpupil+1:size//2+rpupil+1]= phase(coefficients,rpupil,co_num)
4841
return A

zernike.py

-15
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,5 @@
11

22
import numpy as np
3-
import matplotlib.pyplot as plt
4-
import scipy
5-
from scipy.fftpack import fftshift, ifftshift, fft2, ifft2
6-
from mpl_toolkits.axes_grid1 import make_axes_locatable, axes_size
7-
from scipy.signal import correlate2d as correlate
8-
from scipy.signal import general_gaussian
9-
from astropy.io import fits
10-
from scipy import ndimage
11-
from functools import partial
12-
import time
13-
import imreg_dft
14-
import pyfits
15-
16-
17-
183

194
'''
205
# Zernike polynomials in cartesian coordinates

0 commit comments

Comments
 (0)