Xmipp  v3.23.11-Nereus
Classes | Public Member Functions | Public Attributes | List of all members
ProgSubtractProjection Class Reference

#include <subtract_projection.h>

Inheritance diagram for ProgSubtractProjection:
Inheritance graph
[legend]
Collaboration diagram for ProgSubtractProjection:
Collaboration graph
[legend]

Classes

struct  Angles
 

Public Member Functions

void readParticle (const MDRow &rowIn)
 Read and write methods. More...
 
void writeParticle (MDRow &rowOut, FileName, Image< double > &, double, double, double)
 
void createMask (const FileName &, Image< double > &, Image< double > &)
 Processing methods. More...
 
Image< double > binarizeMask (Projection &) const
 
Image< double > invertMask (const Image< double > &)
 
Image< double > applyCTF (const MDRow &, Projection &)
 
void processParticle (const MDRow &rowIn, int, FourierTransformer &, FourierTransformer &)
 
MultidimArray< std::complex< double > > computeEstimationImage (const MultidimArray< double > &, const MultidimArray< double > &, FourierTransformer &)
 
double evaluateFitting (const MultidimArray< std::complex< double > > &, const MultidimArray< std::complex< double > > &) const
 
Matrix1D< double > checkBestModel (MultidimArray< std::complex< double > > &, const MultidimArray< std::complex< double > > &, const MultidimArray< std::complex< double > > &, const MultidimArray< std::complex< double > > &) const
 
 ProgSubtractProjection ()
 Empty constructor. More...
 
 ~ProgSubtractProjection ()
 Destructor. More...
 
void readParams () override
 Read argument from command line. More...
 
void show () const override
 Show. More...
 
void defineParams () override
 Define parameters. More...
 
void preProcess () override
 
void processImage (const FileName &fnImg, const FileName &fnImgOut, const MDRow &rowIn, MDRow &rowOut) override
 
void postProcess () override
 
- Public Member Functions inherited from XmippMetadataProgram
MetaDatagetInputMd ()
 
MetaDataVecgetOutputMd ()
 
 XmippMetadataProgram ()
 Empty constructor. More...
 
virtual int tryRead (int argc, const char **argv, bool reportErrors=true)
 
virtual void init ()
 
virtual void setup (MetaData *md, const FileName &o="", const FileName &oroot="", bool applyGeo=false, MDLabel label=MDL_IMAGE)
 
virtual ~XmippMetadataProgram ()
 
void setMode (WriteModeMetaData _mode)
 
void setupRowOut (const FileName &fnImgIn, const MDRow &rowIn, const FileName &fnImgOut, MDRow &rowOut) const
 Prepare rowout. More...
 
virtual void wait ()
 Wait for the distributor to finish. More...
 
virtual void checkPoint ()
 For very long programs, it may be needed to write checkpoints. More...
 
virtual void run ()
 Run over all images. More...
 
- Public Member Functions inherited from XmippProgram
const char * getParam (const char *param, int arg=0)
 
const char * getParam (const char *param, const char *subparam, int arg=0)
 
int getIntParam (const char *param, int arg=0)
 
int getIntParam (const char *param, const char *subparam, int arg=0)
 
double getDoubleParam (const char *param, int arg=0)
 
double getDoubleParam (const char *param, const char *subparam, int arg=0)
 
float getFloatParam (const char *param, int arg=0)
 
float getFloatParam (const char *param, const char *subparam, int arg=0)
 
void getListParam (const char *param, StringVector &list)
 
int getCountParam (const char *param)
 
bool checkParam (const char *param)
 
bool existsParam (const char *param)
 
void addParamsLine (const String &line)
 
void addParamsLine (const char *line)
 
ParamDefgetParamDef (const char *param) const
 
virtual void quit (int exit_code=0) const
 
virtual int tryRun ()
 
void initProgress (size_t total, size_t stepBin=60)
 
void setProgress (size_t value=0)
 
void endProgress ()
 
void processDefaultComment (const char *param, const char *left)
 
void setDefaultComment (const char *param, const char *comment)
 
void setProgramName (const char *name)
 
void addUsageLine (const char *line, bool verbatim=false)
 
void clearUsage ()
 
void addExampleLine (const char *example, bool verbatim=true)
 
void addSeeAlsoLine (const char *seeAlso)
 
void addKeywords (const char *keywords)
 
const char * name () const
 
virtual void usage (int verb=0) const
 
virtual void usage (const String &param, int verb=2)
 
int version () const
 
virtual void read (int argc, const char **argv, bool reportErrors=true)
 
virtual void read (int argc, char **argv, bool reportErrors=true)
 
void read (const String &argumentsLine)
 
 XmippProgram ()
 
 XmippProgram (int argc, const char **argv)
 
virtual ~XmippProgram ()
 

Public Attributes

FileName fnVolR
 
FileName fnParticles
 
FileName fnImgI
 
FileName fnOut
 
FileName fnMaskVol
 
FileName fnMask
 
FileName fnProj
 
double sampling
 
double padFourier
 
double maxResol
 
double cirmaskrad
 
int sigma
 
int limitfreq
 
int maxwiIdx
 
bool nonNegative
 
bool boost
 
bool subtract
 
MultidimArray< int > wi
 
Image< double > V
 
Image< double > vM
 
Image< double > ivM
 
Image< double > M
 
Image< double > I
 
Image< double > Pctf
 
Image< double > iM
 
Image< double > Mfinal
 
Image< double > Idiff
 
Image< double > cirmask
 
Projection P
 
Projection Pmask
 
Projection PmaskVol
 
FourierFilter FilterG
 
const MultidimArray< double > * ctfImage = nullptr
 
FourierTransformer transformerP
 
FourierTransformer transformerI
 
MultidimArray< std::complex< double > > IFourier
 
MultidimArray< std::complex< double > > PFourier
 
MultidimArray< std::complex< double > > PFourier0
 
MultidimArray< std::complex< double > > PFourier1
 
MultidimArray< std::complex< double > > IiMFourier
 
MultidimArray< std::complex< double > > PiMFourier
 
FourierTransformer transformerIiM
 
FourierTransformer transformerPiM
 
CTFDescription ctf
 
FourierFilter FilterCTF
 
Image< double > padp
 
Image< double > PmaskI
 
Image< double > ImgiM
 
MultidimArray< std::complex< double > > ImgiMFourier
 
MetaDataVec mdParticles
 
MDRowVec row
 
Matrix1D< double > roffset
 
struct Angles part_angles
 
bool disable
 
int rank
 
FourierProjectorprojector
 
FourierProjectorprojectorMask
 
- Public Attributes inherited from XmippMetadataProgram
FileName fn_in
 Filenames of input and output Metadata. More...
 
FileName fn_out
 
FileName baseName
 
FileName pathBaseName
 
FileName oextBaseName
 
bool apply_geo
 Apply geo. More...
 
size_t ndimOut
 Output dimensions. More...
 
size_t zdimOut
 
size_t ydimOut
 
size_t xdimOut
 
DataType datatypeOut
 
size_t mdInSize
 Number of input elements. More...
 
- Public Attributes inherited from XmippProgram
bool doRun
 
bool runWithoutArgs
 
int verbose
 Verbosity level. More...
 
int debug
 

Additional Inherited Members

- Protected Member Functions inherited from XmippMetadataProgram
virtual void initComments ()
 
virtual bool getImageToProcess (size_t &objId, size_t &objIndex)
 
void show () const override
 
virtual void startProcessing ()
 
virtual void finishProcessing ()
 
virtual void writeOutput ()
 
virtual void showProgress ()
 
virtual void defineLabelParam ()
 
- Protected Member Functions inherited from XmippProgram
void defineCommons ()
 
- Protected Attributes inherited from XmippMetadataProgram
WriteModeMetaData mode
 Metadata writing mode: OVERWRITE, APPEND. More...
 
FileName oext
 Output extension and root. More...
 
FileName oroot
 
MDLabel image_label
 MDLabel to be used to read/write images, usually will be MDL_IMAGE. More...
 
bool produces_an_output
 Indicate that a unique final output is produced. More...
 
bool produces_a_metadata
 Indicate that the unique final output file is a Metadata. More...
 
bool each_image_produces_an_output
 Indicate that an output is produced for each image in the input. More...
 
bool allow_apply_geo
 
bool decompose_stacks
 Input Metadata will treat a stack file as a set of images instead of a unique file. More...
 
bool delete_output_stack
 Delete previous output stack file prior to process images. More...
 
bool get_image_info
 Get the input image file dimensions to further operations. More...
 
bool save_metadata_stack
 Save the associated output metadata when output file is a stack. More...
 
bool track_origin
 Include the original input image filename in the output stack. More...
 
bool keep_input_columns
 Keep input metadata columns. More...
 
bool remove_disabled
 Remove disabled images from the input selfile. More...
 
bool allow_time_bar
 Show process time bar. More...
 
bool input_is_metadata
 Input is a metadata. More...
 
bool single_image
 Input is a single image. More...
 
bool input_is_stack
 Input is a stack. More...
 
bool output_is_stack
 Output is a stack. More...
 
bool create_empty_stackfile
 
bool delete_mdIn
 
size_t time_bar_step
 Some time bar related counters. More...
 
size_t time_bar_size
 
size_t time_bar_done
 
- Protected Attributes inherited from XmippProgram
int errorCode
 
ProgramDefprogDef
 Program definition and arguments parser. More...
 
std::map< String, CommentListdefaultComments
 
int argc
 Original command line arguments. More...
 
const char ** argv
 

Detailed Description

Subtract projections from particles

Definition at line 41 of file subtract_projection.h.

Constructor & Destructor Documentation

◆ ProgSubtractProjection()

ProgSubtractProjection::ProgSubtractProjection ( )

Empty constructor.

Definition at line 51 of file subtract_projection.cpp.

52 {
53  produces_a_metadata = true;
55  keep_input_columns = true;
56  save_metadata_stack = true;
57  remove_disabled = false;
58  projector = nullptr;
59  projectorMask = nullptr;
60  rank = 0;
61 }
FourierProjector * projectorMask
FourierProjector * projector
bool remove_disabled
Remove disabled images from the input selfile.
bool save_metadata_stack
Save the associated output metadata when output file is a stack.
bool keep_input_columns
Keep input metadata columns.
bool each_image_produces_an_output
Indicate that an output is produced for each image in the input.
bool produces_a_metadata
Indicate that the unique final output file is a Metadata.

◆ ~ProgSubtractProjection()

ProgSubtractProjection::~ProgSubtractProjection ( )

Destructor.

Definition at line 63 of file subtract_projection.cpp.

64 {
65  delete projector;
66  delete projectorMask;
67 }
FourierProjector * projectorMask
FourierProjector * projector

Member Function Documentation

◆ applyCTF()

Image< double > ProgSubtractProjection::applyCTF ( const MDRow r,
Projection proj 
)

Definition at line 188 of file subtract_projection.cpp.

188  {
190  ctf.readFromMdRow(r);
191  ctf.Tm = sampling;
194  FilterCTF.ctf.enable_CTFnoise = false;
195  FilterCTF.ctf = ctf;
196  // Padding before apply CTF
197  MultidimArray <double> &mpad = padp();
198  mpad.setXmippOrigin();
199  MultidimArray<double> &mproj = proj();
200  mproj.setXmippOrigin();
201  mproj.window(mpad,STARTINGY(mproj)*(int)padFourier, STARTINGX(mproj)*(int)padFourier, FINISHINGY(mproj)*(int)padFourier, FINISHINGX(mproj)*(int)padFourier);
202  FilterCTF.generateMask(mpad);
203  FilterCTF.applyMaskSpace(mpad);
204  //Crop to restore original size
205  mpad.window(mproj,STARTINGY(mproj), STARTINGX(mproj), FINISHINGY(mproj), FINISHINGX(mproj));
206  }
207  return proj;
208  }
Defocus U (Angstroms)
CTFDescription ctf
#define FINISHINGX(v)
Name for the CTF Model (std::string)
#define STARTINGX(v)
#define STARTINGY(v)
void window(MultidimArray< T1 > &result, int n0, int z0, int y0, int x0, int nF, int zF, int yF, int xF, T1 init_value=0) const
#define CTF
double Tm
Sampling rate (A/pixel)
Definition: ctf.h:240
void readFromMdRow(const MDRow &row, bool disable_if_not_K=true)
Definition: ctf.cpp:1172
#define FINISHINGY(v)
virtual bool containsLabel(MDLabel label) const =0
void produceSideInfo()
Produce Side information.
Definition: ctf.cpp:1392
bool enable_CTFnoise
Enable CTFnoise part.
Definition: ctf.h:273
void generateMask(MultidimArray< double > &v)
void applyMaskSpace(MultidimArray< double > &v)

◆ binarizeMask()

Image< double > ProgSubtractProjection::binarizeMask ( Projection m) const

Definition at line 170 of file subtract_projection.cpp.

170  {
171  double maxMaskVol;
172  double minMaskVol;
174  mm.computeDoubleMinMax(minMaskVol, maxMaskVol);
176  DIRECT_MULTIDIM_ELEM(mm,n) = (DIRECT_MULTIDIM_ELEM(mm,n)>0.1*maxMaskVol) ? 1:0;
177  return m;
178  }
return mm
void computeDoubleMinMax(double &minval, double &maxval) const
#define FOR_ALL_DIRECT_ELEMENTS_IN_MULTIDIMARRAY(v)
#define DIRECT_MULTIDIM_ELEM(v, n)
int m
int * n

◆ checkBestModel()

Matrix1D< double > ProgSubtractProjection::checkBestModel ( MultidimArray< std::complex< double > > &  PFourierf,
const MultidimArray< std::complex< double > > &  PFourierf0,
const MultidimArray< std::complex< double > > &  PFourierf1,
const MultidimArray< std::complex< double > > &  IFourierf 
) const

Definition at line 259 of file subtract_projection.cpp.

260  {
261  // Compute R2 coefficient for order 0 model (R20) and order 1 model (R21)
262  auto N = 2.0*(double)MULTIDIM_SIZE(PFourierf);
263  double R20adj = evaluateFitting(IFourierf, PFourierf0); // adjusted R2 for an order 0 model = R2
264  double R21 = evaluateFitting(IFourierf, PFourierf1);
265  double R21adj = 1.0 - (1.0 - R21) * (N - 1.0) / (N - 2.0); // adjusted R2 for an order 1 model -> p = 2
266  //Decide best fitting
267  Matrix1D<double> R2(2);
268  if (R21adj > R20adj) { // Order 1: T(w) = b01 + b1*wi
269  PFourierf = PFourierf1;
270  R2(0) = R21adj;
271  R2(1) = 1;
272  }
273  else { // Order 0: T(w) = b00
274  PFourierf = PFourierf0;
275  R2(0) = R20adj;
276  R2(1) = 0;
277  }
278  return R2;
279 }
#define MULTIDIM_SIZE(v)
double evaluateFitting(const MultidimArray< std::complex< double > > &, const MultidimArray< std::complex< double > > &) const

◆ computeEstimationImage()

MultidimArray< std::complex< double > > ProgSubtractProjection::computeEstimationImage ( const MultidimArray< double > &  Img,
const MultidimArray< double > &  InvM,
FourierTransformer transformerImgiM 
)

Definition at line 229 of file subtract_projection.cpp.

230  {
231  ImgiM().initZeros(Img);
232  ImgiM().setXmippOrigin();
235  transformerImgiM.FourierTransform(ImgiM(),ImgiMFourier,false);
236  return ImgiMFourier;
237 }
MultidimArray< std::complex< double > > ImgiMFourier
#define FOR_ALL_DIRECT_ELEMENTS_IN_MULTIDIMARRAY(v)
#define DIRECT_MULTIDIM_ELEM(v, n)
void FourierTransform(T &v, T1 &V, bool getCopy=true)
Definition: xmipp_fftw.h:166
int * n

◆ createMask()

void ProgSubtractProjection::createMask ( const FileName fnM,
Image< double > &  m,
Image< double > &  im 
)

Processing methods.

Definition at line 149 of file subtract_projection.cpp.

149  {
150  if (fnM.isEmpty())
151  {
152  m().initZeros((int)XSIZE(V()),(int)YSIZE(V()),(int)ZSIZE(V()));
153  im = m;
155  DIRECT_MULTIDIM_ELEM(im(),n) += 1;
156  }
157  else
158  {
159  m.read(fnM);
160  m().setXmippOrigin();
161  im = m;
162  if (!subtract)
163  {
165  DIRECT_MULTIDIM_ELEM(im(),n) = (DIRECT_MULTIDIM_ELEM(m(),n)*(-1))+1;
166  }
167  }
168  }
#define YSIZE(v)
#define XSIZE(v)
#define FOR_ALL_DIRECT_ELEMENTS_IN_MULTIDIMARRAY(v)
#define ZSIZE(v)
#define DIRECT_MULTIDIM_ELEM(v, n)
int m
bool isEmpty() const
int read(const FileName &name, DataMode datamode=DATA, size_t select_img=ALL_IMAGES, bool mapData=false, int mode=WRITE_READONLY)
int * n

◆ defineParams()

void ProgSubtractProjection::defineParams ( )
overridevirtual

Define parameters.

Reimplemented from XmippMetadataProgram.

Definition at line 104 of file subtract_projection.cpp.

105  {
106  //Usage
107  addUsageLine("This program computes the subtraction between particles and a reference");
108  addUsageLine(" volume, by computing its projections with the same angles that input particles have.");
109  addUsageLine(" Then, each particle and the correspondent projection of the reference volume are numerically");
110  addUsageLine(" adjusted and subtracted using a mask which denotes the region to keep or subtract.");
111  //Parameters
113  addParamsLine("--ref <volume>\t: Reference volume to subtract");
114  addParamsLine("[--mask <mask=\"\">]\t: 3D mask for region to keep, no mask implies subtraction of whole images");
115  addParamsLine("[--sampling <sampling=1>]\t: Sampling rate (A/pixel)");
116  addParamsLine("[--max_resolution <f=4>]\t: Maximum resolution (A)");
117  addParamsLine("[--fmask_width <w=40>]\t: Extra width of final mask (A). -1 means no masking.");
118  addParamsLine("[--padding <p=2>]\t: Padding factor for Fourier projector");
119  addParamsLine("[--sigma <s=2>]\t: Decay of the filter (sigma) to smooth the mask transition");
120  addParamsLine("[--limit_freq <l=0>]\t: Limit frequency (= 1) or not (= 0) in adjustment process");
121  addParamsLine("[--nonNegative]\t: Ignore particles with negative beta0 or R2");
122  addParamsLine("[--boost]\t: Perform a boosting of original particles");
123  addParamsLine("[--cirmaskrad <c=-1.0>]\t: Radius of the circular mask");
124  addParamsLine("[--save <structure=\"\">]\t: Path for saving intermediate files");
125  addParamsLine("[--subtract]\t: The mask contains the region to SUBTRACT");
126  addExampleLine("A typical use is:",false);
127  addExampleLine("xmipp_subtract_projection -i input_particles.xmd --ref input_map.mrc --mask mask_vol.mrc "
128  "-o output_particles --sampling 1 --fmask_width 40 --max_resolution 4");
129  }
void addExampleLine(const char *example, bool verbatim=true)
void addUsageLine(const char *line, bool verbatim=false)
void addParamsLine(const String &line)

◆ evaluateFitting()

double ProgSubtractProjection::evaluateFitting ( const MultidimArray< std::complex< double > > &  y,
const MultidimArray< std::complex< double > > &  yp 
) const

Definition at line 239 of file subtract_projection.cpp.

240  {
241  double sumY = 0;
242  double sumY2 = 0;
243  double sumE2 = 0;
245  double realyn = real(DIRECT_MULTIDIM_ELEM(y, n));
246  double imagyn = imag(DIRECT_MULTIDIM_ELEM(y, n));
247  double ereal = realyn - real(DIRECT_MULTIDIM_ELEM(yp, n));
248  double eimag = imagyn - imag(DIRECT_MULTIDIM_ELEM(yp, n));
249  sumE2 += ereal*ereal + eimag*eimag;
250  sumY += realyn + imagyn;
251  sumY2 += realyn*realyn + imagyn*imagyn;
252  }
253  auto meanY = sumY / (2.0*(double)MULTIDIM_SIZE(y));
254  auto varY = sumY2 / (2.0*(double)MULTIDIM_SIZE(y)) - meanY*meanY;
255  auto R2 = 1.0 - (sumE2/(2.0*(double)MULTIDIM_SIZE(y))) / varY;
256  return R2;
257  }
#define MULTIDIM_SIZE(v)
#define FOR_ALL_DIRECT_ELEMENTS_IN_MULTIDIMARRAY(v)
#define DIRECT_MULTIDIM_ELEM(v, n)
int * n

◆ invertMask()

Image< double > ProgSubtractProjection::invertMask ( const Image< double > &  m)

Definition at line 180 of file subtract_projection.cpp.

180  {
181  PmaskI = m;
182  MultidimArray<double> &mPmaskI=PmaskI();
184  DIRECT_MULTIDIM_ELEM(mPmaskI,n) = (DIRECT_MULTIDIM_ELEM(mPmaskI,n)*(-1))+1;
185  return PmaskI;
186  }
#define FOR_ALL_DIRECT_ELEMENTS_IN_MULTIDIMARRAY(v)
#define DIRECT_MULTIDIM_ELEM(v, n)
int m
int * n

◆ postProcess()

void ProgSubtractProjection::postProcess ( )
overridevirtual

Reimplemented from XmippMetadataProgram.

Definition at line 474 of file subtract_projection.cpp.

475 {
477 }
void write(const FileName &outFile, WriteModeMetaData mode=MD_OVERWRITE) const

◆ preProcess()

void ProgSubtractProjection::preProcess ( )
overridevirtual

Reimplemented from XmippMetadataProgram.

Definition at line 281 of file subtract_projection.cpp.

281  {
282  // Read input volume, mask and particles metadata
283  show();
284  V.read(fnVolR);
285  V().setXmippOrigin();
286 
287  // Create 2D circular mask to avoid edge artifacts after wrapping
288  size_t Xdim;
289  size_t Ydim;
290  size_t Zdim;
291  size_t Ndim;
292  V.getDimensions(Xdim, Ydim, Zdim, Ndim);
293  cirmask().initZeros((int)Ydim, (int)Xdim);
294  cirmask().setXmippOrigin();
295  if (cirmaskrad == -1.0)
296  cirmaskrad = (double)XSIZE(V())/2;
298  cirmask.write(formatString("%s/cirmask.mrc", fnProj.c_str()));
299 
300  // Create mock image of same size as particles (and referencce volume) to get
301  I().initZeros((int)Ydim, (int)Xdim);
302  I().initConstant(1);
304 
305  // Initialize Gaussian LPF to smooth mask
308  FilterG.w1=sigma;
309 
310  // Construct frequencies image
312  Matrix1D<double> w(2);
313  double cutFreq = sampling/maxResol;
314  for (int i=0; i<YSIZE(wi); i++) {
316  for (int j=0; j<XSIZE(wi); j++) {
318  DIRECT_A2D_ELEM(wi,i,j) = (int)round((sqrt(YY(w)*YY(w) + XX(w)*XX(w))) * (int)XSIZE(IFourier)); // indexes
319  }
320  }
321  if (limitfreq == 0)
322  maxwiIdx = (int)XSIZE(wi);
323  else
324  DIGFREQ2FFT_IDX(cutFreq, (int)YSIZE(IFourier), maxwiIdx)
325 
326  if (rank==0)
327  {
328  // Read or create mask keep and compute inverse of mask keep (mask subtract)
329  createMask(fnMask, vM, ivM);
332  // Initialize Fourier projectors
333  std::cout << "-------Initializing projectors-------" << std::endl;
335  projectorMask = new FourierProjector(vM(), padFourier, cutFreq, xmipp_transformation::BSPLINE3);
336  std::cout << "-------Projectors initialized-------" << std::endl;
337  }
338  else
339  {
342  }
343  }
#define YSIZE(v)
FourierProjector * projectorMask
FourierTransformer transformerI
void show() const override
Show.
void sqrt(Image< double > &op)
#define DIRECT_A2D_ELEM(v, i, j)
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)
doublereal * w
#define DIGFREQ2FFT_IDX(freq, size, idx)
Definition: xmipp_fft.h:61
FourierProjector * projector
#define i
#define XX(v)
Definition: matrix1d.h:85
MultidimArray< int > wi
#define XSIZE(v)
#define FOR_ALL_DIRECT_ELEMENTS_IN_MULTIDIMARRAY(v)
#define DIRECT_MULTIDIM_ELEM(v, n)
void FourierTransform(T &v, T1 &V, bool getCopy=true)
Definition: xmipp_fftw.h:166
__device__ float FFT_IDX2DIGFREQ(int idx, int size)
void createMask(const FileName &, Image< double > &, Image< double > &)
Processing methods.
#define j
#define YY(v)
Definition: matrix1d.h:93
int round(double x)
Definition: ap.cpp:7245
String formatString(const char *format,...)
int read(const FileName &name, DataMode datamode=DATA, size_t select_img=ALL_IMAGES, bool mapData=false, int mode=WRITE_READONLY)
#define REALGAUSSIAN
void initZeros(const MultidimArray< T1 > &op)
void RaisedCosineMask(MultidimArray< double > &mask, double r1, double r2, int mode, double x0, double y0, double z0)
Definition: mask.cpp:36
int * n
MultidimArray< std::complex< double > > IFourier
#define LOWPASS
void getDimensions(size_t &Xdim, size_t &Ydim, size_t &Zdim, size_t &Ndim) const

◆ processImage()

void ProgSubtractProjection::processImage ( const FileName fnImg,
const FileName fnImgOut,
const MDRow rowIn,
MDRow rowOut 
)
overridevirtual

Implements XmippMetadataProgram.

Definition at line 345 of file subtract_projection.cpp.

346  {
347  // Initialize aux variable
348  disable = false;
349  // Project volume and process projections
350  const auto sizeI = (int)XSIZE(I());
351  processParticle(rowIn, sizeI, transformerP, transformerI);
352  // Build projected and final masks
353  if (fnMask.isEmpty()) { // If there is no provided mask
354  M().initZeros(P());
355  // inverse mask (iM) is all 1s
356  iM = invertMask(M);
357  }
358  else { // If a mask has been provided
360  // Apply binarization, shift and gaussian filter to the projected mask
361  M = binarizeMask(Pmask);
362  selfTranslate(xmipp_transformation::LINEAR, M(), roffset, xmipp_transformation::DONT_WRAP);
364  if (subtract) // If the mask contains the part to SUBTRACT: iM = input mask
365  iM = M;
366  else // If the mask contains the part to KEEP: iM = INVERSE of original mask
367  iM = invertMask(M);
368  }
369 
370  // Compute estimation images: IiM = I*iM and PiM = P*iM
373 
374  // Estimate transformation with model of order 0: T(w) = beta00 and model of order 1: T(w) = beta01 + beta1*w
376  num0.initZeros(maxwiIdx+1);
378  den0.initZeros(maxwiIdx+1);
379  Matrix2D<double> A1;
380  A1.initZeros(2,2);
381  Matrix1D<double> b1;
382  b1.initZeros(2);
384  int win = DIRECT_MULTIDIM_ELEM(wi, n);
385  if (win < maxwiIdx)
386  {
387  double realPiMFourier = real(DIRECT_MULTIDIM_ELEM(PiMFourier,n));
388  double imagPiMFourier = imag(DIRECT_MULTIDIM_ELEM(PiMFourier,n));
389  DIRECT_MULTIDIM_ELEM(num0,win) += real(DIRECT_MULTIDIM_ELEM(IiMFourier,n)) * realPiMFourier
390  + imag(DIRECT_MULTIDIM_ELEM(IiMFourier,n)) * imagPiMFourier;
391  DIRECT_MULTIDIM_ELEM(den0,win) += realPiMFourier*realPiMFourier + imagPiMFourier*imagPiMFourier;
392  A1(0,0) += realPiMFourier*realPiMFourier + imagPiMFourier*imagPiMFourier;
393  A1(0,1) += win*(realPiMFourier + imagPiMFourier);
394  A1(1,1) += 2*win;
395  b1(0) += real(DIRECT_MULTIDIM_ELEM(IiMFourier,n)) * realPiMFourier + imag(DIRECT_MULTIDIM_ELEM(IiMFourier,n)) * imagPiMFourier;
396  b1(1) += win*(real(DIRECT_MULTIDIM_ELEM(IiMFourier,n))+imag(DIRECT_MULTIDIM_ELEM(IiMFourier,n)));
397  }
398  }
399  A1(1,0) = A1(0,1);
400 
401  // Compute beta00 from order 0 model
402  double beta00 = num0.sum()/den0.sum();
403  if (nonNegative && beta00 < 0)
404  {
405  disable = true;
406  }
407  // Apply adjustment order 0: PFourier0 = T(w) * PFourier = beta00 * PFourier
410  DIRECT_MULTIDIM_ELEM(PFourier0,n) *= beta00;
411  PFourier0(0,0) = IiMFourier(0,0);
412 
413  // Compute beta01 and beta1 from order 1 model
415  h.A = A1;
416  h.b = b1;
417  Matrix1D<double> betas1;
418  solveLinearSystem(h,betas1);
419  double beta01 = betas1(0);
420  double beta1 = betas1(1);
421 
422  // Apply adjustment order 1: PFourier1 = T(w) * PFourier = (beta01 + beta1*w) * PFourier
426  PFourier1(0,0) = IiMFourier(0,0);
427 
428  // Check best model
430  double beta0save;
431  double beta1save;
432  if (R2adj(1) == 0)
433  {
434  beta0save = beta00;
435  beta1save = 0;
436  }
437  else
438  {
439  beta0save = beta01;
440  beta1save = beta1;
441  }
442 
443  // Create empty new image for output particle
444  MultidimArray<double> &mIdiff=Idiff();
445  mIdiff.initZeros(I());
446  mIdiff.setXmippOrigin();
447 
448  if (boost) // Boosting of original particles
449  {
450  if (R2adj(1) == 0)
451  {
453  DIRECT_MULTIDIM_ELEM(IFourier,n) /= beta00;
454  }
455  else if (R2adj(1) == 1)
456  {
459  }
461  }
462  else // Subtraction
463  {
464  // Recover adjusted projection (P) in real space
466  mIdiff.initZeros(I());
467  mIdiff.setXmippOrigin();
470  }
471  writeParticle(rowOut, fnImgOut, Idiff, R2adj(0), beta0save, beta1save);
472 }
FourierProjector * projectorMask
MultidimArray< std::complex< double > > PiMFourier
MultidimArray< std::complex< double > > IiMFourier
Image< double > binarizeMask(Projection &) const
MultidimArray< std::complex< double > > PFourier1
void inverseFourierTransform()
Definition: xmipp_fftw.cpp:329
FourierTransformer transformerI
FourierTransformer transformerP
void writeParticle(MDRow &rowOut, FileName, Image< double > &, double, double, double)
Matrix1D< double > checkBestModel(MultidimArray< std::complex< double > > &, const MultidimArray< std::complex< double > > &, const MultidimArray< std::complex< double > > &, const MultidimArray< std::complex< double > > &) const
Matrix1D< double > roffset
MultidimArray< std::complex< double > > computeEstimationImage(const MultidimArray< double > &, const MultidimArray< double > &, FourierTransformer &)
FourierTransformer transformerIiM
Matrix1D< double > b
void selfTranslate(int SplineDegree, MultidimArray< T > &V1, const Matrix1D< double > &v, bool wrap=xmipp_transformation::WRAP, T outside=0)
Matrix2D< double > A
MultidimArray< int > wi
#define XSIZE(v)
#define FOR_ALL_DIRECT_ELEMENTS_IN_MULTIDIMARRAY(v)
#define DIRECT_MULTIDIM_ELEM(v, n)
void projectVolume(FourierProjector &projector, Projection &P, int Ydim, int Xdim, double rot, double tilt, double psi, const MultidimArray< double > *ctf)
void initZeros()
Definition: matrix1d.h:592
void solveLinearSystem(PseudoInverseHelper &h, Matrix1D< double > &result)
const MultidimArray< double > * ctfImage
bool isEmpty() const
void processParticle(const MDRow &rowIn, int, FourierTransformer &, FourierTransformer &)
MultidimArray< std::complex< double > > PFourier
void initZeros()
Definition: matrix2d.h:626
void initZeros(const MultidimArray< T1 > &op)
FourierTransformer transformerPiM
Image< double > invertMask(const Image< double > &)
int * n
MultidimArray< std::complex< double > > IFourier
double sum() const
MultidimArray< std::complex< double > > PFourier0
void applyMaskSpace(MultidimArray< double > &v)

◆ processParticle()

void ProgSubtractProjection::processParticle ( const MDRow rowIn,
int  sizeImg,
FourierTransformer transformerPf,
FourierTransformer transformerIf 
)

Definition at line 210 of file subtract_projection.cpp.

210  {
211  readParticle(rowprocess);
212  rowprocess.getValueOrDefault(MDL_ANGLE_ROT, part_angles.rot, 0);
213  rowprocess.getValueOrDefault(MDL_ANGLE_TILT, part_angles.tilt, 0);
214  rowprocess.getValueOrDefault(MDL_ANGLE_PSI, part_angles.psi, 0);
215  roffset.initZeros(2);
216  rowprocess.getValueOrDefault(MDL_SHIFT_X, roffset(0), 0);
217  rowprocess.getValueOrDefault(MDL_SHIFT_Y, roffset(1), 0);
218  roffset *= -1;
220  selfTranslate(xmipp_transformation::LINEAR, P(), roffset, xmipp_transformation::WRAP);
221  Pctf = applyCTF(rowprocess, P);
222  MultidimArray<double> &mPctf = Pctf();
225  transformerPf.FourierTransform(Pctf(), PFourier, false);
226  transformerIf.FourierTransform(I(), IFourier, false);
227 }
Rotation angle of an image (double,degrees)
Tilting angle of an image (double,degrees)
Shift for the image in the X axis (double)
Special label to be used when gathering MDs in MpiMetadataPrograms.
Matrix1D< double > roffset
FourierProjector * projector
void selfTranslate(int SplineDegree, MultidimArray< T > &V1, const Matrix1D< double > &v, bool wrap=xmipp_transformation::WRAP, T outside=0)
#define FOR_ALL_DIRECT_ELEMENTS_IN_MULTIDIMARRAY(v)
Image< double > applyCTF(const MDRow &, Projection &)
#define DIRECT_MULTIDIM_ELEM(v, n)
void projectVolume(FourierProjector &projector, Projection &P, int Ydim, int Xdim, double rot, double tilt, double psi, const MultidimArray< double > *ctf)
void initZeros()
Definition: matrix1d.h:592
void FourierTransform(T &v, T1 &V, bool getCopy=true)
Definition: xmipp_fftw.h:166
const MultidimArray< double > * ctfImage
void readParticle(const MDRow &rowIn)
Read and write methods.
MultidimArray< std::complex< double > > PFourier
Shift for the image in the Y axis (double)
int * n
MultidimArray< std::complex< double > > IFourier

◆ readParams()

void ProgSubtractProjection::readParams ( )
overridevirtual

Read argument from command line.

Reimplemented from XmippMetadataProgram.

Definition at line 70 of file subtract_projection.cpp.

71  {
73  fnVolR = getParam("--ref");
74  fnMask=getParam("--mask");
75  sigma=getIntParam("--sigma");
76  sampling = getDoubleParam("--sampling");
77  padFourier = getDoubleParam("--padding");
78  maxResol = getDoubleParam("--max_resolution");
79  limitfreq = getIntParam("--limit_freq");
80  cirmaskrad = getDoubleParam("--cirmaskrad");
81  fnProj = getParam("--save");
82  nonNegative = checkParam("--nonNegative");
83  boost = checkParam("--boost");
84  subtract = checkParam("--subtract");
85  }
double getDoubleParam(const char *param, int arg=0)
const char * getParam(const char *param, int arg=0)
bool checkParam(const char *param)
int getIntParam(const char *param, int arg=0)

◆ readParticle()

void ProgSubtractProjection::readParticle ( const MDRow rowIn)

Read and write methods.

Definition at line 131 of file subtract_projection.cpp.

131  {
132  r.getValueOrDefault(MDL_IMAGE, fnImgI, "no_filename");
133  I.read(fnImgI);
134  I().setXmippOrigin();
135  }
int read(const FileName &name, DataMode datamode=DATA, size_t select_img=ALL_IMAGES, bool mapData=false, int mode=WRITE_READONLY)
Name of an image (std::string)

◆ show()

void ProgSubtractProjection::show ( ) const
overridevirtual

Show.

Reimplemented from XmippProgram.

Definition at line 88 of file subtract_projection.cpp.

88  {
89  if (!verbose)
90  return;
91  std::cout
92  << "Input particles:\t" << fnParticles << std::endl
93  << "Reference volume:\t" << fnVolR << std::endl
94  << "Mask of the region to keep:\t" << fnMask << std::endl
95  << "Sigma of low pass filter:\t" << sigma << std::endl
96  << "Sampling rate:\t" << sampling << std::endl
97  << "Padding factor:\t" << padFourier << std::endl
98  << "Max. Resolution:\t" << maxResol << std::endl
99  << "Limit frequency:\t" << limitfreq << std::endl
100  << "Output particles:\t" << fnOut << std::endl;
101  }
int verbose
Verbosity level.

◆ writeParticle()

void ProgSubtractProjection::writeParticle ( MDRow rowOut,
FileName  fnImgOut,
Image< double > &  img,
double  R2a,
double  b0save,
double  b1save 
)

Definition at line 137 of file subtract_projection.cpp.

137  {
138  img.write(fnImgOut);
139  rowOut.setValue(MDL_IMAGE, fnImgOut);
140  rowOut.setValue(MDL_SUBTRACTION_R2, R2a);
141  rowOut.setValue(MDL_SUBTRACTION_BETA0, b0save);
142  rowOut.setValue(MDL_SUBTRACTION_BETA1, b1save);
143  if (nonNegative && (disable || R2a < 0))
144  {
145  rowOut.setValue(MDL_ENABLED, -1);
146  }
147  }
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)
Is this image enabled? (int [-1 or 1])
R2 coefficient of subtracted particle.
Beta 0 coefficient of adjusted model for subtract particle.
void setValue(MDLabel label, const T &d, bool addLabel=true)
Name of an image (std::string)
Beta 1 coefficient of adjusted model for subtract particle.

Member Data Documentation

◆ boost

bool ProgSubtractProjection::boost

Definition at line 60 of file subtract_projection.h.

◆ cirmask

Image<double> ProgSubtractProjection::cirmask

Definition at line 75 of file subtract_projection.h.

◆ cirmaskrad

double ProgSubtractProjection::cirmaskrad

Definition at line 55 of file subtract_projection.h.

◆ ctf

CTFDescription ProgSubtractProjection::ctf

Definition at line 97 of file subtract_projection.h.

◆ ctfImage

const MultidimArray<double>* ProgSubtractProjection::ctfImage = nullptr

Definition at line 82 of file subtract_projection.h.

◆ disable

bool ProgSubtractProjection::disable

Definition at line 116 of file subtract_projection.h.

◆ FilterCTF

FourierFilter ProgSubtractProjection::FilterCTF

Definition at line 98 of file subtract_projection.h.

◆ FilterG

FourierFilter ProgSubtractProjection::FilterG

Definition at line 80 of file subtract_projection.h.

◆ fnImgI

FileName ProgSubtractProjection::fnImgI

Definition at line 47 of file subtract_projection.h.

◆ fnMask

FileName ProgSubtractProjection::fnMask

Definition at line 50 of file subtract_projection.h.

◆ fnMaskVol

FileName ProgSubtractProjection::fnMaskVol

Definition at line 49 of file subtract_projection.h.

◆ fnOut

FileName ProgSubtractProjection::fnOut

Definition at line 48 of file subtract_projection.h.

◆ fnParticles

FileName ProgSubtractProjection::fnParticles

Definition at line 46 of file subtract_projection.h.

◆ fnProj

FileName ProgSubtractProjection::fnProj

Definition at line 51 of file subtract_projection.h.

◆ fnVolR

FileName ProgSubtractProjection::fnVolR

Definition at line 45 of file subtract_projection.h.

◆ I

Image<double> ProgSubtractProjection::I

Definition at line 70 of file subtract_projection.h.

◆ Idiff

Image<double> ProgSubtractProjection::Idiff

Definition at line 74 of file subtract_projection.h.

◆ IFourier

MultidimArray< std::complex<double> > ProgSubtractProjection::IFourier

Definition at line 86 of file subtract_projection.h.

◆ IiMFourier

MultidimArray< std::complex<double> > ProgSubtractProjection::IiMFourier

Definition at line 90 of file subtract_projection.h.

◆ iM

Image<double> ProgSubtractProjection::iM

Definition at line 72 of file subtract_projection.h.

◆ ImgiM

Image<double> ProgSubtractProjection::ImgiM

Definition at line 101 of file subtract_projection.h.

◆ ImgiMFourier

MultidimArray< std::complex<double> > ProgSubtractProjection::ImgiMFourier

Definition at line 102 of file subtract_projection.h.

◆ ivM

Image<double> ProgSubtractProjection::ivM

Definition at line 67 of file subtract_projection.h.

◆ limitfreq

int ProgSubtractProjection::limitfreq

Definition at line 57 of file subtract_projection.h.

◆ M

Image<double> ProgSubtractProjection::M

Definition at line 69 of file subtract_projection.h.

◆ maxResol

double ProgSubtractProjection::maxResol

Definition at line 54 of file subtract_projection.h.

◆ maxwiIdx

int ProgSubtractProjection::maxwiIdx

Definition at line 58 of file subtract_projection.h.

◆ mdParticles

MetaDataVec ProgSubtractProjection::mdParticles

Definition at line 105 of file subtract_projection.h.

◆ Mfinal

Image<double> ProgSubtractProjection::Mfinal

Definition at line 73 of file subtract_projection.h.

◆ nonNegative

bool ProgSubtractProjection::nonNegative

Definition at line 59 of file subtract_projection.h.

◆ P

Projection ProgSubtractProjection::P

Definition at line 77 of file subtract_projection.h.

◆ padFourier

double ProgSubtractProjection::padFourier

Definition at line 53 of file subtract_projection.h.

◆ padp

Image<double> ProgSubtractProjection::padp

Definition at line 99 of file subtract_projection.h.

◆ part_angles

struct Angles ProgSubtractProjection::part_angles

Definition at line 114 of file subtract_projection.h.

◆ Pctf

Image<double> ProgSubtractProjection::Pctf

Definition at line 71 of file subtract_projection.h.

◆ PFourier

MultidimArray< std::complex<double> > ProgSubtractProjection::PFourier

Definition at line 87 of file subtract_projection.h.

◆ PFourier0

MultidimArray< std::complex<double> > ProgSubtractProjection::PFourier0

Definition at line 88 of file subtract_projection.h.

◆ PFourier1

MultidimArray< std::complex<double> > ProgSubtractProjection::PFourier1

Definition at line 89 of file subtract_projection.h.

◆ PiMFourier

MultidimArray< std::complex<double> > ProgSubtractProjection::PiMFourier

Definition at line 91 of file subtract_projection.h.

◆ Pmask

Projection ProgSubtractProjection::Pmask

Definition at line 78 of file subtract_projection.h.

◆ PmaskI

Image<double> ProgSubtractProjection::PmaskI

Definition at line 100 of file subtract_projection.h.

◆ PmaskVol

Projection ProgSubtractProjection::PmaskVol

Definition at line 79 of file subtract_projection.h.

◆ projector

FourierProjector* ProgSubtractProjection::projector

Definition at line 133 of file subtract_projection.h.

◆ projectorMask

FourierProjector* ProgSubtractProjection::projectorMask

Definition at line 134 of file subtract_projection.h.

◆ rank

int ProgSubtractProjection::rank

Definition at line 132 of file subtract_projection.h.

◆ roffset

Matrix1D<double> ProgSubtractProjection::roffset

Definition at line 107 of file subtract_projection.h.

◆ row

MDRowVec ProgSubtractProjection::row

Definition at line 106 of file subtract_projection.h.

◆ sampling

double ProgSubtractProjection::sampling

Definition at line 52 of file subtract_projection.h.

◆ sigma

int ProgSubtractProjection::sigma

Definition at line 56 of file subtract_projection.h.

◆ subtract

bool ProgSubtractProjection::subtract

Definition at line 61 of file subtract_projection.h.

◆ transformerI

FourierTransformer ProgSubtractProjection::transformerI

Definition at line 84 of file subtract_projection.h.

◆ transformerIiM

FourierTransformer ProgSubtractProjection::transformerIiM

Definition at line 94 of file subtract_projection.h.

◆ transformerP

FourierTransformer ProgSubtractProjection::transformerP

Definition at line 83 of file subtract_projection.h.

◆ transformerPiM

FourierTransformer ProgSubtractProjection::transformerPiM

Definition at line 95 of file subtract_projection.h.

◆ V

Image<double> ProgSubtractProjection::V

Definition at line 65 of file subtract_projection.h.

◆ vM

Image<double> ProgSubtractProjection::vM

Definition at line 66 of file subtract_projection.h.

◆ wi

MultidimArray<int> ProgSubtractProjection::wi

Definition at line 62 of file subtract_projection.h.


The documentation for this class was generated from the following files: