Ejemplo n.º 1
0
class LocfitEstimator(BaseEstimator):
    """
    An R locfit estimator that pretends to work like a sklearn estimator.
    """
    def __init__(self):
        rfile = "/net/noble/vol2/home/katecook/proj/2015HiC-differential/src/call_locfit.R"
        rfh = open(rfile, 'r')
        string = ''.join(rfh.readlines())
        self.locfit = SignatureTranslatedAnonymousPackage(string, "locfit")

    def fit(self, X, y):
        X_robj = robjects.FloatVector(X)
        y_robj = robjects.FloatVector(y)
        self.fit_ = self.locfit.do_fit(X_robj, y_robj)

    def predict(self, X):
        check_is_fitted(self, "fit_")
        shape = X.shape
        flat = X.flatten().T
        #logging.debug(flat.shape)
        #logging.debug(flat)
        X_robj = robjects.FloatVector(flat)
        #logging.debug(X_robj)
        y_robj = self.locfit.safepredict(self.fit_, X_robj)
        y_ = np.array(y_robj).T.reshape(shape)
        logging.debug(y_)
        np.savetxt("y.txt", y_, delimiter='\t')
        return y_

    def score(self, X, y):
        yest = self.safepredict(X)
        return r2_score(y, yest, sample_weight=None)