| Bsoft 2.0.5 Bernard's software package

bfft.cpp
Go to the documentation of this file.
1
10 #include "rwimg.h"
11 #include "qsort_functions.h"
12 #include "options.h"
13 #include "timer.h"
14
15 // Declaration of global variables
16 extern int verbose; // Level of output to the screen
17
18 // Function prototype
19 int img_fft_times(int ndim, int minsize, int maxsize, int opt);
20
21 /* Usage assistance */
22 const char* use[] = {
23 " ",
24 "Usage: bfft [options] file.hkl file.mrc",
25 "---------------------------------------",
26 "Fast Fourier transforms to and from image and reflection formats.",
27 " ",
28 "Actions:",
29 "-inverse Backward transform (default forward).",
30 "-phaseshift Shift phase by half of the size.",
31 "-powerspectrum Calculate powerspectrum estimate (can be used with the -tile option).",
32 "-average Average multiple power spectra (can be used with the -tile option).",
33 "-logarithm Calculate the logarithm of the power spectrum.",
34 "-color 1.5 Generate a phase-coloured power spectrum: amplitude scaling.",
35 //"-weigh Weigh reflections with FOM.",
36 //"-cutoff 0.2 Remove reflections with FOM < cutoff threshold.",
37 "-zeroorigin Zero the transform origin.",
38 "-halfshift Shift correlation map by half the size to put the origin in the middle.",
39 //"-cone 45 Remove missing cone.",
40 "-test 2,75,278,1 Test FFT execution time: dimensions,size min and max, and optimization flag.",
41 " ",
42 "Parameters:",
43 "-verbose 7 Verbosity of output.",
44 "-datatype u Force writing of a new data type.",
45 "-resolution 4.5,130 Resolution (default 0 - 10000 angstrom).",
46 "-free 15 % flagged for R-free calculations (default 10).",
47 "-origin 0,-10,30 Set origin, used with -size option (default 0,0,0).",
48 "-size 22,33,14 Size for transform or hkl input (voxels, default from data).",
49 "-sampling 2,3.5,1 Sampling (angstrom/voxel, a single value sets all three).",
50 "-unitcell 100,100,100,90,90,90 Unit cell parameters.",
51 "-symmetry 1 Space group.",
52 "-tile 1024,1024,1 Size of tiles for calculating a power spectrum.",
53 " ",
54 //"Output:",
55 //"-Postscript out.ps Postscript output for radial logarithm of power spectrum (only with -logarithm option).",
56 " ",
57 NULL
58 };
59
60 int main(int argc, char **argv)
61 {
62 // Initialize variables
63 DataType nudatatype(Unknown_Type); // Conversion to new type
64 fft_direction setdir(FFTW_FORWARD); // Forward transform
65 int phase_shift(0); // Flag to shift phase by half the size
66 int setpower(0); // Power spectrum flags
67 int setfom(0); // Flag to modify reflections and FOM
68
69 Vector3<long> size; // Size for extraction or HKL
70 Vector3<double> nuorigin; // Origin for extraction or HKL
71 int set_origin(0); // Flag for setting the origin
72 Vector3<double> sam; // Sampling
73 int spacegroup(0); // Spacegroup
74 UnitCell uc(0,0,0,M_PI_2,M_PI_2,M_PI_2); // Unit cell parameters
75 Vector3<long> tile_size; // Size of power spectrum tiles
76
77 double hires(0), lores(1e4); // Limiting resolution range (hires must be > 0 to be set)
78 int zero_origin(0); // Don't zero the transform origin
79 int halfshift(0); // Flag to shift origin to middle
80 double cone(M_PI_2); // Missing cone (default none)
81 int weigh(0); // No weighting
82 double fomcut(0); // Selection based on FOM cutoff
83 double pfree(10); // % Rfree flags
84
85 double colour_phase_scale(0); // Scale for coloured phases
86
87 int test_dim(0); // Number of dimensions to test execution times
88 int test_minsize(0); // Minimum size to test execution times
89 int test_maxsize(0); // Maximum size to test execution times
90 int test_opt(0); // Optimization flag for testing execution times
91
92 Bstring ps_file; // Postscript file for radial power spectrum
93
94 int optind;
95 Boption* option = get_option_list(use, argc, argv, optind);
96 Boption* curropt;
97 for ( curropt = option; curropt; curropt = curropt->next ) {
98 if ( curropt->tag == "datatype" )
99 nudatatype = curropt->datatype();
100 if ( curropt->tag == "inverse" ) setdir = FFTW_BACKWARD;
101 if ( curropt->tag == "phaseshift" ) phase_shift = 1;
102 if ( curropt->tag == "powerspectrum" ) setpower |= 1;
103 if ( curropt->tag == "average" ) setpower |= 2;
104 if ( curropt->tag == "logarithm" ) { setpower |= 8; }
105 if ( curropt->tag == "halfshift" ) { setpower |= 4; halfshift = 1; }
106 if ( curropt->tag == "color" )
107 if ( ( colour_phase_scale = curropt->value.real() ) < 0.001 )
108 cerr << "-color: A scale for the phase colours must be specified" << endl;
109 if ( curropt->tag == "test" )
110 if ( curropt->values(test_dim, test_minsize, test_maxsize, test_opt) < 2 )
111 cerr << "-test: At least the number of dimensions and one size must be specified" << endl;
112 if ( curropt->tag == "zeroorigin" ) zero_origin = 1;
113 if ( curropt->tag == "cone" ) {
114 if ( ( cone = curropt->value.real() ) < 1 )
115 cerr << "-cone: A cone angle must be specified" << endl;
116 else
117 setfom = 1;
118 if ( cone < 0 ) cone = -cone;
119 if ( cone > TWOPI ) cone *= M_PI/180;
120 }
121 if ( curropt->tag == "resolution" ) {
122 if ( curropt->values(hires, lores) < 1 )
123 cerr << "-resolution: A high resolution limit must be specified" << endl;
124 else
125 setfom = 1;
126 }
127 if ( curropt->tag == "weigh" )
128 setfom = weigh = 1;
129 if ( curropt->tag == "cutoff" ) {
130 if ( ( fomcut = curropt->value.real() ) < 0.001 )
131 cerr << "-cutoff: FOM cutoff must be specified" << endl;
132 else
133 setfom = 1;
134 if ( fomcut < 0 ) fomcut = 0;
135 }
136 if ( curropt->tag == "free" )
137 if ( ( pfree = curropt->value.real() ) < 0.001 )
138 cerr << "-free: %% reflections flagged must be specified" << endl;
139 if ( curropt->tag == "origin" ) {
140 nuorigin = curropt->origin();
141 set_origin = 1;
142 }
143 if ( curropt->tag == "size" )
144 size = curropt->size();
145 if ( curropt->tag == "sampling" )
146 sam = curropt->sampling();
147 if ( curropt->tag == "unitcell" )
148 uc = curropt->unit_cell();
149 if ( curropt->tag == "symmetry" )
150 if ( ( spacegroup = curropt->value.integer() ) < 1 )
151 cerr << "-symmetry: The space group number must be specified" << endl;
152 if ( curropt->tag == "tile" )
153 tile_size = curropt->size();
154 if ( curropt->tag == "Postscript" )
155 ps_file = curropt->filename();
156 }
157 option_kill(option);
158
159 double ti = timer_start();
160
161 #ifdef HAVE_GCD
162 fftwf_init_threads();
163 fftwf_plan_with_nthreads(system_processors());
164 #endif
165 if ( verbose )
166 cout << "Number of threads: " << system_processors() << endl;
167
168 if ( test_dim && test_minsize )
169 img_fft_times(test_dim, test_minsize, test_maxsize, test_opt);
170
171 // Allocate memory for the structure factors and image parameters
172 Bimage* p = NULL;
173
174 // Read the file
175 if ( optind >= argc-1 ) {
176 bexit(0);
177 } else if ( ( p = read_img(argv[optind++], 1, -1) ) == NULL ) {
178 cerr << "File not read!" << endl;
179 bexit(-1);
180 }
181
182 if ( nudatatype == Unknown_Type )
183 nudatatype = p->data_type();
184
185 if ( p->data_type() < Float )
186 p->change_type(Float);
187
188 if ( spacegroup ) p->space_group(spacegroup);
189
190 if ( sam.volume() > 0 ) p->sampling(sam);
191
192 if ( set_origin ) p->origin(nuorigin);
193
194 if ( uc.check() ) p->unit_cell(uc);
195
196 // Apply symmetery and select specific reflections if it is a transform
197 if ( p->compound_type() == TComplex ) {
198 if ( setfom ) {
199 if ( zero_origin ) p->zero_fourier_origin();
200 }
201 }
202
203 if ( size.volume() > 0 )
204 if ( p->fourier_type() > NoTransform )
205 p->change_transform_size(size);
206
207 Bimage* pnu = NULL;
208
209 if ( verbose & VERB_DEBUG )
210 cout << "DEBUG bfft: setpower=" << setpower << endl;
211
212 if ( setpower ) {
213 if ( tile_size.volume() > 0 ) {
214 setpower |= 2;
215 pnu = p->powerspectrum_tiled(0, tile_size, setpower);
216 if ( pnu ) {
217 delete p;
218 p = pnu;
219 }
220 } else {
221 p->power_spectrum(setpower);
222 }
223 } else {
224 if ( phase_shift && p->fourier_type() == Standard )
225 p->phase_shift_to_center();
226 if ( p->fft(setdir) )
227 return error_show("bfft", __FILE__, __LINE__);
228 if ( phase_shift && p->fourier_type() == Standard )
229 p->phase_shift_to_center();
230 }
231
232 if ( size.volume() > 0 )
233 if ( p->fourier_type() > NoTransform )
234 p->change_transform_size(size);
235
236 if ( p->compound_type() == TComplex ) {
237 if ( setfom ) {
238 if ( zero_origin ) p->zero_fourier_origin();
239 }
240
241 if ( colour_phase_scale > 0 ) {
242 pnu = p->intensities_phase_colored(colour_phase_scale);
243 if ( pnu ) {
244 delete p;
245 p = pnu;
246 nudatatype = p->data_type();
247 }
248 Bstring filename("ps_color_wheel.png");
249 pnu = new Bimage(UCharacter, TRGB, 512, 512, 1, 1);
250 pnu->phase_colour_wheel();
251 write_img(filename, pnu, 0);
252 delete pnu;
253 }
254 }
255
256 // Write an output file if a file name is given
257 if ( optind < argc ) {
258 p->change_type(nudatatype);
259 p->label(get_cmd_line(argc, argv));
260 if ( halfshift ) p->center_wrap();
261 write_img(argv[optind], p, 0);
262 }
263
264 delete p;
265
266 #ifdef HAVE_GCD
267 fftwf_cleanup_threads();
268 #endif
269
270 if ( verbose & VERB_TIME )
271 timer_report(ti);
272
273 bexit(0);
274 }
275
289 int img_fft_times(int ndim, int minsize, int maxsize, int opt)
290 {
291 long i, size, nprime;
292 long* prime = NULL;
293 Bimage* p = NULL;
294 double tvs, tvp, tvf;
295 double dtp, dte;
296 fft_plan plan;
297
298 if ( ndim < 1 ) ndim = 1;
299 if ( ndim > 3 ) ndim = 3;
300 if ( minsize < 1 ) minsize = 1;
301 if ( maxsize < minsize ) maxsize = minsize;
302
303 long n = maxsize - minsize + 1;
304 int_float* t = new int_float[n];
305
306 cout << endl << "Timing the execution of fast Fourier transforms:" << endl;
307 cout << "FFTW planner option: " << opt << endl;
308 cout << "Number of dimensions: " << ndim << endl;
309 cout << "Size range: " << minsize << " - " << maxsize << endl << endl;
310 cout << "Size\tPtime\tEtime\tPrime factors" << endl;
311 for ( size = minsize; size <= maxsize; size++ ) {
312 switch ( ndim ) {
313 case 1:
314 p = new Bimage(Float, TComplex, size, 1, 1, 1);
315 break;
316 case 2:
317 p = new Bimage(Float, TComplex, size, size, 1, 1);
318 break;
319 case 3:
320 p = new Bimage(Float, TComplex, size, size, size, 1);
321 break;
322 }
323 prime = prime_factors(size, nprime);
324 tvs = getwalltime();
325 plan = p->fft_setup(FFTW_FORWARD, opt);
326 tvp = getwalltime();
327 if ( p->fft(plan, 0) )
328 return error_show("img_fft_times", __FILE__, __LINE__);
329 tvf = getwalltime();
330 fft_destroy_plan(plan);
331 dtp = tvp - tvs;
332 dte = tvf - tvp;
333 cout << size << tab << fixed << setprecision(5) << dtp << tab << dte;
334 for ( i=0; i<nprime; i++ ) cout << tab << prime[i];
335 cout << endl;
336 delete[] prime;
337 delete p;
338 t[size-minsize].i = size;
339 t[size-minsize].f = dte;
340 }
341 cout << endl;
342
343 qsort((void *) t, n, sizeof(int_float), QsortSmallToLargeIntFloat);
344
345 cout << "Ordered times:" << endl;
346 cout << "Size\tEtime" << endl;
347 for ( i=0, size=minsize; i<n; i++, size++ ) cout << t[i].i << tab << t[i].f << endl;
348 cout << endl;
349
350 delete[] t;
351
352 return 0;
353 }
Bimage::change_type
void change_type(char letter)
Get the data type indicated by a single letter code.
Definition: Bimage.cpp:1743
Boption::unit_cell
UnitCell unit_cell()
Definition: options.h:291
M_PI_2
#define M_PI_2
Definition: utilities.h:62
UnitCell
Definition: UnitCell.h:22
Bstring::real
double real()
Definition: Bstring.cpp:545
qsort_functions.h
Header file for utility functions for use in qsort calls.
DataType
DataType
Base data type specifier.
Definition: utilities.h:146
Bimage::sampling
Vector3< double > sampling()
Definition: Bimage.h:505
Bimage::fft
int fft()
Definition: Bimage.h:822
Boption::sampling
Vector3< double > sampling()
Definition: options.h:202
Bimage::powerspectrum_tiled
Bimage * powerspectrum_tiled(long img_num, Vector3< long > tile_size, int flags=0)
Prepares a tiled power spectrum from an image for determining CTF parameters.
Definition: Bimage_powerspectrum.cpp:97
Boption::value
Bstring value
Definition: options.h:30
VERB_DEBUG
#define VERB_DEBUG
Definition: utilities.h:112
Bimage::power_spectrum
int power_spectrum(int flags=0)
Calculates a power spectrum.
Definition: Bimage_powerspectrum.cpp:48
Boption::tag
Bstring tag
Definition: options.h:29
system_processors
size_t system_processors()
Returns the number of processors.
Definition: utilities.cpp:186
Bimage::compound_type
CompoundType compound_type()
Definition: Bimage.h:351
prime_factors
long * prime_factors(long number, long &n)
Finds all the prime factor for the input number.
Definition: math_util.cpp:152
Bimage::center_wrap
int center_wrap()
Centers an image with wrapping.
Definition: Bimage_transform.cpp:713
getwalltime
double getwalltime()
Returns the current time.
Definition: timer.cpp:23
use
const char * use[]
Definition: bfft.cpp:22
TComplex
Definition: Bimage.h:59
Bimage::zero_fourier_origin
void zero_fourier_origin()
Zeroes the first voxel in each image.
Definition: Bimage.cpp:2011
get_option_list
Boption * get_option_list(const char *use[], int argc, char *argv[], int &optind)
Parses command line arguments based on a template.
Definition: options.cpp:40
read_img
Bimage * read_img(char *filename, int readdata, int img_select)
General driver function to read multiple image formats.
Definition: rwimg.cpp:76
get_cmd_line
string get_cmd_line(int argc, char **argv)
Returns the command line as a string.
Definition: utilities.cpp:36
Boption::filename
Bstring filename()
Definition: options.h:85
verbose
int verbose
timer.h
Utilities for timing functions.
QsortSmallToLargeIntFloat
int QsortSmallToLargeIntFloat(const void *x, const void *y)
Utility function for sorting int float pairs in qsort.
Definition: qsort_functions.cpp:73
Float
Definition: utilities.h:157
int_float::f
float f
Definition: qsort_functions.h:12
Bimage
General image parameter class.
Definition: Bimage.h:163
Boption::size
Vector3< long > size()
Definition: options.h:163
M_PI
#define M_PI
Definition: utilities.h:59
img_fft_times
int img_fft_times(int ndim, int minsize, int maxsize, int opt)
Tests the execution times for a series of image sizes.
Definition: bfft.cpp:289
main
int main(int argc, char **argv)
Definition: bfft.cpp:60
Bimage::phase_shift_to_center
int phase_shift_to_center()
Phase shifts a set of reflections to the nominal center of the image origin.
Definition: Bimage_complex.cpp:382
Boption
A class for a command line input option.
Definition: options.h:26
Bimage::label
void label(string s)
Definition: Bimage.h:235
VERB_TIME
#define VERB_TIME
Definition: utilities.h:110
fft_plan
fftwf_plan fft_plan
Definition: fft.h:16
Standard
Definition: Bimage.h:41
Bimage::unit_cell
void unit_cell(UnitCell uc)
Definition: Bimage.h:601
write_img
int write_img(const char *filename, Bimage *p, int compression)
General driver function to write multiple image formats.
Definition: rwimg.cpp:280
int_float
Definition: qsort_functions.h:12
Bimage::phase_colour_wheel
int phase_colour_wheel()
Generates a phase color wheel.
Definition: Bimage_color.cpp:562
Bimage::space_group
long space_group()
Definition: Bimage.h:591
fft_direction
int fft_direction
Definition: fft.h:15
TRGB
Definition: Bimage.h:62
Boption::datatype
DataType datatype()
Definition: options.h:93
Boption::next
Boption * next
Definition: options.h:28
UCharacter
Definition: utilities.h:149
Bstring
Definition: Bstring.h:29
Bimage::fft_setup
fft_plan fft_setup(fft_direction dir, int opt=0)
Sets up a plan for fast Fourier transforms.
Definition: Bimage_fft.cpp:29
Vector3< long >
fft_destroy_plan
int fft_destroy_plan(fft_plan plan)
Deallocates a plan for fast Fourier transforms.
Definition: fft.cpp:87
Boption::origin
Vector3< double > origin()
Definition: options.h:180
timer_report
double timer_report(double lasttime)
Reports the time elapsed since a given time.
Definition: timer.cpp:67
option_kill
int option_kill(Boption *option)
Deallocates a linked list of option structures.
Definition: options.cpp:109
Bstring::integer
long integer()
Definition: Bstring.cpp:539
Unknown_Type
Definition: utilities.h:147
rwimg.h
Header file for 2D and 3D image I/O.
int_float::i
int i
Definition: qsort_functions.h:12
options.h
Header file for option handlers.
UnitCell::check
bool check()
Definition: UnitCell.h:34
NoTransform
Definition: Bimage.h:40
Bimage::change_transform_size
Vector3< double > change_transform_size(Vector3< long > nusize)
Resizes a "standard" transform.
Definition: Bimage_fft.cpp:239
Vector3::volume
double volume()
Definition: Vector3.h:235
Bimage::data_type
DataType data_type()
Definition: Bimage.h:349
Bimage::intensities_phase_colored
Bimage * intensities_phase_colored(double scale)
Generates a power spectrum with phases colored according to a color wheel.
Definition: Bimage_complex.cpp:1358
Boption::values
long values(T1 &v1, T2 &v2)
Definition: options.h:115
bexit
int bexit(int value)
Exit function for cleanup and debugging.
Definition: utilities.cpp:273
TWOPI
#define TWOPI
Definition: utilities.h:68
timer_start
double timer_start()
Starts timer and prints the time.
Definition: timer.cpp:52
Bimage::fourier_type
FourierType fourier_type()
Definition: Bimage.h:367
error_show
int error_show(string message, string file, int line)
Displays the error with file and line reference.
Definition: utilities.cpp:755
tab
ostream & tab(ostream &out)
Definition: utilities.cpp:21
Bimage::origin
void origin(double ox, double oy, double oz)
Definition: Bimage.h:563