Newer
Older
/* film_gls.cc
Mark Woolrich, FMRIB Image Analysis Group
Copyright (C) 1999-2000 University of Oxford */
#include <iostream>
#include <fstream>
#include <strstream>
#include "newmatap.h"
#include "newmatio.h"
#include "miscmaths/volumeseries.h"
#include "miscmaths/volume.h"
#include "miscmaths/miscmaths.h"
#include "utils/log.h"
#include "AutoCorrEstimator.h"
#include "paradigm.h"
#include "FilmGlsOptions.h"
#include "glimGls.h"
using namespace FILM;
using namespace Utilities;
// parse command line
FilmGlsOptions& globalopts = FilmGlsOptions::getInstance();
globalopts.parse_command_line(argc, argv, logger);
// if needed for SUSAN spatial smoothing, output the 12th volume for use later
epivol = x.getVolume(int(sizeTS/2)).AsColumn();
epivol.writeAsInt(logger.getDir() + "/" + globalopts.epifname);
}
// This also removes the mean from each of the time series:
x.thresholdSeries(globalopts.thresh, true);
// if needed for SUSAN spatial smoothing, also threshold the epi volume
if(globalopts.smoothACEst)
{
epivol.setPreThresholdPositions(x.getPreThresholdPositions());
epivol.threshold();
}
int numTS = x.getNumSeries();
// Load paradigm:
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);
}
write_ascii_matrix(logger.appendDir("Gc"), parad.getDesignMatrix());
int numParams = parad.getDesignMatrix().Ncols();
GlimGls glimGls(numTS, sizeTS, numParams);
VolumeSeries residuals(sizeTS, numTS, x.getInfo(), x.getPreThresholdPositions());
for(int i = 1; i <= numTS; i++)
{
glimGls.setData(x.getSeries(i), parad.getDesignMatrix(), i);
residuals.setSeries(glimGls.getResiduals(),i);
cout << "Estimating residual autocorrelation..." << endl;
if(globalopts.fitAutoRegressiveModel)
{
acEst.fitAutoRegressiveModel();
}
else if(globalopts.tukey)
{
if(globalopts.tukeysize == 0)
globalopts.tukeysize = (int)(2*sqrt(sizeTS))/2;
acEst.calcRaw();
// SUSAN smooth raw estimates:
if(globalopts.smoothACEst)
acEst.spatiallySmooth(logger.getDir() + "/" + globalopts.epifname, epivol, globalopts.ms, globalopts.epifname, globalopts.susanpath, globalopts.epith, globalopts.tukeysize);
}
acEst.tukey(globalopts.tukeysize);
}
else if(globalopts.multitaper)
{
acEst.calcRaw();
acEst.multitaper(int(globalopts.multitapersize));
// Smooth raw estimates:
if(globalopts.smoothACEst)
{
acEst.spatiallySmooth(logger.getDir() + "/" + globalopts.epifname, epivol, globalopts.ms, globalopts.epifname, globalopts.susanpath, globalopts.epith);
cout << "Prewhitening and Computing PEs..." << endl;
cout << "Percentage done:" << endl;
{
ColumnVector xw(sizeTS);
ColumnVector xprew(sizeTS);
if(!globalopts.noest)
{
acEst.setDesignMatrix(parad.getDesignMatrix());
// Use autocorr estimate to prewhiten data:
xprew = x.getSeries(i);
Matrix designmattw;
acEst.preWhiten(xprew, xw, i, designmattw);
x.setSeries(xw,i);
glimGls.setData(x.getSeries(i), designmattw, i);
}
glimGls.setData(x.getSeries(i), parad.getDesignMatrix(), i);
residuals.setSeries(glimGls.getResiduals(),i);
x.unthresholdSeries();
x.writeAsFloat(logger.getDir() + "/wx");
}
x.CleanUp();
// Write out threshac:
VolumeSeries& threshac = acEst.getEstimates();
int cutoff = sizeTS/2;
if(globalopts.tukey)
cutoff = globalopts.tukeysize;
VolumeInfo volinfo = x.getInfo();
volinfo.v = cutoff;
threshac.unthresholdSeries(volinfo,x.getPreThresholdPositions());
threshac.writeAsFloat(logger.getDir() + "/threshac1");
threshac.thresholdSeries();
threshac.CleanUp();
// write residuals
residuals.unthresholdSeries(x.getInfo(),x.getPreThresholdPositions());
residuals.writeAsFloat(logger.getDir() + "/res4d");
residuals.CleanUp();
glimGls.Save(x.getInfo(), x.getPreThresholdPositions());
cerr << "Completed" << endl;
}
catch(Exception p_excp)
{
cerr << p_excp.what() << endl;
}