Xmipp  v3.23.11-Nereus
resolution_fsc.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  *
3  * Authors: Sjors Scheres (scheres@cnb.csic.es)
4  * Roberto Marabini
5  *
6  * Unidad de Bioinformatica of Centro Nacional de Biotecnologia , CSIC
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
21  * 02111-1307 USA
22  *
23  * All comments concerning this program package may be sent to the
24  * e-mail address 'xmipp@cnb.csic.es'
25  ***************************************************************************/
26 
27 #include "resolution_fsc.h"
28 
29 
31 {
32  apply_geo = true;
33 
34  addUsageLine("Calculate the resolution of one or more volumes or images with respect to a single reference.");
35  addUsageLine("+ ");
36  addUsageLine("+Three methods are employed:");
37  addUsageLine("+ ");
38  addUsageLine("+* Differential Phase Residual (DPR)",true);
39  addUsageLine("+In this method the the resolution is defined as the spatial frequency at which the average phase");
40  addUsageLine("+discrepancy between the two transforms exceeds 45 degrees.");
41  addUsageLine("+* Fourier Ring Correlation (FRC)", true);
42  addUsageLine("+In this method the resolution is defined as the spatial frequency at which annular samplings of");
43  addUsageLine("+the two Fourier transforms register negligible cross-correlation.");
44  addUsageLine("+* Spectral Signal-to-Noise Ratio (SSNR)", true);
45  addUsageLine("+This method is based on the measurement of the signal-to-noise ratio as a function of spatial");
46  addUsageLine("+frequency. The SSNR is determined by comparing the Fourier transform of individual images with ");
47  addUsageLine("+that of the global average image. (this option is only available for 2D images not for volumes,");
48  addUsageLine("+ see the program ssnr if you are interested in computing the signal to noise ratio in 3D).");
49  addUsageLine("+ ");
50  addUsageLine("+To calculate \"the resolution of a reconstruction\", use --set_of_images, where the program divides the");
51  addUsageLine("+corresponding set of projection images into two subsets and reconstructs two volumes from these subsets.");
52  addUsageLine("+This program may then be used to calculate the DPR and FRC between these two volumes. The resulting plots");
53  addUsageLine("+are commonly used to assess the high-resolution limit of the initial reconstruction (see Frank's book for more details).");
54  addUsageLine(" ");
55  addUsageLine("The program writes out filename.frc files, for each input volume or image, or selfilename.frc, the");
56  addUsageLine("set_of_images mode. These ACSII files contain the DPR, FRC and SSNR as a function of resolution (in 1/Angstrom).");
57  addUsageLine(" The .frc files also contain a column for the FRC expected for pure noise.");
58 
59  addParamsLine(" -i <input_file> : either an image/volume or a selection file");
60  addParamsLine(" requires --ref;");
61  addParamsLine("or --set_of_images <selfile> : selfile containing a set of 2D-images");
62  addParamsLine(" [--oroot <root_file=\"\">] : Root of the output metadata. If not set, input file rootname is taken.");
63  addParamsLine(" [-o <output_file=\"\">] : Output file name.");
64 
65  addParamsLine(" [--ref <input_file>] : filename for reference image/volume");
66  addParamsLine(" [--sampling_rate <Ts=1>] : Pixel size (Angstrom)");
67  addParamsLine(" alias -s;");
68  addParamsLine(" [--dont_apply_geo] : for 2D-images: do not apply transformation stored in the header");
69  addParamsLine(" [--do_dpr] : compute dpr, by default only frc is computed");
70  addParamsLine(" [--max_sam <max_sr=-1>] : set fsc to 0 for frequencies above this one (Angstrom), -1 -> all fequencies");
71  addParamsLine(" : --max_sam = 10A -> frequencies higher than 0.1 A^-1 =0");
72  //for R-factor
73  addParamsLine(" [--do_rfactor] : compute R-factor for input volumes");
74  addParamsLine(" [--min_sam <min_sr=-1>] : minimum frequency may use for calculating R-factor (Angstrom)");
75  addParamsLine(" : --min_sam = 10A -> frequencies smaller than 0.1 A^-1 =0");
76 
77  addExampleLine("Resolution of subset2.vol volume with respect to subset1.vol reference volume using 5.6 pixel size (in Angstrom):", false);
78  addExampleLine("xmipp_resolution_fsc --ref subset1.vol -i subset2.vol --sampling_rate 5.6 ");
79  addExampleLine("Resolution of a set of images using 5.6 pixel size (in Angstrom):", false);
80  addExampleLine("xmipp_resolution_fsc --set_of_images selfile.sel --sampling_rate 5.6");
81 }
82 
84 {
85  sam = getDoubleParam("--sampling_rate");
86 
87  apply_geo = !checkParam("--dont_apply_geo");
88 
89  max_sam = getDoubleParam("--max_sam");
90  do_dpr = checkParam("--do_dpr");
91  do_set_of_images = checkParam("--set_of_images");
92  //for calculating r-factor
93  min_sam = getDoubleParam("--min_sam");
94  do_rfactor = checkParam("--do_rfactor");
95 
97  {
98  fn_sel = getParam("--set_of_images");
99  if (checkParam("-i") || checkParam("--ref"))
100  REPORT_ERROR(ERR_ARG_INCORRECT, "--set_of_images should not be provided with -i or --ref");
101  }
102  else
103  {
104  fn_ref = getParam("--ref");
105  fn_img = getParam("-i");
106  }
107 
108  do_o = checkParam("-o");
109  if (do_o)
110  fn_out = getParam("-o");
111  else
112  fn_root = getParam("--oroot");
113 }
114 
116  const MultidimArray<double> &freq,
117  const MultidimArray<double> &frc,
118  const MultidimArray<double> &frc_noise,
119  const MultidimArray<double> &dpr,
120  const MultidimArray<double> &error_l2,
121  double max_sam, bool do_dpr, double rFactor)
122 {
123  MetaDataVec MD;
124 
125  FileName fn_frc;
126  if (do_o)
127  fn_frc = fn_out;
128  else
129  fn_frc = fnRoot + ".frc";
130  size_t id;
132  {
133  if (i>0)
134  {
135  id=MD.addObject();
136  if(max_sam >0 && ((1./dAi(freq, i))<max_sam) )
137  {
138  if(do_dpr)
139  dAi(dpr, i)=0.;
140  dAi(frc, i)=0.;
141  }
142  if(min_sam >0 && ((1./dAi(freq, i))>min_sam) )
143  {
144  if(do_dpr)
145  dAi(dpr, i)=0.;
146  dAi(frc, i)=0.;
147  }
148  MD.setValue(MDL_RESOLUTION_FREQ,dAi(freq, i),id);
149  MD.setValue(MDL_RESOLUTION_FRC,dAi(frc, i),id);
150  if(do_dpr)
151  MD.setValue(MDL_RESOLUTION_DPR,dAi(dpr, i),id);
152  MD.setValue(MDL_RESOLUTION_ERRORL2,dAi(error_l2, i),id);
153  MD.setValue(MDL_RESOLUTION_FRCRANDOMNOISE,dAi(frc_noise, i),id);
154  MD.setValue(MDL_RESOLUTION_FREQREAL,1./dAi(freq, i),id);
155  }
156  }
157  MD.write(fn_frc);
158  MetaDataVec MD2;
159  MD2.setColumnFormat(false);
160  id=MD2.addObject();
161  MD2.setValue(MDL_RESOLUTION_RFACTOR,rFactor,id);
162  fn_frc.compose("rfactor", fn_frc);
163  MD2.write(fn_frc, MD_APPEND);
164 }
165 
167 {
168  Image<double> refI,img;
169  //if (apply_geo)
170  // refI.readApplyGeo(fn_ref);
171  //else
172  refI.read(fn_ref);
173  refI().setXmippOrigin();
174  //if (apply_geo)
175  // img.readApplyGeo(fn_img);
176  //else
177  img.read(fn_img);
178  img().setXmippOrigin();
179 
180  MultidimArray<double> freq, frc, dpr, frc_noise, error_l2;
181  double rFactor=-1.;
182  double min_samp = sam/min_sam;
183  if (min_sam <0)
184  min_samp = 0.;
185  if (max_sam <0)
186  max_sam = 2 * sam;
187 
188  frc_dpr(refI(), img(), sam, freq, frc, frc_noise, dpr, error_l2, do_dpr, do_rfactor, min_samp, sam/max_sam, &rFactor);
189 
190  writeFiles((fn_root.empty())?img.name():fn_root, freq, frc, frc_noise, dpr, error_l2, max_sam, do_dpr, rFactor);
191  return true;
192 }
193 
195 {
196  MetaDataDb MD(fn_sel), MDout;
197  MultidimArray<double> freq, frc, dpr, frc_noise, ssnr, error_l2;
198  getFourierStatistics(MD, sam, MDout, do_dpr, max_sam);
199  FileName fnRoot=(fn_root.empty())?fn_sel:fn_root;
200  MDout.write(fnRoot+".frc");
201  return true;
202 }
203 
205 {
206  if (!do_set_of_images)
207  process_img();
208  else
209  process_sel();
210 }
#define dAi(v, i)
double getDoubleParam(const char *param, int arg=0)
void writeFiles(const FileName &fnRoot, const MultidimArray< double > &freq, const MultidimArray< double > &frc, const MultidimArray< double > &frc_noise, const MultidimArray< double > &dpr, const MultidimArray< double > &error_l2, double max_sam, bool do_dpr, double rFactor)
#define REPORT_ERROR(nerr, ErrormMsg)
Definition: xmipp_error.h:211
void compose(const String &str, const size_t no, const String &ext="")
void frc_dpr(MultidimArray< double > &m1, MultidimArray< double > &m2, double sampling_rate, MultidimArray< double > &freq, MultidimArray< double > &frc, MultidimArray< double > &frc_noise, MultidimArray< double > &dpr, MultidimArray< double > &error_l2, bool dodpr, bool doRfactor, double minFreq, double maxFreq, double *rFactor)
Definition: xmipp_fftw.cpp:491
const FileName & name() const
void write(const FileName &outFile, WriteModeMetaData mode=MD_OVERWRITE) const
void getFourierStatistics(MetaDataDb &MDin, double sam, MetaData &MDout, bool do_dpr, double max_sam, MDLabel image_label)
#define i
differential phase residual (double)
const char * getParam(const char *param, int arg=0)
bool setValue(const MDObject &mdValueIn, size_t id)
size_t addObject() override
Frequency in A (double)
Incorrect argument received.
Definition: xmipp_error.h:113
void addExampleLine(const char *example, bool verbatim=true)
void write(const FileName &outFile, WriteModeMetaData mode=MD_OVERWRITE) const override
Frequency in 1/A (double)
virtual void setColumnFormat(bool column)
#define FOR_ALL_ELEMENTS_IN_ARRAY1D(v)
Error in l2 (double)
Fourier shell correlation noise (double)
bool checkParam(const char *param)
int read(const FileName &name, DataMode datamode=DATA, size_t select_img=ALL_IMAGES, bool mapData=false, int mode=WRITE_READONLY)
void addUsageLine(const char *line, bool verbatim=false)
void addParamsLine(const String &line)
Fourier shell correlation (double)