/* Do inference ------------------------------------------------------------ */ int NaiveBayes::doInference(const MultidimArray<double> &newFeatures, double &cost, Matrix1D<double> &classesProbs, Matrix1D<double> &allCosts) { classesProbs=__priorProbsLog10; for(int f=0; f<Nfeatures; f++) { const LeafNode &leaf_f=*(__leafs[f]); double newFeatures_f=DIRECT_A1D_ELEM(newFeatures,f); for (int k=0; k<K; k++) { double p = leaf_f.assignProbability(newFeatures_f, k); if (fabs(p) < 1e-2) VEC_ELEM(classesProbs,k) += -2*DIRECT_A1D_ELEM(__weights,f); else VEC_ELEM(classesProbs,k) += DIRECT_A1D_ELEM(__weights,f)*std::log10(p); #ifdef DEBUG_FINE_CLASSIFICATION if(debugging == true) { std::cout << "Feature " << f << " Probability for class " << k << " = " << classesProbs(k) << " increase= " << p << std::endl; char c; // COSS std::cin >> c; // if (c=='q') debugging = false; } #endif } } classesProbs-=classesProbs.computeMax(); // std::cout << "classesProbs " << classesProbs.transpose() << std::endl; for (int k=0; k<K; k++) VEC_ELEM(classesProbs,k)=pow(10.0,VEC_ELEM(classesProbs,k)); classesProbs*=1.0/classesProbs.sum(); // std::cout << "classesProbs norm " << classesProbs.transpose() << std::endl; allCosts=__cost*classesProbs; // std::cout << "allCosts " << allCosts.transpose() << std::endl; int bestk=0; cost=VEC_ELEM(allCosts,0)=std::log10(VEC_ELEM(allCosts,0)); for (int k=1; k<K; k++) { VEC_ELEM(allCosts,k)=std::log10(VEC_ELEM(allCosts,k)); if (VEC_ELEM(allCosts,k)<cost) { cost=VEC_ELEM(allCosts,k); bestk=k; } } #ifdef DEBUG_CLASSIFICATION if(debugging == true) { for (int k=0; k<K; k++) classesProbs(k)=log10(classesProbs(k)); std::cout << "Class probababilities=" << classesProbs.transpose() << "\n costs=" << allCosts.transpose() << " best class=" << bestk << " cost=" << cost << std::endl; char c; // COSS std::cin >> c; // if (c=='q') debugging = false; } #endif return bestk; }
void PolyZernikes::fit(const Matrix1D<int> & coef, MultidimArray<double> & im, MultidimArray<double> &weight, MultidimArray<bool> & ROI, int verbose) { this->create(coef); size_t xdim = XSIZE(im); size_t ydim = YSIZE(im); //int numZer = (size_t)coef.sum(); int numZer = (size_t)coef.sum(); //Actually polOrder corresponds to the polynomial order +1 int polOrder=(int)ZERNIKE_ORDER(coef.size()); im.setXmippOrigin(); Matrix2D<double> polValue(polOrder,polOrder); //First argument means number of images //Second argument means number of pixels WeightedLeastSquaresHelper weightedLeastSquaresHelper; Matrix2D<double>& zerMat=weightedLeastSquaresHelper.A; zerMat.resizeNoCopy((size_t)ROI.sum(), numZer); double iMaxDim2 = 2./std::max(xdim,ydim); size_t pixel_idx=0; weightedLeastSquaresHelper.b.resizeNoCopy((size_t)ROI.sum()); weightedLeastSquaresHelper.w.resizeNoCopy(weightedLeastSquaresHelper.b); FOR_ALL_ELEMENTS_IN_ARRAY2D(im) { if ( (A2D_ELEM(ROI,i,j))) { //For one i we swap the different j double y=i*iMaxDim2; double x=j*iMaxDim2; //polValue = [ 0 y y2 y3 ... // x xy xy2 xy3 ... // x2 x2y x2y2 x2y3 ] //dMij(polValue,py,px) py es fila, px es columna for (int py = 0; py < polOrder; ++py) { double ypy=std::pow(y,py); for (int px = 0; px < polOrder; ++px) dMij(polValue,px,py) = ypy*std::pow(x,px); } Matrix2D<int> *fMat; //We generate the representation of the Zernike polynomials for (int k=0; k < numZer; ++k) { fMat = &fMatV[k]; if (fMat == NULL) continue; double temp = 0; for (size_t px = 0; px < (*fMat).Xdim(); ++px) for (size_t py = 0; py < (*fMat).Ydim(); ++py) temp += dMij(*fMat,py,px)*dMij(polValue,py,px); dMij(zerMat,pixel_idx,k) = temp; } VEC_ELEM(weightedLeastSquaresHelper.b,pixel_idx)=A2D_ELEM(im,i,j); VEC_ELEM(weightedLeastSquaresHelper.w,pixel_idx)=std::abs(A2D_ELEM(weight,i,j)); ++pixel_idx; } } Matrix1D<double> zernikeCoefficients; weightedLeastSquares(weightedLeastSquaresHelper, zernikeCoefficients); fittedCoeffs = zernikeCoefficients; // Pointer to the image to be fitted MultidimArray<double> reconstructed; reconstructed.resizeNoCopy(im); pixel_idx=0; FOR_ALL_ELEMENTS_IN_ARRAY2D(im) if (A2D_ELEM(ROI,i,j)) { double temp=0; for (int k=0; k < numZer; ++k) temp+=dMij(zerMat,pixel_idx,k)*VEC_ELEM(fittedCoeffs,k); A2D_ELEM(reconstructed,i,j)=temp; if ( fabs(A2D_ELEM(reconstructed,i,j)-A2D_ELEM(im,i,j)) > PI) A2D_ELEM(ROI,i,j) = false; ++pixel_idx; } pixel_idx=0; if (verbose > 0) { Image<double> save; save()=reconstructed; save.write("reconstructedZernikes.xmp"); ROI.write("ROI.txt"); } }