Exemplo n.º 1
0
double corr(double *a, double *b, int n)
{
    double v12, v11, v22, y1, y2, y ;
    double *aa, *bb ;
    ZALLOC(aa, n, double) ;
    ZALLOC(bb, n, double) ;
    y1 = asum(a,n)/ (double) n ;
    y2 = asum(b,n)/ (double) n ;

    vsp(aa, a, -y1, n) ;
    vsp(bb, b, -y2, n) ;

    v12 = vdot(aa, bb, n) ;
    v11 = asum2(aa, n) ;
    v22 = asum2(bb, n) ;

    y = v11*v22 ;
    if (y==0.0) fatalx("(corr) constant vector\n") ;


    free(aa) ;
    free(bb) ;
    return (v12/sqrt(y)) ;

}
Exemplo n.º 2
0
double corrx(double *a, double *b, int n)
// like corr but constant vec returns 0
{
    double v12, v11, v22, y1, y2, y ;
    double *aa, *bb ;

    ZALLOC(aa, n, double) ;
    ZALLOC(bb, n, double) ;
    y1 = asum(a,n)/ (double) n ;
    y2 = asum(b,n)/ (double) n ;

    vsp(aa, a, -y1, n) ;
    vsp(bb, b, -y2, n) ;

    v12 = vdot(aa, bb, n) ;
    v11 = asum2(aa, n) ;
    v22 = asum2(bb, n) ;

    free(aa) ;
    free(bb) ;

    y = v11*v22 ;
    y += 1.0e-12 ;

    return (v12/sqrt(y)) ;

}
Exemplo n.º 3
0
double
oldtwestxx (double *lam, int m, double *pzn, double *pzvar)
{
  double lsum, logsum;
  double *ww;
  double a, p, yn, var;
  double ylike, ybase, y, ylmax, ynmax, yld, yld2, ainc, ym;
  int k;

  ZALLOC(ww, m, double);
  copyarr (lam, ww, m);
  lsum = asum (ww, m);
  vlog (ww, ww, m);
  logsum = asum (ww, m);

  ylmax = -1.0e20;
  yn = (double) m;
  ybase = xxlikex (m, yn, logsum, lsum);

  for (k = 1; k <= 100; ++k)
    {
      a = yn / 2.0;
      ylike = xxlikex (m, a, logsum, lsum);
      yld = xxliked (m, a, logsum, lsum);
      ylike -= ybase;
      if (verbose)
        printf ("ynloop %12.3f %12.3f %12.3f\n", yn / (double) m, ylike, yld);
      if (ylike < ylmax)
        break;
      ylmax = ylike;
      ynmax = yn;
      yn *= 1.1;
    }
  a = ynmax / 2.0;
  for (k = 1; k <= 10; ++k)
    {
// newton iteration
      ylike = xxlikex (m, a, logsum, lsum);
      yld = xxliked (m, a, logsum, lsum);
      yld2 = xxliked2 (m, a, logsum, lsum);
      ylike -= ybase;
      ainc = -yld / yld2;
      a += ainc;
      if (verbose)
        printf ("newton: %3d  %15.9f  %15.9f  %15.9f\n", k, ylike, yld, ainc);
    }
  fflush (stdout);
  yn = 2.0 * a;
  ym = (double) m;
  var = lsum / (2.0 * a * ym);

  *pzn = yn;
  *pzvar = var;

  free (ww);
  return 0;
}
Exemplo n.º 4
0
VALUE nrmp( //
		int n,        //size of the array, sx.size==sy.size
		const VALUE* x,    //
		VALUE2 p,         //
		int incx       //
		) {
	if (p == 1.0) {
		return asum(n, x, incx);
	}
	if (p == 2.0) {
		return nrm2(n, x, incx);
	}
	VALUE zero = 0.0e+0;
	VALUE norm = zero;
	if (n <= 0 || incx <= 0) {
		return norm;
	} else if (n == 1) {
		norm = Abs(x[0]);
	} else {
		for (int i = 0; i < (n - 1) * incx; i += incx) {
			if (x[i] != zero) {
				norm += pow(Abs(x[i]), p);
			}
		}
		norm = pow(norm, 1.0 / double(p));
	}
	return norm;
}  // << ------------------------------------------
Exemplo n.º 5
0
TYPE nrm1( //
		const ArrayListV<TYPE>& ax  //[in]
		) {
	int n = ax.size();
	const TYPE* x = ax.getPointer();
	return asum(n, x, 1);
}
double dirmult(double *pp, int *aa, int len)
{
  int t, i, m  ;
  double y1, y2, ysum ;
  double top, bot ;
  
  m = len ;

  t = intsum(aa,m) ;
  if (t < 1) return 0.0 ;

  top = bot = 0.0 ;
  ysum = asum(pp,m) ;
  for (i=0; i<m; i++) {
   top +=  lgamma(pp[i] + (double) aa[i]) ;
   bot += lgamma(pp[i]) ;
  }
  top += lgamma(ysum) ;
  bot += lgamma(ysum + (double) t) ;


  y1 = top-bot ;

  return y1 -y2 ;
}
Exemplo n.º 7
0
double variance(double *a, int n)
{

    double *aa ;
    double y1, y2 ;

    ZALLOC(aa, n, double) ;
    y1 = asum(a,n)/ (double) n ;
    vsp(aa, a, -y1, n) ;

    y2 = asum(aa,n)/ (double) n ;

    free(aa) ;
    return y2 ;

}
Exemplo n.º 8
0
double trace(double *a, int n)
{
    double *diags, t ;
    ZALLOC(diags,n,double) ;
    getdiag(diags,a,n) ; /* extract diagonal */
    t = asum(diags,n) ;
    free(diags) ;
    return t ;
}
Exemplo n.º 9
0
double anova(double *vec, int len, int *xtypes, int numeg)
// anova 1 but f statistic
{
   int i, k ; 
   double y1, top, bot, ftail ;  
   double *w0, *w1, *popsize, *wmean ;

   static int ncall2  = 0 ;

   if (numeg >= len) fatalx("bad anova\n") ;
   ZALLOC(w0, len, double) ;
   ZALLOC(w1, len, double) ;
   ZALLOC(wmean, numeg, double) ;
   ZALLOC(popsize, numeg, double) ;

   y1 = asum(vec, len)/ (double) len ;  // mean
   vsp(w0, vec, -y1, len) ;

    for (i=0; i<len; i++)  { 
     k = xtypes[i] ;
     ++popsize[k] ;
     wmean[k] += w0[i] ;
    }

/* debug */
    if (numeg == 2)  {  
     ++ncall2 ;
     for (i=0; i<len; ++i) {  
      if (ncall2<0) break ;
      k = xtypes[i] ;
//    printf("yy %4d %4d %12.6f %12.6f\n", i, k, vec[i], w0[i]) ;
     }
    }

    vsp(popsize, popsize, 1.0e-12, numeg) ;
    vvd(wmean, wmean, popsize, numeg) ;

    vvt(w1, wmean, wmean, numeg) ;
    top = vdot(w1, popsize, numeg) ;
    
    for (i=0; i<len ; i++)   {  
     k = xtypes[i] ;
     w1[i] = w0[i] - wmean[k] ;
    }
    bot = asum2(w1, len) / (double) (len-numeg) ;
    bot *= (double) (numeg-1) ;
    ftail = rtlf(numeg-1, len-numeg, top/bot) ;

    free(w0) ; 
    free(w1) ; 
    free(popsize) ;
    free(wmean) ;

    return ftail ;

}
Exemplo n.º 10
0
double binomtail(int n, int t, double p, char c) 
{
/** 
 c = '+':   P(S>=t) 
 c = '-':   P(S<t) 
 WARNING <= t use binomtail(n, t+1, ... 
*/
    double *bindis ;
    double val ;

    ZALLOC(bindis, n+1, double) ;
    genlogbin(bindis, n, p) ;
    vexp(bindis, bindis, n+1) ;
    if (c=='+') 
     val = asum(bindis+t, n-t+1) ; 
    else  
     val = asum(bindis, t) ;
    free(bindis) ;
    return val ;
}
Exemplo n.º 11
0
double bal1 (double *a, int n)
// WARNING a is input and output
{
    double y ;

    y = asum(a, n) ;
    if (y<=0.0) fatalx("bad bal1\n") ;
    vst(a, a, 1.0/y, n) ;
    return y ;

}
Exemplo n.º 12
0
double pdinv(double *cinv, double *coeff, int n) 
// cinv and coeff can be same
// cinv can be NULL 
// return log det (coeff) 
{
   double *tt;
   double *p ;
   double t, sum, y ;
   int i,j, k ;

/**
   pmat(coeff, n) ;
*/
   ZALLOC (tt, n*n, double);
   ZALLOC (p, n, double );
   

  copyarr(coeff,tt,n*n); 
  
  choldc (tt, n, p) ;

 
  for (i=0; i<n; i++) {
    tt[i*n+i] = 1.0/p[i] ;
    for (j=i+1; j<n; j++) {
      sum=0.0 ;
      for (k=i; k<j; k++) {
        sum -= tt[j*n+k]*tt[k*n+i] ;
      }
      tt[j*n+i] = sum/p[j] ;

    }
  }

   for (i=0; i<n; i++) 
    for (j=i; j<n; j++) {
     sum=0.0 ;
     if (cinv == NULL) break ;
     for (k=j; k<n; k++) {
      sum += tt[k*n+j]*tt[k*n+i] ;
     }
     cinv[i*n+j] = cinv[j*n+i] = sum ;
    }

    vlog(p, p, n) ; 
    y = 2.0*asum(p, n) ;


   free(tt) ;
   free(p) ;

   return y ;

}
Exemplo n.º 13
0
double chitest(double *a, double *p, int n) 
/* a is n boxes.  Goodness of fit test to p */
{
 
 double *x, *b, *pp ;
 double y1=0.0, y2=0.0 ;
 int i ;

 ZALLOC(pp, n, double) ;
 if (p != NULL)
  copyarr(p,pp,n) ;
 else 
  vclear(pp, 1.0, n) ;

 y1 = asum(pp,n) ;
 y2 = asum(a,n) ;

 if ( (y1==0.0) || (y2==0.0) ) { 
  free(pp) ;
  return 0.0 ;
 }

 ZALLOC(x,n,double) ;
 ZALLOC(b,n,double) ;


 vst (x, pp, y2/y1, n) ;  /* expected */

 vsp (x, x, .0001, n) ;
 vvm (b, a, x, n) ;  
 vvt (b, b, b, n) ;
 vvd (b, b, x, n) ;

 y1 = asum(b,n) ;

 free(x) ;
 free(b) ;

 return y1 ;

}
Exemplo n.º 14
0
/** this is the code to parallelize */
void
domult(double  *tvecs, double  *tblock, int numrow, int len) 
{
  int i ;
  double ycheck ;
  vzero(tvecs, len*len) ;
  for (i=0; i<numrow; i++) {  
    ycheck = asum(tblock+i*len, len) ;  
    if (fabs(ycheck)>.00001) fatalx("bad ycheck\n") ;
    addoutersym(tvecs, tblock+i*len, len) ;
  }
}
Exemplo n.º 15
0
void bal(double *a, double *b, int n) 
/** 
 normalize mean 0 s.d 1 
*/
{
    double t ;
    t = asum(b,n)/ (double) n ;
    vsp (a, b, -t, n) ;

    t = asum2(a,n)/ (double) n ;
    vst (a, a, 1.0/sqrt(t), n) ;
}
Exemplo n.º 16
0
double twestxx(double *lam, int m, double *pzn,  double *pzvar) 
{
  double tw, y ;

  if (twl2mode == NO)  return oldtwestxx(lam, m, pzn, pzvar) ;
  (void) doeig2(lam,  m, pzn, &tw)  ;

  y = (*pzn) * (double) m ;
  *pzvar = asum(lam, m) / y ;
  return tw ;

}
Exemplo n.º 17
0
double binlogtail(int n, int t, double p, char c) 
{

    double *bindis ;
    double val, base ;

    ZALLOC(bindis, n+1, double) ;
    genlogbin(bindis, n, p) ;
    base = bindis[t] ;
    vsp(bindis, bindis, -base, n+1) ;
    if (c=='+') {
     vexp(bindis+t, bindis+t, n-t+1) ;
     val = asum(bindis+t, n-t+1) ; 
    }
    else  {
     vexp(bindis, bindis, t) ;
     val = asum(bindis, t) ;
    }
    free(bindis) ;
    return (log(val) + base) ;

}
Exemplo n.º 18
0
void ransamp(int *samp, int nsamp, double *p, int plen) 
/** 
 pick nsamp elements from random distribution 
 uses randis but array is at least sorted optimally 
*/
{
    double *px ;  
    int *indx ;
    double y ;
    int i, j, k ;

    if (plen<=1) { 
     ivzero(samp, nsamp) ;
     return ;
    }

    ZALLOC(px, plen, double) ;
    ZALLOC(indx, plen, int) ;

    y = asum(p, plen) ;
    vst(px, p, -1.0/y, plen) ;       
    sortit(px, indx, plen) ;
    vst(px, px, -1.0, plen) ;

    for (i=0; i<nsamp; i++) {  
/** 
 really need binary chop picker 
*/
     j = randis(px, plen) ;
     if (j<0) {  
      for (k=0; k<plen; k++) {  
       printf("zz %d %d %12.6f  %12.6f\n",k, indx[k], p[k], px[k]) ;
      }
      fatalx("bad ransamp\n") ;
     }
     k = indx[j] ;  
     samp[i] = k ;
    }
    

    free (px) ;
    free (indx) ;


}
Exemplo n.º 19
0
double
dotwcalc(double *lambda, int m, double *ptw, double *pzn, double *pzvar, int minm) 
{
  double nv, mv, tzn, tm ; 
  double *evals ;
  double y, top, bot, zn, tw, ystat ;
  double tail, lsum ;

  if (m<minm) { 
   *pzn = *pzvar = *ptw = -1 ;
   return -1.0 ;
  }
  lsum = asum(lambda, m) ;
  if (lsum<=0.0) {  
   *pzn = *pzvar = *ptw = -1 ;
   return -1.0 ;
  }

  tzn = *pzn ;
  tm  = (double) m ;

  y = (double) m  / lsum ;
  ystat = lambda[0] * y * tzn ;

  if (tzn>0.0) {  
   tw = twnorm(ystat, tm, tzn) ;
   *pzn = tzn ;
   *ptw = tw ;  
   tail = twtail(tw) ;
   return tail ;
  }
   ZALLOC(evals, m, double) ;
   vst(evals, lambda, y, m) ;
   top = (double) (m*(m+2)) ;
   bot = asum2(evals, m) - (double) m ;
   zn = top/bot ;  // see appendix to eigenpaper  NJP
   y = evals[0]*zn ;
   tw = twnorm(y, tm, zn) ;
   *pzn = zn ;
   *ptw = tw ;  
   tail = twtail(tw) ;
   free(evals) ;
   return tail ;
}
Exemplo n.º 20
0
double
anova1 (double *vec, int len, int *xtypes, int numeg)
{
  int i, k;
  double y1, y2, ylike;
  double *w0, *w1, *popsize, *wmean;

  ZALLOC(w0, len, double);
  ZALLOC(w1, len, double);
  ZALLOC(wmean, numeg, double);
  ZALLOC(popsize, numeg, double);

  y1 = asum (vec, len) / (double) len;  // mean
  vsp (w0, vec, -y1, len);

  for (i = 0; i < len; i++)
    {
      k = xtypes[i];
      ++popsize[k];
      wmean[k] += w0[i];
    }

  vsp (popsize, popsize, 1.0e-12, numeg);
  vvd (wmean, wmean, popsize, numeg);

  for (i = 0; i < len; i++)
    {
      k = xtypes[i];
      w1[i] = w0[i] - wmean[k];
    }

  y1 = asum2 (w0, len) / (double) len;
  y2 = asum2 (w1, len) / (double) len;
  ylike = 0.5 * ((double) len) * log (y1 / y2);

  free (w0);
  free (w1);
  free (popsize);
  free (wmean);

  return ylike;

}
Exemplo n.º 21
0
static Point_2 centroid(const Polygon_2& poly)
{
  assert(poly.size() >= 3);

  Polygon_2::Vertex_circulator vcir = poly.vertices_circulator();
  Polygon_2::Vertex_circulator vend = vcir;
  Polygon_2::Vertex_circulator vnext = vcir; ++vnext;

  Vector_2 centre(0, 0);
  NT a(0), asum(0);
  do {
    a = (vcir->x() * vnext->y()) - (vnext->x() * vcir->y());
    centre = centre + a * ((*vcir - CGAL::ORIGIN) + (*vnext - CGAL::ORIGIN)); // slow...
    asum += a;
    vcir = vnext;
    ++vnext;
  } while(vcir != vend);
  centre = centre / (asum * 3);
  return CGAL::ORIGIN + centre;
}
Exemplo n.º 22
0
double hwstat(double *x) 
/** Hardy-Weinberg equilibrium test 
    returns standard normal in null case. 
    +sign is excess heterozygosity 
    x[0] [1] [2] are counts for homozm hetero h**o (alt allele)
*/
{

     double p, q, ysum, s1, y1, y2, ychi, sig ;
     double a1[3], a2[3] ;

     ysum = asum(x,3) ;
     if (ysum < 0.001) return 0.0 ;
     s1 = 2*x[2]+x[1] ;
     p  = 0.5*s1/ysum;
     q = 1.0-p ;

     a1 [0] = q*q ;
     a1 [1] = 2*p*q ;
     a1 [2] = p*p ;

     vsp(a1, a1, 1.0e-8, 3) ;
     vst(a2, x, 1.0/ysum, 3) ;
     vsp(a2, a2, 1.0e-8, 3) ;

     y2 = vldot(x, a2, 3) ;
     y1 = vldot(x, a1, 3) ;

     ychi = 2.0*(y2-y1) ;
     sig = sqrt(ychi+1.0e-8) ;

     if (a2[1]<a1[1]) sig = -sig ;
/* negative => hets lo */

     return sig ;
   

}
Exemplo n.º 23
0
double doeig2(double *vals, int m, double *pzn, double *ptw) 
{
  static int ncall = 0 ;
  double y, tw, tail ;
  double zn, top, bot ;
  double *evals ;
 
  ++ncall ;
  ZALLOC(evals, m, double) ;
  copyarr(vals, evals, m) ;
  y = (double) m / asum(evals, m) ;
  vst(evals, evals, y, m) ;      
  top = (double) (m*(m+2)) ;
  bot = asum2(evals, m) - (double) m ;
  zn = top/bot ;
  y = evals[0]*zn ;
  tw = twnorm(y, (double) m, zn) ;
  tail = twtail(tw) ;
  free(evals) ;
  *pzn = zn ;
  *ptw = tw ;  
  return tail ;
}
Exemplo n.º 24
0
int
ridoutlier (double *evecs, int n, int neigs, double thresh, int *badlist,
            OUTLINFO **outinfo)
{
  /* badlist contains list of outliers */
  double *ww, *w2, y1, y2, yy, zz;
  int *vbad;
  int i, j;
  int nbad = 0;
  OUTLINFO *outpt;

  if (outliermode > 1)
    return 0;
  if (n < 3)
    return 0;
  ZALLOC(ww, n, double);
  ZALLOC(vbad, n, int);
  for (j = 0; j < n; j++)
    {
      outpt = outinfo[j];
      outpt->vecno = -1;
    }
  for (i = 0; i < neigs; ++i)
    {
      copyarr (evecs + i * n, ww, n);
      if (outliermode == 0)
        {
          y1 = asum (ww, n) / (double) n;
          vsp (ww, ww, -y1, n);
          y2 = asum2 (ww, n) / (double) n;
          y2 = sqrt (y2);
          vst (ww, ww, 1.0 / y2, n);

          for (j = 0; j < n; j++)
            {
              if (fabs (ww[j]) > thresh)
                {
                  vbad[j] = 1;
                  outpt = outinfo[j];
                  if (outpt->vecno < 0)
                    {
                      outpt->vecno = i;
                      outpt->score = ww[j];
                    }
                }
            }
        }
      if (outliermode == 1)
        {
          ZALLOC(w2, n, double);
          for (j = 0; j < n; j++)
            {
              yy = ww[j];
              ww[j] = 0;
              y1 = asum (ww, n) / (double) (n - 1);
              vsp (w2, ww, -y1, n);
              w2[j] = 0;
              y2 = asum2 (w2, n) / (double) n;
              y2 = sqrt (y2);
              zz = yy - y1;
              zz /= y2;
              if (fabs (zz) > thresh)
                {
                  vbad[j] = 1;
                  outpt = outinfo[j];
                  if (outpt->vecno < 0)
                    {
                      outpt->vecno = i;
                      outpt->score = zz;
                    }
                }
              ww[j] = yy;
            }
          free (w2);
        }
    }
  for (j = 0; j < n; j++)
    {
      if (vbad[j] == 1)
        {
          badlist[nbad] = j;
          ++nbad;
        }
    }
  free (ww);
  free (vbad);
  return nbad;

}
 template<class T> void asum(const hoNDArray<T>& x, typename realType<T>::Type& r)
 {
     asum(x.get_number_of_elements(), x.begin(), r);
 }
 template<class T> typename realType<T>::Type asum(const hoNDArray<T>& x)
 {
     typename realType<T>::Type r;
     asum(x, r);
     return r;
 }
Exemplo n.º 27
0
int main(){
	srand(time(0));
	size_t n = 100;
	std::complex<double> *A = new std::complex<double>[n*n];
	std::complex<double> *Acopy = new std::complex<double>[n*n];
	std::complex<double> *w = new std::complex<double>[n];
	std::complex<double> *U = new std::complex<double>[n*n];
	std::complex<double> *V = new std::complex<double>[n*n];
	
	for(size_t i = 0; i < n; ++i){
		for(size_t j = 0; j < n; ++j){
			A[i+j*n] = crand();
		}
	}
	
	//makeid(n,n,A);
	/*
	A[1+0*n] = frand();
	A[2+1*n] = frand();
	A[2+0*n] = frand();
	makesym(n,n,A);
	*/
	
	//print(n,n,A); std::cout << std::endl;
	
	
	for(size_t i = 0; i < n; ++i){
		for(size_t j = 0; j < n; ++j){
			Acopy[i+j*n] = A[i+j*n];
		}
	}
	
	size_t lwork = 2*n;
	std::complex<double> *work = new std::complex<double>[lwork];
	size_t lrwork = 8*n;
	double *rwork = new double[lrwork];
	int info;
	info = RNP::Eigensystem(n,A,n,w,U,n,V,n, work,rwork);
	delete [] work;
	delete [] rwork;
	
	//std::cout << "alpha = "; print(n,alpha); std::cout << std::endl;
	//std::cout << "beta  = "; print(n,beta ); std::cout << std::endl;
	
	std::cout << "Info = " << info << std::endl;
	
	// Verify
	std::complex<double> *temp = new std::complex<double>[n*n];
	double maxerr = 0;
	
	for(size_t i = 0; i < n; ++i){
		// Compute A*V - w*V
		for(size_t j = 0; j < n; ++j){
			std::complex<double> asum(0);
			for(size_t k = 0; k < n; ++k){
				asum += Acopy[i+k*n]*V[k+j*n];
			}
			temp[i+j*n] = asum-w[j]*V[i+j*n];
			double curerr = std::abs(temp[i+j*n]);
			if(curerr > maxerr){ maxerr = curerr; }
		}
	}
	//print(n,n,temp); std::cout << std::endl;
	std::cout << "Max err = " << maxerr << std::endl;
	
	maxerr = 0;
	for(size_t i = 0; i < n; ++i){
		// Compute beta*U^H*A - alpha*U^H*B
		for(size_t j = 0; j < n; ++j){
			std::complex<double> asum(0);
			for(size_t k = 0; k < n; ++k){
				asum += Acopy[k+j*n]*std::conj(U[k+i*n]);
			}
			temp[i+j*n] = asum-w[i]*std::conj(U[i+j*n]);
			double curerr = std::abs(temp[i+j*n]);
			if(curerr > maxerr){ maxerr = curerr; }
		}
	}
	//print(n,n,temp); std::cout << std::endl;
	std::cout << "Max err = " << maxerr << std::endl;
	
	delete [] temp;
	
	//print(n,evals); std::cout << std::endl;
	//print(n,n,evecs); std::cout << std::endl;
	
	delete [] A;
	delete [] Acopy;
	delete [] w;
	delete [] U;
	delete [] V;
	return 0;
}
int gpuBufferSensePrepGadget::process(
		GadgetContainerMessage<IsmrmrdReconData>* m1) {

	IsmrmrdReconData* recondata= m1->getObjectPtr();

	if (recondata->rbit_.size() != 1){
		throw std::runtime_error("gpuBufferSensePrepGadget only support a single encoding space");
	}

	IsmrmrdReconBit& reconbit = recondata->rbit_[0];

	GenericReconJob job;

	IsmrmrdDataBuffered* buffer = &reconbit.data_;

	//Use reference data if available.
	if (reconbit.ref_){
		GDEBUG("Using Reference data for CSM estimation\n");
		buffer = reconbit.ref_.get_ptr();
	}

	size_t ncoils = buffer->headers_[0].active_channels;


	std::vector<size_t> new_order = {0,1,2,4,5,6,3};

	boost::shared_ptr<cuNDArray<float>> dcw;
	boost::shared_ptr<cuNDArray<floatd2>> traj;
	if (buffer->trajectory_){
		auto & trajectory = *buffer->trajectory_;

		if (buffer->headers_[0].trajectory_dimensions == 3){
			auto traj_dcw = separate_traj_and_dcw(&trajectory);
			dcw = boost::make_shared<cuNDArray<float>>(std::get<1>(traj_dcw).get());
			traj = boost::make_shared<cuNDArray<floatd2>>(std::get<0>(traj_dcw).get());
		} else if (buffer->headers_[0].trajectory_dimensions == 2){
			auto old_traj_dims = *trajectory.get_dimensions();
			std::vector<size_t> traj_dims (old_traj_dims.begin()+1,old_traj_dims.end()); //Remove first element
			hoNDArray<floatd2> tmp_traj(traj_dims,(floatd2*)trajectory.get_data_ptr());
			traj = boost::make_shared<cuNDArray<floatd2>>(tmp_traj);
		} else {
			throw std::runtime_error("Unsupported number of trajectory dimensions");
		}
	}
	{
		auto tmpdim = *buffer->data_.get_dimensions();
		for (auto dim : tmpdim)
			std::cout << dim << " ";
		std::cout << std::endl;
		auto permuted = permute((hoNDArray<float_complext>*)&buffer->data_,&new_order);
		cuNDArray<float_complext> data(*permuted);
		if (dcw){
			float scale_factor = float(prod(image_dims_recon_os_))/asum(dcw.get());
			*dcw *= scale_factor;
		}

		auto reg_images = reconstruct_regularization(&data,traj.get(),dcw.get(),ncoils);
		//reg_images->squeeze();

		auto csm = estimate_b1_map<float,2>(reg_images.get());
		*reg_images *= *csm;
		auto combined = sum(reg_images.get(),reg_images->get_number_of_dimensions()-1);

		auto tmp_combined = abs(reg_images.get());
		auto tmpcsm = abs(csm.get());
		job.csm_host_ = csm->to_host();
		job.reg_host_ = combined->to_host();
	}


	IsmrmrdDataBuffered* mainbuffer = &reconbit.data_;

	//Permute as Sensegadgets expect last dimension to be coils. *Sigh*
	job.dat_host_ =permute((hoNDArray<float_complext>*)&mainbuffer->data_,&new_order);

	if (mainbuffer->trajectory_){
		auto & trajectory = *mainbuffer->trajectory_;
		if (mainbuffer->headers_[0].trajectory_dimensions >2 ){
			auto traj_dcw = separate_traj_and_dcw(&trajectory);
			job.tra_host_ = std::get<0>(traj_dcw);
			job.dcw_host_ = std::get<1>(traj_dcw);
		} else if (mainbuffer->headers_[0].trajectory_dimensions == 2){
			auto old_traj_dims = *trajectory.get_dimensions();
			std::vector<size_t> traj_dims (old_traj_dims.begin()+1,old_traj_dims.end()); //Remove first element
			hoNDArray<floatd2> tmp_traj(traj_dims,(floatd2*)trajectory.get_data_ptr());
			job.tra_host_ = boost::make_shared<hoNDArray<floatd2>>(tmp_traj);
			auto host_dcw = boost::make_shared<hoNDArray<float>>(traj_dims);
			fill(host_dcw.get(),1.0f);
			job.dcw_host_ = host_dcw;

		} else {
			throw std::runtime_error("Unsupported number of trajectory dimensions");
		}
	}
	{
		float scale_factor = float(prod(image_dims_recon_os_))/asum(job.dcw_host_.get());
		*job.dcw_host_  *= scale_factor;
	}

	auto data_dims = *job.dat_host_->get_dimensions();
	//Sense gadgets expect only 1 dimension for encoding, so collapse the first
	size_t elements = std::accumulate(data_dims.begin(),data_dims.end()-1,1,std::multiplies<size_t>());
	std::vector<size_t> new_data_dims = {elements,data_dims.back()};
	job.dat_host_->reshape(&new_data_dims);

	size_t traj_elements = job.tra_host_->get_number_of_elements();
	auto traj_dims = *job.tra_host_->get_dimensions();

	size_t kpoints_per_frame = traj_dims[0]*profiles_per_frame_;
	if (traj_elements%kpoints_per_frame){
		std::stringstream ss;
		ss << "Profiles per frame (" << profiles_per_frame_ << ") must be a divisor of total number of profiles (" << traj_elements/traj_dims[0] << ")";
		throw std::runtime_error(ss.str());
	}
	std::vector<size_t> new_traj_dims ={kpoints_per_frame,traj_elements/kpoints_per_frame};

	job.tra_host_->reshape(&new_traj_dims);
	job.dcw_host_->reshape(&new_traj_dims);


	//Let's invent some image headers!
	size_t total_frames = profiles_per_frame_ > 0 ? mainbuffer->headers_.get_number_of_elements()/profiles_per_frame_ : 1 ;
	job.image_headers_ = boost::shared_array<ISMRMRD::ImageHeader>(new ISMRMRD::ImageHeader[total_frames]);
	for (size_t i = 0; i < total_frames; i++){
		job.image_headers_[i] = create_image_header(mainbuffer->headers_[i*profiles_per_frame_],mainbuffer->sampling_,i,total_frames);
	}


	m1->release(); //We be done with everything now.

	auto header_message = new GadgetContainerMessage<ISMRMRD::ImageHeader>(job.image_headers_[0]);

	auto job_message = new GadgetContainerMessage<GenericReconJob>(job);

	header_message->cont(job_message);

	if (!this->next()->putq(header_message)){
		GDEBUG("Failed to put message on que");
		return GADGET_FAIL;
	} else
		return GADGET_OK;



	//cuNDArray<float_complext> reg_images = reconstruct_regularization(reconbit.data_);
}
Exemplo n.º 29
0
void
dof2score(double *f2score, double *f2scoresig, SNP **xsnplist, int *xindex, int *xtypes, 
 int nrows, int ncols, int numeg, int nblocks) 
{

   int t1, t2 ;
   int a, b, c, d ;
   int c1[2], c2[2], *cc ;
   int *rawcol, *popall, *pop0, *pop1 ;
   int k, g, i, col, j ; 
   double ya, yb, y, jest, jsig, mean ;
   SNP *cupt ;
   double top, bot, *djack, *wjack, gtop, gbot, *wbot, *wtop ;
   double *btop, *bbot, wt ;
   double ytop, ybot ;
   double y1, y2, yscal ;
   double *w1, *w2, *ww, m1, m2 ;  
   int bnum, totnum  ;
   
   if (nrows==0) fatalx("badbug\n") ;

   ZALLOC(w1, nblocks, double) ;
   ZALLOC(w2, nblocks, double) ;
   ZALLOC(ww, nblocks, double) ;
   ZALLOC(wjack, nblocks, double) ;
   ZALLOC(djack, nblocks, double) ;
   ZALLOC(btop, nblocks, double) ;
   ZALLOC(bbot, nblocks, double) ;
   ZALLOC(wtop, nblocks, double) ;
   ZALLOC(wbot, nblocks, double) ;

   
   setjquart(NO, YES, -1.0) ;

   totnum = 0 ;
   for (col=0; col<ncols;  ++col)  {
    cupt = xsnplist[col] ;
    if (cupt -> ignore) continue ;

    bnum = cupt -> tagnumber ; 
    if (bnum<0) continue ;
    if (bnum>=nblocks) fatalx("logic bug\n") ;

      f2sc(&ytop,  &ybot, cupt, indivmarkers, xindex, xtypes, nrows, 2, 0, 1) ;
      if (isnan(ytop)) fatalx("zznan\n") ;
      if (ybot < -0.5) continue ;

      btop[bnum] += ytop ;
      bbot[bnum] += ybot ;
      ++wjack[bnum] ;
      ++totnum  ;
      
   }

    gtop = asum(btop, nblocks) ;
    gbot = asum(bbot, nblocks) ;

    *f2score = mean = gtop/gbot ;

    for (k=0; k<nblocks; k++) {  
     top = btop[k] ; 
     bot = bbot[k] ;
     wtop[k] = gtop-top ; 
     wbot[k] = gbot-bot ;
     wbot[k] += 1.0e-10 ;
     djack[k] = wtop[k]/wbot[k] ;  // delete-block estimate
    }
      
    wjackest(&jest, &jsig, mean, djack, wjack, nblocks) ;

    *f2scoresig = jsig ;

    free(w1) ; 
    free(w2) ; 
    free(ww) ; 

    free(wtop) ; 
    free(wbot) ; 
    free(djack) ;
    free(wjack) ;

    free(btop) ;
    free(bbot) ;

}
Exemplo n.º 30
0
void
dof3score(double *f3score, double *f3scoresig, SNP **xsnplist, int *xindex, int *xtypes, 
 int nrows, int ncols, int numeg, int nblocks) 
{

   int t1, t2 ;
   int a, b, c, d ;
   int c1[2], c2[2], *cc ;
   int *rawcol, *popall, *pop0, *pop1 ;
   int k, g, i, col, j ; 
   double ya, yb, y, jest, jsig, mean ;
   SNP *cupt ;
   double top, bot, *djack, *wjack, gtop, gbot, *wbot, *wtop ;
   double *btop, *bbot, wt ;
   double ytop, ybot ;
   double y1, y2, yscal ;
   double *w1, *w2, *ww, m1, m2 ;  
   int bnum, totnum  ;
   FILE *fff ; 
   double xn[3], xmean[3], xh[3] ;
   
   if (snpdetailsname != NULL) openit(snpdetailsname, &fff, "w") ;
   
   if (nrows==0) fatalx("badbug\n") ;

   ZALLOC(w1, nblocks, double) ;
   ZALLOC(w2, nblocks, double) ;
   ZALLOC(ww, nblocks, double) ;
   ZALLOC(wjack, nblocks, double) ;
   ZALLOC(djack, nblocks, double) ;
   ZALLOC(btop, nblocks, double) ;
   ZALLOC(bbot, nblocks, double) ;
   ZALLOC(wtop, nblocks, double) ;
   ZALLOC(wbot, nblocks, double) ;

   
   setjquart(pubjack, jackweight, jackquart) ;

   totnum = 0 ;
   for (col=0; col<ncols;  ++col)  {
    cupt = xsnplist[col] ;
    if (cupt -> ignore) continue ;

    bnum = cupt -> tagnumber ; 
    if (bnum<0) continue ;
    if (bnum>=nblocks) fatalx("logic bug\n") ;

      f3sc(&ytop,  &ybot, cupt, indivmarkers, xindex, xtypes, nrows, 2, 0, 1) ;
      if (isnan(ytop)) fatalx("zznan\n") ;
      if (ybot < -0.5) continue ;
      if ((dzeromode == NO) && (ybot<=0.001)) continue ;
      if (snpdetailsname != NULL)  {

       finfo(xn+0, xmean+0, xh+0, 0) ; 
       finfo(xn+1, xmean+1, xh+1, 1) ; 
       finfo(xn+2, xmean+2, xh+2, 2) ;  // Sardin Karit CEU

       fprintf(fff, "%20s ", cupt -> ID) ;
       for (a=0; a<=2; ++a) { 
        fprintf(fff, "  %6.0f", xn[a]) ;
        fprintf(fff, " %9.3f", xmean[a]) ;
        fprintf(fff, " %9.3f", xh[a]) ;
       }
       fprintf(fff, "  %9.3f ", ytop) ;         
  //   fprintf(fff, "%9.3f ", ybot/2.0) ;         
       fprintf(fff, " %c ", cupt -> alleles[0]) ;
       fprintf(fff, "%c", cupt -> alleles[1]) ;
       fprintf(fff, "\n") ;
       
        
      }
     
      btop[bnum] += ytop ;
      bbot[bnum] += ybot ;
      ++wjack[bnum] ;
      ++totnum  ;
      
   }

   if (totnum <= 1) fatalx("no data...\n") ;
/**
   printf("totnum: %d ", totnum) ;
   printmat(wjack, 1, 10) ;
*/



    gtop = asum(btop, nblocks) ;
    gbot = asum(bbot, nblocks) ;

    *f3score = mean = gtop/gbot ;

    for (k=0; k<nblocks; k++) {  
     top = btop[k] ; 
     bot = bbot[k] ;
     wtop[k] = gtop-top ; 
     wbot[k] = gbot-bot ;
     wbot[k] += 1.0e-10 ;
     djack[k] = wtop[k]/wbot[k] ;  // delete-block estimate
    }
      
    estjackq(&jest, &jsig, btop, bbot,  wjack, nblocks) ;
    *f3score = jest ;
    *f3scoresig = jsig ;

    free(w1) ; 
    free(w2) ; 
    free(ww) ; 

    free(wtop) ; 
    free(wbot) ; 
    free(djack) ;
    free(wjack) ;

    free(btop) ;
    free(bbot) ;
    if (snpdetailsname != NULL) fclose(fff) ;

}