const unsigned KMplex::testingQuota(const KMeans::Cluster& cluster) const { unsigned quota; double den = 0.0; double num = cluster.size() * stdev(cluster.distances()); switch(alloc_) { case Neyman: for(unsigned i = 0; i < kmeans_->K(); ++i) if(!kmeans_->cluster(i).empty()) { den += (double)kmeans_->cluster(i).size() * stdev(kmeans_->cluster(i).distances()); } quota = (unsigned)((num / den) * dataset_->length() * testingFraction_) ; break; case Single: quota = 1; break; default: // Fall through case Proportional: quota = (unsigned)(cluster.size() * testingFraction_ + 0.5); break; } return quota; }
void normalization(MAT &matN,MAT &matStd) { double evStd = stdev(matStd); double avrStd = average(matStd); double evN = stdev(matN); double avrN = average(matN); for(UINT i(0);i<matN._row;i++) { for(UINT j(0);j<matN._col;j++) { double norm = (matN.GetValue(i,j)-avrN)/evN; matN.SetValue(i,j,norm*evStd+avrStd); } } }
// for a set of frames, for each pixel in the frame calc the stdev // of each element (YUV values) over the set of the frames std::vector<PixelStDev> CalcUVPixelStdev( const std::vector< FrameSection >& frames ) { // we have 6 (4*Y U, V) * frames.size * frames.begin->size numbers // we assume all frames are identical but that sould be enforced later mfxU32 fSize = frames.begin()->size(); std::vector< PixelStDev > stdev(fSize); // we should flatten the data and do //for( mfxU32 xx =0; xx != N; ++xx ) // for now for( std::vector< FrameSection >::const_iterator it = frames.begin(); it != frames.end(); ++it) { // now iterate over the pixels in the frame mfxU32 f = 0; for( FrameSection::const_iterator pIt = it->begin(); pIt != it->end(); ++pIt, ++f) { stdev[f] += *pIt; } } return stdev; }
std::vector<double> DiffusionProcess::simulate1path(const std::vector<double> &dDates) const { std::size_t iNDates = dDates.size(); std::vector<double> dResult(iNDates, dX0_); std::tr1::normal_distribution<double> dist(0.0,1.0); double dOldValue = dX0_; for (std::size_t iDate = 1 ; iDate < iNDates ; ++iDate) { double t0 = dDates[iDate - 1], dt = dDates[iDate] - t0; dOldValue = expectation(t0, dOldValue, dt) + stdev(t0, dOldValue, dt) * dist(*m_eng); if (bFloorSimulation_ && dOldValue < dFloor_) { dResult[iDate] = dFloor_; if (bStartFromFloor_) { dOldValue = dFloor_; } } else if (bCapSimulation_ && dOldValue > dCap_) { dResult[iDate] = dCap_; if (bStartFromCap_) { dOldValue = dCap_; } } else { dResult[iDate] = dOldValue; } } return dResult; }
bool utManagerThread::stabilityCheck() { oldMcutPoss.push_back(motionCUTPos); if (oldMcutPoss.size()>12) { // keep the buffer size constant oldMcutPoss.erase(oldMcutPoss.begin()); Vector mean(2,0.0); Vector stdev(2,0.0); // find mean and standard deviation for (size_t i=0; i<oldMcutPoss.size(); i++) { mean+=oldMcutPoss[i]; stdev+=oldMcutPoss[i]*oldMcutPoss[i]; } mean/=oldMcutPoss.size(); stdev=stdev/oldMcutPoss.size()-mean*mean; stdev(0)=sqrt(stdev(0)); stdev(1)=sqrt(stdev(1)); // if samples are mostly lying around the mean if ((2.0*stdev(0)<8.0) && (2.0*stdev(1)<8.0)) return true; } return false; }
static bool bench_try(struct thrarg *thrarg, unsigned iters) { const unsigned min_samples = 10; double sum, avg, std_dev = 1.0, u = 1.0, e = 1.0; size_t n, i; bool print_samples = thrarg->params.print_samples; bool success = false; const unsigned max_samples = thrarg->params.max_samples ? thrarg->params.max_samples : 400; const double error = thrarg->params.max_error ? thrarg->params.max_error / 100.0 : 0.05; double *samples = (double *)calloc(max_samples, sizeof(double)); if (max_samples < min_samples) return false; sum = 0.0; avg = 0.0; n = 0; for (i = 0; i < max_samples; i++) { bench_once(thrarg, iters); samples[i] = thrarg->result.avg; sum += samples[i]; n = i + 1; if (n < min_samples) continue; avg = sum/n; std_dev = stdev(n, samples, avg); double t = t_val(n); u = std_dev * t; e = u/avg; //fprintf(stderr, "a=%f e=%f u=%f t=%f sd=%f\n", avg, e, u, t, std_dev); if (e < error) { success = true; break; } } thrarg->result.avg = avg; thrarg->result.samples = n; thrarg->result.iters = iters; thrarg->result.sum = sum; thrarg->result.sdev = std_dev; thrarg->result.u = u; thrarg->result.err = e; if (print_samples) { for (i = 0; i < n; i++) fprintf(stderr, "%f\n", samples[i]); } fprintf(stderr, "i = %d n = %zd sdev = %f u = %f e = %f a = %f\n", iters, n, std_dev, u, e, avg); //fprintf(stderr, "i = %d n = %zd sdev = %f u = %f e = %f a = %f me = %f s = %hhd\n", //iters, n, std_dev, u, e, avg, error, (char)success); return success; }
// ###################################################################### // normalize image by subtracting it with mean and dividing by stdev Image<float> GistEstimatorFFT::normalize(Image<float> img) { Image<float> tImg = img; double tMean = float(mean(img)); double tStdev = float(stdev(img)); tImg -= tMean; tImg /= tStdev; return tImg; }
void printStatsRGB(const Image<PixRGB<byte> >& img, const std::string& imgname, const std::string& imgtype) { this->openFile(); Image<byte> r, g, b; getComponents(img, r, g, b); const double mean_r = mean(r); const double stdev_r = stdev(r); const Range<byte> range_r = getRange(r); const double mean_g = mean(g); const double stdev_g = stdev(g); const Range<byte> range_g = getRange(g); const double mean_b = mean(b); const double stdev_b = stdev(b); const Range<byte> range_b = getRange(b); fprintf(this->file, "%06d " "R=[%d .. %f +/- %f .. %d] " "G=[%d .. %f +/- %f .. %d] " "B=[%d .. %f +/- %f .. %d] %% %s (%dx%d %s)\n", this->frameNumber, int(range_r.min()), mean_r, stdev_r, int(range_r.max()), int(range_g.min()), mean_g, stdev_g, int(range_g.max()), int(range_b.min()), mean_b, stdev_b, int(range_b.max()), imgname.c_str(), img.getWidth(), img.getHeight(), imgtype.c_str()); fflush(this->file); this->rangeR.merge(range_r); this->rangeG.merge(range_g); this->rangeB.merge(range_b); this->meanR += mean_r; this->meanG += mean_g; this->meanB += mean_b; ++(this->count); }
int main() { hwtimer_t tsct; uint64_t timetsc[MAX_RUNS], meanval; double sdval; int i; set_affinity(0); for (i = 0; i < MAX_RUNS; i++) { init_timer(&tsct); sleep(1); start_timer(&tsct); stop_timer(&tsct); timetsc[i] = get_timer_ns(&tsct); } meanval = mean(timetsc, MAX_RUNS); sdval = stdev(timetsc, MAX_RUNS); printf("Raw Precision: mean %lu stdev %f\n", meanval, sdval); //calculating accuracy for (i = 0; i < MAX_RUNS; i++) { init_timer(&tsct); start_timer(&tsct); sleep(1); stop_timer(&tsct); timetsc[i] = get_timer_ns(&tsct); if (timetsc[i] < 1000000000) timetsc[i] = 1000000000 - timetsc[i]; else timetsc[i] -= 1000000000; } meanval = mean(timetsc, MAX_RUNS); sdval = stdev(timetsc, MAX_RUNS); printf("Raw Error: mean %lu stdev %f\n", meanval, sdval); return 0; }
void _calcStats( vector<int> &data, vector<QString> &stats ) { if (data.empty()) { stats[0] = "Undefined"; stats[1] = "Undefined"; stats[2] = "Undefined"; stats[3] = "Undefined"; } else { stats[0] = QString::number( mean(data) ); stats[1] = QString::number( stdev(data) ); stats[2] = QString::number( min_element(data) ); stats[3] = QString::number( max_element(data) ); } if (data.size() == 1) { stats[1] = "Undefined"; } }
result_t RandomBattery::distribute(int trials){ const int n_bins = 32; double bins[n_bins]; for (int i = 0; i < n_bins; i++){ bins[i] = 0; } for (int i = 0; i < trials; i++){ int n = rng->rand<int>(n_bins); bins[n]++; } plot(bins, n_bins, (float)trials/n_bins); result_t result; stdev(result, bins, n_bins); return result; }
result_t RandomBattery::bitHistogram(int trials){ double places[32]; for (int i = 0; i < 32; i++){ places[i] = 0; } for (int i = 0; i < trials; i++){ uint32_t x = rng->rand32(); for (int b = 0; b < 32; b++){ places[b] += x & 1; x = x >> 1; } } plot(places, 32, trials/2); result_t result; stdev(result, places, 32); return result; }
result_t RandomBattery::byteHistogram(int trials){ double bytes[256]; for (int i = 0; i < 256; i++){ bytes[i] = 0; } for (int i = 0; i < trials; i++){ uint32_t x = rng->rand32(); bytes[x & 0x000000ff ]++; bytes[x & 0x0000ff00 >> 8]++; bytes[x & 0x00ff0000 >> 16]++; bytes[x & 0xff000000 >> 24]++; } plot(bytes, 256, 4*trials/256); result_t result; stdev(result, bytes, 256); return result; }
void init_params() { ExtendArray<float> vec(ntrees); ExtendArray<float> treelens(ntrees); // compute tree lengths and their mean float meantreelens = 0.0; for (int j=0; j<ntrees; j++) { treelens[j] = 0.0; for (int i=0; i<nspecies; i++) if (lengths[j][i] > 0) treelens[j] += lengths[j][i]; meantreelens += treelens[j]; } meantreelens /= ntrees; // compute variance of tree lengths float vartreelens = variance(treelens.get(), ntrees, meantreelens); // initialize gene rate parameter gene_nu = (1.0 / vartreelens) + 1.0; // initialize species rate parameters for (int i=0; i<nspecies; i++) { float sum = 0.0; for (int j=0; j<ntrees; j++) { if (lengths[j][i] > 0) { vec[j] = lengths[j][i] / (treelens[j] / meantreelens) / times[i]; sum += vec[j]; } } float mu = sum / ntrees; float sigma = stdev(vec.get(), ntrees); sp_alpha[i] = mu*mu / sigma / sigma; sp_beta[i] = mu / sigma / sigma; } }
std::vector<double> VectorDataSet::standardDeviation(const std::vector<int>& patterns) { int pIndex; std::vector<double> m = mean(patterns); std::vector<double> stdev(numFeatures, 0); // avg[n] = avg[n-1] - (avg[n-1] - xn) / n for (unsigned int i = 0; i < patterns.size(); i++){ pIndex = patterns[i]; for (int j = 0; j < X[pIndex].size(); j++) { double value = (X[pIndex][j] - m[j]) * (X[pIndex][j] - m[j]); stdev[j] = stdev[j] - (stdev[j] - value) / (i + 1); } } for (unsigned int j = 0; j < stdev.size(); j++){ stdev[j] = sqrt(stdev[j]); } return stdev; }
static void addMeasurement(Packet_t *packet) { xvector[vectorPos] = packet->accX; yvector[vectorPos] = packet->accY; zvector[vectorPos] = packet->accZ; vectorPos++; if (vectorPos < NUM_SAMPLES) return; vectorPos = 0; float s = stdev(xvector) + stdev(yvector) + stdev(zvector); // PRINTF("stdev = %u\n", (uint16_t) s); float accelVector[3] = { average(xvector) - groundVector[0], average(yvector) - groundVector[1], average(zvector) - groundVector[2] }; // PRINTF("accel=(%d %d %d)\n", (int)accelVector[0], (int)accelVector[1], (int)accelVector[2]); bool nowFwd, nowBack; float accLen = vlength(accelVector); if (accLen < ONE_G / 10) { nowBack = nowFwd = false; } else { normalizeQuick(accelVector, accLen); nowFwd = sameDirection(fwdVector, accelVector); nowBack = inverseDirection(fwdVector, accelVector); } int now = nowFwd ? 1 : (nowBack ? -1 : 0); bool diffFromPrevious; static bool notFirstTime; if (notFirstTime) { // float difference = (calcDiff(xvector, prevxvector) // + calcDiff(yvector, prevyvector) // + calcDiff(zvector, prevzvector)) / 3.0; float difference = calcDiff(xvector, prevavgxvector) + calcDiff(yvector, prevavgyvector) + calcDiff(zvector, prevavgzvector); // PRINTF("difference=%d\n", (int)difference); diffFromPrevious = (difference >= 30 * (ONE_G / 10)); } else { notFirstTime = true; diffFromPrevious = false; } bool pastBack = false; bool pastFwd = false; if (past[0] + past[1] + past[2] >= 1) { pastFwd = true; } else if (past[0] + past[1] + past[2] <= -1) { pastBack = true; } past[0] = past[1]; past[1] = past[2]; past[2] = now; bool wasMoving = !bayesStandingMode; #if 0 PRINTF("SSD \t%d\n", (int)(s >= THRESH_LOW)); PRINTF("MSD \t%d\n", (int)(s >= THRESH_MEDIUM)); PRINTF("LSD \t%d\n" , (int)(s > THRESH_HIGH)); PRINTF("Accelerating\t%d\n", (int)(nowFwd)); PRINTF("PastAccel \t%d\n", (int)(pastFwd)); PRINTF("PastBrake \t%d\n", (int)(pastBack)); PRINTF("WasMoving \t%d\n", (int)(wasMoving)); PRINTF("Diff \t%d\n", (int)(diffFromPrevious)); #endif calcNewProb(s, wasMoving, pastBack, pastFwd, nowFwd, diffFromPrevious); }
double DiffusionProcess::Generate1(double t0, double x0, double dt) const { std::tr1::normal_distribution<double> dist(expectation(t0, x0, dt),stdev(t0, x0, dt)); return dist(*m_eng); }
// ###################################################################### void SimulationViewerStats::save1(const ModelComponentSaveInfo& sinfo) { // Use a LINFO here since we are already slowed down by writing // stats, we should at least have a short debug on this fact LINFO("SAVING STATS TO %s",itsStatsFname.getVal().c_str()); // Lock the file. We put this here to support multi-process in the // future. However, this will not work on NFS mounts. struct flock fl; int fd; lockFile(itsStatsFname.getVal().c_str(),fd,fl); // Since this is more or less debug info we open and flush every iteration. // rather than once each run. std::ofstream statsFile; statsFile.open(itsStatsFname.getVal().c_str(),std::ios_base::app); // get the OFS to save to, assuming sinfo is of type // SimModuleSaveInfo (will throw a fatal exception otherwise): nub::ref<FrameOstream> ofs = dynamic_cast<const SimModuleSaveInfo&>(sinfo).ofs; // also get the SimEventQueue: SimEventQueue *q = dynamic_cast<const SimModuleSaveInfo&>(sinfo).q; // initialize our stats dump file if desired: if(itsFrameIdx == 0) { rutz::shared_ptr<SimReqVCXmaps> vcxm(new SimReqVCXmaps(this)); q->request(vcxm); // VisualCortex is now filling-in the maps... if (itsStatsFname.getVal().size()) { itsStatsFile = new std::ofstream(itsStatsFname.getVal().c_str()); if (itsStatsFile->is_open() == false) LFATAL("Failed to open '%s' for writing.", itsStatsFname.getVal().c_str()); // dump the settings of the model: getRootObject()->printout(*itsStatsFile, "# "); // list all our channels: //LFATAL("FIXME"); // also get the SimEventQueue: rutz::shared_ptr<ChannelMaps> chm = vcxm->channelmaps(); uint numSubmaps = chm->numSubmaps(); *itsStatsFile << "# CHANNELS: "; for(uint i = 0; i < numSubmaps; i++) { NamedImage<float> smap = chm->getRawCSmap(i); *itsStatsFile << smap.name() << ", "; } *itsStatsFile << std::endl; } } // We flush frequently since this output is debuggy in nature or it's being used // to collect information which needs assurance of accuracy for instance in // RSVP analysis. It is better to err on the side of caution. (*itsStatsFile).flush(); (*itsStatsFile).close(); // get the basic input frame info if (SeC<SimEventInputFrame> e = q->check<SimEventInputFrame>(this)) { itsSizeX = e->frame().getWidth(); itsSizeY = e->frame().getHeight(); itsFrameNumber = (unsigned int)e->frameNum(); itsFrameIdx = itsFrameNumber; } else { itsFrameNumber = itsFrameIdx; itsFrameIdx++; } // get the latest input frame: // Since we are only using it's basic statistics (Height / Width) , we don't care about it's // blackboard status. Use SEQ_ANY then. Otherwise, this will not fetch at any rate. Image< PixRGB<byte> > input; if (SeC<SimEventRetinaImage> e = q->check<SimEventRetinaImage>(this,SEQ_ANY)) input = e->frame().colorByte(); else LINFO("No input? Check the SimEventCue."); // Get the current frame number or keep track on your own /* if (SeC<SimEventInputFrame> e = q->check<SimEventInputFrame>(this)) itsFrameIdx = e->frameNum(); else itsFrameIdx++; */ // get the latest raw AGM: Image<float> agm; if (SeC<SimEventAttentionGuidanceMapOutput> e = q->check<SimEventAttentionGuidanceMapOutput>(this)) agm = e->agm(1.0F); else LINFO("No AGM? Check the SimEventCue."); // if we are missing input or agm, return: // We also need to warn so that we know why the stats file may be empty bool quit = false; if (input.initialized() == false) { LINFO("WARNING!!! Input seems not to be initialized, so detailed stats cannot be saved."); quit = true; } if(agm.initialized() == false) { LINFO("WARNING!!! NO Attention Guidance MAP \"AGM\" so detailed stats cannot be saved."); quit = true; } if(quit == true) return; // update the trajectory: Image< PixRGB<byte> > res; const int w = input.getWidth(); // save image results? if so let's prepare it if (itsSaveXcombo.getVal() || itsSaveYcombo.getVal()) { Image<float> nagm = getMap(*q); res = colGreyCombo(input, nagm, itsSaveXcombo.getVal(), itsDisplayInterp.getVal()); } // if we are saving single channel stats save saliency stats using a compatable format // SEE: SingleChannel.C / saveStats(..) for more info on format if (itsGetSingleChannelStats.getVal()) saveCompat(agm); // Save a bunch of stats? if (statsFile) { // start with the current simulation time: statsFile <<std::endl<<"= "<<q->now().msecs() <<" # time of frame in ms"<<std::endl; // get min/max/avg and stdev and number of peaks in AGM: float mi, ma, av; getMinMaxAvg(agm, mi, ma, av); double sdev = stdev(agm); double peaksum; int npeaks = findPeaks(agm, 0.0f, 255.0f, peaksum); // find the location of max in the AGM, at scale of original input: float maxval; Point2D<int> maxloc; findMax(agm, maxloc, maxval); float scale = float(w) / float(agm.getWidth()); maxloc.i = int(maxloc.i * scale + 0.4999F); maxloc.j = int(maxloc.j * scale + 0.4999F); if (res.initialized()) { drawPatch(res, maxloc, 4, COL_YELLOW); drawPatch(res, maxloc + Point2D<int>(w, 0), 4, COL_YELLOW); } // find the location of min in the AGM, at scale of original input: float minval; Point2D<int> minloc; findMin(agm, minloc, minval); minloc.i = int(minloc.i * scale + 0.4999F); minloc.j = int(minloc.j * scale + 0.4999F); if (res.initialized()) { drawPatch(res, minloc, 4, COL_GREEN); drawPatch(res, minloc + Point2D<int>(w, 0), 4, COL_GREEN); } // save some stats for that location: statsFile <<maxloc.i<<' '<<maxloc.j<<' '<<minloc.i<<' ' <<minloc.j<<' '<<ma<<' '<<mi<<' '<<av<<' '<<sdev <<' '<<npeaks<<' '<<peaksum <<" # Xmax Ymax Xmin Ymin max min avg std npeaks peaksum" <<std::endl; // build a vector of points where we will save samples. First is // the max, second the min, then a bunch of random locations: std::vector<Point2D<int> > loc; loc.push_back(maxloc); loc.push_back(minloc); for (uint n = 0; n < 100; n ++) loc.push_back(Point2D<int>(randomUpToNotIncluding(input.getWidth()), randomUpToNotIncluding(input.getHeight()))); // Get all the conspicuity maps: ImageSet<float> cmap; //LFATAL("FIXME"); rutz::shared_ptr<SimReqVCXmaps> vcxm(new SimReqVCXmaps(this)); q->request(vcxm); // VisualCortex is now filling-in the maps... rutz::shared_ptr<ChannelMaps> chm = vcxm->channelmaps(); uint numSubmaps = chm->numSubmaps(); for(uint i=0;i < numSubmaps; i++) { NamedImage<float> tempMap = chm->getRawCSmap(i); Image<float> m = tempMap; cmap.push_back(m); // also store sample points at the min/max locations: Point2D<int> p; float v; findMax(m, p, v); loc.push_back(p); findMin(m, p, v); loc.push_back(p); } /* for (uint i = 0; i < itsBrain->getVC()->numChans(); i ++) { Image<float> m = itsBrain->getVC()->subChan(i)->getOutput(); cmap.push_back(m); // also store sample points at the min/max locations: Point2D<int> p; float v; findMax(m, p, v); loc.push_back(p); findMin(m, p, v); loc.push_back(p); } */ // Go over all sample points and save feature map and // conspicuity map values at those locations: for (uint i = 0; i < loc.size(); i ++) { Point2D<int> p = loc[i]; Point2D<int> pp(int(p.i / scale), int(p.j / scale)); statsFile <<p.i<<' '<<p.j<<" "; // do the conspicuity maps first. Since they are all at the // scale of the AGM, we use pp: for (uint j = 0; j < cmap.size(); j ++) { if((int(p.i / scale) < agm.getWidth()) && (int(p.j / scale) < agm.getHeight())) { (statsFile)<<cmap[j].getVal(pp)<<' '; (statsFile)<<" "; } else { (statsFile)<<"-1"<<' '; (statsFile)<<" "; } } // now the feature maps, we use coordinates p: /* TOO BOGUS - disabled for now std::vector<double> f; itsBrain->getVC()->getFeatures(p, f); for (uint j = 0; j < f.size(); j ++) (*statsFile)<<f[j]<<' '; */ statsFile <<"# features "<<i<<" at ("<<p.i <<", "<<p.j<<')'<<std::endl; } } statsFile.flush(); statsFile.close(); unlockFile(itsStatsFname.getVal().c_str(),fd,fl); // save results? if (res.initialized()) ofs->writeRGB(res, "T", FrameInfo("SimulationViewerStats trajectory", SRC_POS)); // Should we compute attention gate stats // If we have AG stats we will save the basic LAM stats anyways if(itsComputeAGStats.getVal()) computeAGStats(*q); else if (SeC<SimEventAttentionGateOutput> ag = q->check<SimEventAttentionGateOutput>(this)) computeLAMStats(ag->lam()); //! Save the overlap image if(itsOverlap.initialized()) ofs->writeRGB(itsOverlap, "AG-STAT-MASK", FrameInfo("Stats mask overlap", SRC_POS)); if(itsVisualSegments.initialized()) ofs->writeRGB(itsVisualSegments, "AG-STAT-SEGS", FrameInfo("Stats segments", SRC_POS)); if(itsVisualCandidates.initialized()) ofs->writeGray(itsVisualCandidates, "AG-STAT-CAND", FrameInfo("Stats candidates", SRC_POS)); }
int IEVChannelSumGadget::process(GadgetContainerMessage< ISMRMRD::ImageHeader>* m1) { GadgetContainerMessage<hoNDArray< float > > *unfiltered_unwrapped_msg_ptr = AsContainerMessage<hoNDArray<float>>(m1->cont()); GadgetContainerMessage<hoNDArray< float > > *filtered_unwrapped_msg_ptr = AsContainerMessage<hoNDArray<float>>(unfiltered_unwrapped_msg_ptr->cont()); GadgetContainerMessage<ISMRMRD::MetaContainer> *meta; static int c=0; int e; int echo = m1->getObjectPtr()->contrast; float inv_echo_time; int echo_offset=yres*xres*num_ch*echo; if(!filtered_unwrapped_msg_ptr || !unfiltered_unwrapped_msg_ptr) { GERROR("Wrong types received in IEVChannelSumGadget. Filtered and unfiltered phase expected.\n"); return GADGET_FAIL; } meta = AsContainerMessage<ISMRMRD::MetaContainer>(filtered_unwrapped_msg_ptr->cont()); //float* filtered_phase_ptr= filtered_unwrapped_msg_ptr->getObjectPtr()->get_data_ptr(); m1->getObjectPtr()->channels=1; //yes? inv_echo_time=1/echoTimes[echo];//to avoid millions of divisions per slice memcpy(filtered_phase_ptr+echo_offset, filtered_unwrapped_msg_ptr->getObjectPtr()->get_data_ptr(), xres*yres*num_ch*sizeof(float)); for (int i = 0; i < xres*yres*num_ch; i++) freq_ptr[echo_offset+i] = filtered_phase_ptr[echo_offset+i]*inv_echo_time; hdr_ptr[echo]=*(m1->getObjectPtr()); if(meta) { meta->getObjectPtr()->append("StudyInstanceUID", studyInstanceUID.c_str());//may be in xml header, may not be, in that case put it in xml so it can get to dicom char TE[10]; sprintf(TE, "%f", echoTimes[echo]*1000); meta->getObjectPtr()->append("TE", TE); attributes[echo]=*(meta->getObjectPtr()); } unfiltered_unwrapped_msg_ptr->release();//all data have been copied if(echo==(numEchos-1)) { float* weights= new float[xres*yres*num_ch]; float** channel_weights= new float* [num_ch]; float* to_normalize = new float[xres*yres]; int ch; if(iev.value()==int(IEV::YES))//just to allow this to work without IEV/make it very easy to compare { #pragma omp parallel //expanded parallel --- to allow sample to be allocated once { float* sample = new float[numEchos]; int start = omp_get_thread_num()/omp_get_num_threads()*xres*yres*num_ch; int end = (omp_get_thread_num()+1)/omp_get_num_threads()*xres*yres*num_ch; for(int i =start; i <end; i++) { ///////// for(int j = 0; j < numEchos; j++) sample[j]=freq_ptr[i+j*xres*yres*num_ch]; //assuming all(6-10 at at time) pages can be held in memory, this isn't terrible weights[i]=stdev(sample, numEchos); //find standard deviation between echoes ///// } delete[] sample; } #pragma omp parallel for private(ch) for(ch = 0; ch < num_ch; ch++) { float* temp_array; channel_weights[ch]=&weights[ch*xres*yres]; medianFilter(channel_weights[ch], xres, yres); for (int i = 0; i < xres*yres; i++) { channel_weights[ch][i]=1/(channel_weights[ch][i]+FLT_MIN); //weight as inverse, } } for (int i = 0; i < xres*yres; i++) to_normalize[i] = 0; for(int ch=0; ch< num_ch; ch++) for (int i = 0; i < xres*yres; i++) { to_normalize[i]+=channel_weights[ch][i]; } for(int ch=0; ch< num_ch; ch++) for (int i = 0; i < xres*yres; i++) { channel_weights[ch][i]/=to_normalize[i]; //normalize weights } } else { #pragma omp parallel for private(ch) for(int ch=0; ch< num_ch; ch++) { channel_weights[ch]=&weights[ch*xres*yres]; for (int i = 0; i < xres*yres; i++) { channel_weights[ch][i]=1; } } } for(e=0; e<numEchos; e++) { hdr_ptr[e].channels=1; hdr_ptr[e].contrast=e; hdr_ptr[e].data_type = ISMRMRD::ISMRMRD_FLOAT;//GADGET_IMAGE_REAL_FLOAT; hdr_ptr[e].image_type = ISMRMRD::ISMRMRD_IMTYPE_PHASE;//There is no frequency image type hdr_ptr[e].slice= (hdr_ptr[e].image_index) % num_slices;//was hdr_ptr[e].image_index___-1_____) % num_slices before decrementor was added upstream if(output_phase.value()) { // GadgetContainerMessage<ISMRMRD::ImageHeader>* phase_hdr = new GadgetContainerMessage<ISMRMRD::ImageHeader>(hdr_ptr[e]); //*(phase_hdr->getObjectPtr()) =*(hdr_ptr[e]->getObjectPtr()); GadgetContainerMessage<hoNDArray< float > > *comb_phase_msg = new GadgetContainerMessage<hoNDArray< float > >(); phase_hdr->getObjectPtr()->image_series_index=series_id_offset+1; try{comb_phase_msg->getObjectPtr()->create(xres,yres);} catch (std::runtime_error &err){ GEXCEPTION(err,"Unable to create output image\n"); return GADGET_FAIL; } float* output_ptr=comb_phase_msg->getObjectPtr()->get_data_ptr(); phase_hdr->cont(comb_phase_msg); // for (int i = 0; i < xres*yres; i++) { output_ptr[i]=filtered_phase_ptr[e*xres*yres*num_ch+i]*channel_weights[0][i]; //instead of setting to 0 and adding first channel } for(int ch=1; ch< num_ch; ch++) for (int i = 0; i < xres*yres; i++) { output_ptr[i]+=filtered_phase_ptr[e*xres*yres*num_ch+xres*yres*ch+i]*channel_weights[ch][i];; //instead of setting to 0 and adding first channel } // if(meta) { GadgetContainerMessage<ISMRMRD::MetaContainer>* meta = new GadgetContainerMessage<ISMRMRD::MetaContainer>(attributes[e]); comb_phase_msg->cont(meta); } // if (this->next()->putq(phase_hdr) == -1) { m1->release(); GERROR("Unable to put collapsed images on next gadget's queue\n"); return GADGET_FAIL; } } if(output_LFS.value()) { // GadgetContainerMessage<ISMRMRD::ImageHeader>* freq_hdr=new GadgetContainerMessage<ISMRMRD::ImageHeader>(hdr_ptr[e]); //*(freq_hdr->getObjectPtr()) =*(hdr_ptr[e]->getObjectPtr()); GadgetContainerMessage<hoNDArray< float > > *comb_freq_msg = new GadgetContainerMessage<hoNDArray< float > >(); freq_hdr->getObjectPtr()->image_series_index=series_id_offset+output_phase.value()+1; try{comb_freq_msg->getObjectPtr()->create(xres,yres);} catch (std::runtime_error &err){ GEXCEPTION(err,"Unable to create output image\n"); return GADGET_FAIL; } float* output_ptr=comb_freq_msg->getObjectPtr()->get_data_ptr(); freq_hdr->cont(comb_freq_msg); freq_hdr->getObjectPtr()->image_type = 6; // for (int i = 0; i < xres*yres; i++) output_ptr[i]=freq_ptr[e*xres*yres*num_ch+i]*channel_weights[0][i]; //instead of setting to 0 and adding first channel for(int ch=1; ch< num_ch; ch++) for (int i = 0; i < xres*yres; i++) { output_ptr[i]+=freq_ptr[e*xres*yres*num_ch+xres*yres*ch+i]*channel_weights[ch][i]; } // if(meta) { GadgetContainerMessage<ISMRMRD::MetaContainer>* meta = new GadgetContainerMessage<ISMRMRD::MetaContainer>(attributes[e]); meta->getObjectPtr()->set(GADGETRON_DATA_ROLE, GADGETRON_IMAGE_FREQMAP); //*(meta->getObjectPtr())=*(attributes[e]->getObjectPtr()); comb_freq_msg->cont(meta); } // if (this->next()->putq(freq_hdr) == -1) { //m1->release(); GERROR("Unable to put collapsed images on next gadget's queue\n"); return GADGET_FAIL; } } } delete[] to_normalize; delete[] weights; delete[] channel_weights; //if(output.value()==int(OUTPUT::PHASE)) // delete[] unfiltered_phase_ptr; } return GADGET_OK; }
int main (int argc, char *argv[]) { int i; int result; int value, option; int b=0; DATA a; /*** * Checking for error in command line entry * * **/ if (argc < 1) { fprintf(stderr, "usage: %s host message\n", argv[0]); exit(1); } /*** * Prompt user to enter values **/ printf("**************\nEnter number of desired command from options below, or '0' to quit:\n 1. Search(A,N) \n 2. Zero(A,N) \n 3. Sum(A) \n 4. Median(A) \n 5. Average(A) \n 6. STDEV(A) \n*************\n"); /*** ** Check to see if number entered is 1-6 **/ scanf("%i",&option); if (option!=1&&option!=2&&option!=3&&option!=4&&option!=5&&option!=6&&option!=0) { b=1; } /*** ** Check for validity of option choosen **/ while(b>0) { if (option == 0){exit(1);} //handle quit command. printf("Please enter a valid number, between 1-6 inclusive, to select desired command from options below.\n"); printf("**************\nEnter number of desired command,or '0' to quit:\n 1. Search(A,N) \n 2. Zero(A,N) \n 3. Sum(A) \n 4. Median(A) \n 5. Average(A) \n 6. STDEV(A) \n*************\n"); scanf("%i",&option); if (option!=1&&option!=2&&option!=3&&option!=4&&option!=5&&option!=6&&option!=0) { b=1; } if (option==1||option==2||option==3||option==4||option==5||option==6||option == 0) { b=0; break; } } switch (option) { case 0: exit(1); break; case 1: printf("Enter values for A array, must be 11 numbers:"); for(i=0;i<max_array;++i) { scanf("%i",&a.array[i]); } printf("Enter N value:"); scanf("%i",&value); search(a,value); break; case 2: printf("Enter values for A array, must be 11 numbers:"); for(i=0;i<max_array;++i) { scanf("%i",&a.array[i]); } printf("Enter N value:"); scanf("%i",&value); zero(a, value); break; case 3: printf("Enter values for A array, must be 11 numbers:"); for(i=0;i<max_array;++i) { scanf("%i",&a.array[i]); } sum(a); break; case 4: printf("Enter values for A array, must be 11 numbers:"); for(i=0;i<max_array;++i) { scanf("%i",&a.array[i]); } median(a); break; case 5: printf("Enter values for A array, must be 11 numbers:"); for(i=0;i<max_array;++i) { scanf("%i",&a.array[i]); } average(a); break; case 6: printf("Enter values for A array, must be 10 numbers:"); for(i=0;i<max_array-1;++i) { scanf("%i",&a.arr[i]); } stdev(a); break; } }
int main(int argc, char **argv) { // counter uint64_t i; // arg placeholder for getopt int c; // timing variables clock_t t1, t2; // simulation input krapivsky_input_t *input; // model data structure krapivsky_model_t *km; // model parameters double p = 0.2; double lambda = 3.5; double mu = 1.8; // number of nodes to simulate uint64_t target_n_nodes = 1000; // number of simulations to run uint64_t n_runs = 1; // base output dir char base_dir_name[BUFSIZE] = "."; // edge output filename char edge_file_name[BUFSIZE] = "edges.csv"; char edge_file_fullname[BUFSIZE]; // write out the network's edges? int write_edges = 0; // degree output filename char degree_file_name[BUFSIZE] = "degree.csv"; char degree_file_fullname[BUFSIZE]; char run_degree_file_fullname[BUFSIZE]; // write out the network's edges? int write_degree = 0; // output filename for each run, constructed automatically char run_file_fullname[BUFSIZE]; // output filename for timing data char time_file_name[BUFSIZE] = "times.csv"; char time_file_fullname[BUFSIZE]; FILE *time_file; // random seed filename char random_seed_file_name[BUFSIZE] = "randomseed.csv"; // write out timing information? int write_time = 0; // array of execution times double *times; // the type of data structure to use for node indexing char type[BUFSIZE] = "heap"; // parse command line args while((c = getopt(argc, argv, "d:s:e:t:p:m:l:n:o:r:u:")) != -1){ switch(c) { case 'd': write_degree = 1; strcpy(degree_file_name, optarg); break; case 's': strcpy(random_seed_file_name, optarg); break; case 'e': strcpy(edge_file_name, optarg); write_edges = 1; break; case 't': strcpy(type, optarg); break; case 'p': sscanf(optarg, "%lf", &p); break; case 'm': sscanf(optarg, "%lf", &mu); break; case 'l': sscanf(optarg, "%lf", &lambda); break; case 'n': sscanf(optarg, "%llu", &target_n_nodes); break; case 'o': strcpy(base_dir_name,optarg); break; case 'r': sscanf(optarg, "%llu", &n_runs); break; case 'u': write_time = 1; strcpy(time_file_name,optarg); break; } } // prepend basedir to filenames sprintf(edge_file_fullname, "%s/%s", base_dir_name, edge_file_name); sprintf(time_file_fullname, "%s/%s", base_dir_name, time_file_name); sprintf(degree_file_fullname, "%s/%s", base_dir_name, degree_file_name); // initialize pseudorandom number generator rand_init(base_dir_name, random_seed_file_name); // open the time file if we'll be using it if(write_time) { times = (double*) malloc(n_runs * sizeof(*times)); time_file = fopen(time_file_fullname,"w"); } // construct the simulation input input = krapivsky_make_input(p, lambda, mu, target_n_nodes); // run the simulations for(i=0;i<n_runs;i++) { t1 = clock(); if(strcmp(type,"lnu") == 0) { km = krapivsky_bstreap_simulate_lnu(input); } else if(strcmp(type,"lnn") == 0) { km = krapivsky_bstreap_simulate_lnn(input); } else if(strcmp(type,"lsu") == 0) { km = krapivsky_bstreap_simulate_lsu(input); } else if(strcmp(type,"lsn") == 0) { km = krapivsky_bstreap_simulate_lsn(input); } else if(strcmp(type,"heap") == 0) { km = krapivsky_heap_simulate(input); } else if(strcmp(type,"lnupareto") == 0) { km = krapivsky_bstreap_simulate_pareto_lnu(input); } else if(strcmp(type,"lnnpareto") == 0) { km = krapivsky_bstreap_simulate_pareto_lnn(input); } else if(strcmp(type,"lsupareto") == 0) { km = krapivsky_bstreap_simulate_pareto_lsu(input); } else if(strcmp(type,"lsnpareto") == 0) { km = krapivsky_bstreap_simulate_pareto_lsn(input); } else if(strcmp(type,"heappareto") == 0) { km = krapivsky_heap_simulate_pareto(input); } else if(strcmp(type,"heapnormal") == 0) { km = krapivsky_heap_simulate_normal(input); } else if(strcmp(type,"heapquadratic") == 0) { km = krapivsky_heap_simulate_quadratic(input); } else if(strcmp(type,"heapalpha100") == 0) { km = krapivsky_heap_simulate_alpha_100(input); } else if(strcmp(type,"heapalpha101") == 0) { km = krapivsky_heap_simulate_alpha_101(input); } else if(strcmp(type,"heapalpha102") == 0) { km = krapivsky_heap_simulate_alpha_102(input); } else if(strcmp(type,"heapalpha103") == 0) { km = krapivsky_heap_simulate_alpha_103(input); } else if(strcmp(type,"heapalpha104") == 0) { km = krapivsky_heap_simulate_alpha_104(input); } else if(strcmp(type,"heapalpha105") == 0) { km = krapivsky_heap_simulate_alpha_105(input); } else if(strcmp(type,"heapalpha106") == 0) { km = krapivsky_heap_simulate_alpha_106(input); } else if(strcmp(type,"heapalpha107") == 0) { km = krapivsky_heap_simulate_alpha_107(input); } else if(strcmp(type,"heapalpha108") == 0) { km = krapivsky_heap_simulate_alpha_108(input); } else if(strcmp(type,"heapalpha109") == 0) { km = krapivsky_heap_simulate_alpha_109(input); } else if(strcmp(type,"heapalpha110") == 0) { km = krapivsky_heap_simulate_alpha_110(input); } else if(strcmp(type,"heapalpha115") == 0) { km = krapivsky_heap_simulate_alpha_115(input); } else if(strcmp(type,"heapalpha120") == 0) { km = krapivsky_heap_simulate_alpha_120(input); } else if(strcmp(type,"heapalpha130") == 0) { km = krapivsky_heap_simulate_alpha_130(input); } else if(strcmp(type,"heapalpha140") == 0) { km = krapivsky_heap_simulate_alpha_140(input); } else if(strcmp(type,"heapalpha150") == 0) { km = krapivsky_heap_simulate_alpha_150(input); } else if(strcmp(type,"heapalpha160") == 0) { km = krapivsky_heap_simulate_alpha_160(input); } else if(strcmp(type,"heapalpha170") == 0) { km = krapivsky_heap_simulate_alpha_170(input); } else if(strcmp(type,"heapalpha180") == 0) { km = krapivsky_heap_simulate_alpha_180(input); } else if(strcmp(type,"heapalpha190") == 0) { km = krapivsky_heap_simulate_alpha_190(input); } else if(strcmp(type,"heapalpha200") == 0) { km = krapivsky_heap_simulate_alpha_200(input); } else { printf("Unknown type: %s\n",type); return 1; } t2 = clock(); if(write_edges) { sprintf(run_file_fullname, "%s_%llu", edge_file_fullname, i); krapivsky_write_edges(km,run_file_fullname); } if(write_time) { times[i] = ((double) (t2 - t1))/CLOCKS_PER_SEC; //time in seconds fprintf(time_file, "%lf\n", times[i]); } if(write_degree) { sprintf(run_degree_file_fullname, "%s_%llu", degree_file_fullname, i); krapivsky_write_degrees(km,run_degree_file_fullname); } krapivsky_free(km); } free(input); // output and clean up timing data if(write_time) { printf("Simulated %llu networks with %llu nodes each.\nEach network took %lf +/- %lf seconds to simulate.\n", n_runs, target_n_nodes, mean(times,n_runs), stdev(times,n_runs)); free(times); fclose(time_file); } return 0; }
void makeMuonTimingPlot (std::string fname) { TFile file(fname.c_str()); TDirectoryFile *dir = (TDirectoryFile*)file.Get("Muons"); TList *hlist = dir->GetListOfKeys(); TH1F *h1; bool foundHist = false; for (auto hist : *hlist) { TString name = hist->GetName(); if (!name.Contains("hMuTimeP")) continue; TH1F *h0 = (TH1F*)dir->Get(name.Data()); if (!foundHist) { h1 = (TH1F*)h0->Clone(); h1->Sumw2(); foundHist = true; } else { h1->Add(h0); } } TH1F *h2 = new TH1F("h2", "h2", 200, -100, 100); h2->GetXaxis()->SetTitle("muon time (ns)"); h2->GetYaxis()->SetTitle("Fraction of Muons/ns"); h2->GetYaxis()->SetTitleOffset(1.1); h2->GetXaxis()->SetTitleOffset(0.8); h2->SetTitle("CSC Muon Time"); h2->SetTitleFont(42); h2->SetTitleSize(0.052); h2->Sumw2(); h2->SetLineColor(kCyan+3); h2->SetFillColor(kCyan+3); for (int ibin = 1; ibin <= 200; ibin++) { h2->SetBinContent(ibin, h1->GetBinContent(ibin)); h2->SetBinError(ibin, h1->GetBinError(ibin)); if (ibin == 200) h2->SetEntries(h1->GetEntries()); } TCanvas c1("c1", "c1", 600, 400); gStyle->SetOptStat(""); double rms = h2->GetRMS(); double avg = h2->GetMean(); TLatex cms(0.17, 0.83, "CMS"); cms.SetNDC(); cms.SetTextFont(61); cms.SetTextSize(0.06); TLatex prelim(0.17, 0.81, "Preliminary"); prelim.SetNDC(); prelim.SetTextAlign(13); prelim.SetTextFont(52); prelim.SetTextSize(0.0456); TLatex data(0.17, 0.76, "Data 2016"); data.SetNDC(); data.SetTextAlign(13); data.SetTextFont(52); data.SetTextSize(0.0456); TLatex lumi(0.9, 0.93, "4.0 fb^{-1} (13 TeV)"); lumi.SetNDC(); lumi.SetTextAlign(31); lumi.SetTextFont(42); lumi.SetTextSize(0.052); TLatex mean(0.7, 0.81, Form("Mean %2.1f", avg)); mean.SetNDC(); mean.SetTextAlign(11); mean.SetTextFont(61); mean.SetTextSize(0.06); TLatex stdev(0.7, 0.76, Form("RMS %2.1f", rms)); stdev.SetNDC(); stdev.SetTextAlign(11); stdev.SetTextFont(61); stdev.SetTextSize(0.06); h2->GetXaxis()->SetRangeUser(-6*rms,6*rms); TH1F* h2norm = (TH1F*)h2->DrawNormalized("hist"); gPad->Update(); cms.Draw(); prelim.Draw(); data.Draw(); lumi.Draw(); mean.Draw(); stdev.Draw(); TPaveText *title = (TPaveText*)gPad->GetPrimitive("title"); title->SetBorderSize(0); title->SetFillColor(0); title->SetFillStyle(0); title->SetX1NDC(0.13); title->SetY1NDC(0.88); title->SetX2NDC(0.9); title->SetY2NDC(0.98); title->SetTextFont(42); title->SetTextSize(0.052); title->SetTextAlign(11); c1.Print("plots/muon_time_all.pdf"); c1.Print("plots/muon_time_all.png"); c1.Print("plots/muon_time_all.root"); }
bool isosplit1d(int N,int *labels,double &pp,double *X) { QTime timer; timer.start(); int minsize=4; //hard coded to be consistent with calibration //qDebug() << "ELAPSED" << __FILE__ << __LINE__ << timer.elapsed(); timer.start(); QVector<double> avg(N),stdev(N),scores(N); if (!read_isosplit_calibration(N,avg,stdev,scores)) { printf ("ERROR: problem in read_isosplit_calibration.\n"); return false; } int curve_len=avg.count(); if (curve_len==0) { printf ("ERROR: calibration not performed for N=%d\n",N); return false; } int num_trials=scores.count(); //qDebug() << "ELAPSED" << __FILE__ << __LINE__ << timer.elapsed(); timer.start(); //compute the curve and cutpoint Mda curve_mda; curve_mda.allocate(1,curve_len); double *curve=curve_mda.dataPtr(); double cutpoint; //qDebug() << "ELAPSED" << __FILE__ << __LINE__ << timer.elapsed(); timer.start(); if (!get_isosplit_curve(curve_len,N,curve,cutpoint,X,minsize)) { printf ("ERROR in get_isosplit_curve\n"); return false; } //qDebug() << "ELAPSED" << __FILE__ << __LINE__ << timer.elapsed(); timer.start(); //compute the score by comparing to calibration avg and stdev and then compute the pp double score0=0; for (int ii=0; ii<curve_len; ii++) { if (stdev[ii]) { double diff0=avg[ii]-curve[ii]; //we need a change of at least 0.1 to count it //this is important in the case where stdev is extremely close to 0 if (qAbs(diff0)<0.1) diff0=0; double tmp_score=diff0/stdev[ii]; if (tmp_score>score0) score0=tmp_score; } } if (score0!=0) { double tmp_ct=0; for (int jj=0; jj<scores.count(); jj++) { if (scores[jj]<score0) tmp_ct++; } pp=tmp_ct/num_trials; } else { pp=0; } //qDebug() << "ELAPSED" << __FILE__ << __LINE__ << timer.elapsed(); timer.start(); //set the labels for (int kk=0; kk<N; kk++) { if (X[kk]<cutpoint) labels[kk]=1; else labels[kk]=2; } //qDebug() << "ELAPSED" << __FILE__ << __LINE__ << timer.elapsed(); timer.start(); return true; }
double corcoe(MAT &matX,MAT &matY) { return cov(matX,matY)/(stdev(matX)*stdev(matY)); }
double sderr( double* x, int len){ return stdev(x,len) / sqrt(len); }