Newer
Older
/* film_gls.cc
Mark Woolrich, FMRIB Image Analysis Group
Copyright (C) 1999-2000 University of Oxford */
#include <iostream>
#include <fstream>
#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);
volume4D<float> input_data;
volumeinfo vinfo;
read_volume4D(input_data,globalopts.inputfname,vinfo);
int sizeTS = input_data.tsize();
Matrix datam;
VolumeSeries x,y;
y.read(globalopts.inputfname);
x.setInfo(y.getInfo());
x=input_data.matrix();
x.thresholdSeries(globalopts.thresh, true);
//save_volume_dtype(input_data[int(sizeTS/2)-1],logger.getDir() + "/" + globalopts.epifname,DT_SIGNED_INT );
save_volume(input_data[int(sizeTS/2)-1],logger.getDir() + "/" + globalopts.epifname);
volume<float> mask(input_data[0]);
volume4D<float> epivol2(mask.xsize(),mask.ysize(),mask.zsize(),1);
epivol2[0]=input_data[int(sizeTS/2)-1];
mask=0;
for(int t=0;t<input_data.tsize();t++) mask+=input_data[t];
mask/=input_data.tsize();
for(int z=0;z<input_data.zsize();z++)
for(int y=0;y<input_data.ysize();y++)
for(int x=0;x<input_data.xsize();x++)
if ( mask(x,y,z) > globalopts.thresh )
for(int t=0;t<input_data.tsize();t++) input_data(x,y,z,t)-= mask(x,y,z);
mask.binarise(globalopts.thresh,mask.max()+1,exclusive);
input_data*=mask;
datam=input_data.matrix(mask);
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_vest(logger.appendDir("Gc"), parad.getDesignMatrix());
OUT(parad.getDesignMatrix().Nrows());
OUT(parad.getDesignMatrix().Ncols());
OUT(sizeTS);
OUT(numTS);
int numParams = parad.getDesignMatrix().Ncols();
GlimGls glimGls(numTS, sizeTS, numParams);
VolumeSeries residuals(sizeTS, numTS, x.getInfo(), x.getPreThresholdPositions());
Matrix residualsm = datam;
acEst.mask=mask;
glimGls.setData(datam.Column(i), parad.getDesignMatrix(), i);
residuals.setSeries(glimGls.getResiduals(),i);
cout << "Estimating residual autocorrelation..." << endl;
volume4D<float> beta;
beta.setmatrix(acEst.fitAutoRegressiveModel(),mask);
copybasicproperties(input_data,beta);
FslSetCalMinMax(&vinfo,beta.min(),beta.max());
save_volume4D(beta,LogSingleton::getInstance().getDir() + "/betas",vinfo);
if(globalopts.tukeysize == 0)
globalopts.tukeysize = (int)(2*sqrt(sizeTS))/2;
acEst.calcRaw();
// SUSAN smooth raw estimates:
if(globalopts.smoothACEst)
ColumnVector epivol = epivol2.matrix(mask).t();
acEst.spatiallySmooth(logger.getDir() + "/" + globalopts.epifname, epivol, globalopts.ms, globalopts.epifname, 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)
{
ColumnVector epivol = epivol2.matrix(mask).t();
acEst.spatiallySmooth(logger.getDir() + "/" + globalopts.epifname, epivol, globalopts.ms, globalopts.epifname, globalopts.epith);
cout << "Prewhitening and Computing PEs..." << endl;
cout << "Percentage done:" << endl;
Matrix mean_prewhitened_dm(parad.getDesignMatrix().Nrows(),parad.getDesignMatrix().Ncols());
mean_prewhitened_dm=0;
{
ColumnVector xw(sizeTS);
ColumnVector xprew(sizeTS);
if(!globalopts.noest)
{
acEst.setDesignMatrix(parad.getDesignMatrix());
xprew = datam.Column(i);
Matrix designmattw;
acEst.preWhiten(xprew, xw, i, designmattw);
datam.Column(i)=xw;
glimGls.setData(datam.Column(i), designmattw, i);
residuals.setSeries(glimGls.getResiduals(),i);
if(globalopts.output_pwdata || globalopts.verbose)
{
mean_prewhitened_dm=mean_prewhitened_dm+designmattw;
}
glimGls.setData(datam.Column(i), parad.getDesignMatrix(), i);
residuals.setSeries(glimGls.getResiduals(),i);
if(globalopts.output_pwdata || globalopts.verbose)
{
mean_prewhitened_dm=mean_prewhitened_dm+parad.getDesignMatrix();
}
if(globalopts.output_pwdata || globalopts.verbose)
{
mean_prewhitened_dm=mean_prewhitened_dm/numTS;
}
// write residuals
residuals.writeThresholdedSeriesAsFloat(x.getInfo(),x.getPreThresholdPositions(),logger.getDir() + "/res4d");
residuals.CleanUp();
if(globalopts.output_pwdata || globalopts.verbose)
input_data.setmatrix(datam,mask);
FslSetCalMinMax(&vinfo,input_data.min(),input_data.max());
save_volume4D(input_data,logger.getDir() + "/prewhitened_data",vinfo);
// Write out whitened design matrix
write_vest(logger.appendDir("mean_prewhitened_dm.mat"), mean_prewhitened_dm);
Matrix& threshacm = acEst.getEstimates();
int cutoff = sizeTS/2;
if(globalopts.tukey)
cutoff = globalopts.tukeysize;
Mark Woolrich
committed
cutoff = MISCMATHS::Max(1,cutoff);
threshacm = threshacm.Rows(1,cutoff);
volume4D<float> threshac;
threshac.setmatrix(threshacm,mask);
copybasicproperties(input_data,threshac);
threshac.set_intent(NIFTI_INTENT_ESTIMATE,0,0,0);
FslSetCalMinMax(&vinfo,threshac.min(),threshac.max());
save_volume4D(threshac,logger.getDir() + "/threshac1",vinfo);
threshacm.CleanUp();
glimGls.Save(x.getInfo(), x.getPreThresholdPositions());
cerr << "Completed" << endl;
}
catch(Exception p_excp)
{
cerr << p_excp.what() << endl;
}
catch(...)
{
cerr << "Uncaught exception!" << endl;
}