'''
Created on Dec 17, 2014

@author: Dominik Lang
'''

import csv
import os.path
from random import shuffle
import collections
import numpy
from sklearn.preprocessing import Imputer


class DataHandler(object):

    def __init__(self):
        pass
    
    '''
    @summary: A method to handle reading the data in from the csv file
    @return: List containing the rows of the dataset as seperate lists
    '''
    def readData(self):
        # We get the path to the current file, then go one directory up to find the data file
        basepath = os.path.dirname(__file__)
        filepath = os.path.abspath(os.path.join(basepath, "..","data.csv"))
        
        output=[]
    
        with open(filepath, 'rb') as csvfile:
            i=0
            linereader = csv.reader(csvfile, delimiter=',')
            for row in linereader:
                if i==0:
                    i+=1
                    continue
                    
                output.append(row)
                
        return output

    '''
    @summary: A method that splits the dataset into a training and a test set
    '''
    def splitData(self,dataset):
        sets = collections.namedtuple('Sets', ['train', 'test'])
        third=len(dataset)/3
        shuffle(dataset)
        testset=dataset[0:third]
        trainset=dataset[third:-1]
        s=sets(trainset,testset)
        
        return s
    
    def vectorizeData(self,dataset):
        vectors = collections.namedtuple('vectors', ['X', 'Y'])
        x=[]
        y=[]
        
        for i in dataset:
            atts=i[0:-2]
            c=i[-1]
            x.append(atts)
            y.append(c)
        
        x=numpy.asarray(x)
        y=numpy.asarray(y)
        output=vectors(x,y)
        return output
    
