forked from e0404/matRad
-
Notifications
You must be signed in to change notification settings - Fork 0
/
MatRad_Config.m
306 lines (254 loc) · 12 KB
/
MatRad_Config.m
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
classdef MatRad_Config < handle
% MatRad_Config MatRad Configuration class
% This class is used globally through Matlab to handle default values and
% logging and is declared as global matRad_cfg.
% Usage:
% matRad_cfg = MatRad_Config.instance();
%
% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% Copyright 2019 the matRad development team.
%
% This file is part of the matRad project. It is subject to the license
% terms in the LICENSE file found in the top-level directory of this
% distribution and at https://github.com/e0404/matRad/LICENSES.txt. No part
% of the matRad project, including this file, may be copied, modified,
% propagated, or distributed except according to the terms contained in the
% LICENSE file.
%
% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
properties
%Logging
logLevel = 3; %1 = only Errors, 2 = with Warnings, 3 = Info output, 4 = deprecation warnings, 5 = debug information
keepLog = false; %Stores the full log in memory
writeLog = false; %Writes the log to a file on-the-fly
%Default Properties
propDoseCalc;
propOpt;
propMC;
propStf;
%Disable GUI
disableGUI = false;
end
properties (SetAccess = private)
messageLog = {};
logFileHandle;
%For storing the Environment & its version
env;
envVersion;
isOctave; %Helper bool to check for Octave
isMatlab; %Helper bool to check for Matlab
end
properties (Constant)
matRadRoot = fileparts(mfilename('fullpath'));
end
methods (Access = private)
function obj = MatRad_Config()
%MatRad_Config Constructs an instance of this class.
% The configuration is implemented as a singleton and used globally
% Therefore its constructor is private
% For instantiation, use the static MatRad_Config.instance();
%Set Version
obj.getEnvironment();
%Just to catch people messing with the properties in the file
if ~isempty(obj.writeLog) && obj.writeLog
logFile = [matRadRoot filesep 'matRad.log'];
obj.logFileHandle = fopen(logFile,'a');
end
%Call the reset function for remaining inatialization
obj.reset();
end
function displayToConsole(obj,type,formatSpec,varargin)
%displayToConsole lowest-level logging function for matRad.
% Display to console will be called from the public wrapper
% functions dispError, dispWarning, dispInfo, dispDebug
%
% input
% type: type of the log information.
% Needs to be one of 'error', 'warning', 'info' or 'debug'.
% formatSpec: string to print using format specifications similar to fprintf
% varargin: variables according to formatSpec
if nargin < 4
forwardArgs = {formatSpec};
else
forwardArgs = [{formatSpec},varargin(:)'];
end
if obj.keepLog
obj.messageLog{end+1,1} = upper(type);
obj.messageLog{end,2} = sprintf(forwardArgs{:});
end
switch type
case{'info'}
if obj.logLevel >= 3
fprintf(forwardArgs{:});
end
case{'debug'}
if obj.logLevel >= 5
forwardArgs{1} = ['DEBUG: ' forwardArgs{1}];
fprintf(forwardArgs{:});
end
case{'dep'}
if obj.logLevel >= 4
forwardArgs{1} = ['DEPRECATION WARNING: ' forwardArgs{1}];
warning(forwardArgs{:});
end
case{'warning'}
if obj.logLevel >= 2
warning(forwardArgs{:});
end
case {'error'}
if obj.logLevel >= 1
%We create an error structure to later clean the
%stack trace from the last two files/lines (i.e.,
%this function / file)
err.message = sprintf(forwardArgs{:});
err.identifier = 'matRad:Error';
err.stack = dbstack(2);
error(err);
end
otherwise
error('Log type %s not defined!',type);
end
if obj.writeLog
fprintf(obj.logFileHandle,forwardArgs{:});
end
end
end
methods
function reset(obj)
%Set all default properties for matRad's computations
obj.setDefaultProperties();
end
function setDefaultProperties(obj)
%setDefaultProperties set matRad's default computation
% properties
% input
obj.propStf.defaultLongitudinalSpotSpacing = 2;
obj.propStf.defaultAddMargin = true; %expand target for beamlet finding
obj.propDoseCalc.defaultResolution = struct('x',3,'y',3,'z',3); %[mm]
obj.propDoseCalc.defaultLateralCutOff = 0.995; %[rel.]
obj.propDoseCalc.defaultGeometricCutOff = 50; %[mm]
obj.propDoseCalc.defaultSsdDensityThreshold = 0.05; %[rel.]
obj.propDoseCalc.defaultUseGivenEqDensityCube = false; %Use the given density cube ct.cube and omit conversion from cubeHU.
obj.propDoseCalc.defaultIgnoreOutsideDensities = true; %Ignore densities outside of cst contours
obj.propDoseCalc.defaultUseCustomPrimaryPhotonFluence = false; %Use a custom primary photon fluence
obj.propOpt.defaultMaxIter = 500;
obj.propMC.ompMC_defaultHistories = 1e6;
obj.propMC.ompMC_defaultOutputVariance = false;
obj.propMC.MCsquare_defaultHistories = 1e6;
obj.propMC.direct_defaultHistories = 2e4;
obj.disableGUI = false;
end
%%For testing
function setDefaultPropertiesForTesting(obj)
%setDefaultPropertiesForTesting sets matRad's default
%properties during testing to reduce computational load
obj.logLevel = 1; %Omit output except errors
obj.propStf.defaultLongitudinalSpotSpacing = 20;
obj.propStf.defaultAddMargin = true; %expand target for beamlet finding
obj.propDoseCalc.defaultResolution = struct('x',5,'y',6,'z',7); %[mm]
obj.propDoseCalc.defaultGeometricCutOff = 20;
obj.propDoseCalc.defaultLateralCutOff = 0.8;
obj.propDoseCalc.defaultSsdDensityThreshold = 0.05;
obj.propDoseCalc.defaultUseGivenEqDensityCube = false; %Use the given density cube ct.cube and omit conversion from cubeHU.
obj.propDoseCalc.defaultIgnoreOutsideDensities = true;
obj.propDoseCalc.defaultUseCustomPrimaryPhotonFluence = false; %Use a custom primary photon fluence
obj.propOpt.defaultMaxIter = 10;
obj.propMC.ompMC_defaultHistories = 100;
obj.propMC.ompMC_defaultOutputVariance = true;
obj.propMC.MCsquare_defaultHistories = 100;
obj.propMC.direct_defaultHistories = 100;
obj.disableGUI = true;
end
function dispDebug(obj,formatSpec,varargin)
%dispDebug print debug messages (log level >= 4)
% input
% formatSpec: string to print using format specifications similar to fprintf
% varargin: variables according to formatSpec
obj.displayToConsole('debug',formatSpec,varargin{:});
end
function dispInfo(obj,formatSpec,varargin)
%dispInfo print information console output (log level >= 3)
% input
% formatSpec: string to print using format specifications similar to fprintf
% varargin: variables according to formatSpec
obj.displayToConsole('info',formatSpec,varargin{:});
end
function dispError(obj,formatSpec,varargin)
%dispError print errors (forwarded to "error" that will stop the program) (log level >= 1)
% input
% formatSpec: string to print using format specifications
% similar to 'error'
% varargin: variables according to formatSpec
obj.displayToConsole('error',formatSpec,varargin{:});
end
function dispWarning(obj,formatSpec,varargin)
%dispError print warning (forwarded to 'warning') (log level >= 2)
% input
% formatSpec: string to print using format specifications
% similar to 'warning'
% varargin: variables according to formatSpec
obj.displayToConsole('warning',formatSpec,varargin{:});
end
function dispDeprecationWarning(obj,formatSpec,varargin)
%dispDeprecationWarning wrapper for deprecation warnings forwarded to displayToConsole
obj.displayToConsole('dep',formatSpec,varargin{:});
end
function obj = writeLogToFile(obj,filename)
%writeLogToFile writes the log kept in MatRad_Config to file.
% Note that the switch keepLog must be enabled for MatRad_Config to store all logging output.
singleString = '%s: %s\n';
fID = fopen(filename,'w');
fprintf(fID,repmat(singleString,1,size(obj.messageLog,1)),obj.messageLog{:});
fclose(fID);
end
function set.logLevel(obj,newLogLevel)
%%Property set methods for logLevel
minLevel = 1;
maxLevel = 5;
if newLogLevel >= minLevel && newLogLevel <= maxLevel
obj.logLevel = newLogLevel;
else
obj.dispError('Invalid log level. Value must be between %d and %d',minLevel,maxLevel);
end
end
function set.writeLog(obj,writeLog)
if writeLog
logFile = [obj.matRadRoot filesep 'matRad.log'];
obj.logFileHandle = fopen(logFile,'a');
obj.writeLog = true;
else
fclose(obj.logFileHandle);
obj.writeLog = false;
end
end
function getEnvironment(obj)
% getEnvironment function to get the software environment
% matRad is running on
obj.isOctave = exist('OCTAVE_VERSION', 'builtin') ~= 0;
obj.isMatlab = ~obj.isOctave;
if obj.isOctave
obj.env = 'OCTAVE';
obj.envVersion = OCTAVE_VERSION;
else
obj.env = 'MATLAB';
vData = ver(obj.env);
obj.envVersion = vData.Version;
end
end
end
methods(Static)
function obj = instance()
%instance creates a singleton instance of MatRad_Config
% In MatRad_Config, the constructor is private to make sure only on global instance exists.
% Call this static functino to get or create an instance of the matRad configuration class
persistent uniqueInstance;
if isempty(uniqueInstance)
obj = MatRad_Config();
uniqueInstance = obj;
else
obj = uniqueInstance;
end
end
end
end