MNE-CPP  0.1.9
A Framework for Electrophysiology
compute_fwd_settings.cpp
1 
2 
3 #include "compute_fwd_settings.h"
4 
5 #include <stdio.h>
6 
7 using namespace Eigen;
8 using namespace FWDLIB;
9 
10 #define X 0
11 #define Y 1
12 #define Z 2
13 
14 #ifndef PROGRAM_VERSION
15 #define PROGRAM_VERSION "2.10"
16 #endif
17 
18 //=============================================================================================================
19 // STATIC DEFINITIONS
20 //=============================================================================================================
21 
22 //=============================================================================================================
23 // DEFINE MEMBER METHODS
24 //=============================================================================================================
25 
26 ComputeFwdSettings::ComputeFwdSettings()
27 {
28  initMembers();
29 }
30 
31 //=============================================================================================================
32 
33 ComputeFwdSettings::ComputeFwdSettings(int *argc,char **argv)
34 {
35  initMembers();
36 
37  if (!check_args(argc,argv))
38  return;
39 
40 // mne_print_version_info(stderr,argv[0],PROGRAM_VERSION,__DATE__,__TIME__);
41  printf("%s version %s compiled at %s %s\n",argv[0],PROGRAM_VERSION,__DATE__,__TIME__);
42 
43  checkIntegrity();
44 }
45 
46 //=============================================================================================================
47 
48 ComputeFwdSettings::~ComputeFwdSettings()
49 {
50  //ToDo Garbage collection
51 }
52 
53 //=============================================================================================================
54 
55 void ComputeFwdSettings::checkIntegrity()
56 {
57  if (srcname.isEmpty()) {
58  qCritical("Source space name is missing. Use the --src option to specify it.");
59  return;
60  }
61  if (!mri_head_ident) {
62  if (mriname.isEmpty() && transname.isEmpty()) {
63  qCritical("MRI <-> head coordinate transformation is missing. Use the --mri or --trans option to specify it.");
64  return;
65  }
66  }
67  if (measname.isEmpty()) {
68  qCritical("Source of coil and electrode locations is missing. Use the --meas option to specify it.");
69  return;
70  }
71  if (solname.isEmpty()) {
72  qCritical("Solution name is missing. Use the --fwd option to specify it.");
73  return;
74  }
75  if (! (include_meg || include_eeg)) {
76  qCritical("Employ the --meg and --eeg options to select MEG and/or EEG");
77  return;
78  }
79 }
80 
81 //=============================================================================================================
82 
83 void ComputeFwdSettings::initMembers()
84 {
85  // Init origin
86  r0 << 0.0f,0.0f,0.04f;
87 
88  filter_spaces = true;
89  accurate = false;
90  fixed_ori = false;
91  include_meg = false;
92  include_eeg = false;
93  compute_grad = false;
94  mindist = 0.0f;
95  coord_frame = FIFFV_COORD_HEAD;
96  do_all = false;
97  nlabel = 0;
98 
99  eeg_sphere_rad = 0.09f;
100  scale_eeg_pos = false;
101  use_equiv_eeg = true;
102  use_threads = true;
103 
104  pFiffInfo = Q_NULLPTR;
105  meg_head_t = Q_NULLPTR;
106 }
107 
108 //=============================================================================================================
109 
110 void ComputeFwdSettings::usage(char *name)
111 {
112  printf("usage : %s [options]\n",name);
113  printf("\t--meg to compute the MEG forward solution\n");
114  printf("\t--eeg to compute the EEG forward solution\n");
115  printf("\t--grad compute the gradient of the field with respect to the dipole coordinates as well\n");
116  printf("\t--fixed to calculate only for the source orientation given by the surface normals\n");
117  printf("\t--mricoord do calculations in MRI coordinates instead of head coordinates\n");
118  printf("\t--accurate use more accurate coil definitions in MEG forward computation\n");
119  printf("\t--src name specify the source space\n");
120  printf("\t--label name label file to select the sources (can have multiple of these)\n");
121  printf("\t--mri name take head/MRI coordinate transform from here (Neuromag MRI description file)\n");
122  printf("\t--trans name take head/MRI coordinate transform from here (text file)\n");
123  printf("\t--notrans head and MRI coordinate systems are identical.\n");
124  printf("\t--meas name take MEG sensor and EEG electrode locations from here\n");
125  printf("\t--bem name BEM model name\n");
126  printf("\t--origin x:y:z/mm use a sphere model with this origin (head coordinates/mm)\n");
127  printf("\t--eegscalp scale the electrode locations to the surface of the scalp when using a sphere model\n");
128  printf("\t--eegmodels name read EEG sphere model specifications from here.\n");
129  printf("\t--eegmodel name name of the EEG sphere model to use (default : Default)\n");
130  printf("\t--eegrad rad/mm radius of the scalp surface to use in EEG sphere model (default : %7.1f mm)\n",1000*eeg_sphere_rad);
131  printf("\t--mindist dist/mm minimum allowable distance of the sources from the inner skull surface.\n");
132  printf("\t--mindistout name Output the omitted source space points here.\n");
133  printf("\t--includeall Omit all source space checks\n");
134  printf("\t--all calculate forward solution in all nodes instead the selected ones only.\n");
135  printf("\t--fwd name save the solution here\n");
136  printf("\t--help print this info.\n");
137  printf("\t--version print version info.\n\n");
138  exit(1);
139 }
140 
141 //=============================================================================================================
142 
143 QString ComputeFwdSettings::build_command_line(QString old, QString new_item)
144 {
145  if (!new_item.isEmpty() && new_item.size() > 0) {
146  if (!old.isEmpty()) {
147  old += " ";
148  }
149  old += new_item;
150  }
151  return old;
152 }
153 
154 //=============================================================================================================
155 
156 bool ComputeFwdSettings::check_unrecognized_args(int argc, char **argv)
157 {
158  int k;
159 
160  if (argc > 1) {
161  printf("Unrecognized arguments : ");
162  for (k = 1; k < argc; k++)
163  printf("%s ",argv[k]);
164  printf("\n");
165  qCritical("Check the command line.");
166  return false;
167  }
168  return true;
169 }
170 
171 //=============================================================================================================
172 
173 bool ComputeFwdSettings::check_args (int *argc,char **argv)
174 {
175  int found;
176  char *last;
177 
178  if ((last = strrchr(argv[0],'/')) == NULL)
179  last = argv[0];
180  else
181  last++;
182  command = build_command_line(command,last);
183  for (int k = 0; k < *argc; k++) {
184  found = 0;
185  if (strcmp(argv[k],"--version") == 0) {
186  printf("%s version %s compiled at %s %s\n", argv[0],PROGRAM_VERSION,__DATE__,__TIME__);
187  exit(0);
188  }
189  else if (strcmp(argv[k],"--help") == 0) {
190  usage(argv[0]);
191  exit(1);
192  }
193  else if (strcmp(argv[k],"--meg") == 0) {
194  found = 1;
195  include_meg = true;
196  }
197  else if (strcmp(argv[k],"--eeg") == 0) {
198  found = 1;
199  include_eeg = true;
200  }
201  else if (strcmp(argv[k],"--grad") == 0) {
202  found = 1;
203  compute_grad = true;
204  }
205  else if (strcmp(argv[k],"--all") == 0) {
206  found = 1;
207  do_all = true;
208  }
209  else if (strcmp(argv[k],"--accurate") == 0) {
210  found = 1;
211  accurate = true;
212  }
213  else if (strcmp(argv[k],"--fixed") == 0) {
214  found = 1;
215  fixed_ori = true;
216  }
217  else if (strcmp(argv[k],"--src") == 0) {
218  found = 2;
219  if (k == *argc - 1) {
220  qCritical("--src: argument required.");
221  return false;
222  }
223  srcname = QString(argv[k+1]);
224  }
225  else if (strcmp(argv[k],"--mri") == 0) {
226  found = 2;
227  if (k == *argc - 1) {
228  qCritical("--mri: argument required.");
229  return false;
230  }
231  mri_head_ident = false;
232  mriname = QString(argv[k+1]);
233  transname.clear();
234  }
235  else if (strcmp(argv[k],"--trans") == 0) {
236  found = 2;
237  if (k == *argc - 1) {
238  qCritical("--trans: argument required.");
239  return false;
240  }
241  mri_head_ident = false;
242  transname = QString(argv[k+1]);
243  mriname.clear();
244  }
245  else if (strcmp(argv[k],"--notrans") == 0) {
246  found = 1;
247  mri_head_ident = true;
248  mriname.clear();
249  transname.clear();
250  }
251  else if (strcmp(argv[k],"--meas") == 0) {
252  found = 2;
253  if (k == *argc - 1) {
254  qCritical("--meas: argument required.");
255  return false;
256  }
257  measname = QString(argv[k+1]);
258  }
259  else if (strcmp(argv[k],"--bem") == 0) {
260  found = 2;
261  if (k == *argc - 1) {
262  qCritical("--bem: argument required.");
263  return false;
264  }
265  bemname = QString(argv[k+1]);
266  }
267  else if (strcmp(argv[k],"--origin") == 0) {
268  found = 2;
269  if (k == *argc - 1) {
270  qCritical("--origin: argument required.");
271  return false;
272  }
273  if (sscanf(argv[k+1],"%f:%f:%f",r0[X],r0[Y],r0[Z]) != 3) {
274  qCritical("Could not interpret the origin.");
275  return false;
276  }
277  r0[X] = r0[X]/1000.0f;
278  r0[Y] = r0[Y]/1000.0f;
279  r0[Z] = r0[Z]/1000.0f;
280  }
281  else if (strcmp(argv[k],"--eegrad") == 0) {
282  found = 2;
283  if (k == *argc - 1) {
284  qCritical("--eegrad: argument required.");
285  return false;
286  }
287  if (sscanf(argv[k+1],"%g",&eeg_sphere_rad) != 1) {
288  qCritical("Incomprehensible radius : %s",argv[k+1]);
289  return false;
290  }
291  if (eeg_sphere_rad <= 0) {
292  qCritical("Radius must be positive");
293  return false;
294  }
295  eeg_sphere_rad = eeg_sphere_rad/1000.0f;
296  }
297  else if (strcmp(argv[k],"--eegmodels") == 0) {
298  found = 2;
299  if (k == *argc - 1) {
300  qCritical("--eegmodels: argument required.");
301  return false;
302  }
303  eeg_model_file = QString(argv[k+1]);
304  }
305  else if (strcmp(argv[k],"--eegmodel") == 0) {
306  found = 2;
307  if (k == *argc - 1) {
308  qCritical("--eegmodel: argument required.");
309  return false;
310  }
311  eeg_model_name = QString(argv[k+1]);
312  }
313  else if (strcmp(argv[k],"--eegscalp") == 0) {
314  found = 1;
315  scale_eeg_pos = true;
316  }
317  else if (strcmp(argv[k],"--mindist") == 0) {
318  found = 2;
319  if (k == *argc - 1) {
320  qCritical("--mindist: argument required.");
321  return false;
322  }
323  if (sscanf(argv[k+1],"%f",&mindist) != 1) {
324  qCritical("Could not interpret the distance.");
325  return false;
326  }
327  if (mindist <= 0.0)
328  mindist = 0.0f;
329  mindist = mindist/1000.0f;
330  }
331  else if (strcmp(argv[k],"--includeall") == 0) {
332  found = 1;
333  filter_spaces = false;
334  }
335  else if (strcmp(argv[k],"--mindistout") == 0) {
336  found = 2;
337  if (k == *argc - 1) {
338  qCritical("--mindistout: argument required.");
339  return false;
340  }
341  mindistoutname = QString(argv[k+1]);
342  }
343  else if (strcmp(argv[k],"--mricoord") == 0) {
344  found = 1;
345  coord_frame = FIFFV_COORD_MRI;
346  }
347  else if (strcmp(argv[k],"--fwd") == 0) {
348  found = 2;
349  if (k == *argc - 1) {
350  qCritical("--fwd: argument required.");
351  return false;
352  }
353  solname = QString(argv[k+1]);
354  }
355  else if (strcmp(argv[k],"--label") == 0) {
356  found = 2;
357  if (k == *argc - 1) {
358  qCritical("--label: argument required.");
359  return false;
360  }
361  labels.append(QString(argv[k+1]));
362  nlabel++;
363  }
364  if (found) {
365  for (int p = k; p < k + found; p++)
366  command = build_command_line(command,argv[p]);
367  for (int p = k; p < *argc-found; p++)
368  argv[p] = argv[p+found];
369  *argc = *argc - found;
370  k = k - found;
371  }
372  }
373  return check_unrecognized_args(*argc,argv);
374 }
compute_fwd_settings.h
Compute Forward Setting class declaration.
k
int k
Definition: fiff_tag.cpp:322