Xmipp  v3.23.11-Nereus
volume_halves_restoration_gpu.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  *
3  * Authors: Martin Horacek (horacek1martin@gmail.com)
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  ***************************************************************************/
26 
27 template< typename T >
29  readFilenames();
30  readDenoisingParams();
31  readDeconvolutionParams();
32  readFilterBankParams();
33  readDifferenceParams();
34  readMaskParams();
35 }
36 
37 template< typename T >
39  fnV1 = getParam("--i1");
40  fnV2 = getParam("--i2");
41  fnRoot = getParam("--oroot");
42 }
43 
44 template< typename T >
46  const int iters = getIntParam("--denoising");
47  if (iters < 0) {
48  REPORT_ERROR(ERR_ARG_BADCMDLINE, "`denoising N` has to be non-negative integer");
49  }
50 
51  builder.setDenoising(iters);
52 }
53 
54 template< typename T >
56  const int iters = getIntParam("--deconvolution");
57  const T sigma = getDoubleParam("--deconvolution", 1);
58  const T lambda = getDoubleParam("--deconvolution", 2);
59  if (iters < 0) {
60  REPORT_ERROR(ERR_ARG_BADCMDLINE, "`deconvolution N` has to be non-negative integer");
61  }
62 
63  builder.setDeconvolution(iters, sigma, lambda);
64 }
65 
66 template< typename T >
68  const T bankStep = getDoubleParam("--filterBank", 0);
69  const T bankOverlap = getDoubleParam("--filterBank", 1);
70  const int weightFun = getIntParam("--filterBank", 2);
71  const T weightPower = getDoubleParam("--filterBank", 3);
72  if (bankStep < 0 || bankStep > 0.5001) {
73  REPORT_ERROR(ERR_ARG_BADCMDLINE, "`filterBank step` parameter has to be in interval [0, 0.5].");
74  }
75  if (bankOverlap < 0 || bankOverlap > 1.001) {
76  REPORT_ERROR(ERR_ARG_BADCMDLINE, "`filterBank overlap` parameter has to be in interval [0, 1]");
77  }
78 
79  if (weightFun < 0 || weightFun > 3) {
80  REPORT_ERROR(ERR_ARG_BADCMDLINE, "`filterBank weightFun` parameter has to be 0, 1 or 2");
81  }
82 
83  builder.setFilterBank(bankStep, bankOverlap, weightFun, weightPower);
84 }
85 
86 template< typename T >
88  const int iters = getIntParam("--difference");
89  const T Kdiff = getDoubleParam("--difference", 1);
90  if (iters < 0) {
91  REPORT_ERROR(ERR_ARG_BADCMDLINE, "`difference N` has to be non-negative integer");
92  }
93 
94  builder.setDifference(iters, Kdiff);
95 }
96 
97 template< typename T >
99  if (checkParam("--mask")) {
100  mask.fn_mask = getParam("--mask");
101  mask.mask_type = "binary_file";
102  mask.type = READ_BINARY_MASK;
103 
104  if (checkParam("--center")) {
105  mask.x0 = getDoubleParam("--center", 0);
106  mask.y0 = getDoubleParam("--center", 1);
107  mask.z0 = getDoubleParam("--center", 2);
108  }
109  }
110 }
111 
112 template< typename T >
114  if (!verbose)
115  return;
116  std::cout
117  << "Input/Ouput filenames:" << std::endl
118  << " Volume1: " << fnV1 << std::endl
119  << " Volume2: " << fnV2 << std::endl
120  << " Rootname: " << fnRoot << std::endl
121  ;
122  std::cout << restorator;
123 
124  mask.show();
125 }
126 
127 template< typename T >
129  addUsageLine("Given two halves of a volume (and an optional mask), produce a better estimate of the volume underneath");
130  addParamsLine(" --i1 <volume1> : First half");
131  addParamsLine(" --i2 <volume2> : Second half");
132  addParamsLine(" [--oroot <root=\"volumeRestored\">] : Output rootname");
133  addParamsLine(" [--denoising <N=0>] : Number of iterations of denoising in real space");
134  addParamsLine(" [--deconvolution <N=0> <sigma0=0.2> <lambda=0.001>] : Number of iterations of deconvolution in Fourier space, initial sigma and lambda");
135  addParamsLine(" [--filterBank <step=0> <overlap=0.5> <weightFun=1> <weightPower=3>] : Frequency step for the filter bank (typically, 0.01; between 0 and 0.5)");
136  addParamsLine(" : filter overlap is between 0 (no overlap) and 1 (full overlap)");
137  addParamsLine(" : Weight function (0=mean, 1=min, 2=mean*diff");
138  addParamsLine(" [--difference <N=0> <K=1.5>] : Number of iterations of difference evaluation in real space");
139  addParamsLine(" [--mask <binary_file>] : Read from file and cast to binary");
140  addParamsLine(" [--center <x0=0> <y0=0> <z0=0>] : Mask center");
141 }
142 
143 template< typename T >
145  builder.setVerbosity(verbose);
146  auto restorator = builder.build();
147 
148  show(restorator);
149 
150  readData();
151  restorator.apply(V1(), V2(), maskData);
152 
153  saveResults(restorator);
154 }
155 
156 template< typename T >
158  V1.read(fnV1);
159  V2.read(fnV2);
160  V1().setXmippOrigin();
161  V2().setXmippOrigin();
162 
163  checkInputDimensions();
164 
165  if (!mask.fn_mask.isEmpty()) {
166  mask.generate_mask();
167  maskData = mask.get_binary_mask().data;
168  }
169 }
170 
171 template< typename T >
173  if (XSIZE(V1()) != XSIZE(V2()) || YSIZE(V1()) != YSIZE(V2())
174  || ZSIZE(V1()) != ZSIZE(V2())) {
175  REPORT_ERROR(ERR_MATRIX_DIM, "Input volumes have different dimensions");
176  }
177  if (maskData) {
178  auto& maskArray = mask.get_binary_mask();
179  if (XSIZE(V1()) != XSIZE(maskArray) || YSIZE(V1()) != YSIZE(maskArray)
180  || ZSIZE(V1()) != ZSIZE(maskArray)) {
181  REPORT_ERROR(ERR_MATRIX_DIM, "Mask and input volumes have different dimensions");
182  }
183  }
184 }
185 
186 template< typename T >
188  V1() = restorator.getReconstructedVolume1();
189  saveImage(V1, "_restored1.vol");
190  V1() = restorator.getReconstructedVolume2();
191  saveImage(V1, "_restored2.vol");
192  V1() = restorator.getFilterBankVolume();
193  saveImage(V1, "_filterBank.vol");
194  V1() = restorator.getDeconvolvedS();
195  saveImage(V1, "_deconvolved.vol");
196  V1() = restorator.getConvolvedS();
197  saveImage(V1, "_convolved.vol");
198  V1() = restorator.getAverageDifference();
199  saveImage(V1, "_avgDiff.vol");
200 }
201 
202 template< typename T >
203 void ProgVolumeHalvesRestorationGpu<T>::saveImage(Image<T>& image, std::string&& filename) {
204  if (XSIZE(image()) != 0) {
205  image.write(fnRoot + filename);
206  }
207 }
208 
Errors on command line parameters.
Definition: xmipp_error.h:112
#define YSIZE(v)
virtual void read(int argc, const char **argv, bool reportErrors=true)
#define REPORT_ERROR(nerr, ErrormMsg)
Definition: xmipp_error.h:211
virtual void show() const
const MultidimArray< T > & getReconstructedVolume2() const
const MultidimArray< T > & getConvolvedS() const
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)
const MultidimArray< T > & getReconstructedVolume1() const
const MultidimArray< T > & getDeconvolvedS() const
const MultidimArray< T > & getAverageDifference() const
const MultidimArray< T > & getFilterBankVolume() const
double * lambda
#define READ_BINARY_MASK
Definition: mask.h:374
Problem with matrix dimensions.
Definition: xmipp_error.h:150
#define XSIZE(v)
#define ZSIZE(v)
void apply(const MultidimArray< T > &volume1, const MultidimArray< T > &volume2, const int *mask)