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

#include <base_art_recons.h>

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

Public Member Functions

 SinPartARTRecons ()
 
virtual ~SinPartARTRecons ()
 
void preProcess (GridVolume &vol_basis0, int level=FULL, int rank=-1)
 Produce Plain side information from the Class parameters. More...
 
virtual void singleStep (GridVolume &vol_in, GridVolume *vol_out, Projection &theo_proj, Projection &read_proj, int sym_no, Projection &diff_proj, Projection &corr_proj, Projection &alig_proj, double &mean_error, int numIMG, double lambda, int act_proj, const FileName &fn_ctf, const MultidimArray< int > *maskPtr, bool refine)
 
void postProcess (GridVolume &vol_basis)
 
- Public Member Functions inherited from ARTReconsBase
virtual ~ARTReconsBase ()
 
virtual void readParams (XmippProgram *program)
 
virtual void print (std::ostream &o) const
 
virtual void applySymmetry (GridVolume &vol_in, GridVolume *vol_out, int grid_type)
 
void initHistory (const GridVolume &vol_basis0)
 
void iterations (GridVolume &vol_basis, int rank=-1)
 

Additional Inherited Members

- Static Public Member Functions inherited from ARTReconsBase
static void defineParams (XmippProgram *program, bool mpiMode=false)
 
- Public Attributes inherited from ARTReconsBase
BasicARTParameters artPrm
 

Detailed Description

Definition at line 146 of file base_art_recons.h.

Constructor & Destructor Documentation

◆ SinPartARTRecons()

SinPartARTRecons::SinPartARTRecons ( )
inline

Definition at line 151 of file base_art_recons.h.

152  {}

◆ ~SinPartARTRecons()

virtual SinPartARTRecons::~SinPartARTRecons ( )
inlinevirtual

Definition at line 154 of file base_art_recons.h.

155  {}

Member Function Documentation

◆ postProcess()

void SinPartARTRecons::postProcess ( GridVolume vol_basis)
virtual

Finish iterations. For WLS: delete residual images Else: do nothing.

Reimplemented from ARTReconsBase.

Definition at line 1025 of file base_art_recons.cpp.

1026 {
1027  // Destroy created threads. This is done in a tricky way. At this point, threads
1028  // are "slept" waiting for a barrier to be reached by the master thread to continue
1029  // projecting/backprojecting a new projection. Here we set the flag destroy=true so
1030  // the threads won't process a projections but will return.
1031  if( artPrm.threads > 1 )
1032  {
1033  for( int c = 0 ; c < artPrm.threads ; c++ )
1034  {
1035  project_threads[c].destroy = true;
1036  }
1037 
1038  // Trigger threads "self-destruction"
1040 
1041  // Wait for effective threads death
1042  for( int c = 0 ; c < artPrm.threads ; c++ )
1043  {
1044  pthread_join(*(th_ids+c),nullptr);
1045  }
1046 
1047  // Destroy barrier and mutex, as they are no longer needed.
1048  // Sjors, 27march2009
1049  // NO, don't do this, because running a second threaded-ART
1050  // in a single program will not have mutexes anymore...
1051  //pthread_mutex_destroy( &project_mutex );
1052  //barrier_destroy( &project_barrier );
1053  }
1054 }
doublereal * c
barrier_t project_barrier
Definition: projection.cpp:46
project_thread_params * project_threads
Definition: projection.cpp:48
BasicARTParameters artPrm
int barrier_wait(barrier_t *barrier)
int threads
Number of threads to use. Can not be different than 1 when using MPI.
Definition: basic_art.h:267

◆ preProcess()

void SinPartARTRecons::preProcess ( GridVolume vol_basis0,
int  level = FULL,
int  rank = -1 
)
virtual

Produce Plain side information from the Class parameters.

Reimplemented from ARTReconsBase.

Definition at line 796 of file base_art_recons.cpp.

797 {
798  ARTReconsBase::preProcess(vol_basis0, level, rank);
799 
800  // As this is a threaded implementation, create structures for threads, and
801  // create threads
802  if( artPrm.threads > 1 )
803  {
804  th_ids = (pthread_t *)malloc( artPrm.threads * sizeof( pthread_t));
805 
806  // Initialize the structures which will contain the parameters passed to different
807  // threads
809 
810  // Initialize barrier to wait for working threads and the master thread.
812 
813  // Threads are created in a waiting state. They can only run when master thread unlocks them by calling
814  // barrier_wait()
815 
816  for( int c = 0 ; c < artPrm.threads ; c++ )
817  {
820  project_threads[c].destroy = false;
821 
822  pthread_create( (th_ids+c), nullptr, project_SimpleGridThread<double>, (void *)(project_threads+c) );
823  }
824  }
825 }
int barrier_init(barrier_t *barrier, int needed)
template void * project_SimpleGridThread< double >(void *)
doublereal * c
virtual void preProcess(GridVolume &vol_basis0, int level=FULL, int rank=-1)
Produce Plain side information from the Class parameters.
barrier_t project_barrier
Definition: projection.cpp:46
project_thread_params * project_threads
Definition: projection.cpp:48
BasicARTParameters artPrm
int threads
Number of threads to use. Can not be different than 1 when using MPI.
Definition: basic_art.h:267

◆ singleStep()

void SinPartARTRecons::singleStep ( GridVolume vol_in,
GridVolume vol_out,
Projection theo_proj,
Projection read_proj,
int  sym_no,
Projection diff_proj,
Projection corr_proj,
Projection alig_proj,
double &  mean_error,
int  numIMG,
double  lambda,
int  act_proj,
const FileName fn_ctf,
const MultidimArray< int > *  maskPtr,
bool  refine 
)
virtual

Run a single step of ART. An ART iteration is compound of as many steps as projections, this function runs a single step of the process. In ART the pointer to the output volume must point to the same vol_in, while in SIRT it should point to a second volume. The read projection must be provided to the algorithm but the rest of projections are output by the routine. The mean error is also an output. numIMG is a normalizing factor to be used in SIRT, if you are running pure ART then this factor should be 1.

The symmetry matrix from which the view is derived must be given in sym_no. In fact, it is not used in this version of ART, but it is needed for the crystal counterpart.

Reimplemented from ARTReconsBase.

Definition at line 828 of file base_art_recons.cpp.

835 {
836  // Prepare to work with CTF ................................................
837  FourierFilter ctf;
838  auto *footprint = (ImageOver *) & artPrm.basis.blobprint;
839  auto *footprint2 = (ImageOver *) & artPrm.basis.blobprint2;
840  bool remove_footprints = false;
841  double weight, sqrtweight;
842 
843  if (fn_ctf != "" && !artPrm.unmatched)
844  {
845  if (artPrm.basis.type != Basis::blobs)
846  REPORT_ERROR(ERR_VALUE_INCORRECT, "ART_single_step: This way of correcting for the CTF "
847  "only works with blobs");
848  // It is a description of the CTF
849  ctf.FilterShape = ctf.FilterBand = CTF;
850  ctf.ctf.enable_CTFnoise = false;
851  ctf.ctf.read(fn_ctf);
852  ctf.ctf.Tm /= BLOB_SUBSAMPLING;
853  ctf.ctf.produceSideInfo();
854 
855  // Create new footprints
856  footprint = new ImageOver;
857  footprint2 = new ImageOver;
858  remove_footprints = true;
859 
860  // Enlarge footprint, bigger than necessary to avoid
861  // aliasing
862  *footprint = artPrm.basis.blobprint;
863  (*footprint)().setXmippOrigin();
864  double blob_radius = artPrm.basis.blob.radius;
865  int finalsize = 2 * CEIL(30 + blob_radius) + 1;
866  footprint->window(
867  FIRST_XMIPP_INDEX(finalsize), FIRST_XMIPP_INDEX(finalsize),
868  LAST_XMIPP_INDEX(finalsize), LAST_XMIPP_INDEX(finalsize));
869 
870  // Generate mask to the size of the footprint, correct phase
871  // and apply CTF
872  ctf.generateMask((*footprint)());
873  ctf.correctPhase();
874  ctf.applyMaskSpace((*footprint)());
875 
876  // Remove unnecessary regions
877  finalsize = 2 * CEIL(15 + blob_radius) + 1;
878  footprint->window(
879  FIRST_XMIPP_INDEX(finalsize), FIRST_XMIPP_INDEX(finalsize),
880  LAST_XMIPP_INDEX(finalsize), LAST_XMIPP_INDEX(finalsize));
881 #ifdef DEBUG
882 
883  Image<double> save;
884  save() = (*footprint)();
885  save.write("PPPfootprint.xmp");
886 #endif
887 
888  // Create footprint2
889  *footprint2 = *footprint;
890  (*footprint2)() *= (*footprint2)();
891  }
892 
893  // Project structure .......................................................
894  // The correction image is reused in this call to store the normalising
895  // projection, ie, the projection of an all-1 volume
896  Matrix2D<double> *A = nullptr;
898  A = new Matrix2D<double>;
899  corr_proj().initZeros();
900 
901  project_GridVolume(vol_in, artPrm.basis, theo_proj,
902  corr_proj, YSIZE(read_proj()), XSIZE(read_proj()),
903  read_proj.rot(), read_proj.tilt(), read_proj.psi(), FORWARD, artPrm.eq_mode,
904  artPrm.GVNeq, A, maskPtr, artPrm.ray_length, artPrm.threads);
905 
906  if (fn_ctf != "" && artPrm.unmatched)
907  {
908  ctf.generateMask(theo_proj());
909  ctf.applyMaskSpace(theo_proj());
910  }
911 
912  // Print system matrix
914  {
915  std::cout << "Equation system (Ax=b) ----------------------\n";
916  std::cout << "Size: "<< (*A).mdimx <<"x"<<(*A).mdimy<< std::endl;
917  for (size_t i = 0; i < MAT_YSIZE(*A); i++)
918  {
919  bool null_row = true;
920  for (size_t j = 0; j < MAT_XSIZE(*A); j++)
921  if (MAT_ELEM(*A, i, j) != 0)
922  {
923  null_row = false;
924  break;
925  }
926  if (!null_row)
927  {
928  std::cout << "pixel=" << integerToString(i, 3) << " --> "
929  << DIRECT_MULTIDIM_ELEM(read_proj(), i) << " = ";
930  for (size_t j = 0; j < MAT_XSIZE(*A); j++)
931  std::cout << MAT_ELEM(*A, i, j) << " ";
932  std::cout << std::endl;
933  }
934  }
935  std::cout << "---------------------------------------------\n";
936  delete A;
937  }
938 
939  // Refine ..............................................................
940  if (refine)
941  {
943  /*
944  Image<double> save;
945  save()=theo_proj(); save.write("PPPtheo.xmp");
946  save()=read_proj(); save.write("PPPread.xmp");
947  */
948  alignImages(theo_proj(),read_proj(),M);
949  //save()=read_proj(); save.write("PPPread_aligned.xmp");
950  std::cout << M << std::endl;
951  read_proj().rangeAdjust(theo_proj());
952  //save()=read_proj(); save.write("PPPread_aligned_grey.xmp");
953  //std::cout << "Press any key\n";
954  //char c; std::cin >> c;
955  }
956 
957  // Now compute differences .................................................
958  double applied_lambda = lambda / numIMG; // In ART mode, numIMG=1
959 
960  mean_error = 0;
961  diff_proj().resize(read_proj());
962 
963  // Weighted least-squares ART for Maximum-Likelihood refinement
964  if (artPrm.WLS)
965  {
966  weight = read_proj.weight() / artPrm.sum_weight;
967  sqrtweight = sqrt(weight);
968 
970  {
971  // Compute difference image and error
972  IMGPIXEL(diff_proj, i, j) = IMGPIXEL(read_proj, i, j) - IMGPIXEL(theo_proj, i, j);
973  mean_error += IMGPIXEL(diff_proj, i, j) * IMGPIXEL(diff_proj, i, j);
974 
975  // Subtract the residual image (stored in alig_proj!)
976  IMGPIXEL(diff_proj, i, j) = sqrtweight * IMGPIXEL(diff_proj, i, j) - IMGPIXEL(alig_proj, i, j);
977 
978  // Calculate the correction and the updated residual images
979  IMGPIXEL(corr_proj, i, j) =
980  applied_lambda * IMGPIXEL(diff_proj, i, j) / (weight * IMGPIXEL(corr_proj, i, j) + 1.);
981  IMGPIXEL(alig_proj, i, j) += IMGPIXEL(corr_proj, i, j);
982  IMGPIXEL(corr_proj, i, j) *= sqrtweight;
983 
984  }
985  mean_error /= XSIZE(diff_proj()) * YSIZE(diff_proj());
986  mean_error *= weight;
987 
988  }
989  else
990  {
991  long int Nmean=0;
993  {
994  if (maskPtr!=nullptr)
995  if ((*maskPtr)(i,j)<0.5)
996  continue;
997  // Compute difference image and error
998  IMGPIXEL(diff_proj, i, j) = IMGPIXEL(read_proj, i, j) - IMGPIXEL(theo_proj, i, j);
999  mean_error += IMGPIXEL(diff_proj, i, j) * IMGPIXEL(diff_proj, i, j);
1000  Nmean++;
1001 
1002  // Compute the correction image
1003  IMGPIXEL(corr_proj, i, j) = XMIPP_MAX(IMGPIXEL(corr_proj, i, j), 1);
1004  IMGPIXEL(corr_proj, i, j) =
1005  applied_lambda * IMGPIXEL(diff_proj, i, j) / IMGPIXEL(corr_proj, i, j);
1006  }
1007  mean_error /= Nmean;
1008  }
1009 
1010  // Backprojection of correction plane ......................................
1011  project_GridVolume(*vol_out, artPrm.basis, theo_proj,
1012  corr_proj, YSIZE(read_proj()), XSIZE(read_proj()),
1013  read_proj.rot(), read_proj.tilt(), read_proj.psi(), BACKWARD, artPrm.eq_mode,
1014  artPrm.GVNeq, nullptr, maskPtr, artPrm.ray_length, artPrm.threads);
1015 
1016  // Remove footprints if necessary
1017  if (remove_footprints)
1018  {
1019  delete footprint;
1020  delete footprint2;
1021  }
1022 }
#define MAT_YSIZE(m)
Definition: matrix2d.h:124
#define YSIZE(v)
#define XMIPP_MAX(x, y)
Definition: xmipp_macros.h:193
CTFDescription ctf
Basis basis
Basis function. By default, blobs.
Definition: basic_art.h:97
double alignImages(const MultidimArray< double > &Iref, const AlignmentTransforms &IrefTransforms, MultidimArray< double > &I, Matrix2D< double > &M, bool wrap, AlignmentAux &aux, CorrelationAux &aux2, RotationalCorrelationAux &aux3)
Definition: filters.cpp:2047
#define REPORT_ERROR(nerr, ErrormMsg)
Definition: xmipp_error.h:211
double psi(const size_t n=0) const
void sqrt(Image< double > &op)
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)
struct blobtype blob
Blob parameters.
Definition: basis.h:61
#define BACKWARD
Definition: blobs.cpp:1092
String integerToString(int I, int _width, char fill_with)
tBasisFunction type
Basis function to use.
Definition: basis.h:52
double rot(const size_t n=0) const
ImageOver blobprint2
Square of the footprint.
Definition: basis.h:74
#define IMGMATRIX(I)
double weight(const size_t n=0) const
#define i
#define FOR_ALL_ELEMENTS_IN_ARRAY2D(m)
void read(const FileName &fn, bool disable_if_not_K=true)
Definition: ctf.cpp:1220
double tilt(const size_t n=0) const
#define MAT_ELEM(m, i, j)
Definition: matrix2d.h:116
bool print_system_matrix
Print system matrix.
Definition: basic_art.h:249
double * lambda
GridVolumeT< int > * GVNeq
Definition: basic_art.h:381
#define CEIL(x)
Definition: xmipp_macros.h:225
#define CTF
void project_GridVolume(GridVolumeT< T > &vol, const Basis &basis, Projection &proj, Projection &norm_proj, int Ydim, int Xdim, double rot, double tilt, double psi, int FORW, int eq_mode, GridVolumeT< int > *GVNeq, Matrix2D< double > *M, const MultidimArray< int > *mask, double ray_length, int threads)
#define XSIZE(v)
double Tm
Sampling rate (A/pixel)
Definition: ctf.h:240
#define DIRECT_MULTIDIM_ELEM(v, n)
#define j
#define FORWARD
Definition: blobs.cpp:1091
bool unmatched
Apply unmatched projectors to correct for the CTF.
Definition: basic_art.h:232
#define MAT_XSIZE(m)
Definition: matrix2d.h:120
#define FIRST_XMIPP_INDEX(size)
Definition: xmipp_macros.h:439
void produceSideInfo()
Produce Side information.
Definition: ctf.cpp:1392
const int BLOB_SUBSAMPLING
Definition: basis.h:34
BasicARTParameters artPrm
bool enable_CTFnoise
Enable CTFnoise part.
Definition: ctf.h:273
#define LAST_XMIPP_INDEX(size)
Definition: xmipp_macros.h:448
Incorrect value received.
Definition: xmipp_error.h:195
void generateMask(MultidimArray< double > &v)
int threads
Number of threads to use. Can not be different than 1 when using MPI.
Definition: basic_art.h:267
double radius
Spatial radius in Universal System units.
Definition: blobs.h:115
void applyMaskSpace(MultidimArray< double > &v)
#define IMGPIXEL(I, i, j)
ImageOver blobprint
Blob footprint.
Definition: basis.h:71

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