MNE-CPP  0.1.9
A Framework for Electrophysiology
mne_sss_data.cpp
Go to the documentation of this file.
1 //=============================================================================================================
37 //=============================================================================================================
38 // INCLUDES
39 //=============================================================================================================
40 
41 #include "mne_sss_data.h"
42 #include <fiff/fiff_file.h>
43 #include <fiff/fiff_constants.h>
44 #include <fiff/fiff_stream.h>
45 #include <fiff/fiff_tag.h>
46 
47 //============================= dot.h =============================
48 
49 #define X 0
50 #define Y 1
51 #define Z 2
52 
53 #define VEC_COPY(to,from) {\
54  (to)[X] = (from)[X];\
55  (to)[Y] = (from)[Y];\
56  (to)[Z] = (from)[Z];\
57  }
58 
59 //=============================================================================================================
60 // QT INCLUDES
61 //=============================================================================================================
62 
63 #include <QFile>
64 
65 //=============================================================================================================
66 // USED NAMESPACES
67 //=============================================================================================================
68 
69 using namespace Eigen;
70 using namespace MNELIB;
71 using namespace FIFFLIB;
72 
73 //=============================================================================================================
74 // DEFINE MEMBER METHODS
75 //=============================================================================================================
76 
77 MneSssData::MneSssData()
79 , coord_frame(FIFFV_COORD_UNKNOWN)
80 , nchan(0)
81 , out_order(0)
82 , in_order(0)
83 , comp_info(NULL)
84 , ncomp(0)
85 , in_nuse(0)
86 , out_nuse(0)
87 {
88 }
89 
90 //=============================================================================================================
91 
92 MneSssData::MneSssData(const MneSssData& p_MneSssData)
93 : job(p_MneSssData.job)
94 , coord_frame(p_MneSssData.coord_frame)
95 , nchan(p_MneSssData.nchan)
96 , out_order(p_MneSssData.out_order)
97 , in_order(p_MneSssData.in_order)
98 , ncomp(p_MneSssData.ncomp)
99 , in_nuse(p_MneSssData.in_nuse)
100 , out_nuse(p_MneSssData.out_nuse)
101 {
102  origin[0] = p_MneSssData.origin[0];
103  origin[1] = p_MneSssData.origin[1];
104  origin[2] = p_MneSssData.origin[2];
105 
106  comp_info = (int *)malloc(ncomp*sizeof(int));
107 
108  for (int k = 0; k < ncomp; k++)
109  comp_info[k] = p_MneSssData.comp_info[k];
110 }
111 
112 //=============================================================================================================
113 
115 {
116  if ((char *)(comp_info) != NULL)
117  free((char *)(comp_info));
118 }
119 
120 //=============================================================================================================
121 
123 {
124  QFile file(name);
125  FiffStream::SPtr stream(new FiffStream(&file));
126 
127  MneSssData* s = NULL;
128 
129  if(stream->open())
130  s = read_sss_data_from_node(stream,stream->dirtree());
131 
132  stream->close();
133  return s;
134 }
135 
136 //=============================================================================================================
137 
138 MneSssData *MneSssData::read_sss_data_from_node(QSharedPointer<FiffStream> &stream, const QSharedPointer<FiffDirNode> &start)
139 {
140  MneSssData* s = new MneSssData();
141  QList<FiffDirNode::SPtr> sss;
142  FiffDirNode::SPtr node;
143  FiffTag::SPtr t_pTag;
144  float *r0;
145  int j,p,q,n;
146  /*
147  * Locate the SSS information
148  */
149  sss = start->dir_tree_find(FIFFB_SSS_INFO);
150  if (sss.size() > 0) {
151  node = sss[0];
152  /*
153  * Read the SSS information, require all tags to be present
154  */
155  if (!node->find_tag(stream, FIFF_SSS_JOB, t_pTag))
156  goto bad;
157  s->job = *t_pTag->toInt();
158 
159  if (!node->find_tag(stream, FIFF_SSS_FRAME, t_pTag))
160  goto bad;
161  s->coord_frame = *t_pTag->toInt();
162 
163  if (!node->find_tag(stream, FIFF_SSS_ORIGIN, t_pTag))
164  goto bad;
165  r0 = t_pTag->toFloat();
166  VEC_COPY(s->origin,r0);
167 
168  if (!node->find_tag(stream, FIFF_SSS_ORD_IN, t_pTag))
169  goto bad;
170  s->in_order = *t_pTag->toInt();
171 
172  if (!node->find_tag(stream, FIFF_SSS_ORD_OUT, t_pTag))
173  goto bad;
174  s->out_order = *t_pTag->toInt();
175 
176  if (!node->find_tag(stream, FIFF_SSS_NMAG, t_pTag))
177  goto bad;
178  s->nchan = *t_pTag->toInt();
179 
180  if (!node->find_tag(stream, FIFF_SSS_COMPONENTS, t_pTag))
181  goto bad;
182  qDebug() << "ToDo: Check whether comp_info contains the right stuff!!! - use VectorXi instead";
183  s->comp_info = t_pTag->toInt();
184  s->ncomp = t_pTag->size()/sizeof(fiff_int_t);
185 
186  if (s->ncomp != (s->in_order*(2+s->in_order) + s->out_order*(2+s->out_order))) {
187  printf("Number of SSS components does not match the expansion orders listed in the file");
188  goto bad;
189  }
190  /*
191  * Count the components in use
192  */
193  for (j = 0, n = 3, p = 0; j < s->in_order; j++, n = n + 2) {
194  for (q = 0; q < n; q++, p++)
195  if (s->comp_info[p])
196  s->in_nuse++;
197  }
198  for (j = 0, n = 3; j < s->out_order; j++, n = n + 2) {
199  for (q = 0; q < n; q++, p++)
200  s->out_nuse++;
201  }
202  }
203  /*
204  * There it is!
205  */
206  return s;
207 
208 bad : {
209  /*
210  * Not entirely happy
211  */
212  if (s)
213  delete s;
214  return NULL;
215  }
216 }
217 
218 //=============================================================================================================
219 namespace MNELIB
220 {
221 
222 typedef struct {
223  int frame;
224  const char *name;
226 
227 }
228 
229 //=============================================================================================================
230 
231 const char *mne_coord_frame_name_1(int frame)
232 {
233  static frameNameRec_1 frames[] = {
234  {FIFFV_COORD_UNKNOWN,"unknown"},
235  {FIFFV_COORD_DEVICE,"MEG device"},
236  {FIFFV_COORD_ISOTRAK,"isotrak"},
237  {FIFFV_COORD_HPI,"hpi"},
238  {FIFFV_COORD_HEAD,"head"},
239  {FIFFV_COORD_MRI,"MRI (surface RAS)"},
240  {FIFFV_MNE_COORD_MRI_VOXEL, "MRI voxel"},
241  {FIFFV_COORD_MRI_SLICE,"MRI slice"},
242  {FIFFV_COORD_MRI_DISPLAY,"MRI display"},
243  {FIFFV_MNE_COORD_CTF_DEVICE,"CTF MEG device"},
244  {FIFFV_MNE_COORD_CTF_HEAD,"CTF/4D/KIT head"},
245  {FIFFV_MNE_COORD_RAS,"RAS (non-zero origin)"},
246  {FIFFV_MNE_COORD_MNI_TAL,"MNI Talairach"},
247  {FIFFV_MNE_COORD_FS_TAL_GTZ,"Talairach (MNI z > 0)"},
248  {FIFFV_MNE_COORD_FS_TAL_LTZ,"Talairach (MNI z < 0)"},
249  {-1,"unknown"}
250  };
251  int k;
252  for (k = 0; frames[k].frame != -1; k++) {
253  if (frame == frames[k].frame)
254  return frames[k].name;
255  }
256  return frames[k].name;
257 }
258 
259 //=============================================================================================================
260 
261 void MneSssData::print(FILE *f) const
262 {
263  int j,p,q,n;
264 
265  fprintf(f,"job : %d\n",this->job);
266  fprintf(f,"coord frame : %s\n",mne_coord_frame_name_1(this->coord_frame));
267  fprintf(f,"origin : %6.1f %6.1f %6.1f mm\n",1000*this->origin[0],1000*this->origin[1],1000*this->origin[2]);
268  fprintf(f,"in order : %d\n",this->in_order);
269  fprintf(f,"out order : %d\n",this->out_order);
270  fprintf(f,"nchan : %d\n",this->nchan);
271  fprintf(f,"ncomp : %d\n",this->ncomp);
272  fprintf(f,"in nuse : %d\n",this->in_nuse);
273  fprintf(f,"out nuse : %d\n",this->out_nuse);
274  fprintf(f,"comps : ");
275  /*
276  * This produces the same output as maxfilter
277  */
278  for (j = 0, n = 3, p = 0; j < this->in_order; j++, n = n + 2) {
279  if (j > 0)
280  fprintf(f,";");
281  for (q = 0; q < n; q++, p++)
282  fprintf(f,"%1d",this->comp_info[p]);
283  }
284  fprintf(f,"//");
285  for (j = 0, n = 3; j < this->out_order; j++, n = n + 2) {
286  if (j > 0)
287  fprintf(f,";");
288  for (q = 0; q < n; q++, p++)
289  fprintf(f,"%1d",this->comp_info[p]);
290  }
291  fprintf(f,"\n");
292  return;
293 }
FIFFV_MNE_COORD_MRI_VOXEL
#define FIFFV_MNE_COORD_MRI_VOXEL
Definition: fiff_constants.h:482
MNELIB::MneSssData::nchan
int nchan
Definition: mne_sss_data.h:145
FIFF_SSS_ORD_IN
#define FIFF_SSS_ORD_IN
Definition: fiff_file.h:530
MNELIB::MneSssData::comp_info
int * comp_info
Definition: mne_sss_data.h:148
MNELIB::MneSssData::ncomp
int ncomp
Definition: mne_sss_data.h:149
FIFFV_MNE_COORD_CTF_HEAD
#define FIFFV_MNE_COORD_CTF_HEAD
Definition: fiff_constants.h:481
MNELIB::MneSssData::read_sss_data
static MneSssData * read_sss_data(const QString &name)
Definition: mne_sss_data.cpp:122
MNELIB::MneSssData::job
int job
Definition: mne_sss_data.h:142
FIFFV_MNE_COORD_CTF_DEVICE
#define FIFFV_MNE_COORD_CTF_DEVICE
Definition: fiff_constants.h:480
fiff_tag.h
FiffTag class declaration, which provides fiff tag I/O and processing methods.
FIFFLIB::FiffStream::SPtr
QSharedPointer< FiffStream > SPtr
Definition: fiff_stream.h:107
MNELIB::frameNameRec_1
Definition: mne_sss_data.cpp:222
mne_sss_data.h
MNE SSS Data (MneSssData) class declaration.
MNELIB::MneSssData::out_nuse
int out_nuse
Definition: mne_sss_data.h:151
FIFFLIB::FiffDirNode::SPtr
QSharedPointer< FiffDirNode > SPtr
Definition: fiff_dir_node.h:76
MNELIB::MneSssData::in_nuse
int in_nuse
Definition: mne_sss_data.h:150
MNELIB::MneSssData::read_sss_data_from_node
static MneSssData * read_sss_data_from_node(QSharedPointer< FIFFLIB::FiffStream > &stream, const QSharedPointer< FIFFLIB::FiffDirNode > &start)
Definition: mne_sss_data.cpp:138
fiff_stream.h
FiffStream class declaration.
k
int k
Definition: fiff_tag.cpp:322
FIFF_SSS_COMPONENTS
#define FIFF_SSS_COMPONENTS
Definition: fiff_file.h:533
MNELIB::MneSssData::print
void print(FILE *f) const
Definition: mne_sss_data.cpp:261
FIFFV_MNE_COORD_MNI_TAL
#define FIFFV_MNE_COORD_MNI_TAL
Definition: fiff_constants.h:484
FIFFB_SSS_INFO
#define FIFFB_SSS_INFO
Definition: fiff_file.h:427
MNELIB::MneSssData::~MneSssData
~MneSssData()
Definition: mne_sss_data.cpp:114
fiff_constants.h
Fiff constants.
MNELIB::MneSssData
MNE SSS Data description.
Definition: mne_sss_data.h:82
MNELIB::MneSssData::MneSssData
MneSssData()
Definition: mne_sss_data.cpp:77
FIFFLIB::FiffTag::SPtr
QSharedPointer< FiffTag > SPtr
Definition: fiff_tag.h:152
FIFFV_MNE_COORD_FS_TAL_LTZ
#define FIFFV_MNE_COORD_FS_TAL_LTZ
Definition: fiff_constants.h:486
FIFFV_MNE_COORD_RAS
#define FIFFV_MNE_COORD_RAS
Definition: fiff_constants.h:483
FIFFV_MNE_COORD_FS_TAL_GTZ
#define FIFFV_MNE_COORD_FS_TAL_GTZ
Definition: fiff_constants.h:485
FIFFLIB::FiffStream
FIFF File I/O routines.
Definition: fiff_stream.h:104
MNELIB::MneSssData::in_order
int in_order
Definition: mne_sss_data.h:147
MNELIB::MneSssData::out_order
int out_order
Definition: mne_sss_data.h:146
fiff_file.h
Header file describing the numerical values used in fif files.
FIFF_SSS_ORD_OUT
#define FIFF_SSS_ORD_OUT
Definition: fiff_file.h:531
FIFF_SSS_JOB
#define FIFF_SSS_JOB
Definition: fiff_file.h:528
FIFF_SSS_NMAG
#define FIFF_SSS_NMAG
Definition: fiff_file.h:532
FIFF_SSS_ORIGIN
#define FIFF_SSS_ORIGIN
Definition: fiff_file.h:529
MNELIB::MneSssData::coord_frame
int coord_frame
Definition: mne_sss_data.h:143
MNELIB::MneSssData::origin
float origin[3]
Definition: mne_sss_data.h:144
FIFFV_SSS_JOB_NOTHING
#define FIFFV_SSS_JOB_NOTHING
Definition: fiff_file.h:538
FIFF_SSS_FRAME
#define FIFF_SSS_FRAME
Definition: fiff_file.h:527