Bsoft  2.1.0
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->scale();
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 )
226  if ( p->fft(setdir) )
227  return error_show("bfft", __FILE__, __LINE__);
228  if ( phase_shift && p->fourier_type() == Standard )
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  if ( halfshift ) p->center_wrap();
260  write_img(argv[optind], p, 0);
261  }
262 
263  delete p;
264 
265 #ifdef HAVE_GCD
266  fftwf_cleanup_threads();
267 #endif
268 
269  if ( verbose & VERB_TIME )
270  timer_report(ti);
271 
272  bexit(0);
273 }
274 
288 int img_fft_times(int ndim, int minsize, int maxsize, int opt)
289 {
290  long i, size, nprime;
291  long* prime = NULL;
292  Bimage* p = NULL;
293  double tvs, tvp, tvf;
294  double dtp, dte;
295  fft_plan plan;
296 
297  if ( ndim < 1 ) ndim = 1;
298  if ( ndim > 3 ) ndim = 3;
299  if ( minsize < 1 ) minsize = 1;
300  if ( maxsize < minsize ) maxsize = minsize;
301 
302  long n = maxsize - minsize + 1;
303  int_float* t = new int_float[n];
304 
305  cout << endl << "Timing the execution of fast Fourier transforms:" << endl;
306  cout << "FFTW planner option: " << opt << endl;
307  cout << "Number of dimensions: " << ndim << endl;
308  cout << "Size range: " << minsize << " - " << maxsize << endl << endl;
309  cout << "Size\tPtime\tEtime\tPrime factors" << endl;
310  for ( size = minsize; size <= maxsize; size++ ) {
311  switch ( ndim ) {
312  case 1:
313  p = new Bimage(Float, TComplex, size, 1, 1, 1);
314  break;
315  case 2:
316  p = new Bimage(Float, TComplex, size, size, 1, 1);
317  break;
318  case 3:
319  p = new Bimage(Float, TComplex, size, size, size, 1);
320  break;
321  }
322  prime = prime_factors(size, nprime);
323  tvs = getwalltime();
324  plan = p->fft_setup(FFTW_FORWARD, opt);
325  tvp = getwalltime();
326  if ( p->fft(plan, 0) )
327  return error_show("img_fft_times", __FILE__, __LINE__);
328  tvf = getwalltime();
329  fft_destroy_plan(plan);
330  dtp = tvp - tvs;
331  dte = tvf - tvp;
332  cout << size << tab << fixed << setprecision(5) << dtp << tab << dte;
333  for ( i=0; i<nprime; i++ ) cout << tab << prime[i];
334  cout << endl;
335  delete[] prime;
336  delete p;
337  t[size-minsize].i = size;
338  t[size-minsize].f = dte;
339  }
340  cout << endl;
341 
342  qsort((void *) t, n, sizeof(int_float), QsortSmallToLargeIntFloat);
343 
344  cout << "Ordered times:" << endl;
345  cout << "Size\tEtime" << endl;
346  for ( i=0, size=minsize; i<n; i++, size++ ) cout << t[i].i << tab << t[i].f << endl;
347  cout << endl;
348 
349  delete[] t;
350 
351  return 0;
352 }
Boption
A class for a command line input option.
Definition: options.h:26
qsort_functions.h
Header file for utility functions for use in qsort calls.
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
Boption::unit_cell
UnitCell unit_cell()
Definition: options.h:279
main
int main(int argc, char **argv)
Definition: bfft.cpp:60
Boption::scale
Vector3< double > scale()
Definition: options.h:190
VERB_DEBUG
#define VERB_DEBUG
Definition: utilities.h:116
UCharacter
@ UCharacter
Definition: utilities.h:153
system_processors
size_t system_processors()
Returns the number of processors.
Definition: utilities.cpp:193
Bstring
Definition: Bstring.h:29
Bimage::zero_fourier_origin
void zero_fourier_origin()
Zeroes the first voxel in each image.
Definition: Bimage.cpp:2033
verbose
int verbose
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:288
QsortSmallToLargeIntFloat
int QsortSmallToLargeIntFloat(const void *x, const void *y)
Utility function for sorting int float pairs in qsort.
Definition: qsort_functions.cpp:73
Bstring::integer
long integer()
Definition: Bstring.cpp:547
Boption::datatype
DataType datatype()
Definition: options.h:93
Boption::next
Boption * next
Definition: options.h:28
Vector3< long >
Bimage::unit_cell
void unit_cell(UnitCell uc)
Definition: Bimage.h:608
bexit
int bexit(int value)
Exit function for cleanup and debugging.
Definition: utilities.cpp:280
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
error_show
int error_show(string message, string file, int line)
Displays the error with file and line reference.
Definition: utilities.cpp:719
TComplex
@ TComplex
Definition: Bimage.h:60
Bstring::real
double real()
Definition: Bstring.cpp:553
options.h
Header file for option handlers.
fft_direction
int fft_direction
Definition: fft.h:19
Boption::origin
Vector3< double > origin()
Definition: options.h:180
int_float::f
float f
Definition: qsort_functions.h:12
M_PI_2
#define M_PI_2
Definition: utilities.h:66
Bimage::phase_colour_wheel
int phase_colour_wheel()
Generates a phase color wheel.
Definition: Bimage_color.cpp:574
Standard
@ Standard
Definition: Bimage.h:42
int_float
Definition: qsort_functions.h:12
Boption::tag
Bstring tag
Definition: options.h:29
Boption::values
long values(T1 &v1, T2 &v2)
Definition: options.h:115
read_img
Bimage * read_img(char *filename, int readdata, int img_select)
General driver function to read multiple image formats.
Definition: rwimg.cpp:77
write_img
int write_img(const char *filename, Bimage *p, int compression)
General driver function to write multiple image formats.
Definition: rwimg.cpp:281
Bimage::change_transform_size
Vector3< double > change_transform_size(Vector3< long > nusize)
Resizes a "standard" transform.
Definition: Bimage_fft.cpp:239
fft_destroy_plan
int fft_destroy_plan(fft_plan plan)
Deallocates a plan for fast Fourier transforms.
Definition: fft.cpp:87
use
const char * use[]
Definition: bfft.cpp:22
Bimage::fft
int fft()
Definition: Bimage.h:830
Bimage::fourier_type
FourierType fourier_type()
Definition: Bimage.h:380
Boption::value
Bstring value
Definition: options.h:30
Bimage::power_spectrum
int power_spectrum(int flags=0)
Calculates a power spectrum.
Definition: Bimage_powerspectrum.cpp:48
TRGB
@ TRGB
Definition: Bimage.h:63
Bimage
General image parameter class.
Definition: Bimage.h:177
Bimage::compound_type
CompoundType compound_type()
Definition: Bimage.h:364
timer.h
Utilities for timing functions.
Unknown_Type
@ Unknown_Type
Definition: utilities.h:151
VERB_TIME
#define VERB_TIME
Definition: utilities.h:114
Bimage::center_wrap
int center_wrap()
Centers an image with wrapping.
Definition: Bimage_transform.cpp:708
timer_start
double timer_start()
Starts timer and prints the time.
Definition: timer.cpp:61
prime_factors
long * prime_factors(long number, long &n)
Finds all the prime factor for the input number.
Definition: math_util.cpp:152
Bimage::data_type
DataType data_type()
Definition: Bimage.h:362
M_PI
#define M_PI
Definition: utilities.h:63
UnitCell::check
bool check()
Definition: UnitCell.h:34
tab
ostream & tab(ostream &out)
Definition: utilities.cpp:22
Boption::size
Vector3< long > size()
Definition: options.h:163
timer_report
double timer_report(double lasttime)
Reports the time elapsed since a given time.
Definition: timer.cpp:76
Bimage::origin
void origin(double ox, double oy, double oz)
Definition: Bimage.h:570
Bimage::change_type
void change_type(char letter)
Get the data type indicated by a single letter code.
Definition: Bimage.cpp:1765
Vector3::volume
double volume()
Definition: Vector3.h:237
rwimg.h
Header file for 2D and 3D image I/O.
fft_plan
fftwf_plan fft_plan
Definition: fft.h:20
Bimage::space_group
long space_group()
Definition: Bimage.h:598
Boption::filename
Bstring filename()
Definition: options.h:85
option_kill
int option_kill(Boption *option)
Deallocates a linked list of option structures.
Definition: options.cpp:111
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:1363
Bimage::sampling
void sampling(long nn, Vector3< double > u)
Definition: Bimage.h:514
UnitCell
Definition: UnitCell.h:22
getwalltime
double getwalltime()
Returns the current time.
Definition: timer.cpp:26
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
DataType
DataType
Base data type specifier.
Definition: utilities.h:150
int_float::i
int i
Definition: qsort_functions.h:12
Float
@ Float
Definition: utilities.h:161
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
NoTransform
@ NoTransform
Definition: Bimage.h:41
TWOPI
#define TWOPI
Definition: utilities.h:72