Skip to content
Snippets Groups Projects
film_gls.cc 5.7 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>
Stephen Smith's avatar
Stephen Smith committed
#define WANT_STREAM
#define WANT_MATH

#include "newmatap.h"
#include "newmatio.h"
David Flitney's avatar
David Flitney committed
#include "miscmaths/volumeseries.h"
#include "miscmaths/volume.h"
#include "miscmaths/miscmaths.h"
#include "utils/log.h"
Stephen Smith's avatar
Stephen Smith committed
#include "AutoCorrEstimator.h"
#include "paradigm.h"
#include "FilmGlsOptions.h"
#include "glimGls.h"
#include <vector>
#include <string>
Stephen Smith's avatar
Stephen Smith committed

using namespace NEWMAT;
Mark Woolrich's avatar
Mark Woolrich committed
using namespace FILM;
using namespace Utilities;
Mark Woolrich's avatar
Mark Woolrich committed
using namespace MISCMATHS;
Stephen Smith's avatar
Stephen Smith committed

int main(int argc, char *argv[])
{
  try{
Stephen Smith's avatar
Stephen Smith committed
    // Setup logging:
Mark Woolrich's avatar
Mark Woolrich committed
    Log& logger = LogSingleton::getInstance();
Stephen Smith's avatar
Stephen Smith committed

Mark Woolrich's avatar
Mark Woolrich committed
    // parse command line
    FilmGlsOptions& globalopts = FilmGlsOptions::getInstance();
    globalopts.parse_command_line(argc, argv, logger);
Stephen Smith's avatar
Stephen Smith committed

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
    int sizeTS = x.getNumVolumes();

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)
      {
Mark Woolrich's avatar
Mark Woolrich committed
	epivol = x.getVolume(int(sizeTS/2)).AsColumn();
David Flitney's avatar
David Flitney committed
	epivol.setInfo(x.getInfo());
Stephen Smith's avatar
Stephen Smith committed
	
	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();
      }
    
Mark Woolrich's avatar
Mark Woolrich committed
    int numTS = x.getNumSeries();

    // Load paradigm:
Stephen Smith's avatar
Stephen Smith committed
    Paradigm parad;
Mark Woolrich's avatar
Mark Woolrich committed
    if(!globalopts.ac_only)
      {	
	parad.load(globalopts.paradigmfname, "", "", false, sizeTS);
      }
    else
      {
	// set design matrix to be one ev with all ones:
	Matrix mat(sizeTS,1);
	mat = 1;
	parad.setDesignMatrix(mat);
      }
Stephen Smith's avatar
Stephen Smith committed

    if(globalopts.verbose)
      {
Mark Woolrich's avatar
Mark Woolrich committed
	write_ascii_matrix(logger.appendDir("Gc"), parad.getDesignMatrix());
Stephen Smith's avatar
Stephen Smith committed
      }
    
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:
David Flitney's avatar
David Flitney committed
    VolumeSeries residuals(sizeTS, numTS, x.getInfo(), 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
	cout << "Calculating residuals..." << endl; 
Mark Woolrich's avatar
Mark Woolrich committed
	for(int i = 1; i <= numTS; i++)
	  {						    
	    glimGls.setData(x.getSeries(i), parad.getDesignMatrix(), i);
	    residuals.setSeries(glimGls.getResiduals(),i);
Mark Woolrich's avatar
Mark Woolrich committed
	cout << "Completed" << endl; 
Mark Woolrich's avatar
Mark Woolrich committed
	cout << "Estimating residual autocorrelation..." << endl; 
Mark Woolrich's avatar
Mark Woolrich committed
	if(globalopts.fitAutoRegressiveModel)
	  {
	    acEst.fitAutoRegressiveModel();
	  }
	else if(globalopts.tukey)
	  {    
Mark Woolrich's avatar
Mark Woolrich committed
	    if(globalopts.tukeysize == 0)
	      globalopts.tukeysize = (int)(2*sqrt(sizeTS))/2;

	    acEst.calcRaw();

Mark Woolrich's avatar
Mark Woolrich committed
	    // 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, globalopts.tukeysize);		    
	      }	    
Mark Woolrich's avatar
Mark Woolrich committed
	    acEst.tukey(globalopts.tukeysize);
	  }
	else if(globalopts.multitaper)
	  {
Mark Woolrich's avatar
Mark Woolrich committed
	    acEst.calcRaw();
	    acEst.multitaper(int(globalopts.multitapersize));
Mark Woolrich's avatar
Mark Woolrich committed
	  }
	else if(globalopts.pava)
	  {
Mark Woolrich's avatar
Mark Woolrich committed
	    acEst.calcRaw();

Mark Woolrich's avatar
Mark Woolrich committed
	    // 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
    cout << "Completed" << endl; 
Stephen Smith's avatar
Stephen Smith committed

Mark Woolrich's avatar
Mark Woolrich committed
    cout << "Prewhitening and Computing PEs..." << endl;
    cout << "Percentage done:" << endl;
Mark Woolrich's avatar
Mark Woolrich committed
    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);
Mark Woolrich's avatar
Mark Woolrich committed
	   
	if(!globalopts.noest)
	  {
	    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);
Stephen Smith's avatar
 
Stephen Smith committed
	    if ( (100.0*i)/numTS > co )
Stephen Smith's avatar
 
Stephen Smith committed
		cout << co << ",";
Mark Woolrich's avatar
Mark Woolrich committed
		cout.flush();
Stephen Smith's avatar
 
Stephen Smith committed
		co++;
Mark Woolrich's avatar
Mark Woolrich committed
	    
	    glimGls.setData(x.getSeries(i), designmattw, i);
Stephen Smith's avatar
Stephen Smith committed
	  }
Mark Woolrich's avatar
Mark Woolrich committed
	else
Stephen Smith's avatar
 
Stephen Smith committed
	    if ( (100.0*i)/numTS > co )
Stephen Smith's avatar
 
Stephen Smith committed
		cout << co << ",";
Mark Woolrich's avatar
Mark Woolrich committed
		cout.flush();
Stephen Smith's avatar
 
Stephen Smith committed
		co++;
Mark Woolrich's avatar
Mark Woolrich committed
	      }
	    
	    glimGls.setData(x.getSeries(i), parad.getDesignMatrix(), i);
	    residuals.setSeries(glimGls.getResiduals(),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;
Mark Woolrich's avatar
Mark Woolrich committed
    cutoff = MISCMATHS::Max(3,cutoff);
Mark Woolrich's avatar
Mark Woolrich committed
    threshac = threshac.Rows(1,cutoff);
David Flitney's avatar
David Flitney committed
    VolumeInfo volinfo = x.getInfo();
    volinfo.v = cutoff;
    threshac.unthresholdSeries(volinfo,x.getPreThresholdPositions());
Mark Woolrich's avatar
Mark Woolrich committed
    threshac.writeAsFloat(logger.getDir() + "/threshac1");
    threshac.thresholdSeries();
    threshac.CleanUp();

    // write residuals
David Flitney's avatar
David Flitney committed
    residuals.unthresholdSeries(x.getInfo(),x.getPreThresholdPositions());
Mark Woolrich's avatar
Mark Woolrich committed
    residuals.writeAsFloat(logger.getDir() + "/res4d");
    residuals.CleanUp();
Mark Woolrich's avatar
Mark Woolrich committed
    // save gls results:
David Flitney's avatar
David Flitney committed
    glimGls.Save(x.getInfo(), 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;
    }
Stephen Smith's avatar
Stephen Smith committed
  return 0;
}