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

#include <angular_sph_alignment.h>

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

Public Member Functions

 ProgAngularSphAlignment ()
 Empty constructor. More...
 
 ~ProgAngularSphAlignment ()
 Destructor. More...
 
void readParams ()
 Read argument from command line. More...
 
void show ()
 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 &nc) const
 Length of coefficients vector. More...
 
void minimizepos (int l2, Matrix1D< double > &steps) const
 Determine the positions to be minimize of a vector containing spherical harmonic coefficients. More...
 
void fillVectorTerms (int l1, int l2)
 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 applyCTFImage (double const &deltaDefocusU, double const &deltaDefocusV, double const &deltaDefocusAngle)
 
void updateCTFImage (double defocusU, double defocusV, double angle)
 
double tranformImageSph (double *pclnm, double rot, double tilt, double psi, 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
 
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
 
FourierFilter filter
 
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
 
CTFDescription ctf
 
FourierFilter FilterCTF
 
int vecSize
 
Matrix1D< double > clnm
 
Matrix1D< double > steps_cp
 
double totalDeformation
 
double sumV
 
double sumVd
 
bool showOptimization
 
double correlation
 
- 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 42 of file angular_sph_alignment.h.

Constructor & Destructor Documentation

◆ ProgAngularSphAlignment()

ProgAngularSphAlignment::ProgAngularSphAlignment ( )

Empty constructor.

Definition at line 35 of file angular_sph_alignment.cpp.

35  : Rerunable("")
36 {
37  resume = false;
38  produces_a_metadata = true;
40  showOptimization = false;
41 }
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.

◆ ~ProgAngularSphAlignment()

ProgAngularSphAlignment::~ProgAngularSphAlignment ( )
default

Destructor.

Member Function Documentation

◆ applyCTFImage()

void ProgAngularSphAlignment::applyCTFImage ( double const &  deltaDefocusU,
double const &  deltaDefocusV,
double const &  deltaDefocusAngle 
)

Definition at line 506 of file angular_sph_alignment.cpp.

507  {
508  double defocusU=old_defocusU+deltaDefocusU;
509  double defocusV=old_defocusV+deltaDefocusV;
510  double angle=old_defocusAngle+deltaDefocusAngle;
511  if (defocusU!=currentDefocusU || defocusV!=currentDefocusV || angle!=currentAngle) {
512  updateCTFImage(defocusU,defocusV,angle);
513  }
514  FilterCTF.ctf = ctf;
516  if (phaseFlipped)
519 }
CTFDescription ctf
void updateCTFImage(double defocusU, double defocusV, double angle)
void generateMask(MultidimArray< double > &v)
void applyMaskSpace(MultidimArray< double > &v)

◆ createWorkFiles()

virtual void ProgAngularSphAlignment::createWorkFiles ( )
inlineprotectedvirtual

Reimplemented in MpiProgAngularSphAlignment.

Definition at line 201 of file angular_sph_alignment.h.

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

◆ defineParams()

void ProgAngularSphAlignment::defineParams ( )
virtual

Define parameters.

Reimplemented from XmippMetadataProgram.

Definition at line 96 of file angular_sph_alignment.cpp.

97 {
98  addUsageLine("Make a continuous angular assignment with deformations");
99  defaultComments["-i"].clear();
100  defaultComments["-i"].addComment("Metadata with initial alignment");
101  defaultComments["-o"].clear();
102  defaultComments["-o"].addComment("Metadata with the angular alignment and deformation parameters");
104  addParamsLine(" --ref <volume> : Reference volume");
105  addParamsLine(" [--mask <m=\"\">] : Reference volume mask");
106  addParamsLine(" [--odir <outputDir=\".\">] : Output directory");
107  addParamsLine(" [--max_shift <s=-1>] : Maximum shift allowed in pixels");
108  addParamsLine(" [--max_angular_change <a=5>] : Maximum angular change allowed (in degrees)");
109  addParamsLine(" [--max_resolution <f=4>] : Maximum resolution (A)");
110  addParamsLine(" [--sampling <Ts=1>] : Sampling rate (A/pixel)");
111  addParamsLine(" [--Rmax <R=-1>] : Maximum radius (px). -1=Half of volume size");
112  addParamsLine(" [--RDef <r=-1>] : Maximum radius of the deformation (px). -1=Half of volume size");
113  addParamsLine(" [--l1 <l1=3>] : Degree Zernike Polynomials=1,2,3,...");
114  addParamsLine(" [--l2 <l2=2>] : Harmonical depth of the deformation=1,2,3,...");
115  addParamsLine(" [--optimizeAlignment] : Optimize alignment");
116  addParamsLine(" [--optimizeDeformation] : Optimize deformation");
117  addParamsLine(" [--optimizeDefocus] : Optimize defocus");
118  addParamsLine(" [--phaseFlipped] : Input images have been phase flipped");
119  addParamsLine(" [--regularization <l=0.01>] : Regularization weight");
120  addParamsLine(" [--resume] : Resume processing");
121  addExampleLine("A typical use is:",false);
122  addExampleLine("xmipp_angular_sph_alignment -i anglesFromContinuousAssignment.xmd --ref reference.vol -o assigned_anglesAndDeformations.xmd --optimizeAlignment --optimizeDeformation --depth 1");
123 }
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 ProgAngularSphAlignment::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 530 of file angular_sph_alignment.cpp.

532 {
533  size_t idxY0=(VEC_XSIZE(clnm)-8)/3;
534  double Ncount=0.0;
535  double modg=0.0;
536  double diff2=0.0;
537 
538  def=0.0;
539  size_t idxZ0=2*idxY0;
540  sumVd=0.0;
541  double RmaxF=RmaxDef;
542  double RmaxF2=RmaxF*RmaxF;
543  double iRmaxF=1.0/RmaxF;
544  // Rotation Matrix
546  R.initIdentity(3);
547  Euler_angles2matrix(rot, tilt, psi, R, false);
548  R = R.inv();
549  Matrix1D<double> pos;
550  pos.initZeros(3);
551 
552  // TODO: Poner primero i y j en el loop, acumular suma y guardar al final
553  for (int k=STARTINGZ(mV); k<=FINISHINGZ(mV); k++)
554  {
555  for (int i=STARTINGY(mV); i<=FINISHINGY(mV); i++)
556  {
557  for (int j=STARTINGX(mV); j<=FINISHINGX(mV); j++)
558  {
559  ZZ(pos) = k; YY(pos) = i; XX(pos) = j;
560  pos = R * pos;
561  double gx=0.0;
562  double gy=0.0;
563  double gz=0.0;
564  // TODO: Sacar al bucle de z
565  double k2=ZZ(pos)*ZZ(pos);
566  double kr=ZZ(pos)*iRmaxF;
567  double k2i2=k2+YY(pos)*YY(pos);
568  double ir=YY(pos)*iRmaxF;
569  double r2=k2i2+XX(pos)*XX(pos);
570  double jr=XX(pos)*iRmaxF;
571  double rr=sqrt(r2)*iRmaxF;
572  if (r2<RmaxF2) {
573  for (size_t idx=0; idx<idxY0; idx++) {
574  if (VEC_ELEM(steps_cp,idx) == 1) {
575  double zsph=0.0;
576  int l1 = VEC_ELEM(vL1,idx);
577  int n = VEC_ELEM(vN,idx);
578  int l2 = VEC_ELEM(vL2,idx);
579  int m = VEC_ELEM(vM,idx);
580  zsph=ZernikeSphericalHarmonics(l1,n,l2,m,jr,ir,kr,rr);
581  if (rr>0 || l2==0) {
582  gx += VEC_ELEM(clnm,idx) *zsph;
583  gy += VEC_ELEM(clnm,idx+idxY0) *zsph;
584  gz += VEC_ELEM(clnm,idx+idxZ0) *zsph;
585  }
586  }
587  }
588  auto k_mask = (int)(ZZ(pos)+gz);
589  auto i_mask = (int)(YY(pos)+gy);
590  auto j_mask = (int)(XX(pos)+gx);
591  int voxelI_mask = 0;
592  if (!V_mask.outside(k_mask, i_mask, j_mask)) {
593  voxelI_mask = A3D_ELEM(V_mask, k_mask, i_mask, j_mask);
594  }
595  if (voxelI_mask == 1) {
596  double voxelI=mV.interpolatedElement3D(XX(pos)+gx,YY(pos)+gy,ZZ(pos)+gz);
597  A2D_ELEM(mP,i,j) += voxelI;
598  sumVd += voxelI;
599  modg += gx*gx+gy*gy+gz*gz;
600  Ncount++;
601  }
602  }
603  }
604  }
605  }
606 
607  def = sqrt(modg/Ncount);
608  totalDeformation = def;
609 }
#define A2D_ELEM(v, i, j)
#define VEC_ELEM(v, i)
Definition: matrix1d.h:245
#define FINISHINGX(v)
#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
#define FINISHINGZ(v)
#define STARTINGX(v)
#define i
ql0001_ & k(htemp+1),(cvec+1),(atemp+1),(bj+1),(bl+1),(bu+1),(x+1),(clamda+1), &iout, infoqp, &zero,(w+1), &lenw,(iw+1), &leniw, &glob_grd.epsmac
#define STARTINGY(v)
#define A3D_ELEM(V, k, i, j)
#define XX(v)
Definition: matrix1d.h:85
T interpolatedElement3D(double x, double y, double z, T outside_value=(T) 0) const
void initZeros()
Definition: matrix1d.h:592
#define j
#define YY(v)
Definition: matrix1d.h:93
int m
#define FINISHINGY(v)
bool outside(int k, int i, int j) const
double ZernikeSphericalHarmonics(int n, int m, double xr, double yr, double zr, double r)
double psi(const double x)
MultidimArray< int > V_mask
float r2
#define STARTINGZ(v)
int * n
#define ZZ(v)
Definition: matrix1d.h:101
int ir
void initIdentity()
Definition: matrix2d.h:673

◆ fillVectorTerms()

void ProgAngularSphAlignment::fillVectorTerms ( int  l1,
int  l2 
)

Zernike and SPH coefficients allocation.

Definition at line 484 of file angular_sph_alignment.cpp.

485 {
486  int idx = 0;
491  for (int h=0; h<=l2; h++) {
492  int totalSPH = 2*h+1;
493  auto aux = (int)(std::floor(totalSPH/2));
494  for (int l=h; l<=l1; l+=2) {
495  for (int m=0; m<totalSPH; m++) {
496  VEC_ELEM(vL1,idx) = l;
497  VEC_ELEM(vN,idx) = h;
498  VEC_ELEM(vL2,idx) = h;
499  VEC_ELEM(vM,idx) = m-aux;
500  idx++;
501  }
502  }
503  }
504 }
#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 ProgAngularSphAlignment::finishProcessing ( )
virtual

Write the final parameters.

Reimplemented from XmippMetadataProgram.

Reimplemented in MpiProgAngularSphAlignment.

Definition at line 190 of file angular_sph_alignment.cpp.

190  {
192  rename(Rerunable::getFileName().c_str(), fn_out.c_str());
193 }
const FileName & getFileName() const

◆ minimizepos()

void ProgAngularSphAlignment::minimizepos ( int  l2,
Matrix1D< double > &  steps 
) const

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

Definition at line 472 of file angular_sph_alignment.cpp.

473 {
474  int size = 0;
475  numCoefficients(L1,l2,size);
476  auto totalSize = (int)((steps.size()-8)/3);
477  for (int idx=0; idx<size; idx++) {
478  VEC_ELEM(steps,idx) = 1.;
479  VEC_ELEM(steps,idx+totalSize) = 1.;
480  VEC_ELEM(steps,idx+2*totalSize) = 1.;
481  }
482 }
#define VEC_ELEM(v, i)
Definition: matrix1d.h:245
size_t size() const
Definition: matrix1d.h:508
void numCoefficients(int l1, int l2, int &nc) const
Length of coefficients vector.

◆ numCoefficients()

void ProgAngularSphAlignment::numCoefficients ( int  l1,
int  l2,
int &  nc 
) const

Length of coefficients vector.

Definition at line 451 of file angular_sph_alignment.cpp.

452 {
453  // l1 -> Degree Zernike
454  // l2 & h --> Degree SPH
455  for (int h=0; h<=l2; h++)
456  {
457  // For the current SPH degree (h), determine the number of SPH components/equations
458  int numSPH = 2*h+1;
459  // Finf the total number of radial components with even degree for a given l1 and h
460  int count=l1-h+1;
461  int numEven=(count>>1)+(count&1 && !(h&1));
462  // Total number of components is the number of SPH as many times as Zernike components
463  if (h%2 == 0) {
464  nc += numSPH*numEven;
465  }
466  else {
467  nc += numSPH*(count-numEven);
468  }
469  }
470 }

◆ preProcess()

void ProgAngularSphAlignment::preProcess ( )
virtual

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

Reimplemented from XmippMetadataProgram.

Definition at line 125 of file angular_sph_alignment.cpp.

126 {
127  V.read(fnVolR);
128  V().setXmippOrigin();
129  Xdim=XSIZE(V());
130  Vdeformed().initZeros(V());
131 
132  Ifilteredp().initZeros(Xdim,Xdim);
133  Ifilteredp().setXmippOrigin();
134 
135  if (RmaxDef<0)
136  RmaxDef = Xdim/2;
137 
138  // Read Reference mask if avalaible (otherwise sphere of radius RmaxDef is used)
139  Mask mask;
140  mask.type = BINARY_CIRCULAR_MASK;
141  mask.mode = INNER_MASK;
142  if (fnMaskR != "") {
143  Image<double> aux;
144  aux.read(fnMaskR);
145  typeCast(aux(), V_mask);
147  }
148  else {
149  mask.R1 = RmaxDef;
150  mask.generate_mask(V());
151  V_mask = mask.get_binary_mask();
153  }
154 
155  // Total Volume Mass (Inside Mask)
156  sumV = 0.0;
158  if (DIRECT_MULTIDIM_ELEM(V_mask,n) == 1) {
160  }
161  }
162 
163  // Construct mask
164  if (Rmax<0)
165  Rmax=Xdim/2;
166  mask.R1 = Rmax;
167  mask.generate_mask(Xdim,Xdim);
168  mask2D=mask.get_binary_mask();
169 
170  // Low pass filter
173  filter.raised_w=0.02;
174 
175  // Transformation matrix
176  A.initIdentity(3);
177 
178  // CTF Filter
180  FilterCTF.ctf.enable_CTFnoise = false;
182 
183  vecSize = 0;
186 
187  createWorkFiles();
188 }
CTFDescription ctf
MultidimArray< int > mask2D
Definition: mask.h:360
void numCoefficients(int l1, int l2, int &nc) const
Length of coefficients vector.
#define CTF
double R1
Definition: mask.h:413
#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
void produceSideInfo()
Produce Side information.
Definition: ctf.cpp:1392
MultidimArray< int > V_mask
int read(const FileName &name, DataMode datamode=DATA, size_t select_img=ALL_IMAGES, bool mapData=false, int mode=WRITE_READONLY)
bool enable_CTFnoise
Enable CTFnoise part.
Definition: ctf.h:273
const MultidimArray< int > & get_binary_mask() const
Definition: mask.h:707
int * n
#define LOWPASS
void initIdentity()
Definition: matrix2d.h:673
int mode
Definition: mask.h:407
void fillVectorTerms(int l1, int l2)
Zernike and SPH coefficients allocation.
constexpr int INNER_MASK
Definition: mask.h:47

◆ processImage()

void ProgAngularSphAlignment::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 293 of file angular_sph_alignment.cpp.

294 {
296  int totalSize = 3*vecSize+8;
297  p.initZeros(totalSize);
298  clnm.initZeros(totalSize);
299 
300  rowOut=rowIn;
301 
302  flagEnabled=1;
303 
309  if (rowIn.containsLabel(MDL_FLIP))
310  rowIn.getValue(MDL_FLIP,old_flip);
311  else
312  old_flip = false;
313 
315  {
316  hasCTF=true;
317  ctf.readFromMdRow(rowIn);
318  ctf.Tm = Ts;
323  }
324  else
325  hasCTF=false;
326 
327  if (verbose>=2)
328  std::cout << "Processing " << fnImg << std::endl;
329  I.read(fnImg);
330  I().setXmippOrigin();
331 
332  Ifiltered()=I();
334 
335  for (int h=1;h<=L2;h++)
336  {
337  if (verbose>=2)
338  {
339  std::cout<<std::endl;
340  std::cout<<"------------------------------ Basis Degrees: ("<<L1<<","<<h<<") ----------------------------"<<std::endl;
341  }
342  steps.clear();
343  steps.initZeros(totalSize);
344 
345  // Optimize
346  double cost=-1;
347  try
348  {
349  cost=1e38;
350  int iter;
351  if (optimizeAlignment)
352  steps(totalSize-8)=steps(totalSize-7)=steps(totalSize-6)=steps(totalSize-5)=steps(totalSize-4)=1.;
353  if (optimizeDefocus)
354  steps(totalSize-3)=steps(totalSize-2)=steps(totalSize-1)=1.;
356  {
357  minimizepos(h,steps);
358  }
359  steps_cp = steps;
360  powellOptimizer(p, 1, totalSize, &continuousSphCost, this, 0.01, cost, iter, steps, verbose>=2);
361 
362  if (verbose>=3)
363  {
364  showOptimization = true;
366  showOptimization = false;
367  }
368 
369  if (cost>0)
370  {
371  flagEnabled=-1;
372  p.initZeros();
373  }
374  cost=-cost;
376  if (verbose>=2)
377  {
378  std::cout<<std::endl;
379  for (int j=1;j<4;j++)
380  {
381  switch (j)
382  {
383  case 1:
384  std::cout << "X Coefficients=(";
385  break;
386  case 2:
387  std::cout << "Y Coefficients=(";
388  break;
389  case 3:
390  std::cout << "Z Coefficients=(";
391  break;
392  }
393  for (int i=(j-1)*vecSize;i<j*vecSize;i++)
394  {
395  std::cout << p(i);
396  if (i<j*vecSize-1)
397  std::cout << ",";
398  }
399  std::cout << ")" << std::endl;
400  }
401  std::cout << "Radius=" << RmaxDef << std::endl;
402  std::cout << " Dshift=(" << p(totalSize-5) << "," << p(totalSize-4) << ") "
403  << "Drot=" << p(totalSize-3) << " Dtilt=" << p(totalSize-2)
404  << " Dpsi=" << p(totalSize-1) << std::endl;
405  std::cout << " Total deformation=" << totalDeformation << std::endl;
406  std::cout<<std::endl;
407  }
408  }
409  catch (XmippError &XE)
410  {
411  std::cerr << XE.what() << std::endl;
412  std::cerr << "Warning: Cannot refine " << fnImg << std::endl;
413  flagEnabled=-1;
414  }
415  }
416 
417  //AJ NEW
418  writeImageParameters(fnImg);
419  //END AJ
420 
421 }
Rotation angle of an image (double,degrees)
Defocus U (Angstroms)
void clear()
Definition: matrix1d.cpp:67
Tilting angle of an image (double,degrees)
void minimizepos(int l2, Matrix1D< double > &steps) const
Determine the positions to be minimize of a vector containing spherical harmonic coefficients.
double DeltafV
Defocus in V (in Angstroms). Negative values are underfocused.
Definition: ctf.h:830
Shift for the image in the X axis (double)
virtual void writeImageParameters(const FileName &fnImg)
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
double DeltafU
Global gain. By default, 1.
Definition: ctf.h:828
double azimuthal_angle
Azimuthal angle (between X and U) in degrees.
Definition: ctf.h:832
double continuousSphCost(double *x, void *_prm)
T & getValue(MDLabel label)
Flip the image? (bool)
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
int verbose
Verbosity level.
void initZeros()
Definition: matrix1d.h:592
#define j
double steps
virtual bool containsLabel(MDLabel label) const =0
void produceSideInfo()
Produce Side information.
Definition: ctf.cpp:1392
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
void applyMaskSpace(MultidimArray< double > &v)

◆ readParams()

void ProgAngularSphAlignment::readParams ( )
virtual

Read argument from command line.

Reimplemented from XmippMetadataProgram.

Definition at line 46 of file angular_sph_alignment.cpp.

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

◆ show()

void ProgAngularSphAlignment::show ( )

Show.

Definition at line 70 of file angular_sph_alignment.cpp.

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

◆ tranformImageSph()

double ProgAngularSphAlignment::tranformImageSph ( double *  pclnm,
double  rot,
double  tilt,
double  psi,
double  deltaDefocusU,
double  deltaDefocusV,
double  deltaDefocusAngle 
)

Definition at line 196 of file angular_sph_alignment.cpp.

198 {
199  const MultidimArray<double> &mV=V();
201  VEC_ELEM(clnm,i)=pclnm[i+1];
202  double deformation=0.0;
203  totalDeformation=0.0;
204  P().initZeros((int)XSIZE(I()),(int)XSIZE(I()));
205  P().setXmippOrigin();
206  deformVol(P(), mV, deformation, rot, tilt, psi);
207  if (hasCTF) {
208  applyCTFImage(deltaDefocusU, deltaDefocusV, deltaDefocusAngle);
209  }
210  double cost=0;
211  if (old_flip)
212  {
213  MAT_ELEM(A,0,0)*=-1;
214  MAT_ELEM(A,0,1)*=-1;
215  MAT_ELEM(A,0,2)*=-1;
216  }
217 
218  applyGeometry(xmipp_transformation::LINEAR,Ifilteredp(),Ifiltered(),A,xmipp_transformation::IS_NOT_INV,xmipp_transformation::DONT_WRAP,0.);
220  const MultidimArray<double> mP=P();
221  const MultidimArray<int> &mMask2D=mask2D;
222  MultidimArray<double> &mIfilteredp=Ifilteredp();
223  double corr=correlationIndex(mIfilteredp,mP,&mMask2D);
224  cost=-corr;
225 
226 #ifdef DEBUG
227  std::cout << "A=" << A << std::endl;
228  Image<double> save;
229  save()=P();
230  save.write("PPPtheo.xmp");
231  save()=Ifilteredp();
232  save.write("PPPfilteredp.xmp");
233  save()=Ifiltered();
234  save.write("PPPfiltered.xmp");
235  // Vdeformed.write("PPPVdeformed.vol");
236  std::cout << "Cost=" << cost << " deformation=" << deformation << std::endl;
237  std::cout << "Press any key" << std::endl;
238  char c; std::cin >> c;
239 #endif
240 
241  if (showOptimization)
242  {
243  std::cout << "A=" << A << std::endl;
244  Image<double> save(P);
245  save.write("PPPtheo.xmp");
246  save()=Ifilteredp();
247  save.write("PPPfilteredp.xmp");
248  save()=Ifiltered();
249  save.write("PPPfiltered.xmp");
250  std::cout << "Cost=" << cost << " corr=" << corr << std::endl;
251  std::cout << "Deformation=" << totalDeformation << std::endl;
252  std::cout << "Press any key" << std::endl;
253  char c; std::cin >> c;
254  }
255 
256  double massDiff=std::abs(sumV-sumVd)/sumV;
257  double retval=cost+lambda*(deformation + massDiff);
258  if (showOptimization)
259  std::cout << cost << " " << deformation << " " << lambda*deformation << " " << sumV << " " << sumVd << " " << massDiff << " " << retval << std::endl;
260  return retval;
261 }
#define VEC_ELEM(v, i)
Definition: matrix1d.h:245
MultidimArray< int > mask2D
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)
#define i
#define MAT_ELEM(m, i, j)
Definition: matrix2d.h:116
#define FOR_ALL_ELEMENTS_IN_MATRIX1D(v)
Definition: matrix1d.h:72
void applyCTFImage(double const &deltaDefocusU, double const &deltaDefocusV, double const &deltaDefocusAngle)
#define XSIZE(v)
double psi(const double x)
double cost
Cost, scaled between 0 and 1.
Definition: micrograph.h:66
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.
void applyMaskSpace(MultidimArray< double > &v)

◆ updateCTFImage()

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

Definition at line 521 of file angular_sph_alignment.cpp.

522 {
523  ctf.K=1; // get pure CTF with no envelope
524  currentDefocusU=ctf.DeltafU=defocusU;
525  currentDefocusV=ctf.DeltafV=defocusV;
528 }
double DeltafV
Defocus in V (in Angstroms). Negative values are underfocused.
Definition: ctf.h:830
double DeltafU
Global gain. By default, 1.
Definition: ctf.h:828
double azimuthal_angle
Azimuthal angle (between X and U) in degrees.
Definition: ctf.h:832
double K
Global gain. By default, 1.
Definition: ctf.h:238
void produceSideInfo()
Produce Side information.
Definition: ctf.cpp:1392

◆ writeImageParameters()

void ProgAngularSphAlignment::writeImageParameters ( const FileName fnImg)
virtual

Write the parameters found for one image

Reimplemented in MpiProgAngularSphAlignment.

Definition at line 424 of file angular_sph_alignment.cpp.

424  {
425  MetaDataVec md;
426  int pos = 3*vecSize;
427  size_t objId = md.addObject();
428  md.setValue(MDL_IMAGE, fnImg, objId);
429  if (flagEnabled==1) {
430  md.setValue(MDL_ENABLED, 1, objId);
431  }
432  else {
433  md.setValue(MDL_ENABLED, -1, objId);
434  }
435  md.setValue(MDL_ANGLE_ROT, old_rot+p(pos+2), objId);
436  md.setValue(MDL_ANGLE_TILT, old_tilt+p(pos+3), objId);
437  md.setValue(MDL_ANGLE_PSI, old_psi+p(pos+4), objId);
438  md.setValue(MDL_SHIFT_X, old_shiftX+p(pos+0), objId);
439  md.setValue(MDL_SHIFT_Y, old_shiftY+p(pos+1), objId);
440  md.setValue(MDL_FLIP, old_flip, objId);
442  std::vector<double> vectortemp;
443  for (int j = 0; j < VEC_XSIZE(clnm); j++) {
444  vectortemp.push_back(clnm(j));
445  }
446  md.setValue(MDL_SPH_COEFFICIENTS, vectortemp, objId);
447  md.setValue(MDL_COST, correlation, objId);
449 }
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> ProgAngularSphAlignment::A

Definition at line 109 of file angular_sph_alignment.h.

◆ clnm

Matrix1D<double> ProgAngularSphAlignment::clnm

Definition at line 136 of file angular_sph_alignment.h.

◆ correlation

double ProgAngularSphAlignment::correlation

Definition at line 146 of file angular_sph_alignment.h.

◆ ctf

CTFDescription ProgAngularSphAlignment::ctf

Definition at line 130 of file angular_sph_alignment.h.

◆ currentAngle

double ProgAngularSphAlignment::currentAngle

Definition at line 128 of file angular_sph_alignment.h.

◆ currentDefocusU

double ProgAngularSphAlignment::currentDefocusU

Definition at line 126 of file angular_sph_alignment.h.

◆ currentDefocusV

double ProgAngularSphAlignment::currentDefocusV

Definition at line 127 of file angular_sph_alignment.h.

◆ filter

FourierFilter ProgAngularSphAlignment::filter

Definition at line 107 of file angular_sph_alignment.h.

◆ FilterCTF

FourierFilter ProgAngularSphAlignment::FilterCTF

Definition at line 132 of file angular_sph_alignment.h.

◆ flagEnabled

int ProgAngularSphAlignment::flagEnabled

Definition at line 87 of file angular_sph_alignment.h.

◆ fnMaskR

FileName ProgAngularSphAlignment::fnMaskR

Filename of the reference volume mask

Definition at line 48 of file angular_sph_alignment.h.

◆ fnOutDir

FileName ProgAngularSphAlignment::fnOutDir

Output directory.

Definition at line 50 of file angular_sph_alignment.h.

◆ fnVolR

FileName ProgAngularSphAlignment::fnVolR

Filename of the reference volume

Definition at line 46 of file angular_sph_alignment.h.

◆ hasCTF

bool ProgAngularSphAlignment::hasCTF

Definition at line 120 of file angular_sph_alignment.h.

◆ I

Image<double> ProgAngularSphAlignment::I

Definition at line 100 of file angular_sph_alignment.h.

◆ Ifiltered

Image<double> ProgAngularSphAlignment::Ifiltered

Definition at line 102 of file angular_sph_alignment.h.

◆ Ifilteredp

Image<double> ProgAngularSphAlignment::Ifilteredp

Definition at line 103 of file angular_sph_alignment.h.

◆ ignoreCTF

bool ProgAngularSphAlignment::ignoreCTF

Definition at line 76 of file angular_sph_alignment.h.

◆ Ip

Image<double> ProgAngularSphAlignment::Ip

Definition at line 101 of file angular_sph_alignment.h.

◆ L1

int ProgAngularSphAlignment::L1

Degrees of Zernike polynomials and spherical harmonics

Definition at line 52 of file angular_sph_alignment.h.

◆ L2

int ProgAngularSphAlignment::L2

Definition at line 53 of file angular_sph_alignment.h.

◆ lambda

double ProgAngularSphAlignment::lambda

Definition at line 82 of file angular_sph_alignment.h.

◆ mask2D

MultidimArray<int> ProgAngularSphAlignment::mask2D

Definition at line 93 of file angular_sph_alignment.h.

◆ maxAngularChange

double ProgAngularSphAlignment::maxAngularChange

Maximum angular change allowed

Definition at line 62 of file angular_sph_alignment.h.

◆ maxResol

double ProgAngularSphAlignment::maxResol

Maximum frequency (A)

Definition at line 64 of file angular_sph_alignment.h.

◆ maxShift

double ProgAngularSphAlignment::maxShift

Maximum shift allowed

Definition at line 60 of file angular_sph_alignment.h.

◆ old_defocusAngle

double ProgAngularSphAlignment::old_defocusAngle

Definition at line 124 of file angular_sph_alignment.h.

◆ old_defocusU

double ProgAngularSphAlignment::old_defocusU

Definition at line 122 of file angular_sph_alignment.h.

◆ old_defocusV

double ProgAngularSphAlignment::old_defocusV

Definition at line 123 of file angular_sph_alignment.h.

◆ old_flip

bool ProgAngularSphAlignment::old_flip

Definition at line 118 of file angular_sph_alignment.h.

◆ old_psi

double ProgAngularSphAlignment::old_psi

Definition at line 113 of file angular_sph_alignment.h.

◆ old_rot

double ProgAngularSphAlignment::old_rot

Definition at line 111 of file angular_sph_alignment.h.

◆ old_shiftX

double ProgAngularSphAlignment::old_shiftX

Definition at line 115 of file angular_sph_alignment.h.

◆ old_shiftY

double ProgAngularSphAlignment::old_shiftY

Definition at line 116 of file angular_sph_alignment.h.

◆ old_tilt

double ProgAngularSphAlignment::old_tilt

Definition at line 112 of file angular_sph_alignment.h.

◆ optimizeAlignment

bool ProgAngularSphAlignment::optimizeAlignment

Definition at line 70 of file angular_sph_alignment.h.

◆ optimizeDefocus

bool ProgAngularSphAlignment::optimizeDefocus

Definition at line 74 of file angular_sph_alignment.h.

◆ optimizeDeformation

bool ProgAngularSphAlignment::optimizeDeformation

Definition at line 72 of file angular_sph_alignment.h.

◆ optimizeRadius

bool ProgAngularSphAlignment::optimizeRadius

Definition at line 78 of file angular_sph_alignment.h.

◆ p

Matrix1D<double> ProgAngularSphAlignment::p

Definition at line 86 of file angular_sph_alignment.h.

◆ P

Image<double> ProgAngularSphAlignment::P

Definition at line 105 of file angular_sph_alignment.h.

◆ phaseFlipped

bool ProgAngularSphAlignment::phaseFlipped

Definition at line 80 of file angular_sph_alignment.h.

◆ resume

bool ProgAngularSphAlignment::resume

Resume computations

Definition at line 91 of file angular_sph_alignment.h.

◆ Rmax

int ProgAngularSphAlignment::Rmax

Maximum radius

Definition at line 68 of file angular_sph_alignment.h.

◆ RmaxDef

int ProgAngularSphAlignment::RmaxDef

Definition at line 84 of file angular_sph_alignment.h.

◆ showOptimization

bool ProgAngularSphAlignment::showOptimization

Definition at line 144 of file angular_sph_alignment.h.

◆ steps_cp

Matrix1D<double> ProgAngularSphAlignment::steps_cp

Definition at line 138 of file angular_sph_alignment.h.

◆ sumV

double ProgAngularSphAlignment::sumV

Definition at line 141 of file angular_sph_alignment.h.

◆ sumVd

double ProgAngularSphAlignment::sumVd

Definition at line 142 of file angular_sph_alignment.h.

◆ totalDeformation

double ProgAngularSphAlignment::totalDeformation

Definition at line 140 of file angular_sph_alignment.h.

◆ Ts

double ProgAngularSphAlignment::Ts

Sampling rate

Definition at line 66 of file angular_sph_alignment.h.

◆ V

Image<double> ProgAngularSphAlignment::V

Definition at line 98 of file angular_sph_alignment.h.

◆ V_mask

MultidimArray<int> ProgAngularSphAlignment::V_mask

Definition at line 94 of file angular_sph_alignment.h.

◆ Vdeformed

Image<double> ProgAngularSphAlignment::Vdeformed

Definition at line 99 of file angular_sph_alignment.h.

◆ vecSize

int ProgAngularSphAlignment::vecSize

Definition at line 134 of file angular_sph_alignment.h.

◆ vL1

Matrix1D<int> ProgAngularSphAlignment::vL1

Zernike and SPH coefficients vectors

Definition at line 55 of file angular_sph_alignment.h.

◆ vL2

Matrix1D<int> ProgAngularSphAlignment::vL2

Definition at line 57 of file angular_sph_alignment.h.

◆ vM

Matrix1D<int> ProgAngularSphAlignment::vM

Definition at line 58 of file angular_sph_alignment.h.

◆ vN

Matrix1D<int> ProgAngularSphAlignment::vN

Definition at line 56 of file angular_sph_alignment.h.

◆ Xdim

size_t ProgAngularSphAlignment::Xdim

Definition at line 96 of file angular_sph_alignment.h.


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