Xmipp  v3.23.11-Nereus
Public Member Functions | Public Attributes | Protected Member Functions | List of all members

#include <angular_sph_alignment_gpu.h>

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

Public Member Functions

 ProgAngularSphAlignmentGpu ()
 Empty constructor. More...
 
 ~ProgAngularSphAlignmentGpu ()
 Destructor. More...
 
void readParams ()
 Read argument from command line. More...
 
void show () const override
 Show. More...
 
void defineParams ()
 Define parameters. More...
 
void preProcess ()
 
void processImage (const FileName &fnImg, const FileName &fnImgOut, const MDRow &rowIn, MDRow &rowOut)
 
void numCoefficients (int l1, int l2, int &vecSize)
 Length of coefficients vector. More...
 
void minimizepos (int L1, int l2, Matrix1D< double > &steps)
 Determine the positions to be minimize of a vector containing spherical harmonic coefficients. More...
 
void fillVectorTerms (int l1, int l2, Matrix1D< int > &vL1, Matrix1D< int > &vN, Matrix1D< int > &vL2, Matrix1D< int > &vM)
 Zernike and SPH coefficients allocation. More...
 
void deformVol (MultidimArray< double > &mVD, const MultidimArray< double > &mV, double &def, double rot, double tilt, double psi)
 Deform a volumen using Zernike-Spherical harmonic basis. More...
 
void updateCTFImage (double defocusU, double defocusV, double angle)
 
double tranformImageSph (double *pclnm, double rot, double tilt, double psi, Matrix2D< double > &A, double deltaDefocusU, double deltaDefocusV, double deltaDefocusAngle)
 
virtual void finishProcessing ()
 
virtual void writeImageParameters (const FileName &fnImg)
 
- 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 fnMaskR
 
FileName fnOutDir
 Output directory. More...
 
int L1
 
int L2
 
Matrix1D< int > vL1
 
Matrix1D< int > vN
 
Matrix1D< int > vL2
 
Matrix1D< int > vM
 
double maxShift
 
double maxAngularChange
 
double maxResol
 
double Ts
 
int Rmax
 
bool optimizeAlignment
 
bool optimizeDeformation
 
bool optimizeDefocus
 
bool ignoreCTF
 
bool optimizeRadius
 
bool phaseFlipped
 
double lambda
 
int RmaxDef
 
Matrix1D< double > p
 
int flagEnabled
 
int device
 
bool resume
 
MultidimArray< int > mask2D
 
MultidimArray< int > V_mask
 
size_t Xdim
 
Image< double > V
 
Image< double > Vdeformed
 
Image< double > I
 
Image< double > Ip
 
Image< double > Ifiltered
 
Image< double > Ifilteredp
 
Image< double > P
 
std::unique_ptr< FourierFilterfilter
 
Matrix2D< double > A
 
double old_rot
 
double old_tilt
 
double old_psi
 
double old_shiftX
 
double old_shiftY
 
bool old_flip
 
bool hasCTF
 
double old_defocusU
 
double old_defocusV
 
double old_defocusAngle
 
double currentDefocusU
 
double currentDefocusV
 
double currentAngle
 
std::unique_ptr< CTFDescriptionctf
 
std::unique_ptr< FourierFilterFilterCTF
 
int vecSize
 
Matrix1D< double > clnm
 
Matrix1D< double > steps_cp
 
double totalDeformation
 
double sumV
 
double sumVd
 
bool showOptimization
 
double correlation
 
AngularAlignmentGpu::AngularSphAlignment angularAlignGpu
 
int onesInSteps
 
bool useFakeKernel
 
Matrix2D< double > R
 
- 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
 

Protected Member Functions

virtual void createWorkFiles ()
 
- Protected Member Functions inherited from XmippMetadataProgram
virtual void initComments ()
 
virtual void postProcess ()
 
virtual bool getImageToProcess (size_t &objId, size_t &objIndex)
 
void show () const override
 
virtual void startProcessing ()
 
virtual void writeOutput ()
 
virtual void showProgress ()
 
virtual void defineLabelParam ()
 
- Protected Member Functions inherited from XmippProgram
void defineCommons ()
 
- Protected Member Functions inherited from Rerunable
 Rerunable (const FileName &fn)
 
virtual void createWorkFiles (bool resume, MetaData *md)
 
const FileNamegetFileName () const
 
void setFileName (const FileName &fn)
 

Additional Inherited Members

- 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

Predict Continuous Parameters.

Definition at line 49 of file angular_sph_alignment_gpu.h.

Constructor & Destructor Documentation

◆ ProgAngularSphAlignmentGpu()

ProgAngularSphAlignmentGpu::ProgAngularSphAlignmentGpu ( )

Empty constructor.

Definition at line 39 of file angular_sph_alignment_gpu.cpp.

39  : Rerunable("")
40 {
41  resume = false;
42  produces_a_metadata = true;
44  showOptimization = false;
45 }
bool each_image_produces_an_output
Indicate that an output is produced for each image in the input.
Rerunable(const FileName &fn)
bool produces_a_metadata
Indicate that the unique final output file is a Metadata.

◆ ~ProgAngularSphAlignmentGpu()

ProgAngularSphAlignmentGpu::~ProgAngularSphAlignmentGpu ( )
default

Destructor.

Member Function Documentation

◆ createWorkFiles()

virtual void ProgAngularSphAlignmentGpu::createWorkFiles ( )
inlineprotectedvirtual

Definition at line 199 of file angular_sph_alignment_gpu.h.

199  {
201  }
virtual void createWorkFiles(bool resume, MetaData *md)

◆ defineParams()

void ProgAngularSphAlignmentGpu::defineParams ( )
virtual

Define parameters.

Reimplemented from XmippMetadataProgram.

Definition at line 103 of file angular_sph_alignment_gpu.cpp.

104 {
105  addUsageLine("Make a continuous angular assignment with deformations");
106  defaultComments["-i"].clear();
107  defaultComments["-i"].addComment("Metadata with initial alignment");
108  defaultComments["-o"].clear();
109  defaultComments["-o"].addComment("Metadata with the angular alignment and deformation parameters");
111  addParamsLine(" --ref <volume> : Reference volume");
112  addParamsLine(" [--mask <m=\"\">] : Reference volume mask");
113  addParamsLine(" [--odir <outputDir=\".\">] : Output directory");
114  addParamsLine(" [--max_shift <s=-1>] : Maximum shift allowed in pixels");
115  addParamsLine(" [--max_angular_change <a=5>] : Maximum angular change allowed (in degrees)");
116  addParamsLine(" [--max_resolution <f=4>] : Maximum resolution (A)");
117  addParamsLine(" [--sampling <Ts=1>] : Sampling rate (A/pixel)");
118  addParamsLine(" [--Rmax <R=-1>] : Maximum radius (px). -1=Half of volume size");
119  addParamsLine(" [--RDef <r=-1>] : Maximum radius of the deformation (px). -1=Half of volume size");
120  addParamsLine(" [--l1 <l1=3>] : Degree Zernike Polynomials=1,2,3,...");
121  addParamsLine(" [--l2 <l2=2>] : Harmonical depth of the deformation=1,2,3,...");
122  addParamsLine(" [--optimizeAlignment] : Optimize alignment");
123  addParamsLine(" [--optimizeDeformation] : Optimize deformation");
124  addParamsLine(" [--optimizeDefocus] : Optimize defocus");
125  addParamsLine(" [--phaseFlipped] : Input images have been phase flipped");
126  addParamsLine(" [--regularization <l=0.01>] : Regularization weight");
127  addParamsLine(" [--resume] : Resume processing");
128  addParamsLine(" [--useCPU] : Uses fake kernel (CPU) instead of GPU kernel");
129  addParamsLine(" [--device <dev=0>] : GPU device to use. 0th by default");
130  addExampleLine("A typical use is:",false);
131  addExampleLine("xmipp_cuda_angular_sph_alignment -i anglesFromContinuousAssignment.xmd --ref reference.vol -o assigned_anglesAndDeformations.xmd --optimizeAlignment --optimizeDeformation --depth 1");
132 }
void addExampleLine(const char *example, bool verbatim=true)
void addUsageLine(const char *line, bool verbatim=false)
void addParamsLine(const String &line)
std::map< String, CommentList > defaultComments
Definition: xmipp_program.h:83

◆ deformVol()

void ProgAngularSphAlignmentGpu::deformVol ( MultidimArray< double > &  mVD,
const MultidimArray< double > &  mV,
double &  def,
double  rot,
double  tilt,
double  psi 
)

Deform a volumen using Zernike-Spherical harmonic basis.

Definition at line 554 of file angular_sph_alignment_gpu.cpp.

561 {
562  size_t idxY0=(VEC_XSIZE(clnm)-8)/3;
563 
564  // Rotation Matrix
565  R.initIdentity(3);
566  Euler_angles2matrix(rot, tilt, psi, R, false);
567  R = R.inv();
568 
569  double RmaxF=RmaxDef;
570 
572 
574  //angularAlignGpu.runKernelTest(clnm, idxY0, RmaxF * RmaxF, 1.0/RmaxF, R, mV, steps_cp, vL1, vN, vL2, vM, V_mask, mP);
575 
577  auto outputs = angularAlignGpu.getOutputs();
578 
579  sumVd = outputs.sumVD;
580  def = sqrt(outputs.modg/outputs.count);
581  totalDeformation = def;
582 }
#define VEC_XSIZE(m)
Definition: matrix1d.h:77
void Euler_angles2matrix(T alpha, T beta, T gamma, Matrix2D< T > &A, bool homogeneous)
Definition: geometry.cpp:624
void sqrt(Image< double > &op)
void inv(Matrix2D< T > &result) const
Definition: matrix2d.cpp:663
AngularAlignmentGpu::AngularSphAlignment angularAlignGpu
double psi(const double x)
void initIdentity()
Definition: matrix2d.h:673

◆ fillVectorTerms()

void ProgAngularSphAlignmentGpu::fillVectorTerms ( int  l1,
int  l2,
Matrix1D< int > &  vL1,
Matrix1D< int > &  vN,
Matrix1D< int > &  vL2,
Matrix1D< int > &  vM 
)

Zernike and SPH coefficients allocation.

Definition at line 522 of file angular_sph_alignment_gpu.cpp.

524 {
525  int idx = 0;
526  vL1.initZeros(vecSize);
527  vN.initZeros(vecSize);
528  vL2.initZeros(vecSize);
529  vM.initZeros(vecSize);
530  for (int h=0; h<=l2; h++) {
531  int totalSPH = 2*h+1;
532  int aux = std::floor(totalSPH/2);
533  for (int l=h; l<=l1; l+=2) {
534  for (int m=0; m<totalSPH; m++) {
535  VEC_ELEM(vL1,idx) = l;
536  VEC_ELEM(vN,idx) = h;
537  VEC_ELEM(vL2,idx) = h;
538  VEC_ELEM(vM,idx) = m-aux;
539  idx++;
540  }
541  }
542  }
543 }
#define VEC_ELEM(v, i)
Definition: matrix1d.h:245
__host__ __device__ float2 floor(const float2 v)
void initZeros()
Definition: matrix1d.h:592
int m

◆ finishProcessing()

void ProgAngularSphAlignmentGpu::finishProcessing ( )
virtual

Write the final parameters.

Reimplemented from XmippMetadataProgram.

Definition at line 211 of file angular_sph_alignment_gpu.cpp.

211  {
213  rename(Rerunable::getFileName().c_str(), fn_out.c_str());
214 }
const FileName & getFileName() const

◆ minimizepos()

void ProgAngularSphAlignmentGpu::minimizepos ( int  L1,
int  l2,
Matrix1D< double > &  steps 
)

Determine the positions to be minimize of a vector containing spherical harmonic coefficients.

Definition at line 509 of file angular_sph_alignment_gpu.cpp.

510 {
511  int size = 0;
512  numCoefficients(L1,l2,size);
513  onesInSteps = size;
514  int totalSize = (steps.size()-8)/3;
515  for (int idx=0; idx<size; idx++) {
516  VEC_ELEM(steps,idx) = 1.;
517  VEC_ELEM(steps,idx+totalSize) = 1.;
518  VEC_ELEM(steps,idx+2*totalSize) = 1.;
519  }
520 }
#define VEC_ELEM(v, i)
Definition: matrix1d.h:245
size_t size() const
Definition: matrix1d.h:508
void numCoefficients(int l1, int l2, int &vecSize)
Length of coefficients vector.

◆ numCoefficients()

void ProgAngularSphAlignmentGpu::numCoefficients ( int  l1,
int  l2,
int &  vecSize 
)

Length of coefficients vector.

Definition at line 493 of file angular_sph_alignment_gpu.cpp.

494 {
495  for (int h=0; h<=l2; h++)
496  {
497  int numSPH = 2*h+1;
498  int count=l1-h+1;
499  int numEven=(count>>1)+(count&1 && !(h&1));
500  if (h%2 == 0) {
501  vecSize += numSPH*numEven;
502  }
503  else {
504  vecSize += numSPH*(l1-h+1-numEven);
505  }
506  }
507 }

◆ preProcess()

void ProgAngularSphAlignmentGpu::preProcess ( )
virtual

Produce side info. An exception is thrown if any of the files is not found

Reimplemented from XmippMetadataProgram.

Definition at line 136 of file angular_sph_alignment_gpu.cpp.

137 {
138  V.read(fnVolR);
139  V().setXmippOrigin();
140  Xdim=XSIZE(V());
141  Vdeformed().initZeros(V());
142  // sumV=V().sum();
143 
144  Ifilteredp().initZeros(Xdim,Xdim);
145  Ifilteredp().setXmippOrigin();
146 
147  if (RmaxDef<0)
148  RmaxDef = Xdim/2;
149 
150  // Read Reference mask if avalaible (otherwise sphere of radius RmaxDef is used)
151  Mask mask;
152  mask.type = BINARY_CIRCULAR_MASK;
153  mask.mode = INNER_MASK;
154  if (fnMaskR != "") {
155  Image<double> aux;
156  aux.read(fnMaskR);
157  typeCast(aux(), V_mask);
159  }
160  else {
161  mask.R1 = RmaxDef;
162  mask.generate_mask(V());
163  V_mask = mask.get_binary_mask();
165  }
166 
167  // Total Volume Mass (Inside Mask)
168  sumV = 0.0;
170  if (DIRECT_MULTIDIM_ELEM(V_mask,n) == 1) {
172  }
173  }
174 
175  // Construct mask
176  if (Rmax<0)
177  Rmax=Xdim/2;
178  mask.R1 = Rmax;
179  mask.generate_mask(Xdim,Xdim);
180  mask2D=mask.get_binary_mask();
181 
182  // Define pointers
183  filter = std::make_unique<FourierFilter>();
184  FilterCTF = std::make_unique<FourierFilter>();
185  ctf = std::make_unique<CTFDescription>();
186 
187  // Low pass filter
188  std::cout << "Preprocess begins" << std::endl;
189  filter->FilterBand=LOWPASS;
190  filter->w1=Ts/maxResol;
191  filter->raised_w=0.02;
192 
193  // Transformation matrix
194  A.initIdentity(3);
195 
196  // CTF Filter
197  FilterCTF->FilterBand = CTF;
198  FilterCTF->ctf.enable_CTFnoise = false;
199  FilterCTF->ctf.produceSideInfo();
200 
201  vecSize = 0;
204 
205  createWorkFiles();
206 
207  // GPU preparation
209 }
void fillVectorTerms(int l1, int l2, Matrix1D< int > &vL1, Matrix1D< int > &vN, Matrix1D< int > &vL2, Matrix1D< int > &vM)
Zernike and SPH coefficients allocation.
Definition: mask.h:360
AngularAlignmentGpu::AngularSphAlignment angularAlignGpu
#define CTF
void numCoefficients(int l1, int l2, int &vecSize)
Length of coefficients vector.
double R1
Definition: mask.h:413
void associateWith(ProgAngularSphAlignmentGpu *prog)
#define XSIZE(v)
#define FOR_ALL_DIRECT_ELEMENTS_IN_MULTIDIMARRAY(v)
int type
Definition: mask.h:402
#define DIRECT_MULTIDIM_ELEM(v, n)
void generate_mask(bool apply_geo=false)
Definition: mask.cpp:1577
void typeCast(const Matrix1D< T1 > &v1, Matrix1D< T2 > &v2)
Definition: matrix1d.h:1227
#define BINARY_CIRCULAR_MASK
Definition: mask.h:365
std::unique_ptr< CTFDescription > ctf
int read(const FileName &name, DataMode datamode=DATA, size_t select_img=ALL_IMAGES, bool mapData=false, int mode=WRITE_READONLY)
const MultidimArray< int > & get_binary_mask() const
Definition: mask.h:707
std::unique_ptr< FourierFilter > FilterCTF
int * n
#define LOWPASS
void initIdentity()
Definition: matrix2d.h:673
int mode
Definition: mask.h:407
std::unique_ptr< FourierFilter > filter
constexpr int INNER_MASK
Definition: mask.h:47

◆ processImage()

void ProgAngularSphAlignmentGpu::processImage ( const FileName fnImg,
const FileName fnImgOut,
const MDRow rowIn,
MDRow rowOut 
)
virtual

Predict angles and shift. At the input the pose parameters must have an initial guess of the parameters. At the output they have the estimated pose.

Implements XmippMetadataProgram.

Definition at line 327 of file angular_sph_alignment_gpu.cpp.

328 {
330  int totalSize = 3*vecSize+8;
331  p.initZeros(totalSize);
332  clnm.initZeros(totalSize);
333 
334  rowOut=rowIn;
335 
336  flagEnabled=1;
337 
338  // Read input image and initial parameters
339 // ApplyGeoParams geoParams;
340 // geoParams.only_apply_shifts=false;
341 // geoParams.wrap=DONT_WRAP;
342 
348  if (rowIn.containsLabel(MDL_FLIP))
349  rowIn.getValue(MDL_FLIP,old_flip);
350  else
351  old_flip = false;
352 
354  {
355  hasCTF=true;
356  ctf->readFromMdRow(rowIn);
357  ctf->Tm = Ts;
358  ctf->produceSideInfo();
359  old_defocusU=ctf->DeltafU;
360  old_defocusV=ctf->DeltafV;
361  old_defocusAngle=ctf->azimuthal_angle;
362  }
363  else
364  hasCTF=false;
365 
366  if (verbose>=2)
367  std::cout << "Processing " << fnImg << std::endl;
368  I.read(fnImg);
369  I().setXmippOrigin();
370 
371  Ifiltered()=I();
372  filter->applyMaskSpace(Ifiltered());
373 
374  // GPU preparation
376 
377  for (int h=1;h<=L2;h++)
378  {
379  if (verbose>=2)
380  {
381  std::cout<<std::endl;
382  std::cout<<"------------------------------ Basis Degrees: ("<<L1<<","<<h<<") ----------------------------"<<std::endl;
383  }
384  steps.clear();
385  steps.initZeros(totalSize);
386 
387  // Optimize
388  double cost=-1;
389  try
390  {
391  cost=1e38;
392  int iter;
393  if (optimizeAlignment)
394  steps(totalSize-8)=steps(totalSize-7)=steps(totalSize-6)=steps(totalSize-5)=steps(totalSize-4)=1.;
395  if (optimizeDefocus)
396  steps(totalSize-3)=steps(totalSize-2)=steps(totalSize-1)=1.;
398  {
399  minimizepos(L1,h,steps);
400  }
401  steps_cp = steps;
402  powellOptimizer(p, 1, totalSize, &continuousSphCost, this, 0.01, cost, iter, steps, verbose>=2);
403 
404  if (verbose>=3)
405  {
406  showOptimization = true;
408  showOptimization = false;
409  }
410 
411  if (cost>0)
412  {
413  flagEnabled=-1;
414  p.initZeros();
415  }
416  cost=-cost;
418  if (verbose>=2)
419  {
420  std::cout<<std::endl;
421  for (int j=1;j<4;j++)
422  {
423  switch (j)
424  {
425  case 1:
426  std::cout << "X Coefficients=(";
427  break;
428  case 2:
429  std::cout << "Y Coefficients=(";
430  break;
431  case 3:
432  std::cout << "Z Coefficients=(";
433  break;
434  }
435  for (int i=(j-1)*vecSize;i<j*vecSize;i++)
436  {
437  std::cout << p(i);
438  if (i<j*vecSize-1)
439  std::cout << ",";
440  }
441  std::cout << ")" << std::endl;
442  }
443  std::cout << "Radius=" << RmaxDef << std::endl;
444  std::cout << " Dshift=(" << p(totalSize-5) << "," << p(totalSize-4) << ") "
445  << "Drot=" << p(totalSize-3) << " Dtilt=" << p(totalSize-2)
446  << " Dpsi=" << p(totalSize-1) << std::endl;
447  std::cout << " Total deformation=" << totalDeformation << std::endl;
448  std::cout<<std::endl;
449  }
450  }
451  catch (XmippError &XE)
452  {
453  std::cerr << XE.what() << std::endl;
454  std::cerr << "Warning: Cannot refine " << fnImg << std::endl;
455  flagEnabled=-1;
456  }
457  }
458 
459  //AJ NEW
460  writeImageParameters(fnImg);
461  //END AJ
462 
463 }
Rotation angle of an image (double,degrees)
Defocus U (Angstroms)
void clear()
Definition: matrix1d.cpp:67
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.
Name for the CTF Model (std::string)
void powellOptimizer(Matrix1D< double > &p, int i0, int n, double(*f)(double *x, void *), void *prm, double ftol, double &fret, int &iter, const Matrix1D< double > &steps, bool show)
glob_prnt iter
#define i
AngularAlignmentGpu::AngularSphAlignment angularAlignGpu
T & getValue(MDLabel label)
Flip the image? (bool)
int verbose
Verbosity level.
void initZeros()
Definition: matrix1d.h:592
#define j
double steps
double continuousSphCost(double *x, void *_prm)
void minimizepos(int L1, int l2, Matrix1D< double > &steps)
Determine the positions to be minimize of a vector containing spherical harmonic coefficients.
virtual bool containsLabel(MDLabel label) const =0
std::unique_ptr< CTFDescription > ctf
double cost
Cost, scaled between 0 and 1.
Definition: micrograph.h:66
int read(const FileName &name, DataMode datamode=DATA, size_t select_img=ALL_IMAGES, bool mapData=false, int mode=WRITE_READONLY)
Shift for the image in the Y axis (double)
T * adaptForNumericalRecipes() const
Definition: matrix1d.h:844
std::unique_ptr< FourierFilter > filter
virtual void writeImageParameters(const FileName &fnImg)

◆ readParams()

void ProgAngularSphAlignmentGpu::readParams ( )
virtual

Read argument from command line.

Reimplemented from XmippMetadataProgram.

Definition at line 50 of file angular_sph_alignment_gpu.cpp.

51 {
53  fnVolR = getParam("--ref");
54  fnMaskR = getParam("--mask");
55  fnOutDir = getParam("--odir");
56  Rerunable::setFileName(fnOutDir+"/sphDone.xmd");
57  maxShift = getDoubleParam("--max_shift");
58  maxAngularChange = getDoubleParam("--max_angular_change");
59  maxResol = getDoubleParam("--max_resolution");
60  Ts = getDoubleParam("--sampling");
61  Rmax = getIntParam("--Rmax");
62  RmaxDef = getIntParam("--RDef");
63  optimizeAlignment = checkParam("--optimizeAlignment");
64  optimizeDeformation = checkParam("--optimizeDeformation");
65  optimizeDefocus = checkParam("--optimizeDefocus");
66  phaseFlipped = checkParam("--phaseFlipped");
67  L1 = getIntParam("--l1");
68  L2 = getIntParam("--l2");
69  lambda = getDoubleParam("--regularization");
70  resume = checkParam("--resume");
71  useFakeKernel = checkParam("--useCPU");
72  device = getIntParam("--device");
73 }
double getDoubleParam(const char *param, int arg=0)
FileName fnOutDir
Output directory.
void setFileName(const FileName &fn)
const char * getParam(const char *param, int arg=0)
bool checkParam(const char *param)
int getIntParam(const char *param, int arg=0)

◆ show()

void ProgAngularSphAlignmentGpu::show ( ) const
overridevirtual

Show.

Reimplemented from XmippProgram.

Definition at line 76 of file angular_sph_alignment_gpu.cpp.

77 {
78  if (!verbose)
79  return;
81  std::cout
82  << "Output directory: " << fnOutDir << std::endl
83  << "Reference volume: " << fnVolR << std::endl
84  << "Reference mask: " << fnMaskR << std::endl
85  << "Max. Shift: " << maxShift << std::endl
86  << "Max. Angular Change: " << maxAngularChange << std::endl
87  << "Max. Resolution: " << maxResol << std::endl
88  << "Sampling: " << Ts << std::endl
89  << "Max. Radius: " << Rmax << std::endl
90  << "Max. Radius Deform. " << RmaxDef << std::endl
91  << "Zernike Degree: " << L1 << std::endl
92  << "SH Degree: " << L2 << std::endl
93  << "Optimize alignment: " << optimizeAlignment << std::endl
94  << "Optimize deformation:" << optimizeDeformation<< std::endl
95  << "Optimize defocus; " << optimizeDefocus << std::endl
96  << "Phase flipped: " << phaseFlipped << std::endl
97  << "Regularization: " << lambda << std::endl
98  << "Device: " << device << std::endl
99  ;
100 }
FileName fnOutDir
Output directory.
int verbose
Verbosity level.
void show() const override

◆ tranformImageSph()

double ProgAngularSphAlignmentGpu::tranformImageSph ( double *  pclnm,
double  rot,
double  tilt,
double  psi,
Matrix2D< double > &  A,
double  deltaDefocusU,
double  deltaDefocusV,
double  deltaDefocusAngle 
)

Definition at line 217 of file angular_sph_alignment_gpu.cpp.

220 {
221  const MultidimArray<double> &mV=V();
223  VEC_ELEM(clnm,i)=pclnm[i+1];
224  double deformation=0.0;
225  totalDeformation=0.0;
226  P().initZeros((int)XSIZE(I()),(int)XSIZE(I()));
227  P().setXmippOrigin();
228  deformVol(P(), mV, deformation, rot, tilt, psi);
229  if (hasCTF)
230  {
231  double defocusU=old_defocusU+deltaDefocusU;
232  double defocusV=old_defocusV+deltaDefocusV;
233  double angle=old_defocusAngle+deltaDefocusAngle;
234  if (defocusU!=currentDefocusU || defocusV!=currentDefocusV || angle!=currentAngle) {
235  updateCTFImage(defocusU,defocusV,angle);
236  }
237  FilterCTF->ctf = *ctf;
238  FilterCTF->generateMask(P());
239  if (phaseFlipped)
240  FilterCTF->correctPhase();
241  FilterCTF->applyMaskSpace(P());
242  }
243  double cost=0;
244  if (old_flip)
245  {
246  MAT_ELEM(A,0,0)*=-1;
247  MAT_ELEM(A,0,1)*=-1;
248  MAT_ELEM(A,0,2)*=-1;
249  }
250 
251  applyGeometry(xmipp_transformation::LINEAR,Ifilteredp(),Ifiltered(),A,xmipp_transformation::IS_NOT_INV,xmipp_transformation::DONT_WRAP,0.);
252  filter->applyMaskSpace(P());
253  const MultidimArray<double> &mP=P();
254  const MultidimArray<int> &mMask2D=mask2D;
255  MultidimArray<double> &mIfilteredp=Ifilteredp();
256  double corr=correlationIndex(mIfilteredp,mP,&mMask2D);
257  cost=-corr;
258 
259 #ifdef DEBUG
260  std::cout << "A=" << A << std::endl;
261  Image<double> save;
262  save()=P();
263  save.write("PPPtheo.xmp");
264  save()=Ifilteredp();
265  save.write("PPPfilteredp.xmp");
266  save()=Ifiltered();
267  save.write("PPPfiltered.xmp");
268  // Vdeformed.write("PPPVdeformed.vol");
269  std::cout << "Cost=" << cost << " deformation=" << deformation << std::endl;
270  std::cout << "Press any key" << std::endl;
271  char c; std::cin >> c;
272 #endif
273 
274  if (showOptimization)
275  {
276  std::cout << "A=" << A << std::endl;
277  Image<double> save(P);
278  save.write("PPPtheo.xmp");
279  save()=Ifilteredp();
280  save.write("PPPfilteredp.xmp");
281  save()=Ifiltered();
282  save.write("PPPfiltered.xmp");
283  // Vdeformed.write("PPPVdeformed.vol");
284  std::cout << "Cost=" << cost << " corr=" << corr << std::endl;
285  std::cout << "Deformation=" << totalDeformation << std::endl;
286  std::cout << "Press any key" << std::endl;
287  char c; std::cin >> c;
288  }
289 
290  double massDiff=std::abs(sumV-sumVd)/sumV;
291  double retval=cost+lambda*(deformation + massDiff);
292  if (showOptimization)
293  std::cout << cost << " " << deformation << " " << lambda*deformation << " " << sumV << " " << sumVd << " " << massDiff << " " << retval << std::endl;
294  return retval;
295 }
#define VEC_ELEM(v, i)
Definition: matrix1d.h:245
doublereal * c
void applyGeometry(int SplineDegree, MultidimArray< std::complex< double > > &V2, const MultidimArray< std::complex< double > > &V1, const Matrix2D< double > &A, bool inv, bool wrap, std::complex< double > outside, MultidimArray< double > *BcoeffsPtr)
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)
double correlationIndex(const MultidimArray< T > &x, const MultidimArray< T > &y, const MultidimArray< int > *mask=NULL, MultidimArray< double > *Contributions=NULL)
void abs(Image< double > &op)
void deformVol(MultidimArray< double > &mVD, const MultidimArray< double > &mV, double &def, double rot, double tilt, double psi)
Deform a volumen using Zernike-Spherical harmonic basis.
#define i
#define MAT_ELEM(m, i, j)
Definition: matrix2d.h:116
#define FOR_ALL_ELEMENTS_IN_MATRIX1D(v)
Definition: matrix1d.h:72
void updateCTFImage(double defocusU, double defocusV, double angle)
#define XSIZE(v)
double psi(const double x)
std::unique_ptr< CTFDescription > ctf
double cost
Cost, scaled between 0 and 1.
Definition: micrograph.h:66
std::unique_ptr< FourierFilter > FilterCTF
std::unique_ptr< FourierFilter > filter

◆ updateCTFImage()

void ProgAngularSphAlignmentGpu::updateCTFImage ( double  defocusU,
double  defocusV,
double  angle 
)

Definition at line 545 of file angular_sph_alignment_gpu.cpp.

546 {
547  ctf->K=1; // get pure CTF with no envelope
548  currentDefocusU=ctf->DeltafU=defocusU;
549  currentDefocusV=ctf->DeltafV=defocusV;
550  currentAngle=ctf->azimuthal_angle=angle;
551  ctf->produceSideInfo();
552 }
std::unique_ptr< CTFDescription > ctf

◆ writeImageParameters()

void ProgAngularSphAlignmentGpu::writeImageParameters ( const FileName fnImg)
virtual

Write the parameters found for one image

Definition at line 466 of file angular_sph_alignment_gpu.cpp.

466  {
467  MetaDataVec md;
468  int pos = 3*vecSize;
469  size_t objId = md.addObject();
470  md.setValue(MDL_IMAGE, fnImg, objId);
471  if (flagEnabled==1) {
472  md.setValue(MDL_ENABLED, 1, objId);
473  }
474  else {
475  md.setValue(MDL_ENABLED, -1, objId);
476  }
477  md.setValue(MDL_ANGLE_ROT, old_rot+p(pos+2), objId);
478  md.setValue(MDL_ANGLE_TILT, old_tilt+p(pos+3), objId);
479  md.setValue(MDL_ANGLE_PSI, old_psi+p(pos+4), objId);
480  md.setValue(MDL_SHIFT_X, old_shiftX+p(pos+0), objId);
481  md.setValue(MDL_SHIFT_Y, old_shiftY+p(pos+1), objId);
482  md.setValue(MDL_FLIP, old_flip, objId);
484  std::vector<double> vectortemp;
485  for (int j = 0; j < VEC_XSIZE(clnm); j++) {
486  vectortemp.push_back(clnm(j));
487  }
488  md.setValue(MDL_SPH_COEFFICIENTS, vectortemp, objId);
489  md.setValue(MDL_COST, correlation, objId);
491 }
Rotation angle of an image (double,degrees)
#define VEC_XSIZE(m)
Definition: matrix1d.h:77
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.
Is this image enabled? (int [-1 or 1])
const FileName & getFileName() const
bool setValue(const MDObject &mdValueIn, size_t id)
size_t addObject() override
Deformation in voxels.
Cost for the image (double)
Flip the image? (bool)
#define j
void append(const FileName &outFile) const
Deformation coefficients.
Shift for the image in the Y axis (double)
Name of an image (std::string)

Member Data Documentation

◆ A

Matrix2D<double> ProgAngularSphAlignmentGpu::A

Definition at line 109 of file angular_sph_alignment_gpu.h.

◆ angularAlignGpu

AngularAlignmentGpu::AngularSphAlignment ProgAngularSphAlignmentGpu::angularAlignGpu

Definition at line 139 of file angular_sph_alignment_gpu.h.

◆ clnm

Matrix1D<double> ProgAngularSphAlignmentGpu::clnm

Definition at line 129 of file angular_sph_alignment_gpu.h.

◆ correlation

double ProgAngularSphAlignmentGpu::correlation

Definition at line 137 of file angular_sph_alignment_gpu.h.

◆ ctf

std::unique_ptr<CTFDescription> ProgAngularSphAlignmentGpu::ctf

Definition at line 123 of file angular_sph_alignment_gpu.h.

◆ currentAngle

double ProgAngularSphAlignmentGpu::currentAngle

Definition at line 121 of file angular_sph_alignment_gpu.h.

◆ currentDefocusU

double ProgAngularSphAlignmentGpu::currentDefocusU

Definition at line 121 of file angular_sph_alignment_gpu.h.

◆ currentDefocusV

double ProgAngularSphAlignmentGpu::currentDefocusV

Definition at line 121 of file angular_sph_alignment_gpu.h.

◆ device

int ProgAngularSphAlignmentGpu::device

Definition at line 93 of file angular_sph_alignment_gpu.h.

◆ filter

std::unique_ptr<FourierFilter> ProgAngularSphAlignmentGpu::filter

Definition at line 107 of file angular_sph_alignment_gpu.h.

◆ FilterCTF

std::unique_ptr<FourierFilter> ProgAngularSphAlignmentGpu::FilterCTF

Definition at line 125 of file angular_sph_alignment_gpu.h.

◆ flagEnabled

int ProgAngularSphAlignmentGpu::flagEnabled

Definition at line 90 of file angular_sph_alignment_gpu.h.

◆ fnMaskR

FileName ProgAngularSphAlignmentGpu::fnMaskR

Filename of the reference volume mask

Definition at line 55 of file angular_sph_alignment_gpu.h.

◆ fnOutDir

FileName ProgAngularSphAlignmentGpu::fnOutDir

Output directory.

Definition at line 57 of file angular_sph_alignment_gpu.h.

◆ fnVolR

FileName ProgAngularSphAlignmentGpu::fnVolR

Filename of the reference volume

Definition at line 53 of file angular_sph_alignment_gpu.h.

◆ hasCTF

bool ProgAngularSphAlignmentGpu::hasCTF

Definition at line 117 of file angular_sph_alignment_gpu.h.

◆ I

Image<double> ProgAngularSphAlignmentGpu::I

Definition at line 103 of file angular_sph_alignment_gpu.h.

◆ Ifiltered

Image<double> ProgAngularSphAlignmentGpu::Ifiltered

Definition at line 103 of file angular_sph_alignment_gpu.h.

◆ Ifilteredp

Image<double> ProgAngularSphAlignmentGpu::Ifilteredp

Definition at line 103 of file angular_sph_alignment_gpu.h.

◆ ignoreCTF

bool ProgAngularSphAlignmentGpu::ignoreCTF

Definition at line 79 of file angular_sph_alignment_gpu.h.

◆ Ip

Image<double> ProgAngularSphAlignmentGpu::Ip

Definition at line 103 of file angular_sph_alignment_gpu.h.

◆ L1

int ProgAngularSphAlignmentGpu::L1

Degrees of Zernike polynomials and spherical harmonics

Definition at line 59 of file angular_sph_alignment_gpu.h.

◆ L2

int ProgAngularSphAlignmentGpu::L2

Definition at line 59 of file angular_sph_alignment_gpu.h.

◆ lambda

double ProgAngularSphAlignmentGpu::lambda

Definition at line 85 of file angular_sph_alignment_gpu.h.

◆ mask2D

MultidimArray<int> ProgAngularSphAlignmentGpu::mask2D

Definition at line 99 of file angular_sph_alignment_gpu.h.

◆ maxAngularChange

double ProgAngularSphAlignmentGpu::maxAngularChange

Maximum angular change allowed

Definition at line 65 of file angular_sph_alignment_gpu.h.

◆ maxResol

double ProgAngularSphAlignmentGpu::maxResol

Maximum frequency (A)

Definition at line 67 of file angular_sph_alignment_gpu.h.

◆ maxShift

double ProgAngularSphAlignmentGpu::maxShift

Maximum shift allowed

Definition at line 63 of file angular_sph_alignment_gpu.h.

◆ old_defocusAngle

double ProgAngularSphAlignmentGpu::old_defocusAngle

Definition at line 119 of file angular_sph_alignment_gpu.h.

◆ old_defocusU

double ProgAngularSphAlignmentGpu::old_defocusU

Definition at line 119 of file angular_sph_alignment_gpu.h.

◆ old_defocusV

double ProgAngularSphAlignmentGpu::old_defocusV

Definition at line 119 of file angular_sph_alignment_gpu.h.

◆ old_flip

bool ProgAngularSphAlignmentGpu::old_flip

Definition at line 115 of file angular_sph_alignment_gpu.h.

◆ old_psi

double ProgAngularSphAlignmentGpu::old_psi

Definition at line 111 of file angular_sph_alignment_gpu.h.

◆ old_rot

double ProgAngularSphAlignmentGpu::old_rot

Definition at line 111 of file angular_sph_alignment_gpu.h.

◆ old_shiftX

double ProgAngularSphAlignmentGpu::old_shiftX

Definition at line 113 of file angular_sph_alignment_gpu.h.

◆ old_shiftY

double ProgAngularSphAlignmentGpu::old_shiftY

Definition at line 113 of file angular_sph_alignment_gpu.h.

◆ old_tilt

double ProgAngularSphAlignmentGpu::old_tilt

Definition at line 111 of file angular_sph_alignment_gpu.h.

◆ onesInSteps

int ProgAngularSphAlignmentGpu::onesInSteps

Definition at line 141 of file angular_sph_alignment_gpu.h.

◆ optimizeAlignment

bool ProgAngularSphAlignmentGpu::optimizeAlignment

Definition at line 73 of file angular_sph_alignment_gpu.h.

◆ optimizeDefocus

bool ProgAngularSphAlignmentGpu::optimizeDefocus

Definition at line 77 of file angular_sph_alignment_gpu.h.

◆ optimizeDeformation

bool ProgAngularSphAlignmentGpu::optimizeDeformation

Definition at line 75 of file angular_sph_alignment_gpu.h.

◆ optimizeRadius

bool ProgAngularSphAlignmentGpu::optimizeRadius

Definition at line 81 of file angular_sph_alignment_gpu.h.

◆ p

Matrix1D<double> ProgAngularSphAlignmentGpu::p

Definition at line 89 of file angular_sph_alignment_gpu.h.

◆ P

Image<double> ProgAngularSphAlignmentGpu::P

Definition at line 105 of file angular_sph_alignment_gpu.h.

◆ phaseFlipped

bool ProgAngularSphAlignmentGpu::phaseFlipped

Definition at line 83 of file angular_sph_alignment_gpu.h.

◆ R

Matrix2D<double> ProgAngularSphAlignmentGpu::R

Definition at line 145 of file angular_sph_alignment_gpu.h.

◆ resume

bool ProgAngularSphAlignmentGpu::resume

Resume computations

Definition at line 97 of file angular_sph_alignment_gpu.h.

◆ Rmax

int ProgAngularSphAlignmentGpu::Rmax

Maximum radius

Definition at line 71 of file angular_sph_alignment_gpu.h.

◆ RmaxDef

int ProgAngularSphAlignmentGpu::RmaxDef

Definition at line 87 of file angular_sph_alignment_gpu.h.

◆ showOptimization

bool ProgAngularSphAlignmentGpu::showOptimization

Definition at line 135 of file angular_sph_alignment_gpu.h.

◆ steps_cp

Matrix1D<double> ProgAngularSphAlignmentGpu::steps_cp

Definition at line 131 of file angular_sph_alignment_gpu.h.

◆ sumV

double ProgAngularSphAlignmentGpu::sumV

Definition at line 133 of file angular_sph_alignment_gpu.h.

◆ sumVd

double ProgAngularSphAlignmentGpu::sumVd

Definition at line 133 of file angular_sph_alignment_gpu.h.

◆ totalDeformation

double ProgAngularSphAlignmentGpu::totalDeformation

Definition at line 133 of file angular_sph_alignment_gpu.h.

◆ Ts

double ProgAngularSphAlignmentGpu::Ts

Sampling rate

Definition at line 69 of file angular_sph_alignment_gpu.h.

◆ useFakeKernel

bool ProgAngularSphAlignmentGpu::useFakeKernel

Definition at line 143 of file angular_sph_alignment_gpu.h.

◆ V

Image<double> ProgAngularSphAlignmentGpu::V

Definition at line 103 of file angular_sph_alignment_gpu.h.

◆ V_mask

MultidimArray<int> ProgAngularSphAlignmentGpu::V_mask

Definition at line 99 of file angular_sph_alignment_gpu.h.

◆ Vdeformed

Image<double> ProgAngularSphAlignmentGpu::Vdeformed

Definition at line 103 of file angular_sph_alignment_gpu.h.

◆ vecSize

int ProgAngularSphAlignmentGpu::vecSize

Definition at line 127 of file angular_sph_alignment_gpu.h.

◆ vL1

Matrix1D<int> ProgAngularSphAlignmentGpu::vL1

Zernike and SPH coefficients vectors

Definition at line 61 of file angular_sph_alignment_gpu.h.

◆ vL2

Matrix1D<int> ProgAngularSphAlignmentGpu::vL2

Definition at line 61 of file angular_sph_alignment_gpu.h.

◆ vM

Matrix1D<int> ProgAngularSphAlignmentGpu::vM

Definition at line 61 of file angular_sph_alignment_gpu.h.

◆ vN

Matrix1D<int> ProgAngularSphAlignmentGpu::vN

Definition at line 61 of file angular_sph_alignment_gpu.h.

◆ Xdim

size_t ProgAngularSphAlignmentGpu::Xdim

Definition at line 101 of file angular_sph_alignment_gpu.h.


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