39 ReconsInfo * &IMG_Inf,
bool do_not_use_symproj)
45 bool is_there_ctf =
false;
46 bool is_ctf_unique =
false;
48 int trueIMG = selfile.
size();
51 if (!do_not_use_symproj)
52 numIMG = trueIMG * (SL.
symsNo() + 1);
66 is_ctf_unique =
false;
69 if (IMG_Inf !=
nullptr)
71 if ((IMG_Inf =
new ReconsInfo[numIMG]) ==
nullptr)
75 std::cout <<
"Reading angle information ...\n";
77 for (
size_t objId : selfile.
ids())
81 if (is_there_ctf && !is_ctf_unique)
92 else if (is_there_ctf)
106 if (SL.
symsNo() > 0 && !do_not_use_symproj)
110 int sym_index =
SYMINDEX(SL,
j, i, trueIMG);
112 IMG_Inf[sym_index].
seed = imgInfo.
seed;
114 IMG_Inf[sym_index].
fn_ctf = fn_ctf;
115 else if (is_there_ctf)
117 IMG_Inf[sym_index].
sym =
j;
122 double drot, dtilt, dpsi;
126 IMG_Inf[sym_index].
rot = (float)drot;
127 IMG_Inf[sym_index].
tilt = (float)dtilt;
128 IMG_Inf[sym_index].
psi = (float)dpsi;
158 ordered_list.
resize(numIMG);
159 for (i = 0; i < numIMG; i++)
178 std::cout <<
"Sorting projections ...\n";
181 for (i = 1; i < numIMG; i++)
187 if (N != -1 && i > N)
189 for (j = 0; j < numIMG; j++)
195 if (N != -1 && i > N)
197 if (
A1D_ELEM(product, j) < min_prod)
206 A1D_ELEM(ordered_list, i) = min_prod_proj;
207 A1D_ELEM(chosen, min_prod_proj) = 1;
216 std::cout << std::endl;
235 ordered_list.
resize(numIMG);
238 std::cout <<
"Randomizing projections ...\n";
242 for (
int i = numIMG;
i > 0;
i--)
249 ptr = (ptr + 1) % numIMG;
253 ptr = (ptr + 1) % numIMG;
269 std::cout << std::endl;
276 double &kappa,
double &pow_residual_vol,
double &pow_residual_imgs)
281 double sqrtweight, dim2;
283 std::vector<MultidimArray<double> > newres_imgs;
286 residual_vol.
resize(vol_basis);
290 std::cout <<
"Backprojection of residual images " << std::endl;
294 for (
int iact_proj = 0; iact_proj < prm.
numIMG ; iact_proj++)
299 read_proj() *= sqrtweight;
300 dummy_proj().resize(read_proj());
307 read_proj,
YSIZE(read_proj()),
XSIZE(read_proj()),
329 Zoutput_volume_size, Youtput_volume_size, Xoutput_volume_size);
330 pow_residual_vol = residual_vox().sum2() /
MULTIDIM_SIZE(residual_vox());
331 residual_vox.
clear();
333 std::cout <<
"Projection of residual volume; kappa = " << kappa << std::endl;
338 pow_residual_imgs = 0.;
339 new_proj().resize(read_proj());
343 dim2 = (double)
YSIZE(read_proj()) *
XSIZE(read_proj());
344 for (
int iact_proj = 0; iact_proj < prm.
numIMG ; iact_proj++)
347 dummy_proj,
YSIZE(read_proj()),
XSIZE(read_proj()),
361 new_proj() *= sqrtweight * kappa;
387 pow_residual_imgs /= dim2;
397 int _Zoutput_volume_size,
int _Youtput_volume_size,
398 int _Xoutput_volume_size)
423 (*ptr_vol_out).initZeros();
430 int DWT_iterations = 2;
431 int keep_from_iteration = 0;
435 int DWT_iterations = 2;
436 int keep_from_iteration = 0;
440 int DWT_iterations = 2;
441 int keep_from_iteration = 0;
444 Bilib_DWT(vol_voxels(), DWTV, DWT_iterations);
447 vol_voxels.
write(
"PPPVariability.vol");
449 std::cout <<
"Press any key\n";
454 int x1, x2, y1, y2, z1, z2;
455 SelectDWTBlock(keep_from_iteration, DWTV,
"000", x1, x2, y1, y2, z1, z2);
473 Image<double> SignificantT2, SignificantMaxRatio, SignificantMinRatio;
477 SignificantT2().initZeros(zsize, ysize, xsize);
478 SignificantMaxRatio().initZeros(zsize, ysize, xsize);
479 SignificantMinRatio().initZeros(zsize, ysize, xsize);
480 std::cout <<
"Classifying voxels ...\n";
482 int counter = 0, nxny = ysize * zsize;
496 std::ofstream fh_dat;
497 fh_dat.open(
"PPP.dat");
500 "Cannot open PPP.dat for output");
501 fh_dat <<
NFEATURES <<
" " << nmax << std::endl;
502 std::vector< Matrix1D<double> > v;
510 v_aux(0) =
VA[
n](
k,
i,
j + xsize);
511 v_aux(1) =
VA[
n](
k, i + ysize,
j);
512 v_aux(2) =
VA[
n](
k, i + ysize,
j + xsize);
513 v_aux(3) =
VA[
n](k + zsize,
i,
j);
514 v_aux(4) =
VA[
n](k + zsize,
i,
j + xsize);
515 v_aux(5) =
VA[
n](k + zsize, i + ysize,
j);
516 v_aux(6) =
VA[
n](k + zsize, i + ysize,
j + xsize);
520 v_aux(0) =
VA[
n](
k,
i,
j);
521 v_aux(1) =
VA[
n](
k,
i,
j + xsize);
522 v_aux(2) =
VA[
n](
k, i + ysize,
j);
523 v_aux(3) =
VA[
n](
k, i + ysize,
j + xsize);
524 v_aux(4) =
VA[
n](k + zsize,
i,
j);
525 v_aux(5) =
VA[
n](k + zsize,
i,
j + xsize);
526 v_aux(6) =
VA[
n](k + zsize, i + ysize,
j);
527 v_aux(7) =
VA[
n](k + zsize, i + ysize,
j + xsize);
531 v_aux(0) =
VA[
n](k / 2, i / 2,
j / 2);
532 v_aux(1) =
VA[
n](k / 2, i / 2,
j / 2 + xsize2);
533 v_aux(2) =
VA[
n](k / 2, i / 2 + ysize2,
j / 2);
534 v_aux(3) =
VA[
n](k / 2, i / 2 + ysize2,
j / 2 + xsize2);
535 v_aux(4) =
VA[
n](k / 2 + zsize2, i / 2,
j / 2);
536 v_aux(5) =
VA[
n](k / 2 + zsize2, i / 2,
j / 2 + xsize2);
537 v_aux(6) =
VA[
n](k / 2 + zsize2, i / 2 + ysize2,
j / 2);
538 v_aux(7) =
VA[
n](k / 2 + zsize2, i / 2 + ysize2,
j / 2 + xsize2);
539 v_aux(8) =
VA[
n](
k,
i,
j + xsize);
540 v_aux(9) =
VA[
n](
k, i + ysize,
j);
541 v_aux(10) =
VA[
n](
k, i + ysize,
j + xsize);
542 v_aux(11) =
VA[
n](k + zsize,
i,
j);
543 v_aux(12) =
VA[
n](k + zsize,
i,
j + xsize);
544 v_aux(13) =
VA[
n](k + zsize, i + ysize,
j);
545 v_aux(14) =
VA[
n](k + zsize, i + ysize,
j + xsize);
548 v_aux = v_aux * v_aux;
551 fh_dat << v_aux.
transpose() << std::endl;
557 if (system(
"xmipp_fcmeans -din PPP.dat -std::cout PPP -c 2 -saveclusters > /dev/null")==-1)
563 #define GET_RESULTS(fh,fn,avg,cov,N,idx) \ 566 REPORT_ERROR(ERR_IO_NOTOPEN,(std::string)"VariabilityClass::finishAnalysis: " \ 567 "Cannot open "+fn+" for input"); \ 569 while (!fh.eof()) { \ 571 if (n!=n_previous) { \ 575 aux.fromVector(v[n]); \ 576 cov+=aux*aux.transpose(); \ 581 aux.fromVector(avg); \ 582 cov-=aux*aux.transpose(); 589 GET_RESULTS(fh_0,
"PPP.0", avg0, covariance0, N0, idx0);
592 std::cout <<
"Class 0 is:\n";
593 for (
size_t n = 0;
n < idx0.
size();
n++)
608 GET_RESULTS(fh_1,
"PPP.1", avg1, covariance1, N1, idx1);
611 std::cout <<
"Class 1 is:\n";
612 for (
size_t n = 0;
n < idx1.
size();
n++)
627 covariance = 1.0 / (N0 + N1 - 2) *
628 ((N0 - 1.0) * covariance0 + (N1 - 1.0) * covariance1);
629 covariance *= (1.0 / N0 + 1.0 / N1);
631 T2 = (double)(N0 + N1 - avg_diff.
size() - 1) /
632 ((N0 + N1 - 2) * avg_diff.
size()) *
638 double variance = ((N0 - 1) * covariance0(0, 0) + (N1 - 1) * covariance1(0, 0)) /
640 double t = (avg1(0) - avg0(0)) /
sqrt(variance * (1.0 / N0 + 1.0 / N1));
650 svdcmp(covariance, U, eigenvalues, V);
654 for (
size_t n = 0;
n < idx0.
size();
n++)
655 for (
size_t np =
n + 1; np < idx0.
size(); np++)
656 if (idx0(
n) && idx0(np))
657 coocurrence(
n, np)++;
659 for (
size_t n = 0;
n < idx1.
size();
n++)
660 for (
size_t np =
n + 1; np < idx1.
size(); np++)
661 if (idx1(
n) && idx1(np))
662 coocurrence(
n, np)++;
665 SignificantT2(
k,
i,
j) = T2(0, 0);
668 SignificantMinRatio(
k,
i,
j) = eigenvalues(1) / eigenvalues(0);
669 SignificantMaxRatio(
k,
i,
j) = eigenvalues(
NFEATURES - 1) / eigenvalues(0);
672 std::cout <<
"T2 for this classification is " << T2(0, 0) << std::endl;
673 std::cout <<
"Eigenvalues are " << eigenvalues.
transpose() << std::endl;
676 if (++counter % nxny == 0)
683 if (system(
"rm PPP.dat PPP.cod PPP.vs PPP.err PPP.his PPP.inf PPP.0 PPP.1")==-1)
687 for (
int np =
n + 1; np <
nmax; np++)
688 coocurrence(np,
n) = coocurrence(
n, np);
696 #define POCS_N_measure 8 700 int _Zoutput_volume_size,
int _Youtput_volume_size,
701 int _Xoutput_volume_size)
704 POCS_state = POCS_measuring;
721 POCS_global_mean_error = 0;
732 Image<double> vol_POCS, theo_POCS_vol, corr_POCS_vol, vol_voxels;
734 if (apply_POCS && POCS_i % POCS_freq == 0)
744 vol_voxels.
write(
"PPPvolPOCS0.vol");
745 std::cout <<
"Stats PPPvolPOCS0.vol: ";
746 vol_voxels().printStats();
747 std::cout << std::endl;
756 vol_POCS().resize(vol_voxels());
757 vol_POCS().initZeros();
761 vol_POCS.
write(
"PPPvolPOCS1.vol");
762 std::cout <<
"Stats PPPvolPOCS1.vol: ";
763 vol_POCS().printStats();
764 std::cout << std::endl;
770 desired_volume = &vol_aux;
773 vol_aux.
write(
"PPPvolPOCS2.vol");
774 std::cout <<
"Stats PPPvolPOCS2.vol: ";
775 vol_aux().printStats();
776 std::cout << std::endl;
784 aux_mask.
resize(vol_POCS());
786 aux_mask(
k,
i,
j) = 1 - (int)vol_POCS(
k,
i,
j);
787 long mask_voxels = vol_POCS().countThreshold(
"below", 0.5, 0);
789 aux_mask, vol_voxels(), hist, 300);
790 double known_percentage;
793 threshold = hist.
percentil(100 - known_percentage);
795 if (vol_voxels(
k,
i,
j) < threshold)
796 vol_POCS(
k,
i,
j) = 1;
800 vol_POCS.
write(
"PPPvolPOCS3.vol");
801 std::cout <<
"Stats PPPvolPOCS3.vol: ";
802 vol_POCS().printStats();
803 std::cout << std::endl;
811 int relax = 0, posi = 0;
833 if (desired_volume ==
nullptr)
836 &(theo_POCS_vol()),
nullptr,
839 POCS_mean_error, POCS_max_error,
VARTK);
843 if (vol_POCS(
k,
i,
j) == 1)
844 (*desired_volume)(
k,
i,
j) = 0;
849 &(theo_POCS_vol()), &((*desired_volume)()),
852 POCS_mean_error, POCS_max_error,
VARTK);
854 std::cout <<
" POCS Iteration " << i
855 <<
" POCS Error=" << POCS_mean_error << std::endl;
860 if (desired_volume ==
nullptr)
863 if (vol_POCS(
k,
i,
j))
864 vol_basis(0)(
k,
i,
j) = 0;
868 vol_basis(0)().initZeros();
870 vol_basis(0)(
k,
i,
j) = (*desired_volume)(
k,
i,
j);
872 POCS_mean_error = -1;
878 POCS_global_mean_error += POCS_mean_error;
882 if (
prm->
numIMG - images < 100 || images % 100 == 0 ||
883 desired_volume !=
nullptr)
886 POCS_state = POCS_measuring;
897 std::cout <<
"M:" << POCS_vec_i <<
" " << POCS_mean_error << std::endl;
900 POCS_errors(POCS_vec_i++) = POCS_mean_error;
907 POCS_state = POCS_use;
910 std::cout <<
"1: Changing to " << POCS_freq << std::endl;
917 POCS_errors.computeStats(POCS_avg,
918 POCS_stddev, dummy, POCS_min);
921 std::cout <<
"Reference errors: " << POCS_errors.transpose() << std::endl;
922 std::cout <<
"Checking " <<
ABS(POCS_mean_error - POCS_avg) <<
" " << 1.2*1.96*POCS_stddev << std::endl;
925 if (
ABS(POCS_mean_error - POCS_avg) < 1.2*1.96*POCS_stddev)
927 if (POCS_mean_error < POCS_avg)
929 double max_error = POCS_errors(0);
932 if (POCS_errors(
i) > max_error)
934 max_error = POCS_errors(
i);
937 POCS_errors(POCS_vec_i) = POCS_mean_error;
942 else if (POCS_freq < 3)
947 std::cout <<
"2: Changing to " << POCS_freq << std::endl;
962 std::cout <<
"3: Changing to " << POCS_freq << std::endl;
966 else if (POCS_used > 2)
971 POCS_state = POCS_lowering;
974 std::cout <<
"Lowering\n";
982 POCS_errors.computeStats(POCS_avg,
983 POCS_stddev, POCS_max, dummy);
989 POCS_state = POCS_measuring;
1000 POCS_mean_error = -1;
bool is_crystal
Is this a crystal 0 means NO 1 YES.
Just to locate unclassified errors.
void init_progress_bar(long total)
MDRowVec row
Header information of projection.
void selfWindow(int n0, int z0, int y0, int x0, int nF, int zF, int yF, int xF, T init_value=0)
bool positivity
Apply positivity constraint.
int numIMG
Total number of images to process (taking symmetries into account)
void Bilib_DWT(const MultidimArray< double > &input, MultidimArray< double > &result, int iterations, int isign)
void resize(size_t Ndim, size_t Zdim, size_t Ydim, size_t Xdim, bool copy=true)
size_t projXdim
Projection X dimension.
#define GET_RESULTS(fh, fn, avg, cov, N, idx)
void getMatrices(int i, Matrix2D< double > &L, Matrix2D< double > &R, bool homogeneous=true) const
Basis basis
Basis function. By default, blobs.
#define REPORT_ERROR(nerr, ErrormMsg)
void resize(const Matrix1D< double > &corner1, const Matrix1D< double > &corner2)
void updateResidualVector(BasicARTParameters &prm, GridVolume &vol_basis, double &kappa, double &pow_residual_vol, double &pow_residual_imgs)
void fromVector(const Matrix1D< T > &op1)
void apply_binary_mask(const MultidimArray< int > &mask, const MultidimArray< T > &m_in, MultidimArray< T > &m_out, T subs_val=(T) 0)
#define FOR_ALL_DIRECT_ELEMENTS_IN_ARRAY2D(m)
void Euler_angles2matrix(T alpha, T beta, T gamma, Matrix2D< T > &A, bool homogeneous)
void symmetrizeVolume(const SymList &SL, const MultidimArray< double > &V_in, MultidimArray< double > &V_out, int spline, bool wrap, bool do_outside_avg, bool sum, bool helical, bool dihedral, bool helicalDihedral, double rotHelical, double rotPhaseHelical, double zHelical, double heightFraction, const MultidimArray< double > *mask, int Cn)
void sqrt(Image< double > &op)
void BinaryCircularMask(MultidimArray< int > &mask, double radius, int mode, double x0, double y0, double z0)
void ART_voxels2blobs_single_step(GridVolume &vol_in, GridVolume *vol_out, const struct blobtype &blob, const Matrix2D< double > *D, double lambda, MultidimArray< double > *theo_vol, const MultidimArray< double > *read_vol, MultidimArray< double > *corr_vol, const MultidimArray< double > *mask_vol, double &mean_error, double &max_error, int eq_mode, int threads)
There is not enough memory for allocation.
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)
void newIteration()
Start New ART iteration.
void inv(Matrix2D< T > &result) const
struct blobtype blob
Blob parameters.
POCSClass(BasicARTParameters *_prm, int _Zoutput_volume_size, int _Youtput_volume_size, int _Xoutput_volume_size)
Constructor.
double percentil(double percent_mass)
void newProjection()
Start new Projection.
Matrix2D< T > transpose() const
#define FOR_ALL_DIRECT_ELEMENTS_IN_ARRAY3D(V)
tBasisFunction type
Basis function to use.
std::vector< MultidimArray< double > > VA
Vector of training vectors.
size_t projYdim
Projection Y dimension.
void Euler_apply_transf(const Matrix2D< double > &L, const Matrix2D< double > &R, double rot, double tilt, double psi, double &newrot, double &newtilt, double &newpsi)
void changeToVoxels(GridVolume &vol_basis, MultidimArray< double > *vol_voxels, int Zdim, int Ydim, int Xdim, int threads=1) const
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
Matrix1D< T > transpose() const
void initLinear(T minF, T maxF, int n=1, const String &mode="incr")
void threshold(double *phi, unsigned long nvox, double limit)
double known_volume
Known volume. If -1, not applied.
#define EULER_CLIPPING(rot, tilt, psi)
#define FOR_ALL_ELEMENTS_IN_ARRAY3D(V)
double tilt
Tilting angle.
GridVolumeT< int > * GVNeq
void sortRandomly(int numIMG, MultidimArray< int > &ordered_list)
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)
Incorrect argument received.
ReconsInfo * IMG_Inf
Array with all the sorting information for each projection.
void progress_bar(long rlen)
void newUpdateVolume(GridVolume *ptr_vol_out, Projection &read_proj)
FileName fn_ctf
CTF filename.
FileName fn_proj
Projection filename.
void sortPerpendicular(int numIMG, ReconsInfo *IMG_Inf, MultidimArray< int > &ordered_list, int N)
#define DIRECT_A3D_ELEM(v, k, i, j)
void svdcmp(const Matrix2D< T > &a, Matrix2D< double > &u, Matrix1D< double > &w, Matrix2D< double > &v)
int force_sym
Force the reconstruction to be symmetric this number of times.
VariabilityClass(BasicARTParameters *_prm, int _Zoutput_volume_size, int _Youtput_volume_size, int _Xoutput_volume_size)
Constructor.
void apply(GridVolume &vol_basis, int it, int images)
Apply.
void SelectDWTBlock(int scale, const MultidimArray< T > &I, const std::string &quadrant, int &x1, int &x2)
void setRow(size_t i, const Matrix1D< T > &v)
MultidimArray< int > ordered_list
Order in which projections will be presented to algorithm.
double rot
Rotational angle.
T dotProduct(const Matrix1D< T > &v1, const Matrix1D< T > &v2)
void typeCast(const Matrix1D< T1 > &v1, Matrix1D< T2 > &v2)
#define TELL_SAVE_AT_EACH_STEP
Image< double > * surface_mask
void noSort(int numIMG, MultidimArray< int > &ordered_list)
void setAngles(double _rot, double _tilt, double _psi)
void initZeros(const MultidimArray< T1 > &op)
unsigned int randomize_random_generator()
#define SYMINDEX(SL, sym_no, i, numIMG)
int N
Number of updates so far.
void buildReconsInfo(MetaDataVec &selfile, const FileName &fn_ctf, const SymList &SL, ReconsInfo *&IMG_Inf, bool do_not_use_symproj)
void getRow(size_t i, Matrix1D< T > &v) const
int threads
Number of threads to use. Can not be different than 1 when using MPI.
void resize(size_t Ydim, size_t Xdim, bool noCopy=false)
void compute_hist_within_binary_mask(const MultidimArray< int > &mask, MultidimArray< T > &v, Histogram1D &hist, int no_steps)
SymList SL
A list with the symmetry matrices.
std::vector< Projection > residual_imgs