# Path: mimicus/tools/datasets.py
# def csv2numpy(csv_in):
# def numpy2csv(csv_out, X, y, file_names=None):
# def standardize_csv(csv_in, csv_out, standardizer=None):
#     TOTAL_ROWS = 0
#     X = numpy.array(numpy.zeros((TOTAL_ROWS, FeatureDescriptor.get_feature_count())), dtype=numpy.float64, order='C')


import random
import string
import tempfile
import threading
import numpy
import rpy2.rinterface as rinterface
import rpy2.robjects as robjects
from sklearn.base import BaseEstimator
from mimicus.tools import datasets
    
    def decision_function(self, X):
        '''
        Classifies novel data points using a trained model. Returns a 
        list of predictions, one per data point, giving the probability 
        of the given data point belonging to the positive class. 
        '''
        assert self.model_trained, 'Must train a model before classification'
        with _R_lock:
            with tempfile.NamedTemporaryFile() as tmpfile:
                datasets.numpy2csv(tmpfile, X, numpy.zeros((X.shape[0],)))
                tmpfile.seek(0)
                # Read in the CSV file with the samples to be classified, omitting the second column (filename)
                robjects.r('{novel} <- read.csv("{csv}", header=TRUE, colClasses={cc})'.format(novel=self.noveldata_Rname, csv=tmpfile.name, cc=_r_colClasses))
                # Classify the new data points
                robjects.r('{pred} <- predict({model}, {novel}, type="prob")'.format(pred=self.predictions_Rname, model=self.model_Rname, novel=self.noveldata_Rname))
                predictions = list(robjects.r['{pred}'.format(pred=self.predictions_Rname)])
        # The first half of predictions is for the negative class, so get rid of the second half
        predictions = predictions[len(predictions) / 2:]
        res = numpy.zeros((X.shape[0], 1))
        for r, i in zip(predictions, range(X.shape[0])):
            res[i] = r
        return res
    
    def fit(self, X, y):
        '''
        Trains a new random forest classifier. 
        '''
        with _R_lock:
            with tempfile.NamedTemporaryFile() as tmpfile:

# Path: mimicus/tools/datasets.py
# def csv2numpy(csv_in):
# def numpy2csv(csv_out, X, y, file_names=None):
# def standardize_csv(csv_in, csv_out, standardizer=None):
#     TOTAL_ROWS = 0
#     X = numpy.array(numpy.zeros((TOTAL_ROWS, FeatureDescriptor.get_feature_count())), dtype=numpy.float64, order='C')


# Path: mimicus/classifiers/R_randomForest.py
import random
import string
import tempfile
import threading
import numpy
import rpy2.rinterface as rinterface
import rpy2.robjects as robjects
from sklearn.base import BaseEstimator
from mimicus.tools import datasets

    
    def decision_function(self, X):
        '''
        Classifies novel data points using a trained model. Returns a 
        list of predictions, one per data point, giving the probability 
        of the given data point belonging to the positive class. 
        '''
        assert self.model_trained, 'Must train a model before classification'
        with _R_lock:
            with tempfile.NamedTemporaryFile() as tmpfile:
                datasets.numpy2csv(tmpfile, X, numpy.zeros((X.shape[0],)))
                tmpfile.seek(0)
                # Read in the CSV file with the samples to be classified, omitting the second column (filename)
                robjects.r('{novel} <- read.csv("{csv}", header=TRUE, colClasses={cc})'.format(novel=self.noveldata_Rname, csv=tmpfile.name, cc=_r_colClasses))
                # Classify the new data points
                robjects.r('{pred} <- predict({model}, {novel}, type="prob")'.format(pred=self.predictions_Rname, model=self.model_Rname, novel=self.noveldata_Rname))
                predictions = list(robjects.r['{pred}'.format(pred=self.predictions_Rname)])
        # The first half of predictions is for the negative class, so get rid of the second half
        predictions = predictions[len(predictions) / 2:]
        res = numpy.zeros((X.shape[0], 1))
        for r, i in zip(predictions, range(X.shape[0])):
            res[i] = r
        return res
    
    def fit(self, X, y):
        '''
        Trains a new random forest classifier. 
        '''
        with _R_lock:
            with tempfile.NamedTemporaryFile() as tmpfile:

                datasets.numpy2csv(tmpfile, X, y)