-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathModelVariable.java
More file actions
464 lines (395 loc) · 13.3 KB
/
ModelVariable.java
File metadata and controls
464 lines (395 loc) · 13.3 KB
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
package models;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import org.joda.time.LocalDate;
import org.joda.time.Years;
import org.joda.time.Days;
import configuration.Consts;
import configuration.Configuration;
import configuration.Utils;
class ModelVariableCalcPart {
private boolean needsCol = false;
private boolean needsVariable= false;
private int refColnumber; //starting with 0
private String refVariable;
private double value;
private LocalDate mydate;
private LocalDate myreferencedate;
private boolean isConstant = false; //
private boolean isDate;
private boolean isAgeFromDate;
public ModelVariableCalcPart (String type, String value) throws Exception {
myreferencedate=Configuration.getReferenceDate();
parseType(type);
parseValue(value);
}
private void parseType (String type) {
//isConstant = (type.equals(Consts.aggValue));
isDate= (type.equals(Consts.aggDate));
isAgeFromDate= (type.equals(Consts.aggAge));
}
private void parseValue (String value) throws Exception {
if (value.startsWith(Consts.reference)) {
try {
//Is Integer -> refers to column
refColnumber = Integer.parseInt(value.substring(1))-1;
needsCol=true;
} catch (Exception e) {
//is no integer -> take as variable
refVariable = value.substring(1);
needsVariable=true;
}
} else {
//test if value contains "," -> change to "."
String newvalue = value.replace(",", ".");
this.value=Double.parseDouble(newvalue);
isConstant = true;
}
}
public boolean needsCol() {
return needsCol;
}
public boolean needsVariable() {
return needsVariable;
}
public int getColnumber() {
return refColnumber;
}
public String getRefVariable() {
return refVariable;
}
//used for column inputfiles -> parse
public double getValue(String inputvalue) {
double newval;
//parse inputvalue & calc
if (isDate) {
mydate = Utils.parseDate(inputvalue);
newval = Days.daysBetween(myreferencedate, mydate).getDays();
} else if (isAgeFromDate) {
mydate = Utils.parseDate(inputvalue);
newval = Years.yearsBetween(mydate,myreferencedate).getYears();
} else
try {
//test if value contains "," -> change to "."
String myval = inputvalue.replace(",", ".");
newval = Double.parseDouble(myval);
} catch (Exception e) { newval = 1; }
return newval;
}
//used for inputfiles from other variable
public double getValue(double inputvalue) {
if (isConstant) return value;
else return inputvalue;
}
public double getValue() {
return value;
}
public boolean isConstant() {
return isConstant;
}
public boolean isDate() {
return isDate;
}
}
class ModelVariableCalc {
private List <ModelVariableCalcPart> plusParts= new ArrayList<ModelVariableCalcPart>();
private List <ModelVariableCalcPart> minusParts= new ArrayList<ModelVariableCalcPart>();
boolean isStd = false; //only 1
private boolean dependsOnOtherVar = false;
private List<String> otherVars = new ArrayList<String>();
public ModelVariableCalc(String calculation) throws Exception {
//Parse Otherfieldfilter
if (!calculation.equals("")) {
//Split "+", then split "-"
String[] plustokens = calculation.split("\\+");
String[] minustokens;
String[] parts;
for (int i=0; i<plustokens.length; i++) {
//split again by "-"
minustokens = plustokens[i].split("\\-");
//now: 1st is +, following are -
for (int j=0; j<minustokens.length; j++) {
//look for ()
parts = minustokens[j].split(Consts.bracketEsc);
ModelVariableCalcPart p = new ModelVariableCalcPart(parts[0],parts[1]);
if (p.needsVariable()) {
dependsOnOtherVar=true;
otherVars.add(p.getRefVariable());
}
if (j==0) plusParts.add(p);
else minusParts.add(p);
}
}
} else isStd=true;
}
public boolean dependsOnOtherVar() {
return dependsOnOtherVar;
}
public List<String> getOtherVarsDependent () {
return otherVars;
}
public Double getValue (String[] columnvalues, HashMap<String,Variable> vars) {
if (columnvalues==null && isStd) return null;
else if (isStd) return 1.;
else {
double myval = 0;
double addval = 0;
for (ModelVariableCalcPart p : plusParts) {
if (p.needsCol() && columnvalues!=null) addval=p.getValue(columnvalues[p.getColnumber()]);
else if (p.needsVariable()) {
if (vars != null && vars.containsKey(p.getRefVariable())) {
addval=p.getValue(vars.get(p.getRefVariable()).getProfvalue());
if (!vars.get(p.getRefVariable()).isAllowed()) addval=0;
}
else addval=0;
}
else addval=p.getValue();
myval = myval +addval;
}
for (ModelVariableCalcPart p : minusParts) {
if (p.needsCol() && columnvalues!=null) addval=p.getValue(columnvalues[p.getColnumber()]);
else if (p.needsVariable()) {
if (vars != null && vars.containsKey(p.getRefVariable())) {
addval=p.getValue(vars.get(p.getRefVariable()).getProfvalue());
if (!vars.get(p.getRefVariable()).isAllowed()) addval=0;
}
else addval=0;
}
else addval=p.getValue();
myval = myval - addval;
}
return myval;
}
}
}
class ModelVariableAgg {
private boolean isSum;
private boolean isCount;
private boolean isMean;
private boolean isMin;
private boolean isMax;
private boolean isMaxDistance;
private boolean isOccurence;
public ModelVariableAgg (String type) throws Exception {
if (type.equals("")) this.isMax=true; //Default: MAX
else {
this.parseType(type);
}
}
private void parseType (String type) {
isSum= (type.equals(Consts.aggSum));
isCount= (type.equals(Consts.aggCount));
isMean= (type.equals(Consts.aggMean));
isMin= (type.equals(Consts.aggMin));
isMax= (type.equals(Consts.aggMax));
isMaxDistance= (type.equals(Consts.aggMaxDistance));
isOccurence= (type.equals(Consts.aggOccurence));
}
public double aggregateValues (List<Double> values) {
double myval=0;
if (isOccurence) return 1;
if (isSum) {
for (double d: values) myval+=d;
//needs rounding.... double not precise; gives not exactly correct numbers
myval = myval*100;
myval = Math.round(myval);
myval = myval /100;
return myval;
}
if (isCount) return values.size();
if (isMean) {
for (double d: values) myval+=d;
//needs rounding.... double not precise; gives not exactly correct numbers
myval = myval*100/values.size();
myval = Math.round(myval);
myval = myval /100;
return myval;
}
if (isMin) {
myval=1000000;
for (double d: values) myval = Math.min(myval, d);
return myval;
}
if (isMax) {
myval=-1000000;
for (double d: values) myval = Math.max(myval,d);
return myval;
}
if (isMaxDistance) {
//find min and max
double myvalmin=1000000;
double myvalmax=-1000000;
for (double d: values) {
myvalmin = Math.min(myvalmin, d);
myvalmax = Math.max(myvalmax, d);
}
return myvalmax-myvalmin+1; //returns 1 if myvalmax=myvalmin (to separate from 0)
}
return myval;
}
public boolean isOccurence() {
return isOccurence;
}
public boolean isMax() {
return isMax;
}
public boolean isMin() {
return isMin;
}
}
/**
* The Class ModelField.
* Represents one field in an inputfiles file, e.g. ICD_CODE
* Is valid for one model
*
* Holds a list of filters for the field
* Holds the aggregation type from Aggregation in Model.config
* (e.g. empty, i.e. standard; see Consts for values allowed)
* --> Only one aggregation type per field (per model per inputfile) allowed; last one is used
* (technical reason)
*
*/
public class ModelVariable {
//private final static Logger LOGGER = Logger.getLogger(ModelVariable.class.getName());
private String[] namePrefixes;
private int[] nameColumnNumbers;
private ColumnFilter[] cols;
private ModelVariableCalc calc;
private ModelVariableAgg agg;
private double filterstart = -1;
private double filterend = -1;
private boolean set1; //if true: set var to 1 if >0 (after filter)
private boolean include; //if true: only include patients that have this
private boolean exclude; //if true: exclude all patients that have this
private boolean target; //if target: print separately, do not include in coeff-calculation
private boolean hideme; //if true: do not print var
public ModelVariable (ModelVariableReadIn readvar, Model mymodel, Configuration config) throws ModelConfigException{
String[] tokens;
//parse variable name
try {
tokens = readvar.getVariableCol().split(Consts.referenceEsc);
namePrefixes = new String[tokens.length];
nameColumnNumbers = new int[tokens.length-1];
for (int i=0; i<tokens.length; i++) {
if (i==0) namePrefixes[i]= tokens[i];
else namePrefixes[i]= tokens[i].substring(1);
if (i<tokens.length-1) nameColumnNumbers[i]=Integer.parseInt(tokens[i+1].substring(0,1))-1;
}
} catch (Exception e) {
throw new ModelConfigException("Fehler bei Variable "+ readvar.getVariableCol() + ": "+ Consts.modVariableCol + " nicht lesbar",e);
}
//parse var filter
try {
parseFilter(readvar.getFilterCol());
} catch (Exception e) {
throw new ModelConfigException("Fehler bei Variable "+ readvar.getVariableCol() + ": "+ Consts.modFilterCol + " nicht lesbar",e);
}
//parse calculation
try {
this.calc = new ModelVariableCalc(readvar.getCalcCol());
} catch (Exception e) {
throw new ModelConfigException("Fehler bei Variable "+ readvar.getVariableCol() + ": "+ Consts.modCalcCol + " nicht lesbar",e);
}
//parse aggregation
try {
this.agg = new ModelVariableAgg(readvar.getAggCol());
} catch (Exception e) {
throw new ModelConfigException("Fehler bei Variable "+ readvar.getVariableCol() + ": "+ Consts.modAggCol + " nicht lesbar",e);
}
//parse rest
this.set1=Consts.wahr.equals(readvar.getSet1Col());
this.include=Consts.wahr.equals(readvar.getIncludeCol()); if (this.include) mymodel.setInclusion(true);
this.exclude=Consts.wahr.equals(readvar.getExcludeCol()); if (this.exclude) mymodel.setExclusion(true);
this.target=Consts.wahr.equals(readvar.getTargetCol()); if (this.target) mymodel.setHasTargets(true);
this.hideme=Consts.wahr.equals(readvar.getHideCol());
//parse columns + their filters
int number = 0;
try {
cols = new ColumnFilter[readvar.getColumns().length];
for (int i=0; i<readvar.getColumns().length; i++) {
number=i+1;
cols[i] = new ColumnFilter(readvar.getColumns()[i],readvar.getFilters()[i],null);
}
} catch (Exception e) {
throw new ModelConfigException("Fehler bei Variable "+ readvar.getVariableCol() + ": " + Consts.modColumnCol + number + " bzw. " + Consts.modColfilterCol + number + " nicht lesbar",e);
}
}
private void parseFilter (String filter) throws Exception {
if (!filter.equals("")) {
String[] tokens = filter.split("-");
if (!tokens[0].equals("")) filterstart=Double.parseDouble(tokens[0]);
else filterstart=-1;
if (tokens.length>1) {
if (!tokens[1].equals("")) filterend=Double.parseDouble(tokens[1]);
else filterend=-1;
} else filterend=filterstart;
}
}
/*
* get column values, substrings
* returns null, if row is not allowed
*/
public String[] getColumnValues (InputFile inputrow) {
String[] myCols = new String[cols.length];
String myval;
for (int i=0; i<myCols.length;i++) {
//column definition in config file might be empty
if (cols[i].getColumn()!= null ) {
//get value from inputrow, and substring it
myval = cols[i].getValueSubstring(inputrow.getValue(cols[i].getColumn()));
if (!cols[i].isAllowed(myval)) return null;
myCols[i]=myval;
}
}
return myCols;
}
public String getName (String[] columns) {
String myname = "";
for (int i=0; i<nameColumnNumbers.length; i++) {
if (columns!=null && columns[nameColumnNumbers[i]] != null)
myname=myname + namePrefixes[i] + columns[nameColumnNumbers[i]];
else myname=myname + namePrefixes[i] + "null";
}
myname=myname + namePrefixes[namePrefixes.length-1];
return myname;
}
public boolean varIsAllowed (double value) {
//simply parse to string
return ((filterstart==-1 || value >= filterstart ) // value >= filterstart
&& (filterend==-1 || value <= filterend)); //value <= filterend
}
public boolean set1 () {
return set1;
}
public boolean isInclude () {
return include;
}
public boolean isExclude () {
return exclude;
}
public boolean isTarget () {
return target;
}
public boolean hideme () {
return hideme;
}
public boolean dependsOnOtherVar () {
return calc.dependsOnOtherVar();
}
public List<String> getOtherVarsDependent () {
return calc.getOtherVarsDependent();
}
public boolean isOccAgg () {
return agg.isOccurence();
}
public Double getValue (String[] columnvalues, HashMap<String,Variable> vars) {
return this.calc.getValue(columnvalues, vars);
}
public double aggregateValues (List<Double> values) {
return this.agg.aggregateValues(values);
}
public boolean filterBeforeAggregation() {
return this.agg.isMax() || this.agg.isMin();
}
}