Program: 117185
Project: 117185
Buggy source:
/** 
 * Generates a set of Gnuplot scripts for visualizing low dimensional components in a GAM.
 * @param gam the GAM model.
 * @param instances the training set.
 * @param dirPath the directory path to write to.
 * @param outputTerminal output plot format (png or pdf).
 * @throws IOException
 */
public static void generateGnuplotScripts(GAM gam,Instances instances,String dirPath,Terminal outputTerminal) throws IOException {
  List<Attribute> attributes=instances.getAttributes();
  Map<Integer,Attribute> attMap=new HashMap<>(attributes.size());
  for (  Attribute attribute : attributes) {
    attMap.put(attribute.getIndex(),attribute);
  }
  List<int[]> terms=gam.getTerms();
  List<Regressor> regressors=gam.getRegressors();
  File dir=new File(dirPath);
  if (!dir.exists()) {
    dir.mkdirs();
  }
  double[] value=new double[attributes.size()];
  Instance point=new Instance(value);
  String terminal=outputTerminal.toString();
  for (int i=0; i < terms.size(); i++) {
    int[] term=terms.get(i);
    Regressor regressor=regressors.get(i);
    if (term.length == 1) {
      Attribute f=attMap.get(term[0]);
switch (f.getType()) {
case BINNED:
        int numBins=((BinnedAttribute)f).getNumBins();
      if (numBins == 1) {
        continue;
      }
    break;
case NOMINAL:
  int numStates=((NominalAttribute)f).getStates().length;
if (numStates == 1) {
  continue;
}
break;
default :
break;
}
PrintWriter out=new PrintWriter(dir.getAbsolutePath() + File.separator + f.getName()+ "String_Node_Str");
out.printf("String_Node_Str",terminal);
out.printf("String_Node_Str",f.getName(),terminal);
out.println("String_Node_Str");
out.println("String_Node_Str");
switch (f.getType()) {
case BINNED:
int numBins=((BinnedAttribute)f).getNumBins();
Bins bins=((BinnedAttribute)f).getBins();
double[] boundaries=bins.getBoundaries();
double start=boundaries[0] - 1;
if (boundaries.length >= 2) {
start=boundaries[0] - (boundaries[1] - boundaries[0]);
}
out.printf("String_Node_Str",start,boundaries[boundaries.length - 1]);
out.println("String_Node_Str");
List<Double> predList=new ArrayList<>();
for (int j=0; j < numBins; j++) {
point.setValue(term[0],j);
predList.add(regressor.regress(point));
}
point.setValue(term[0],0);
out.printf("String_Node_Str",start,predList.get(0));
for (int j=0; j < numBins; j++) {
point.setValue(term[0],j);
out.printf("String_Node_Str",boundaries[j],predList.get(j));
if (j < numBins - 1) {
out.printf("String_Node_Str",boundaries[j],predList.get(j + 1));
}
}
out.println("String_Node_Str");
break;
case NOMINAL:
out.println("String_Node_Str");
out.println("String_Node_Str");
out.println("String_Node_Str");
out.println("String_Node_Str");
out.println("String_Node_Str");
out.println("String_Node_Str");
String[] states=((NominalAttribute)f).getStates();
for (int j=0; j < states.length; j++) {
point.setValue(term[0],j);
out.printf("String_Node_Str",states[j],regressor.regress(point));
}
out.println("String_Node_Str");
break;
default :
Set<Double> values=new HashSet<>();
for (Instance instance : instances) {
values.add(instance.getValue(term[0]));
}
List<Double> list=new ArrayList<>(values);
Collections.sort(list);
out.printf("String_Node_Str",list.get(0),list.get(list.size() - 1));
if (regressor instanceof CubicSpline) {
CubicSpline spline=(CubicSpline)regressor;
out.println("String_Node_Str");
out.println("String_Node_Str");
double[] knots=spline.getKnots();
double[] w=spline.getCoefficients();
StringBuilder sb=new StringBuilder();
sb.append("String_Node_Str").append(spline.getIntercept());
sb.append("String_Node_Str").append(w[0]).append("String_Node_Str");
sb.append("String_Node_Str").append(w[1]).append("String_Node_Str");
sb.append("String_Node_Str").append(w[2]).append("String_Node_Str");
for (int j=0; j < knots.length; j++) {
sb.append("String_Node_Str").append(w[j + 3]).append("String_Node_Str");
sb.append("String_Node_Str").append(knots[j]).append("String_Node_Str");
}
sb.append("String_Node_Str");
out.println(sb.toString());
}
 else {
out.println("String_Node_Str");
for (double v : list) {
point.setValue(term[0],v);
out.printf("String_Node_Str",v,regressor.regress(point));
}
}
break;
}
out.flush();
out.close();
}
 else if (term.length == 2) {
Attribute f1=attMap.get(term[0]);
Attribute f2=attMap.get(term[1]);
PrintWriter out=new PrintWriter(dir.getAbsolutePath() + File.separator + f1.getName()+ "String_Node_Str"+ f2.getName()+ "String_Node_Str");
out.printf("String_Node_Str",terminal);
out.printf("String_Node_Str",f1.getName(),f2.getName(),terminal);
out.println("String_Node_Str");
int size1=0;
if (f1.getType() == Attribute.Type.BINNED) {
size1=((BinnedAttribute)f1).getNumBins();
}
 else if (f1.getType() == Attribute.Type.NOMINAL) {
size1=((NominalAttribute)f1).getCardinality();
}
int size2=0;
if (f2.getType() == Attribute.Type.BINNED) {
size2=((BinnedAttribute)f2).getNumBins();
}
 else if (f2.getType() == Attribute.Type.NOMINAL) {
size2=((NominalAttribute)f2).getCardinality();
}
if (f1.getType() == Attribute.Type.NOMINAL) {
out.print("String_Node_Str");
String[] states=((NominalAttribute)f1).getStates();
for (int j=0; j < states.length; j++) {
out.printf("String_Node_Str",states[j],j);
}
out.println("String_Node_Str");
}
if (f2.getType() == Attribute.Type.NOMINAL) {
out.print("String_Node_Str");
String[] states=((NominalAttribute)f2).getStates();
for (int j=0; j < states.length; j++) {
out.printf("String_Node_Str",states[j],j);
}
out.println("String_Node_Str");
}
out.println("String_Node_Str");
out.println("String_Node_Str");
out.println("String_Node_Str");
out.println("String_Node_Str");
Bins bins1=((BinnedAttribute)f1).getBins();
double[] boundaries1=bins1.getBoundaries();
double start1=boundaries1[0] - 1;
if (boundaries1.length >= 2) {
start1=boundaries1[0] - (boundaries1[1] - boundaries1[0]);
}
Bins bins2=((BinnedAttribute)f2).getBins();
double[] boundaries2=bins2.getBoundaries();
double start2=boundaries2[0] - 1;
if (boundaries2.length >= 2) {
start2=boundaries2[0] - (boundaries2[1] - boundaries2[0]);
}
out.printf("String_Node_Str",start1,boundaries1[boundaries1.length - 1]);
out.printf("String_Node_Str",start2,boundaries2[boundaries2.length - 1]);
out.println("String_Node_Str");
for (int r=-1; r < size1; r++) {
if (r == -1) {
point.setValue(term[0],0);
}
 else {
point.setValue(term[0],r);
}
for (int c=-1; c < size2; c++) {
if (c == -1) {
point.setValue(term[1],0);
}
 else {
point.setValue(term[1],c);
}
if (c == -1) {
out.print(start2 + "String_Node_Str");
}
 else {
out.print(boundaries2[c] + "String_Node_Str");
}
if (r == -1) {
out.print(start1 + "String_Node_Str");
}
 else {
out.print(boundaries1[r] + "String_Node_Str");
}
out.println(gam.regress(point));
}
out.println();
}
out.println("String_Node_Str");
out.flush();
out.close();
}
}
}

Fixed version (ground truth):
/** 
 * Generates a set of Gnuplot scripts for visualizing low dimensional components in a GAM.
 * @param gam the GAM model.
 * @param instances the training set.
 * @param dirPath the directory path to write to.
 * @param outputTerminal output plot format (png or pdf).
 * @throws IOException
 */
public static void generateGnuplotScripts(GAM gam,Instances instances,String dirPath,Terminal outputTerminal) throws IOException {
  List<Attribute> attributes=instances.getAttributes();
  int p=-1;
  Map<Integer,Attribute> attMap=new HashMap<>(attributes.size());
  for (  Attribute attribute : attributes) {
    int attIndex=attribute.getIndex();
    attMap.put(attIndex,attribute);
    if (attIndex > p) {
      p=attIndex;
    }
  }
  p++;
  List<int[]> terms=gam.getTerms();
  List<Regressor> regressors=gam.getRegressors();
  File dir=new File(dirPath);
  if (!dir.exists()) {
    dir.mkdirs();
  }
  double[] value=new double[p];
  Instance point=new Instance(value);
  String terminal=outputTerminal.toString();
  for (int i=0; i < terms.size(); i++) {
    int[] term=terms.get(i);
    Regressor regressor=regressors.get(i);
    if (term.length == 1) {
      Attribute f=attMap.get(term[0]);
switch (f.getType()) {
case BINNED:
        int numBins=((BinnedAttribute)f).getNumBins();
      if (numBins == 1) {
        continue;
      }
    break;
case NOMINAL:
  int numStates=((NominalAttribute)f).getStates().length;
if (numStates == 1) {
  continue;
}
break;
default :
break;
}
PrintWriter out=new PrintWriter(dir.getAbsolutePath() + File.separator + f.getName()+ "String_Node_Str");
out.printf("String_Node_Str",terminal);
out.printf("String_Node_Str",f.getName(),terminal);
out.println("String_Node_Str");
out.println("String_Node_Str");
switch (f.getType()) {
case BINNED:
int numBins=((BinnedAttribute)f).getNumBins();
Bins bins=((BinnedAttribute)f).getBins();
double[] boundaries=bins.getBoundaries();
double start=boundaries[0] - 1;
if (boundaries.length >= 2) {
start=boundaries[0] - (boundaries[1] - boundaries[0]);
}
out.printf("String_Node_Str",start,boundaries[boundaries.length - 1]);
out.println("String_Node_Str");
List<Double> predList=new ArrayList<>();
for (int j=0; j < numBins; j++) {
point.setValue(term[0],j);
predList.add(regressor.regress(point));
}
point.setValue(term[0],0);
out.printf("String_Node_Str",start,predList.get(0));
for (int j=0; j < numBins; j++) {
point.setValue(term[0],j);
out.printf("String_Node_Str",boundaries[j],predList.get(j));
if (j < numBins - 1) {
out.printf("String_Node_Str",boundaries[j],predList.get(j + 1));
}
}
out.println("String_Node_Str");
break;
case NOMINAL:
out.println("String_Node_Str");
out.println("String_Node_Str");
out.println("String_Node_Str");
out.println("String_Node_Str");
out.println("String_Node_Str");
out.println("String_Node_Str");
String[] states=((NominalAttribute)f).getStates();
for (int j=0; j < states.length; j++) {
point.setValue(term[0],j);
out.printf("String_Node_Str",states[j],regressor.regress(point));
}
out.println("String_Node_Str");
break;
default :
Set<Double> values=new HashSet<>();
for (Instance instance : instances) {
values.add(instance.getValue(term[0]));
}
List<Double> list=new ArrayList<>(values);
Collections.sort(list);
out.printf("String_Node_Str",list.get(0),list.get(list.size() - 1));
if (regressor instanceof CubicSpline) {
CubicSpline spline=(CubicSpline)regressor;
out.println("String_Node_Str");
out.println("String_Node_Str");
double[] knots=spline.getKnots();
double[] w=spline.getCoefficients();
StringBuilder sb=new StringBuilder();
sb.append("String_Node_Str").append(spline.getIntercept());
sb.append("String_Node_Str").append(w[0]).append("String_Node_Str");
sb.append("String_Node_Str").append(w[1]).append("String_Node_Str");
sb.append("String_Node_Str").append(w[2]).append("String_Node_Str");
for (int j=0; j < knots.length; j++) {
sb.append("String_Node_Str").append(w[j + 3]).append("String_Node_Str");
sb.append("String_Node_Str").append(knots[j]).append("String_Node_Str");
}
sb.append("String_Node_Str");
out.println(sb.toString());
}
 else {
out.println("String_Node_Str");
for (double v : list) {
point.setValue(term[0],v);
out.printf("String_Node_Str",v,regressor.regress(point));
}
}
break;
}
out.flush();
out.close();
}
 else if (term.length == 2) {
Attribute f1=attMap.get(term[0]);
Attribute f2=attMap.get(term[1]);
PrintWriter out=new PrintWriter(dir.getAbsolutePath() + File.separator + f1.getName()+ "String_Node_Str"+ f2.getName()+ "String_Node_Str");
out.printf("String_Node_Str",terminal);
out.printf("String_Node_Str",f1.getName(),f2.getName(),terminal);
out.println("String_Node_Str");
int size1=0;
if (f1.getType() == Attribute.Type.BINNED) {
size1=((BinnedAttribute)f1).getNumBins();
}
 else if (f1.getType() == Attribute.Type.NOMINAL) {
size1=((NominalAttribute)f1).getCardinality();
}
int size2=0;
if (f2.getType() == Attribute.Type.BINNED) {
size2=((BinnedAttribute)f2).getNumBins();
}
 else if (f2.getType() == Attribute.Type.NOMINAL) {
size2=((NominalAttribute)f2).getCardinality();
}
if (f1.getType() == Attribute.Type.NOMINAL) {
out.print("String_Node_Str");
String[] states=((NominalAttribute)f1).getStates();
for (int j=0; j < states.length; j++) {
out.printf("String_Node_Str",states[j],j);
}
out.println("String_Node_Str");
}
if (f2.getType() == Attribute.Type.NOMINAL) {
out.print("String_Node_Str");
String[] states=((NominalAttribute)f2).getStates();
for (int j=0; j < states.length; j++) {
out.printf("String_Node_Str",states[j],j);
}
out.println("String_Node_Str");
}
out.println("String_Node_Str");
out.println("String_Node_Str");
out.println("String_Node_Str");
out.println("String_Node_Str");
Bins bins1=((BinnedAttribute)f1).getBins();
double[] boundaries1=bins1.getBoundaries();
double start1=boundaries1[0] - 1;
if (boundaries1.length >= 2) {
start1=boundaries1[0] - (boundaries1[1] - boundaries1[0]);
}
Bins bins2=((BinnedAttribute)f2).getBins();
double[] boundaries2=bins2.getBoundaries();
double start2=boundaries2[0] - 1;
if (boundaries2.length >= 2) {
start2=boundaries2[0] - (boundaries2[1] - boundaries2[0]);
}
out.printf("String_Node_Str",start1,boundaries1[boundaries1.length - 1]);
out.printf("String_Node_Str",start2,boundaries2[boundaries2.length - 1]);
out.println("String_Node_Str");
for (int r=-1; r < size1; r++) {
if (r == -1) {
point.setValue(term[0],0);
}
 else {
point.setValue(term[0],r);
}
for (int c=-1; c < size2; c++) {
if (c == -1) {
point.setValue(term[1],0);
}
 else {
point.setValue(term[1],c);
}
if (c == -1) {
out.print(start2 + "String_Node_Str");
}
 else {
out.print(boundaries2[c] + "String_Node_Str");
}
if (r == -1) {
out.print(start1 + "String_Node_Str");
}
 else {
out.print(boundaries1[r] + "String_Node_Str");
}
out.println(gam.regress(point));
}
out.println();
}
out.println("String_Node_Str");
out.flush();
out.close();
}
}
}