package de.mpi.rgblab.data;
import java.util.*;
import java.io.*;
import java.lang.Integer;
public class DataReader {
// private variables
private String FileName;
private Vector< Vector<Double> > data;
private Vector<Integer> data_class_label;
private HashMap<Integer, Integer> data_class_label_index;
private int data_count;
private int dim_count;
private int number_classes;
private double min_y = 100000;
private double max_y = -100000;
private double min_x = 100000;;
private double max_x = -100000;;;
// public interface
public DataReader(String file) {
// FileName
this.FileName=file;
// Data
this.data=new Vector< Vector<Double> >();
// Data counter
this.data_count=0;
// class label
data_class_label = new Vector<Integer>();
// class label index
data_class_label_index = new HashMap<Integer, Integer>();
// class label
TreeSet<Integer> class_labels = new TreeSet<Integer>();
// needed to read file
String data_line;
BufferedReader f;
// first run flag
int is_first_run=1;
try {
f=new BufferedReader(new FileReader(this.FileName));
while((data_line=f.readLine()) != null) {
String[] result = data_line.split("[,;|]");
if(is_first_run > 0) {
this.dim_count=result.length-1;
}
if (result.length !=3)
continue;
Vector<Double> data_item=new Vector<Double>();
for (int i=0; i<(result.length-1); i++) {
Double my_data_value_double = Double.valueOf(result[i]);
data_item.addElement(my_data_value_double);
}
if (max_y < data_item.get(1))
max_y = data_item.get(1);
if (min_y > data_item.get(1))
min_y = data_item.get(1);
if (max_x < data_item.get(0))
max_x = data_item.get(0);
if (min_x > data_item.get(0))
min_x = data_item.get(0);
data.addElement(data_item);
Integer my_class_label= (Integer) Integer.valueOf(result[result.length-1]);
data_class_label.addElement(my_class_label);
class_labels.add(my_class_label);
this.data_count++;
is_first_run=0;
}
int class_count_id=0;
Iterator it = class_labels.iterator();
while(it.hasNext()) {
int class_label=(Integer) it.next();
data_class_label_index.put(class_label, class_count_id);
class_count_id++;
}
f.close();
// number of classes
this.number_classes=data_class_label_index.size();
} catch (IOException e) {
System.out.println("Can't read file");
}
}
public void get_data(Vector< Vector<Float> > my_data) {
//my_data = data;
Iterator it=data.iterator();
while(it.hasNext()) {
Vector<Float> my_data_item = new Vector<Float>();
Vector<Double> data_item=(Vector<Double>) it.next();
Iterator it2=data_item.iterator();
while(it2.hasNext()) {
Double my_item=(Double) it2.next();
my_data_item.addElement((float) (double)my_item);
}
my_data.addElement(my_data_item);
}
}
public void get_data_int(Vector< Vector<Integer> > my_data) {
//my_data = data;
Iterator it=data.iterator();
while(it.hasNext()) {
Vector<Integer> my_data_item = new Vector<Integer>();
Vector<Double> data_item=(Vector<Double>) it.next();
Iterator it2=data_item.iterator();
while(it2.hasNext()) {
Double my_item=(Double) it2.next();
my_data_item.addElement((int) (double) my_item);
}
my_data.addElement(my_data_item);
}
}
public void get_class_label(Vector<Integer> my_class_label) {
//my_class_label = data_class_label;
Iterator it=data_class_label.iterator();
while(it.hasNext()) {
Integer my_label=(Integer) it.next();
my_class_label.addElement(get_class_label_index(my_label));
}
}
private int get_class_label_index(int my_class_label) {
return data_class_label_index.get(my_class_label);
}
public int get_number_classes() {
return this.number_classes;
}
public double get_max_x() {
return max_x;
}
public double get_max_y() {
return max_y;
}
public double get_min_x() {
return min_x;
}
public double get_min_y() {
return min_y;
}
public int get_data_count() {
return this.data_count;
}
public int get_dim_count() {
return this.dim_count;
}
}