-
Notifications
You must be signed in to change notification settings - Fork 9
/
RtExperiment.cpp
552 lines (428 loc) · 13.5 KB
/
RtExperiment.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
/*=========================================================================
* definition of a class that holds information about the loaded experiment
*
* Copyright 2007-2013, the MURFI dev team.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0.txt
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*=========================================================================*/
#include"RtExperiment.h"
#include"RtConductor.h"
#include"RtConfigFmriExperiment.h"
#include"RtConfigFmriRun.h"
// site specific defines (siteID, etc)
#include"site_config.h"
#include"ace/Get_Opt.h"
// limit
#include"RtLimit.h"
// platform independent filesystem access
#include<boost/filesystem.hpp>
#include<boost/filesystem/operations.hpp>
using namespace boost::filesystem;
#include"util/timer/timer.h"
//////////////////////////////////////////////////////////////////////////////
// experiment data
//////////////////////////////////////////////////////////////////////////////
namespace {
// experiment configuration
RtConfigFmriExperiment config;
// one or the other of these should be set to read the configuration
string confFilename;
string confXmlStr;
string defaultConfigXML(
"<?xml version='1.0' encoding='UTF-8'?>"
"<scanner>"
" <option name='disabled'> false </option>"
" <option name='receiveImages'> true </option>"
" <option name='saveImages'> true </option>"
" <option name='port'> 15000 </option>"
" <option name='onlyReadMoCo'> false </option>"
" <option name='unmosaic'> true </option>"
"</scanner>"
"<infoserver>"
" <option name='disabled'> false </option>"
" <option name='port'> 15001 </option>"
"</infoserver>"
);
// store and allow access to collected and computed data
RtDataStore dataStore;
// tcpip interface to experiment information
RtInfoServer *infoServer = NULL;
// tcpip interface to scanner input
RtInputScannerImages scannerInput;
// conductor to execute fmri runs
RtConductor *conductor = NULL;
// raw data identifiers that we've seen (need for knowing which data
// streams are novel)
map<string, unsigned int> uids;
// number of unique input data series we've seen
unsigned int numExistingSeries;
// unique id for this study (date and time of first initialization)
unsigned int studyID;
// list of mask filenames to align
vector<string> masksToAlign;
// timers
timer experimentTimer;
timer computeTimer;
string execName;
string subjectName;
string subjectsDir;
unsigned int numDataPointsForErrEst = 0;
} // anonymous namespace
//* methods for retreiving info about the the experiment *//
// get the unique ID number for this study
unsigned int getExperimentStudyID() {
return studyID;
}
// get the unique ID number for this image series UID
// checks if this uid exists
// if so returns its index+1
// if not adds it to the end and returns the length for the list
unsigned int getSeriesNumFromUID(char *uid) {
// serach for the uid
map<string, unsigned int>::iterator i = uids.find(uid);
unsigned int seriesNum = 0;
if(i == uids.end()) { // not found
string s(uid);
uids[s] = uids.size()+1+numExistingSeries;
seriesNum = uids.size()+numExistingSeries;
}
else {
seriesNum = (*i).second;
}
if(DEBUG_LEVEL & ADVANCED) {
cerr << "getsernumfromuid: uid=" << uid << " found=" << (i!=uids.end())
<< " series num=" << seriesNum << endl;
}
return seriesNum;
}
// get a new unique series number
unsigned int getNextUniqueSeriesNum() {
string max = "A";
map<string, unsigned int>::iterator i = uids.begin();
for(; i != uids.end(); i++) {
if((*i).first >= max) {
max = (*i).first;
}
}
// add "A" to the max string
string s(max + "A");
uids[s] = uids.size()+1+numExistingSeries;
return uids.size()+numExistingSeries;
}
// reset and start the compute timer
bool startComputeTimer() {
if(computeTimer.is_running()) {
return false;
}
computeTimer.restart();
return true;
}
// stop the compute timer and return the elapsed time
double stopComputeTimer() {
if(!computeTimer.is_running()) {
return -1;
}
computeTimer.stop();
return computeTimer.elapsed_time();
}
// get the current experiment elapsed time in ms
double getExperimentElapsedTime() {
return 1000*experimentTimer.elapsed_time();
}
// get the configuration for this experiment
RtConfigFmriExperiment &getExperimentConfig() {
return config;
}
// get the configuration filename for this experiment
string getExperimentConfigFile() {
return confFilename;
}
// get the data store for this experiment
RtDataStore &getDataStore() {
return dataStore;
}
// get the conductor for this experiment
RtConductor &getConductor() {
return *conductor;
}
unsigned int getNumDataPointsForErrEst() {
return numDataPointsForErrEst;
}
// initialize the experiment (call before the first run is prepared)
// returns true for success
bool initExperiment() {
cout << "intializing experiment" << endl;
// set the start time of the experiment
experimentTimer.restart();
// make the study id
ACE_Date_Time t;
char str[] = "yyyyhhmmss";
sprintf(str, "%04ld%02ld%02ld%02ld",
t.year(), t.hour(), t.minute(), t.second());
RtConfigVal::convert<unsigned int>(studyID,str);
// parse the configuration
if(!confFilename.empty()) { // look first to a file
cout << "parsing config file...";
config.set("study:subject:name", subjectName);
config.set("study:subjectsDir", subjectsDir);
if(!config.parseConfigFile(confFilename)) {
cout << "failed" << endl;
cerr << "ERROR: failed to parse config file: " << confFilename << endl;
return false;
}
cout << "done" << endl;
}
else { // next look to a string
if(confXmlStr.empty()) {
confXmlStr = defaultConfigXML;
}
// TODO: what to do when subject or subjects dir is set in the
// config string?
config.set("study:subject:name", subjectName);
config.set("study:subjectsDir", subjectsDir);
cout << "parsing config xml string...";
if(!config.parseConfigStr(confXmlStr)) {
cout << "failed" << endl;
cerr << "ERROR: failed to parse config string" << confXmlStr << endl;
return false;
}
cout << "done" << endl;
}
// count existing series for this experiment
cout << "searching for existing series... " << endl;
numExistingSeries = -1;
path p;
do {
numExistingSeries++;
p.operator=(config.getVolFilename(numExistingSeries+1,1));
} while(exists(p) && is_regular(p)
&& numExistingSeries < TOO_MANY_EXISTING_SERIES);
cout << "found " << numExistingSeries << " existing series" << endl;
// start info server
infoServer = new RtInfoServer();
if(!infoServer->open(config)) {
cerr << "ERROR: could not initialize info server" << endl;
}
else {
infoServer->activate(); // start the info server thread
}
if(!scannerInput.open(config)) {
cerr << "ERROR: could not add scanner input" << endl;
}
else {
scannerInput.activate(); // start the scanner input thread
}
return true;
}
// deinitialize the experiment (call after last run is complete)
// returns true for success
bool deinitExperiment() {
cout << "deintializing experiment" << endl;
// set the start time of the experiment
experimentTimer.stop();
// stop info server
if(NULL != infoServer && !infoServer->close()) {
cerr << "ERROR: could not deinitialize info server" << endl;
}
else {
cout << "stopped the infoserver" << endl;
}
// stop info server
if(!scannerInput.close()) {
cerr << "ERROR: could not deinitialize scanner input" << endl;
}
else {
cout << "stopped the scanner image listener" << endl;
}
// delete stuff
delete conductor;
delete infoServer;
return true;
}
// initialize and run the backend computation system
int executeRun(RtConfigFmriRun &conf) {
numDataPointsForErrEst = conf.get(
"processor:current-activation:numDataPointsForErrEst");
// make sure only one backend is running at a time
if(conductor != NULL && conductor->isRunning()) {
cerr << "an instance of the backend computation is already running. "
<< "can't start another" << endl;
return 1;
}
else if(conductor != NULL) {
delete conductor;
conductor = NULL;
}
// reinitialize scanner listener
scannerInput.init(conf);
// clear align mask names
masksToAlign.clear();
conductor = new RtConductor();
// add the inputs and outputs to the conductor
conductor->addExistingInput(&scannerInput);
if(NULL != infoServer) {
conductor->addExistingOutput(infoServer);
}
conductor->configure(conf);
conductor->activate();
return 0;
}
// initialize and run the backend computation system and block
int executeRunBlocking(RtConfigFmriRun &conf) {
// make sure only one backend is running at a time
if(conductor != NULL && conductor->isRunning()) {
cerr << "an instance of the backend computation is already running. "
<< "can't start another" << endl;
return 1;
}
else if(conductor != NULL) {
delete conductor;
conductor = NULL;
}
// reinitialize scanner listener
scannerInput.init(conf);
conductor = new RtConductor();
// add the inputs and outputs to the conductor
conductor->addExistingInput(&scannerInput);
if(NULL != infoServer) {
conductor->addExistingOutput(infoServer);
}
conductor->configure(conf);
conductor->svc();
return 0;
}
// prints the usage info for the realtime system
void printUsage() {
cout << "usage: " << endl << execName
<< " [-f conffile | -s confxmlstr]"
<< endl;
}
// parse command line args
bool parseArgs(int argc, char **args) {
ACE_TRACE(("parseArgs"));
execName = args[0];
// set up short options
static const ACE_TCHAR options[] = ACE_TEXT (":f:c:s:h?");
ACE_Get_Opt cmdOpts(argc, args, options, 1, 0,
ACE_Get_Opt::PERMUTE_ARGS, 1);
// set up long options
cmdOpts.long_option("conffile", 'f', ACE_Get_Opt::ARG_REQUIRED);
cmdOpts.long_option("conffile", 'c', ACE_Get_Opt::ARG_REQUIRED);
cmdOpts.long_option("confxmlstr", 's', ACE_Get_Opt::ARG_REQUIRED);
cmdOpts.long_option("help", 'h', ACE_Get_Opt::NO_ARG);
cmdOpts.long_option("help", '?', ACE_Get_Opt::NO_ARG);
// handle options
for(int option; (option = cmdOpts ()) != EOF; ) {
if(DEBUG_LEVEL & MODERATE) {
cout << "-" << (char) option << " " << cmdOpts.opt_arg() << endl;
}
switch(option) {
case 'f':
case 'c':
confFilename = cmdOpts.opt_arg();
break;
case 's':
confXmlStr = cmdOpts.opt_arg();
break;
case 'h':
case '?':
return false; // automatically prints usage
case ':':
cerr << "ERROR: -" << cmdOpts.opt_opt()
<< " requires an argument" << endl;
return false;
default:
cerr << "ERROR: unknown command line parameter: " << option << endl;
return false;
}
}
// // parse the xml config (file or string)
// if(confFilename.empty() && confXmlStr.empty()) {
// cerr << "ERROR: no configuration specified" << endl;
// return false;
// }
// else if(DEBUG_LEVEL & BASIC) {
// cout << "config file is: " << confFilename << endl
// << "config str is: " << confXmlStr << endl;
// }
return true;
}
bool initializeSystem(int argc, char** args) {
// get subject's directory
if (getenv("MURFI_SUBJECT_NAME") == NULL ||
getenv("MURFI_SUBJECTS_DIR") == NULL) {
cout << "ERROR: The environment variables MURFI_SUBJECT_NAME and "
<< "MURFI_SUBJECTS_DIR must be set."
<< endl;
return false;
}
subjectName = getenv("MURFI_SUBJECT_NAME");
subjectsDir = getenv("MURFI_SUBJECTS_DIR");
// setup arguments
if(!parseArgs(argc, args)) {
printUsage();
return false;
}
return true;
}
int murfi(int argc, char** args) {
ACE_TRACE(("ACE_TMAIN"));
// initialize the system
if (!initializeSystem(argc, args)) {
return 1;
}
// initialize experiment
if(!initExperiment()) {
cerr << "ERROR: experiment initialization failed. check your config"
<< endl;
return false;
}
int result = 0;
// if the gui frontend is being used, give it control (confused yet?)
if((config.isSet("gui:disabled") &&
config.get("gui:disabled")==false) ||
(config.isSet("oldgui:disabled") && // "backward" compatibility
config.get("oldgui:disabled")==false)) {
#ifndef USE_FRONTEND
cerr << "ERROR: this build does not support the gui frontend" << endl;
return 1;
#endif
return 0;
}
else { // just run once in the terminal with the passed config
RtConfigFmriRun runConf;
runConf.parseConfigFile(confFilename);
conductor = new RtConductor();
scannerInput.init(runConf);
conductor->addExistingInput(&scannerInput);
if(NULL != infoServer) {
conductor->addExistingOutput(infoServer);
}
conductor->configure(runConf);
result = conductor->svc();
}
// deinitialize experiment
if(!deinitExperiment()) {
cerr << "ERROR: experiment deinitialization failed."
<< endl;
result = 1;
}
return result;
}
// main entry function
#ifndef USE_FRONTEND
int ACE_TMAIN(int argc, char **args) {
return murfi(argc, args);
}
#endif