Xmipp  v3.23.11-Nereus
reconstruct_art.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  *
3  * Authors: Carlos Oscar S. Sorzano (coss@cnb.csic.es)
4  *
5  * Unidad de Bioinformatica of Centro Nacional de Biotecnologia , CSIC
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
20  * 02111-1307 USA
21  *
22  * All comments concerning this program package may be sent to the
23  * e-mail address 'xmipp@cnb.csic.es'
24  ***************************************************************************/
25 
26 #include <fstream>
27 #include "reconstruct_art.h"
28 #include "art_crystal.h"
29 #include <sys/time.h>
30 
31 
33 {
34  isMpi = false;
35  artRecons = nullptr;
36 }
38 {
39  delete artRecons;
40 }
41 
42 void ProgReconsART::setIO(const FileName &fn_in, const FileName &fn_out)
43 {}
44 
46 {
47  addUsageLine("Generate 3D reconstructions from projections using the ART algorithm (even with SIRT).");
48  addUsageLine("+A history file with the information of the reconstruction process is created. You can ");
49  addUsageLine("+also give symmetry elements to specify different points of view reusing the projections ");
50  addUsageLine("+you have. The reconstruction is performed using some basis (blobs or voxels) in ");
51  addUsageLine("+different grids (BCC (by default), FCC or CC).");
52 
54 
55  addParamsLine(" == Special Parameters for crystals == ");
57 
58  addExampleLine("+++* Basis volume definition: The basis volume is defined internally such that it should cover ",false);
59  addExampleLine("+++the space cube corresponding to =(-Xdim/2,-Xdim/2,-Xdim/2)= to =(X dim /2,Xdim/2,Xdim/2)= where Xdim ",false);
60  addExampleLine("+++is the X dimension of the projections.", false);
61  addExampleLine("+++* Voxel volume definition: The final reconstructed volume is defined in such a way that it ", false);
62  addExampleLine("+++covers the whole basis volume. For these two definitions and the fact that the basis has got ", false);
63  addExampleLine("+++some radius (normally 2) the final reconstructions are a little wider than the original ",false);
64  addExampleLine("+++projections (usually =2*radius+1=)",false);
65  addExampleLine("+++* Crystal reconstructions: Crystal projections must be the deformed projections of the ",false);
66  addExampleLine("+++deformed volume. I mean, the lattice vectors of the volume to reconstruct need not to lie ",false);
67  addExampleLine("+++on a grid point inside the BCC, FCC or CC grid. If we ant to force that they lie on a grid ",false);
68  addExampleLine("+++point we have to deform the volume. When he have this deformed volume and we project it, the ",false);
69  addExampleLine("+++two 3D lattice vectors project to two different 2D lattice vectors, that in principle there ",false);
70  addExampleLine("+++is no relationship between them. We must force this two 2D lattice vectors to align with ",false);
71  addExampleLine("+++=a=(Xdim,0)= and =b=(0,Ydim)= by deforming the projection. This is the second deformation. ",false);
72  addExampleLine("+++This complex projections are either generated from phantoms using the program Project or ",false);
73  addExampleLine("+++from lists of Fourier spots coming out from MRC as APH files by applying the program ",false);
74  addExampleLine("+++Spots2RealSpace2D.",false);
75  addExampleLine("+++* Surface constraints: These constraints force the volume to be 0 at known places. ",false);
76  addExampleLine("+++This has got the beneficial consequence that mass tends to be compacted where it should be.",false);
77  addExampleLine("+++* Grids: This program defines three grids for working with data. The typical simple cubic grid, ",false);
78  addExampleLine("+++the face-centered grid (fcc) and the body-centered cubic grid (bcc). We define these grids as follows: ",false);
79  addExampleLine("+++| Simple Cubic Grid | %ATTACHURL%/image004.gif |",false);
80  addExampleLine("+++| Face-centered cubic grid | %ATTACHURL%/image008.gif |",false);
81  addExampleLine("+++| Body-centered cubic grid | %ATTACHURL%/image006.gif |",false);
82  addExampleLine("+++where %ATTACHURL%/image010.gif is the set of integers and %ATTACHURL%/image002.gif is a positive ",false);
83  addExampleLine("+++real number called sampling distance. See =Gabor H., Geometry of Digital Spaces, Birkhauser, 1998, Massachussets=",false);
84  addExampleLine("+++Spots2RealSpace2D. %BR%",false);
85  addExampleLine("+++In the case of the simple cubic grid the voxels are cubes of volume %ATTACHURL%/image013.gif ³",false);
86  addExampleLine("+++. The voxels associated to the bcc grid are truncated octahedra, polyhedra of 8 hexagonal ",false);
87  addExampleLine("+++faces and 6 square faces, of volume 4 %ATTACHURL%/image013.gif ³ .Finally, the voxels associated ",false);
88  addExampleLine("+++to the fcc grid are rhombic dodacehedra, polyhedra of 12 identical rhombic faces, of volume 2 ",false);
89  addExampleLine("+++%ATTACHURL%/image013.gif ³. However, in practice the above definitions are implemented using a ",false);
90  addExampleLine("+++combination of simple cubic grids. Clearly, a simple cubic grid defines the complex simple cubic ",false);
91  addExampleLine("+++grid and thus the relative size used in the implementation is exactly the %ATTACHURL%/image013.gif ³. ",false);
92  addExampleLine("+++For defining the bcc grid two simple cubic grids are used, it can be seen from the definition for ",false);
93  addExampleLine("+++the bcc above that the valid positions for even values of =z=, =x= and =y= have to be also even, ",false);
94  addExampleLine("+++in the case of odd values of =z= , =x= and =y= have to be odd. Consequently, the relative size used ",false);
95  addExampleLine("+++in the BCC grid is equivalent to 2 %ATTACHURL%/image013.gif above. For defining the fcc grid four ",false);
96  addExampleLine("+++simple cubic grids are used, it can be seen from the definition for the fcc above that the valid ",false);
97  addExampleLine("+++positions for even values of =z=, the sum of =x= and =y= has to be even; in the case of odd values ",false);
98  addExampleLine("+++of =z= the sum of =x= and =y= has to be odd. Consequently, the relative size used in the FCC grid ",false);
99  addExampleLine("+++is equivalent to 2 %ATTACHURL%/image013.gif above. %BR%",false);
100  addExampleLine(" ",false); // For style twiki reasons
101  addExampleLine("Basic reconstructing commands:",false);
102  addExampleLine("reconstruct_art -i projections.sel -o artrec");
103  addExampleLine("Create the equivalent noisy reconstruction:",false);
104  addExampleLine("reconstruct_art -i projections.sel -o artrec --noisy_reconstruction");
105  // addExampleLine("Reconstruct using SIRT parallelization algorithm for five iterations:",false);
106  // addExampleLine("reconstruct_art -i projections.sel -o artrec -n 5 --parallel_mode SIRT");
107  addExampleLine("Save the basis information at each iteration:",false);
108  addExampleLine("reconstruct_art -i projections.sel -o artrec -n 3 --save_basis");
109 }
110 
112 {
113 
114 
115  if (checkParam("--crystal"))
117  else
119 
120  artRecons->readParams(this);
121 
122  if (artRecons->artPrm.threads > 1 && isMpi)
123  REPORT_ERROR(ERR_ARG_BADCMDLINE, "Threads not compatible in mpi version.");
125  REPORT_ERROR(ERR_ARG_BADCMDLINE, "If --parallel_mode is passed, then mpi version must be used.");
126 
127 }
128 
130 {
131  if (verbose > 0)
132  {
133  std::cout << " =====================================================================" << std::endl;
134  std::cout << " ART reconstruction method " << std::endl;
135  std::cout << " =====================================================================" << std::endl;
136  std::cout << " Projections file : " << artRecons->artPrm.fn_sel << std::endl;
137  std::cout << " Output rootname : " << artRecons->artPrm.fn_root << std::endl;
138  std::cout << " Iterations : " << artRecons->artPrm.no_it << std::endl;
139  std::cout << " Lambda : " << artRecons->artPrm.lambda_list(0) << std::endl;
140  std::cout << std::endl;
141  std::cout << " More info in the history file: " << artRecons->artPrm.fn_root + ".hist" << std::endl;
142  std::cout << " ---------------------------------------------------------------------\n" << std::endl;
143  }
144 }
145 
147 {
149 
150  Image<double> vol_voxels;
151  GridVolume vol_basis;
152  // Configure time clock
153  time_config();
154 
155  struct timeval start_time, end_time;
156  long int init_usecs, process_usecs, finish_usecs;
157 
158  gettimeofday(&start_time, nullptr);
159 
160  show();
161  // Produce side information and initial volume
162  artRecons->preProcess(vol_basis);
163  if (verbose > 0)
164  {
165  std::cout << " ---------------------------------------------------------------------" << std::endl;
166  std::cout << " Projections : " << artRecons->artPrm.numIMG << std::endl;
167  std::cout << " ---------------------------------------------------------------------" << std::endl;
168  }
169  // Show parameters and initiate history
170  artRecons->initHistory(vol_basis);
171 
172  gettimeofday(&end_time, nullptr);
173 
174  init_usecs = (end_time.tv_sec-start_time.tv_sec)*1000000+(end_time.tv_usec-start_time.tv_usec);
175 
176  gettimeofday(&start_time,nullptr);
177 
178  // Iterations
179  artRecons->iterations(vol_basis);
180 
181 
182  gettimeofday(&end_time,nullptr);
183 
184  process_usecs = (end_time.tv_sec-start_time.tv_sec)*1000000+(end_time.tv_usec-start_time.tv_usec);
185 
186  gettimeofday(&start_time,nullptr);
187 
188  // Finish iterations
189  artRecons->postProcess(vol_basis);
190 
191  // Write final volume
192  int Xoutput_volume_size=(artPrm.Xoutput_volume_size==0) ?
193  artPrm.projXdim:artPrm.Xoutput_volume_size;
194  int Youtput_volume_size=(artPrm.Youtput_volume_size==0) ?
195  artPrm.projYdim:artPrm.Youtput_volume_size;
196  int Zoutput_volume_size=(artPrm.Zoutput_volume_size==0) ?
197  artPrm.projXdim:artPrm.Zoutput_volume_size;
198 
199  // int min_distance = ceil( ( 2 * artPrm.grid_relative_size ) / artPrm.basis.blob.radius) + 1;
200 
201  artPrm.basis.changeToVoxels(vol_basis, &(vol_voxels()),
202  Zoutput_volume_size, Youtput_volume_size, Xoutput_volume_size,artPrm.threads);
203 
204  vol_voxels.write(artPrm.fn_out);
205 
206  if (artPrm.tell&TELL_SAVE_BASIS)
207  vol_basis.write(artPrm.fn_root+".basis");
208  artPrm.fh_hist->close();
209 
210  gettimeofday(&end_time,nullptr);
211 
212  finish_usecs = (end_time.tv_sec-start_time.tv_sec)*1000000+(end_time.tv_usec-start_time.tv_usec);
213 
214  std::cout << "INIT_TIME: " << (double)init_usecs/(double)1000000 << std::endl;
215  std::cout << "PROCESS_TIME: " << (double)process_usecs/(double)1000000 << std::endl;
216  std::cout << "FINISH_TIME: " << (double)finish_usecs/(double)1000000 << std::endl;
217 
218 }
219 
Errors on command line parameters.
Definition: xmipp_error.h:112
int numIMG
Total number of images to process (taking symmetries into account)
Definition: basic_art.h:348
size_t projXdim
Projection X dimension.
Definition: basic_art.h:333
void setIO(const FileName &fn_in, const FileName &fn_out)
Functions of common reconstruction interface.
Basis basis
Basis function. By default, blobs.
Definition: basic_art.h:97
#define REPORT_ERROR(nerr, ErrormMsg)
Definition: xmipp_error.h:211
void initHistory(const GridVolume &vol_basis0)
void write(const FileName &name="", size_t select_img=ALL_IMAGES, bool isStack=false, int mode=WRITE_OVERWRITE, CastWriteMode castMode=CW_CAST, int _swapWrite=0)
#define TELL_SAVE_BASIS
Definition: basic_art.h:276
FileName fn_sel
Selection file with all images to process.
Definition: basic_art.h:208
std::ofstream * fh_hist
File handler for the history file.
Definition: basic_art.h:339
size_t projYdim
Projection Y dimension.
Definition: basic_art.h:336
void changeToVoxels(GridVolume &vol_basis, MultidimArray< double > *vol_voxels, int Zdim, int Ydim, int Xdim, int threads=1) const
Definition: basis.cpp:261
virtual void readParams(XmippProgram *program)
static void defineParams(XmippProgram *program, bool mpiMode=false)
void time_config()
virtual void postProcess(GridVolume &vol_basis)
ARTReconsBase * artRecons
virtual void preProcess(GridVolume &vol_basis0, int level=FULL, int rank=-1)
Produce Plain side information from the Class parameters.
void addExampleLine(const char *example, bool verbatim=true)
ARTParallelMode parallel_mode
Definition: basic_art.h:109
int verbose
Verbosity level.
FileName fn_root
Definition: basic_art.h:217
int no_it
Number of iterations.
Definition: basic_art.h:100
void write(const FileName &fn) const
Definition: grids.h:1196
void iterations(GridVolume &vol_basis, int rank=-1)
static void defineParams(XmippProgram *program, const char *prefix=nullptr, const char *comment=nullptr)
Definition: art_crystal.cpp:36
bool checkParam(const char *param)
BasicARTParameters artPrm
void addUsageLine(const char *line, bool verbatim=false)
int threads
Number of threads to use. Can not be different than 1 when using MPI.
Definition: basic_art.h:267
FileName fn_out
Name of the output volume, also used to set the root of rest output files.
Definition: basic_art.h:217
void addParamsLine(const String &line)
Matrix1D< double > lambda_list
Relaxation parameter.
Definition: basic_art.h:103