-
Notifications
You must be signed in to change notification settings - Fork 9
/
RtConfigFmriExperiment.cpp
371 lines (301 loc) · 10.5 KB
/
RtConfigFmriExperiment.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
/*=========================================================================
* RtConfigFmriExperiment.cpp defines a class that controls configuration of
* an entire real-time fMRI experimental session.
*
* 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"RtConfigFmriExperiment.h"
#include"RtExperiment.h"
#include"RtLimit.h"
#include"site_config.h"
#include "boost/filesystem.hpp"
#include "boost/filesystem/operations.hpp"
#include "ace/Trace.h"
#include<cstdlib>
using namespace boost::filesystem;
// defaults
// dirs
static const string DEFAULT_SOFTWAREDIR( "/sw/murfi/trunk");
static const string DEFAULT_VOLUMEDIR( "img/");
static const string DEFAULT_MASKDIR( "mask/");
static const string DEFAULT_XFMDIR( "xfm/");
static const string DEFAULT_CONFDIR( "scripts/");
static const string DEFAULT_LOGDIR( "log/");
// formats
static const string DEFAULT_VOLUMEFORMAT( "nii");
// file names
static const string DEFAULT_VOLUMEFILESTEM("img");
static const string DEFAULT_LOGNAME( "log");
static const string DEFAULT_LOGFILEEXT( "rtl");
static const string DESIGN_NAME( "design");
static const string DESIGN_EXT( "mat");
static const string DEFAULT_STUDYREFNAME( "study_ref");
static const string DEFAULT_XFMFILEEXT( "xfm");
static const string DEFAULT_SERIESXFMMOD( "series");
// ports
static const unsigned int DEFAULT_INFOSERVERPORT(15001);
// copy constructor (called often)
RtConfigFmriExperiment::RtConfigFmriExperiment(
const RtConfigFmriExperiment &other)
: RtConfig(other) {
}
// set default config info
void RtConfigFmriExperiment::setDefaults() {
// nothing to do here.
}
// validate the configuration
// checks for valid setup of different parts of the program
// returns true for success
bool RtConfigFmriExperiment::validateConfig() {
ACE_TRACE(("RtConfigFmriExperiment::validateConfig"));
// return value
bool valid = true;
// tmp storage vars
path p;
// check subject
if(!isSet("study:subject:name")) {
cerr << "ERROR: no subject name specified" << endl;
valid = false;
}
// check subjects directory
if(!isSet("study:subjectsDir")) {
cerr << "ERROR: no subjects directory specified" << endl;
valid = false;
}
// check for existance of studyDir
p.operator=(get("study:subjectsDir").filepath()
/ get("study:subject:name").str());
set("study:directory",p.string());
path studyDir(p); // for local use
// create the study directory if it does not exist
if(!exists(studyDir)) {
cout << "creating study directory: " << studyDir.string() << endl;
create_directory(studyDir);
}
if(!( exists(p) && is_directory(p) )) {
cerr << "ERROR: study directory " << p.string() << " is bad" << endl;
valid = false;
}
// check or set the paths for all kinds of data
// software directory
if(!isSet("study:softwareDir")) { // not set, make default
set("study:softwareDir",DEFAULT_SOFTWAREDIR);
}
// study id
if(!isSet("study:id")) { // not set, make default
set("study:id", getExperimentStudyID());
}
// site id
if(!isSet("study:siteID")) { // not set, make default
set("study:siteID", SITE_ID_NUMBER);
}
// gui
if(!isSet("gui:disabled")) { // not set, make default
set("gui:disabled", true);
}
// volume directory
if(!isSet("study:volumeDir")) { // not set, make default
p.operator=(studyDir / DEFAULT_VOLUMEDIR);
set("study:volumeDir",p.string());
}
else {
p.operator=(get("study:volumeDir").filepath());
}
// create volume directory if it doesn't exist
if(!( exists(p) && is_directory(p) )) {
cout << "creating volume directory: " << p.string() << endl;
create_directory(p);
}
// mask directory
if(!isSet("study:maskDir")) { // not set, make default
p.operator=(studyDir / DEFAULT_MASKDIR);
set("study:maskDir",p.string());
}
else {
p.operator=(get("study:maskDir").filepath());
}
// create mask directory if it doesn't exist
if(!( exists(p) && is_directory(p) )) {
cout << "creating mask directory: " << p.string() << endl;
create_directory(p);
}
// xfm directory
if(!isSet("study:xfm:directory")) { // not set, make default
p.operator=(studyDir / DEFAULT_XFMDIR);
set("study:xfm:directory",p.string());
}
else {
p.operator=(get("study:xfm:directory").filepath());
}
// create xfm directory if it doesn't exist
if(!( exists(p) && is_directory(p) )) {
cout << "creating xfm directory: " << p.string() << endl;
create_directory(p);
}
// conf directory
if(!isSet("study:confDir")) { // not set, make default
p.operator=(studyDir / DEFAULT_CONFDIR);
set("study:confDir",p.string());
}
else {
p.operator=(get("study:confDir").filepath());
}
// create conf directory if it doesn't exist
if(!( exists(p) && is_directory(p) )) {
cout << "creating conf directory: " << p.string() << endl;
create_directory(p);
}
// log directory
if(!isSet("study:log:directory")) { // not set, make default
p.operator=(studyDir / DEFAULT_LOGDIR);
set("study:log:directory",p.string());
}
else {
p.operator=(get("study:log:directory").filepath());
}
// create log directory if it doesn't exist
if(!( exists(p) && is_directory(p) )) {
cout << "creating log directory: " << p.string() << endl;
create_directory(p);
}
// formats
// set the default volume format if its not been set
string volumeFormat;
if(!isSet("study:volumeFormat")) { // not set, make default
set("study:volumeFormat",DEFAULT_VOLUMEFORMAT);
}
else { // check that the secified format is valid
if(get("study:volumeFormat").str() != DEFAULT_VOLUMEFORMAT) {
cerr << "ERROR: unsupported volume format "
<< get("study:volumeFormat") << endl;
valid = false;
}
}
// set the default volume fileStem if its not been set
string volumeFileStem;
if(!isSet("study:volumeFileStem")) { // not set, make default
set("study:volumeFileStem",DEFAULT_VOLUMEFILESTEM);
}
// filenames
// check logfile name
if(get("study:log:disabled")==false) {
if(!isSet("study:log:filename")) {
p.operator=(get("study:log:directory").filepath()
/ (DEFAULT_LOGNAME + "." + DEFAULT_LOGFILEEXT));
cout << "using default logfile name " << p.string() << endl;
set("study:log:filename",p.string());
}
}
// study reference volume
if(!isSet("study:xfm:referenceVol")) {
p.operator=(get("study:xfm:directory").filepath()
/ (DEFAULT_STUDYREFNAME + "."
+ get("study:volumeFormat").str()));
cout << "using default study reference volume name " << p.string() << endl;
set("study:xfm:referenceVol",p.string());
}
// general setup
// check info server
if(get("infoserver:disabled")==false) {
if(!isSet("infoserver:port")) {
set("infoserver:port",DEFAULT_INFOSERVERPORT);
}
if((unsigned int) get("infoserver:port") < 1
|| (unsigned int) get("infoserver:port") > MAX_TCPIP_PORT_NUM) {
cerr << "WARNING: invalid port number for infoserver, disabling"
<< endl;
set("infoserver:disabled",true);
}
}
return valid;
}
//******* computed values must have special functions
// get the filename for the series reference volume
string RtConfigFmriExperiment::getSeriesRefVolFilename(unsigned int series) {
stringstream ss;
ss << "series" << series << "_ref." << get("study:volumeFormat").str();
path seriesFile(get("study:xfm:directory").filepath() / ss.str());
return seriesFile.string();
}
// get the filename for the transformation file that takes the
// experiment reference volume into the space of the current series
string RtConfigFmriExperiment::getSeriesXfmFilename(unsigned int series) {
stringstream ss;
ss << "series" << series << "." << DEFAULT_XFMFILEEXT;
path xfmFile(get("study:xfm:directory").filepath() / ss.str());
return xfmFile.string();
}
// get the filename of a file transformed by using the series xfm on
// an input file
string RtConfigFmriExperiment::getSeriesXfmOutputFilename(unsigned int series,
string input) {
size_t ext = input.rfind('.');
size_t slashLoc = input.rfind('/');
if(ext == string::npos || slashLoc > ext) {
ext = input.size();
}
stringstream sins;
sins << "_series" << series;
input.insert(ext, sins.str());
return input;
}
// get the filename for the mask volume for a single series
string RtConfigFmriExperiment::getSeriesMaskFilename(unsigned int series,
string roiID) {
stringstream ss;
ss << "series" << series << "_" << roiID << "."
<< get("study:volumeFormat").str();
path p(get("study:maskDir").filepath() / ss.str());
return p.string();
}
// determine if the reference volume already exists
// true if reference volume exists
bool RtConfigFmriExperiment::getStudyRefVolExists() {
path p(get("study:xfm:referenceVol").filepath());
return exists(p) && is_regular(p);
}
// build a filename for a volume in this experiment
// in
// series number
// timepoint
// out
// absolute file string
string RtConfigFmriExperiment::getVolFilename(int _seriesNum,int _timepoint) {
// five digit filename assumption here!
char srnum[6];
char acnum[6];
sprintf(srnum,"%05d",_seriesNum);
sprintf(acnum,"%05d",_timepoint);
path p(get("study:volumeDir").filepath()
/ (get("study:volumeFileStem").str()
+ "-" + srnum
+ "-" + acnum
+ "." + get("study:volumeFormat").str()));
return p.string();
}
// build a filename for a design matrix in this experiment
// in
// series number
// out
// absolute file string
string RtConfigFmriExperiment::getDesignFilename(int _seriesNum) {
// five digit filename assumption here!
char srnum[6];
sprintf(srnum,"%05d",_seriesNum);
path p(get("study:volumeDir").filepath()
/ (DESIGN_NAME + "-" + srnum + "." + DESIGN_EXT));
return p.string();
}