Skip to content
Snippets Groups Projects
film_gls.cc 5.37 KiB
Newer Older
Stephen Smith's avatar
Stephen Smith committed
/*  film_gls.cc

    Mark Woolrich, FMRIB Image Analysis Group

    Copyright (C) 1999-2000 University of Oxford  */

Stephen Smith's avatar
Stephen Smith committed
/*  CCOPYRIGHT  */
Stephen Smith's avatar
Stephen Smith committed

#include <iostream>
#include <fstream>
#include <strstream>
#define WANT_STREAM
#define WANT_MATH

#include "newmatap.h"
#include "newmatio.h"
#include "VolumeSeries.h"
#include "Volume.h"
#include "glim.h"
#include "miscmaths.h"
#include "gaussComparer.h"
#include "Log.h"
#include "AutoCorrEstimator.h"
#include "paradigm.h"
#include "FilmGlsOptions.h"
#include "glimGls.h"
Mark Woolrich's avatar
Mark Woolrich committed
#include <vector>
Stephen Smith's avatar
Stephen Smith committed
#include <string>

#ifndef NO_NAMESPACE
using namespace NEWMAT;
using namespace TACO;
using namespace UTILS;
#endif

int main(int argc, char *argv[])
{
  try{
    rand();
    // parse command line to find out directory name for logging:
    ofstream out2;
    FilmGlsOptions& globalopts = FilmGlsOptions::getInstance();
    globalopts.parse_command_line(argc, argv, out2);

    // Setup logging:
    Log& logger = Log::getInstance();
    logger.setLogFile("glslogfile");
    logger.establishDir(globalopts.datadir);

    // parse command line again to output arguments to logfile
    globalopts.parse_command_line(argc, argv, logger.str());

Mark Woolrich's avatar
Mark Woolrich committed
    // load data
Stephen Smith's avatar
Stephen Smith committed
    VolumeSeries x;
    x.read(globalopts.inputfname);

Mark Woolrich's avatar
Mark Woolrich committed
    // if needed for SUSAN spatial smoothing, output the 12th volume for use later
Stephen Smith's avatar
Stephen Smith committed
    Volume epivol;
    if(globalopts.smoothACEst)
      {
	epivol = x.getVolume(12).AsColumn();
	epivol.setDims(x.getDims());
	
	epivol.writeAsInt(logger.getDir() + "/" + globalopts.epifname);
      }

    // This also removes the mean from each of the time series:
    x.thresholdSeries(globalopts.thresh, true);

Mark Woolrich's avatar
Mark Woolrich committed
    // if needed for SUSAN spatial smoothing, also threshold the epi volume
Stephen Smith's avatar
Stephen Smith committed
    if(globalopts.smoothACEst)
      {
	epivol.setPreThresholdPositions(x.getPreThresholdPositions());
	epivol.threshold();
      }

    int sizeTS = x.getNumVolumes();
    int numTS = x.getNumSeries();
    
    // Load paradigm: 
    Paradigm parad;
Mark Woolrich's avatar
Mark Woolrich committed
    parad.load(globalopts.paradigmfname, "", "", false, sizeTS);
Stephen Smith's avatar
Stephen Smith committed

    if(globalopts.verbose)
      {
	logger.out("Gc", parad.getDesignMatrix());
      }
    
Mark Woolrich's avatar
Mark Woolrich committed
    // Setup GLM:
Mark Woolrich's avatar
Mark Woolrich committed
    int numParams = parad.getDesignMatrix().Ncols();
    GlimGls glimGls(numTS, sizeTS, numParams);
    
Mark Woolrich's avatar
Mark Woolrich committed
    // Residuals container:
Mark Woolrich's avatar
Mark Woolrich committed
    VolumeSeries residuals(sizeTS, numTS, x.getDims(), x.getPreThresholdPositions());
Mark Woolrich's avatar
Mark Woolrich committed

    // Setup autocorrelation estimator:
Mark Woolrich's avatar
Mark Woolrich committed
    AutoCorrEstimator acEst(residuals);
Stephen Smith's avatar
Stephen Smith committed

Mark Woolrich's avatar
Mark Woolrich committed
    if(!globalopts.noest)
Stephen Smith's avatar
Stephen Smith committed
      {
Mark Woolrich's avatar
Mark Woolrich committed
	cerr << "Calculating residuals..." << endl; 
	for(int i = 1; i <= numTS; i++)
	  {						    
	    glimGls.setData(x.getSeries(i), parad.getDesignMatrix(), i);
	    residuals.getSeries(i) = glimGls.getResiduals();
	  }
	cerr << "Completed" << endl; 
Mark Woolrich's avatar
Mark Woolrich committed
	cerr << "Estimating residual autocorrelation..." << endl; 
	
	acEst.calcRaw();
	
	if(globalopts.fitAutoRegressiveModel)
	  {
	    acEst.fitAutoRegressiveModel();
	  }
	else if(globalopts.tukey)
	  {    
	    // SUSAN smooth raw estimates:
	    if(globalopts.smoothACEst)
Mark Woolrich's avatar
Mark Woolrich committed
		acEst.spatiallySmooth(logger.getDir() + "/" + globalopts.epifname, epivol, globalopts.ms, globalopts.epifname, globalopts.susanpath, globalopts.epith);		    
Mark Woolrich's avatar
Mark Woolrich committed
	    if(globalopts.tukeysize == 0)
	      globalopts.tukeysize = (int)(2*sqrt(sizeTS))/2;
Mark Woolrich's avatar
Mark Woolrich committed
	    acEst.tukey(globalopts.tukeysize);
	  }
	else if(globalopts.multitaper)
	  {
	    acEst.multitaper(globalopts.multitapersize);
	  }
	else if(globalopts.pava)
	  {
	    // Smooth raw estimates:
	    if(globalopts.smoothACEst)
	      { 
		acEst.spatiallySmooth(logger.getDir() + "/" + globalopts.epifname, epivol, globalopts.ms, globalopts.epifname, globalopts.susanpath, globalopts.epith);		    
Stephen Smith's avatar
Stephen Smith committed
	    
Mark Woolrich's avatar
Mark Woolrich committed
	    acEst.pava();
Stephen Smith's avatar
Stephen Smith committed
	  }
Stephen Smith's avatar
Stephen Smith committed
      }
Mark Woolrich's avatar
Mark Woolrich committed
    cerr << "Completed" << endl; 
Stephen Smith's avatar
Stephen Smith committed

Mark Woolrich's avatar
Mark Woolrich committed
    cerr << "Prewhitening and Computing PEs..." << endl;   
    int co = 1;
Stephen Smith's avatar
Stephen Smith committed
    for(int i = 1; i <= numTS; i++)
Mark Woolrich's avatar
Mark Woolrich committed
      {						
	ColumnVector xw(sizeTS);
	ColumnVector xprew(sizeTS);
	    
	acEst.setDesignMatrix(parad.getDesignMatrix());
Mark Woolrich's avatar
Mark Woolrich committed
	// Use autocorr estimate to prewhiten data:
	xprew = x.getSeries(i);	
	Matrix designmattw;
	acEst.preWhiten(xprew, xw, i, designmattw);
	    
	if(co > 1000)
Stephen Smith's avatar
Stephen Smith committed
	  {
Mark Woolrich's avatar
Mark Woolrich committed
	    co = 1;
	    cerr << (float)i/(float)numTS << ",";
Stephen Smith's avatar
Stephen Smith committed
	  }
Mark Woolrich's avatar
Mark Woolrich committed
	else
	  co++;
	   
	x.getSeries(i) = xw;
    
	glimGls.setData(x.getSeries(i), designmattw, i);
    
Stephen Smith's avatar
Stephen Smith committed
      }
Stephen Smith's avatar
Stephen Smith committed
    cerr << "Completed" << endl;

Mark Woolrich's avatar
Mark Woolrich committed
    cerr << "Saving results... " << endl;

Mark Woolrich's avatar
Mark Woolrich committed
    if(globalopts.verbose)
Mark Woolrich's avatar
Mark Woolrich committed
	// output whitened x
Mark Woolrich's avatar
Mark Woolrich committed
	x.unthresholdSeries();
	x.writeAsFloat(logger.getDir() + "/wx");
      }
Mark Woolrich's avatar
Mark Woolrich committed
    x.CleanUp();

    // Write out threshac:
    VolumeSeries& threshac = acEst.getEstimates();
    int cutoff = sizeTS/2;
    if(globalopts.tukey)
      cutoff = globalopts.tukeysize;
    threshac = threshac.Rows(1,cutoff);
    VolumeSeries::Dims dims = x.getDims();
    dims.v = cutoff;
    threshac.unthresholdSeries(dims,x.getPreThresholdPositions());
    threshac.writeAsFloat(logger.getDir() + "/threshac1");
    threshac.thresholdSeries();
    threshac.CleanUp();

    // write residuals
    residuals.unthresholdSeries(x.getDims(),x.getPreThresholdPositions());
    residuals.writeAsFloat(logger.getDir() + "/res4d");
    residuals.CleanUp();
Mark Woolrich's avatar
Mark Woolrich committed
    // save gls results:
Stephen Smith's avatar
Stephen Smith committed
    glimGls.Save(x.getDims(), x.getPreThresholdPositions());
Mark Woolrich's avatar
Mark Woolrich committed
    glimGls.CleanUp();

Stephen Smith's avatar
Stephen Smith committed
    cerr << "Completed" << endl;
  }  
  catch(Exception p_excp) 
    {
      cerr << p_excp.what() << endl;
    }
  catch(...) 
    {
      cerr << "Image error" << endl;
    } 
  return 0;
}