void DataSet::loadstim2(){ name = "fMRI_stim"; type = FMRI_STIM; dims[3] = 220; dims[0] = 3; dims[1] = 1; dims[2] = 1; train = gsl_matrix_float_calloc(dims[3], dims[0]*dims[1]); std::cout << "Loading stimulus" << std::endl; for (int i = 0; i < 220; ++i) { if (i%55 < 15) gsl_matrix_float_set(train, i, 0, 1); else if ((i - 20)%55 < 15 && i >= 20) gsl_matrix_float_set(train, i, 1, 1); else gsl_matrix_float_set(train, i, 2, 1); } }
VAttrList VNCM(VAttrList list,VImage mask,VShort minval,VShort first,VShort length,VFloat threshold) { VAttrList out_list=NULL; VAttrListPosn posn; VImage src[NSLICES],map=NULL; VImage dest=NULL; size_t b,r,c,i,j,i1,n,m,nt,last,ntimesteps,nrows,ncols,nslices; gsl_matrix_float *mat=NULL; float *A=NULL,*ev=NULL; /* ** get image dimensions */ i = ntimesteps = nrows = ncols = 0; for (VFirstAttr (list, & posn); VAttrExists (& posn); VNextAttr (& posn)) { if (VGetAttrRepn (& posn) != VImageRepn) continue; VGetAttrValue (& posn, NULL,VImageRepn, & src[i]); if (VPixelRepn(src[i]) != VShortRepn) continue; if (VImageNBands(src[i]) > ntimesteps) ntimesteps = VImageNBands(src[i]); if (VImageNRows(src[i]) > nrows) nrows = VImageNRows(src[i]); if (VImageNColumns(src[i]) > ncols) ncols = VImageNColumns(src[i]); i++; if (i >= NSLICES) VError(" too many slices"); } nslices = i; /* get time steps to include */ if (length < 1) length = ntimesteps-2; last = first + length -1; if (last >= ntimesteps) last = ntimesteps-1; if (first < 0) first = 1; nt = last - first + 1; i1 = first+1; if (nt < 2) VError(" not enough timesteps, nt= %d",nt); fprintf(stderr,"# ntimesteps: %d, first= %d, last= %d, nt= %d\n", (int)ntimesteps,(int)first,(int)last,(int)nt); /* count number of voxels */ n = 0; for (b=0; b<nslices; b++) { if (VImageNRows(src[b]) < 2) continue; for (r=0; r<nrows; r++) { for (c=0; c<ncols; c++) { if (VPixel(src[b],i1,r,c,VShort) < minval) continue; if (VGetPixel(mask,b,r,c) < 0.5) continue; n++; } } } fprintf(stderr," nvoxels: %ld\n",(long)n); /* ** voxel addresses */ map = VCreateImage(1,5,n,VFloatRepn); if (map == NULL) VError(" error allocating addr map"); VFillImage(map,VAllBands,0); VPixel(map,0,3,0,VFloat) = nslices; VPixel(map,0,3,1,VFloat) = nrows; VPixel(map,0,3,2,VFloat) = ncols; i = 0; for (b=0; b<nslices; b++) { if (VImageNRows(src[b]) < 2) continue; for (r=0; r<nrows; r++) { for (c=0; c<ncols; c++) { if (VPixel(src[b],i1,r,c,VShort) < minval) continue; if (VGetPixel(mask,b,r,c) < 0.5) continue; VPixel(map,0,0,i,VFloat) = b; VPixel(map,0,1,i,VFloat) = r; VPixel(map,0,2,i,VFloat) = c; i++; } } } /* ** avoid casting to float, copy data to matrix */ mat = gsl_matrix_float_calloc(n,nt); for (i=0; i<n; i++) { b = VPixel(map,0,0,i,VFloat); r = VPixel(map,0,1,i,VFloat); c = VPixel(map,0,2,i,VFloat); float *ptr = gsl_matrix_float_ptr(mat,i,0); int k; j = 0; for (k=first; k<=last; k++) { if (j >= mat->size2) VError(" j= %d %d",j,mat->size2); if (k >= VImageNBands(src[b])) VError(" k= %d %d",k, VImageNBands(src[b])); *ptr++ = (float) VPixel(src[b],k,r,c,VShort); j++; } } /* ** compute similarity matrix */ m = (n*(n+1))/2; fprintf(stderr," matrix computation, n= %ld...\n",(long)n); A = (float *) calloc(m,sizeof(float)); if (!A) VError(" err allocating correlation matrix"); memset(A,0,m*sizeof(float)); size_t progress=0; #pragma omp parallel for shared(progress) private(j) schedule(guided) firstprivate(mat,A) for (i=0; i<n; i++) { if (i%100 == 0) fprintf(stderr," %d00\r",(int)(++progress)); const float *arr1 = gsl_matrix_float_const_ptr(mat,i,0); for (j=0; j<i; j++) { const float *arr2 = gsl_matrix_float_const_ptr(mat,j,0); const double v = Correlation(arr1,arr2,nt); const size_t k=j+i*(i+1)/2; if (k >= m) VError(" illegal addr k= %d, m= %d",k,m); A[k] = (float)v; } } fprintf(stderr," matrix done.\n"); gsl_matrix_float_free(mat); /* ** eigenvector centrality */ ev = (float *) VCalloc(n,sizeof(float)); DegreeCentrality(A,ev,n,threshold); dest = WriteOutput(src[0],map,nslices,nrows,ncols,ev,n); VSetAttr(VImageAttrList(dest),"name",NULL,VStringRepn,"DegreeCM"); out_list = VCreateAttrList(); VAppendAttr(out_list,"image",NULL,VImageRepn,dest); return out_list; }
float * VCorrMatrix(VAttrList list,VImage map,VShort first,VShort length) { VAttrListPosn posn; VImage src[NSLICES]; size_t b,r,c,i,j,i1,n,m,nt,last,ntimesteps,nrows,ncols,nslices; gsl_matrix_float *mat=NULL; float *A=NULL; /* ** get image dimensions */ i = ntimesteps = nrows = ncols = 0; for (VFirstAttr (list, & posn); VAttrExists (& posn); VNextAttr (& posn)) { if (VGetAttrRepn (& posn) != VImageRepn) continue; VGetAttrValue (& posn, NULL,VImageRepn, & src[i]); if (VPixelRepn(src[i]) != VShortRepn) continue; if (VImageNBands(src[i]) > ntimesteps) ntimesteps = VImageNBands(src[i]); if (VImageNRows(src[i]) > nrows) nrows = VImageNRows(src[i]); if (VImageNColumns(src[i]) > ncols) ncols = VImageNColumns(src[i]); i++; if (i >= NSLICES) VError(" too many slices"); } nslices = i; /* get time steps to include */ if (length < 1) length = ntimesteps-2; last = first + length -1; if (last >= ntimesteps) last = ntimesteps-1; if (first < 0) first = 1; nt = last - first + 1; i1 = first+1; if (nt < 2) VError(" not enough timesteps, nt= %d",nt); /* number of voxels */ n = VImageNColumns(map); fprintf(stderr," ntimesteps: %d, first= %d, last= %d, nt= %d, nvoxels: %ld\n", (int)ntimesteps,(int)first,(int)last,(int)nt,(long)n); /* ** avoid casting to float, copy data to matrix */ mat = gsl_matrix_float_calloc(n,nt); if (!mat) VError(" err allocating mat"); for (i=0; i<n; i++) { b = VPixel(map,0,0,i,VShort); r = VPixel(map,0,1,i,VShort); c = VPixel(map,0,2,i,VShort); float *ptr = gsl_matrix_float_ptr(mat,i,0); int k; j = 0; for (k=first; k<=last; k++) { if (j >= mat->size2) VError(" j= %d %d",j,mat->size2); if (k >= VImageNBands(src[b])) VError(" k= %d %d",k, VImageNBands(src[b])); *ptr++ = (float) VPixel(src[b],k,r,c,VShort); j++; } } /* ** compute similarity matrix */ m = (n*(n+1))/2; fprintf(stderr," compute correlation matrix...\n"); A = (float *) VCalloc(m,sizeof(float)); if (!A) VError(" err allocating correlation matrix"); memset(A,0,m*sizeof(float)); size_t progress=0; #pragma omp parallel for shared(progress) private(j) schedule(guided) firstprivate(mat,A) for (i=0; i<n; i++) { if (i%100 == 0) fprintf(stderr," %d00\r",(int)(++progress)); const float *arr1 = gsl_matrix_float_const_ptr(mat,i,0); for (j=0; j<i; j++) { const float *arr2 = gsl_matrix_float_const_ptr(mat,j,0); const double v = Correlation(arr1,arr2,nt); const size_t k=j+i*(i+1)/2; if (k >= m) VError(" illegal addr k= %d, m= %d",k,m); A[k] = v; } } fprintf(stderr," matrix done.\n"); gsl_matrix_float_free(mat); return A; }
VImage SimilarityMatrix(VAttrList list, VImage mask, VShort minval, VShort fisher) { VAttrListPosn posn; VImage src[NSLICES], dest = NULL, map = NULL; int b, r, c, i, j, n, ntimesteps, nrows, ncols, nslices; double u, x, y, smin, smax, tiny = 1.0e-6; gsl_matrix_float *mat = NULL; /* ** get image dimensions */ i = ntimesteps = nrows = ncols = 0; for(VFirstAttr(list, & posn); VAttrExists(& posn); VNextAttr(& posn)) { if(VGetAttrRepn(& posn) != VImageRepn) continue; VGetAttrValue(& posn, NULL, VImageRepn, & src[i]); if(VPixelRepn(src[i]) != VShortRepn) continue; if(VImageNBands(src[i]) > ntimesteps) ntimesteps = VImageNBands(src[i]); if(VImageNRows(src[i]) > nrows) nrows = VImageNRows(src[i]); if(VImageNColumns(src[i]) > ncols) ncols = VImageNColumns(src[i]); i++; if(i >= NSLICES) VError(" too many slices"); } nslices = i; n = 0; for(b = 0; b < nslices; b++) { if(VImageNRows(src[b]) < 2) continue; for(r = 0; r < nrows; r++) { for(c = 0; c < ncols; c++) { if(VPixel(src[b], 0, r, c, VShort) < minval) continue; if(VGetPixel(mask, b, r, c) < 0.1) continue; n++; } } } fprintf(stderr, " n: %d\n", n); /* ** voxel addresses */ map = VCreateImage(1, 5, n, VFloatRepn); VFillImage(map, VAllBands, 0); VPixel(map, 0, 3, 0, VFloat) = nslices; VPixel(map, 0, 3, 1, VFloat) = nrows; VPixel(map, 0, 3, 2, VFloat) = ncols; i = 0; for(b = 0; b < nslices; b++) { if(VImageNRows(src[b]) < 2) continue; for(r = 0; r < nrows; r++) { for(c = 0; c < ncols; c++) { if(VPixel(src[b], 0, r, c, VShort) < minval) continue; if(VGetPixel(mask, b, r, c) < 0.1) continue; VPixel(map, 0, 0, i, VFloat) = b; VPixel(map, 0, 1, i, VFloat) = r; VPixel(map, 0, 2, i, VFloat) = c; i++; } } } /* ini output image */ if(n < 8000) dest = VCreateImage(1, n, n, VFloatRepn); else if(n >= 8000 && n < 10000) dest = VCreateImage(1, n, n, VShortRepn); else dest = VCreateImage(1, n, n, VSByteRepn); VFillImage(dest, VAllBands, 0); smin = VPixelMinValue(dest); smax = VPixelMaxValue(dest); /* ** avoid casting to float, copy data to matrix */ mat = gsl_matrix_float_calloc(n, ntimesteps); for(i = 0; i < n; i++) { b = VPixel(map, 0, 0, i, VFloat); r = VPixel(map, 0, 1, i, VFloat); c = VPixel(map, 0, 2, i, VFloat); float *ptr = gsl_matrix_float_ptr(mat, i, 0); int k; j = 0; for(k = 0; k < ntimesteps; k++) { if(j >= mat->size2) VError(" j= %d %d", j, mat->size2); if(k >= VImageNBands(src[b])) VError(" k= %d %d", k, VImageNBands(src[b])); *ptr++ = (float) VPixel(src[b], k, r, c, VShort); j++; } } /* ** main process loop */ for(i = 0; i < n; i++) { if(i % 50 == 0) fprintf(stderr, " i: %4d\r", i); const float *arr1 = gsl_matrix_float_const_ptr(mat, i, 0); for(j = 0; j <= i; j++) { const float *arr2 = gsl_matrix_float_const_ptr(mat, j, 0); u = Correlation(arr1, arr2, ntimesteps); /* Fisher r-to-z transform */ if(fisher) { x = 1 + u; y = 1 - u; if(x < tiny) x = tiny; if(y < tiny) y = tiny; u = 0.5 * log(x / y); } if(VPixelRepn(dest) != VFloatRepn) { u *= smax; if(u < smin) u = smin; if(u > smax) u = smax; } VSetPixel(dest, 0, i, j, u); VSetPixel(dest, 0, j, i, u); } } VCopyImageAttrs(src[0], dest); VSetAttr(VImageAttrList(dest), "similarity_metric", NULL, VStringRepn, "corr_pearson"); VSetAttr(VImageAttrList(dest), "name", NULL, VStringRepn, "similarity_matrix"); VSetAttr(VImageAttrList(dest), "map", NULL, VImageRepn, map); return dest; }