-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPatient.java
More file actions
253 lines (218 loc) · 7.42 KB
/
Patient.java
File metadata and controls
253 lines (218 loc) · 7.42 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
package models;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import configuration.Consts;
/**
* The Class PatientModel.
* Represents a full model with all variables for a patient, as read in when processing inputfiles
*
* can return the coeff sum (i.e. risk score) for this patient and model
*/
class PatientModel {
private HashMap<String,Variable> variables = new HashMap<String,Variable>(); //variablename -> PatientVariable
private Model model;
private boolean profValuesAreCalculated=false;
private boolean amIincluded=true;
private boolean amIexcluded=false;
public PatientModel(Model model) {
this.model = model;
//if (model.hasInclusion()) amIincluded=false;
}
public void updateVariables(InputFile inputfile) {
profValuesAreCalculated=false;
variables = this.model.updateVariables(inputfile, variables);
}
private void calcProfValues () {
if (!profValuesAreCalculated) {
variables = this.model.updateVariablesNoInputfile(variables); //this allows variables w/o inputfile
//1. Determine calculation sequence
List<String> allVars = new ArrayList<String>(variables.keySet());
@SuppressWarnings("unchecked")
List<String>[] rounds = (ArrayList<String>[])new ArrayList[5];
List<String> allIncludedVars = new ArrayList<String>();
for (int i=0; i<rounds.length; i++) {
rounds[i] = new ArrayList<String>();
if (allVars.size()>0) {
for (String var : allVars) {
if (i==0 && !variables.get(var).dependsOnOtherVars()) rounds[i].add(var);
else if (i>0 && allIncludedVars.containsAll(variables.get(var).getOtherVarsDependent()))
rounds[i].add(var);
}
allVars.removeAll(rounds[i]);
allIncludedVars.addAll(rounds[i]);
}
}
rounds[rounds.length-1].addAll(allVars); // add all remaining; hopefully should be able to calc
//2. Now round for round: calc values
for (int i=0; i<rounds.length; i++) {
for (String var : rounds[i]) {
variables.get(var).calcProfvalue(variables);
if (variables.get(var).isInclude()) amIincluded=amIincluded && variables.get(var).isAllowed();
if (variables.get(var).isAllowed() && variables.get(var).isExclude()) amIexcluded=true;
if (!amIincluded || amIexcluded) break;
}
if (!amIincluded || amIexcluded) break;
}
profValuesAreCalculated = true;
}
}
public double getCoeffSum () {
calcProfValues();
double mysum = model.getCoeffIntercept();
for (String var : variables.keySet()) {
if (!variables.get(var).hideme() && variables.get(var).isAllowed() && !variables.get(var).isTarget())
mysum += variables.get(var).getCalcCoeff(model.getCoeff(var));
}
if (this.model.getType().equals(Consts.logRegFlag)) {
mysum = Math.exp(mysum) / (1+Math.exp(mysum));
}
return mysum;
}
/**
* adds Variables from that model to the list of knownVars
* @param knownVars, isTarget (i target vars?)
* @return
*/
public ArrayList<String> addToKnownVariables (ArrayList<String> knownVars, boolean isTarget) {
ArrayList<String> newKnownVars = knownVars;
for (String var : variables.keySet()) {
if (!knownVars.contains(var) && !variables.get(var).hideme() && variables.get(var).isAllowed() && variables.get(var).isTarget()==isTarget) { //Variable ist bisher nicht bekannt Und ist gültig -> add
newKnownVars.add(var);
}
}
return newKnownVars;
}
public ArrayList<String> addToKnownVariables (ArrayList<String> knownVars) {
ArrayList<String> newKnownVars = knownVars;
for (String var : variables.keySet()) {
if (!knownVars.contains(var) && !variables.get(var).hideme() && variables.get(var).isAllowed()) { //Variable ist bisher nicht bekannt Und ist gültig -> add
newKnownVars.add(var);
}
}
return newKnownVars;
}
public ArrayList<String> getAllTargetVariables () {
ArrayList<String> newVars = new ArrayList<String>();
for (String var : variables.keySet()) {
if (!variables.get(var).hideme() && variables.get(var).isAllowed() && variables.get(var).isTarget()) { //Variable ist bisher nicht bekannt Und ist gültig -> add
newVars.add(var);
}
}
return newVars;
}
/**
* returns Profvalues in same order as knownVars (as String)
* @param knownVars
* @return
*/
public ArrayList<String> getProfvalues (ArrayList<String> knownVars) {
calcProfValues();
ArrayList<String> profvalues = new ArrayList<String>();
for (int i = 0; i < knownVars.size(); i++) {
//has Patient this Variable? If not, return ""
if (variables.get(knownVars.get(i)) == null || !variables.get(knownVars.get(i)).isAllowed()) {
profvalues.add(Consts.navalue);
} else {
profvalues.add(String.valueOf(variables.get(knownVars.get(i)).getProfvalue())); //gets the Profil-value from PatientVariable, ordered by KnownVars order
}
}
return profvalues;
}
public boolean amIincluded() {
calcProfValues();
return amIincluded && !amIexcluded;
}
}
/**
* The Class Patient.
* Represents a patient with information / variables for all models that are currently processed
*
* One patient is processed at a time (inputfiles are sorted)
*/
public class Patient {
//private final static Logger LOGGER = Logger.getLogger(Patient.class.getName());
/** The models. */
private HashMap<Model,PatientModel> models = new HashMap<Model,PatientModel>();
/** The pid. */
private String pid;
/**
* Instantiates a new patient.
*
* @param pid the pid
*/
public Patient (String pid) {
this.pid = pid;
}
/**
* Checks if is patient.
*
* @param newpid the newpid
* @return true, if is patient
*/
public boolean isPatient (String newpid) {
return pid.equals(newpid);
}
/**
* Process row.
*
* @param model the model
* @param inputfile the inputfile
*/
public void processRow (Model model, InputFile inputfile) throws Exception {
//Patient does not have that model yet -> create
if (!models.containsKey(model)) {
PatientModel mypatientmodel = new PatientModel(model);
this.models.put(model,mypatientmodel);
}
this.models.get(model).updateVariables(inputfile);
}
/**
* Gets the coeff sum.
*
* @param model the model
* @return the coeff sum
*/
public double getCoeffSum (Model model) {
return this.models.get(model).getCoeffSum();
}
/**
* Gets the pid.
*
* @return the pid
*/
public String getPid() {
return this.pid;
}
/**
* Gets the.
*
* @return the string
*/
public String get() {
return this.pid;
}
/**
* Gets the profvalues.
*
* @param model the model
* @param knownVars the known vars
* @return the profvalues
*/
public ArrayList<String> getProfvalues (Model model, ArrayList<String> knownVars) {
return models.get(model).getProfvalues(knownVars);
}
public boolean areYouIncluded (Model model) {
if (!models.containsKey(model)) return false;
return models.get(model).amIincluded();
}
public ArrayList<String> addToKnownVariables (Model m, ArrayList<String> knownVars, boolean isTarget) {
return models.get(m).addToKnownVariables(knownVars,isTarget);
}
public ArrayList<String> addToKnownVariables (Model m, ArrayList<String> knownVars) {
return models.get(m).addToKnownVariables(knownVars);
}
public ArrayList<String> getAllTargetVariables (Model m) {
return models.get(m).getAllTargetVariables();
}
}