void initialise() { // Get the MDs for both untilted and tilted particles MDu.read(fn_unt); MDt.read(fn_til); if (MDu.numberOfObjects() != MDt.numberOfObjects()) REPORT_ERROR("Tiltpair plot ERROR: untilted and tilted STAR files have unequal number of entries."); // Get the symmetry point group int pgGroup, pgOrder; SL.isSymmetryGroup(fn_sym, pgGroup, pgOrder); SL.read_sym_file(fn_sym); // Make postscript header fh_eps.open(fn_eps.c_str(), std::ios::out); if (!fh_eps) REPORT_ERROR("Tiltpair plot ERROR: Cannot open " + fn_eps + " for output"); fh_eps << "%%!PS-Adobe-2.0\n"; fh_eps << "%% Creator: Tilt pair analysis \n"; fh_eps << "%% Pages: 1\n"; fh_eps << "0 setgray\n"; fh_eps << "0.1 setlinewidth\n"; // Draw circles on postscript: 250pixels=plot_max_tilt fh_eps << "300 400 83 0 360 arc closepath stroke\n"; fh_eps << "300 400 167 0 360 arc closepath stroke\n"; fh_eps << "300 400 250 0 360 arc closepath stroke\n"; fh_eps << "300 150 newpath moveto 300 650 lineto stroke\n"; fh_eps << "50 400 newpath moveto 550 400 lineto stroke\n"; }
void symmetriseMap(MultidimArray<DOUBLE> &img, FileName &fn_sym, bool do_wrap) { if (img.getDim() != 3) REPORT_ERROR("symmetriseMap ERROR: symmetriseMap can only be run on 3D maps!"); img.setXmippOrigin(); SymList SL; SL.read_sym_file(fn_sym); Matrix2D<DOUBLE> L(4, 4), R(4, 4); // A matrix from the list MultidimArray<DOUBLE> sum, aux; sum = img; aux.resize(img); for (int isym = 0; isym < SL.SymsNo(); isym++) { SL.get_matrices(isym, L, R); applyGeometry(img, aux, R, IS_INV, do_wrap); sum += aux; } // Overwrite the input img = sum / (SL.SymsNo() + 1); }
// Check whether projection directions are unique ================================= bool directions_are_unique(double rot, double tilt, double rot2, double tilt2, double rot_limit, double tilt_limit, SymList &SL, bool include_mirrors, Matrix2D<double> &Laux, Matrix2D<double> &Raux) { bool are_unique = true; double rot2p, tilt2p, psi2p, psi2 = 0.; double diff_rot, diff_tilt; int isymmax=SL.symsNo(); for (int isym = 0; isym <= isymmax; isym++) { if (isym == 0) { rot2p = rot2; tilt2p = tilt2; psi2p = psi2; } else { SL.getMatrices(isym - 1, Laux, Raux,false); Euler_apply_transf(Laux, Raux, rot2, tilt2, psi2, rot2p, tilt2p, psi2p); } double aux=rot - rot2p; diff_rot = fabs(realWRAP(aux, -180, 180)); aux=tilt - tilt2p; diff_tilt = fabs(realWRAP(aux, -180, 180)); if ((rot_limit - diff_rot) > 1e-3 && (tilt_limit - diff_tilt) > 1e-3) are_unique = false; Euler_another_set(rot2p, tilt2p, psi2p, rot2p, tilt2p, psi2p); aux=rot - rot2p; diff_rot = fabs(realWRAP(aux, -180, 180)); aux=tilt - tilt2p; diff_tilt = fabs(realWRAP(aux, -180, 180)); if ((rot_limit - diff_rot) > 1e-3 && (tilt_limit - diff_tilt) > 1e-3) are_unique = false; if (!include_mirrors) { Euler_up_down(rot2p, tilt2p, psi2p, rot2p, tilt2p, psi2p); aux=rot - rot2p; diff_rot = fabs(realWRAP(aux, -180, 180)); aux=tilt - tilt2p; diff_tilt = fabs(realWRAP(aux, -180, 180)); if ((rot_limit - diff_rot) > 1e-3 && (tilt_limit - diff_tilt) > 1e-3) are_unique = false; Euler_another_set(rot2p, tilt2p, psi2p, rot2p, tilt2p, psi2p); aux=rot - rot2p; diff_rot = fabs(realWRAP(aux, -180, 180)); aux=tilt - tilt2p; diff_tilt = fabs(realWRAP(aux, -180, 180)); if ((rot_limit - diff_rot) > 1e-3 && (tilt_limit - diff_tilt) > 1e-3) are_unique = false; } } return are_unique; }
DOUBLE check_symmetries(DOUBLE rot1, DOUBLE tilt1, DOUBLE psi1, DOUBLE &rot2, DOUBLE &tilt2, DOUBLE &psi2) { int imax = SL.SymsNo() + 1; Matrix2D<DOUBLE> L(4, 4), R(4, 4); // A matrix from the list DOUBLE best_ang_dist = 3600; DOUBLE best_rot2, best_tilt2, best_psi2; DOUBLE tilt_angle, alpha, beta; for (int i = 0; i < imax; i++) { DOUBLE rot2p, tilt2p, psi2p; if (i == 0) { rot2p = rot2; tilt2p = tilt2; psi2p = psi2; } else { SL.get_matrices(i - 1, L, R); L.resize(3, 3); // Erase last row and column R.resize(3, 3); // as only the relative orientation is useful and not the translation Euler_apply_transf(L, R, rot2, tilt2, psi2, rot2p, tilt2p, psi2p); } DOUBLE ang_dist = check_tilt_pairs(rot1, tilt1, psi1, rot2p, tilt2p, psi2p); if (ang_dist < best_ang_dist) { best_ang_dist = ang_dist; best_rot2 = rot2p; best_tilt2 = tilt2p; best_psi2 = psi2p; } } rot2 = best_rot2; tilt2 = best_tilt2; psi2 = best_psi2; return best_ang_dist; }
int find_nearest_direction(double rot1, double tilt1, std::vector<double> &rotList, std::vector<double> &tiltList, SymList &SL, Matrix2D<double> &Laux, Matrix2D<double> &Raux) { int optdir; double dist, mindist; double newrot, newtilt, newpsi; optdir = -1; mindist = 9999.; int imax=SL.symsNo(); size_t nmax=rotList.size(); double *ptrRot=NULL; double *ptrTilt=NULL; if (nmax>0) { ptrRot=&rotList[0]; ptrTilt=&tiltList[0]; } for (size_t n=0; n<nmax; ++n, ++ptrRot, ++ptrTilt) { dist = distance_directions(rot1, tilt1, *ptrRot, *ptrTilt, false); if (dist < mindist) { mindist = dist; optdir = n; } for (int i = 0; i < imax; i++) { SL.getMatrices(i, Laux, Raux, false); Euler_apply_transf(Laux, Raux, rot1, tilt1, 0., newrot, newtilt, newpsi); dist = distance_directions(newrot, newtilt, *ptrRot, *ptrTilt, false); if (dist < mindist) { mindist = dist; optdir = n; } } } return optdir; }
void ProgValidationNonTilt::run() { //Clustering Tendency and Cluster Validity Stephen D. Scott randomize_random_generator(); MetaData md,mdGallery,mdOut,mdOut2,mdSort; MDRow row; FileName fnOut,fnOut2, fnGallery; fnOut = fnDir+"/clusteringTendency.xmd"; fnGallery = fnDir+"/gallery.doc"; fnOut2 = fnDir+"/validation.xmd"; size_t nSamplesRandom = 500; md.read(fnParticles); mdGallery.read(fnGallery); mdSort.sort(md,MDL_IMAGE_IDX,true,-1,0); size_t maxNImg; size_t sz = md.size(); if (useSignificant) mdSort.getValue(MDL_IMAGE_IDX,maxNImg,sz); else { mdSort.getValue(MDL_ITEM_ID,maxNImg,sz); } String expression; MDRow rowP,row2; SymList SL; int symmetry, sym_order; SL.readSymmetryFile(fnSym.c_str()); SL.isSymmetryGroup(fnSym.c_str(), symmetry, sym_order); /* double non_reduntant_area_of_sphere = SL.nonRedundantProjectionSphere(symmetry,sym_order); double area_of_sphere_no_symmetry = 4.*PI; double correction = std::sqrt(non_reduntant_area_of_sphere/area_of_sphere_no_symmetry); */ double correction = 1; double validation = 0; double num_images = 0; MetaData tempMd; std::vector<double> sum_u(nSamplesRandom); double sum_w=0; std::vector<double> H0(nSamplesRandom); std::vector<double> H(nSamplesRandom); std::vector<double> p(nSamplesRandom); if (rank==0) init_progress_bar(maxNImg); for (size_t idx=0; idx<=maxNImg;idx++) { if ((idx)%Nprocessors==rank) { if (useSignificant) expression = formatString("imageIndex == %lu",idx); else expression = formatString("itemId == %lu",idx); tempMd.importObjects(md, MDExpression(expression)); if (tempMd.size()==0) continue; //compute H_0 from noise obtainSumU_2(mdGallery, tempMd,sum_u,H0); //compute H from experimental obtainSumW(tempMd,sum_w,sum_u,H,correction); std::sort(H0.begin(),H0.end()); std::sort(H.begin(),H.end()); double P = 0; for(size_t j=0; j<sum_u.size();j++) { //P += H0.at(j)/H.at(j); P += H0.at(size_t((1-significance_noise)*nSamplesRandom))/H.at(j); p.at(j) = H0.at(j)/H.at(j); } P /= (nSamplesRandom); if (useSignificant) rowP.setValue(MDL_IMAGE_IDX,idx); else rowP.setValue(MDL_ITEM_ID,idx); rowP.setValue(MDL_WEIGHT,P); mdPartial.addRow(rowP); tempMd.clear(); if (rank==0) progress_bar(idx+1); } } if (rank==0) progress_bar(maxNImg); synchronize(); gatherClusterability(); if (rank == 0) { mdPartial.write(fnOut); std::vector<double> P; mdPartial.getColumnValues(MDL_WEIGHT,P); for (size_t idx=0; idx< P.size();idx++) { if (P[idx] > 1) validation += 1.; num_images += 1.; } validation /= (num_images); row2.setValue(MDL_IMAGE,fnInit); row2.setValue(MDL_WEIGHT,validation); mdOut2.addRow(row2); mdOut2.write(fnOut2); } }
void ProgValidationNonTilt::run() { //Clustering Tendency and Cluster Validity Stephen D. Scott randomize_random_generator(); //char buffer[400]; //sprintf(buffer, "xmipp_reconstruct_significant -i %s --initvolumes %s --odir %s --sym %s --iter 1 --alpha0 %f --angularSampling %f",fnIn.c_str(), fnInit.c_str(),fnDir.c_str(),fnSym.c_str(),alpha0,angularSampling); //system(buffer); MetaData md,mdOut,mdOut2; FileName fnMd,fnOut,fnOut2; fnMd = fnDir+"/angles_iter001_00.xmd"; fnOut = fnDir+"/clusteringTendency.xmd"; fnOut2 = fnDir+"/validation.xmd"; size_t nSamplesRandom = 250; md.read(fnMd); size_t maxNImg; size_t sz = md.size(); md.getValue(MDL_IMAGE_IDX,maxNImg,sz); String expression; MDRow rowP,row2; SymList SL; int symmetry, sym_order; SL.readSymmetryFile(fnSym.c_str()); SL.isSymmetryGroup(fnSym.c_str(), symmetry, sym_order); double non_reduntant_area_of_sphere = SL.nonRedundantProjectionSphere(symmetry,sym_order); double area_of_sphere_no_symmetry = 4.*PI; double correction = std::sqrt(non_reduntant_area_of_sphere/area_of_sphere_no_symmetry); double validation = 0; MetaData tempMd; std::vector<double> sum_u(nSamplesRandom); //std::vector<double> sum_w(nSamplesRandom); double sum_w=0; std::vector<double> H0(nSamplesRandom); std::vector<double> H(nSamplesRandom); if (rank==0) init_progress_bar(maxNImg); for (size_t idx=0; idx<=maxNImg;idx++) { if ((idx+1)%Nprocessors==rank) { expression = formatString("imageIndex == %lu",idx); tempMd.importObjects(md, MDExpression(expression)); if (tempMd.size()==0) continue; //compute H_0 from noise obtainSumU(tempMd,sum_u,H0); //compute H from experimental obtainSumW(tempMd,sum_w,sum_u,H,correction); std::sort(H0.begin(),H0.end()); std::sort(H.begin(),H.end()); double P = 0; for(size_t j=0; j<sum_u.size();j++) P += H0.at(j)/H.at(j); P /= (nSamplesRandom); rowP.setValue(MDL_IMAGE_IDX,idx); rowP.setValue(MDL_WEIGHT,P); mdPartial.addRow(rowP); //sum_u.clear(); //sum_w.clear(); //H0.clear(); //H.clear(); tempMd.clear(); if (rank==0) progress_bar(idx+1); } } if (rank==0) progress_bar(maxNImg); synchronize(); gatherClusterability(); if (rank == 0) { mdPartial.write(fnOut); std::vector<double> P; mdPartial.getColumnValues(MDL_WEIGHT,P); for (size_t idx=0; idx< P.size();idx++) { if (P[idx] > 1) validation += 1; } validation /= (maxNImg+1); } row2.setValue(MDL_IMAGE,fnInit); row2.setValue(MDL_WEIGHT,validation); mdOut2.addRow(row2); mdOut2.write(fnOut2); }