-
Notifications
You must be signed in to change notification settings - Fork 9
/
RtFSLInterface.cpp
237 lines (193 loc) · 6.34 KB
/
RtFSLInterface.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
/*=========================================================================
* class to support interaction with the FSL tools
*
* WARNING! platform specific code in here....
*
* 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"RtFSLInterface.h"
#include"RtExperiment.h"
#include"ace/Task.h"
#include"ace/Mutex.h"
// support class for farming jobs to threads
class SystemCommand : public ACE_Task_Base {
public:
SystemCommand(string _command) {
command = _command;
started = false;
running = false;
success = false;
}
// start the thread to run the command
int svc() {
started = true;
// here is the system call!!
if(system(NULL)) { // check for availability
running = true;
success = (system(command.c_str()) == 0);
running = false;
}
else {
success = false;
running = false;
}
return (success ? 0 : 1);
}
string getCommand() { return command; }
bool getStarted() { return started; }
bool getRunning() { return running; }
bool getSuccess() { return success; }
private:
string command;
bool started;
bool running;
bool success;
};
// static declaration
map<unsigned int, SystemCommand*> RtFSLInterface::cmdMap;
// execute a command string in the system environment
FslJobID RtFSLInterface::runCommand(string command) {
FslJobID ret = FSL_NO_SUCH_JOB;
// if NOT windoze
#ifndef WIN32
// start the job
ACE_Mutex mut;
mut.acquire();
FslJobID newID = RtFSLInterface::getNextJobID();
SystemCommand *syscmd = new SystemCommand(command);
cmdMap[newID] = syscmd;
syscmd->activate();
ret = newID;
mut.release();
#else
cout << "RtFSLInterface::runCommand: fsl system calls "
<< "are unavailable on windows" << endl;
#endif
return ret;
}
// execute a command string in the system environment and block till finished
FslJobStatus RtFSLInterface::runCommandBlocking(string command) {
FslJobStatus ret = FSL_JOB_ERROR;
// if NOT windoze
#ifndef WIN32
// start the job
SystemCommand *syscmd = new SystemCommand(command);
syscmd->svc();
ret = (syscmd->getSuccess() ? FSL_JOB_FINISHED : FSL_JOB_ERROR);
#else
cout << "RtFSLInterface::runCommand: fsl system calls "
<< "are unavailable on windows" << endl;
#endif
return ret;
}
// perform a same-subject registration of two EPI volumes
// in:
// target image that will be registered to
// movable image that will be registered
// xfmFile file to save the transformation in (if empty a temp file
// is created)
FslJobID RtFSLInterface::registerSameSubjEPI(string target, string movable,
string xfmFile, bool block) {
string command = getExperimentConfig().get("study:softwareDir");
command
+= string("/scripts/fsl_reg_subj_epi.sh ")
+ " -t " + target
+ " -m " + movable
+ " -s " + xfmFile;
return (block ?
RtFSLInterface::runCommandBlocking(command) :
RtFSLInterface::runCommand(command)
);
}
// apply a pre-computed transformation to a file
// in:
// target image that was registered to
// movable image that was registered
// input image to be transformed
// output image filename to save to
// xfmFile file to save the transformation in (if empty a temp file
// is created)
FslJobID RtFSLInterface::applyTransform(string target, string movable,
string input, string output,
string xfmFile, bool block) {
string command = getExperimentConfig().get("study:softwareDir");
command
+= string("/scripts/fsl_reg_subj_epi.sh ")
+ " -t " + target
+ " -m " + movable
+ " -i " + input
+ " -o " + output
+ " -r " + xfmFile;
return (block ?
RtFSLInterface::runCommandBlocking(command) :
RtFSLInterface::runCommand(command)
);
}
// make a brain mask out of an epi volume
// in:
// filename of the volume to base the mask off of
FslJobID RtFSLInterface::makeBrainMask(string brainVolume,
string maskFilename,
string betOptions,
bool block) {
string command = "../scripts/make_bet_mask.sh "
+ brainVolume + " "
+ maskFilename + " "
+ betOptions;
return (block ?
RtFSLInterface::runCommandBlocking(command) :
RtFSLInterface::runCommand(command)
);
}
// run a block design analysis
// in:
// filePrefix full path plus file prefix of volumes to include
// runNum series number of the images to include
// blockLength number of seconds per block
// numConditions number of stimulus conditions (including rest)
FslJobID RtFSLInterface::runAnalysis(string filePrefix, unsigned int runNum,
float blockLength,
unsigned int numConditions) {
cout << "RtFSLInterface::runAnalysis() is not yet implemented" << endl;
return cmdMap.size();
}
// get the next job id
FslJobID RtFSLInterface::getNextJobID() {
return cmdMap.size();
}
// get a job status
FslJobStatus RtFSLInterface::getJobStatus(FslJobID jobID) {
// check that the job exists
if(jobID >= cmdMap.size()) {
return FSL_NO_SUCH_JOB;
}
// get the job
SystemCommand *cmd = cmdMap[jobID];
if(cmd == NULL) {
return FSL_NO_SUCH_JOB;
}
// find its status
if(!cmd->getStarted() || cmd->getRunning()) {
return FSL_JOB_RUNNING;
}
else if(cmd->getStarted() && !cmd->getRunning() && cmd->getSuccess()) {
return FSL_JOB_FINISHED;
}
else if(cmd->getStarted() && !cmd->getRunning() && !cmd->getSuccess()) {
return FSL_JOB_ERROR;
}
return FSL_JOB_UNKNOWN_STATE;
}