See More

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 filevars= new ArrayList(); 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 updateVariables(InputFile inputrow, HashMap 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 coeffs = new HashMap(); /** The modelfiles. */ private HashMap modelfiles = new HashMap(); private List vars_without_file = new ArrayList(); /** 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 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 fields_data = new HashMap(); //(header -> value) CSVReader reader = new CSVReader(new FileReader(configfile), ';', '"'); List 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 value) //this prohibits errors from wrong column order in config file for (int j=0; j 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 updateVariables(InputFile inputfile, HashMap existingVars ) { if (inputfileIsRelevant(inputfile)) return modelfiles.get(inputfile).updateVariables(inputfile,existingVars); else return existingVars; } public HashMap updateVariablesNoInputfile(HashMap 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; } }