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

    Mark Woolrich, FMRIB Image Analysis Group

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

/*  CCOPYRIGHT  */

#include "paradigm.h"
Christian Beckmann's avatar
Christian Beckmann committed
#include "log.h"
Stephen Smith's avatar
Stephen Smith committed
#include "miscmaths.h"
Mark Woolrich's avatar
Mark Woolrich committed
#include <fstream>

using namespace FILM;

namespace FILM {

  void Paradigm::read_vest_waveform(string p_fname, Matrix& p_mat)
  {
    ifstream in;
    in.open(p_fname.c_str(), ios::in);
    
    if(!in)
      {
	cerr << "Unable to open " << p_fname << endl;
	throw;
      }
    
    int numWaves = 0;
    int numPoints = 0;
  
    string str;

    while(true)
      {
	in >> str;
	if(str == "/Matrix")
	  break;
	else if(str == "/NumWaves")
	  {
	    in >> numWaves;
	  }
	else if(str == "/NumPoints" || str == "/NumContrasts")
	  {
	    in >> numPoints;
	  }
      }

    if(numWaves != 0)
      {
	p_mat.ReSize(numPoints, numWaves);
      }
    else
      {
	numWaves = p_mat.Ncols();
	numPoints = p_mat.Nrows();
      }
      
    for(int i = 1; i <= numPoints; i++)
      {
	for(int j = 1; j <= numWaves; j++)    
	  {
	    in >> p_mat(i,j);
	  }
      }

    in.close();
  }
  
  void Paradigm::load(const string& p_paradfname, const string& p_tcontrastfname, const string& p_fcontrastfname, bool p_blockdesign, int p_sizets)
Stephen Smith's avatar
Stephen Smith committed
    {
      if(p_paradfname != "")
Mark Woolrich's avatar
Mark Woolrich committed
	read_vest_waveform(p_paradfname, designMatrix);
Stephen Smith's avatar
Stephen Smith committed

Mark Woolrich's avatar
Mark Woolrich committed
      if(p_tcontrastfname != "")
	read_vest_waveform(p_tcontrastfname, tcontrasts);       

      if(p_fcontrastfname != "")
	read_vest_waveform(p_fcontrastfname, fcontrasts);   
Stephen Smith's avatar
Stephen Smith committed
       
      // Check time series match up with design
      if(p_paradfname != "" && designMatrix.Nrows() != p_sizets)
	{
	  cerr << "Num scans = "<< p_sizets << ", design matrix rows = " << designMatrix.Nrows() << endl;
	  throw Exception("size of design matrix does not match number of scans\n");
	}
      
Mark Woolrich's avatar
Mark Woolrich committed
      // Check contrasts match
      if(p_tcontrastfname != "" &&  p_fcontrastfname != "" && tcontrasts.Nrows() != fcontrasts.Ncols())
	{ 
	  cerr << "tcontrasts.Nrows()  = " << tcontrasts.Nrows() << endl;
	  cerr << "fcontrasts.Ncols()  = " << fcontrasts.Ncols() << endl;
	  throw Exception("size of tcontrasts does not match fcontrasts\n");
	}

Stephen Smith's avatar
Stephen Smith committed
      // Check contrast matches design matrix
Mark Woolrich's avatar
Mark Woolrich committed
      if(p_paradfname != "" &&  p_tcontrastfname != "" && designMatrix.Ncols() != tcontrasts.Ncols())
Stephen Smith's avatar
Stephen Smith committed
	 { 
Mark Woolrich's avatar
Mark Woolrich committed
	  cerr << "Num tcontrast cols  = " << tcontrasts.Ncols() << ", design matrix cols = " 
Stephen Smith's avatar
Stephen Smith committed
	    << designMatrix.Ncols() << endl;
Mark Woolrich's avatar
Mark Woolrich committed
	  throw Exception("size of design matrix does not match t contrasts\n");
Stephen Smith's avatar
Stephen Smith committed
	 }

      if(p_blockdesign)
	{
	  // Need to adjust amplitude from (-1,1) to (0,1)
	  designMatrix = (designMatrix + 1)/2;
	}
    }