Skip to content
Snippets Groups Projects
glimGls.cc 3.3 KiB
Newer Older
Stephen Smith's avatar
Stephen Smith committed
/*  glimGls.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 <sstream>
Stephen Smith's avatar
Stephen Smith committed

#include "glimGls.h"
#include "miscmaths/miscmaths.h"
#include "utils/log.h"
David Flitney's avatar
David Flitney committed
#include "miscmaths/volume.h"
#include "miscmaths/volumeseries.h"
Stephen Smith's avatar
Stephen Smith committed

using namespace MISCMATHS;
Mark Woolrich's avatar
Mark Woolrich committed
using namespace Utilities;

namespace FILM {
Stephen Smith's avatar
Stephen Smith committed

Mark Woolrich's avatar
Mark Woolrich committed
  GlimGls::GlimGls(const int pnumTS, const int psizeTS, const int pnumParams) : 
    numTS(pnumTS),
    sizeTS(psizeTS),
    numParams(pnumParams),
    corrections(numParams*numParams,numTS),
    b(numParams, numTS),
    sigmaSquareds(numTS),
    dof(sizeTS - numParams)
  {
    I.ReSize(sizeTS);
    for(int i=1; i<=sizeTS; i++)
      {
	I(i,i)=1;
      }
  }
  
Mark Woolrich's avatar
Mark Woolrich committed
  void GlimGls::CleanUp()
  {
    corrections.CleanUp();
    sigmaSquareds.CleanUp();
    b.CleanUp();
    r.CleanUp();
  }

Stephen Smith's avatar
Stephen Smith committed
  void GlimGls::setData(const ColumnVector& y, const Matrix& x, const int ind)
    {
      // compute b
      //Matrix inv_xx = (x.t()*x).i();
      Matrix inv_xx = pinv(x.t()*x);
Stephen Smith's avatar
Stephen Smith committed
      b.Column(ind) = inv_xx*x.t()*y;

      // compute r
Mark Woolrich's avatar
Mark Woolrich committed
      Matrix R = I-x*inv_xx*x.t();
      r = R*y;
Stephen Smith's avatar
Stephen Smith committed

      // compute sigma squareds 
Mark Woolrich's avatar
Mark Woolrich committed
      //      sigmaSquareds(ind) = (r.t()*r/sizeTS).AsScalar();
      sigmaSquareds(ind) = (r.t()*r).AsScalar()/R.Trace();
Stephen Smith's avatar
Stephen Smith committed

      // set corrections
      SetCorrection(inv_xx, ind);
    }

Mark Woolrich's avatar
Mark Woolrich committed
  void GlimGls::Save(const VolumeInfo& pvolinfo, const ColumnVector& prethreshpos)
Stephen Smith's avatar
Stephen Smith committed
    {
      // Need to save b, sigmaSquareds, corrections and dof 
Mark Woolrich's avatar
Mark Woolrich committed
      Log& logger = LogSingleton::getInstance();
Mark Woolrich's avatar
Mark Woolrich committed
      VolumeInfo volinfo = pvolinfo;

Stephen Smith's avatar
Stephen Smith committed
      // b:
      Volume peVol;
      for(int i = 1; i <= numParams; i++)
	{
	  peVol = b.Row(i).AsColumn();
Mark Woolrich's avatar
Mark Woolrich committed
	  volinfo.intent_code = NIFTI_INTENT_ESTIMATE;
David Flitney's avatar
David Flitney committed
	  peVol.setInfo(volinfo);
Stephen Smith's avatar
Stephen Smith committed
	  peVol.setPreThresholdPositions(prethreshpos);
Mark Woolrich's avatar
Mark Woolrich committed
	  peVol.unthreshold();
Stephen Smith's avatar
Stephen Smith committed
	  
	  // Add param number to "pe" to create filename:
	  ostringstream osc;
	  osc << i;
Stephen Smith's avatar
Stephen Smith committed
	  
	  peVol.writeAsFloat(logger.getDir() + "/pe" + osc.str().c_str());
Stephen Smith's avatar
Stephen Smith committed
	}

      // sigmaSquareds:
Mark Woolrich's avatar
Mark Woolrich committed
      volinfo.intent_code = NIFTI_INTENT_ESTIMATE;
David Flitney's avatar
David Flitney committed
      sigmaSquareds.setInfo(volinfo);
Stephen Smith's avatar
Stephen Smith committed
      sigmaSquareds.setPreThresholdPositions(prethreshpos);
      sigmaSquareds.unthreshold();	
      sigmaSquareds.writeAsFloat(logger.getDir() + "/sigmasquareds");

      // dof:
      ColumnVector dofVec(1);
      dofVec = dof;
Mark Woolrich's avatar
Mark Woolrich committed
      write_ascii_matrix(logger.appendDir("dof"), dofVec);
Mark Woolrich's avatar
Mark Woolrich committed
      // corrections (are the pes correlation matrix (x.t()*x).i() reshapen to a vector):
David Flitney's avatar
David Flitney committed
      VolumeInfo newvolinfo = volinfo;
      newvolinfo.v = numParams*numParams;
Mark Woolrich's avatar
Mark Woolrich committed
      newvolinfo.intent_code = NIFTI_INTENT_NONE;
      //corrections.setInfo(newvolinfo);
      //corrections.setPreThresholdPositions(prethreshpos);
      //corrections.unthresholdSeries();
      //corrections.writeAsFloat(logger.getDir() + "/corrections");
      corrections.writeThresholdedSeriesAsFloat(newvolinfo,prethreshpos,logger.getDir() + "/corrections");
Stephen Smith's avatar
Stephen Smith committed
    }

  void GlimGls::SetCorrection(const Matrix& corr, const int ind)
    {
      Tracer ts("SetCorrection");

      // puts Matrix corr which is p*p into Matrix correction
      // as a p*p length row:
      int p = corr.Nrows();
      
      for (int i = 1; i <= p; i++)
	{
	  for (int j = 1; j <= p; j++)
	    {
Mark Woolrich's avatar
Mark Woolrich committed
	      corrections((i-1)*p + j, ind) = corr(i,j);