vector<Cross> createCrossingList() { vector<Cross> crosslist; ParticleGrid<Particle>::GridArray *g = pcontainer.getVoxelGridArray(); for (int i = 0;i < g->gridsize;i++) { if (g->occnt[i] > 0) { for (int k = 0; k < g->occnt[i]; k++) { Particle *p1 = g->grid[g->csize*i+ k]; for (int j = k+1; j < g->occnt[i]; j++) { Particle *p2 = g->grid[g->csize*i+ j]; Cross x; x.i = p1->label; x.j = p2->label; x.wi = p1->w; x.wj = p2->w; crosslist.push_back(x); } } } } return crosslist; }
// write out to MATLAB void writeout(REAL *npoints) { for (int k = 0; k < pcontainer.pcnt; k++) { Particle *p = &(pcontainer.particles[k]); npoints[attrcnt*k] = p->R.x; npoints[attrcnt*k+1] = p->R.y; npoints[attrcnt*k+2] = p->R.z; npoints[attrcnt*k+3] = p->N.x; npoints[attrcnt*k+4] = p->N.y; npoints[attrcnt*k+5] = p->N.z; npoints[attrcnt*k+6] = p->label; npoints[attrcnt*k+7] = p->len; npoints[attrcnt*k+8] = pcontainer.ID_2_index(p->mID); npoints[attrcnt*k+9] = pcontainer.ID_2_index(p->pID); npoints[attrcnt*k+10] = p->Di; npoints[attrcnt*k+11] = p->Da; npoints[attrcnt*k+12] = p->Dp; npoints[attrcnt*k+13] = p->w; npoints[attrcnt*k+14] = p->q; } }
void WriteOutParticles(float *npoints) { for (int k = 0; k < m_ParticleGrid.pcnt; k++) { Particle *p = &(m_ParticleGrid.particles[k]); npoints[m_NumAttributes*k] = p->R.GetX(); npoints[m_NumAttributes*k+1] = p->R.GetY(); npoints[m_NumAttributes*k+2] = p->R.GetZ(); npoints[m_NumAttributes*k+3] = p->N.GetX(); npoints[m_NumAttributes*k+4] = p->N.GetY(); npoints[m_NumAttributes*k+5] = p->N.GetZ(); npoints[m_NumAttributes*k+6] = p->cap; npoints[m_NumAttributes*k+7] = p->len; npoints[m_NumAttributes*k+8] = m_ParticleGrid.ID_2_index(p->mID); npoints[m_NumAttributes*k+9] = m_ParticleGrid.ID_2_index(p->pID); } }
void GibbsTrackingFilter< ItkQBallImageType > ::EstimateParticleWeight() { MITK_INFO << "GibbsTrackingFilter: estimating particle weight"; float minSpacing; if(m_QBallImage->GetSpacing()[0]<m_QBallImage->GetSpacing()[1] && m_QBallImage->GetSpacing()[0]<m_QBallImage->GetSpacing()[2]) minSpacing = m_QBallImage->GetSpacing()[0]; else if (m_QBallImage->GetSpacing()[1] < m_QBallImage->GetSpacing()[2]) minSpacing = m_QBallImage->GetSpacing()[1]; else minSpacing = m_QBallImage->GetSpacing()[2]; float m_ParticleLength = 1.5*minSpacing; float m_ParticleWidth = 0.5*minSpacing; // seed random generators Statistics::MersenneTwisterRandomVariateGenerator::Pointer randGen = Statistics::MersenneTwisterRandomVariateGenerator::New(); if (m_RandomSeed>-1) randGen->SetSeed(m_RandomSeed); else randGen->SetSeed(); // instantiate all necessary components SphereInterpolator* interpolator = new SphereInterpolator(m_LutPath); // handle lookup table not found cases if( !interpolator->IsInValidState() ) { m_IsInValidState = false; m_AbortTracking = true; m_BuildFibers = false; mitkThrow() << "Unable to load lookup tables."; } ParticleGrid* particleGrid = new ParticleGrid(m_MaskImage, m_ParticleLength, m_ParticleGridCellCapacity); GibbsEnergyComputer* encomp = new GibbsEnergyComputer(m_QBallImage, m_MaskImage, particleGrid, interpolator, randGen); // EnergyComputer* encomp = new EnergyComputer(m_QBallImage, m_MaskImage, particleGrid, interpolator, randGen); MetropolisHastingsSampler* sampler = new MetropolisHastingsSampler(particleGrid, encomp, randGen, m_CurvatureThreshold); float alpha = log(m_EndTemperature/m_StartTemperature); m_ParticleWeight = 0.01; int ppv = 0; // main loop int neededParts = 3000; while (ppv<neededParts) { if (ppv<1000) m_ParticleWeight /= 2; else m_ParticleWeight = ppv*m_ParticleWeight/neededParts; encomp->SetParameters(m_ParticleWeight,m_ParticleWidth,m_ConnectionPotential*m_ParticleLength*m_ParticleLength,m_CurvatureThreshold,m_InexBalance,m_ParticlePotential); for( int step = 0; step < 10; step++ ) { // update temperatur for simulated annealing process float temperature = m_StartTemperature * exp(alpha*(((1.0)*step)/((1.0)*10))); sampler->SetTemperature(temperature); for (unsigned long i=0; i<10000; i++) sampler->MakeProposal(); } ppv = particleGrid->m_NumParticles; particleGrid->ResetGrid(); } delete sampler; delete encomp; delete particleGrid; delete interpolator; MITK_INFO << "GibbsTrackingFilter: finished estimating particle weight"; }
RJMCMCBase(float *points,int numPoints, float *dimg, const int *dsz, double *voxsize, double cellsize) : m_QBallImageData(dimg) , datasz(dsz) , enc(0) , width(dsz[1]*voxsize[0]) , height(dsz[2]*voxsize[1]) , depth(dsz[3]*voxsize[2]) , voxsize(voxsize) , m_NumAttributes(0) , m_AcceptedProposals(0) { fprintf(stderr,"Data dimensions (mm) : %f x %f x %f\n",width,height,depth); fprintf(stderr,"Data dimensions (voxel) : %i x %i x %i\n",datasz[1],datasz[2],datasz[3]); fprintf(stderr,"voxel size (mm) : %lf x %lf x %lf\n",voxsize[0],voxsize[1],voxsize[2]); float cellcnt_x = (int)((float)width/cellsize) +1; float cellcnt_y = (int)((float)height/cellsize) +1; float cellcnt_z = (int)((float)depth/cellsize) +1; //int cell_capacity = 2048; //int cell_capacity = 64; int cell_capacity = 512; fprintf(stderr,"grid dimensions : %f x %f x %f\n",cellcnt_x,cellcnt_y,cellcnt_z); fprintf(stderr,"grid cell size (mm) : %f^3\n",cellsize); fprintf(stderr,"cell capacity : %i\n",cell_capacity); fprintf(stderr,"#cells*cellcap : %.1f K\n",cell_capacity*cellcnt_x*cellcnt_y*cellcnt_z/1000); int minsize = 1000000; int err = m_ParticleGrid.allocate(((numPoints>minsize)? (numPoints+100000) : minsize), cellcnt_x, cellcnt_y, cellcnt_z, cellsize, cell_capacity); if (err == -1) { fprintf(stderr,"RJMCMCBase: out of Memory!\n"); return; } m_NumAttributes = 10; for (int k = 0; k < numPoints; k++) { Particle *p = m_ParticleGrid.newParticle(pVector(points[m_NumAttributes*k], points[m_NumAttributes*k+1],points[m_NumAttributes*k+2])); if (p!=0) { p->N = pVector(points[m_NumAttributes*k+3],points[m_NumAttributes*k+4],points[m_NumAttributes*k+5]); p->cap = points[m_NumAttributes*k+6]; p->len = points[m_NumAttributes*k+7]; p->mID = (int) points[m_NumAttributes*k+8]; p->pID = (int) points[m_NumAttributes*k+9]; if (p->mID != -1) m_ParticleGrid.concnt++; if (p->pID != -1) m_ParticleGrid.concnt++; p->label = 0; } else { fprintf(stderr,"error: cannot allocate particle, con. indices will be wrong! \n"); } } m_ParticleGrid.concnt /= 2; m_Iterations = 0; m_AcceptedProposals = 0; }
RJMCMCBase(REAL *points,int numPoints, REAL *vfmap, REAL *dimg, const int *dsz, double *voxsize, double cellsize, REAL *cellsize2, REAL len, int numcores) { dataimg = dimg; datasz = dsz; this->vfmap = vfmap; width = datasz[2]*voxsize[0]; height = datasz[3]*voxsize[1]; depth = datasz[4]*voxsize[2]; this->voxsize = voxsize; this->numcores = numcores; fprintf(stderr,"Data dimensions (mm) : %f x %f x %f\n",width,height,depth); fprintf(stderr,"Data dimensions (voxel) : %i x %i x %i\n",datasz[2],datasz[3],datasz[4]); fprintf(stderr,"Data directions (sizeLUT * dirs) : %i * %i\n",datasz[0],datasz[1]); fprintf(stderr,"voxel size (mm) : %f x %f x %f\n",voxsize[0],voxsize[1],voxsize[2]); ////// dimensions of the particle grid // the one to find connection partners REAL cellcnt_x = (int)((REAL)width/cellsize) +2; REAL cellcnt_y = (int)((REAL)height/cellsize) +2; REAL cellcnt_z = (int)((REAL)depth/cellsize) +2; int cell_capacity = 2048; // for finding partners whithn the same voxel REAL cellcnt_x2 = (int)((REAL)width/cellsize2[0]) +2; REAL cellcnt_y2 = (int)((REAL)height/cellsize2[1]) +2; REAL cellcnt_z2 = (int)((REAL)depth/cellsize2[2]) +2; int cell_capacity2 = 20; fprintf(stderr,"grid dimensions : %f x %f x %f\n",cellcnt_x,cellcnt_y,cellcnt_z); fprintf(stderr,"grid cell size (mm) : %f^3\n",cellsize); fprintf(stderr,"grid2 dimensions : %f x %f x %f\n",cellcnt_x2,cellcnt_y2,cellcnt_z2); fprintf(stderr,"grid2 cell size (mm) : %fx%fx%f\n",cellsize2[0],cellsize2[1],cellsize2[2]); fprintf(stderr,"cell capacity : %i\n",cell_capacity); fprintf(stderr,"#cells*cellcap : %.1f K\n",cell_capacity*cellcnt_x*cellcnt_y*cellcnt_z/1000); #ifdef PARALLEL_OPENMP fprintf(stderr,"#threads : %i/%i\n",numcores,omp_get_max_threads()); #endif // allocate the particle grid int minsize = 100000; int err = pcontainer.allocate(((numPoints>minsize)? (numPoints+100000) : minsize), cellcnt_x, cellcnt_y, cellcnt_z, cellsize,cell_capacity, cellcnt_x2, cellcnt_y2, cellcnt_z2, cellsize2,cell_capacity2,len); if (err == -1) { fprintf(stderr,"RJMCMCBase: out of Memory!\n"); return; } // read the data from MATLAB into our structure attrcnt = 15; for (int k = 0; k < numPoints; k++) { Particle *p = pcontainer.newParticle(pVector(points[attrcnt*k ], points[attrcnt*k+1],points[attrcnt*k+2]), pVector(points[attrcnt*k+3], points[attrcnt*k+4],points[attrcnt*k+5])); if (p!=0) { p->N = pVector(points[attrcnt*k+3],points[attrcnt*k+4],points[attrcnt*k+5]); p->len = points[attrcnt*k+7]; p->mID = (int) points[attrcnt*k+8]; p->pID = (int) points[attrcnt*k+9]; p->Di = points[attrcnt*k+10]; p->Da = points[attrcnt*k+11]; p->Dp = points[attrcnt*k+12]; p->w = points[attrcnt*k+13]; p->q = points[attrcnt*k+14]; p->label = 0; p->active = true; if (p->mID != -1) { pcontainer.removeFromGrid(1,p); pcontainer.concnt++; } if (p->pID != -1) { pcontainer.removeFromGrid(0,p); pcontainer.concnt++; } } else { fprintf(stderr,"error: cannot allocate particle, con. indices will be wrong! \n"); } } pcontainer.concnt /= 2; itmax = 0; accepted = 0; }
/////////////////////////////// the MAIN LOOP void iterate(double handle) { #ifdef TIMING tic(&total_time); #endif int frq = 500000; // number iteration between MATLAB calls of 'drawnow' int numits; int outer = 0; REAL updownratio_break; bool adaptiveschedule = false; numits = (int) itmax; if (frq > numits) { frq = numits; outer = 1; } else { outer = numits/frq; } if (gammaDE != 0) // then we have an adative temp-schedule { REAL itmax_d = gammaDE; stats.lam_deltaE = itmax_d; birthstats.lam_deltaE = itmax_d; deathstats.lam_deltaE = itmax_d; shiftstats.lam_deltaE = itmax_d; capstats.lam_deltaE = itmax_d; vfstats.lam_deltaE = itmax_d; connstats.lam_deltaE = itmax_d; adaptiveschedule = true; } else { adaptiveschedule = false; } #ifdef PARALLEL_OPENMP omp_set_dynamic(0); // Explicitly disable dynamic teams omp_set_num_threads(numcores); // Use n threads for all consecutive parallel regions #endif #ifdef LINUX_MACHINE static struct timeval timeS; #endif long time =0; int f; for (f = 0; f < outer;f++) { stats.clear(); birthstats.clear(); deathstats.clear(); connstats.clear(); shiftstats.clear(); capstats.clear(); vfstats.clear(); //fprintf(stderr,"parain\n"); fflush(stderr); time = 0; #ifdef LINUX_MACHINE gettimeofday( &timeS, NULL); time -= (timeS.tv_sec*1000000 + timeS.tv_usec); #endif #ifdef PARALLEL_OPENMP #pragma omp parallel for #endif for (int it = 0; it < frq;it++) { if (!pcontainer.needs_reallocation) iterate_onestep(); } if (pcontainer.needs_reallocation) { if (pcontainer.reallocate() == -1) { fprintf(stderr,"out of Memory!!\n"); return; } } #ifdef LINUX_MACHINE gettimeofday( &timeS, NULL); time += (timeS.tv_sec*1000000 + timeS.tv_usec); #endif fprintf(stderr,"#freeslots: %i pcnt:%i #particles:%i\n", (int) pcontainer.freeslots.size(),(int) pcontainer.pcnt,(int)(pcontainer.pcnt-pcontainer.freeslots.size())); pcontainer.defrag(); fprintf(stderr,"#it: %ik #p: %i #c: %i time: %.2fs \n", frq*(f+1)/1000, pcontainer.pcnt,pcontainer.concnt,double(time)/1000000); stats.report_movetype(" total:"); char buf[256]; sprintf(buf,"birth (%.3f):",p_birth); birthstats.report_movetype(buf); sprintf(buf,"death (%.3f):",p_death); deathstats.report_movetype(buf); sprintf(buf,"shift (%.3f):",p_shift); shiftstats.report_movetype(buf); sprintf(buf,"model (%.3f):",p_Dmod); capstats.report_movetype(buf); sprintf(buf,"volfr (%.3f):",p_vfmod); vfstats.report_movetype(buf); sprintf(buf,"track (%.3f):",p_conprob); connstats.report_movetype(buf); REAL cr = (REAL) pcontainer.celloverflows; if (cr > 0.01) fprintf(stderr,"warning: celloverflows : %.2f \n",cr); if (handle != 0) { mexEvalString("drawnow"); if (mexGet(handle,"Tag") == 0) { fprintf(stderr,"Termination by User.\n\n"); break; } } accepted = 0; if (adaptiveschedule && stats.deltaE < 0) { fprintf(stderr,"equilibrium threshold reached, breaking\n"); break; } } iterations_done = (f+1)*frq; //it; }