Package de.mpi.rgblab.data

Source Code of de.mpi.rgblab.data.Data0

package de.mpi.rgblab.data;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Iterator;
import java.util.Set;
import java.util.SortedMap;
import java.util.TreeMap;
import java.util.Vector;

import de.mpi.rgblab.color.ColorModel;
import de.mpi.rgblab.color.ColorTransformer;
import de.mpi.rgblab.color.ColorType;
import de.mpi.rgblab.math.numeric.Numeric;

public class Data0 {

  // current data is a vector of coordinates (x,y), the classlabel and
  // measures (z_score, visibility_score)
  // x, y, classlabel, z_score, visibility_score
  protected Vector<Vector<Double>> coordinates; // = new
                          // Vector<Vector<Double>>();
  protected Vector<Integer> class_labels; // = new Vector<Integer>();
  protected Vector<Double> saliency; // = new Vector<Double>();
  protected Vector<Double> visibility; // = new Vector<Double>();

  protected int classNumbers;

  // protected Vector<ColorModel> rgbColors = new Vector<ColorModel>();
  // protected Vector<ColorModel> labColors = new Vector<ColorModel>();

  // binary search tree for every classlabel. given the x and y coordinates we
  // can search in log time for a certain poitn or set of points.
  protected TreeMap[] ts;

  protected double min_saliency;
  protected double max_saliency;
  protected double min_visibility;
  protected double max_visibility;

  protected String outputFile = "measures.dat";
  // protected String inputFile =
  // "usa_houshold_and_income_screen_coordinates.dat";

  protected static ColorModel labWhite = new ColorModel(100, 0, 0,
      ColorType.LAB);

  public Data0() {
    // init();
  }

  public boolean importData(String inputData) {
    coordinates = new Vector<Vector<Double>>();
    class_labels = new Vector<Integer>();
    saliency = new Vector<Double>();
    visibility = new Vector<Double>();

    readInputData(inputData);

    ts = new TreeMap[classNumbers];

    for (int i = 0; i < classNumbers; i++) {
      ts[i] = new TreeMap<Vector<Double>, Vector<Double>>(
          new DataComparator());
    }
    // init tree
    for (int i = 0; i < coordinates.size(); i++) {
      int label = class_labels.get(i).intValue();
      Vector<Double> tmp = new Vector<Double>();
      tmp.add((double) label);
      ts[label].put((Vector<Double>) coordinates.get(i), tmp);
    }

    initMinSaliency();
    initMaxSaliency();
    initMinVisibility();
    initMaxVisibility();

    return true;

  }

  private void initMinSaliency() {
    min_saliency = 1000000;
  }

  private void initMaxSaliency() {
    max_saliency = 0;
  }

  private void initMinVisibility() {
    min_visibility = 1000000;

  }

  private void initMaxVisibility() {
    max_visibility = 0;
  }

  // public Data(Vector<ColorModel> rgbColors) {
  // this.rgbColors = rgbColors;
  // init();
  //
  // }
  //
  // public Data(Vector<ColorModel> rgbColors, String inputFile) {
  // this.rgbColors = rgbColors;
  // this.inputFile = inputFile;
  // init();
  // }

  private static void rgbToLabColors(Vector<ColorModel> rgbColors,
      Vector<ColorModel> labColors) {
    // copy the the vector to an array
    ColorModel[] my_rgbColor = new ColorModel[rgbColors.size()];
    rgbColors.copyInto(my_rgbColor);

    // transform to lab space
    ColorModel labColor[] = ColorTransformer.rgb2lab(my_rgbColor);

    // delete the old elements
    if (false == labColors.isEmpty())
      labColors.removeAllElements();
    // array to vector
    for (int i = 0; i < labColor.length; i++) {
      labColors.addElement(labColor[i]);
    }
  }

  private void readInputData(String inputFile) {

    // read the input data

    DataReader fileReader = new DataReader(inputFile);
    fileReader.get_data(coordinates);
    fileReader.get_class_label(class_labels);
    classNumbers = fileReader.get_number_classes();

    /*
     * adding the class_labels to the vector of the coordinates
     *
     * Vector <Double> row = new Vector<Double>(); double classLabel;
     * Vector<Integer> class_labels = new Vector<Integer>();
     * fileReader.get_class_label(class_labels);
     *
     * for(int j=0; j< current_data.size(); j++) { row = (Vector<Double>)
     * current_data.get(j); classLabel = class_labels.get(j);
     * row.add(classLabel); current_data.set(j, row); }
     */
  }

  public double getMaxSaliency() {
    return max_saliency;
  }

  public double getMinSaliency() {
    return min_saliency;
  }

  public double getMaxVisibility() {
    return max_visibility;
  }

  public double getMinVisibility() {
    return min_visibility;
  }

  public Vector<Double> getVisibility() {
    return visibility;
  }

  public Vector<Double> getSaliency() {
    return saliency;
  }

  public TreeMap[] computeVisibility(int radius) { // , Vector<ColorModel>
                            // rgbColors) {

    initMinVisibility();
    initMaxVisibility();
    visibility.clear();
    // this.rgbColors = rgbColors_;
    // Vector<ColorModel> labColors = new Vector<ColorModel>();
    // rgbToLabColors(rgbColors, labColors);

    System.out.println("start computing visibility");
    long currentTimeInSeconds = System.currentTimeMillis() / 1000;

    double x1 = 0, y1 = 0, x2 = 0, y2 = 0, z, z2, visibility_score;
    Vector<Double> key_row, key_row2, value_row, value_row2;

    Set[] valuesSet = new Set[classNumbers];
    Set[] keysSet = new Set[classNumbers];

    // k - current class label
    for (int k = 0; k < classNumbers; k++) {
      Iterator it_keys = ts[k].keySet().iterator();
      Iterator it_values = ts[k].values().iterator();

      while (it_keys.hasNext()) {
        key_row = (Vector<Double>) it_keys.next();
        x1 = key_row.elementAt(0);
        y1 = key_row.elementAt(1);

        value_row = (Vector<Double>) it_values.next();
        z = value_row.get(1);

        Vector<Double> border1 = new Vector<Double>();
        border1.add(x1 - radius);
        border1.add(y1 - radius);

        Vector<Double> border2 = new Vector<Double>();
        border2.add(x1 + radius);
        border2.add(y1 + radius);

        SortedMap s = ts[k].subMap(border1, border2);
        Iterator it_k = s.keySet().iterator();
        Iterator it_v = s.values().iterator();
        while (it_k.hasNext()) {
          key_row2 = (Vector<Double>) it_k.next();
          x2 = key_row2.elementAt(0);
          y2 = key_row2.elementAt(1);
          value_row2 = (Vector<Double>) it_v.next();
          z2 = value_row2.get(1);

          if (x2 != x1 && y2 != y1) {
            if ((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2) <= Math
                .pow(radius, 2)) {
              z += z2;
              // break;
            }
          }
        }

        if (z > max_visibility)
          max_visibility = z;
        if (z < min_visibility)
          min_visibility = z;

        if (value_row.size() < 3)
          value_row.add(z);
        else
          value_row.set(2, z);
        visibility.add(z);
      }
    }

    currentTimeInSeconds = System.currentTimeMillis() / 1000
        - currentTimeInSeconds;
    System.out.println("visibility computed in: " + currentTimeInSeconds
        + "seconds");
    return ts;
  }

  public TreeMap[] getData() {
    return ts;
  }

  public Vector<Vector<Double>> getCoordinates() {
    return coordinates;
  }

  public TreeMap[] computeSaliency(int radius, Vector<ColorModel> rgbColors) {
    System.out.println("start computing saliency");
    long currentTimeInSeconds = System.currentTimeMillis() / 1000;

    computeSaliencyTest(radius, rgbColors, 1);

    currentTimeInSeconds = System.currentTimeMillis() / 1000
        - currentTimeInSeconds;
    System.out.println("saliency computed in: " + currentTimeInSeconds
        + "seconds");
    return ts;
  }

  public TreeMap[] computeSaliency(double radius,
      Vector<ColorModel> rgbColors, int computing_type) {

    initMinSaliency();
    initMaxSaliency();
    saliency.clear();
    // int my_count1 = 0;
    // tmp variables
    // Iterator it_map_matrix1 = coordinates.iterator();
    // Iterator it_map_matrix2;
    // Iterator it_tmp = it_map_matrix1;

    double x1 = 0, y1 = 0, x2 = 0, y2 = 0;
    Vector<Double> key_row, key_row2, value_row;
    ColorModel tmp;
    double z = 0;

    Vector<ColorModel> labColors = new Vector<ColorModel>();
    rgbToLabColors(rgbColors, labColors);

    Vector<ColorModel> nbh = new Vector<ColorModel>();

    // k - current class label
    for (int k = 0; k < classNumbers; k++) {

      Iterator it_values = ts[k].values().iterator();
      Iterator it_keys = ts[k].keySet().iterator();

      while (it_keys.hasNext()) {
        key_row = (Vector<Double>) it_keys.next();
        x1 = key_row.elementAt(0);
        y1 = key_row.elementAt(1);

        value_row = (Vector<Double>) it_values.next();

        Vector<Double> border1 = new Vector<Double>();
        border1.add(x1 - radius);
        border1.add(y1 - radius);
        // border1.add(x1);
        // border1.add(y1);

        Vector<Double> border2 = new Vector<Double>();
        border2.add(x1 + radius);
        border2.add(y1 + radius);

        nbh.add(labWhite);

        for (int i = 0; i < classNumbers; i++) {

          SortedMap s = ts[i].subMap(border1, border2);
          Iterator it_s = s.keySet().iterator();
          while (it_s.hasNext()) {
            key_row2 = (Vector<Double>) it_s.next();
            x2 = key_row2.elementAt(0);
            y2 = key_row2.elementAt(1);

            if (x2 != x1 && y2 != y1) {
              if ((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2) <= Math
                  .pow(radius, 2)) {
                nbh.add((ColorModel) labColors.elementAt(i));
                break;
              } else if (x1 * x1 > (x1 + radius) * (x1 + radius)) {
                System.out.println("problem");
              }
            }
          }
        }

        tmp = (ColorModel) labColors.elementAt(k);

        // compute z score
        // if (computing_type == 0)
        // z = Numeric.compute_z_score(tmp, nbh);
        // // compute euclidian distance
        // else if (computing_type == 1) {
        // System.out.println("current color: " + tmp);
        // System.out.println("neigbours' color: " + nbh);
        z = Numeric.eucl_Distance(tmp, nbh);
        // } else {
        // System.out.println("false computing type");
        // return null;
        // }

        if (z > max_saliency)
          max_saliency = z;
        if (z < min_saliency)
          min_saliency = z;

        if (value_row.size() < 2)
          value_row.add(z);
        else
          value_row.set(1, z);

        saliency.add(z);
        nbh.clear();

      }

    }

    return ts;

  }

  public TreeMap[] computeSaliencyTest(double radius,
      Vector<ColorModel> rgbColors, int computing_type) {

    BufferedWriter out;
    FileWriter fw;

    initMinSaliency();
    initMaxSaliency();
    saliency.clear();
    // int my_count1 = 0;
    // tmp variables
    // Iterator it_map_matrix1 = coordinates.iterator();
    // Iterator it_map_matrix2;
    // Iterator it_tmp = it_map_matrix1;

    double x1 = 0, y1 = 0, x2 = 0, y2 = 0;
    Vector<Double> key_row, key_row2, value_row;
    ColorModel tmp;
    double z = 0;

    Vector<ColorModel> labColors = new Vector<ColorModel>();
    rgbToLabColors(rgbColors, labColors);

    Vector<ColorModel> nbh = new Vector<ColorModel>();

    // k - current class label

    try {
      File file = new File("saliency0_test.dat");
      fw = new FileWriter(file);
      out = new BufferedWriter(fw);
      for (int k = 0; k < classNumbers; k++) {

        Iterator it_values = ts[k].values().iterator();
        Iterator it_keys = ts[k].keySet().iterator();

        while (it_keys.hasNext()) {
          key_row = (Vector<Double>) it_keys.next();
          x1 = key_row.elementAt(0);
          y1 = key_row.elementAt(1);

          value_row = (Vector<Double>) it_values.next();

          Vector<Double> border1 = new Vector<Double>();
          border1.add(x1 - radius);
          border1.add(y1 - radius);
          // border1.add(x1);
          // border1.add(y1);

          Vector<Double> border2 = new Vector<Double>();
          border2.add(x1 + radius);
          border2.add(y1 + radius);

       

          for (int i = 0; i < classNumbers; i++) {

            SortedMap s = ts[i].subMap(border1, border2);
            Iterator it_s = s.keySet().iterator();
            while (it_s.hasNext()) {
              key_row2 = (Vector<Double>) it_s.next();
              x2 = key_row2.elementAt(0);
              y2 = key_row2.elementAt(1);

              if (x2 != x1 && y2 != y1) {
                if ((x1 - x2) * (x1 - x2) + (y1 - y2)
                    * (y1 - y2) <= Math.pow(radius, 2)) {
                  nbh
                      .add((ColorModel) labColors
                          .elementAt(i));
                  break;
                } else if (x1 * x1 > (x1 + radius)
                    * (x1 + radius)) {
                  System.out.println("problem");
                }
              }
            }
          }

          nbh.add(labWhite);
          tmp = (ColorModel) labColors.elementAt(k);

          // compute z score
          // if (computing_type == 0)
          // z = Numeric.compute_z_score(tmp, nbh);
          // // compute euclidian distance
          // else if (computing_type == 1) {
          // System.out.println("current color: " + tmp);
          // System.out.println("neigbours' color: " + nbh);
          z = Numeric.eucl_Distance(tmp, nbh);
          // } else {
          // System.out.println("false computing type");
          // return null;
          // }

          if (z > max_saliency)
            max_saliency = z;
          if (z < min_saliency)
            min_saliency = z;

          if (value_row.size() < 2)
            value_row.add(z);
          else
            value_row.set(1, z);

          out.write(Double.toString(x1));
          out.write("|");
          out.write(Double.toString(y1));
          out.write("|");
          out.write(Integer.toString(k));
          out.write("|");
          out.write(Double.toString(z));
          out.write("|");
          out.write(Integer.toString(nbh.size()));
          out.write("|");
          for (int tmpi = 0; tmpi < nbh.size(); tmpi++) {
            out.write(Double.toString(nbh.get(tmpi).getDimension_1()));
            out.write(",");
            out.write(Double.toString(nbh.get(tmpi).getDimension_2()));
            out.write(",");
            out.write(Double.toString(nbh.get(tmpi).getDimension_3()));
            out.write("|");
          }
          out.write(System.getProperty("line.separator"));
          saliency.add(z);
          nbh.clear();

        }

      }
      out.flush();
      out.close();
    } catch (IOException e) {
      e.printStackTrace();
    }

    return ts;

  }

  public void export(String filename) {
    outputFile = filename;
    export();
  }

  public void export() {
    BufferedWriter out;
    FileWriter fw;

    double x1 = 0, y1 = 0, class_ = 0, sal_score = 0, vis_score = 0;
    Vector<Double> key_row, key_row2, value_row;

    try {
      File file = new File(outputFile);
      fw = new FileWriter(file);
      out = new BufferedWriter(fw);

      for (int k = 0; k < classNumbers; k++) {
        Iterator it_keys = ts[k].keySet().iterator();
        Iterator it_values = ts[k].values().iterator();

        while (it_keys.hasNext()) {
          key_row = (Vector<Double>) it_keys.next();
          x1 = key_row.elementAt(0);
          y1 = key_row.elementAt(1);

          value_row = (Vector<Double>) it_values.next();
          class_ = value_row.get(0);
          try {
            sal_score = value_row.get(1);
            vis_score = value_row.get(2);
          } catch (Exception e) {

          }

          out.write(Double.toString(x1));
          out.write('|');
          out.write(Double.toString(y1));
          out.write('|');
          out.write(Double.toString(class_));
          out.write('|');
          out.write(Double.toString(sal_score));
          out.write('|');
          out.write(Double.toString(vis_score));
          out.write(System.getProperty("line.separator"));
        }

      }
      out.flush();
      out.close();
    } catch (IOException e) {
      e.printStackTrace();
    }

  }

  public int getNumberOfClasses() {
    return classNumbers;
  }
}
TOP

Related Classes of de.mpi.rgblab.data.Data0

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.