コード例 #1
0
ファイル: poppy.c プロジェクト: spacepy/spacepy
/*Performs inner loop of association analysis confidence interval, including
 *bootstrapping and summing on the bootstraps.
 *n_assoc: number of associations for each lag, each time in process1 
 *         (n_lags * n_p1)
 *surr_assoc_total: (out) total number of associations for each lag, each
 *                  surrogate series (n_lags * n_surr)
 *n_lags: number of time lags
 *n_p1: number of times in process 1, i.e. number of association numbers for
 *      each lag
 *n_surr: number of surrogate series, i.e. number of times to execute the
 *        bootstrap for each lag
 *seeds: numbers to seed RNG, reseeded each lag (n_lags)
 *clock_seed: if true, seed RNG from the clock, ignore seeds
 */
void aa_ci(unsigned long *n_assoc, unsigned long *surr_assoc_total,
	   unsigned long n_lags, unsigned long n_p1, unsigned long n_surr,
	   unsigned long *seeds, int clock_seed)
{
  unsigned long sample; /*Sample number*/
  unsigned long *curr_assoc; /*Association numbers for the current lag*/
  unsigned long *max_assoc; /*Just past last value of association numbers*/
  unsigned long *totalp; /*Point to total assoc. no.; current lag, surrogate*/
  unsigned long *max_total; /*Just past last value of total for this lag*/
  rk_state state;
  unsigned long max = n_p1 - 1;
  if(clock_seed)
    rk_randomseed(&state);
  
  max_assoc = n_assoc + n_p1 * n_lags;
  totalp = surr_assoc_total;
  /*Looping over every lag*/
  for(curr_assoc=n_assoc; curr_assoc<max_assoc; curr_assoc += n_p1, seeds++) {
    if(!clock_seed)
      rk_seed(*seeds, &state);
    max_total = totalp + n_surr;
    /*Looping over every desired surrogate for this lag*/
    for(; totalp<max_total; totalp++) {
      *totalp = 0;
      /*Sample with replacement the original distribution, to the same size,
       *and calculate the total of this resampling
       */
      for(sample=0; sample<n_p1; sample++)
	*totalp += curr_assoc[rk_interval(max, &state)];
    }
  }
}
コード例 #2
0
ファイル: poppy.c プロジェクト: spacepy/spacepy
/*Performs inner loop of bootstrapping
 *Bootstraps n_els elements of data, n times
 *surrogates: list of surrogates (n * n_els)
 *data: list of original data (n_els)
 *n: number of bootstraps
 *n_els: number of elements
 *seed: number to seed RNG
 *clock_seed: if true, seed RNG from the clock, ignore seed
 */
void boots(double* surrogates, double* data,
	   unsigned long n, unsigned long n_els,
	   unsigned long sample_size,
	   unsigned long seed, int clock_seed)
{
  rk_state state;
  double *curr_surr, *last_surr;
  unsigned long max = n_els - 1;
  if(clock_seed)
    rk_randomseed(&state);
  else
    rk_seed(seed, &state);
  curr_surr = surrogates;
  last_surr = surrogates + (n * sample_size);
  for(curr_surr=surrogates; curr_surr < last_surr; curr_surr++)
    *curr_surr = data[rk_interval(max, &state)];
}
コード例 #3
0
ファイル: cmontecarlo.c プロジェクト: karandesai-96/tardis
void
montecarlo_main_loop(storage_model_t * storage, int64_t virtual_packet_flag, int nthreads, unsigned long seed)
{
  int64_t finished_packets = 0;
  storage->virt_packet_count = 0;
#ifdef WITH_VPACKET_LOGGING
  storage->virt_packet_nus = (double *)safe_malloc(sizeof(double) * storage->no_of_packets);
  storage->virt_packet_energies = (double *)safe_malloc(sizeof(double) * storage->no_of_packets);
  storage->virt_packet_last_interaction_in_nu = (double *)safe_malloc(sizeof(double) * storage->no_of_packets);
  storage->virt_packet_last_interaction_type = (int64_t *)safe_malloc(sizeof(int64_t) * storage->no_of_packets);
  storage->virt_packet_last_line_interaction_in_id = (int64_t *)safe_malloc(sizeof(int64_t) * storage->no_of_packets);
  storage->virt_packet_last_line_interaction_out_id = (int64_t *)safe_malloc(sizeof(int64_t) * storage->no_of_packets);
  storage->virt_array_size = storage->no_of_packets;
#endif // WITH_VPACKET_LOGGING
#ifdef WITHOPENMP
  omp_set_dynamic(0);
  if (nthreads > 0)
    {
      omp_set_num_threads(nthreads);
    }

#pragma omp parallel firstprivate(finished_packets)
    {
      rk_state mt_state;
      rk_seed (seed + omp_get_thread_num(), &mt_state);
#pragma omp master
      {
        fprintf(stderr, "Running with OpenMP - %d threads\n", omp_get_num_threads());
        print_progress(0, storage->no_of_packets);
      }
#pragma omp for
#else
      rk_state mt_state;
      rk_seed (seed, &mt_state);
      fprintf(stderr, "Running without OpenMP\n");
#endif
      for (int64_t packet_index = 0; packet_index < storage->no_of_packets; ++packet_index)
        {
          int reabsorbed = 0;
          rpacket_t packet;
          rpacket_set_id(&packet, packet_index);
          rpacket_init(&packet, storage, packet_index, virtual_packet_flag);
          if (virtual_packet_flag > 0)
            {
              reabsorbed = montecarlo_one_packet(storage, &packet, -1, &mt_state);
            }
          reabsorbed = montecarlo_one_packet(storage, &packet, 0, &mt_state);
          storage->output_nus[packet_index] = rpacket_get_nu(&packet);
          if (reabsorbed == 1)
            {
              storage->output_energies[packet_index] = -rpacket_get_energy(&packet);
            }
          else
            {
              storage->output_energies[packet_index] = rpacket_get_energy(&packet);
            }
          if ( ++finished_packets%100 == 0 )
            {
#ifdef WITHOPENMP
              // WARNING: This only works with a static sheduler and gives an approximation of progress.
              // The alternative would be to have a shared variable but that could potentially decrease performance when using many threads.
              if (omp_get_thread_num() == 0 )
                print_progress(finished_packets * omp_get_num_threads(), storage->no_of_packets);
#else
              print_progress(finished_packets, storage->no_of_packets);
#endif
            }
        }
#ifdef WITHOPENMP
    }
#endif
  print_progress(storage->no_of_packets, storage->no_of_packets);
  fprintf(stderr,"\n");
}
コード例 #4
0
ファイル: iconic.c プロジェクト: Garyfallidis/nipy
void joint_histogram(double* H, 
		     unsigned int clampI, 
		     unsigned int clampJ,  
		     PyArrayIterObject* iterI,
		     const PyArrayObject* imJ_padded, 
		     const double* Tvox, 
		     int affine, 
		     int interp)
{
  const signed short* J=(signed short*)imJ_padded->data; 
  size_t dimJX=imJ_padded->dimensions[0]-2;
  size_t dimJY=imJ_padded->dimensions[1]-2; 
  size_t dimJZ=imJ_padded->dimensions[2]-2;  
  signed short Jnn[8]; 
  double W[8]; 
  signed short *bufI, *bufJnn; 
  double *bufW; 
  signed short i, j;
  size_t off;
  size_t u2 = imJ_padded->dimensions[2]; 
  size_t u3 = u2+1; 
  size_t u4 = imJ_padded->dimensions[1]*u2;
  size_t u5 = u4+1; 
  size_t u6 = u4+u2; 
  size_t u7 = u6+1; 
  double wx, wy, wz, wxwy, wxwz, wywz; 
  double W0, W2, W3, W4; 
  size_t x, y, z; 
  int nn, nx, ny, nz;
  double Tx, Ty, Tz; 
  double *bufTvox = (double*)Tvox; 
  void (*interpolate)(unsigned int, double*, unsigned int, const signed short*, const double*, int, void*); 
  void* interp_params = NULL; 
  rk_state rng; 

  /* Reset the source image iterator */
  PyArray_ITER_RESET(iterI);

  /* Make sure the iterator the iterator will update coordinate values */ 
  UPDATE_ITERATOR_COORDS(iterI); 

  /* Set interpolation method */ 
  if (interp==0) 
    interpolate = &_pv_interpolation;
  else if (interp>0) 
    interpolate = &_tri_interpolation; 
  else { /* interp < 0 */ 
    interpolate = &_rand_interpolation;
    rk_seed(-interp, &rng); 
    interp_params = (void*)(&rng); 
  }

  /* Re-initialize joint histogram */ 
  memset((void*)H, 0, clampI*clampJ*sizeof(double));

  /* Looop over source voxels */
  while(iterI->index < iterI->size) {
  
    /* Source voxel intensity */
    bufI = (signed short*)PyArray_ITER_DATA(iterI); 
    i = bufI[0];

    /* Compute the transformed grid coordinates of current voxel */ 
    if (affine) {
      /* Get voxel coordinates and apply transformation on-the-fly*/
      x = iterI->coordinates[0];
      y = iterI->coordinates[1];
      z = iterI->coordinates[2];
      _affine_transform(&Tx, &Ty, &Tz, Tvox, x, y, z); 
    }
    else 
      /* Use precomputed transformed coordinates */ 
      bufTvox = _precomputed_transform(&Tx, &Ty, &Tz, (const double*)bufTvox);
       
    /* Test whether the current voxel is below the intensity
       threshold, or the transformed point is completly outside
       the reference grid */
    if ((i>=0) && 
	(Tx>-1) && (Tx<dimJX) && 
	(Ty>-1) && (Ty<dimJY) && 
	(Tz>-1) && (Tz<dimJZ)) {
	
      /* 
	 Nearest neighbor (floor coordinates in the padded
	 image, hence +1). 
	 
	 Notice that using the floor function doubles excetution time.
	 
	 FIXME: see if we can replace this with assembler instructions. 
      */
      nx = FLOOR(Tx) + 1;
      ny = FLOOR(Ty) + 1;
      nz = FLOOR(Tz) + 1;
      
      /* The convention for neighbor indexing is as follows:
       *
       *   Floor slice        Ceil slice
       *
       *     2----6             3----7                     y          
       *     |    |             |    |                     ^ 
       *     |    |             |    |                     |
       *     0----4             1----5                     ---> x
       */
      
      /*** Trilinear interpolation weights.  
	   Note: wx = nnx + 1 - Tx, where nnx is the location in
	   the NON-PADDED grid */ 
      wx = nx - Tx; 
      wy = ny - Ty;
      wz = nz - Tz;
      wxwy = wx*wy;    
      wxwz = wx*wz;
      wywz = wy*wz;
      
      /*** Prepare buffers */ 
      bufJnn = Jnn;
      bufW = W; 
      
      /*** Initialize neighbor list */
      off = nx*u4 + ny*u2 + nz; 
      nn = 0; 
      
      /*** Neighbor 0: (0,0,0) */ 
      W0 = wxwy*wz; 
      APPEND_NEIGHBOR(off, W0); 
      
      /*** Neighbor 1: (0,0,1) */ 
      APPEND_NEIGHBOR(off+1, wxwy-W0);
      
      /*** Neighbor 2: (0,1,0) */ 
      W2 = wxwz-W0; 
      APPEND_NEIGHBOR(off+u2, W2);  
      
      /*** Neightbor 3: (0,1,1) */
      W3 = wx-wxwy-W2;  
      APPEND_NEIGHBOR(off+u3, W3);  
      
      /*** Neighbor 4: (1,0,0) */
      W4 = wywz-W0;  
      APPEND_NEIGHBOR(off+u4, W4); 
      
      /*** Neighbor 5: (1,0,1) */ 
      APPEND_NEIGHBOR(off+u5, wy-wxwy-W4);   
      
      /*** Neighbor 6: (1,1,0) */ 
      APPEND_NEIGHBOR(off+u6, wz-wxwz-W4);  
      
      /*** Neighbor 7: (1,1,1) */ 
      APPEND_NEIGHBOR(off+u7, 1-W3-wy-wz+wywz);  
      
      /* Update the joint histogram using the desired interpolation technique */ 
      interpolate(i, H, clampJ, Jnn, W, nn, interp_params); 
      
      
    } /* End of IF TRANSFORMS INSIDE */
    
    /* Update source index */ 
    PyArray_ITER_NEXT(iterI); 
    
  } /* End of loop over voxels */ 
  

  return; 
}