int main(int argc, char * argv[]) { FSLIO *fslio; FSLIO *fslioOutput; void *buffer; void *outputBuffer; size_t buffsize; char *inputpath = NULL; char *outputpath = NULL; char *maskpath = NULL; int x=-1, y=-1, z=-1, t=0; short xDim, yDim, zDim, vDim; size_t pixtype; short dt; float inter = 0.0, slope = 1.0; BOOL verbose = FALSE; int threads = 1; int ac; if( argc < 2 ) { rsMaskPrintHelp(); return 1; } /* parse parameters */ for( ac = 1; ac < argc; ac++ ) { if( ! strncmp(argv[ac], "-h", 2) ) { rsMaskPrintHelp(); return 1; } else if ( ! strcmp(argv[ac], "-input") ) { if( ++ac >= argc ) { fprintf(stderr, "** missing argument for -input\n"); return 1; } inputpath = argv[ac]; /* no string copy, just pointer assignment */ } else if ( ! strncmp(argv[ac], "-m", 2) ) { if( ++ac >= argc ) { fprintf(stderr, "** missing argument for -m[ask]\n"); return 1; } maskpath = argv[ac]; /* no string copy, just pointer assignment */ } else if ( ! strncmp(argv[ac], "-v", 2) ) { verbose = TRUE; } else if ( ! strcmp(argv[ac], "-output") ) { if( ++ac >= argc ) { fprintf(stderr, "** missing argument for -output\n"); return 1; } outputpath = argv[ac]; /* no string copy, just pointer assignment */ } else if ( ! strcmp(argv[ac], "-threads") ) { if( ++ac >= argc ) { fprintf(stderr, "** missing argument for -threads\n"); return 1; } threads = atoi(argv[ac]); /* no string copy, just pointer assignment */ } else { fprintf(stderr, "\nError, unrecognized command %s\n", argv[ac]); } } rsSetThreadsNum(threads); if ( inputpath == NULL ) { fprintf(stderr, "No input volume specified!\n"); return 1; } if ( outputpath == NULL ) { fprintf(stderr, "No output volume specified!\n"); return 1; } if ( maskpath == NULL ) { fprintf(stderr, "A binary mask must be specified!\n"); return 1; } if ( verbose ) { fprintf(stdout, "Input file: %s\n", inputpath); fprintf(stdout, "Mask file: %s\n", maskpath); fprintf(stdout, "Output file: %s\n", outputpath); } // Load file /* open input file */ fslio = FslOpen(inputpath, "rb"); if (fslio == NULL) { fprintf(stderr, "\nError, could not read header info for %s.\n", inputpath); return 1; } /* determine dimensions */ FslGetDim(fslio, &xDim, &yDim, &zDim, &vDim); if ( verbose ) { fprintf(stdout, "Dim: %d %d %d (%d Volumes)\n", xDim, yDim, zDim, vDim); } if (fslio->niftiptr->scl_slope != 0) { slope = fslio->niftiptr->scl_slope; inter = fslio->niftiptr->scl_inter; } /* determine datatype and initalize buffer */ pixtype = FslGetDataType(fslio, &dt); /* prepare centrality file */ fslioOutput = FslOpen(outputpath, "wb"); if (fslioOutput == NULL) { fprintf(stderr, "\nError, could not read header info for %s.\n", outputpath); return 1; } FslCloneHeader(fslioOutput, fslio); FslSetDim(fslioOutput, xDim, yDim, zDim, vDim); FslSetDimensionality(fslioOutput, 4); FslSetDataType(fslioOutput, dt); char *callString = rsMergeStringArray(argc, argv); rsWriteNiftiHeader(fslioOutput, callString); free(callString); /* load mask */ unsigned long nPoints = 0L; double ***mask = d3matrix(zDim-1, yDim-1, xDim-1); Point3D *maskPoints = rsReadMask(maskpath, xDim, yDim, zDim, &nPoints, NULL, fslio, mask); if ( maskPoints == NULL) { fprintf(stderr, "\nError: Mask invalid.\n"); FslClose(fslio); FslClose(fslioOutput); return 1; } // Prepare buffer buffsize = rsGetBufferSize(xDim, yDim, zDim, vDim, dt); buffer = malloc(buffsize); outputBuffer = malloc(buffsize); if (buffer == NULL || outputBuffer == NULL) { fprintf(stdout, "Not enough free memory :-(\n"); return 1; } FslReadVolumes(fslio, buffer, vDim); rsResetBufferToValue(dt, outputBuffer, slope, inter, xDim, yDim, zDim, vDim, log(-1)); long p; #pragma omp parallel num_threads(rsGetThreadsNum()) private(p) shared(buffer,fslio) { #pragma omp for schedule(guided, 1) for (p = 0L; p<nPoints; p++) { const Point3D *point = &maskPoints[p]; double *pointValues = (double*)malloc(sizeof(double)*vDim); rsExtractTimecourseFromBuffer(dt, pointValues, buffer, slope, inter, point, xDim, yDim, zDim, vDim); rsWriteTimecourseToBuffer(dt, pointValues, outputBuffer, slope, inter, point, xDim, yDim, zDim, vDim); free(pointValues); } } /* write output */ free(buffer); FslWriteVolumes(fslioOutput, outputBuffer, vDim); FslClose(fslioOutput); free(outputBuffer); free(fslioOutput); free(maskPoints); }
int main() { int i,z,d,w; int billID, wordID, freq; double sum; double likelihood[10]; FILE *fp1 = fopen("../data/Bill_Term", "r"); FILE *fp2 = fopen("../data/Term_Bill", "r"); FILE *fpwrite = fopen("../Result/result6479/10topic_NoSeeds_zw", "w"); FILE *fpwrite2 = fopen("../Result/result6479/10topic_NoSeeds_zd", "w"); // initialize double **n_dw1 = dmatrix(N_Bill_Term, 3); double **n_dw2 = dmatrix(N_Bill_Term, 3); double ***p_z_d = d3matrix(10, K, D); double ***p_w_z = d3matrix(10, V, K); // initialize n(d,w), p(z|d), p(w|z) i = 0; while (!feof(fp1)) { fscanf(fp1, "%d\t%d\t%d\n", &billID, &wordID, &freq); // n_dw[billID-1][wordID-1] = freq; n_dw1[i][0] = billID; n_dw1[i][1] = wordID; n_dw1[i][2] = freq; i++; } printf("%s%d\n%s%d\n", "i = ", i, "N_Bill_Term = ", N_Bill_Term); i = 0; while (!feof(fp2)) { fscanf(fp2, "%d\t%d\t%d\n", &billID, &wordID, &freq); n_dw2[i][0] = wordID; n_dw2[i][1] = billID; n_dw2[i][2] = freq; i++; } printf("%s%d\n%s%d\n", "i = ", i, "N_Bill_Term = ", N_Bill_Term); srand((unsigned)time(NULL)); for (i = 0; i < 10; i++) { for (d = 0; d < D; d++) { for (z = 0; z < K; z++) p_z_d[i][z][d] = rand() / (double)RAND_MAX; // p(z|d) // Normalization sum = 0; for (z = 0; z < K; z++) sum += p_z_d[i][z][d]; if (sum != 0) for (z = 0; z < K; z++) p_z_d[i][z][d] /= sum; else printf("%s\n", "Need initialize again."); sum = 0; for (z = 0; z < K; z++) sum += p_z_d[i][z][d]; if (fabs(sum-1)>0.0001) printf("%s\n", "abs(sum-1)>0.0001, Need initialize again."); } } for (i = 0; i < 10; i++) { for (z = 0; z < K; z++) { for (w = 0; w < V; w++) p_w_z[i][w][z] = rand() / (double)RAND_MAX; // p(w|z) //Normalization sum = 0; for (w = 0; w < V; w++) sum += p_w_z[i][w][z]; if (sum != 0) for (w = 0; w < V; w++) p_w_z[i][w][z] /= sum; else printf("%s\n", "Need initialize again."); sum = 0; for (w = 0; w < V; w++) sum += p_w_z[i][w][z]; if (fabs(sum-1)>0.0001) printf("%s\n", "abs(sum-1)>0.0001, Need initialize again."); } } // select the best one, after 50 iterations for (i = 0; i < 10; i++) { printf("%s%d\n", "Initialization ", i); pLSA(n_dw1, n_dw2, p_w_z[i], p_z_d[i], 50); likelihood[i] = calLikelihood(n_dw1, p_z_d[i], p_w_z[i]); printf("%s%d%s%f\n", "Likelihood", i, " is ", likelihood[i]); } int ml = 0; for (i = 1; i < 10; i++) if (likelihood[i] > likelihood[ml]) ml = i; printf("%s%d\n", "ml = ", ml); pLSA(n_dw1, n_dw2, p_w_z[ml], p_z_d[ml], 450); // output printf("%s\n", "Writing to file..."); // d: person, z: topic, w: bill // topic-word printf("%s\n", "p(w|z)"); for (z = 0; z < K; z++) { sum = 0; for (w = 0; w < V; w++) { sum += p_w_z[ml][w][z]; fprintf(fpwrite, "%.12lf", p_w_z[ml][w][z]); if (w != V-1) fprintf(fpwrite, "%s", "\t"); } printf("%lf\n", sum); fprintf(fpwrite, "%s", "\n"); } // topic-bill printf("%s\n", "p(z|d)"); for (z = 0; z < K; z++) { for (d = 0; d < D; d++) { fprintf(fpwrite2, "%.12lf", p_z_d[ml][z][d]); if (d != D-1) fprintf(fpwrite2, "%s", "\t"); } fprintf(fpwrite2, "%s", "\n"); } // clear fclose(fp1); fclose(fp2); fclose(fpwrite); fclose(fpwrite2); free_dmatrix(n_dw1, N_Bill_Term); free_dmatrix(n_dw2, N_Bill_Term); free_d3matrix(p_z_d, 10, K); free_d3matrix(p_w_z, 10, V); return 0; }