-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathModel.java
More file actions
314 lines (270 loc) · 9.29 KB
/
Model.java
File metadata and controls
314 lines (270 loc) · 9.29 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
package models;
import java.io.FileReader;
import java.io.IOException;
import java.util.*;
import java.util.logging.Level;
import java.util.logging.Logger;
import configuration.Consts;
import configuration.Configuration;
import au.com.bytecode.opencsv.CSVReader;
/**
* The Class ModelFile.
* Represents the configuration for an inputfile for a model (all fields and their filters needed)
*
*
*/
class ModelFile {
private List<ModelVariable> filevars= new ArrayList<ModelVariable>();
public ModelFile () {
}
/**
* Adds a field to the model/inputfile relationship
*
* @param field the field
* @param position the position from Model.config
* @param values the allowed values from Model.config
* @param aggregation the aggregation type from Model.config
* @param otherfieldfilter the otherfieldfilter String from Model.config
* @param variable the variablefrom Model.config
*/
public void addVariable (ModelVariable var) {
filevars.add(var);
}
/**
* Gets the Variables for a model on a field in an inputfile.
* *
* @param inputrow the full inputrow from the inputfile; to test filters in other fields
* @return a list of Variables incl. values
*/
public HashMap<String,Variable> updateVariables(InputFile inputrow, HashMap<String,Variable> existingVars) {
Variable myVar;
String[] colvalues;
String myname;
for(ModelVariable variable : filevars) {
//1. create column values array -> returns null if not allowed by rowfilter
colvalues = variable.getColumnValues(inputrow);
//1b: Test for filters
if (colvalues!=null) {
//create variable name
myname = variable.getName(colvalues);
//test filters
if (!existingVars.containsKey(myname)) {
myVar = new Variable();
existingVars.put(myname, myVar);
}
existingVars.get(myname).addRow(variable, colvalues);
} else if (variable.isInclude()) {//"include" vars are always added, even with null, but only once
//create variable name
myname = variable.getName(colvalues);
//test filters
if (!existingVars.containsKey(myname)) {
myVar = new Variable();
existingVars.put(myname, myVar);
existingVars.get(myname).addRow(variable, colvalues);
}
}
}
return existingVars;
}
}
/**
* The Class Model.
* Represents a model as configured by
* Model.config ,i.e. which fields from inputfiles are relevant and how are they to be processed into model variables, and
* Model.coeff ,i.e. which coefficients are assigned to which variables (incl. intercept)
*/
public class Model {
private final static Logger LOGGER = Logger.getLogger(Model.class.getName());
/** The coeffs. */
private HashMap<String,Double> coeffs = new HashMap<String,Double>();
/** The modelfiles. */
private HashMap<InputFile,ModelFile> modelfiles = new HashMap<InputFile,ModelFile>();
private List<ModelVariable> vars_without_file = new ArrayList<ModelVariable>();
/** The name. */
private String name;
/** The type. */
private String type = Consts.logRegFlag;
/** The i have coeffs. */
private boolean iHaveCoeffs = false;
/** The i have inclusion criteria. */
private boolean iHaveInclusion = false;
/** The i have exclusion criteria. */
private boolean iHaveExclusion = false;
private boolean iHaveTargets = false;
private String interceptname = Consts.interceptname;
/**
* Instantiates a new model.
*
* @param name the name
* @param inputfiles the inputfiles
* @param configfile the configfile
* @param coefffile the coefffile
* @throws IOException Signals that an I/O exception has occurred.
*/
public Model (String name, List<InputFile> inputfiles, Configuration config) throws Exception {
this.name = name;
String configfile = config.getModelpath() + "\\" + name+config.getModelConfigExt();
//read fields-data and add per modelfile, if present in corresponding inputfile
HashMap<String,String> fields_data = new HashMap<String,String>(); //(header -> value)
CSVReader reader = new CSVReader(new FileReader(configfile), ';', '"');
List<String[]> myEntries = reader.readAll();
reader.close();
//first line = header-line
String[] headerline = myEntries.get(0);
//make uppercase and count number of column/filters
int columnnumber=0;
for (int j=0; j<headerline.length; j++) {
headerline[j] = headerline[j].toUpperCase();
if (headerline[j].startsWith(Consts.modColumnCol)) columnnumber++;
}
myEntries.remove(0);
ModelVariableReadIn readvar;
ModelVariable newvar;
for (String[] nextline : myEntries) {
//ignore comments and empty lines
if (!nextline[0].isEmpty() && !nextline[0].substring(0, 1).equals(Consts.comment_indicator)) {
//add current line values to Hashmap (header -> value)
//this prohibits errors from wrong column order in config file
for (int j=0; j<headerline.length; j++) {
if (j<nextline.length)
fields_data.put(headerline[j], nextline[j].replaceAll("\\s",""));
}
//find inputfile(s) for this line / fields_data
if (fields_data.get(Consts.modInputfileCol).isEmpty()) {
readvar = new ModelVariableReadIn(fields_data,columnnumber,null);
if (!readvar.getVariableCol().equals("")) {
newvar = new ModelVariable(readvar, this,config);
vars_without_file.add(newvar);
}
} else
for (InputFile myinputfile : inputfiles) {
if (myinputfile.isDatentyp(fields_data.get(Consts.modInputfileCol).toUpperCase())) {
//relevant inputfile is found -> create ModelFile if not present
if (!this.modelfiles.containsKey(myinputfile)) this.modelfiles.put(myinputfile,new ModelFile());
//add vars
readvar = new ModelVariableReadIn(fields_data,columnnumber,myinputfile);
if (!readvar.getVariableCol().equals("") && !readvar.getColumns()[0].isEmpty()) {
newvar = new ModelVariable(readvar, this,config);
this.modelfiles.get(myinputfile).addVariable(newvar);
}
}
}
}
}
if (config.createScores()) {
//read Coeffs, if available
String coefffile=config.getModelpath() + "\\" + name+config.getModelCoeffExt();
try {
reader = new CSVReader(new FileReader(coefffile), ';', '"', 1);
myEntries = reader.readAll();
reader.close();
for (String[] nextline1 : myEntries) {
this.coeffs.put(nextline1[0].toUpperCase(), Double.parseDouble(nextline1[1].replace(",", ".")));
}
this.iHaveCoeffs = true;
} catch (IOException e) {
LOGGER.log(Level.WARNING,"Fehler beim Einlesen der Koeffizienten für Modell "+ name + ". Es werden nur Profile für das Modell gebildet.");
this.iHaveCoeffs = false;
}
}
//set Interceptname
if (Configuration.interceptname != null) this.interceptname=Configuration.interceptname;
}
/**
* Gets the name.
*
* @return the name
*/
public String getName () {
return this.name;
}
/**
* Gets the type.
*
* @return the type
*/
public String getType () {
return this.type;
}
public boolean inputfileIsRelevant (InputFile inputfile) {
return modelfiles.containsKey(inputfile);
}
/**
* Gets the variables.
*
* @param inputfile the inputfile
* @param field the field
* @param value the value
* @return the variables
*/
public HashMap<String,Variable> updateVariables(InputFile inputfile, HashMap<String,Variable> existingVars ) {
if (inputfileIsRelevant(inputfile))
return modelfiles.get(inputfile).updateVariables(inputfile,existingVars);
else return existingVars;
}
public HashMap<String,Variable> updateVariablesNoInputfile(HashMap<String,Variable> existingVars ) {
Variable myVar;
String[] colvalues;
String myname;
for(ModelVariable variable : vars_without_file) {
//1. create column values array -> returns null if not allowed by rowfilter
colvalues = null;
//1b: Test for filters
//if (variable.isInclude()) {//"include" vars are always added, even with null, but only once
//create variable name
myname = variable.getName(colvalues);
//test filters
if (!existingVars.containsKey(myname)) {
myVar = new Variable();
existingVars.put(myname, myVar);
existingVars.get(myname).addRow(variable, colvalues);
}
//}
}
return existingVars;
}
/**
* Gets the coeff.
*
* @param variable the variable
* @return the coeff
*/
public double getCoeff (String variable) {
double mycoeff;
if (this.coeffs.get(variable) == null) { //modell does not contain coeff -> assume it is zero
mycoeff = 0;
} else {
mycoeff = this.coeffs.get(variable);
}
return mycoeff;
}
public double getCoeffIntercept () {
return getCoeff(this.interceptname);
}
/**
* Checks for coeffs.
*
* @return true, if successful
*/
public boolean hasCoeffs () {
return iHaveCoeffs;
}
public boolean hasInclusion () {
return iHaveInclusion;
}
public void setInclusion (boolean b) {
iHaveInclusion = b;
}
public boolean hasExclusion () {
return iHaveExclusion;
}
public void setExclusion (boolean b) {
iHaveExclusion = b;
}
public boolean hasTargets () {
return iHaveTargets;
}
public void setHasTargets (boolean b) {
iHaveTargets = b;
}
}