Example #1
0
int
Resample::UseCorner(const string &refname)
{
  Cube refcube;
  stringstream tmps;
  if (refcube.ReadFile(refname)) {
    Tes reftes;
    if (!(reftes.ReadFile(refname))) {
      refcube=reftes[0];
    }
    else {
      printf("*** resample: invalid reference file: %s\n",refname.c_str());
      return 501;       // panic and exit
    }
  }
  // first get the corner coordinates of both images
  tokenlist ourline,refline;
  ourline.ParseLine(mycube->GetHeader("AbsoluteCornerPosition:"));
  refline.ParseLine(refcube.GetHeader("AbsoluteCornerPosition:"));
  if (ourline.size()!=3) {
    tmps.str("");
    tmps << "resample: input image file doesn't have an absolute corner position tag";
    printErrorMsg(VB_ERROR,tmps.str());
    return 101;
  }
  if (refline.size()!=3) {
    tmps.str("");
    tmps << "resample: reference image file doesn't have an absolute corner position tag";
    printErrorMsg(VB_ERROR,tmps.str());
    return 101;
  }
  double ourpos[3],refpos[3];
  ourpos[0]=strtod(ourline[0]);
  ourpos[1]=strtod(ourline[1]);
  ourpos[2]=strtod(ourline[2]);
  refpos[0]=strtod(refline[0]);
  refpos[1]=strtod(refline[1]);
  refpos[2]=strtod(refline[2]);
  
  // find the start point
  x1=(refpos[0]-ourpos[0])/mycube->voxsize[0];
  y1=(refpos[1]-ourpos[1])/mycube->voxsize[1];
  z1=(refpos[2]-ourpos[2])/mycube->voxsize[2];
  // the sampling interval should provide for 4x resolution in-plane
  xstep=(refcube.voxsize[0]/4.0)/mycube->voxsize[0];
  ystep=(refcube.voxsize[1]/4.0)/mycube->voxsize[1];
  zstep=refcube.voxsize[2]/mycube->voxsize[2];
  // the number of voxels should be the width of the target image
  nx=refcube.dimx*4;
  ny=refcube.dimy*4;
  nz=refcube.dimz;
  return 0;
}
Example #2
0
void
Extractor::init(tokenlist &args)
{
  // defaults
  xvflag=0;
  newdim[0]=41;
  newdim[1]=51;
  newdim[2]=27;

  newcube.voxsize[0]=3.75;
  newcube.voxsize[1]=3.75;
  newcube.voxsize[2]=5.00;

  newcube.origin[0]=21;
  newcube.origin[1]=31;
  newcube.origin[2]=10;

  original.ReadFile(args[0]);

  if (args.size()==1)
    newcube.SetFileName((string)"m"+original.GetFileName());

  if (args.size()==2 || args.size()==3) {
    newcube.SetFileName(args[1]);
    if (args.size()==3) {
      xval=strtol(args[2]);
      xvflag=1;
    }
  }

  if (args.size()==11 || args.size()==12) {
    newcube.SetFileName(args[1]);
    newdim[0]=strtol(args[2]);
    newdim[1]=strtol(args[3]);
    newdim[2]=strtol(args[4]);
    
    newcube.voxsize[0]=strtod(args[5]);
    newcube.voxsize[1]=strtod(args[6]);
    newcube.voxsize[2]=strtod(args[7]);
    
    newcube.origin[0]=strtol(args[8]);
    newcube.origin[1]=strtol(args[9]);
    newcube.origin[2]=strtol(args[10]);
    if (args.size()==12) {
      xval=strtol(args[11]);
      xvflag=1;
    }
  }
  newcube.SetVolume(newdim[0],newdim[1],newdim[2],original.datatype);
}
Example #3
0
int
process_singlemask(vector<VBRegion> &rlist,string fname)
{
  Cube mask;
  if (mask.ReadFile(fname)) {
    return 101;
  }
  VBRegion reg;
  reg=findregion_mask(mask,vb_gt,0.0);
  reg.name=xfilename(fname);
  if (!(reg.voxels.size()))
    return 102;
  rlist.push_back(reg);
  return 0;
}
Example #4
0
void
do_sumrfx(const string &fname,const string &mname,double thresh,vector<VBRegion> &rlist)
{
  int i,j,k;
  Tes rfx;
  Cube mask;

  rfx.ReadFile(fname);
  if (!rfx.data_valid) {
    printf("sumrfx: invalid 4D file %s.\n",fname.c_str());
    return;
  }
  // specified mask, intersect with tes mask
  if (mname.size()) {
    mask.ReadFile(mname);
    for (i=0; i<mask.dimx; i++)
      for (j=0; j<mask.dimy; j++)
	for (k=0; k<mask.dimz; k++)
	  if (rfx.GetMaskValue(i,j,k)<0.5)
	    mask.SetValue(i,j,k,0);
  }
  // default mask
  else {
    mask.SetVolume(rfx.dimx,rfx.dimy,rfx.dimz,vb_byte);
    for (i=0; i<mask.dimx; i++)
      for (j=0; j<mask.dimy; j++)
	for (k=0; k<mask.dimz; k++)
	  mask.SetValue(i,j,k,rfx.GetMaskValue(i,j,k));
  }
  if (!mask.data_valid) {
    printf("sumrfx: invalid mask file %s.\n",mname.c_str());
    return;
  }

  if (mask.dimx != rfx.dimx || mask.dimy != rfx.dimy || mask.dimz != rfx.dimz) {
    printf("sumrfx: mask and rfx volume dimensions don't match\n");
    return;
  }

  printf("sumrfx: thresh %.2f mask file: %s\n",thresh,mname.c_str());

  for (i=0; i<rfx.dimt; i++) {
    Cube mycube;
    rfx.getCube(i,mycube);
    if (!mycube.data_valid) continue;
    do_sumcube(rfx.GetFileName(),i,mycube,mask,thresh,rlist);
  }
}
Example #5
0
int
process_multimask(vector<VBRegion> &rlist,string fname)
{
  Cube mask;
  tokenlist args;
  if (mask.ReadFile(fname)) {
    return 101;
  }
  for (int i=0; i<(int)mask.header.size(); i++) {
    args.ParseLine(mask.header[i]);
    if (args[0]!="vb_maskspec:") continue;
    int value=strtol(args[1]);
    VBRegion reg;
    reg=findregion_mask(mask,vb_eq,(double)value);
    if (reg.voxels.size())
      rlist.push_back(reg);
  }
  return 0;
}
Example #6
0
int
Resample::SetAlign(const string mode,const string ref)
{
  Cube refcube;
  if (refcube.ReadFile(refname))
    return 100;
  if (mode=="-aa" || mode=="-ax") {
    if (ref=="origin")
      x1=(float)mycube->origin[0]-(((float)refcube.origin[0]*refcube.voxsize[0])/mycube->voxsize[0]);
  }
  if (mode=="-aa" || mode=="-ay") {
    if (ref=="origin")
      y1=(float)mycube->origin[1]-(((float)refcube.origin[1]*refcube.voxsize[1])/mycube->voxsize[1]);
  }
  if (mode=="-aa" || mode=="-az") {
    if (ref=="origin")
      z1=(float)mycube->origin[2]-(((float)refcube.origin[2]*refcube.voxsize[2])/mycube->voxsize[2]);
  }
  return 0;
}
Example #7
0
int
VBri::openconfig(string configfile)
{
  ifstream infile;
  infile.open(configfile.c_str());
  if (!infile) {
    QMessageBox::critical(this,"Error","Couldn't open config file.");
    return 2;
  }
  chdir(xdirname(configfile).c_str());

  char buf[STRINGLEN];
  tokenlist args;
  while (infile.getline(buf,STRINGLEN,'\n')) {
    args.ParseLine(buf);
    if (args.size()==0) continue;
    if (args[0][0]=='#' || args[0][0] == ';')
      continue;
    if (args[0]=="image" && args.size()==3) {
      Cube cb;
      if (cb.ReadFile(args[2])) {
        QMessageBox::critical(this,"Error",(format("Couldn't open image %s")%args[2]).str().c_str());
        infile.close();
        return 3;
      }
      find_origin(cb);
      cubelist.push_back(cb);
      cubemap[args[1]]=--cubelist.end();
    }
    else if (args[0]=="left") {
      leftimg=args[1];
    }
    else if (args[0]=="right") {
      rightimg=args[1];
    }
    else if (args[0]=="mask") {
      VBmask vbm;
      vbm.imgname=args[1];
      if (args.size()>2) {
        vbm.rating=strtol(args[2]);
        vbm.xslice=strtol(args[3]);
        vbm.yslice=strtol(args[3]);
        vbm.zslice=strtol(args[3]);
        vbm.notes=args.Tail(4);
      }
      else if (cubemap.count(vbm.imgname)) {
        // FIXME get it from the header...
        vbm.xslice=cubemap[vbm.imgname]->origin[0];
        vbm.yslice=cubemap[vbm.imgname]->origin[1];
        vbm.zslice=cubemap[vbm.imgname]->origin[2];
      }
      masklist.push_back(vbm);
    }
    else if (args[0]=="outfile") {
      if (args.size()>1)
        outfile=args[1];
    }
    else if (args[0]=="randomize") {
      randomizemasks();
    }
    else {
      QMessageBox::critical(this,"Error",((string)"Bad config line: "+buf).c_str());
      infile.close();
      return 4;
    }
  }
  infile.close();
  return 0;
}
Example #8
0
int
main(int argc,char *argv[])
{
  tokenlist args;
  string maskfile;
  vector<string> filelist;

  args.Transfer(argc-1,argv+1);
  for (size_t i=0; i<args.size(); i++) {
    if (args[i]=="-m" && i<args.size()-1)
      maskfile=args[++i];
    else if (args[i]=="-h") {
      vbcmp_help();
      exit(0);
    }
    else if (args[i]=="-v") {
      vbcmp_version();
      exit(0);
    }
    else
      filelist.push_back(args[i]);
  }

  if (filelist.size() < 2) {
    vbcmp_help();
    exit(0);
  }
  Cube mask;
  if (maskfile.size()) {
    if (mask.ReadFile(maskfile)) {
      cout << format("[E] vbcmp: error reading mask file %s\n")%maskfile;
      exit(2);
    }
  }

  string spacer;
  if (filelist.size()>2) spacer="   ";
  for (size_t i=0; i<filelist.size(); i++) {
    for (size_t j=0; j<filelist.size(); j++) {
      if (i>=j) continue;
      if (filelist.size()>2)
        cout << format("[I] vbcmp: comparing %s and %s\n")%filelist[i]%filelist[j];
      int err1,err2;
      Cube cb1,cb2;
      err1=cb1.ReadFile(filelist[i]);
      err2=cb2.ReadFile(filelist[j]);
      if (!err1 && !err2) {
        cmp3d(cb1,cb2,mask,spacer);
        continue;
      }
      Tes ts1,ts2;
      err1=ts1.ReadFile(filelist[i]);
      err2=ts2.ReadFile(filelist[j]); 
      if (!err1 && !err2) {
        cmp4d(ts1,ts2,mask,spacer);
        continue;
      }
      // try vectors before matrices, because all vectors are matrices
      VB_Vector v1,v2;
      err1=v1.ReadFile(filelist[i]);
      err2=v2.ReadFile(filelist[j]);
      if (!err1 && !err2) {
        cmp1d(v1,v2,spacer);
        continue;
      }
      VBMatrix m1,m2;
      err1=m1.ReadFile(filelist[i]);
      err2=m2.ReadFile(filelist[j]);
      if (!err1 && !err2) {
        cmp2d(m1,m2,spacer);
        continue;
      }
      cout << format("[E] vbcmp: couldn't read %s and %s as same type\n")%filelist[i]%filelist[j];
    }
  }
  exit(100);
}
Example #9
0
int
main(int argc,char *argv[])
{
  if (argc<2) {
    vbcmap_help();
    exit(0);
  }

  tokenlist args;
  vector<string>filelist;
  string dvname,ivname,outfile,maskfile,pfile,pmapname;
  args.Transfer(argc-1,argv+1);
  int part=1,nparts=1;
  string perm_mat;
  int perm_index=-1;
  int minlesions=2;
  bool f_yates=0;
  bool f_fisher=0;
  bool f_zscore=0;
  bool f_flip=0;
  bool f_fdr=0;
  bool f_twotailed=0;
  bool f_nodup=0;
  float q=0;

  for (size_t i=0; i<args.size(); i++) {
    if (args[i]=="-v")
      vbcmap_version();
    else if (args[i]=="-h")
      vbcmap_help();
    else if (args[i]=="-2")
      f_twotailed=1;
    else if (args[i]=="-z")
      f_zscore=1;
    else if (args[i]=="-f")
      f_flip=1;
    else if (args[i]=="-x")
      f_fisher=1;
    else if (args[i]=="-y")
      f_yates=1;
    else if (args[i]=="-nodup")
      f_nodup=1;
    else if (args[i]=="-pfile" && i<args.size()-1) {
      pfile=args[++i];
    }
    else if (args[i]=="-pmap" && i<args.size()-1) {
      pmapname=args[++i];
    }
    else if (args[i]=="-m" && i<args.size()-1) {
      maskfile=args[++i];
    }
    else if (args[i]=="-q" && i<args.size()-1) {
      f_fdr=1;
      q=strtod(args[++i]);
    }
    else if (args[i]=="-n" && i<args.size()-1) {
      minlesions=strtol(args[++i]);
      if (minlesions<2) minlesions=2;
    }
    else if (args[i]=="-op" && i<args.size()-2) {
      perm_mat=args[++i];
      perm_index=strtol(args[++i]);
    }
    else if (args[i]=="-p" && i<args.size()-2) {
      part=strtol(args[++i]);
      nparts=strtol(args[++i]);
    }
    else
      filelist.push_back(args[i]);
  }
  if (filelist.size()!=3) {
    vbcmap_help();
    exit(112);
  }
  ivname=filelist[0];
  dvname=filelist[1];
  outfile=filelist[2];

  Cube tmap,mask;
  Tes ts;
  VB_Vector depvar;
  if (ts.ReadFile(ivname)) {
    printf("[E] vbcmap: couldn't get grouping info from %s\n",ivname.c_str());
    exit(101);
  }
  if (depvar.ReadFile(dvname)) {
    printf("[E] vbcmap: couldn't get dependent variable info from %s\n",dvname.c_str());
    exit(102);
  }
  // build our mask
  ts.ExtractMask(mask);

  if (maskfile.size()) {
    Cube tmask;
    if (tmask.ReadFile(maskfile)) {
      printf("[E] vbcmap: couldn't read mask file %s\n",maskfile.c_str());
      exit(103);
    }
    if (!(tmask.dimsequal(mask))) {
      printf("[E] vbcmap: lesion maps and mask files have inconsistent dimensions\n");
      exit(104);
    }
    mask.intersect(tmask);
  }

  // permute order of dv if requested
  VB_Vector perm_order;
  if (perm_index>-1) {
    VBMatrix vm(perm_mat,0,0,perm_index,perm_index);
    perm_order=vm.GetColumn(0);
    VB_Vector tmp(depvar.size());
    for (uint32 i=0; i<depvar.size(); i++)
      tmp[i]=depvar[(int)perm_order[i]];
    depvar=tmp;
  }
  // convert dv to bitmask
  bitmask dvbm;
  dvbm.resize(depvar.size());
  dvbm.clear();
  for (size_t i=0; i<depvar.size(); i++) {
    if (!f_flip && fabs(depvar[i])>FLT_MIN)
      dvbm.set(i);
    if (f_flip && !(fabs(depvar[i])>FLT_MIN))
      dvbm.set(i);
  }
  
  string partstring;
  if (nparts>1)
    partstring="_part_"+strnum(part);

  bitmask bm;
  bm.resize(ts.dimt);
  map<bitmask,x2val> statlookup;
  map<bitmask,x2val>::iterator iter;
  Cube statmap(ts.dimx,ts.dimy,ts.dimz,vb_float);
  Cube pmap;
  Tes cimap;
  x2val res;
  Cube fdrmask;
  if (f_fdr || pmapname.size()) {
    pmap.SetVolume(ts.dimx,ts.dimy,ts.dimz,vb_float);
    fdrmask=mask;
    fdrmask.zero();
  }
  vector<double> pvals;   // all p vals used in fdr calculation
  bool f_non1=0;
  int16 val;
  for (int i=0; i<ts.dimx; i++) {
    for (int j=0; j<ts.dimy; j++) {
      for (int k=0; k<ts.dimz; k++) {
        if (!mask.testValue(i,j,k))
          continue;
        for (int m=0; m<ts.dimt; m++) {
          val=ts.getValue<int16>(i,j,k,m);
          if (val) {
            bm.set(m);
            if (val!=1) f_non1=1;
          }
          else
            bm.unset(m);
        }
        if (bm.count()<minlesions)
          continue;
        if (ts.dimt-bm.count()<2)
          continue;
        iter=statlookup.find(bm);
        if (iter==statlookup.end()) {
          // this is a new pattern
          if (f_fdr)
            fdrmask.SetValue(i,j,k,1);
          if (f_fisher)
            res=calc_fisher(bm,dvbm);
          else
            res=calc_chisquared(bm,dvbm,f_yates);
          if (f_twotailed)
            res.p*=2.0;
          // FIXME??? if we need the p value or z score, get it
          // if doing fdr, stash the p value
          if (f_fdr || pmapname.size()) {
            pmap.SetValue(i,j,k,res.p);
            pvals.push_back(res.p);
          }
          // if doing confidence intervals, stash that
          if (f_zscore)
            statmap.SetValue(i,j,k,res.z);
          else
            statmap.SetValue(i,j,k,res.x2);
          statlookup[bm]=res;
        }
        else {
          // this is a previously encountered pattern
          if (f_fdr && !f_nodup) {
            fdrmask.SetValue(i,j,k,1); 
            pvals.push_back(iter->second.p);
          }
          statmap.SetValue(i,j,k,iter->second.x2);
          if (f_fdr || pmapname.size())
            pmap.SetValue(i,j,k,iter->second.p);
        }
      }
    }
  }
  if (f_non1) {
    cout << "[W] vbcmap: non-0/1 values found in lesion map" << endl;
  }
  if (f_fdr) {
    vector<fdrstat> ffs=calc_multi_fdr_thresh(statmap,pmap,fdrmask,q);
    if (ffs.size()) {
      cout << (format("[I] vbcmap: FDR calculation included %d voxels with p values from %.4f to %.4f\n")
               %ffs[0].nvoxels%ffs[0].low%ffs[0].high).str();
      statmap.AddHeader("# the following thresholds must be exceeded for FDR control");
      vbforeach(fdrstat ff,ffs) {
        if (ff.maxind>=0)
          cout << (format("[I] vbcmap: FDR threhsold for q=%.2f is %.4f\n")%ff.q%ff.statval).str();
        else
          cout << (format("[I] vbcmap: no FDR threhsold could be identified for q=%.2f\n")%ff.q).str();
        string tmps=(format("fdrthresh: %g %g")%ff.q%ff.statval).str().c_str();
        statmap.AddHeader(tmps);
      }
    }
  }
Example #10
0
int
VBSim::Go(int argc,char *argv[])
{
  tokenlist args;
  rng=NULL;
  dimx=0;
  dimy=0;
  dimz=0;
  dimt=0;
  n_mean=10.0;
  n_variance=5.0;
  n_fwhm=0.0;
  float vx=1.0;
  float vy=1.0;
  float vz=1.0;
  float vt=2000;
  uint32 rngseed=VBRandom();

  args.Transfer(argc,argv);

  if (args.size() == 0) {
    vbsim_help();
    exit(0);
  }

  for (size_t i=0; i<args.size(); i++) {
    // -d x y z for dims
    if (args[i]=="-d" && i<args.size()-4) {
      dimx=strtol(args[i+1]);
      dimy=strtol(args[i+2]);
      dimz=strtol(args[i+3]);
      dimt=strtol(args[i+4]);
      i+=4;
    }
    // -c for loading anatomy
    else if (args[i]=="-c" && i<args.size()-1) {
      anatname=args[i+1];
      i++;
    }
    // -z for voxel sizes
    else if (args[i]=="-z" && i<args.size()-3) {
      vx=strtod(args[++i]);
      vy=strtod(args[++i]);
      vz=strtod(args[++i]);
      vt=strtod(args[++i]);
    }
    // -n for per-volume noise
    else if (args[i]=="-n" && i<args.size()-3) {
      n_mean=strtod(args[i+1]);
      n_variance=strtod(args[i+2]);
      n_fwhm=strtod(args[i+3]);
      i+=3;
    }
    else if (args[i]=="-s" && i<args.size()-1)
      rngseed=strtol(args[++i]);
    else if (args[i]=="-o" && i<args.size()-1) {
      outfile=args[i+1];
      i++;
    }
    else {
      printf("[E] vbsim: unrecognized argument %s\n",args(i));
      return 140;
    }
  }
  if (dimx<1 || dimy<0 || dimz<0 || dimt<0) {
    printf("[E] vbsim: bad dimensions\n");
    return 110;
  }

  // initialize RNG
  rng=gsl_rng_alloc(gsl_rng_mt19937);
  assert(rng);
  gsl_rng_set(rng,rngseed);

  // FIXME tell the user here what we're doing

  // CREATE ANATOMY (constant image)
  if (anatname.size()) {
    if (anat.ReadFile(anatname)) {
      printf("[E] vbsim: couldn't read %s\n",anatname.c_str());
      return 101;
    }
    if (anat.dimx!=dimx || anat.dimy!=dimy || anat.dimz != dimz) {
      printf("[E] vbsim: %s doesn't match your dimensions\n",anatname.c_str());
      return 102;
    }
  }
  else {
    // create volume according to dimensions, add random noise if requested
    anat.SetVolume(dimx,dimy,dimz,vb_float);
  }

  // SPECIAL CASE-- vecs if dimy=dimz=dimt==0
  if (dimt==0 && dimy==0 && dimz==0) {
    printf("[I] vbsim: creating a vector with %d elements with N(%g,%g)\n",dimx,
           n_mean,sqrt(n_variance));
    if (outfile=="")
      outfile="data.ref";
    VB_Vector vv(dimx);
    
    if (!(isnan(n_mean))) {
      for (int32 i=0; i<dimx; i++)
        vv[i]=n_mean+gaussian_random(sqrt(abs(n_variance)));
    }
    if (vv.WriteFile(outfile)) {
      printf("[E] vbsim: error writing 1D file %s\n",outfile.c_str());
      exit(120);
    }
    printf("[I] vbsim: wrote 1D file %s\n",outfile.c_str());
    exit(0);
  }


  // SPECIAL CASE-- cubes if dimt==0
  if (dimt==0) {
    printf("[I] vbsim: creating a %dx%dx%d 3D volume with N(%g,%g)\n",dimx,dimy,dimz,
           n_mean,sqrt(n_variance));
    if (outfile=="")
      outfile="data.cub";
    Cube vol(dimx,dimy,dimz,vb_float);
    if (!(isnan(n_mean)))
      AddGaussian(vol,n_mean,n_variance,n_fwhm,n_fwhm,n_fwhm);
    vol+=anat;
    vol.setVoxSizes(vx,vy,vz,vt);
    if (vol.WriteFile(outfile)) {
      printf("[E] vbsim: error writing 3D volume %s\n",outfile.c_str());
      exit(120);
    }
    printf("[I] vbsim: wrote 3D volume %s\n",outfile.c_str());
    exit(0);
  }

  printf("[I] vbsim: creating a %dx%dx%dx%d 4D volume with N(%g,%g)\n",dimx,dimy,dimz,dimt,
         n_mean,sqrt(n_variance));
  if (outfile=="")
    outfile="data.tes";
  // CREATE FUNCTIONALS (variable images, one per time point)
  mytes.SetVolume(dimx,dimy,dimz,dimt,vb_float);
  for (int i=0; i<dimt; i++) {
    Cube vol(dimx,dimy,dimz,vb_float);
    if (!(isnan(n_mean)))
      AddGaussian(vol,n_mean,n_variance,n_fwhm,n_fwhm,n_fwhm);
    vol+=anat;
    mytes.SetCube(i,vol);
  }
  mytes.setVoxSizes(vx,vy,vz,vt);
  if (mytes.WriteFile(outfile)) {
    printf("[E] vbsim: error writing 4D volume %s\n",outfile.c_str());
    return 120;
  }
  printf("[I] vbsim: wrote 4D volume %s\n",outfile.c_str());
  return 0;
}
Example #11
0
int
main(int argc,char *argv[])
{
  if (argc==1) {
    vbconv_help();
    exit(0);
  }
  tokenlist args;
  string outfile;
  int floatflag=0,nanflag=0,extractflag=0;
  set<int> includeset,excludeset;
  list<string> filelist;
  VBFF nullff;

  args.Transfer(argc-1,argv+1);
  for (size_t i=0; i<args.size(); i++) {
    if (args[i]=="-f")
      floatflag=1;
    else if (args[i]=="-n")
      nanflag=1;
    else if (args[i]=="-i" && i<args.size()-1) {
      includeset=numberset(args[++i]);
      if (includeset.empty()) {
        cout << "[E] vbconv: invalid inclusion range specified with -i\n";
        exit(111);
      }
    }
    else if (args[i]=="-e" && i<args.size()-1) {
      excludeset=numberset(args[++i]);
      if (excludeset.empty()) {
        cout << "[E] vbconv: invalid exclusion range specified with -e\n";
        exit(111);
      }
    }
    else if (args[i]=="-x" && i<args.size()-1)
      extractflag=1;
    else if (args[i]=="-v") {
      vbconv_version();
      exit(0);
    }
    else if (args[i]=="-h") {
      vbconv_help();
      exit(0);
    }
    else if (args[i]=="-o" && i<args.size()-1)
      outfile=args[++i];
    else {
      filelist.push_back(args[i]);
    }
  }

  if (filelist.size()<1) {
    printf("[E] vbconv: requires at least one input file\n");
    exit(10);
  }
  // if there's no -o flag and exactly two files specified, convert
  // the first to the second
  if (filelist.size()==2 && outfile.size()==0) {
    outfile=filelist.back();
    filelist.pop_back();
  }
  if (outfile.size()==0) {
    printf("[E] vbconv: requires an output filename be provided\n");
    exit(11);
  }

  // multiple files, must be 3D/4D combination
  if (filelist.size()>1) {
    Tes newtes;
    ConvertMultiple(filelist,nanflag,floatflag,newtes);
    if (WriteTes(newtes,outfile,extractflag,floatflag,nanflag,includeset,excludeset))
      exit(223);
    exit(0);  // just in case
  }
  int err;
  // just one file, see what kind
  Cube cb;
  if (cb.ReadFile(filelist.front())==0) {
    cb.fileformat=nullff;
    if (floatflag && cb.datatype!=vb_float)
      cb.convert_type(vb_float);
    if (nanflag)
      cb.removenans();
    if ((err=cb.WriteFile(outfile))) {
      printf("[E] vbconv: error %d writing %s\n",err,outfile.c_str());
      exit(4);
    }
    else {
      printf("[I] vbconv: wrote cube %s\n",outfile.c_str());
      exit(0);
    }
  }
  Tes ts;
  if (ts.ReadFile(filelist.front())==0) {
    ts.fileformat=nullff;
    if ((err=WriteTes(ts,outfile,extractflag,floatflag,nanflag,includeset,excludeset))) {
      printf("[E] vbconv: error %d writing %s\n",err,outfile.c_str());
      exit(4);
    }
    else {
      printf("[I] vbconv: wrote 4D volume %s\n",outfile.c_str());
      exit(0);
    }
  }
  VB_Vector vv;
  if (vv.ReadFile(*filelist.begin())==0) {
    // vv.fileformat=nullff; // FIXME -- can we set 1d fileformat?
    if (vv.WriteFile(outfile)) {
      printf("[E] vbconv: error writing %s\n",outfile.c_str());
      exit(4);
    }
    else {
      printf("[I] vbconv: wrote vector %s\n",outfile.c_str());
      exit(0);
    }
  }
  printf("[E] vbconv: couldn't make sense of file %s\n",filelist.front().c_str());
  exit(100);
}
Example #12
0
int
ConvertMultiple(list<string> &filelist,int nanflag,int floatflag,Tes &newtes)
{
  // printf("[I] vbconv: converting %d files\n",(int)filelist.size());
  // pre-scan for dimensions
  int dimx=0,dimy=0,dimz=0,dimt=0;
  VB_datatype dtype=vb_short;
  for (list<string>::iterator ff=filelist.begin(); ff!=filelist.end(); ff++) {
    Cube cb;
    Tes ts;
    VBImage *im;
    if (cb.ReadHeader(*ff)==0) {
      im=&cb;
      dimt++;
    }
    else if (ts.ReadHeader(*ff)==0) {
      im=&ts;
      dimt+=ts.dimt;
    }
    else {
      printf("[E] vbconv: couldn't read %s\n",ff->c_str());
      exit(51);
    }
    if (ff==filelist.begin()) {
      dimx=im->dimx;
      dimy=im->dimy;
      dimz=im->dimz;
      dimt=1;
      dtype=im->datatype;
    }
    else if (im->dimx!=dimx||im->dimy!=dimy||im->dimz!=dimz) {
      printf("[E] vbconv: incompatible dimensions in file %s\n",ff->c_str());
      exit(51);
    }
  }
  int index=0;
  if (floatflag)
    dtype=vb_float;
  
  // now grab all the images
  newtes.SetVolume(dimx,dimy,dimz,dimt,dtype);
  int findex=1;
  for (list<string>::iterator ff=filelist.begin(); ff!=filelist.end(); ff++) {
    printf("[I] vbconv: converting file %d of %d\n",findex++,(int)filelist.size());
    newtes.AddHeader("vbconv: included file "+*ff);
    Cube cb;
    Tes ts;
    if (cb.ReadFile(*ff)==0) {
      newtes.SetCube(index++,cb);
    }
    else if (ts.ReadFile(*ff)==0) {
      for (int i=0; i<ts.dimt; i++) {
        ts.getCube(i,cb);
        newtes.SetCube(index++,cb);
      }
    }
    else {
      printf("[E] vbconv: couldn't read data from %s\n",ff->c_str());
      exit(5);
    }
  }
  if (nanflag)
    newtes.removenans();
  return 0;
}
Example #13
0
int
RenderMe::Go(tokenlist &args)
{
  if (!args.size()) {
    help();
    return 102;
  }
  for (int i=0; i<args.size(); i++) {
    if (args[i]=="-m" && i+1 < args.size()) {
      maskname=args[i+1];
      i++;
    }
    else if (args[i]=="-d" && i+1 < args.size()) {
      overlayname=args[i+1];
      i++;
    }
    else if (args[i]=="-o" && i+1 < args.size()) {
      outfilename=args[i+1];
      i++;
    }
    else if (args[i]=="-a" && i+1 < args.size()) {
      anatomyname=args[i+1];
      i++;
    }
    else if (args[i]=="-1") {
      vbr.twotailed=0;
    }
    else if (args[i]=="-t" && i+2 < args.size()) {
      vbr.low=strtod(args[i+1]);
      vbr.high=strtod(args[i+2]);
      i+=2;
    }
  }

  if (outfilename.size() && !anatomyname.size() && !overlayname.size()) {
    help();
    return 101;
  }

  char tmp[STRINGLEN];

  if (anatomyname.size()) {
    anatomy.ReadFile(anatomyname);
    if (!anatomy.data_valid){
      sprintf(tmp,"vbrender: anatomy file %s not valid",anatomyname.c_str());
      printErrorMsg(VB_ERROR,tmp);
      return 102;
    }
    vbr.anatomy=&anatomy;
  }

  if (overlayname.size()) {
    overlay.ReadFile(overlayname);
    if (!overlay.data_valid){
      sprintf(tmp,"vbrender: statistical overlay file %s not valid",overlayname.c_str());
      printErrorMsg(VB_ERROR,tmp);
      return 102;
    }
    vbr.overlay=&overlay;
  }

  if (maskname.size()) {
    mask.ReadFile(maskname);
    if (!mask.data_valid){
      sprintf(tmp,"vbrender: mask file %s not valid",maskname.c_str());
      printErrorMsg(VB_ERROR,tmp);
      return 102;
    }
    vbr.mask=&mask;
  }

  QImage im;
  if (vbr.RenderSlices())
    return 103;
  if (vbr.SavePNG(outfilename))
    return 103;
  return 0;
}