Scripts/ExploreCTF: apply_ctf.py

File apply_ctf.py, 2.8 kB (added by marco, 4 years ago)
Line 
1 #!/usr/bin/env giplt
2 # Tab-Size: 4 (soft)
3
4 import getopt, sys
5 import ex.alg
6
7 def setup_ctf(**kwargs):
8     """
9     Returns a CTF object with your parameters of choice. The convention for
10     defocus is such that positive values depict underfocus and negative
11     values are overfocus.
12     """
13     options = {
14         'defocus'   : 858,
15         'cs'        : 2e7,
16         'cc'        : 2e7
17     }
18     options.update(**kwargs)
19     microscope_data=ex.MicroscopeData(options['cs'],options['cc'],0)
20     microscope_data.SetAccelerationVoltage(100*1.0e23)
21     ctf=ex.alg.CTF(ex.TCIFData(microscope_data,
22         ex.Defocus(options['defocus'])))
23     return ctf
24    
25 def ctf_values(**kwargs):
26     """
27     Returns a one-dimensional array containing the CTF values.
28     """
29     options = {
30         'sampling'  : 1,
31     }
32     options.update(kwargs)
33     ctf=setup_ctf(**kwargs)
34     im=CreateImage(Size(2048,1))
35     im.SetPixelSampling(Vec3(options['sampling'],
36         options['sampling'],options['sampling']))
37     im.ApplyIP(alg.FFT())   
38     im.ApplyIP(alg.Fill(1))       
39     ctf_values=im.Apply(ctf)
40     return [ctf_values.GetComplex(Point(x,0)).real for x in range(0,1024)]
41
42 def plot_ctf(**kwargs):
43     """
44     Returns plot data for CTF with the given parameters
45     """
46     ctf_val=ctf_values(**kwargs)
47     pd=gui.PlotData()
48     for x in range(0,1024):
49         pd.AddXY(x,ctf_val[x])
50     return pd
51
52    
53 def print_usage():
54     print 'Usage: apply_ctf.py <options> image'
55     print 'Options:'
56     print '   --defocus=<value>    Defocus in A. Positive values for underfocus'
57     print '         -cc=<value>    Chromatic abberation in mm'
58     print '         -cs=<value>    Spherical abberation in mm'
59     print '  --sampling=<value>    Sampling for image in A/px'
60     print ''
61     print 'values default to defocus=700A, cc=2mm, cs=2mm, sampling=1A/px'
62
63 def image_ctf(image,**kwargs):
64     ctf=setup_ctf(**kwargs)
65     image=LoadImage(image)
66     image.SetPixelSampling(Vec3(1,1,1))
67     image.ApplyIP(alg.FFT())
68     ctf_mod=image.Apply(ctf)
69     return ctf_mod.Apply(alg.FFT())
70    
71    
72 def tuple_to_dict(t):
73     d = {}
74     for k,v in t:
75         d[k[2:]] = int(v)
76     return d
77
78 def get_options():
79     options=['cs=','cc=','defocus=','sampling=']
80     try:
81         opts,args=getopt.getopt(sys.argv[1:], '', options)
82     except getopt.GetOptError, err:
83         print str(err)
84         print_usage()
85         sys.exit(0)
86     if len(args) != 1:
87         print_usage()
88         sys.exit(0)
89     default= {
90         'defocus'   : 700,
91         'cs'        : 2,
92         'cc'        : 2,
93         'sampling'  : 1
94     }
95     default.update(tuple_to_dict(opts))
96     default['cs']*=1e7
97     default['cc']*=1e7
98     return default,args[0]
99        
100 opts,image_file=get_options()
101
102 print opts
103
104 pv=gui.CreatePlotViewer()
105
106 pv.AddData(plot_ctf(**opts)).SetMode(pv.LINES)
107
108 im=image_ctf(image_file,**opts)
109 v=Viewer(im)
110 fft=im.Apply(alg.FFT())
111 v_fft=Viewer(fft)