static int assemble_training_data_and_free_mris(MRI_SURFACE *mris[MAX_SUBJECTS], MRI *mri_overlays[MAX_SUBJECTS][MAX_OVERLAYS], int nsubjects, int noverlays, int **ptraining_classes, double ***ptraining_data, int *pntraining) { int sno, i, ntraining, *training_classes, x, y, z, f, vno, tno ; double **training_data ; for (ntraining = sno = 0 ; sno < nsubjects ; sno++) ntraining += MRISvalidVertices(mris[sno]) ; printf("%2.1fM total training voxels found\n", (float)ntraining/(1024*1024.0)) ; training_classes = (int *)calloc(ntraining, sizeof(int)) ; if (training_classes == NULL) ErrorExit(ERROR_NOFILE, "%s: could not allocate %d-len training class vector", Progname, ntraining) ; training_data = (double **)calloc(ntraining, sizeof(double *)) ; if (training_data == NULL) ErrorExit(ERROR_NOFILE, "%s: could not allocate %d-len training data vector", Progname, ntraining) ; for (tno = sno = 0 ; sno < nsubjects ; sno++) { for (vno = x = 0 ; x < mri_overlays[sno][0]->width ; x++) for (y = 0 ; y < mri_overlays[sno][0]->height ; y++) for (z = 0 ; z < mri_overlays[sno][0]->depth ; z++) for (f = 0 ; f < mri_overlays[sno][0]->nframes ; f++, vno++) { if (tno == Gdiag_no) DiagBreak() ; if (vno == Gdiag_no) DiagBreak(); if (mris[sno]->vertices[vno].ripflag) continue ; // not in cortex training_classes[tno] = mris[sno]->vertices[vno].marked ; training_data[tno] = (double *)calloc(noverlays, sizeof(double)) ; if (training_data[tno] == NULL) ErrorExit(ERROR_NOFILE, "%s: could not allocate %d-len training data vector [%d]", Progname, noverlays, tno) ; for (i = 0 ; i < noverlays ; i++) training_data[tno][i] = MRIgetVoxVal(mri_overlays[sno][i], x, y, z, f) ; tno++ ; } for (i = 0 ; i < noverlays ; i++) MRIfree(&mri_overlays[sno][i]) ; MRISfree(&mris[sno]) ; } *pntraining = ntraining ; *ptraining_classes = training_classes ; *ptraining_data = training_data ; return(NO_ERROR) ; }
int main(int argc, char *argv[]) { char **av, in_surf_fname[STRLEN], *in_patch_fname, *out_patch_fname, hemi[STRLEN] ; int ac, nargs; char path[STRLEN], out_surf_fname[STRLEN], *cp ; int msec, minutes, seconds ; struct timeb start ; MRI_SURFACE *mris_in, *mris_out ; /* rkt: check for and handle version tag */ nargs = handle_version_option (argc, argv, "$Id: mris_map_cuts.c,v 1.3 2011/03/02 00:04:33 nicks Exp $", "$Name: $"); if (nargs && argc - nargs == 1) exit (0); argc -= nargs; Progname = argv[0] ; ErrorInit(NULL, NULL, NULL) ; DiagInit(NULL, NULL, NULL) ; TimerStart(&start) ; ac = argc ; av = argv ; for ( ; argc > 1 && ISOPTION(*argv[1]) ; argc--, argv++) { nargs = get_option(argc, argv) ; argc -= nargs ; argv += nargs ; } if (argc < 3) usage_exit(1) ; in_patch_fname = argv[1] ; out_patch_fname = argv[2] ; FileNamePath(in_patch_fname, path) ; cp = strrchr(in_patch_fname, '/') ; if (!cp) cp = in_patch_fname ; cp = strchr(cp, '.') ; if (cp) { strncpy(hemi, cp-2, 2) ; hemi[2] = 0 ; } else strcpy(hemi, "lh") ; sprintf(in_surf_fname, "%s/%s.%s", path, hemi, orig_surf_name) ; FileNamePath(out_patch_fname, path) ; cp = strrchr(out_patch_fname, '/') ; if (!cp) cp = out_patch_fname ; cp = strchr(cp, '.') ; if (cp) { strncpy(hemi, cp-2, 2) ; hemi[2] = 0 ; } else strcpy(hemi, "lh") ; sprintf(out_surf_fname, "%s/%s.%s", path, hemi, orig_surf_name) ; mris_in = MRISread(in_surf_fname) ; mris_out = MRISread(out_surf_fname) ; MRISsaveVertexPositions(mris_in, CANONICAL_VERTICES) ; MRISsaveVertexPositions(mris_out, CANONICAL_VERTICES) ; if (MRISreadVertexPositions(mris_out, inf_surf_name) != NO_ERROR) ErrorExit(ERROR_BADPARM, "%s: could not inflated surface %s", Progname, inf_surf_name) ; if (MRISreadPatch(mris_in, in_patch_fname) != NO_ERROR) ErrorExit(ERROR_BADPARM, "%s: could not read patch file %s", Progname, in_patch_fname) ; MRISmapCuts(mris_in, mris_out) ; if (dilate) { printf("dilating patch %d times\n", dilate) ; MRISdilateRipped(mris_out, dilate) ; printf("%d valid vertices (%2.1f %% of total)\n", MRISvalidVertices(mris_out), 100.0*MRISvalidVertices(mris_out)/mris_out->nvertices) ; } printf("writing output to %s\n", out_patch_fname) ; MRISwritePatch(mris_out, out_patch_fname) ; msec = TimerStop(&start) ; seconds = nint((float)msec/1000.0f) ; minutes = seconds / 60 ; seconds = seconds % 60 ; fprintf(stderr, "cut mapping took %d minutes" " and %d seconds.\n", minutes, seconds) ; exit(0) ; return(0) ; }
int main(int argc, char *argv[]) { char **av, in_surf_fname[STRLEN], *in_patch_fname, *out_patch_fname, fname[STRLEN], path[STRLEN], *cp, hemi[10] ; int ac, nargs ; MRI_SURFACE *mris ; MRI *mri_vertices ; /* rkt: check for and handle version tag */ nargs = handle_version_option (argc, argv, "$Id: mris_flatten.c,v 1.42 2016/12/10 22:57:46 fischl Exp $", "$Name: $"); if (nargs && argc - nargs == 1) exit (0); argc -= nargs; Gdiag |= DIAG_SHOW ; Progname = argv[0] ; ErrorInit(NULL, NULL, NULL) ; DiagInit(NULL, NULL, NULL) ; Gdiag |= (DIAG_SHOW | DIAG_WRITE) ; memset(&parms, 0, sizeof(parms)) ; parms.dt = .1 ; parms.projection = PROJECT_PLANE ; parms.tol = 0.2 ; parms.n_averages = 1024 ; parms.l_dist = 1.0 ; parms.l_nlarea = 1.0 ; parms.niterations = 40 ; parms.area_coef_scale = 1.0 ; parms.dt_increase = 1.01 /* DT_INCREASE */; parms.dt_decrease = 0.98 /* DT_DECREASE*/ ; parms.error_ratio = 1.03 /*ERROR_RATIO */; parms.integration_type = INTEGRATE_LINE_MINIMIZE ; parms.momentum = 0.9 ; parms.desired_rms_height = -1.0 ; parms.base_name[0] = 0 ; parms.nbhd_size = 7 ; /* out to 7-connected neighbors */ parms.max_nbrs = 12 ; /* 12 at each distance */ ac = argc ; av = argv ; for ( ; argc > 1 && ISOPTION(*argv[1]) ; argc--, argv++) { nargs = get_option(argc, argv) ; argc -= nargs ; argv += nargs ; } if (argc < 3) print_help() ; parms.base_dt = base_dt_scale * parms.dt ; in_patch_fname = argv[1] ; out_patch_fname = argv[2] ; FileNamePath(in_patch_fname, path) ; cp = strrchr(in_patch_fname, '/') ; if (!cp) cp = in_patch_fname ; cp = strchr(cp, '.') ; if (cp) { strncpy(hemi, cp-2, 2) ; hemi[2] = 0 ; } else strcpy(hemi, "lh") ; if (one_surf_flag) sprintf(in_surf_fname, "%s", in_patch_fname) ; else sprintf(in_surf_fname, "%s/%s.%s", path, hemi, original_surf_name) ; if (parms.base_name[0] == 0) { FileNameOnly(out_patch_fname, fname) ; cp = strchr(fname, '.') ; if (cp) strcpy(parms.base_name, cp+1) ; else strcpy(parms.base_name, "flattened") ; } mris = MRISread(in_surf_fname) ; if (!mris) ErrorExit(ERROR_NOFILE, "%s: could not read surface file %s", Progname, in_surf_fname) ; if (sphere_flag) { MRIScenter(mris, mris) ; mris->radius = MRISaverageRadius(mris) ; MRISstoreMetricProperties(mris) ; MRISsaveVertexPositions(mris, ORIGINAL_VERTICES) ; } if (Gdiag_no >= 0) { int n ; printf("vertex %d has %d nbrs before patch:\n", Gdiag_no, mris->vertices[Gdiag_no].vnum) ; for (n = 0 ; n < mris->vertices[Gdiag_no].vnum ; n++) printf("\t%d\n", mris->vertices[Gdiag_no].v[n]) ; } if (one_surf_flag) /* only have the 1 surface - no patch file */ { mris->patch = 1 ; mris->status = MRIS_PATCH ; if (!FEQUAL(rescale,1)) { MRISscaleBrain(mris, mris, rescale) ; MRIScomputeMetricProperties(mris) ; } MRISstoreMetricProperties(mris) ; MRISsaveVertexPositions(mris, ORIGINAL_VERTICES) ; } else { MRISresetNeighborhoodSize(mris, mris->vertices[0].nsize) ; // set back to max if (label_fname) // read in a label instead of a patch { LABEL *area ; area = LabelRead(NULL, label_fname) ; if (area == NULL) ErrorExit(ERROR_BADPARM, "%s: could not read label file %s", Progname, label_fname) ; LabelDilate(area, mris, dilate_label, CURRENT_VERTICES) ; MRISclearMarks(mris) ; LabelMark(area, mris) ; MRISripUnmarked(mris) ; MRISripFaces(mris); mris->patch = 1 ; mris->status = MRIS_CUT ; LabelFree(&area) ; printf("%d valid vertices (%2.1f %% of total)\n", MRISvalidVertices(mris), 100.0*MRISvalidVertices(mris)/mris->nvertices) ; } else { if (MRISreadPatch(mris, in_patch_fname) != NO_ERROR) ErrorExit(ERROR_BADPARM, "%s: could not read patch file %s", Progname, in_patch_fname) ; if (dilate) { printf("dilating patch %d times\n", dilate) ; MRISdilateRipped(mris, dilate) ; printf("%d valid vertices (%2.1f %% of total)\n", MRISvalidVertices(mris), 100.0*MRISvalidVertices(mris)/mris->nvertices) ; } } MRISremoveRipped(mris) ; MRISupdateSurface(mris) ; #if 0 mris->nsize = 1 ; // before recalculation of 2 and 3-nbrs { int vno ; VERTEX *v ; for (vno= 0 ; vno < mris->nvertices ; vno++) { v = &mris->vertices[vno] ; v->vtotal = v->vnum ; v->nsize = 1 ; } } MRISsetNeighborhoodSize(mris, nbrs) ; #endif } if (Gdiag_no >= 0) printf("vno %d is %sin patch\n", Gdiag_no, mris->vertices[Gdiag_no].ripflag ? "NOT " : "") ; if (Gdiag_no >= 0 && mris->vertices[Gdiag_no].ripflag == 0) { int n ; printf("vertex %d has %d nbrs after patch:\n", Gdiag_no, mris->vertices[Gdiag_no].vnum) ; for (n = 0 ; n < mris->vertices[Gdiag_no].vnum ; n++) printf("\t%d\n", mris->vertices[Gdiag_no].v[n]) ; } fprintf(stderr, "reading original vertex positions...\n") ; if (!FZERO(disturb)) mrisDisturbVertices(mris, disturb) ; if (parms.niterations > 0) { MRISresetNeighborhoodSize(mris, nbrs) ; if (!FZERO(parms.l_unfold) || !FZERO(parms.l_expand)) { static INTEGRATION_PARMS p2 ; sprintf(in_surf_fname, "%s/%s.%s", path, hemi, original_surf_name) ; if (stricmp(original_unfold_surf_name,"none") == 0) { printf("using current position of patch as initial position\n") ; MRISstoreMetricProperties(mris) ; /* use current positions */ } else if (!sphere_flag && !one_surf_flag) MRISreadOriginalProperties(mris, original_unfold_surf_name) ; *(&p2) = *(&parms) ; p2.l_dist = 0 ; p2.niterations = 100 ; p2.nbhd_size = p2.max_nbrs = 1 ; p2.n_averages = 0 ; p2.write_iterations = parms.write_iterations > 0 ? 25 : 0 ; p2.tol = -1 ; p2.dt = 0.5 ; p2.l_area = 0.0 ; p2.l_spring = 0.9 ; p2.l_convex = 0.9 ; p2.momentum = 0 ; p2.integration_type = INTEGRATE_MOMENTUM ; MRISrestoreVertexPositions(mris, ORIGINAL_VERTICES) ; #if 0 p2.flags |= IPFLAG_NO_SELF_INT_TEST ; printf("expanding surface....\n") ; MRISexpandSurface(mris, 4.0, &p2) ; // push it away from fissure #endif p2.niterations = 100 ; MRISunfold(mris, &p2, 1) ; p2.niterations = 300 ; p2.l_unfold *= 0.25 ; MRISunfold(mris, &p2, 1) ; p2.l_unfold *= 0.25 ; MRISunfold(mris, &p2, 1) ; #if 0 printf("smoothing unfolded surface..\n"); p2.niterations = 200 ; p2.l_unfold = 0 ; // just smooth it MRISunfold(mris, &p2, max_passes) ; #endif parms.start_t = p2.start_t ; parms.l_unfold = parms.l_convex = parms.l_boundary = parms.l_expand=0 ; MRIfree(&parms.mri_dist) ; } sprintf(in_surf_fname, "%s/%s.%s", path, hemi, original_surf_name) ; if (!sphere_flag && !one_surf_flag) MRISreadOriginalProperties(mris, original_surf_name) ; if (randomly_flatten) MRISflattenPatchRandomly(mris) ; else MRISflattenPatch(mris) ; /* optimize metric properties of flat map */ fprintf(stderr,"minimizing metric distortion induced by projection...\n"); MRISscaleBrain(mris, mris, scale) ; MRIScomputeMetricProperties(mris) ; MRISunfold(mris, &parms, max_passes) ; MRIScenter(mris, mris) ; fprintf(stderr, "writing flattened patch to %s\n", out_patch_fname) ; MRISwritePatch(mris, out_patch_fname) ; } if (plane_flag || sphere_flag) { char fname[STRLEN] ; FILE *fp ; #if 0 sprintf(fname, "%s.%s.out", mris->hemisphere == RIGHT_HEMISPHERE ? "rh" : "lh", parms.base_name); #else sprintf(fname, "flatten.log") ; #endif fp = fopen(fname, "a") ; if (plane_flag) MRIScomputeAnalyticDistanceError(mris, MRIS_PLANE, fp) ; else if (sphere_flag) MRIScomputeAnalyticDistanceError(mris, MRIS_SPHERE, fp) ; fclose(fp) ; } if (mri_overlay) { MRI *mri_flattened ; char fname[STRLEN] ; // if it is NxNx1x1 reshape it to be Nx1x1xN if ( mri_overlay->width == mri_overlay->height && mri_overlay->depth == 1 && mri_overlay->nframes == 1) { MRI *mri_tmp ; printf("reshaping to move 2nd dimension to time\n") ; mri_tmp = mri_reshape( mri_overlay, mri_overlay->width, 1, 1, mri_overlay->height); MRIfree( &mri_overlay ); mri_overlay = mri_tmp; } // put in some special code that knows about icosahedra if (mris->nvertices == 163842 || // ic7 mris->nvertices == 40962 || // ic6 mris->nvertices == 10242 || // ic5 mris->nvertices == 2562) // ic4 { int nvals, start_index, end_index ; MRI *mri_tmp ; printf("cross-hemispheric correlation matrix detected, reshaping...\n") ; nvals = mri_overlay->width * mri_overlay->height * mri_overlay->depth ; if (nvals == 2*mris->nvertices) // it's a corr matrix for both hemis { if (mris->hemisphere == LEFT_HEMISPHERE || mris->hemisphere == RIGHT_HEMISPHERE) { if (mris->hemisphere == LEFT_HEMISPHERE) { start_index = 0 ; end_index = mris->nvertices-1 ; } else { start_index = mris->nvertices ; end_index = 2*mris->nvertices-1 ; } mri_tmp = MRIextract(mri_overlay, NULL, start_index, 0, 0, mris->nvertices, 1, 1) ; MRIfree(&mri_overlay) ; mri_overlay = mri_tmp; } else // both hemis { } } } printf("resampling overlay (%d x %d x %d x %d) into flattened coordinates..\n", mri_overlay->width, mri_overlay->height, mri_overlay->depth, mri_overlay->nframes) ; if (synth_name) { LABEL *area_lh, *area_rh ; char fname[STRLEN], path[STRLEN], fname_no_path[STRLEN] ; int vno, n, vno2, n2 ; MRIsetValues(mri_overlay, 0) ; FileNameOnly(synth_name, fname_no_path) ; FileNamePath(synth_name, path) ; sprintf(fname, "%s/lh.%s", path, fname_no_path) ; area_lh = LabelRead(NULL, fname) ; if (area_lh == NULL) ErrorExit(ERROR_NOFILE, "%s: could not read label from %s", Progname,fname) ; sprintf(fname, "%s/rh.%s", path, fname_no_path) ; area_rh = LabelRead(NULL, fname) ; if (area_rh == NULL) ErrorExit(ERROR_NOFILE, "%s: could not read label from %s", Progname,fname) ; #if 0 for (n = 0 ; n < area_lh->n_points ; n++) { vno = area_lh->lv[n].vno ; MRIsetVoxVal(mri_overlay, vno, 0, 0, vno, 1) ; printf("synthesizing map with vno %d: (%2.1f, %2.1f)\n", vno, mris->vertices[vno].x, mris->vertices[vno].y) ; break ; } #else for (n = 0 ; n < area_lh->n_points ; n++) { vno = area_lh->lv[n].vno ; if (vno >= 0) { for (n2 = 0 ; n2 < area_lh->n_points ; n2++) { vno2 = area_lh->lv[n2].vno ; if (vno2 >= 0) MRIsetVoxVal(mri_overlay, vno, 0, 0, vno2, 1) ; } for (n2 = 0 ; n2 < area_rh->n_points ; n2++) { vno2 = area_rh->lv[n2].vno ; if (vno2 >= 0) MRIsetVoxVal(mri_overlay, vno, 0, 0, mris->nvertices+vno2, 1) ; } } } #endif } mri_flattened = MRIflattenOverlay(mris, mri_overlay, NULL, 1.0, label_overlay, &mri_vertices) ; printf("writing flattened overlay to %s\n", out_patch_fname) ; MRIwrite(mri_flattened, out_patch_fname) ; MRIfree(&mri_flattened) ; FileNameRemoveExtension(out_patch_fname, fname) ; strcat(fname, ".vnos.mgz") ; printf("writing flattened vertex #s to %s\n", fname) ; MRIwrite(mri_vertices, fname) ; MRIfree(&mri_vertices) ; } #if 0 sprintf(fname, "%s.area_error", out_fname) ; printf("writing area errors to %s\n", fname) ; MRISwriteAreaError(mris, fname) ; sprintf(fname, "%s.angle_error", out_fname) ; printf("writing angle errors to %s\n", fname) ; MRISwriteAngleError(mris, fname) ; MRISfree(&mris) ; #endif exit(0) ; return(0) ; /* for ansi */ }