示例#1
0
vector<double> MothurMetastats::calc_qvalues(vector<double>& pValues) {
	try {
        int numRows = pValues.size();
		vector<double> qvalues(numRows, 0.0);

		//fill lambdas with 0.00, 0.01, 0.02... 0.95
		vector<double> lambdas(96, 0);
		for (int i = 1; i < lambdas.size(); i++) { lambdas[i] = lambdas[i-1] + 0.01; }
		
		vector<double> pi0_hat(lambdas.size(), 0);
		
		//calculate pi0_hat
		for (int l = 0; l < lambdas.size(); l++){ // for each lambda value
			int count = 0;
			for (int i = 0; i < numRows; i++){ // for each p-value in order
				if (pValues[i] > lambdas[l]){ count++; }
			}
			pi0_hat[l] = count/(double)(numRows*(1.0-lambdas[l]));
            //cout << lambdas[l] << '\t' << count << '\t' << numRows*(1.0-lambdas[l]) << '\t' << pi0_hat[l] << endl;
		}
		
		double pi0 = smoothSpline(lambdas, pi0_hat, 3);
		
		//order p-values
		vector<double> ordered_qs = qvalues;
		vector<int> ordered_ps(pValues.size(), 0);
		for (int i = 1; i < ordered_ps.size(); i++) {  ordered_ps[i] = ordered_ps[i-1] + 1; }
		vector<double> tempPvalues = pValues;
		OrderPValues(0, numRows-1, tempPvalues, ordered_ps);
		
		ordered_qs[numRows-1] = min((pValues[ordered_ps[numRows-1]]*pi0), 1.0);
		for (int i = (numRows-2); i >= 0; i--){
			double p = pValues[ordered_ps[i]];
			double temp = p*numRows*pi0/(double)(i+1);

			ordered_qs[i] = min(temp, ordered_qs[i+1]);
		}
		
		//re-distribute calculated qvalues to appropriate rows
		for (int i = 0; i < numRows; i++){
			qvalues[ordered_ps[i]] = ordered_qs[i];
		}
		
		return qvalues;
		
	}catch(exception& e) {
		m->errorOut(e, "MothurMetastats", "calc_qvalues");
		exit(1);
	}
}
示例#2
0
    /**
     * A map detector ID and Q ranges
     * This method looks unnecessary as it could be calculated on the fly but
     * the parallelization means that lazy instantation slows it down due to the 
     * necessary CRITICAL sections required to update the cache. The Q range 
     * values are required very frequently so the total time is more than
     * offset by this precaching step
     */
    void SofQW2::initQCache(API::MatrixWorkspace_const_sptr workspace)
    {
      Mantid::Kernel::Timer clock;
      const size_t nhist(workspace->getNumberHistograms());
      const size_t nxpoints = workspace->blocksize();
      const MantidVec & X = workspace->readX(0);
      m_qcached.clear();

      PARALLEL_FOR1(workspace)
      for(int64_t i = 0 ; i < (int64_t)nhist; ++i)
      {
        PARALLEL_START_INTERUPT_REGION

        IDetector_const_sptr det;
        try
        {
           det = workspace->getDetector(i);
           if( det->isMonitor() ) det.reset();
        }
        catch(Kernel::Exception::NotFoundError&)
        {
          // Catch if no detector. Next line tests whether this happened - test placed
          // outside here because Mac Intel compiler doesn't like 'continue' in a catch
          // in an openmp block.
        }
        // If no detector found, skip onto the next spectrum
        if ( !det ) continue;

        std::vector<QValues> qvalues(nxpoints);
        DetectorGroup_const_sptr detGroup = boost::dynamic_pointer_cast<const DetectorGroup>(det);
        if( detGroup )
        {
          std::vector<IDetector_const_sptr> dets = detGroup->getDetectors();
          const size_t ndets(dets.size());
          for( size_t j = 0; j < ndets; ++j )
          {
            IDetector_const_sptr det_j = dets[j];
            QRangeCache qrange(static_cast<size_t>(i), 1.0/(double)ndets);
            for( size_t k = 0; k < nxpoints; ++k)
            {
              qvalues[k] = calculateQValues(det_j, X[k], X[k+1]);
            }
            qrange.qValues = qvalues;
            PARALLEL_CRITICAL(qcache_a)
            {
              m_qcached.insert(m_qcached.end(), qrange);
            }
          }
        }
        else
        {
          QRangeCache qrange(static_cast<size_t>(i), 1.0);
          for( size_t k = 0; k < nxpoints; ++k)
          {
            qvalues[k] = calculateQValues(det, X[k], X[k+1]);
          }
          qrange.qValues = qvalues;
          PARALLEL_CRITICAL(qcache_b)
          {
            m_qcached.insert(m_qcached.end(), qrange);
          }
        }

        PARALLEL_END_INTERUPT_REGION
      }