Пример #1
0
 void roundShifts(MDRow &row)
 {
     double aux = 0.;
     if (row.getValue(MDL_SHIFT_X, aux))
     {
         aux = (double)ROUND(aux);
         row.setValue(MDL_SHIFT_X, aux);
     }
     if (row.getValue(MDL_SHIFT_Y, aux))
     {
         aux = (double)ROUND(aux);
         row.setValue(MDL_SHIFT_Y, aux);
     }
 }
Пример #2
0
void ProgXrayImport::readGeoInfo(const FileName &fn, MDRow &rowGeo) const
{
    double tiltAngle;

    switch (dSource)
    {
    case MISTRAL:
        tiltAngle = dMi(anglesArray, fn.getPrefixNumber()-1);
        break;
    case BESSY:
    case GENERIC:
        {
            FileName fnBase = fn.withoutExtension();
            std::ifstream fhPosition;
            fhPosition.open((fnBase+"-positions.txt").c_str());
            if (!fhPosition)
                REPORT_ERROR(ERR_IO_NOTEXIST,fnBase+"-positions.txt");
            int itemsFound=0;
            std::string line;
            std::vector<std::string> tokens;
            while (!fhPosition.eof())
            {
                getline(fhPosition,line);
                splitString(line," ",tokens);
                if (tokens[0]=="xm:sample:rx")
                {
                    tiltAngle=textToFloat(tokens[1]);
                    itemsFound++;
                }
                if (itemsFound==1)
                    break;
            }
            fhPosition.close();
            if (itemsFound!=1)
                REPORT_ERROR(ERR_VALUE_EMPTY,(std::string)"Cannot find tilt angle in "+
                             fnBase+"-positions.txt");
        }
        break;
    }

    rowGeo.setValue(MDL_ANGLE_TILT, tiltAngle);
}
Пример #3
0
    void processImage(const FileName &fnImg, const FileName &fnImgOut, const MDRow &rowIn, MDRow &rowOut)
    {

        ImageGeneric img;

        switch (operation)
        {
        case HEADER_PRINT:
            img.read(fnImg, _HEADER_ALL);
            img.print();
            break;
        case HEADER_EXTRACT:
            img.read(fnImg, _HEADER_ALL);
            rowOut = img.getGeometry();
            if (round_shifts)
                roundShifts(rowOut);
            rowOut.setValue(MDL_IMAGE, fnImgOut);
            break;
        case HEADER_ASSIGN:
            rowOut = rowIn;
            if (round_shifts)
                roundShifts(rowOut);
            img.readApplyGeo(fnImg, rowOut, params);
            img.setDataMode(_HEADER_ALL);
            img.write(fnImg, ALL_IMAGES, fnImg.isInStack(), WRITE_REPLACE);
            break;
        case HEADER_RESET:
            img.read(fnImg, _HEADER_ALL);
            img.initGeometry();
            img.write(fnImg, ALL_IMAGES, fnImg.isInStack(), WRITE_REPLACE);
            break;
        case HEADER_SAMPLINGRATE:
            {
                img.read(fnImg, _HEADER_ALL);
                if (sampling < 0)
                {
                    double samplingRead;
                    img.image->MDMainHeader.getValue(MDL_SAMPLINGRATE_X, samplingRead);
                    std::cout << samplingRead << std::endl;
                }
                else
                {
                    img.image->MDMainHeader.setValue(MDL_SAMPLINGRATE_X, sampling);
                    img.image->MDMainHeader.setValue(MDL_SAMPLINGRATE_Y, sampling);
                    img.image->MDMainHeader.setValue(MDL_SAMPLINGRATE_Z, sampling);
                    img.write(fnImg, ALL_IMAGES, fnImg.isInStack(), WRITE_REPLACE);
                    std::cout << "New sampling rate (Angstrom) = " << sampling << std::endl;
                }
            }
            break;
        case HEADER_TREE:
            {
                XmippH5File H5File;
                FileName filename = fnImg.removeAllPrefixes().removeFileFormat();

                if (H5File.isHdf5(filename.c_str()))
                {
                    H5File.openFile(fnImg.removeAllPrefixes().removeFileFormat(), H5F_ACC_RDONLY);
                    H5File.showTree();
                }
                else
                    REPORT_ERROR(ERR_IMG_UNKNOWN, "Unknown file format to display its data structure.");
            }
            break;
        }
    }
void ProgSortByStatistics::run()
{
    // Process input selfile ..............................................
    SF.read(fn);
    SF.removeDisabled();
    MetaData SF2 = SF;
    SF = SF2;

    bool trained = false;

    if (fn_train != "")
    {
        SFtrain.read(fn_train);
        processInprocessInputPrepareSPTH(SFtrain,trained);
        trained = true;
        processInprocessInputPrepareSPTH(SF,trained);
    }
    else
        processInprocessInputPrepareSPTH(SF,trained);

    int imgno = 0;
    int numPCAs = pcaAnalyzer.size();

    MultidimArray<double> finalZscore(SF.size());
    MultidimArray<double> ZscoreShape1(SF.size()), sortedZscoreShape1;
    MultidimArray<double> ZscoreShape2(SF.size()), sortedZscoreShape2;
    MultidimArray<double> ZscoreSNR1(SF.size()), sortedZscoreSNR1;
    MultidimArray<double> ZscoreSNR2(SF.size()), sortedZscoreSNR2;
    MultidimArray<double> ZscoreHist(SF.size()), sortedZscoreHist;


    finalZscore.initConstant(0);
    ZscoreShape1.resizeNoCopy(finalZscore);
    ZscoreShape2.resizeNoCopy(finalZscore);
    ZscoreSNR1.resizeNoCopy(finalZscore);
    ZscoreSNR2.resizeNoCopy(finalZscore);
    ZscoreHist.resizeNoCopy(finalZscore);
    sortedZscoreShape1.resizeNoCopy(finalZscore);
    sortedZscoreShape2.resizeNoCopy(finalZscore);
    sortedZscoreSNR1.resizeNoCopy(finalZscore);
    sortedZscoreSNR2.resizeNoCopy(finalZscore);
    sortedZscoreHist.resizeNoCopy(finalZscore);

    double zScore=0;
    int enabled;

    FOR_ALL_OBJECTS_IN_METADATA(SF)
    {
        SF.getValue(MDL_ENABLED,enabled,__iter.objId);
        if ( (enabled==-1)  )
        {
            A1D_ELEM(finalZscore,imgno) = 1e3;
            A1D_ELEM(ZscoreShape1,imgno) = 1e3;
            A1D_ELEM(ZscoreShape2,imgno) = 1e3;
            A1D_ELEM(ZscoreSNR1,imgno) = 1e3;
            A1D_ELEM(ZscoreSNR2,imgno) = 1e3;
            A1D_ELEM(ZscoreHist,imgno) = 1e3;
            imgno++;
            enabled = 0;
        }
        else
        {
            for (int num = 0; num < numPCAs; ++num)
            {
                if (num == 0)
                {
                    A1D_ELEM(ZscoreSNR1,imgno) = pcaAnalyzer[num].getZscore(imgno);
                }
                else if (num == 1)
                {
                    A1D_ELEM(ZscoreShape2,imgno) = pcaAnalyzer[num].getZscore(imgno);
                }
                else if (num == 2)
                {
                    A1D_ELEM(ZscoreShape1,imgno) = pcaAnalyzer[num].getZscore(imgno);
                }
                else if (num == 3)
                {
                    A1D_ELEM(ZscoreSNR2,imgno) = pcaAnalyzer[num].getZscore(imgno);
                }
                else
                {
                    A1D_ELEM(ZscoreHist,imgno) = pcaAnalyzer[num].getZscore(imgno);
                }

                if(zScore < pcaAnalyzer[num].getZscore(imgno))
                    zScore = pcaAnalyzer[num].getZscore(imgno);
            }

            A1D_ELEM(finalZscore,imgno)=zScore;
            imgno++;
            zScore = 0;
        }
    }
    pcaAnalyzer.clear();

    // Produce output .....................................................
    MetaData SFout;
    std::ofstream fh_zind;

    if (verbose==2 && !fn_out.empty())
        fh_zind.open((fn_out.withoutExtension() + "_vectors.xmd").c_str(), std::ios::out);

    MultidimArray<int> sorted;
    finalZscore.indexSort(sorted);

    int nr_imgs = SF.size();
    bool thereIsEnable=SF.containsLabel(MDL_ENABLED);
    MDRow row;

    for (int imgno = 0; imgno < nr_imgs; imgno++)
    {
        int isort_1 = DIRECT_A1D_ELEM(sorted,imgno);
        int isort = isort_1 - 1;
        SF.getRow(row, isort_1);

        if (thereIsEnable)
        {
            int enabled;
            row.getValue(MDL_ENABLED, enabled);
            if (enabled==-1)
                continue;
        }

        double zscore=DIRECT_A1D_ELEM(finalZscore,isort);
        double zscoreShape1=DIRECT_A1D_ELEM(ZscoreShape1,isort);
        double zscoreShape2=DIRECT_A1D_ELEM(ZscoreShape2,isort);
        double zscoreSNR1=DIRECT_A1D_ELEM(ZscoreSNR1,isort);
        double zscoreSNR2=DIRECT_A1D_ELEM(ZscoreSNR2,isort);
        double zscoreHist=DIRECT_A1D_ELEM(ZscoreHist,isort);

        DIRECT_A1D_ELEM(sortedZscoreShape1,imgno)=DIRECT_A1D_ELEM(ZscoreShape1,isort);
        DIRECT_A1D_ELEM(sortedZscoreShape2,imgno)=DIRECT_A1D_ELEM(ZscoreShape2,isort);
        DIRECT_A1D_ELEM(sortedZscoreSNR1,imgno)=DIRECT_A1D_ELEM(ZscoreSNR1,isort);
        DIRECT_A1D_ELEM(sortedZscoreSNR2,imgno)=DIRECT_A1D_ELEM(ZscoreSNR2,isort);
        DIRECT_A1D_ELEM(sortedZscoreHist,imgno)=DIRECT_A1D_ELEM(ZscoreHist,isort);

        if (zscore>cutoff && cutoff>0)
        {
            row.setValue(MDL_ENABLED,-1);
            if (addToInput)
                SF.setValue(MDL_ENABLED,-1,isort_1);
        }
        else
        {
            row.setValue(MDL_ENABLED,1);
            if (addToInput)
                SF.setValue(MDL_ENABLED,1,isort_1);
        }

        row.setValue(MDL_ZSCORE,zscore);
        row.setValue(MDL_ZSCORE_SHAPE1,zscoreShape1);
        row.setValue(MDL_ZSCORE_SHAPE2,zscoreShape2);
        row.setValue(MDL_ZSCORE_SNR1,zscoreSNR1);
        row.setValue(MDL_ZSCORE_SNR2,zscoreSNR2);
        row.setValue(MDL_ZSCORE_HISTOGRAM,zscoreHist);

        if (addToInput)
        {
            SF.setValue(MDL_ZSCORE,zscore,isort_1);
            SF.setValue(MDL_ZSCORE_SHAPE1,zscoreShape1,isort_1);
            SF.setValue(MDL_ZSCORE_SHAPE2,zscoreShape2,isort_1);
            SF.setValue(MDL_ZSCORE_SNR1,zscoreSNR1,isort_1);
            SF.setValue(MDL_ZSCORE_SNR2,zscoreSNR2,isort_1);
            SF.setValue(MDL_ZSCORE_HISTOGRAM,zscoreHist,isort_1);
        }

        SFout.addRow(row);
    }

    //Sorting taking into account a given percentage
    if (per > 0)
    {
        MultidimArray<int> sortedShape1,sortedShape2,sortedSNR1,sortedSNR2,sortedHist,
        sortedShapeSF1,sortedShapeSF2,sortedSNR1SF,sortedSNR2SF,sortedHistSF;

        sortedZscoreShape1.indexSort(sortedShape1);
        sortedZscoreShape2.indexSort(sortedShape2);
        sortedZscoreSNR1.indexSort(sortedSNR1);
        sortedZscoreSNR2.indexSort(sortedSNR2);
        sortedZscoreHist.indexSort(sortedHist);
        size_t numPartReject = (size_t)std::floor((per/100)*SF.size());

        for (size_t numPar = SF.size()-1; numPar > (SF.size()-numPartReject); --numPar)
        {
            int isort_1 = DIRECT_A1D_ELEM(sortedShape1,numPar);
            SFout.getRow(row, isort_1);
            row.setValue(MDL_ENABLED,-1);
            SFout.setRow(row,isort_1);

            isort_1 = DIRECT_A1D_ELEM(sortedShape2,numPar);
            SFout.getRow(row, isort_1);
            row.setValue(MDL_ENABLED,-1);
            SFout.setRow(row,isort_1);

            isort_1 = DIRECT_A1D_ELEM(sortedSNR1,numPar);
            SFout.getRow(row, isort_1);
            row.setValue(MDL_ENABLED,-1);
            SFout.setRow(row,isort_1);

            isort_1 = DIRECT_A1D_ELEM(sortedSNR2,numPar);
            SFout.getRow(row, isort_1);
            row.setValue(MDL_ENABLED,-1);
            SFout.setRow(row,isort_1);

            isort_1 = DIRECT_A1D_ELEM(sortedHist,numPar);
            SFout.getRow(row, isort_1);
            row.setValue(MDL_ENABLED,-1);
            SFout.setRow(row,isort_1);

            if (addToInput)
            {
                ZscoreShape1.indexSort(sortedShapeSF1);
                ZscoreShape2.indexSort(sortedShapeSF2);
                ZscoreSNR1.indexSort(sortedSNR1SF);
                ZscoreSNR2.indexSort(sortedSNR2SF);
                ZscoreHist.indexSort(sortedHistSF);

                isort_1 = DIRECT_A1D_ELEM(sortedShapeSF1,numPar);
                SF.getRow(row, isort_1);
                row.setValue(MDL_ENABLED,-1);
                SF.setRow(row,isort_1);

                isort_1 = DIRECT_A1D_ELEM(sortedShapeSF2,numPar);
                SF.getRow(row, isort_1);
                row.setValue(MDL_ENABLED,-1);
                SF.setRow(row,isort_1);

                isort_1 = DIRECT_A1D_ELEM(sortedSNR1SF,numPar);
                SF.getRow(row, isort_1);
                row.setValue(MDL_ENABLED,-1);
                SF.setRow(row,isort_1);

                isort_1 = DIRECT_A1D_ELEM(sortedSNR2SF,numPar);
                SF.getRow(row, isort_1);
                row.setValue(MDL_ENABLED,-1);
                SF.setRow(row,isort_1);

                isort_1 = DIRECT_A1D_ELEM(sortedHistSF,numPar);
                SF.getRow(row, isort_1);
                row.setValue(MDL_ENABLED,-1);
                SF.setRow(row,isort_1);
            }
        }
    }

    if (verbose==2)
        fh_zind.close();
    if (!fn_out.empty())
    {
        MetaData SFsorted;
        SFsorted.sort(SFout,MDL_ZSCORE);
        SFout.write(fn_out,MD_OVERWRITE);
    }
    if (addToInput)
    {
        MetaData SFsorted;
        SFsorted.sort(SF,MDL_ZSCORE);
        SFsorted.write(fn,MD_APPEND);
    }
}