CoverageStats getVariantCoverage(BamTools::BamReader* pReader, const VCFRecord& record, const ReadTable* refTable)
{
    CoverageStats stats;
    
    static const int flankingSize = 100;
    static const double minPercentIdentity = 95.0f;

    bool is_snv = record.refStr.size() == 1 && record.varStr.size() == 1;

    // Grab the reference haplotype
    int eventLength = record.varStr.length();
    int zeroBasedPos = record.refPosition - 1;
    int start = zeroBasedPos - flankingSize - 1;
    if(start < 0)
        start = 0;

    int end = zeroBasedPos + eventLength + 2 * flankingSize;
    const SeqItem& chr = refTable->getRead(record.refName);
    if(end > (int)chr.seq.length())
        end = (int)chr.seq.length();

    std::string reference_haplotype = chr.seq.substr(start, end - start);
    int translatedPos = zeroBasedPos - start;

    std::string variant_haplotype = reference_haplotype;
    
    // Ensure that the reference string at the variant matches the expected
    assert(variant_haplotype.substr(translatedPos, record.refStr.length()) == record.refStr);
    variant_haplotype.replace(translatedPos, record.refStr.length(), record.varStr);

    // Grab all reads in reference region
    int refID = pReader->GetReferenceID(record.refName);
    if(refID < 0)
        return stats;

    int refStart = record.refPosition;
    int refEnd = record.refPosition;
    pReader->SetRegion(refID, refStart, refID, refEnd);
    BamTools::BamAlignment aln;

    std::vector<double> mapping_quality;
    std::vector<BamTools::BamAlignment> alignments;
    while(pReader->GetNextAlignment(aln)) {
        if(aln.MapQuality > 0)
            alignments.push_back(aln);
        mapping_quality.push_back(aln.MapQuality);
    }

    if(!mapping_quality.empty())
        stats.median_mapping_quality = median(mapping_quality);
    else
        stats.median_mapping_quality = 60;

    // Shuffle and take the first 200 alignments only
    std::random_shuffle(alignments.begin(), alignments.end());

    for(size_t i = 0; i < alignments.size() && i < opt::capAlignments; ++i) {
        BamTools::BamAlignment alignment = alignments[i];

        VariantReadSegments segments = splitReadAtVariant(alignment, record);

        if(opt::verbose > 1)
        {
            fprintf(stderr, "var: %zu %s -> %s\n",  record.refPosition, record.refStr.c_str(), record.varStr.c_str());
            fprintf(stderr, "pos: %d\n",  alignment.Position);
            fprintf(stderr, "strand: %s\n", alignment.IsReverseStrand() ? "-" : "+");
            fprintf(stderr, "read: %s\n", alignment.QueryBases.c_str());
            fprintf(stderr, "qual: %s\n", alignment.Qualities.c_str());
            fprintf(stderr, "alnb: %s\n", alignment.AlignedBases.c_str());
            
            fprintf(stderr, "Pre: %s\n",  segments.preSegment.c_str());
            fprintf(stderr, "Var: %s\n",  segments.variantSegment.c_str());
            fprintf(stderr, "Pos: %s\n",  segments.postSegment.c_str());
            
            fprintf(stderr, "PreQual: %s\n",  segments.preQual.c_str());
            fprintf(stderr, "VarQual: %s\n",  segments.variantQual.c_str());
            fprintf(stderr, "PosQual: %s\n",  segments.postQual.c_str());
        }

        bool aligned_at_variant = segments.variantSegment.size() > 0 && 
                                  (segments.preSegment.size() > 0 || segments.postSegment.size() > 0);

        if(!aligned_at_variant)
            continue;
                                        
        stats.n_total_reads += 1;
        
        if(segments.variantSegment == record.refStr)
            continue; // not an evidence read

        // Align the read to the reference and variant haplotype
        SequenceOverlap ref_overlap = Overlapper::computeOverlapAffine(alignment.QueryBases, reference_haplotype);
        SequenceOverlap var_overlap = Overlapper::computeOverlapAffine(alignment.QueryBases, variant_haplotype);
        
        bool quality_alignment = (ref_overlap.getPercentIdentity() >= minPercentIdentity || 
                                 var_overlap.getPercentIdentity() >= minPercentIdentity);

        bool is_evidence_read = quality_alignment && var_overlap.score > ref_overlap.score;
        if(is_evidence_read)
        {
            stats.n_evidence_reads += 1;
            if(is_snv && segments.variantQual.size() == 1)
            {
                char qb = segments.variantQual[0];
                int q = Quality::char2phred(qb);
                stats.snv_evidence_quals.push_back(q);
            }
        }
    }

    return stats;
}
Example #2
0
void CMT::estimate(const std::vector<std::pair<cv::KeyPoint, int> >& keypointsIN, cv::Point2f& center, float& scaleEstimate, float& medRot, std::vector<std::pair<cv::KeyPoint, int> >& keypoints)
{
    center = cv::Point2f(NAN,NAN);
    scaleEstimate = NAN;
    medRot = NAN;

    //At least 2 keypoints are needed for scale
    if(keypointsIN.size() > 1)
    {
        //sort
        std::vector<PairInt> list;
        for(int i = 0; i < keypointsIN.size(); i++)
            list.push_back(std::make_pair(keypointsIN[i].second, i));
        std::sort(&list[0], &list[0]+list.size(), comparatorPair<int>);
        for(int i = 0; i < list.size(); i++)
            keypoints.push_back(keypointsIN[list[i].second]);

        std::vector<int> ind1;
        std::vector<int> ind2;
        for(int i = 0; i < list.size(); i++)
            for(int j = 0; j < list.size(); j++)
            {
                if(i != j && keypoints[i].second != keypoints[j].second)
                {
                    ind1.push_back(i);
                    ind2.push_back(j);
                }
            }
        if(ind1.size() > 0)
        {
            std::vector<int> class_ind1;
            std::vector<int> class_ind2;
            std::vector<cv::KeyPoint> pts_ind1;
            std::vector<cv::KeyPoint> pts_ind2;
            for(int i = 0; i < ind1.size(); i++)
            {
                class_ind1.push_back(keypoints[ind1[i]].second-1);
                class_ind2.push_back(keypoints[ind2[i]].second-1);
                pts_ind1.push_back(keypoints[ind1[i]].first);
                pts_ind2.push_back(keypoints[ind2[i]].first);
            }

            std::vector<float> scaleChange;
            std::vector<float> angleDiffs;
            for(int i = 0; i < pts_ind1.size(); i++)
            {
                cv::Point2f p = pts_ind2[i].pt - pts_ind1[i].pt;
                //This distance might be 0 for some combinations,
                //as it can happen that there is more than one keypoint at a single location
                float dist = sqrt(p.dot(p));
                float origDist = squareForm[class_ind1[i]][class_ind2[i]];
                scaleChange.push_back(dist/origDist);
                //Compute angle
                float angle = atan2(p.y, p.x);
                float origAngle = angles[class_ind1[i]][class_ind2[i]];
                float angleDiff = angle - origAngle;
                //Fix long way angles
                if(fabs(angleDiff) > CV_PI)
                    angleDiff -= sign(angleDiff) * 2 * CV_PI;
                angleDiffs.push_back(angleDiff);
            }
            scaleEstimate = median(scaleChange);
            if(!estimateScale)
                scaleEstimate = 1;
            medRot = median(angleDiffs);
            if(!estimateRotation)
                medRot = 0;
            votes = std::vector<cv::Point2f>();
            for(int i = 0; i < keypoints.size(); i++)
                votes.push_back(keypoints[i].first.pt - scaleEstimate * rotate(springs[keypoints[i].second-1], medRot));
            //Compute linkage between pairwise distances
            std::vector<Cluster> linkageData = linkage(votes);

            //Perform hierarchical distance-based clustering
            std::vector<int> T = fcluster(linkageData, thrOutlier);
            //Count votes for each cluster
            std::vector<int> cnt = binCount(T);
            //Get largest class
            int Cmax = argmax(cnt);

            //Remember outliers
            outliers = std::vector<std::pair<cv::KeyPoint, int> >();
            std::vector<std::pair<cv::KeyPoint, int> > newKeypoints;
            std::vector<cv::Point2f> newVotes;
            for(int i = 0; i < keypoints.size(); i++)
            {
                if(T[i] != Cmax)
                    outliers.push_back(keypoints[i]);
                else
                {
                    newKeypoints.push_back(keypoints[i]);
                    newVotes.push_back(votes[i]);
                }
            }
            keypoints = newKeypoints;

            center = cv::Point2f(0,0);
            for(int i = 0; i < newVotes.size(); i++)
                center += newVotes[i];
            center *= (1.0/newVotes.size());
        }
    }
}
Example #3
0
int main(int argc, char* argv[])
{
  if (argc > 2)
  {
    std::cout << "usage:\n";
    std::cout << "./count [<file>]\n\n";
    std::cout << "  file           Path to the input file containing the tweets\n";
    return 1;
  }

  std::cout << "executing " << argv[0] << std::endl;

  std::string directory;
  const size_t last_slash_idx = std::string(argv[0]).rfind('/');
  if (std::string::npos != last_slash_idx)
  {
    directory = std::string(argv[0]).substr(0, last_slash_idx);
  }

  std::string tweets;  

  if (argc == 2) 
    tweets = std::string(argv[1]);
  else
    tweets = directory + "/../tweet_input/tweets.txt";

  int max_string_size = 0;
  std::map<std::string, int> table;
  std::string aa = directory + "/../tweet_output/ft1.txt";
  std::string bb = directory + "/../tweet_output/ft2.txt"; 
  std::ofstream wc_result(aa.c_str());
  std::ofstream ft2(bb.c_str());
  std::ifstream file;

  file.open(tweets.c_str());
  if (!file.is_open())
  { 
    std::cerr << "ERROR: file not found "  << tweets << std::endl;
    throw;
  }
  if (!wc_result.is_open())
  {
    std::cerr << "ERROR: can not create ouput file" << std::endl;
    throw;
  };

  std::vector<int> words_per_line;
  std::string line;
  while (std::getline(file, line))
  {
    std::set<std::string> line_words;

    //std::cout << line << std::endl;
    std::istringstream is(line);
    std::string word;
    while (is >> word)
    {
      line_words.insert(word);
      if (max_string_size < word.size())
        max_string_size = word.size();
      std::map<std::string, int>::iterator here = table.find(word);
      if (here == table.end())
        table.insert(std::make_pair(word, 1));
      else
       (*here).second ++;
    }
    insert_sort(words_per_line, line_words.size());
    ft2 << std::fixed << std::setprecision(1) << median(words_per_line) << std::endl;
    //ft2 << median(words_per_line) << std::endl;
  }

  file.close();

  std::map<std::string, int>::iterator it = table.begin();

  for ( ; it != table.end(); ++it)
  {
    wc_result << std::left << std::setw(max_string_size) << std::setfill(' ')        << (*it).first;
    wc_result << " " << (*it).second << std::endl;
  }

  wc_result.close();

}
Example #4
0
/*
 * Determines and returns the new Tx rate index.
 */
A_UINT16
rcRateFind(struct ath_softc *sc, struct atheros_node *pSib,
           A_UINT32 frameLen,
           const RATE_TABLE *pRateTable,
           HAL_CHANNEL *curchan, int isretry)
{
#ifdef notyet
    WLAN_STA_CONFIG      *pConfig     = &pdevInfo->staConfig;
    VPORT_BSS            *pVportXrBss = GET_XR_BSS(pdevInfo);
    WLAN_DATA_MAC_HEADER *pHdr        = pDesc->pBufferVirtPtr.header;
#endif
    struct TxRateCtrl_s  *pRc;
    A_UINT32             dt;
    A_UINT32             bestThruput,thisThruput = 0;
    A_UINT32             nowMsec;
    A_UINT8              rate, nextRate, bestRate;
    A_RSSI               rssiLast, rssiReduce;
#if ATH_SUPERG_DYNTURBO
    A_UINT8              currentPrimeState = IS_CHAN_TURBO(curchan);
    /* 0 = regular; 1 = turbo */
    A_UINT8              primeInUse        = sc->sc_dturbo;
#else
    A_UINT8              primeInUse        = 0;
    A_UINT8              currentPrimeState = 0;
#endif
#ifdef notyet
    A_UINT8              xrRateAdaptation = FALSE;
#endif
    int               isChanTurbo      = FALSE;
    A_UINT8              maxIndex, minIndex;
    A_INT8               index;
    int               isProbing = FALSE;

    /* have the real rate control logic kick in */
    pRc = &pSib->txRateCtrl;

#if ATH_SUPERG_DYNTURBO
    /*
     * Reset primeInUse state, if we are currently using XR
     * rate tables or if we have any clients associated in XR mode
     */
#ifdef notyet
    if ((pRateTable == sc->sc_rates[WLAN_MODE_XR]) ||
            (isXrAp(sc) && (pVportXrBss->bss.numAssociatedClients > 0)))
    {
        currentPrimeState = 0;
        primeInUse = 0;
    }
#endif

    /* make sure that rateMax is correct when using TURBO_PRIME tables */
    if (currentPrimeState)
    {
        pRc->rateMax = pRateTable->rateCount - 1;
    } else
    {
        pRc->rateMax = pRateTable->rateCount - 1 - pRateTable->numTurboRates;
    }
#endif /* ATH_SUPERG_DYNTURBO */

    rssiLast   = median(pRc->rssiLast, pRc->rssiLastPrev, pRc->rssiLastPrev2);
    rssiReduce = 0;

    /*
     * Age (reduce) last ack rssi based on how old it is.
     * The bizarre numbers are so the delta is 160msec,
     * meaning we divide by 16.
     *   0msec   <= dt <= 25msec:   don't derate
     *   25msec  <= dt <= 185msec:  derate linearly from 0 to 10dB
     *   185msec <= dt:             derate by 10dB
     */
    nowMsec = A_MS_TICKGET();
    dt = nowMsec - pRc->rssiTime;

    if (dt >= 185) {
        rssiReduce = 10;
    } else if (dt >= 25) {
        rssiReduce = (A_UINT8)((dt - 25) >> 4);
    }
MainWindow::MainWindow()
{
	openAction = new QAction(tr("&Open"), this);
	saveAction = new QAction(tr("&Save"), this);
	exitAction = new QAction(tr("E&xit"), this);
	equalAction = new QAction(tr("&Equalization"), this);
	otsuAction = new QAction(tr("&otsu"), this);
	isodataAction = new QAction(tr("&Isodata"), this);
	manualAction = new QAction(tr("&Manual"), this);
	gammaAction = new QAction(tr("&Gamma"), this);
	stretchingAction = new QAction(tr("&Stretching"), this);
	sigmaAction = new QAction(tr("&Sigma"), this);
	medianAction = new QAction(tr("&Median"), this);
	lineAction = new QAction(tr("&Lines"), this);
	pixelAction = new QAction(tr("&Pixels"), this);
	gaussianAction = new QAction(tr("&Gaussian"), this);
	sobelAction = new QAction(tr("&Sobel"), this);	
	horizontalAction = new QAction(tr("&Line Intensity"), this);
	cannyAction = new QAction(tr("&Canny"),this);
	sumAction = new QAction(tr("&Add"),this);
	resAction = new QAction(tr("&Substract"),this);
	multAction = new QAction(tr("&Multiply"),this);
	divAction = new QAction(tr("&Divide"),this);
	avgAction = new QAction(tr("A&verage"),this);
	andAction = new QAction(tr("&And"),this);
	orAction = new QAction(tr("&Or"),this);
	xorAction = new QAction(tr("&Xor"),this);
	notAction = new QAction(tr("&Not"),this);
	minAction = new QAction(tr("M&in"),this);
	maxAction = new QAction(tr("M&ax"),this);
	kmeansAction = new QAction(tr("&Kmeans"),this);

	saveAction->setEnabled(false);

	connect(openAction, SIGNAL(triggered()), this, SLOT(open()));
	connect(saveAction, SIGNAL(triggered()), this, SLOT(save()));
	connect(exitAction, SIGNAL(triggered()), qApp, SLOT(quit()));
	connect(equalAction, SIGNAL(triggered()), this, SLOT(equalization()));
	connect(otsuAction, SIGNAL(triggered()), this, SLOT(otsuThresh()));
	connect(isodataAction, SIGNAL(triggered()), this, SLOT(isodataSlot()));
	connect(manualAction, SIGNAL(triggered()), this, SLOT(manual()));
	connect(gammaAction,SIGNAL(triggered()),this,SLOT(gamma()));
	connect(stretchingAction,SIGNAL(triggered()),this,SLOT(stretching()));
	connect(sigmaAction, SIGNAL(triggered()), this, SLOT(sigma()));
	connect(medianAction, SIGNAL(triggered()), this, SLOT(median()));
	connect(lineAction, SIGNAL(triggered()), this, SLOT(line()));
	connect(pixelAction, SIGNAL(triggered()), this, SLOT(pixel()));
	connect(gaussianAction, SIGNAL(triggered()), this, SLOT(gaussian()));
	connect(sobelAction, SIGNAL(triggered()), this, SLOT(sobelEdges()));
	connect(horizontalAction,SIGNAL(triggered()),this,SLOT(horizontal()));
	connect(cannyAction,SIGNAL(triggered()),this,SLOT(cannySlot()));
	connect(sumAction,SIGNAL(triggered()),this,SLOT(sum()));
	connect(resAction,SIGNAL(triggered()),this,SLOT(res()));
	connect(multAction,SIGNAL(triggered()),this,SLOT(mult()));
	connect(divAction,SIGNAL(triggered()),this,SLOT(div()));
	connect(avgAction,SIGNAL(triggered()),this,SLOT(avg()));
	connect(andAction,SIGNAL(triggered()),this,SLOT(andSlot()));
	connect(orAction,SIGNAL(triggered()),this,SLOT(orSlot()));
	connect(xorAction,SIGNAL(triggered()),this,SLOT(xorSlot()));
	connect(notAction,SIGNAL(triggered()),this,SLOT(notSlot()));
	connect(minAction,SIGNAL(triggered()),this,SLOT(minSlot()));
	connect(maxAction,SIGNAL(triggered()),this,SLOT(maxSlot()));
	connect(kmeansAction,SIGNAL(triggered()),this,SLOT(kmeansSlot()));
	
	fileMenu = menuBar()->addMenu(tr("&File"));
	equalizationMenu = menuBar()->addMenu(tr("&Equalization"));
	thresholdMenu = menuBar()->addMenu(tr("&Thresholding"));
	contrastMenu = menuBar()->addMenu(tr("&Contrast"));
	noiseMenu = menuBar()->addMenu(tr("&Noise"));
	edgeMenu = menuBar()->addMenu(tr("E&dge"));
	operationMenu = menuBar()->addMenu(tr("&Operation"));
	boolMenu = operationMenu->addMenu(tr("&Boolean"));
	arithMenu = operationMenu->addMenu(tr("&Arithmetic"));
	relMenu = operationMenu->addMenu(tr("&Relational"));
	segmentationMenu = menuBar()->addMenu(tr("&Segmentation"));
	
	equalizationMenu->setEnabled(false);
	thresholdMenu->setEnabled(false);
	contrastMenu->setEnabled(false);
	noiseMenu->setEnabled(false);
	edgeMenu->setEnabled(false);
	operationMenu->setEnabled(false);
	segmentationMenu->setEnabled(false);
	
	fileMenu->addAction(openAction);
	fileMenu->addAction(saveAction);
	fileMenu->addSeparator();
	fileMenu->addAction(exitAction);

	equalizationMenu->addAction(equalAction);
	
	thresholdMenu->addAction(otsuAction);
	thresholdMenu->addAction(isodataAction);
	thresholdMenu->addAction(manualAction);
	
	contrastMenu->addAction(gammaAction);
	contrastMenu->addAction(stretchingAction);
	
	noiseMenu->addAction(sigmaAction);
	noiseMenu->addAction(medianAction);
	noiseMenu->addAction(lineAction);
	noiseMenu->addAction(pixelAction);
	noiseMenu->addAction(gaussianAction);
	
	edgeMenu->addAction(sobelAction);
	edgeMenu->addAction(horizontalAction);
	edgeMenu->addAction(cannyAction);

	boolMenu->addAction(andAction);
	boolMenu->addAction(orAction);
	boolMenu->addAction(xorAction);
	boolMenu->addAction(notAction);
	
	arithMenu->addAction(sumAction);
	arithMenu->addAction(resAction);
	arithMenu->addAction(multAction);
	arithMenu->addAction(divAction);
	arithMenu->addAction(avgAction);
	
	relMenu->addAction(minAction);
	relMenu->addAction(maxAction);
	
	segmentationMenu->addAction(kmeansAction);
	//-----

	viewer = new ImageViewer(this);
	
	QScrollArea * scrollArea = new QScrollArea;
	scrollArea->setWidget(viewer);
	scrollArea->setFixedWidth(600);
    scrollArea->setWidgetResizable(true);
    
 	boxW = new QSpinBox();
 	boxW->setEnabled(false);
 	boxW->setMaximum(65535);
 	
 	boxC = new QSpinBox();
 	boxC->setEnabled(false);
 	boxC->setMaximum(65535);
	
	histoViewer = new ImageViewer(this);
	QScrollArea * histoArea = new QScrollArea;
	histoArea->setWidget(histoViewer);
	histoArea->setFixedSize(268,278);
    histoArea->setWidgetResizable(false);
	
	QVBoxLayout * rightLayout = new QVBoxLayout;
	rightLayout->addWidget(new QLabel("Window:",this));
	rightLayout->addWidget(boxW);
	rightLayout->addWidget(new QLabel("Level:",this));
	rightLayout->addWidget(boxC);
	rightLayout->addWidget(histoArea);
	
	connect(boxW,SIGNAL(valueChanged(int)),this,SLOT(changeW(int)));
	connect(boxC,SIGNAL(valueChanged(int)),this,SLOT(changeC(int)));
	
	QWidget * rightSide = new QWidget;
	rightSide->setLayout(rightLayout);
	
	QHBoxLayout *mainLayout = new QHBoxLayout;
	mainLayout->addWidget(scrollArea);
	mainLayout->addWidget(rightSide);	

	
	QWidget * centralWidget = new QWidget();
	centralWidget->setLayout(mainLayout);
	
	
	setCentralWidget(centralWidget);

	setWindowTitle(tr("DICOM Image Processor"));
    setFixedSize(QSize(900,600));
}
Example #6
0
ffdotpows *subharm_ffdot_plane(int numharm, int harmnum,
                               double fullrlo, double fullrhi,
                               subharminfo * shi, accelobs * obs)
{
   int ii, lobin, hibin, numdata, nice_numdata, nrs, fftlen, binoffset;
   static int numrs_full = 0, numzs_full = 0;
   float powargr, powargi;
   double drlo, drhi, harm_fract;
   ffdotpows *ffdot;
   fcomplex *data, **result;
   presto_datainf datainf;

   if (numrs_full == 0) {
      if (numharm == 1 && harmnum == 1) {
         numrs_full = ACCEL_USELEN;
         numzs_full = shi->numkern;
      } else {
         printf("You must call subharm_ffdot_plane() with numharm=1 and\n");
         printf("harnum=1 before you use other values!  Exiting.\n\n");
         exit(0);
      }
   }
   ffdot = (ffdotpows *) malloc(sizeof(ffdotpows));

   /* Calculate and get the required amplitudes */

   harm_fract = (double) harmnum / (double) numharm;
   drlo = calc_required_r(harm_fract, fullrlo);
   drhi = calc_required_r(harm_fract, fullrhi);
   ffdot->rlo = (int) floor(drlo);
   ffdot->zlo = calc_required_z(harm_fract, obs->zlo);

   /* Initialize the lookup indices */
   if (numharm > 1) {
      double rr, subr;
      for (ii = 0; ii < numrs_full; ii++) {
         rr = fullrlo + ii * ACCEL_DR;
         subr = calc_required_r(harm_fract, rr);
         shi->rinds[ii] = index_from_r(subr, ffdot->rlo);
      }
   }
   ffdot->rinds = shi->rinds;
   ffdot->numrs = (int) ((ceil(drhi) - floor(drlo))
                         * ACCEL_RDR + DBLCORRECT) + 1;
   if (numharm == 1 && harmnum == 1) {
      ffdot->numrs = ACCEL_USELEN;
   } else {
      if (ffdot->numrs % ACCEL_RDR) {
         ffdot->numrs = (ffdot->numrs / ACCEL_RDR + 1) * ACCEL_RDR;
      }
   }
   ffdot->numzs = shi->numkern;
   binoffset = shi->kern[0].kern_half_width;
   fftlen = shi->kern[0].fftlen;
   lobin = ffdot->rlo - binoffset;
   hibin = (int) ceil(drhi) + binoffset;
   numdata = hibin - lobin + 1;
   nice_numdata = next2_to_n(numdata);  // for FFTs
   data = get_fourier_amplitudes(lobin, nice_numdata, obs);
   if (!obs->mmap_file && !obs->dat_input)
       printf("This is newly malloc'd!\n");

   // Normalize the Fourier amplitudes

   if (obs->nph > 0.0) {
       //  Use freq 0 normalization if requested (i.e. photons)
       double norm = 1.0 / sqrt(obs->nph);
       for (ii = 0; ii < numdata; ii++) {
           data[ii].r *= norm;
           data[ii].i *= norm;
       }
   } else if (obs->norm_type == 0) {
       //  old-style block median normalization
       float *powers;
       double norm;

       powers = gen_fvect(numdata);
       for (ii = 0; ii < numdata; ii++)
           powers[ii] = POWER(data[ii].r, data[ii].i);
       norm = 1.0 / sqrt(median(powers, numdata)/log(2.0));
       free(powers);
       for (ii = 0; ii < numdata; ii++) {
           data[ii].r *= norm;
           data[ii].i *= norm;
       }
   } else {
       //  new-style running double-tophat local-power normalization
       float *powers, *loc_powers;

       powers = gen_fvect(nice_numdata);
       for (ii = 0; ii < nice_numdata; ii++) {
           powers[ii] = POWER(data[ii].r, data[ii].i);
       }
       loc_powers = corr_loc_pow(powers, nice_numdata);
       for (ii = 0; ii < numdata; ii++) {
           float norm = invsqrt(loc_powers[ii]);
           data[ii].r *= norm;
           data[ii].i *= norm;
       }
       free(powers);
       free(loc_powers);
   }

   /* Perform the correlations */

   result = gen_cmatrix(ffdot->numzs, ffdot->numrs);
   datainf = RAW;
   for (ii = 0; ii < ffdot->numzs; ii++) {
      nrs = corr_complex(data, numdata, datainf,
                         shi->kern[ii].data, fftlen, FFT,
                         result[ii], ffdot->numrs, binoffset,
                         ACCEL_NUMBETWEEN, binoffset, CORR);
      datainf = SAME;
   }

   // Always free data
   free(data);

   /* Convert the amplitudes to normalized powers */

   ffdot->powers = gen_fmatrix(ffdot->numzs, ffdot->numrs);
   for (ii = 0; ii < (ffdot->numzs * ffdot->numrs); ii++)
      ffdot->powers[0][ii] = POWER(result[0][ii].r, result[0][ii].i);
   free(result[0]);
   free(result);
   return ffdot;
}
Example #7
0
void testMedian(double *x, int * n, double * res)
{
  int err;
  *res = median(x, (size_t) *n, 0, &err);
} 
Example #8
0
static double resolveDistance(const MultiDistance &distance) {
    return median(distance.r, distance.g, distance.b);
}
Example #9
0
struct tree *build_tree(int n , int dir , int lo , int num_proc , double min_x , double max_x ,
                        double min_y , double max_y ) 
{ double med ;
  Tree___0 t ;
  void *tmp ;
  void *__cil_tmp12 ;
  unsigned int __cil_tmp13 ;
  unsigned int __cil_tmp14 ;
  int __cil_tmp15 ;
  int __cil_tmp16 ;
  int __cil_tmp17 ;
  int __cil_tmp18 ;
  unsigned int __cil_tmp19 ;
  unsigned int __cil_tmp20 ;
  int __cil_tmp21 ;
  int __cil_tmp22 ;
  unsigned int __cil_tmp23 ;
  unsigned int __cil_tmp24 ;
  unsigned int __cil_tmp25 ;
  unsigned int __cil_tmp26 ;
  unsigned int __cil_tmp27 ;
  unsigned int __cil_tmp28 ;
  int __cil_tmp29 ;
  int __cil_tmp30 ;
  int __cil_tmp31 ;
  int __cil_tmp32 ;
  unsigned int __cil_tmp33 ;
  unsigned int __cil_tmp34 ;
  int __cil_tmp35 ;
  int __cil_tmp36 ;
  unsigned int __cil_tmp37 ;
  unsigned int __cil_tmp38 ;
  unsigned int __cil_tmp39 ;
  unsigned int __cil_tmp40 ;
  unsigned int __cil_tmp41 ;
  unsigned int __cil_tmp42 ;
  void *__cil_tmp43 ;
  unsigned int __cil_tmp44 ;
  unsigned int __cil_tmp45 ;
  void *__cil_tmp46 ;
  struct tree **mem_47 ;
  struct tree **mem_48 ;
  double *mem_49 ;
  double *mem_50 ;
  struct tree **mem_51 ;
  struct tree **mem_52 ;
  double *mem_53 ;
  double *mem_54 ;
  int *mem_55 ;
  struct tree **mem_56 ;
  struct tree **mem_57 ;

  {
#line 84
  if (n == 0) {
    {
#line 84
    __cil_tmp12 = (void *)0;
#line 84
    return ((struct tree *)__cil_tmp12);
    }
  } else {

  }
  {
#line 86
  tmp = malloc(36U);
#line 86
  t = (struct tree___0 *)tmp;
  }
#line 87
  if (dir) {
    {
#line 88
    dir = ! dir;
#line 89
    med = median(min_x, max_x, n);
#line 91
    __cil_tmp13 = (unsigned int )t;
#line 91
    __cil_tmp14 = __cil_tmp13 + 20;
#line 91
    __cil_tmp15 = n / 2;
#line 91
    __cil_tmp16 = num_proc / 2;
#line 91
    __cil_tmp17 = lo + __cil_tmp16;
#line 91
    __cil_tmp18 = num_proc / 2;
#line 91
    mem_47 = (struct tree **)__cil_tmp14;
#line 91
    *mem_47 = build_tree(__cil_tmp15, dir, __cil_tmp17, __cil_tmp18, min_x, med, min_y,
                         max_y);
#line 92
    __cil_tmp19 = (unsigned int )t;
#line 92
    __cil_tmp20 = __cil_tmp19 + 24;
#line 92
    __cil_tmp21 = n / 2;
#line 92
    __cil_tmp22 = num_proc / 2;
#line 92
    mem_48 = (struct tree **)__cil_tmp20;
#line 92
    *mem_48 = build_tree(__cil_tmp21, dir, lo, __cil_tmp22, med, max_x, min_y, max_y);
#line 98
    __cil_tmp23 = (unsigned int )t;
#line 98
    __cil_tmp24 = __cil_tmp23 + 4;
#line 98
    mem_49 = (double *)__cil_tmp24;
#line 98
    *mem_49 = med;
#line 99
    __cil_tmp25 = (unsigned int )t;
#line 99
    __cil_tmp26 = __cil_tmp25 + 12;
#line 99
    mem_50 = (double *)__cil_tmp26;
#line 99
    *mem_50 = uniform(min_y, max_y);
    }
  } else {
    {
#line 102
    dir = ! dir;
#line 103
    med = median(min_y, max_y, n);
#line 105
    __cil_tmp27 = (unsigned int )t;
#line 105
    __cil_tmp28 = __cil_tmp27 + 20;
#line 105
    __cil_tmp29 = n / 2;
#line 105
    __cil_tmp30 = num_proc / 2;
#line 105
    __cil_tmp31 = lo + __cil_tmp30;
#line 105
    __cil_tmp32 = num_proc / 2;
#line 105
    mem_51 = (struct tree **)__cil_tmp28;
#line 105
    *mem_51 = build_tree(__cil_tmp29, dir, __cil_tmp31, __cil_tmp32, min_x, max_x,
                         min_y, med);
#line 106
    __cil_tmp33 = (unsigned int )t;
#line 106
    __cil_tmp34 = __cil_tmp33 + 24;
#line 106
    __cil_tmp35 = n / 2;
#line 106
    __cil_tmp36 = num_proc / 2;
#line 106
    mem_52 = (struct tree **)__cil_tmp34;
#line 106
    *mem_52 = build_tree(__cil_tmp35, dir, lo, __cil_tmp36, min_x, max_x, med, max_y);
#line 112
    __cil_tmp37 = (unsigned int )t;
#line 112
    __cil_tmp38 = __cil_tmp37 + 12;
#line 112
    mem_53 = (double *)__cil_tmp38;
#line 112
    *mem_53 = med;
#line 113
    __cil_tmp39 = (unsigned int )t;
#line 113
    __cil_tmp40 = __cil_tmp39 + 4;
#line 113
    mem_54 = (double *)__cil_tmp40;
#line 113
    *mem_54 = uniform(min_x, max_x);
    }
  }
#line 115
  mem_55 = (int *)t;
#line 115
  *mem_55 = n;
#line 116
  __cil_tmp41 = (unsigned int )t;
#line 116
  __cil_tmp42 = __cil_tmp41 + 28;
#line 116
  __cil_tmp43 = (void *)0;
#line 116
  mem_56 = (struct tree **)__cil_tmp42;
#line 116
  *mem_56 = (struct tree *)__cil_tmp43;
#line 117
  __cil_tmp44 = (unsigned int )t;
#line 117
  __cil_tmp45 = __cil_tmp44 + 32;
#line 117
  __cil_tmp46 = (void *)0;
#line 117
  mem_57 = (struct tree **)__cil_tmp45;
#line 117
  *mem_57 = (struct tree *)__cil_tmp46;
#line 122
  return ((struct tree *)t);
}
}
Example #10
0
int main()
{
	int i,j,size,psize,count=0,lists[20][20], mainlist[20],indexlist[20][20],temp,k,temppos;
	float meanavg[20],medianavg[20],finalavg[20][2],t;
	printf("Enter the mainlist the size of mainlist:: ");
	scanf("%d",&size);
	printf("Enter the mainlist elements\n");
	for(i=0;i<size;i++)
	scanf("%d",&mainlist[i]);
	printf("Enter number of permutaion \n");
	scanf("%d",&psize);
	//permute(mainlist,0,size-1,psize,lists,count);
	for(i=1;i<=psize;i++)
	{  
	    printf("Enter list %d:: ",i);
		for(j=1;j<=size;j++)
		scanf("%d",&lists[i][j]);
		printf("\n");
	}
	for(i=1;i<=psize;i++)
	{  
		for(j=1;j<=size;j++)
		{
			temp=lists[i][j];
			indexlist[i][temp]=j;
		}
	}
	for(i=1;i<=psize;i++)
	{  
		for(j=1;j<=size;j++)
		printf("%d ",indexlist[i][j]);
		printf("\n");
	}
	sort(indexlist,psize,size);
	mean(indexlist,psize,size,meanavg);
	median(indexlist,psize,size,medianavg);
	printf("After sorted\n");
for(i=1;i<=psize;i++)//sorted indexlist
	{  
		for(j=1;j<=size;j++)
		printf("%d ",indexlist[i][j]);
		printf("\n");
	}
	printf("mean\n"); 
		for(j=1;j<=size;j++)
		printf("%f ",meanavg[j]);
		printf("\n");
	printf("median\n"); 
		for(j=1;j<=size;j++)
		printf("%f ",medianavg[j]);
		printf("\n");
		printf("\nmean final\n");
	for(j=1;j<=size;j++)
	  {
		finalavg[1][j]=(meanavg[j]+medianavg[j])/2.0;
		printf("%f ",finalavg[1][j]);
		finalavg[0][j]=j;
	  }
	 /* printf("\nmean final\n");
	  for(j=1;j<=size;j++)
	  {
		printf("%f ",finalavg[1][j]);
	  }
	  printf("\n");
	for(j=1;j<=size;j++)
	  {
		for(k=j+1;k<=size;k++)
		{
			if(finalavg[1][j]>finalavg[1][k])
			{
				t=finalavg[1][j];
				finalavg[1][j]=finalavg[1][k];
				finalavg[1][k]=t;
				temppos=finalavg[0][j];
				finalavg[0][j]=finalavg[0][k];
				finalavg[0][k]=temppos;
			}
		}
	  printf("final\n");
	  for(j=1;j<=size;j++)
	  {
		printf("%f %.0f \n",finalavg[1][j],finalavg[0][j]);
	  }
	  //getch();*/
	  }
Example #11
0
void DotlikeOptimizer::Reorder(Layout &nodes,Layout &edges) {
	vector<int> affectedRanks;
	{
		NodeV optimVec;
		getCrossoptModelNodes(nodes,edges,optimVec);
		if(optimVec.empty())
			return;
		sort(optimVec.begin(),optimVec.end(),RankLess());
		NodeV::iterator wot = optimVec.begin();
		for(Config::Ranks::iterator ri = config.ranking.begin(); ri!=config.ranking.end(); ++ri)
			for(NodeV::iterator ni = (*ri)->order.begin(); ni!=(*ri)->order.end(); ++ni)
				if(wot!=optimVec.end() && *ni==*wot) {
					DDd(*ni).orderConstraint = -1;
					++wot;
				}
				else
					DDd(*ni).orderConstraint = DDd(*ni).order;
		assert(wot==optimVec.end());
		loops.Field(r_crossopt,"model nodes for crossopt",optimVec.size());
		loops.Field(r_crossopt,"total model nodes",optimVec.front()->g->nodes().size());

		for(NodeV::iterator ni = optimVec.begin(); ni!=optimVec.end(); ++ni)
			if(affectedRanks.empty() || affectedRanks.back()!=DDd(*ni).rank)
				affectedRanks.push_back(DDd(*ni).rank);
		loops.Field(r_crossopt,"ranks for crossopt",affectedRanks.size());
		loops.Field(r_crossopt,"total ranks",config.ranking.size());
	}
	SiftMatrix matrix(config),backupM(config);
	MedianCompare median(DOWN,false);
	CrossingCompare crossing(config,matrix,false);
	OrderConstraintSwitchable switchable;

	Config::Ranks backup = config.ranking;
	// optimize once ignoring node crossings (they can scare the sifting alg) 
	// in a sec we'll sift using the node penalty to clean up
	matrix.m_light = true;
	matrix.recompute();
	backupM = matrix;
	unsigned best = matrix.sumCrossings(),bestPass=0;
	loops.Field(r_crossopt,"crossings before crossopt",best);
	unsigned passes = 32,pass=0,score=0;
	while(pass<passes && best) {
		int tired = 0;
		while(pass<passes && tired<TIRE) {
				assert(matrix.m_light);
			median.m_allowEqual = pass%8>4;
			crossing.m_allowEqual = !median.m_allowEqual;
			LeftRight way = (pass%2) ? RIGHT : LEFT;
			UpDown dir;
				assert(matrix.m_light);
			if(pass%4<2) {
				median.m_dir = UP;
				dir = DOWN;
			}
			else {
				median.m_dir = DOWN;
				dir = UP;
			}
			bubblePass(config,matrix,affectedRanks,dir,way,switchable,median);
				assert(matrix.m_light);

			// something in the next dozen lines crashes MS Visual C++ .NET
			// if run-time checks are enabled so i've disabled them for this file - ?
			unsigned score2 = matrix.sumCrossings();
			do {
				score = score2;
				assert(matrix.m_light);
				bubblePass(config,matrix,affectedRanks,dir,way,switchable,crossing);
				score2 = matrix.sumCrossings();
				assert(score2<=score);
			}
			while(score2<score);
			score = score2;

			if(reportEnabled(r_crossopt)) {
				char buf[10];
				sprintf(buf,"crossings pass %d",pass);
				loops.Field(r_crossopt,buf,score);
			}

			if(score<best) {
				backup = config.ranking;
				backupM = matrix;
				best = score;
				bestPass = pass;
				tired = 0;
			}
			else 
				tired++;
			pass++;
		}
		if(score>best || tired==TIRE) {
			config.Restore(backup);
			//matrix.recompute();
			matrix = backupM;
			tired = 0;
		}
	}
	if(reportEnabled(r_crossopt))
		for(;pass<passes;++pass) {
			char buf[10];
			sprintf(buf,"crossings pass %d",pass);
			loops.Field(r_crossopt,buf,0);
		}

	if(score>=best) {
		config.Restore(backup);
	}
	loops.Field(r_crossopt,"model edge crossings",best);

	// sift out node crossings
	matrix.m_light = false;
	matrix.recompute();
	score = matrix.sumCrossings();
	loops.Field(r_crossopt,"weighted crossings before heavy pass",score);
	/*
	report(r_crossopt,"%d node-node crossings; %d node-edge crossings; %d edge-edge crossings\n",score/(NODECROSS_PENALTY*NODECROSS_PENALTY),
		(score/NODECROSS_PENALTY)%NODECROSS_PENALTY,score%NODECROSS_PENALTY);
	*/
	if(score<NODECROSS_PENALTY) {
		loops.Field(r_crossopt,"weighted crossings after heavy pass",-1);
		return;
	}
	pass = 0;
	passes = 4;
	crossing.m_allowEqual = true;
	// sifting out upward or downward may be better - try both.
	bool improved;
	do {
		improved = false;
		backup = config.ranking;
		bubblePass(config,matrix,affectedRanks,DOWN,RIGHT,switchable,crossing);
		unsigned down = matrix.sumCrossings();
		Config::Ranks backup2 = config.ranking;
		config.Restore(backup);
		matrix.recompute();
		bubblePass(config,matrix,affectedRanks,UP,RIGHT,switchable,crossing);
		unsigned up = matrix.sumCrossings();
		if(down<score && down<up) {
			config.Restore(backup2);
			matrix.recompute();
			score = down;
			improved = true;
		}
		else if(up<score) {
			score = up;
			improved = true;
		}
	}
	while(improved);
	loops.Field(r_crossopt,"weighted crossings after heavy pass",score);
	// absolutely must not leave here with nodes crossing nodes!!
	assert(score<NODECROSS_PENALTY*NODECROSS_PENALTY);
}
Example #12
0
int main(int, char**)
{
    std::vector<std::chrono::duration<double,std::milli>> duration_vector_1;
    std::vector<std::chrono::duration<double,std::milli>> duration_vector_2;
    Halide::Buffer<uint8_t> input = Halide::Tools::load_image("./utils/images/rgb.png");
    Halide::Buffer<int32_t> size(2);
    size(0) = input.extent(0);
    size(1) = input.extent(1);

    Halide::Buffer<uint8_t> output_ref_y(input.width(), input.height());
    Halide::Buffer<uint8_t> output_ref_u(input.width()/2, input.height()/2);
    Halide::Buffer<uint8_t> output_ref_v(input.width()/2, input.height()/2);

    Halide::Buffer<uint8_t> output_tiramisu_y(input.width(), input.height());
    Halide::Buffer<uint8_t> output_tiramisu_u(input.width()/2, input.height()/2);
    Halide::Buffer<uint8_t> output_tiramisu_v(input.width()/2, input.height()/2);

    std::cout << "STARTING TEST\n";
    std::cout << "y size (width, height): " << output_tiramisu_y.width() << ", " << output_tiramisu_y.height() << "\n";
    std::cout << "u size (width, height): " << output_tiramisu_u.width() << ", " << output_tiramisu_u.height() << "\n";
    std::cout << "v size (width, height): " << output_tiramisu_v.width() << ", " << output_tiramisu_v.height() << "\n";
    // Warm up
    rgbyuv420gpu_tiramisu(size.raw_buffer(), input.raw_buffer(), output_tiramisu_y.raw_buffer(), output_tiramisu_u.raw_buffer(), output_tiramisu_v.raw_buffer());
    run_halide(input, output_ref_y, output_ref_u, output_ref_v);

    // Tiramisu
    for (int i=0; i<NB_TESTS; i++)
    {
        auto start1 = std::chrono::high_resolution_clock::now();
        rgbyuv420gpu_tiramisu(size.raw_buffer(), input.raw_buffer(), output_tiramisu_y.raw_buffer(), output_tiramisu_u.raw_buffer(), output_tiramisu_v.raw_buffer());
        auto end1 = std::chrono::high_resolution_clock::now();
        std::chrono::duration<double,std::milli> duration1 = end1 - start1;
        duration_vector_1.push_back(duration1);
    }

    // Reference
    for (int i=0; i<NB_TESTS; i++)
    {
        duration_vector_2.push_back(run_halide(input, output_ref_y, output_ref_u, output_ref_v));
    }

    print_time("performance_CPU.csv", "rgbyuv420gpu",
               {"Tiramisu", "Halide"},
               {median(duration_vector_1), median(duration_vector_2)});

    Halide::Tools::save_image(output_tiramisu_y, "./build/rgbyuv420gpu_y_tiramisu.png");
    Halide::Tools::save_image(output_tiramisu_u, "./build/rgbyuv420gpu_u_tiramisu.png");
    Halide::Tools::save_image(output_tiramisu_v, "./build/rgbyuv420gpu_v_tiramisu.png");
    Halide::Tools::save_image(output_ref_y, "./build/rgbyuv420gpu_y_ref.png");
    Halide::Tools::save_image(output_ref_u, "./build/rgbyuv420gpu_u_ref.png");
    Halide::Tools::save_image(output_ref_v, "./build/rgbyuv420gpu_v_ref.png");

    if (CHECK_CORRECTNESS)
    {
        std::cout << "Compare y buffer\n";
    	compare_buffers("benchmark_rgbyuv420gpu", output_tiramisu_y, output_ref_y);
        std::cout << "Compare u buffer\n";
    	compare_buffers("benchmark_rgbyuv420gpu", output_tiramisu_u, output_ref_u);
        std::cout << "Compare v buffer\n";
    	compare_buffers("benchmark_rgbyuv420gpu", output_tiramisu_y, output_ref_y);
    }

    return 0;
}
Example #13
0
void 
mapper (
    struct vtx_data **graph,	/* data structure with vertex weights */
    double **xvecs,		/* continuous indicator vectors */
    int nvtxs,		/* number of vtxs in graph */
    int *active,		/* space for nmyvals ints */
    int *sets,			/* returned processor assignment for my vtxs */
    int ndims,		/* number of dimensions being divided into */
    int cube_or_mesh,		/* 0 => hypercube, d => d-dimensional mesh */
    int nsets,		/* number of sets to divide into */
    int mediantype,		/* type of eigenvector partitioning to use */
    double *goal,			/* desired set sizes */
    int vwgt_max		/* largest vertex weight */
)
{
    double    temp_goal[2];	/* combined set goals if using option 1. */
    double    wbelow;		/* weight of vertices with negative values */
    double    wabove;		/* weight of vertices with positive values */
    int      *temp_sets;	/* sets vertices get assigned to */
    int       vweight;		/* weight of a vertex */
    int       using_vwgts;	/* are vertex weights being used? */
    int       bits;		/* bits for assigning set numbers */
    int       i, j;		/* loop counters */

    void      median(), rec_median_k(), rec_median_1(), median_assign();
    void      map2d(), map3d();

    /* NOTE: THIS EXPECTS XVECS, NOT YVECS! */

    using_vwgts = (vwgt_max != 1);

    if (ndims == 1 && mediantype == 1)
	mediantype = 3;		/* simpler call than normal option 1. */

    if (mediantype == 0) {	/* Divide at zero instead of median. */
	bits = 1;
	temp_sets = smalloc((nvtxs + 1) * sizeof(int));
	for (j = 1; j <= nvtxs; j++)
	    sets[j] = 0;

	for (i = 1; i <= ndims; i++) {
	    temp_goal[0] = temp_goal[1] = 0;
	    for (j = 0; j < (1 << ndims); j++) {
		if (bits & j)
		    temp_goal[1] += goal[j];
		else
		    temp_goal[0] += goal[j];
	    }
	    bits <<= 1;

	    wbelow = wabove = 0;
	    vweight = 1;
	    for (j = 1; j <= nvtxs; j++) {
		if (using_vwgts)
		    vweight = graph[j]->vwgt;
		if (xvecs[i][j] < 0)
		    wbelow += vweight;
		else if (xvecs[i][j] > 0)
		    wabove += vweight;
	    }

	    median_assign(graph, xvecs[i], nvtxs, temp_goal, using_vwgts, temp_sets,
			  wbelow, wabove, (double) 0.0);

	    for (j = 1; j <= nvtxs; j++)
		sets[j] = (sets[j] << 1) + temp_sets[j];
	}
    }

    else if (mediantype == 1) {	/* Divide using min-cost assignment. */
	if (ndims == 2)
	    map2d(graph, xvecs, nvtxs, sets, goal, vwgt_max);
	else if (ndims == 3)
	    map3d(graph, xvecs, nvtxs, sets, goal, vwgt_max);
    }


    else if (mediantype == 2) {	/* Divide recursively using medians. */
	rec_median_k(graph, xvecs, nvtxs, active, ndims, cube_or_mesh, goal,
		     using_vwgts, sets);
    }

    else if (mediantype == 3) {	/* Cut with independent medians => unbalanced. */
	bits = 1;
	temp_sets = smalloc((nvtxs + 1) * sizeof(int));
	for (j = 1; j <= nvtxs; j++)
	    sets[j] = 0;

	for (i = 1; i <= ndims; i++) {
	    temp_goal[0] = temp_goal[1] = 0;
	    for (j = 0; j < (1 << ndims); j++) {
		if (bits & j)
		    temp_goal[1] += goal[j];
		else
		    temp_goal[0] += goal[j];
	    }
	    bits <<= 1;

	    median(graph, xvecs[i], nvtxs, active, temp_goal, using_vwgts, temp_sets);
	    for (j = 1; j <= nvtxs; j++)
		sets[j] = (sets[j] << 1) + temp_sets[j];
	}
	sfree(temp_sets);
    }

    if (mediantype == 4) {	/* Stripe the domain. */
	rec_median_1(graph, xvecs[1], nvtxs, active, cube_or_mesh, nsets,
		     goal, using_vwgts, sets, TRUE);
    }
}
Example #14
0
//
// Main
//
int somaticVariantFiltersMain(int argc, char** argv)
{
    parseSomaticVariantFiltersOptions(argc, argv);

    Timer* pTimer = new Timer(PROGRAM_IDENT);
    
    // Load Reference
    ReadTable refTable(opt::referenceFile, SRF_NO_VALIDATION);
    refTable.indexReadsByID();

    // Load BAMs
    BamTools::BamReader* pTumorBamReader = new BamTools::BamReader;
    pTumorBamReader->Open(opt::tumorBamFile);
    pTumorBamReader->LocateIndex();

    assert(pTumorBamReader->HasIndex());

    BamTools::BamReader* pNormalBamReader = new BamTools::BamReader;
    pNormalBamReader->Open(opt::normalBamFile);
    pNormalBamReader->LocateIndex();
    assert(pNormalBamReader->HasIndex());

    // Track duplicated variants
    HashSet<std::string> duplicateHash;

    std::ifstream input(opt::vcfFile.c_str());
    std::string line;

    while(getline(input, line))
    {
        if(line.empty())
            continue;

        if(line[0] == '#')
        {
            std::cout << line << "\n";
            continue;
        }
        
        // parse record
        VCFRecord record(line);
        if(record.isMultiAllelic())
        {
            std::cerr << "Error: multi-allelic VCF found, please run vcfbreakmulti\n";
            exit(EXIT_FAILURE);
        }

        // Check if we've seen this variant already
        std::string key = makeVariantKey(record);
        if(duplicateHash.find(key) != duplicateHash.end())
            continue;
        else
            duplicateHash.insert(key);

        if(opt::verbose > 0)
        {
            std::stringstream ss;
            ss << "Variant: " << record << "\n";
            fprintf(stderr, "===============================================\n%s", ss.str().c_str());
        }

        StringStringHash tagHash;
        makeTagHash(record, tagHash);

        StringVector fail_reasons;

        int hplen = 0;
        if(!getTagValue(tagHash, "HPLen", hplen))
            hplen = calculateHomopolymerLength(record, &refTable);
        if(hplen > opt::maxHPLen)
            fail_reasons.push_back("Homopolymer");

        double dust = 0.0f;
        if(!getTagValue(tagHash, "Dust", dust))
            dust = HapgenUtil::calculateDustScoreAtPosition(record.refName, 
                                                            record.refPosition, 
                                                            &refTable);

        if(dust > opt::maxDust)
            fail_reasons.push_back("LowComplexity");
        
        double af;
        if(getTagValue(tagHash, "AF", af) && af < opt::minAF)
            fail_reasons.push_back("LowAlleleFrequency");

        int varDP;
        if(getTagValue(tagHash, "VarDP", varDP) && varDP < opt::minVarDP)
            fail_reasons.push_back("LowVarDP");

        double strandBias;
        if(getTagValue(tagHash, "SB", strandBias) && strandBias >= opt::maxStrandBias)
            fail_reasons.push_back("StrandBias");

        CoverageStats tumor_stats = getVariantCoverage(pTumorBamReader, record, &refTable);
        CoverageStats normal_stats = getVariantCoverage(pNormalBamReader, record, &refTable);

        if(opt::verbose > 0)
        {
            fprintf(stderr, "Tumor: [%zu %zu]\n",  tumor_stats.n_total_reads, tumor_stats.n_evidence_reads);
            fprintf(stderr, "Normal: [%zu %zu]\n", normal_stats.n_total_reads, normal_stats.n_evidence_reads);
        }

        if(normal_stats.n_evidence_reads > opt::maxNormalReads)
            fail_reasons.push_back("NormalEvidence");
        
        if(normal_stats.n_total_reads < opt::minNormalDepth)
            fail_reasons.push_back("LowNormalDepth");

        if(!tumor_stats.snv_evidence_quals.empty())
        {
            double median_quality = median(tumor_stats.snv_evidence_quals);
            if(median_quality < opt::minMedianQuality)
                fail_reasons.push_back("LowQuality");
        }

        if(tumor_stats.median_mapping_quality < opt::minMedianQuality)
            fail_reasons.push_back("LowMappingQuality");

        if(!fail_reasons.empty())
        {
            if(record.passStr != "PASS" && record.passStr != ".")
                fail_reasons.insert(fail_reasons.begin(), record.passStr);

            std::stringstream strss;
            std::copy(fail_reasons.begin(), fail_reasons.end(), std::ostream_iterator<std::string>(strss, ";"));
            record.passStr = strss.str();
            record.passStr.erase(record.passStr.size() - 1); // erase trailing ;
        }

        std::cout << record << "\n";
    }
    
    // Cleanup
    delete pTumorBamReader;
    delete pNormalBamReader;
    delete pTimer;

    return 0;
}
Example #15
0
VC_vector _mergeScaleFactors(SP_spectrum* spectra,size_t nbsp,long width,VC_vector* vout,char* file,size_t line)
{
  size_t    i,j,k,sz,msz,swork,i1,i2;
  double    *df1,*dw1,*df2,*dw2,*work;
  VC_vector rslt,idmin,idmax,sidx;
  size_t    spref,r1min,r1max,r2min,r2max;
  double    med1,med2;
  double    wmin1,wmax1,wmin2,wmax2,wmin,wmax;
  
  
  rslt  =_VC_allocate(nbsp,VCTDOUBLE,file,line);
  for(i=0;i<rslt->size;i++) rslt->ddata[i]=1;

  if(nbsp>1)
    {
      idmin =VC_allocate(nbsp,VCTLONG);
      idmax =VC_allocate(nbsp,VCTLONG);
      sidx  =VC_allocate(nbsp,VCTLONG);
      if(rslt)
	{
	  /************************/
	  /* Create working space */
	  /************************/
	  msz=0;
	  for(i=0;i<nbsp;i++)
	    {
	      sz=spectra[i]->npix;
	      if(sz>msz) msz=sz;
	    }
	  work=(double*)MM_malloc(msz*sizeof(double));
	  swork=0;
	  /****************************************/
	  /*First find the limits of each spectrum*/
	  /****************************************/
	  for(i=0;i<nbsp;i++)
	    {
	      VC_getIdxEdge(spectra[i]->data[spectra[i]->colF],idmin->ldata+i,idmax->ldata+i);
	      
	      if(idmin->ldata[i]>=idmax->ldata[i])
		{
		  OS_message(0,OS_ERR,"[mergeScaleFactors] No data in spectrum #%ld\n",i);
		  exit(1);
		}
	      sidx->ldata[i]=i; //initialise sort array
	    }
	  
	  /****************************************/
	  /*Sort spectra in increasing wavelength */
	  /****************************************/
	  OS_message(0,OS_STD,"[mergeScaleFactors] Sort spectra in increasing wavelength\n");
	  for(i=0;i<nbsp-1;i++)
	    for(j=i+1;j<nbsp;j++)
	      {
		i1=sidx->ldata[i];
		i2=sidx->ldata[j];
		if((spectra[i1]->data[spectra[i1]->colW]->ddata[idmin->ldata[i1]]) <=
		   (spectra[i2]->data[spectra[i2]->colW]->ddata[idmin->ldata[i2]]))
		  continue;
		sidx->ldata[i]=i2;
		sidx->ldata[j]=i1;
	      }

	  spref=sidx->ldata[0];
	  OS_message(0,OS_STD,"[mergeScaleFactors] ------------------------------------------\n");
	  OS_message(0,OS_STD,"[mergeScaleFactors] Use spectrum #%d as reference\n",spref+1);
	  
	  rslt->ddata[spref]=1.;
	  OS_message(0,OS_STD,"[mergeScaleFactors]\n");
	  OS_message(0,OS_STD,"[mergeScaleFactors] Compute scale factors for each spectrum \n");
	  for(i=1;i<nbsp;i++)
	    {
	      i1=sidx->ldata[i-1];
	      i2=sidx->ldata[  i];

	      dw1=spectra[i1]->data[spectra[i1]->colW]->ddata;
	      df1=spectra[i1]->data[spectra[i1]->colF]->ddata;

	      dw2=spectra[i2]->data[spectra[i2]->colW]->ddata;
	      df2=spectra[i2]->data[spectra[i2]->colF]->ddata;

	      /************************/
	      /* Find overlap regions */
	      /************************/
	      wmin1=dw1[idmin->ldata[i1]];
	      wmax1=dw1[idmax->ldata[i1]-1];

	      wmin2=dw2[idmin->ldata[i2]];
	      wmax2=dw2[idmax->ldata[i2]-1];

	      wmin=(wmin1>wmin2)?wmin1:wmin2;
	      wmax=(wmax1<wmax2)?wmax1:wmax2;

	      /***************************/
	      /* Get index of the limits */
	      /* of the overlap          */
	      /***************************/

	      if(wmin>=wmax)
		{
		  r1max=idmax->ldata[i1];
		  r1min=r1max-10*width;
		  r2min=idmin->ldata[i2];
		  r2max=r2min+10*width;
		}
	      else
		{
		  r1min=findNear(spectra[i1]->data[spectra[i1]->colW],wmin);
		  r1max=findNear(spectra[i1]->data[spectra[i1]->colW],wmax);
		  r2min=findNear(spectra[i2]->data[spectra[i2]->colW],wmin);
		  r2max=findNear(spectra[i2]->data[spectra[i2]->colW],wmax);
		  r1max++;
		  r2max++;
		}
	      
	      /**********************/
	      /* Compute median for */
	      /* the overlap        */
	      /**********************/
	      for(j=0,k=r1min;k<r1max;j++,k++) work[j]=df1[k];
	      med1=median(work,r1max-r1min);

	      for(j=0,k=r2min;k<r2max;j++,k++) work[j]=df2[k];
	      med2=median(work,r2max-r2min);
	      
	      if((med1>0)&&(med2>0)) rslt->ddata[i2]=med1/med2*rslt->ddata[i1];

	      OS_message(0,OS_STD,"[mergeScaleFactors] Factor for spectrum #%2d : %f\n",i+1,rslt->ddata[i]);
	    }
	  MM_free(work);
	}
      VC_free(sidx);
      VC_free(idmax);
      VC_free(idmin);
    }
  if(vout) *vout=rslt;
  return rslt;
}
Example #16
0
/* OLD Clipping Routine (uses channel medians) */
int old_clip_times(float *rawdata, int ptsperblk, int numchan, float clip_sigma,
                   float *good_chan_levels)
// Perform time-domain clipping of rawdata.  This is a 2D array with
// ptsperblk*numchan points, each of which is a float.  The clipping
// is done at clip_sigma sigma above/below the running mean.  The
// up-to-date running averages of the channels are returned in
// good_chan_levels (which must be pre-allocated).
{
    static float *median_chan_levels;
    static float running_avg = 0.0, running_std = 0.0, median_sum = 0.0;
    static int blocksread = 0, firsttime = 1;
    float *zero_dm_block, *median_temp;
    float current_med, trigger, running_wgt = 0.05;
    double current_avg = 0.0, current_std = 0.0, scaling;
    float *powptr;
    int ii, jj, clipit = 0, clipped = 0;
    if (firsttime) {
        median_chan_levels = gen_fvect(numchan);
        firsttime = 0;
    }
    zero_dm_block = gen_fvect(ptsperblk);
    median_temp = gen_fvect(ptsperblk);

    /* Calculate the zero DM time series */
    for (ii = 0; ii < ptsperblk; ii++) {
        zero_dm_block[ii] = 0.0;
        powptr = rawdata + ii * numchan;
        for (jj = 0; jj < numchan; jj++)
            zero_dm_block[ii] += *powptr++;
        median_temp[ii] = zero_dm_block[ii];
    }
    current_med = median(median_temp, ptsperblk);

    /* Calculate the current standard deviation and mean  */
    /* but only for data points that are within a certain */
    /* fraction of the median value.  This removes the    */
    /* really strong RFI from the calculation.            */
    {
        float lo_cutoff, hi_cutoff;
        int numgoodpts = 0;
        lo_cutoff = 0.7 * current_med;
        hi_cutoff = 1.3 * current_med;

        /* Find the "good" points */
        for (ii = 0; ii < ptsperblk; ii++) {
            if (zero_dm_block[ii] > lo_cutoff && zero_dm_block[ii] < hi_cutoff) {
                median_temp[numgoodpts] = zero_dm_block[ii];
                numgoodpts++;
            }
        }

        /* Calculate the current average and stddev */
        if (numgoodpts < 1) {
            current_avg = running_avg;
            current_std = running_std;
        } else {
            avg_var(median_temp, numgoodpts, &current_avg, &current_std);
            current_std = sqrt(current_std);
        }
    }

    /* Update a pseudo running average and stdev */
    if (blocksread) {
        running_avg =
            (running_avg * (1.0 - running_wgt) + running_wgt * current_avg);
        running_std =
            (running_std * (1.0 - running_wgt) + running_wgt * current_std);
    } else {
        running_avg = current_avg;
        running_std = current_std;
        if (running_avg == 0.0 || current_avg == 0.0)
            printf("BAD RFI IN BLOCK#1!!!\n\n");
    }

    /* See if any points need clipping */
    trigger = clip_sigma * running_std;
    for (ii = 0; ii < ptsperblk; ii++) {
        if (fabs(zero_dm_block[ii] - running_avg) > trigger) {
            clipit = 1;
            break;
        }
    }

    /* Calculate the channel medians if required */
    if ((blocksread % BLOCKSTOSKIP == 0 && clipit == 0) || blocksread == 0) {
        median_sum = 0.0;
        for (ii = 0; ii < numchan; ii++) {
            powptr = rawdata + ii;
            for (jj = 0; jj < ptsperblk; jj++)
                median_temp[jj] = *(powptr + jj * numchan);
            median_chan_levels[ii] = median(median_temp, ptsperblk);
            median_sum += median_chan_levels[ii];
        }
    }

    /* Update the good channel levels */
    scaling = running_avg / median_sum;
    for (ii = 0; ii < numchan; ii++)
        good_chan_levels[ii] = median_chan_levels[ii] * scaling;

    /* Replace the bad channel data with channel median values */
    /* that are scaled to equal the running_avg.               */
    if (clipit) {
        for (ii = 0; ii < ptsperblk; ii++) {
            if (fabs(zero_dm_block[ii] - running_avg) > trigger) {
                powptr = rawdata + ii * numchan;
                for (jj = 0; jj < numchan; jj++)
                    *powptr++ = good_chan_levels[jj];
                clipped++;
            }
        }
    }
    blocksread++;
    vect_free(zero_dm_block);
    vect_free(median_temp);
    return clipped;
}
Example #17
0
void deredden(fcomplex * fft, int numamps)
/* Attempt to remove rednoise from a time series by using   */
/* a median-filter of logarithmically increasing width.     */
/* Thanks to Jason Hessels and Maggie Livingstone for the   */
/* initial implementation (in rednoise.c)                   */
{
   int ii, initialbuflen = 6, lastbuflen, newbuflen, maxbuflen = 200;
   int newoffset = 1, fixedoffset = 1;
   float *powers, mean_old, mean_new;
   double slope, lineval, scaleval = 1.0, lineoffset = 0;

   powers = gen_fvect(numamps);

   /* Takes care of the DC term */
   fft[0].r = 1.0;
   fft[0].i = 0.0;

   /* Step through the input FFT and create powers */
   for (ii = 0; ii < numamps; ii++) {
      float powargr, powargi;
      powers[ii] = POWER(fft[ii].r, fft[ii].i);
   }

   /* Calculate initial values */
   mean_old = median(powers + newoffset, initialbuflen) / log(2.0);
   newoffset += initialbuflen;
   lastbuflen = initialbuflen;
   newbuflen = initialbuflen * log(newoffset);
   if (newbuflen > maxbuflen)
      newbuflen = maxbuflen;

   while (newoffset + newbuflen < numamps) {
      /* Calculate the next mean */
      mean_new = median(powers + newoffset, newbuflen) / log(2.0);
      //slope = (mean_new-mean_old)/(0.5*(newbuflen+lastbuflen));
      slope = (mean_new - mean_old) / (newbuflen + lastbuflen);

      /* Correct the previous segment */
      lineoffset = 0.5 * (newbuflen + lastbuflen);
      for (ii = 0; ii < lastbuflen; ii++) {
         lineval = mean_old + slope * (lineoffset - ii);
         scaleval = 1.0 / sqrt(lineval);
         fft[fixedoffset + ii].r *= scaleval;
         fft[fixedoffset + ii].i *= scaleval;
      }

      /* Update our values */
      fixedoffset += lastbuflen;
      lastbuflen = newbuflen;
      mean_old = mean_new;
      newoffset += lastbuflen;
      newbuflen = initialbuflen * log(newoffset);
      if (newbuflen > maxbuflen)
         newbuflen = maxbuflen;
   }

   /* Scale the last (partial) chunk the same way as the last point */

   while (fixedoffset < numamps) {
      fft[fixedoffset].r *= scaleval;
      fft[fixedoffset].i *= scaleval;
      fixedoffset++;
   }

   /* Free the powers */
   free(powers);
}
Example #18
0
/* NEW Clipping Routine (uses channel running averages) */
int clip_times(float *rawdata, int ptsperblk, int numchan, float clip_sigma,
               float *good_chan_levels)
// Perform time-domain clipping of rawdata.  This is a 2D array with
// ptsperblk*numchan points, each of which is a float.  The clipping
// is done at clip_sigma sigma above/below the running mean.  The
// up-to-date running averages of the channels are returned in
// good_chan_levels (which must be pre-allocated).
{
    static float *chan_running_avg;
    static float running_avg = 0.0, running_std = 0.0;
    static int blocksread = 0, firsttime = 1;
    static long long current_point = 0;
    static int numonoff = 0, onoffindex = 0;
    static long long *onbins = NULL, *offbins = NULL;
    float *zero_dm_block, *ftmp, *powptr;
    double *chan_avg_temp;
    float current_med, trigger;
    double current_avg = 0.0, current_std = 0.0;
    int ii, jj, clipit = 0, clipped = 0;
    if (firsttime) {
        chan_running_avg = gen_fvect(numchan);
        firsttime = 0;
        {                       // This is experimental code to zap radar-filled data
            char *envval = getenv("CLIPBINSFILE");
            if (envval != NULL) {
                FILE *onofffile = chkfopen(envval, "r");
                numonoff = read_onoff_paris(onofffile, &onbins, &offbins);
                fclose(onofffile);
                printf("\nRead %d bin clipping pairs from '%s'.\n", numonoff,
                       envval);

                //for (ii=0;ii<numonoff;ii++) printf("%lld %lld\n", onbins[ii], offbins[ii]);
            }
        }
    }
    chan_avg_temp = gen_dvect(numchan);
    zero_dm_block = gen_fvect(ptsperblk);
    ftmp = gen_fvect(ptsperblk);

    /* Calculate the zero DM time series */
    for (ii = 0; ii < ptsperblk; ii++) {
        zero_dm_block[ii] = 0.0;
        powptr = rawdata + ii * numchan;
        for (jj = 0; jj < numchan; jj++)
            zero_dm_block[ii] += *powptr++;
        ftmp[ii] = zero_dm_block[ii];
    }
    avg_var(ftmp, ptsperblk, &current_avg, &current_std);
    current_std = sqrt(current_std);
    current_med = median(ftmp, ptsperblk);

    /* Calculate the current standard deviation and mean  */
    /* but only for data points that are within a certain */
    /* fraction of the median value.  This removes the    */
    /* really strong RFI from the calculation.            */
    {
        float lo_cutoff, hi_cutoff;
        int numgoodpts = 0;
        lo_cutoff = current_med - 3.0 * current_std;
        hi_cutoff = current_med + 3.0 * current_std;;
        for (jj = 0; jj < numchan; jj++)
            chan_avg_temp[jj] = 0.0;

        /* Find the "good" points */
        for (ii = 0; ii < ptsperblk; ii++) {
            if (zero_dm_block[ii] > lo_cutoff && zero_dm_block[ii] < hi_cutoff) {
                ftmp[numgoodpts] = zero_dm_block[ii];
                powptr = rawdata + ii * numchan;
                for (jj = 0; jj < numchan; jj++)
                    chan_avg_temp[jj] += *powptr++;
                numgoodpts++;
            }
        }

        //printf("avg = %f  med = %f  std = %f  numgoodpts = %d\n", 
        //       current_avg, current_med, current_std, numgoodpts);

        /* Calculate the current average and stddev */
        if (numgoodpts < 1) {
            current_avg = running_avg;
            current_std = running_std;
            for (jj = 0; jj < numchan; jj++)
                chan_avg_temp[jj] = chan_running_avg[jj];
        } else {
            avg_var(ftmp, numgoodpts, &current_avg, &current_std);
            current_std = sqrt(current_std);
            for (jj = 0; jj < numchan; jj++)
                chan_avg_temp[jj] /= numgoodpts;
        }
    }

    /* Update a pseudo running average and stdev */
    if (blocksread) {
        running_avg = 0.9 * running_avg + 0.1 * current_avg;
        running_std = 0.9 * running_std + 0.1 * current_std;
        for (ii = 0; ii < numchan; ii++)
            chan_running_avg[ii] =
                0.9 * chan_running_avg[ii] + 0.1 * chan_avg_temp[ii];
    } else {
        running_avg = current_avg;
        running_std = current_std;
        for (ii = 0; ii < numchan; ii++)
            chan_running_avg[ii] = chan_avg_temp[ii];
        if (current_avg == 0.0)
            printf("Warning:  problem with clipping in first block!!!\n\n");
    }

    /* See if any points need clipping */
    trigger = clip_sigma * running_std;
    for (ii = 0; ii < ptsperblk; ii++) {
        if (fabs(zero_dm_block[ii] - running_avg) > trigger) {
            clipit = 1;
            break;
        }
    }

    /* or alternatively from the CLIPBINSFILE */
    if (numonoff && ((current_point > onbins[onoffindex]
                      && current_point <= offbins[onoffindex])
                     || (current_point + ptsperblk > onbins[onoffindex]
                         && current_point + ptsperblk <= offbins[onoffindex])
                     || (current_point < onbins[onoffindex]
                         && current_point + ptsperblk > offbins[onoffindex])))
        clipit = 1;

    /* Update the good channel levels */
    for (ii = 0; ii < numchan; ii++)
        good_chan_levels[ii] = chan_running_avg[ii];

    /* Replace the bad channel data with channel median values */
    /* that are scaled to equal the running_avg.               */
    if (clipit) {
        for (ii = 0; ii < ptsperblk; ii++) {
            if ((fabs(zero_dm_block[ii] - running_avg) > trigger) ||
                (numonoff && (current_point > onbins[onoffindex]
                              && current_point <= offbins[onoffindex]))) {
                powptr = rawdata + ii * numchan;
                for (jj = 0; jj < numchan; jj++)
                    *powptr++ = good_chan_levels[jj];
                clipped++;

                //fprintf(stderr, "zapping %lld\n", current_point);
            }
            current_point++;
            if (numonoff && current_point > offbins[onoffindex]
                && onoffindex < numonoff - 1) {
                while (current_point > offbins[onoffindex]
                       && onoffindex < numonoff - 1)
                    onoffindex++;

                //printf("updating index to %d\n", onoffindex);
            }
        }
    } else {
        current_point += ptsperblk;
        if (numonoff && current_point > offbins[onoffindex]
            && onoffindex < numonoff - 1) {
            while (current_point > offbins[onoffindex]
                   && onoffindex < numonoff - 1)
                onoffindex++;

            //printf("updating index to %d\n", onoffindex);
        }
    }
    blocksread++;
    vect_free(chan_avg_temp);
    vect_free(zero_dm_block);
    vect_free(ftmp);
    return clipped;
}
Example #19
0
SP_spectrum _SP_addSpectra(SP_spectrum* spectra,size_t nbsp,double width,double clip,double prc,int error_type,double rebin_pixfactor,double rebin_pixshift,int spline,int tclip,int keepnbsp,char* file,size_t line)
{

  VC_vector   group,factors;
  double*     work,fct;
  double*     tmp;
  double      *dw,*df,*ds;
  double      fact1,fact2;
  double      *wmin,*wmax;
  VC_vector    wlim,wave;
  SP_spectrum rslt;
  VC_vector   eflux,esigm,emask;
  VC_vector   flx1,flx2,wav1,pds1,pds2;
  long        *npix;
  double      y1,y2;
  double       tmin, tmax,tcen,wpas,old_wpas;
  double       w1min,w1max,w2min,w2max;
  long         k1min,k1max,k2min,k2max;
  long         imin,imax,jmin,jmax;
  long        *idmin,*idmax; 
  long        *kmin,*kmax; 
  long        nbgroup,igroup,nbcol;
  long        i,j;
  size_t      si,sj,k,ngood;
  size_t      npix_max,npix_tot;
  int         pourc,old_pourc;
  double      fsum,wsum,weight,sigm_srianand,mean_srianand;
  long        nbg_srianand;
  long        nbg_bastien;
  double      a1,b1;
  SP_spectrum* spline_spectra;

  if(nbsp<2) return NULL;

  /****************************/
  /*  Sort and group by setup */
  /****************************/
  SP_sortSpectrum(spectra,nbsp); 
  OS_message(0,OS_STD,"[SP_addSpectra] new order for spectra :\n");
  for(i=0;i<nbsp;i++)
    {
      OS_message(0,OS_STD,"[SP_addSpectra] %4d : %s\n",i+1,spectra[i]->name);
      
    }
  group=SP_groupSpectrum(spectra,nbsp,prc);
  if(!group)
    {
      OS_message(0,OS_ERR,"not enough memory !!\n");
      exit(1);
    }
  nbgroup=1+group->ldata[nbsp-1];
  OS_message(0,OS_STD,"[SP_addSpectra] found %d group%s\n",nbgroup,(nbgroup>1)?"s":" ");
  
  
  npix_max=0; /*will contain the number of pixel of the biggest spectrum*/
  for(si=0;si<nbsp;si++) 
    npix_max=(spectra[si]->npix>npix_max)?spectra[si]->npix:npix_max;
   
  MMV_malloc(tmp,npix_max,double); /*working buffer*/
  MMV_malloc(idmin,nbsp,long);
  MMV_malloc(idmax,nbsp,long);
  MMV_malloc( kmin,nbsp,long);
  MMV_malloc( kmax,nbsp,long);
  MMV_malloc( wmin,nbsp,double);
  MMV_malloc( wmax,nbsp,double);
  for(i=0;i<nbsp;i++) 
    {
      /**********************************************************/
      /*Find first and last pixel that not have null flux values*/
      /*sort the corresponding wavelength in wmin and wmax and  */
      /*the pixel index in idmin and idmax.                     */
      /**********************************************************/
      VC_getIdxEdge(spectra[i]->data[spectra[i]->colF],idmin+i,idmax+i);
      wmin[i]=spectra[i]->wave[idmin[i]];
      wmax[i]=spectra[i]->wave[idmax[i]-1];
      kmin[i]=kmax[i]=-1;
    }
  for(i=0;i<nbgroup;i++)
    {
      k=0;
      for(j=0;j<nbsp;j++) if(group->ldata[j]==i) k++;
      OS_message(0,OS_STD,"[SP_addSpectra] group #%-2d : %d spectr%s\n",i+1,k,(k>1)?"a":"um");
    }

  /************************************************/
  /*Compute scale factors by group and apply them */
  /************************************************/
  
  imax=imin=igroup=0;
  while(imax<nbsp)
    {
      imax=imin;
      while((imax<nbsp)&&(group->ldata[imax]==igroup)) ++imax;
      //      printf("igroup: %2ld imin: %2ld  imax: %2ld\n",igroup,imin,imax);
      factors=addiScaleFactors(spectra+imin,imax-imin,width,NULL);
      for(i=0,si=imin;si<imax;i++,si++)
	for(sj=0;sj<spectra[si]->npix;sj++)
	  {
	    spectra[si]->flux[sj]*=factors->ddata[i];
	    spectra[si]->sigm[sj]*=factors->ddata[i];
	  }
      VC_free(factors);
      imin=imax;
      igroup++;
    }

  /*************/
  /* Find edge */
  /*************/


  /****************************************/
  /* Compute merge factors and apply them */
  /****************************************/
  /* Two steps for the computation.             */
  /* First find scale factor for the edges      */
  /* Then rebin each edges and compute their    */
  /* ratio. Fit the ratio by a line and take    */
  /* max of 1 and the line value for correction */
  
  if(nbgroup>1)
    {    
      OS_message(0,OS_STD,"[SP_addSpectra] Compute factor for the merging\n");
      MMV_malloc(work,nbsp,double);
      imin=igroup=0;
      while(igroup<nbgroup-1)
	{
	  imax=imin;
	  while((imax<nbsp)&&(group->ldata[imax]==igroup)) imax++;
	  //	  printf("igroup: %2ld imin: %2ld  imax: %2ld\n",igroup,imin,imax);
	  ++igroup;
	  if(imax==nbsp)
	    {
	      OS_message(0,OS_ERR,"[SP_addSpectra] BUG at line %d of file %s\n",__LINE__,__FILE__);
	      exit(1);
	    }
	  jmin=imax;
	  jmax=jmin;
	  while((jmax<nbsp)&&(group->ldata[jmax]==igroup)) jmax++;
	  //printf("igroup: %2ld jmin: %2ld  jmax: %2ld\n",igroup,jmin,jmax);

	  w1min=wmin[imin];
	  w1max=wmax[imin];
	  for(si=imin+1;si<imax;si++)
	    {
	      w1min=(w1min<wmin[si])?w1min:wmin[si];
	      w1max=(w1max>wmax[si])?w1max:wmax[si];
	    }

	  w2min=wmin[jmin];
	  w2max=wmax[jmin];
	  for(sj=jmin+1;sj<jmax;sj++)
	    {
	      w2min=(w2min<wmin[sj])?w2min:wmin[sj];
	      w2max=(w2max>wmax[sj])?w2max:wmax[sj];
	    }

	  tmin=(w1min>w2min)?w1min:w2min;
	  tmax=(w1max<w2max)?w1max:w2max;
	  if(tmin>=tmax) /* no overlap*/
	    {
	      w1min=w1max-width/10.;
	      w2max=w2min+width/10.;
	    }
	  else
	    {
	      w1min=w2min=tmin;
	      w1max=w2max=tmax;
	    }

	  for(si=imin;si<imax;si++)
	    {
	      k1min=findNear(spectra[si]->data[spectra[si]->colW],w1min);
	      k1max=findNear(spectra[si]->data[spectra[si]->colW],w1max)+1;
	      for(i=0,j=k1min;j<k1max;j++,i++) 
		tmp[i]=spectra[si]->flux[j];
	      work[si]=median(tmp,k1max-k1min);
	    }

	  for(sj=jmin;sj<jmax;sj++)
	    {
	      k2min=findNear(spectra[sj]->data[spectra[sj]->colW],w2min);
	      k2max=findNear(spectra[sj]->data[spectra[sj]->colW],w2max)+1;
	      for(i=0,j=k2min;j<k2max;j++,i++) 
		tmp[i]=spectra[sj]->flux[j];
	      work[sj]=median(tmp,k2max-k2min);
	    }

	  fact1=median(work+imin,imax-imin);
	  fact2=median(work+jmin,jmax-jmin);
	  if(fact1<=0) fact1=1;
	  if(fact2<=0) fact2=1;


	  /* Factor are computed. Now rebin edges*/
	  /* imin->imax index of spectra in group 1 */
	  /* jmin->jmax index of spectra in group 2 */
	  /* Compute flx1 and flx2 */
	  
	  if(tmin>=tmax)
	    {
	      fact1/=fact2;
	      OS_message(0,OS_STD,"[SP_addSpectra] Factor for merging for group #%d : %f\n",1+igroup,fact1);
	      for(sj=jmin;sj<jmax;sj++)
		for(j=0;j<spectra[sj]->npix;j++)
		  {
		    spectra[sj]->flux[j]*=fact1;
		    spectra[sj]->sigm[j]*=fact1;
		  }
	  
	    }
	  else
	    {
	      /*First crude addition of the edge 2*/
	      k1min=findNear(spectra[imin]->data[spectra[imin]->colW],w1min);
	      k1max=findNear(spectra[imin]->data[spectra[imin]->colW],w1max)+1;
	      //	      printf("%d  %d\n",k1min,k1max);
	      VCV_allocate(flx1,k1max-k1min,VCTDOUBLE);
	      VCV_allocate(wav1,k1max-k1min,VCTDOUBLE);
	      VCV_allocate(flx2,k1max-k1min,VCTDOUBLE);
	      VCV_allocate(pds1,k1max-k1min,VCTDOUBLE);
	      VCV_allocate(pds2,k1max-k1min,VCTDOUBLE);

	      //	      printf("flx1 %x \n",flx1);
	      //	      printf("flx2 %x \n",flx2);
	      //	      printf("wav1 %x \n",wav1);

	      for(i=k1min,j=0;i<k1max;i++,j++)
		{
		  wav1->ddata[j]=spectra[imin]->wave[i];
		  flx1->ddata[j]=0.;
		  pds1->ddata[j]=0.;
		  flx2->ddata[j]=0.;
		  pds2->ddata[j]=0.;
		}

	      for(si=imin;si<imax;si++)
		{
		  k2min=k2max=-1;
		  dw=spectra[si]->wave+idmin[si];
		  df=spectra[si]->flux+idmin[si];
		  ds=spectra[si]->sigm+idmin[si];

		  for(i=k1min,j=0;i<k1max;i++,j++)
		    {
		      if(j==0) tmin=0.5*(3*wav1->ddata[0]-wav1->ddata[1]);
		      else     tmin=0.5*(wav1->ddata[j-1]+wav1->ddata[j]);
		      
		      if(j==(wav1->size-1)) tmax=0.5*(3*wav1->ddata[j]-wav1->ddata[j-1]);
		      else                  tmax=0.5*(wav1->ddata[j+1]+wav1->ddata[j]);
		      
		      rebinLocal(df,ds,dw,idmax[si]-idmin[si],
				 tmin,tmax,&k2min,&k2max,
				 &fsum,&wsum);
		      if(wsum<=0) continue;
		      weight=fsum/wsum;
		      weight*=weight;
		      pds1->ddata[j]+=weight;
		      flx1->ddata[j]+=weight*fsum;
		    }
		}
	      
	      for(sj=jmin;sj<jmax;sj++)
		{
		  k2min=k2max=-1;
		  dw=spectra[sj]->wave+idmin[sj];
		  df=spectra[sj]->flux+idmin[sj];
		  ds=spectra[sj]->sigm+idmin[sj];

		  for(i=k1min,j=0;i<k1max;i++,j++)
		    {
		      if(j==0) tmin=0.5*(3*wav1->ddata[0]-wav1->ddata[1]);
		      else     tmin=0.5*(wav1->ddata[j-1]+wav1->ddata[j]);
		      
		      if(j==(wav1->size-1)) tmax=0.5*(3*wav1->ddata[j]-wav1->ddata[j-1]);
		      else                  tmax=0.5*(wav1->ddata[j+1]+wav1->ddata[j]);
		      
		      rebinLocal(df,ds,dw,idmax[sj]-idmin[sj],
				 tmin,tmax,&k2min,&k2max,
				 &fsum,&wsum);
		      if(wsum<=0) continue;
		      weight=fsum/wsum;
		      weight*=weight;
		      pds2->ddata[j]+=weight;
		      flx2->ddata[j]+=weight*fsum;
		    }
		}

	      ngood=0;
	      for(j=0;j<wav1->size;j++)
		{
		  if((!pds1->ddata[j])||(!pds2->ddata[j])) continue;
		  y1=flx1->ddata[j]/(fact1*pds1->ddata[j]);
		  y2=flx2->ddata[j]/(fact2*pds2->ddata[j]);
		  
		  if((y1<0.2)||(y2<0.2)) continue;
		  flx1->ddata[ngood]=y1/y2;
		  wav1->ddata[ngood]=wav1->ddata[j];
		  ngood++;
		}
	      if(ngood)
		fitLineIter(flx1->ddata,wav1->ddata,ngood,&a1,&b1);
	      else
		{
		  a1=0;
		  b1=1;
		}
	      wpas=0.1/(wav1->ddata[1]-wav1->ddata[0]);
	      fact1/=fact2;
	      /* apply correction */
	      for(sj=jmin;sj<jmax;sj++)
		for(j=0;j<spectra[sj]->npix;j++)
		  {
		    wsum=spectra[sj]->wave[j];
		    weight=(fact1*(a1*wsum+b1)*expFilter(wsum,w1max,-wpas))+
		      (fact1*(a1*w1max+b1)*expFilter(wsum,w1max,wpas));
		      
		    spectra[sj]->flux[j]*=weight;
		    spectra[sj]->sigm[j]*=weight;
		  }
	      VC_free(pds2);
	      VC_free(pds1);
	      VC_free(flx1);
	      VC_free(flx2);
	      VC_free(wav1);

	    }
	  imin=jmin;
	}
      MM_free(work);
    }


  /**************************/
  /* Compute the wave range */
  /**************************/

  OS_message(0,OS_STD,"[SP_addSpectra] Compute wavelength range\n");

  /*****************************************/
  /* merge all the wave limits and compute */
  /* average pixel size for each group     */
  /*****************************************/
  VCV_allocate(wlim,2*nbgroup,VCTDOUBLE);
  imin=igroup=0;
  while(imin<nbsp)
    {
      imax=imin;
      tmin=wmin[imin];
      tmax=wmax[imin];
      while((imax<nbsp)&&(group->ldata[imax]==igroup))
	{
	  tmin=(tmin<wmin[imax])?tmin:wmin[imax];
	  tmax=(tmax>wmax[imax])?tmax:wmax[imax];
	  imax++;
	}
      wlim->ddata[  2*igroup]=tmin;
      wlim->ddata[1+2*igroup]=tmax;
      imin=imax;
      igroup++;
    }
  VC_hpsort(wlim);
  /* Compute pixel size */
  
  MMV_malloc(npix,(2*nbgroup-1),long);
  
  wpas=0;
  for(i=0;i<2*nbgroup-1;i++)
    {
      k=0;
      npix[i]=0;
      tcen=0.5*(wlim->ddata[i]+wlim->ddata[i+1]);
      old_wpas=wpas;
      wpas=0;
      for(j=0;j<nbsp;j++)
	{
	  if((tcen>=wmax[j])||(tcen<=wmin[j])) continue;
	  k++;
	  wpas+=(wmax[j]-wmin[j])/(float)(idmax[j]-idmin[j]);
	}
      //      printf("chunck %2ld  k: %2ld   swpas: %f   rap:%f\n",i,k,wpas,(k>0)?(wpas/(double)k):0);
      if(k)
	wpas/=(double)k;
      else
	if(!i)
	  {
	    OS_message(0,OS_ERR,"Bug at %s line %d !!!\n",__FILE__,__LINE__);
	    exit(1);
	  }
	else
	  wpas=old_wpas;
      wpas*=rebin_pixfactor;
      npix[i]=(long)((wlim->ddata[i+1]-wlim->ddata[i])/wpas+0.5);
      if(!npix[i])
	{
	  OS_message(0,OS_ERR,"Bug at %s line %d !!!\n",__FILE__,__LINE__);
	  exit(1);
	}
    }
  npix_tot=0;
  for(i=0;i<2*nbgroup-1;i++) npix_tot+=npix[i]-1;
  npix_tot++;

  /* Fill the final wave array */
  VCV_allocate(wave,npix_tot,VCTDOUBLE);
  k=0;
  for(i=0;i<2*nbgroup-1;i++)
    {
      wpas=(wlim->ddata[i+1]-wlim->ddata[i])/(double)(npix[i]);
      for(j=0;j<npix[i]-1;j++)
	wave->ddata[k++]=wlim->ddata[i]+((float)j+rebin_pixshift)*wpas;
    }
  wave->ddata[k++]=wlim->ddata[2*nbgroup-1]+rebin_pixshift*wpas;

  /* display some information */
  OS_message(0,OS_STD,"\n[SP_mergeSpectra] New wavelength range (%d chunks) \n",2*nbgroup-1);
  for(i=0;i<2*nbgroup-1;i++)
    OS_message(0,OS_STD,"[SP_mergeSpectra]  chunks #%-2d : %10.2f %10.2f %9.4f\n",
	       i+1,wlim->ddata[i],wlim->ddata[i+1],
	       (wlim->ddata[i+1]-wlim->ddata[i])/(double)npix[i]);


  //  for(i=0;i<nbsp;i++) printf("i:%2d   wmin:%f   wmax:%f\n",(int)i,wmin[i],wmax[i]);
  MM_free( npix);
  VC_free( wlim);
  MM_free(wmin);
  MM_free(wmax);


  /**********************************/
  /* if spline is set rebin spectra */
  /* with cubic spline              */
  /**********************************/
  if(spline)
    {
      OS_message(0,OS_STD,"[SP_mergeSpectra]  Doing spline rebinning : \n");
      MMV_malloc(spline_spectra,nbsp,SP_spectrum);
      if(!spline_spectra)
	{
	  OS_message(0,OS_ERR,"not enough memory !!\n");
	  MM_free( kmax);
	  MM_free( kmin);
	  MM_free(idmax);
	  MM_free(idmin);
	  MM_free(  tmp);
	  return NULL;
	}
      for(i=0;i<nbsp;i++)
	{
	  tmin=spectra[i]->wave[idmin[i]];
	  tmax=spectra[i]->wave[idmax[i]-1];
	  if(wave->ddata[0]>tmin)
	    imin=0;
	  else
	    while((imin<wave->size)&&(wave->ddata[imin]<tmin)) ++imin;

	  imax=imin;
	  while((imax<wave->size)&&(wave->ddata[imax]<tmax)) ++imax;

	  spline_spectra[i]=SP_allocate(imax-imin,3);
	  if(!spline_spectra[i])
	    {
	      OS_message(0,OS_ERR,"not enough memory !!\n");
	      MM_free(spline_spectra);
	      MM_free( kmax);
	      MM_free( kmin);
	      MM_free(idmax);
	      MM_free(idmin);
	      MM_free(  tmp);
	      return NULL;
	    }
	  /*Copy Wavelength array*/
	  for(k=0,j=imin;j<imax;j++,k++) 
	    spline_spectra[i]->wave[k]=wave->ddata[j];
	  /*********************************/
	  /* do the cubic spline estimation*/
	  /* for the flux                  */
	  NRspline(spectra[i]->wave,spectra[i]->flux,spectra[i]->npix,1e30,1e30,tmp);
	  NRasplint(spectra[i]->wave,spectra[i]->flux,tmp,spectra[i]->npix,
		    spline_spectra[i]->wave,spline_spectra[i]->flux,spline_spectra[i]->npix);
	  /* for the error                 */
	  /* (strange, maybe to fix latter)*/
	  NRspline(spectra[i]->wave,spectra[i]->sigm,spectra[i]->npix,1e30,1e30,tmp);
	  NRasplint(spectra[i]->wave,spectra[i]->sigm,tmp,spectra[i]->npix,
		    spline_spectra[i]->wave,spline_spectra[i]->sigm,spline_spectra[i]->npix);
	  /* Rebin Done                    */
	  /* Put index instead of wavelenght */
	  for(k=0,j=imin;j<imax;j++,k++) 
	    spline_spectra[i]->wave[k]=j;
	  
	  /*********************************/
	  /*Old spectra[i] useless, set it to spline_spectra[i] */
	  SP_free(spectra[i]);
	  spectra[i]=spline_spectra[i];
	}
    }

  /***************************************/
  /* Allocate memory for output spectrum */
  /***************************************/
  nbcol=3;
  if(keepnbsp) ++nbcol;
  if(error_type==_BOTH_ERRORS_) ++nbcol;

  rslt=_SP_allocate(wave->size,nbcol,file,line);

  if(!rslt) 
    {
      OS_message(0,OS_ERR,"not enough memory !!\n");
      MM_free( kmax);
      MM_free( kmin);
      MM_free(idmax);
      MM_free(idmin);
      MM_free(  tmp);
      return NULL;
    }
  
  VC_free(rslt->data[rslt->colW]);
  rslt->data[rslt->colW]=wave;

  /*******************/
  /* Do the addition */
  /*******************/
  VCV_allocate(eflux,nbsp,VCTDOUBLE);
  VCV_allocate(esigm,nbsp,VCTDOUBLE);
  VCV_allocate(emask,nbsp,VCTDOUBLE);

  OS_message(0,OS_STD,"\n[SP_addSpectra] Do the addition :   0%%");
  old_pourc=pourc=0;


  for(j=0;j<rslt->npix;j++)
    {
      pourc=(int)(j*100/(rslt->npix-1));
      if(pourc>old_pourc)
	{
	  old_pourc=pourc;
	  OS_message(0,OS_STD,"\b\b\b\b%3ld%%",pourc);
	  OS_flush();
	}
      
      if(spline)
	{
	  ngood=0;
	  for(i=0;i<nbsp;i++)
	    {
	      eflux->ddata[ngood]=esigm->ddata[ngood]=0.;
	      dw=spectra[i]->wave;
	      df=spectra[i]->flux;
	      ds=spectra[i]->sigm;
	      if(getSplineInter(df,ds,dw,spectra[i]->npix,j,kmin+i,
				eflux->ddata+ngood,esigm->ddata+ngood)) continue;
	      emask->ddata[ngood]=1.;
	      ++ngood;
	    }
	}
      else
	{
	  if(j==0)  tmin=0.5*(3*wave->ddata[0]-wave->ddata[1]);
	  else      tmin=0.5*(wave->ddata[j-1]+wave->ddata[j]);
	  
	  if(j==(rslt->npix-1)) tmax=0.5*(3*wave->ddata[rslt->npix-1]-wave->ddata[rslt->npix-2]);
	  else                  tmax=0.5*(  wave->ddata[j+1]+         wave->ddata[j]);
	  
	  ngood=0;
	  for(i=0;i<nbsp;i++)
	    {
	      eflux->ddata[ngood]=esigm->ddata[ngood]=0.;
	      dw=spectra[i]->wave+idmin[i];
	      df=spectra[i]->flux+idmin[i];
	      ds=spectra[i]->sigm+idmin[i];
	      
	      if(rebinLocal(df,ds,dw,idmax[i]-idmin[i],
			    tmin,tmax,kmin+i,kmax+i,
			    eflux->ddata+ngood,esigm->ddata+ngood)) continue;
	      emask->ddata[ngood]=1.;
	      ++ngood;
	    }
	}
/*       if((tmin>5156)&&(tmax<5158)) */
/* 	printf("tmin:%f  tmax:%f   ngood: %d\n",tmin,tmax,ngood); */

      mean_srianand=sigm_srianand=fsum=wsum=0.0;
      nbg_srianand=nbg_bastien=0;
      if(ngood)
	{
	  eflux->size=ngood;
	  esigm->size=ngood;
	  emask->size=ngood;
	  
	  if(tclip==_DATACLIPPING_)
	    dataClipping(eflux,esigm,&emask,clip);
	  else if (tclip!=_NOCLIPPING_)
	    sigmaClipping(eflux,&emask,clip,0);

	  for(i=0;i<ngood;i++)
	    {
	      if(emask->ddata[i])
		{
		  weight=esigm->ddata[i];
		  mean_srianand+=eflux->ddata[i];
		  sigm_srianand+=eflux->ddata[i]*eflux->ddata[i];
		  ++nbg_srianand;
		  if(weight<=0) continue;
		  ++nbg_bastien;
		  weight=1./(weight*weight);
		  wsum+=weight;
		  fsum+=eflux->ddata[i]*weight;
		}
	    }
	}
      if(nbg_srianand>0)
	{
/* 	  mean_srianand*=mean_srianand; */
/* 	  mean_srianand/=(double)(nbg_srianand); /\* n.<X>^2 *\/ */
/* 	  sigm_srianand-=mean_srianand;  // n<X^2>-n<X>^2 */
/* 	  sigm_srianand/=(double)(nbg_srianand);  //(n/n-1)(<X^2>-<X>^2) */
	  mean_srianand/=(double)(nbg_srianand);
	  mean_srianand*=mean_srianand;
	  
	  sigm_srianand/=(double)(nbg_srianand);
	  sigm_srianand=(sigm_srianand-mean_srianand)/(double)(nbg_srianand);
	}
      else
	sigm_srianand=0.;
      if(wsum>0)
	{
	  wsum=1./wsum;
	  fsum*=wsum;
	  switch (error_type)
	    {
	    case _BOTH_ERRORS_ :
	    case _BASTIEN_     : 
	      {
		wsum=sqrt(wsum);
		sigm_srianand=sqrt(sigm_srianand);
	      }
	      break;
	    case _SRIANAND_                 : wsum=sqrt(sigm_srianand);
	      break;
	    case _BASTIEN_AND_SRIANAND_     : wsum=sqrt(wsum+sigm_srianand);
	      break;
	    case _MAX_BASTIEN_AND_SRIANAND_ : wsum=sqrt((sigm_srianand>wsum)?sigm_srianand:wsum);
	      break;
	    case _MAX_BASTIEN_AND_MAX_SRIANAND_ : 
	      if(nbg_srianand>1)
		fct=(1+sqrt(2./(nbg_srianand-1)));
	      else
		fct=1.;
	      wsum=sqrt((sigm_srianand>wsum)?sigm_srianand*fct:wsum);
	      break;
	    default : wsum=sqrt((sigm_srianand>wsum)?sigm_srianand:wsum);
	    }
	}
      else
	fsum=wsum=0;

      if((!IS_NAN(fsum))&&(!IS_NAN(wsum)))
	{
	  rslt->flux[j]=fsum;
	  rslt->sigm[j]=wsum;
	}
      if((error_type==_BOTH_ERRORS_)&&(!IS_NAN(sigm_srianand)))
	rslt->cont[j]=sigm_srianand;
      if(keepnbsp)
	rslt->data[rslt->ndata-1]->ddata[j]=nbg_bastien;

      eflux->size=nbsp;
      esigm->size=nbsp;
      emask->size=nbsp;
    }
  OS_message(0,OS_STD,"\b\b\b\b%3ld%%\n",100);
  VC_free(emask);
  VC_free(esigm);
  VC_free(eflux);
  MM_free( kmin);
  MM_free( kmax);
  MM_free(idmin);
  MM_free(idmax);
  MM_free(tmp);
  return rslt;
}
Example #20
0
 double median(){ return median(0,m_width,0,m_height); }
Example #21
0
void prepareColBicor(double * col, size_t nr, double maxPOutliers, int fallback,
                     int cosine,
                     double * res, size_t * nNAentries, 
                     int * NAmed, volatile int * zeroMAD,
                     double * aux, double * aux2)
{
  // const double asymptCorr = 1.4826, qnorm75 = 0.6744898;
  // Note to self: asymptCorr * qnorm75 is very close to 1 and should equal 1 theoretically. Should
  // probably leave them out completely. 

  if (fallback==4)
  {
    prepareColCor(col, nr, cosine, res, nNAentries, NAmed);
    return;
  }

  int err = 0;

  // Calculate the median of col

  memcpy((void *)res, (void *)col, nr * sizeof(double));
  double med = median(res, nr, 0, &err);

  // Create a conditional copy of the median
  double medX;
  if (cosine) medX = 0; else medX = med;

  *zeroMAD = 0;
  // calculate absolute deviations from the median

  if (ISNAN(med))
  {
    *NAmed = 1;
    for (size_t k=0; k<nr; k++) *(res + k) = 0;
  } else {
    *NAmed = 0;
    *nNAentries = 0;
    for (size_t k=0; k<nr; k++)
      if (ISNAN(col[k]))
      {
        (*nNAentries)++;
        res[k] = NA_REAL;
        aux[k] = NA_REAL;
      } else {
        res[k] = col[k] - medX;
        aux[k] = fabs(col[k] - med);
      }

    // calculate mad, i.e. median absolute deviation
    double mad = median(aux, nr, 0, &err);

    // If mad is zero, value of fallback decides what is it we will do.
    if (mad==0)
    {
       *zeroMAD = 1;
       switch (fallback)
       {
          case 1: 
          {
             // Return after zeoring out results and setting the NAmed flag
             for (size_t k=0; k<nr; k++) res[k] = 0;
             *NAmed = 1;
             return;
          }
          case 2: 
             // Switch to Pearson correlation and return
             // Rprintf("mad is zero in a column. Switching to Pearson for this column.\n");
             prepareColCor(col, nr, cosine, res, nNAentries, NAmed);
          case 3: 
             // Do nothing: the setting of *zeroMAD above is enough.
             return;
       }
    } 

    // We now re-use aux to store a copy of the weights ux. To calculate them, first get (x-med)/(9*mad).

    // Rprintf("median: %6.4f, mad: %6.4f, cosine: %d\n", med, mad, cosine);

    double denom = 9.0 * mad;
    for (size_t k=0; k<nr; k++)
      if (!ISNAN(col[k]))
        aux[k] = (col[k] - med) / denom;
      else
        aux[k] = NA_REAL;

    // Get the low and high quantiles  of ux
    memcpy((void *)aux2, (void *)aux, nr * sizeof(double));
    double lowQ = quantile(aux2, nr, maxPOutliers, 0, &err);

    memcpy((void *)aux2, (void *)aux, nr * sizeof(double));
    double hiQ = quantile(aux2, nr, 1-maxPOutliers, 0, &err);

    // Rprintf("prepareColBicor: lowQ=%f, hiQ = %f\n", lowQ, hiQ);
    // If the low quantile is below -1, rescale the aux (that serve as ux below)
    // such that the low quantile will fall at -1; similarly for the high quantile

    if (lowQ > -RefUX) lowQ = -RefUX;
    if (hiQ < RefUX) hiQ = RefUX;
    lowQ = fabs(lowQ);

    for (size_t k=0; k<nr; k++) if (!ISNAN(aux[k]))
    {
      if (aux[k] < 0)
        aux[k] = aux[k] * RefUX / lowQ;
      else
        aux[k] = aux[k] * RefUX / hiQ;
    }

    // Calculate the (1-ux^2)^2 * (x-median(x)) 

    LDOUBLE sum = 0;
    for (size_t k=0; k<nr; k++)
      if (!ISNAN(res[k]))
      {
        double ux = aux[k];
        if (fabs(ux) > 1) ux = 1;  // sign of ux doesn't matter.
        ux = 1-ux*ux;
        res[k] *= ux*ux ;
        sum += res[k]*res[k];
      } else
        res[k] = 0;
    sum = sqrtl(sum);
    if (sum==0)
    {
       for (size_t k=0; k<nr; k++)
          res[k] = 0;
       *NAmed = 1;
    } else {
       for (size_t k=0; k<nr; k++)
          res[k] = res[k] / sum;
    }
  }
}
Example #22
0
 double median(Mask* m){ return median(0,m_width,0,m_height,m); }
bool BlockchainExplorerDataBuilder::fillBlockDetails(const Block &block, BlockDetails& blockDetails) {
  Crypto::Hash hash = get_block_hash(block);

  blockDetails.majorVersion = block.majorVersion;
  blockDetails.minorVersion = block.minorVersion;
  blockDetails.timestamp = block.timestamp;
  blockDetails.prevBlockHash = block.previousBlockHash;
  blockDetails.nonce = block.nonce;
  blockDetails.hash = hash;

  blockDetails.reward = 0;
  for (const TransactionOutput& out : block.baseTransaction.outputs) {
    blockDetails.reward += out.amount;
  }

  if (block.baseTransaction.inputs.front().type() != typeid(BaseInput))
    return false;
  blockDetails.height = boost::get<BaseInput>(block.baseTransaction.inputs.front()).blockIndex;

  Crypto::Hash tmpHash = core.getBlockIdByHeight(blockDetails.height);
  blockDetails.isOrphaned = hash != tmpHash;

  if (!core.getBlockDifficulty(blockDetails.height, blockDetails.difficulty)) {
    return false;
  }

  std::vector<size_t> blocksSizes;
  if (!core.getBackwardBlocksSizes(blockDetails.height, blocksSizes, parameters::CRYPTONOTE_REWARD_BLOCKS_WINDOW)) {
    return false;
  }
  blockDetails.sizeMedian = median(blocksSizes);

  size_t blockSize = 0;
  if (!core.getBlockSize(hash, blockSize)) {
    return false;
  }
  blockDetails.transactionsCumulativeSize = blockSize;

  size_t blokBlobSize = getObjectBinarySize(block);
  size_t minerTxBlobSize = getObjectBinarySize(block.baseTransaction);
  blockDetails.blockSize = blokBlobSize + blockDetails.transactionsCumulativeSize - minerTxBlobSize;

  if (!core.getAlreadyGeneratedCoins(hash, blockDetails.alreadyGeneratedCoins)) {
    return false;
  }

  if (!core.getGeneratedTransactionsNumber(blockDetails.height, blockDetails.alreadyGeneratedTransactions)) {
    return false;
  }

  uint64_t prevBlockGeneratedCoins = 0;
  if (blockDetails.height > 0) {
    if (!core.getAlreadyGeneratedCoins(block.previousBlockHash, prevBlockGeneratedCoins)) {
      return false;
    }
  }
  uint64_t maxReward = 0;
  uint64_t currentReward = 0;
  int64_t emissionChange = 0;
  if (!core.getBlockReward(blockDetails.sizeMedian, 0, prevBlockGeneratedCoins, 0, maxReward, emissionChange)) {
    return false;
  }
  if (!core.getBlockReward(blockDetails.sizeMedian, blockDetails.transactionsCumulativeSize, prevBlockGeneratedCoins, 0, currentReward, emissionChange)) {
    return false;
  }

  blockDetails.baseReward = maxReward;
  if (maxReward == 0 && currentReward == 0) {
    blockDetails.penalty = static_cast<double>(0);
  } else {
    if (maxReward < currentReward) {
      return false;
    }
    blockDetails.penalty = static_cast<double>(maxReward - currentReward) / static_cast<double>(maxReward);
  }


  blockDetails.transactions.reserve(block.transactionHashes.size() + 1);
  TransactionDetails transactionDetails;
  if (!fillTransactionDetails(block.baseTransaction, transactionDetails, block.timestamp)) {
    return false;
  }
  blockDetails.transactions.push_back(std::move(transactionDetails));

  std::list<Transaction> found;
  std::list<Crypto::Hash> missed;
  core.getTransactions(block.transactionHashes, found, missed, blockDetails.isOrphaned);
  if (found.size() != block.transactionHashes.size()) {
    return false;
  }

  blockDetails.totalFeeAmount = 0;

  for (const Transaction& tx : found) {
    TransactionDetails transactionDetails;
    if (!fillTransactionDetails(tx, transactionDetails, block.timestamp)) {
      return false;
    }
    blockDetails.transactions.push_back(std::move(transactionDetails));
    blockDetails.totalFeeAmount += transactionDetails.fee;
  }
  return true;
}
Example #24
0
 double median(int offset){ return median(0,m_width,0,m_height,offset); }
Example #25
0
int main(int, char**)
{
    std::vector<std::chrono::duration<double,std::milli>> duration_vector_1;
    std::vector<std::chrono::duration<double,std::milli>> duration_vector_2;

#if SYNTHETIC_INPUT
    Halide::Buffer<uint8_t> im1(10, 10);
    Halide::Buffer<uint8_t> im2(10, 10);

    for (int i = 0; i < 10; i++)
	    for (int j = 0; j < 10; j++)
	    {
		    im1(i, j) = (uint8_t) i*i+j*j;
		    im2(i, j) = (uint8_t) i*i+j*j;
	    }
#else
    Halide::Buffer<uint8_t> im1 = Halide::Tools::load_image("./utils/images/rgb.png");
    Halide::Buffer<uint8_t> im2 = Halide::Tools::load_image("./utils/images/rgb.png");
#endif

    Halide::Buffer<float> Ix_m(im1.width(), im1.height());
    Halide::Buffer<float> Iy_m(im1.width(), im1.height());
    Halide::Buffer<float> It_m(im1.width(), im1.height());
    Halide::Buffer<int> C1(_NC);
    Halide::Buffer<int> C2(_NC);
    Halide::Buffer<int> SIZES(2);
    Halide::Buffer<int> u(_NC);
    Halide::Buffer<int> v(_NC);
    Halide::Buffer<float> A(2, 4*w*w);
    Halide::Buffer<float> tA(4*w*w, 2);
    Halide::Buffer<double> pinvA(4*w*w, 2);
    Halide::Buffer<double> det(1);
    Halide::Buffer<float> tAA(2, 2);
    Halide::Buffer<double> X(2, 2);

    SIZES(0) = im1.height();
    SIZES(1) = im1.width();
    C1(0) = 500; C2(0) = 400;
    C1(1) = 800; C2(1) = 900;
    C1(2) = 200; C2(2) = 400;
    C1(3) = 400; C2(3) = 200;
    C1(4) = 400; C2(4) = 500;
    C1(5) = 800; C2(5) = 200;
    C1(6) = 200; C2(6) = 900;
    C1(7) = 900; C2(7) = 200;

    det(0) = 0;
    init_buffer(Ix_m, (float) 0);
    init_buffer(Iy_m, (float) 0);
    init_buffer(It_m, (float) 0);
    init_buffer(A, (float) 0);
    init_buffer(tA, (float) 0);
    init_buffer(pinvA, (double) 0);
    init_buffer(tAA, (float) 0);
    init_buffer(X, (double) 0);

    // Warm up
    optical_flow_tiramisu(SIZES.raw_buffer(), im1.raw_buffer(), im2.raw_buffer(),
			  Ix_m.raw_buffer(), Iy_m.raw_buffer(), It_m.raw_buffer(),
			  C1.raw_buffer(), C2.raw_buffer(), u.raw_buffer(), v.raw_buffer(), A.raw_buffer(), pinvA.raw_buffer(), det.raw_buffer(), tAA.raw_buffer(), tA.raw_buffer(), X.raw_buffer());

    // Tiramisu
    for (int i=0; i<NB_TESTS; i++)
    {
        auto start1 = std::chrono::high_resolution_clock::now();
        optical_flow_tiramisu(SIZES.raw_buffer(), im1.raw_buffer(), im2.raw_buffer(),
			  Ix_m.raw_buffer(), Iy_m.raw_buffer(), It_m.raw_buffer(),
			  C1.raw_buffer(), C2.raw_buffer(), u.raw_buffer(), v.raw_buffer(), A.raw_buffer(), pinvA.raw_buffer(), det.raw_buffer(), tAA.raw_buffer(), tA.raw_buffer(), X.raw_buffer());
        auto end1 = std::chrono::high_resolution_clock::now();
        std::chrono::duration<double,std::milli> duration1 = end1 - start1;
        duration_vector_1.push_back(duration1);
    }

    std::cout << "Time: " << median(duration_vector_1) << std::endl;

#if SYNTHETIC_INPUT
    print_buffer(im1);
    print_buffer(im2);

    print_buffer(Ix_m);
    print_buffer(Iy_m);
    print_buffer(It_m);

    print_buffer(A);
    print_buffer(tA);
    print_buffer(tAA);
    print_buffer(det);
    print_buffer(X);
    print_buffer(pinvA);
#endif

    std::cout << "Output" << std::endl;

    print_buffer(u);
    print_buffer(v);

    return 0;
}
Example #26
0
 double median(int offset, Mask* m){ return median(0,m_width,0,m_height,offset,m); }
bool dgCollisionConvexHull::Create (dgInt32 count, dgInt32 strideInBytes, const dgFloat32* const vertexArray, dgFloat32 tolerance)
{
	dgInt32 stride = strideInBytes / sizeof (dgFloat32);
	dgStack<dgFloat64> buffer(3 * 2 * count);
	for (dgInt32 i = 0; i < count; i ++) {
		buffer[i * 3 + 0] = vertexArray[i * stride + 0];
		buffer[i * 3 + 1] = vertexArray[i * stride + 1];
		buffer[i * 3 + 2] = vertexArray[i * stride + 2];
	}

	dgConvexHull3d* convexHull =  new (GetAllocator()) dgConvexHull3d (GetAllocator(), &buffer[0], 3 * sizeof (dgFloat64), count, tolerance);
	if (!convexHull->GetCount()) {
		// this is a degenerated hull hull to add some thickness and for a thick plane
		delete convexHull;

		dgStack<dgVector> tmp(3 * count);
		for (dgInt32 i = 0; i < count; i ++) {
			tmp[i][0] = dgFloat32 (buffer[i*3 + 0]);
			tmp[i][1] = dgFloat32 (buffer[i*3 + 1]);
			tmp[i][2] = dgFloat32 (buffer[i*3 + 2]);
			tmp[i][2] = dgFloat32 (0.0f);
		}
	
		dgObb sphere;
		sphere.SetDimensions (&tmp[0][0], sizeof (dgVector), count);

		dgInt32 index = 0;
		dgFloat32 size = dgFloat32 (1.0e10f);
		for (dgInt32 i = 0; i < 3; i ++) {
			if (sphere.m_size[i] < size) {
				index = i;
				size = sphere.m_size[i];
			}
		}
		dgVector normal (dgFloat32 (0.0f), dgFloat32 (0.0f), dgFloat32 (0.0f), dgFloat32 (0.0f));
		normal[index] = dgFloat32 (1.0f);
		dgVector step = sphere.RotateVector (normal.Scale3 (dgFloat32 (0.05f)));
		for (dgInt32 i = 0; i < count; i ++) {
			dgVector p1 (tmp[i] + step);
			dgVector p2 (tmp[i] - step);

			buffer[i * 3 + 0] = p1.m_x;
			buffer[i * 3 + 1] = p1.m_y;
			buffer[i * 3 + 2] = p1.m_z;
			buffer[(i + count) * 3 + 0] = p2.m_x;
			buffer[(i + count) * 3 + 1] = p2.m_y;
			buffer[(i + count) * 3 + 2] = p2.m_z;
		}
		count *= 2;
		convexHull =  new (GetAllocator()) dgConvexHull3d (GetAllocator(), &buffer[0], 3 * sizeof (dgFloat64), count, tolerance);
		if (!convexHull->GetCount()) {
			delete convexHull;
			return false;
		}
	}

	// check for degenerated faces
	for (bool success = false; !success;  ) {
		success = true;
		const dgBigVector* const hullVertexArray = convexHull->GetVertexPool();

		dgStack<dgInt8> mask(convexHull->GetVertexCount());
		memset (&mask[0], 1, mask.GetSizeInBytes());
		for (dgConvexHull3d::dgListNode* node = convexHull->GetFirst(); node; node = node->GetNext()) {
			dgConvexHull3DFace& face = node->GetInfo();
			const dgBigVector& p0 = hullVertexArray[face.m_index[0]];
			const dgBigVector& p1 = hullVertexArray[face.m_index[1]];
			const dgBigVector& p2 = hullVertexArray[face.m_index[2]];
			dgBigVector p1p0 (p1 - p0);
			dgBigVector p2p0 (p2 - p0);
			dgBigVector normal (p2p0 * p1p0);
			dgFloat64 mag2 = normal % normal;
			if (mag2 < dgFloat64 (1.0e-6f * 1.0e-6f)) {
				success = false;
				dgInt32 index = -1;
				dgBigVector p2p1 (p2 - p1);
				dgFloat64 dist10 = p1p0 % p1p0;
				dgFloat64 dist20 = p2p0 % p2p0;
				dgFloat64 dist21 = p2p1 % p2p1;
				if ((dist10 >= dist20) && (dist10 >= dist21)) {
					index = 2;
				} else if ((dist20 >= dist10) && (dist20 >= dist21)) {
					index = 1;
				} else if ((dist21 >= dist10) && (dist21 >= dist20)) {
					index = 0;
				}
				dgAssert (index != -1);
				mask[face.m_index[index]] = 0;
			}
		}
		if (!success) {
			dgInt32 count = 0;
			dgInt32 vertexCount = convexHull->GetVertexCount();
			for (dgInt32 i = 0; i < vertexCount; i ++) {
				if (mask[i]) {
					buffer[count * 3 + 0] = hullVertexArray[i].m_x;
					buffer[count * 3 + 1] = hullVertexArray[i].m_y;
					buffer[count * 3 + 2] = hullVertexArray[i].m_z;
					count ++;
				}
			}
			delete convexHull;
			convexHull =  new (GetAllocator()) dgConvexHull3d (GetAllocator(), &buffer[0], 3 * sizeof (dgFloat64), count, tolerance);
		}
	}

	dgAssert (convexHull);
	dgInt32 vertexCount = convexHull->GetVertexCount();
	if (vertexCount < 4) {
		delete convexHull;
		return false;
	}
	

	const dgBigVector* const hullVertexArray = convexHull->GetVertexPool();

	dgPolyhedra polyhedra (GetAllocator());
	polyhedra.BeginFace();
	for (dgConvexHull3d::dgListNode* node = convexHull->GetFirst(); node; node = node->GetNext()) {
		dgConvexHull3DFace& face = node->GetInfo();
		polyhedra.AddFace (face.m_index[0], face.m_index[1], face.m_index[2]);
	}
	polyhedra.EndFace();

	if (vertexCount > 4) {
//		bool edgeRemoved = false;
//		while (RemoveCoplanarEdge (polyhedra, hullVertexArray)) {
//			edgeRemoved = true;
//		}
//		if (edgeRemoved) {
//			if (!CheckConvex (polyhedra, hullVertexArray)) {
//				delete convexHull;
//				return false;
//			}
//		}
		while (RemoveCoplanarEdge (polyhedra, hullVertexArray));
	}

	dgStack<dgInt32> vertexMap(vertexCount);
	memset (&vertexMap[0], -1, vertexCount * sizeof (dgInt32));

	dgInt32 mark = polyhedra.IncLRU();
	dgPolyhedra::Iterator iter (polyhedra);
	for (iter.Begin(); iter; iter ++) {
		dgEdge* const edge = &iter.GetNode()->GetInfo();
		if (edge->m_mark != mark) {
			if (vertexMap[edge->m_incidentVertex] == -1) {
				vertexMap[edge->m_incidentVertex] = m_vertexCount;
				m_vertexCount ++;
			}
			dgEdge* ptr = edge;
			do {
				ptr->m_mark = mark;
				ptr->m_userData = m_edgeCount;
				m_edgeCount ++;
				ptr = ptr->m_twin->m_next;
			} while (ptr != edge) ;
		}
	} 

	m_vertex = (dgVector*) m_allocator->Malloc (dgInt32 (m_vertexCount * sizeof (dgVector)));
	m_simplex = (dgConvexSimplexEdge*) m_allocator->Malloc (dgInt32 (m_edgeCount * sizeof (dgConvexSimplexEdge)));
	m_vertexToEdgeMapping = (const dgConvexSimplexEdge**) m_allocator->Malloc (dgInt32 (m_vertexCount * sizeof (dgConvexSimplexEdge*)));

	for (dgInt32 i = 0; i < vertexCount; i ++) {
		if (vertexMap[i] != -1) {
			m_vertex[vertexMap[i]] = hullVertexArray[i];
			m_vertex[vertexMap[i]].m_w = dgFloat32 (0.0f);
		}
	}
	delete convexHull;

	vertexCount = m_vertexCount;
	mark = polyhedra.IncLRU();;
	for (iter.Begin(); iter; iter ++) {
		dgEdge* const edge = &iter.GetNode()->GetInfo();
		if (edge->m_mark != mark) {
			dgEdge *ptr = edge;
			do {
				ptr->m_mark = mark;
				dgConvexSimplexEdge* const simplexPtr = &m_simplex[ptr->m_userData];
				simplexPtr->m_vertex = vertexMap[ptr->m_incidentVertex];
				simplexPtr->m_next = &m_simplex[ptr->m_next->m_userData];
				simplexPtr->m_prev = &m_simplex[ptr->m_prev->m_userData];
				simplexPtr->m_twin = &m_simplex[ptr->m_twin->m_userData];

				ptr = ptr->m_twin->m_next;
			} while (ptr != edge) ;
		}
	} 

	
	m_faceCount = 0;
	dgStack<char> faceMarks (m_edgeCount);
	memset (&faceMarks[0], 0, m_edgeCount * sizeof (dgInt8));

	dgStack<dgConvexSimplexEdge*> faceArray (m_edgeCount);
	for (dgInt32 i = 0; i < m_edgeCount; i ++) {
		dgConvexSimplexEdge* const face = &m_simplex[i];
		if (!faceMarks[i]) {
			dgConvexSimplexEdge* ptr = face;
			do {
				dgAssert ((ptr - m_simplex) >= 0);
				faceMarks[dgInt32 (ptr - m_simplex)] = '1';
				ptr = ptr->m_next;
			} while (ptr != face);

			faceArray[m_faceCount] = face;
			m_faceCount ++;
		}
	}
	m_faceArray = (dgConvexSimplexEdge **) m_allocator->Malloc(dgInt32 (m_faceCount * sizeof(dgConvexSimplexEdge *)));
	memcpy (m_faceArray, &faceArray[0], m_faceCount * sizeof(dgConvexSimplexEdge *));
	
	if (vertexCount > DG_CONVEX_VERTEX_CHUNK_SIZE) {
		// create a face structure for support vertex
		dgStack<dgConvexBox> boxTree (vertexCount);
		dgTree<dgVector,dgInt32> sortTree(GetAllocator());
		dgStack<dgTree<dgVector,dgInt32>::dgTreeNode*> vertexNodeList(vertexCount);

		dgVector minP ( dgFloat32 (1.0e15f),  dgFloat32 (1.0e15f),  dgFloat32 (1.0e15f), dgFloat32 (0.0f)); 
		dgVector maxP (-dgFloat32 (1.0e15f), -dgFloat32 (1.0e15f), -dgFloat32 (1.0e15f), dgFloat32 (0.0f)); 	
		for (dgInt32 i = 0; i < vertexCount; i ++) {
			const dgVector& p = m_vertex[i];
			vertexNodeList[i] = sortTree.Insert (p, i);
			minP.m_x = dgMin (p.m_x, minP.m_x); 
			minP.m_y = dgMin (p.m_y, minP.m_y); 
			minP.m_z = dgMin (p.m_z, minP.m_z); 
			
			maxP.m_x = dgMax (p.m_x, maxP.m_x); 
			maxP.m_y = dgMax (p.m_y, maxP.m_y); 
			maxP.m_z = dgMax (p.m_z, maxP.m_z); 
		}

		boxTree[0].m_box[0] = minP;
		boxTree[0].m_box[1] = maxP;
		boxTree[0].m_leftBox = -1;
		boxTree[0].m_rightBox = -1;
		boxTree[0].m_vertexStart = 0;
		boxTree[0].m_vertexCount = vertexCount;
		dgInt32 boxCount = 1;

		dgInt32 stack = 1;
		dgInt32 stackBoxPool[64];
		stackBoxPool[0] = 0;

		while (stack) {
			stack --;
			dgInt32 boxIndex = stackBoxPool[stack];
			dgConvexBox& box = boxTree[boxIndex];
			if (box.m_vertexCount > DG_CONVEX_VERTEX_CHUNK_SIZE) {
				dgVector median (dgFloat32 (0.0f), dgFloat32 (0.0f), dgFloat32 (0.0f), dgFloat32 (0.0f));
				dgVector varian (dgFloat32 (0.0f), dgFloat32 (0.0f), dgFloat32 (0.0f), dgFloat32 (0.0f));
				for (dgInt32 i = 0; i < box.m_vertexCount; i ++) {
					dgVector& p = vertexNodeList[box.m_vertexStart + i]->GetInfo();
					minP.m_x = dgMin (p.m_x, minP.m_x); 
					minP.m_y = dgMin (p.m_y, minP.m_y); 
					minP.m_z = dgMin (p.m_z, minP.m_z); 

					maxP.m_x = dgMax (p.m_x, maxP.m_x); 
					maxP.m_y = dgMax (p.m_y, maxP.m_y); 
					maxP.m_z = dgMax (p.m_z, maxP.m_z); 

					median += p;
					varian += p.CompProduct3 (p);
				}

				varian = varian.Scale3 (dgFloat32 (box.m_vertexCount)) - median.CompProduct3(median);
				dgInt32 index = 0;
				dgFloat64 maxVarian = dgFloat64 (-1.0e10f);
				for (dgInt32 i = 0; i < 3; i ++) {
					if (varian[i] > maxVarian) {
						index = i;
						maxVarian = varian[i];
					}
				}
				dgVector center = median.Scale3 (dgFloat32 (1.0f) / dgFloat32 (box.m_vertexCount));
				dgFloat32 test = center[index];

				dgInt32 i0 = 0;
				dgInt32 i1 = box.m_vertexCount - 1;
				do {    
					for (; i0 <= i1; i0 ++) {
						dgFloat32 val = vertexNodeList[box.m_vertexStart + i0]->GetInfo()[index];
						if (val > test) {
							break;
						}
					}

					for (; i1 >= i0; i1 --) {
						dgFloat32 val = vertexNodeList[box.m_vertexStart + i1]->GetInfo()[index];
						if (val < test) {
							break;
						}
					}

					if (i0 < i1)	{
						dgSwap(vertexNodeList[box.m_vertexStart + i0], vertexNodeList[box.m_vertexStart + i1]);
						i0++; 
						i1--;
					}
				} while (i0 <= i1);

				if (i0 == 0){
					i0 = box.m_vertexCount / 2;
				}
				if (i0 >= (box.m_vertexCount - 1)){
					i0 = box.m_vertexCount / 2;
				}


				{
					dgVector minP ( dgFloat32 (1.0e15f),  dgFloat32 (1.0e15f),  dgFloat32 (1.0e15f), dgFloat32 (0.0f)); 
					dgVector maxP (-dgFloat32 (1.0e15f), -dgFloat32 (1.0e15f), -dgFloat32 (1.0e15f), dgFloat32 (0.0f)); 	
					for (dgInt32 i = i0; i < box.m_vertexCount; i ++) {
						const dgVector& p = vertexNodeList[box.m_vertexStart + i]->GetInfo();
						minP.m_x = dgMin (p.m_x, minP.m_x); 
						minP.m_y = dgMin (p.m_y, minP.m_y); 
						minP.m_z = dgMin (p.m_z, minP.m_z); 

						maxP.m_x = dgMax (p.m_x, maxP.m_x); 
						maxP.m_y = dgMax (p.m_y, maxP.m_y); 
						maxP.m_z = dgMax (p.m_z, maxP.m_z); 
					}

					box.m_rightBox = boxCount;
					boxTree[boxCount].m_box[0] = minP;
					boxTree[boxCount].m_box[1] = maxP;
					boxTree[boxCount].m_leftBox = -1;
					boxTree[boxCount].m_rightBox = -1;
					boxTree[boxCount].m_vertexStart = box.m_vertexStart + i0;
					boxTree[boxCount].m_vertexCount = box.m_vertexCount - i0;
					stackBoxPool[stack] = boxCount;
					stack ++;
					boxCount ++;
				}

				{
					dgVector minP ( dgFloat32 (1.0e15f),  dgFloat32 (1.0e15f),  dgFloat32 (1.0e15f), dgFloat32 (0.0f)); 
					dgVector maxP (-dgFloat32 (1.0e15f), -dgFloat32 (1.0e15f), -dgFloat32 (1.0e15f), dgFloat32 (0.0f)); 	
					for (dgInt32 i = 0; i < i0; i ++) {
						const dgVector& p = vertexNodeList[box.m_vertexStart + i]->GetInfo();
						minP.m_x = dgMin (p.m_x, minP.m_x); 
						minP.m_y = dgMin (p.m_y, minP.m_y); 
						minP.m_z = dgMin (p.m_z, minP.m_z); 

						maxP.m_x = dgMax (p.m_x, maxP.m_x); 
						maxP.m_y = dgMax (p.m_y, maxP.m_y); 
						maxP.m_z = dgMax (p.m_z, maxP.m_z); 
					}

					box.m_leftBox = boxCount;
					boxTree[boxCount].m_box[0] = minP;
					boxTree[boxCount].m_box[1] = maxP;
					boxTree[boxCount].m_leftBox = -1;
					boxTree[boxCount].m_rightBox = -1;
					boxTree[boxCount].m_vertexStart = box.m_vertexStart;
					boxTree[boxCount].m_vertexCount = i0;
					stackBoxPool[stack] = boxCount;
					stack ++;
					boxCount ++;
				}
			}
		}

		for (dgInt32 i = 0; i < m_vertexCount; i ++) {
			m_vertex[i] = vertexNodeList[i]->GetInfo();
			vertexNodeList[i]->GetInfo().m_w = dgFloat32 (i);
		}

		m_supportTreeCount = boxCount;
		m_supportTree = (dgConvexBox*) m_allocator->Malloc(dgInt32 (boxCount * sizeof(dgConvexBox)));		
		memcpy (m_supportTree, &boxTree[0], boxCount * sizeof(dgConvexBox));

		for (dgInt32 i = 0; i < m_edgeCount; i ++) {
			dgConvexSimplexEdge* const ptr = &m_simplex[i];
			dgTree<dgVector,dgInt32>::dgTreeNode* const node = sortTree.Find(ptr->m_vertex);
			dgInt32 index = dgInt32 (node->GetInfo().m_w);
			ptr->m_vertex = dgInt16 (index);
		}
	}

	for (dgInt32 i = 0; i < m_edgeCount; i ++) {
		dgConvexSimplexEdge* const edge = &m_simplex[i];
		m_vertexToEdgeMapping[edge->m_vertex] = edge;
	}


	SetVolumeAndCG ();
	return true;
}
Example #28
0
VC_vector _addiScaleFactors(SP_spectrum* spectra,size_t nbsp,double width,VC_vector* vout,char* file,size_t line)
{
  int done;
  size_t i,j,k,l,sz,msz,swork;
  double *df;
  double *dw;
  double *work;
  VC_vector rslt,idmin,idmax,lwave,fctrs;
  size_t spref,nbchunk,cmin,cmax,rmin,rmax;
  double medref,aux;
  double wmin,wmax;
  double chkmin,chkmax;
  
  
  rslt  =_VC_allocate(nbsp,VCTDOUBLE,file,line);
  VCV_allocate(idmin,nbsp,VCTLONG);
  VCV_allocate(idmax,nbsp,VCTLONG);
/*   printf("%d\n",(long)idmin); */
  if(rslt)
    {
      /*************************/
      /* Create working space */
      /*************************/
      msz=0;
      for(i=0;i<nbsp;i++)
	{
	  sz=spectra[i]->npix;
	  if(sz>msz) msz=sz;
	}
      MMV_malloc(work,msz,double);
      swork=0;
      /****************************************/
      /*First find the limits of each spectrum*/
      /****************************************/
      for(i=0;i<nbsp;i++)
	{
	  VC_getIdxEdge(spectra[i]->data[spectra[i]->colF],idmin->ldata+i,idmax->ldata+i);

	  if(idmin->ldata[i]>=idmax->ldata[i])
	    {
	      OS_message(0,OS_ERR,"[addiScaleFactors] No data in spectrum #%ld\n",i);
	      exit(1);
	    }
	}
      
      /*********************************************/
      /*Find the spectrum with the greatest median */
      /*********************************************/
      OS_message(0,OS_STD,"[addiScaleFactors] Search for spectra with the greatest median\n");
      spref=0;
      medref=1;
      for(i=0;i<nbsp;i++)
	{
	  sz=spectra[i]->npix;
	  df=spectra[i]->data[spectra[i]->colF]->ddata;
	  for(j=idmin->ldata[i],k=0;j<idmax->ldata[i];j++,k++)
	    work[k]=df[j];
	  swork=idmax->ldata[i]-idmin->ldata[i];
	  
	  aux=median(work,swork);
	  OS_message(0,OS_STD,"[addiScaleFactors] #%2d : %f\n",i+1,aux);
	  if((0==i)||(aux>medref))
	    {
	      medref=aux;
	      spref=i;
	    }
	}
      OS_message(0,OS_STD,"[addiScaleFactors] ------------------------------------------\n");
      OS_message(0,OS_STD,"[addiScaleFactors] Use spectrum #%d as reference\n",spref+1);

      wmin=spectra[spref]->data[spectra[spref]->colW]->ddata[idmin->ldata[spref]];
      wmax=spectra[spref]->data[spectra[spref]->colW]->ddata[idmax->ldata[spref]-1];

      nbchunk=(size_t)((wmax-wmin)/width);
      nbchunk=(nbchunk<1)?1:nbchunk;
      VCV_allocate(fctrs,nbchunk,VCTDOUBLE);
      lwave=VC_span(wmin,wmax,nbchunk+1);

      OS_message(0,OS_STD,"[addiScaleFactors]\n");
      OS_message(0,OS_STD,"[addiScaleFactors] Compute scale factors for each spectrum \n");
      for(i=0;i<nbsp;i++)
	{
	  dw=spectra[i]->data[spectra[i]->colW]->ddata;
	  df=spectra[i]->data[spectra[i]->colF]->ddata;
	  wmin=dw[idmin->ldata[i]];
	  wmax=dw[idmax->ldata[i]-1];
	  if(i!=spref)
	    {
	      done=0;
	      for(j=0;j<nbchunk;j++)
		{
		  chkmin=(wmin>lwave->ddata[  j])?wmin:lwave->ddata[  j];
		  chkmax=(wmax<lwave->ddata[1+j])?wmax:lwave->ddata[1+j];
		  
		  if(chkmin>=chkmax) continue;

		  rmin=findNear(spectra[spref]->data[spectra[spref]->colW],chkmin);
		  rmax=findNear(spectra[spref]->data[spectra[spref]->colW],chkmax)+1;
		  cmin=findNear(spectra[    i]->data[spectra[    i]->colW],chkmin);
		  cmax=findNear(spectra[    i]->data[spectra[    i]->colW],chkmax)+1;
		  for(k=rmin,l=0;k<rmax;k++,l++)
		    work[l]=spectra[spref]->data[spectra[spref]->colF]->ddata[k];
		  medref=median(work,rmax-rmin);

		  for(k=cmin,l=0;k<cmax;k++,l++)
		    work[l]=df[k];
		  aux=median(work,cmax-cmin);

		  if((aux>0)&&(medref>0))
		    fctrs->ddata[done++]=medref/aux;
		  if((medref<=0)&&(aux<=0))
		    fctrs->ddata[done++]=1.;
		}
	      if(done<=0)
		{
		  OS_message(0,OS_STD,"[addiScaleFactors] no common region. Set factor to 1\n");
		  rslt->ddata[i]=1.;
		}
	      else
		rslt->ddata[i]=median(fctrs->ddata,done);
	    }
	  else
	    rslt->ddata[i]=1.;

	  OS_message(0,OS_STD,"[addiScaleFactors] Factor for spectrum #%2d : %f\n",i+1,rslt->ddata[i]);
	}
      VC_free(lwave);
      VC_free(fctrs);
      MM_free(work);
    }
  VC_free(idmax);
  VC_free(idmin);
  if(vout) *vout=rslt;
  return rslt;
}
static dnnError_t simple_net(int want_groups_conv)
{
    dnnError_t err;
    size_t outputSize[dimension] = {(N - K_X + 2 * P_X) / S_X + 1, (N - K_Y + 2 * P_Y) / S_Y + 1, FIn, BATCH_SIZE};
    size_t outputStrides[dimension] = {1, (N - K_X + 2 * P_X) / S_X + 1,
                                       ((N - K_X + 2 * P_X) / S_X + 1) * ((N - K_Y + 2 * P_Y) / S_Y + 1),
                                       ((N - K_X + 2 * P_X) / S_X + 1) * ((N - K_Y + 2 * P_Y) / S_Y + 1) * FIn};

    size_t inputSize[dimension] = {N, N, FIn, BATCH_SIZE};
    size_t inputStrides[dimension] = {1, N, N * N, N * N * FIn};

    int inputOffset[dimension - 2] = {-P_X, -P_Y};

    dnnLayout_t lt_user_input = NULL,
                lt_user_output = NULL;
    dnnPrimitive_t cv_user_to_pool1_input = NULL;
    size_t kernelSize[2] = {K_X, K_Y};
    size_t kernelStride[2] = {S_X, S_Y};
    dnnLayout_t lt_pool1_input = NULL;
    dnnPrimitive_t pool1 = NULL;
    void *resPool1[dnnResourceNumber] = {0};
    dnnLayout_t lt_pool1_output = NULL,
                lt_pool1_workspace = NULL;
    dnnPrimitive_t cv_pool1_to_user_output = NULL;
    dnnPrimitiveAttributes_t attributes = NULL;

    double *user_i = NULL,
           *user_o = NULL;

    /*** data allocation ***/
    user_i = (double *)malloc(sizeof(double) * (inputSize[0] * inputSize[1] * inputSize[2] * inputSize[3]));

    if (user_i == NULL)
    {
        err = E_MEMORY_ERROR;
        goto bail_out;
    }

    /*** User's data description ***/
    CHECK_ERR(dnnLayoutCreate_F64(&lt_user_input, dimension, inputSize, inputStrides), err);
    CHECK_ERR(dnnLayoutCreate_F64(&lt_user_output, dimension, outputSize, outputStrides), err);

    /* Initialize attributes */
    CHECK_ERR(dnnPrimitiveAttributesCreate_F64(&attributes), err);

    /*** Pooling section ***/
    CHECK_ERR(dnnPoolingCreateForward_F64(&pool1, attributes, dnnAlgorithmPoolingMax,
                                          lt_user_input, kernelSize, kernelStride, inputOffset, dnnBorderZeros),
              err);

    CHECK_ERR(dnnLayoutCreateFromPrimitive_F64(&lt_pool1_input, pool1, dnnResourceSrc), err);
    CHECK_ERR(dnnLayoutCreateFromPrimitive_F64(&lt_pool1_output, pool1, dnnResourceDst), err);
    CHECK_ERR(dnnLayoutCreateFromPrimitive_F64(&lt_pool1_workspace, pool1, dnnResourceWorkspace), err);
    CHECK_ERR(init_conversion(&cv_user_to_pool1_input, &resPool1[dnnResourceSrc], lt_pool1_input, lt_user_input, user_i), err);

    CHECK_ERR(dnnAllocateBuffer_F64(&resPool1[dnnResourceDst], lt_pool1_output), err);
    CHECK_ERR(dnnAllocateBuffer_F64(&resPool1[dnnResourceWorkspace], lt_pool1_workspace), err);

    CHECK_ERR(init_conversion(&cv_pool1_to_user_output, &user_o, lt_user_output, lt_pool1_output, resPool1[dnnResourceDst]), err);

    srand(1);
    for (int i = 0; i < inputSize[0] * inputSize[1] * inputSize[2] * inputSize[3]; i++)
        user_i[i] = rand() % 10;

    /*** Execution ***/

    if (cv_user_to_pool1_input)
        CHECK_ERR(dnnConversionExecute_F64(cv_user_to_pool1_input, user_i, resPool1[dnnResourceSrc]), err);

    double times[NB_TESTS];
    clock_t start, end;
    for (int i = 0; i < NB_TESTS; i++)
    {
        start = clock();
        CHECK_ERR(dnnExecute_F64(pool1, (void *)resPool1), err);
        end = clock();
        double time_taken = ((double)(end - start) / CLOCKS_PER_SEC) * 1000;
        times[i] = time_taken;
    }
    printf("Pool time: %f.\n", median(NB_TESTS, times));

    if (cv_pool1_to_user_output)
        CHECK_ERR(dnnConversionExecute_F64(cv_pool1_to_user_output, resPool1[dnnResourceDst], user_o), err);
    FILE *f = fopen("mkl_result.txt", "w");
    if (f == NULL)
    {
        printf("Error opening file!\n");
        exit(1);
    }
    for (int n = 0; n < BATCH_SIZE; ++n)
        for (int z = 0; z < FIn; ++z)
            for (int y = 0; y < outputSize[1]; ++y)
                for (int x = 0; x < outputSize[0]; ++x)
                    fprintf(f, "%.0f", user_o[x + y * outputSize[0] + z * outputSize[0] * outputSize[1] + n * outputSize[0] * outputSize[1] * FIn]);

    fclose(f);

bail_out:

    dnnDelete_F64(pool1);
    dnnDelete_F64(cv_user_to_pool1_input);
    dnnDelete_F64(cv_pool1_to_user_output);
    dnnLayoutDelete_F64(lt_user_input);
    dnnLayoutDelete_F64(lt_user_output);
    dnnLayoutDelete_F64(lt_pool1_input);
    dnnLayoutDelete_F64(lt_pool1_output);
    dnnLayoutDelete_F64(lt_pool1_workspace);
    dnnPrimitiveAttributesDestroy_F64(attributes);
    dnnReleaseBuffer_F64(resPool1[dnnResourceDst]);
    dnnReleaseBuffer_F64(resPool1[dnnResourceWorkspace]);
    if ((void *)user_o != resPool1[dnnResourceDst])
        dnnReleaseBuffer_F64((void *)user_o);

    free(user_i);

    return err;
}
Example #30
0
int
main(int argc, char *argv[])
{
	GarbledCircuit gc;

    block *outputMap = allocate_blocks(2 * m);
    block *inputLabels = allocate_blocks(2 * n);
    block seed;

	int *timeGarble = calloc(times, sizeof(int));
	int *timeEval = calloc(times, sizeof(int));
	double *timeGarbleMedians = calloc(times, sizeof(double));
	double *timeEvalMedians = calloc(times, sizeof(double));

    unsigned char hash[SHA_DIGEST_LENGTH];

    GarbleType type = GARBLE_TYPE_STANDARD;

    seed = seedRandom(NULL);

    createInputLabels(inputLabels, n);
    buildAESCircuit(&gc, inputLabels);
	/* readCircuitFromFile(&gc, AES_CIRCUIT_FILE_NAME); */
    garbleCircuit(&gc, outputMap, type);
    hashGarbledCircuit(&gc, hash, type);

    {
        block *extractedLabels = allocate_blocks(n);
        block *computedOutputMap = allocate_blocks(m);
        int *inputs = calloc(n, sizeof(int));
        int *outputVals = calloc(m, sizeof(int));
        for (int i = 0; i < n; ++i) {
            inputs[i] = rand() % 2;
        }
        extractLabels(extractedLabels, inputLabels, inputs, gc.n);
        evaluate(&gc, extractedLabels, computedOutputMap, type);
        assert(mapOutputs(outputMap, computedOutputMap, outputVals, m) == SUCCESS);
        {
            GarbledCircuit gc2;

            (void) seedRandom(&seed);
            createInputLabels(inputLabels, n);
            buildAESCircuit(&gc2, inputLabels);
            assert(checkGarbledCircuit(&gc2, hash, type) == SUCCESS);
        }
        free(extractedLabels);
        free(computedOutputMap);
        free(inputs);
        free(outputVals);
    }

	for (int j = 0; j < times; j++) {
		for (int i = 0; i < times; i++) {
			timeGarble[i] = timedGarble(&gc, outputMap, type);
			timeEval[i] = timedEval(&gc, inputLabels, type);
		}
		timeGarbleMedians[j] = ((double) median(timeGarble, times)) / gc.q;
		timeEvalMedians[j] = ((double) median(timeEval, times)) / gc.q;
	}
	double garblingTime = doubleMean(timeGarbleMedians, times);
	double evalTime = doubleMean(timeEvalMedians, times);
	printf("%lf %lf\n", garblingTime, evalTime);

    free(outputMap);
    free(inputLabels);
    free(timeGarble);
    free(timeEval);
    free(timeGarbleMedians);
    free(timeEvalMedians);
	return 0;
}