from numpy.core.fromnumeric import transpose
from sklearn import linear_model
from scipy.special import logit
from scipy import stats
from copy import deepcopy
from numpy import random, concatenate, quantile, matmul, transpose
import logging


class singleRegModel():
    """
    data struct for running a single regression test
    """
    def __init__(self, regressor):
        self.regressor = regressor
        self.mmodel = None
        # params
        self.quantile_limit_ = 0.95


    def train_binary(self, x_train, y_train):
        self.mmodel = deepcopy(self.regressor)
        self.mmodel.fit(x_train, y_train)


    def train_quant(self, init_x, follow_x, init_y, follow_iter):
        self.train_binary(init_x, init_y)
        if follow_x is None:
            logging.warning("No samples have missing MAF - no follow up training")
            return

        for i in range(follow_iter):
            init_preds = self.mmodel.predict(init_x)
            upper_limit = quantile(init_preds, self.quantile_limit_)
            follow_y = self.mmodel.predict(follow_x)
            follow_y[follow_y > upper_limit] = upper_limit

            x_merge = concatenate((init_x, follow_x))
            y_merge = concatenate((init_y, follow_y))

            self.mmodel = deepcopy(self.regressor)
            self.mmodel.fit(x_merge, y_merge)


    def predict_prob(self, input_x):
        preds = matmul(input_x, transpose(self.mmodel.coef_)) + self.mmodel.intercept_
        probs = preds[:,0]
        return probs


    def predict_quant(self, input_x):
        #preds = matmul(input_x, transpose(self.mmodel.coef_)) + self.mmodel.intercept_
        #print(preds, self.mmodel.predict(input_x))
        #probs = preds[:,0]
        #return probs
        return self.mmodel.predict(input_x)

    
class predOutcome():
    """
    store output for prediction
    """
    def __init__(self):
        self.true_y = None
        self.test_y = None
        self.train_ys = [] # with CV training can have multiple results
        self.cancer_status = None # binary: 0 for normal and 1 for cance
[((22, 22, 22, 46), 'copy.deepcopy', 'deepcopy', ({(22, 31, 22, 45): 'self.regressor'}, {}), '(self.regressor)', False, 'from copy import deepcopy\n'), ((29, 12, 29, 82), 'logging.warning', 'logging.warning', ({(29, 28, 29, 81): '"""No samples have missing MAF - no follow up training"""'}, {}), "('No samples have missing MAF - no follow up training')", False, 'import logging\n'), ((34, 26, 34, 68), 'numpy.quantile', 'quantile', ({(34, 35, 34, 45): 'init_preds', (34, 47, 34, 67): 'self.quantile_limit_'}, {}), '(init_preds, self.quantile_limit_)', False, 'from numpy import random, concatenate, quantile, matmul, transpose\n'), ((38, 22, 38, 53), 'numpy.concatenate', 'concatenate', ({(38, 34, 38, 52): '(init_x, follow_x)'}, {}), '((init_x, follow_x))', False, 'from numpy import random, concatenate, quantile, matmul, transpose\n'), ((39, 22, 39, 53), 'numpy.concatenate', 'concatenate', ({(39, 34, 39, 52): '(init_y, follow_y)'}, {}), '((init_y, follow_y))', False, 'from numpy import random, concatenate, quantile, matmul, transpose\n'), ((41, 26, 41, 50), 'copy.deepcopy', 'deepcopy', ({(41, 35, 41, 49): 'self.regressor'}, {}), '(self.regressor)', False, 'from copy import deepcopy\n'), ((46, 32, 46, 60), 'numpy.transpose', 'transpose', ({(46, 42, 46, 59): 'self.mmodel.coef_'}, {}), '(self.mmodel.coef_)', False, 'from numpy import random, concatenate, quantile, matmul, transpose\n')]