-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVariable.java
More file actions
163 lines (134 loc) · 4.9 KB
/
Variable.java
File metadata and controls
163 lines (134 loc) · 4.9 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
package models;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
//combines columnvalues and ModelVariable
class Allrows {
private List<String[]> rows = new ArrayList<String[]>();
public Allrows(String[] colvalues) {
this.add(colvalues);
}
public void add (String[] colvalues) {
this.rows.add(colvalues);
}
public List<String[]> getAllRows () {
return rows;
}
}
/**
* The Class Variable.
* Used when building Variables from an inputfile(row) for a model, based on ModelFieldFilter
* Example:
* Field: ICD_CODE
* Stringposition: 1-3 (i.e. first 3 characters in icd)
* Values: F32 (i.e. count only Depression icds)
*
* There might be multiple filters on one field, see List in ModelField
*
*/
public class Variable {
//private final static Logger LOGGER = Logger.getLogger(Variable.class.getName());
private HashMap<ModelVariable,Allrows> rows = new HashMap<ModelVariable,Allrows>();
private boolean include = false; //if true: only include patients that have this
private boolean exclude = false; //if true: exclude all patients that have this
private boolean isTarget = false; //if true: is Target
private boolean hideme = false; //if true: do not print var
private boolean set1 = false; //if true: set 1
private boolean dependsOnOtherVars = false;
private boolean isAllowed = true; //false, if all modelvariables or variable itself were filtered out
private double profvalue = 1;
private boolean profvalueIsCalulated = false;
private boolean aggIsOccurence = true; //i.e. 1 (=occurence)
public Variable() {
}
public void addRow (ModelVariable v,String[] values) {
profvalueIsCalulated = false;
if (!rows.containsKey(v)) {
Allrows myrow = new Allrows(values);
rows.put(v, myrow);
} else rows.get(v).add(values);
if (v.set1()) set1=true; //i.e.: if true for one ModelVar -> true for all
if (v.isInclude()) include=true; //i.e.: if true for one ModelVar -> true for all
if (v.isExclude()) exclude=true; //i.e.: if true for one ModelVar -> true for all
if (v.isTarget()) isTarget=true; //i.e.: if true for one ModelVar -> true for all
if (v.hideme()) hideme=true; //i.e.: if true for one ModelVar -> true for all
if (v.dependsOnOtherVar()) dependsOnOtherVars=true; //if true for one -> must be calculated later
if (!v.isOccAgg()) aggIsOccurence = false;
}
public boolean isInclude () {
return include;
}
public boolean isExclude () {
return exclude;
}
public boolean isTarget () {
return isTarget;
}
public boolean hideme () {
return hideme;
}
public boolean dependsOnOtherVars () {
return dependsOnOtherVars;
}
public List<String> getOtherVarsDependent () {
List<String> myVars = new ArrayList<String>();
for (ModelVariable v : rows.keySet()) {
if (v.dependsOnOtherVar()) myVars.addAll(v.getOtherVarsDependent());
}
return myVars;
}
public double getProfvalue () {
if (!profvalueIsCalulated) this.calcProfvalue();
return profvalue;
}
public double getCalcCoeff (double coeff) {
if (!profvalueIsCalulated) this.calcProfvalue();
return profvalue * coeff;
}
//calc w/o other know variables
public void calcProfvalue() {
this.calcProfvalue(null);
}
public void calcProfvalue (HashMap<String,Variable> vars) {
if (profvalueIsCalulated) return;
profvalueIsCalulated=true;
//if (aggIsOccurence) this.profvalue=1; //values might be null (for inclusion vars) -> move to below
//else {
List<Double> allvalues = new ArrayList<Double>();
//work through variables, filter and consolidate rows, aggregate
ModelVariable aggV =null;
Double d;
for (ModelVariable v : rows.keySet()) {
//1: calculate values from single rows
for (String[] singlerow : rows.get(v).getAllRows()) {
//if (singlerow!= null) {
d = v.getValue(singlerow, vars); //gives null if row is null and var is not calculated from other vars
if (d!=null) allvalues.add(d);
//}
}
aggV=v; //use last for aggregation & check
}
//2:filter, aggregate and filter again
//1. filter before aggregation -> only for MAX, MIN
if (aggV.filterBeforeAggregation()) {
for (Iterator<Double> iterator = allvalues.iterator(); iterator.hasNext();) {
double x = iterator.next();
if (!aggV.varIsAllowed(x)) {
// Remove the current element from the iterator and the list.
iterator.remove();
}
}
}
if (allvalues.size()>0) {
if (aggIsOccurence) this.profvalue=1;
else this.profvalue=aggV.aggregateValues(allvalues);
if (aggV.varIsAllowed(this.profvalue)) isAllowed=true; else isAllowed=false;
if (this.set1 && this.profvalue>0) this.profvalue=1;
} else isAllowed=false;
//}
}
public boolean isAllowed() {
return isAllowed;
}
}