Exemple #1
0
/* 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;
}
Exemple #2
0
//#define DEBUG
double spatial_Bspline03_proj(
    const Matrix1D<double> &r, const Matrix1D<double> &u)
{
    // Avoids divisions by zero and allows orthogonal rays computation
    static Matrix1D<double> ur(3);
    if (XX(u) == 0) XX(ur) = XMIPP_EQUAL_ACCURACY;
    else XX(ur) = XX(u);
    if (YY(u) == 0) YY(ur) = XMIPP_EQUAL_ACCURACY;
    else YY(ur) = YY(u);
    if (ZZ(u) == 0) ZZ(ur) = XMIPP_EQUAL_ACCURACY;
    else ZZ(ur) = ZZ(u);

    // Some precalculated variables
    double x_sign = SGN(XX(ur));
    double y_sign = SGN(YY(ur));
    double z_sign = SGN(ZZ(ur));

    // Compute the minimum and maximum alpha for the ray
    double alpha_xmin = (-2 - XX(r)) / XX(ur);
    double alpha_xmax = (2 - XX(r)) / XX(ur);
    double alpha_ymin = (-2 - YY(r)) / YY(ur);
    double alpha_ymax = (2 - YY(r)) / YY(ur);
    double alpha_zmin = (-2 - ZZ(r)) / ZZ(ur);
    double alpha_zmax = (2 - ZZ(r)) / ZZ(ur);

    double alpha_min = XMIPP_MAX(XMIPP_MIN(alpha_xmin, alpha_xmax),
                           XMIPP_MIN(alpha_ymin, alpha_ymax));
    alpha_min = XMIPP_MAX(alpha_min, XMIPP_MIN(alpha_zmin, alpha_zmax));
    double alpha_max = XMIPP_MIN(XMIPP_MAX(alpha_xmin, alpha_xmax),
                           XMIPP_MAX(alpha_ymin, alpha_ymax));
    alpha_max = XMIPP_MIN(alpha_max, XMIPP_MAX(alpha_zmin, alpha_zmax));
    if (alpha_max - alpha_min < XMIPP_EQUAL_ACCURACY) return 0.0;

#ifdef DEBUG
    std::cout << "Pixel:  " << r.transpose() << std::endl
    << "Dir:    " << ur.transpose() << std::endl
    << "Alpha x:" << alpha_xmin << " " << alpha_xmax << std::endl
    << "        " << (r + alpha_xmin*ur).transpose() << std::endl
    << "        " << (r + alpha_xmax*ur).transpose() << std::endl
    << "Alpha y:" << alpha_ymin << " " << alpha_ymax << std::endl
    << "        " << (r + alpha_ymin*ur).transpose() << std::endl
    << "        " << (r + alpha_ymax*ur).transpose() << std::endl
    << "Alpha z:" << alpha_zmin << " " << alpha_zmax << std::endl
    << "        " << (r + alpha_zmin*ur).transpose() << std::endl
    << "        " << (r + alpha_zmax*ur).transpose() << std::endl
    << "alpha  :" << alpha_min  << " " << alpha_max  << std::endl
    << std::endl;
#endif

    // Compute the first point in the volume intersecting the ray
    static Matrix1D<double> v(3);
    V3_BY_CT(v, ur, alpha_min);
    V3_PLUS_V3(v, r, v);

#ifdef DEBUG
    std::cout << "First entry point: " << v.transpose() << std::endl;
    std::cout << "   Alpha_min: " << alpha_min << std::endl;
#endif

    // Follow the ray
    double alpha = alpha_min;
    double ray_sum = 0;
    do
    {
        double alpha_x = (XX(v) + x_sign - XX(r)) / XX(ur);
        double alpha_y = (YY(v) + y_sign - YY(r)) / YY(ur);
        double alpha_z = (ZZ(v) + z_sign - ZZ(r)) / ZZ(ur);

        // Which dimension will ray move next step into?, it isn't neccesary to be only
        // one.
        double diffx = ABS(alpha - alpha_x);
        double diffy = ABS(alpha - alpha_y);
        double diffz = ABS(alpha - alpha_z);
        double diff_alpha = XMIPP_MIN(XMIPP_MIN(diffx, diffy), diffz);
        ray_sum += spatial_Bspline03_integral(r, ur, alpha, alpha + diff_alpha);

        // Update alpha and the next entry point
        if (ABS(diff_alpha - diffx) <= XMIPP_EQUAL_ACCURACY) alpha = alpha_x;
        if (ABS(diff_alpha - diffy) <= XMIPP_EQUAL_ACCURACY) alpha = alpha_y;
        if (ABS(diff_alpha - diffz) <= XMIPP_EQUAL_ACCURACY) alpha = alpha_z;
        XX(v) += diff_alpha * XX(ur);
        YY(v) += diff_alpha * YY(ur);
        ZZ(v) += diff_alpha * ZZ(ur);

#ifdef DEBUG
        std::cout << "Alpha x,y,z: " << alpha_x << " " << alpha_y
        << " " << alpha_z << " ---> " << alpha << std::endl;

        std::cout << "    Next entry point: " << v.transpose() << std::endl
        << "    diff_alpha: " << diff_alpha << std::endl
        << "    ray_sum: " << ray_sum << std::endl
        << "    Alfa tot: " << alpha << "alpha_max: " << alpha_max <<
        std::endl;

#endif
    }
    while ((alpha_max - alpha) > XMIPP_EQUAL_ACCURACY);
    return ray_sum;
}