def train_oneslice_model(areas_all,train_true):
    #train OneSliceModel using CNN_B result
    print(" --- train oneslice model :");
    oneslice_model = OneSliceModel();
    oneslice_model.fit(areas_all,train_true);
    oneslice_predict = oneslice_model.predict(areas_all);
    analysis.evaluate_pred(oneslice_predict, train_true);
    return oneslice_predict;
def train_sex_age_model(info, train_true):
    ##train sex_age model
    print(" ------ train sex age model :");
    sa_model = SexAgeModel();
    sa_model.fit(info,train_true);
    sa_predict = sa_model.predict(info);
    analysis.evaluate_pred(sa_predict, train_true);
    return sa_predict;
def train_ch4_model(ch4_data, train_true):
    #tencia's 4-ch model, implemented 02/27
    print(" ---- train ch4 model :");
    ch4_model = Ch4Model();
    ch4_model.fit(ch4_data, train_true);
    ch4_pred = ch4_model.predict(ch4_data);
    analysis.evaluate_pred(ch4_pred, train_true);
    return ch4_pred;
def train_sax_cnt_filter_model(areas_all, cont_all, train_true,cleaner=[]):
    print(" ---- train sax countour filter model :");
    cnt_result = analysis.get_preliminary_volume_cnt_filter(areas_all,cont_all,cleaner=cleaner);
    cnt_sax_model = SaxFilterModel();
    cnt_sax_model.fit(cnt_result,train_true);
    cnt_sax_predict = cnt_sax_model.predict(cnt_result);
    analysis.evaluate_pred(cnt_sax_predict, train_true);
    return cnt_sax_predict;
def train_sax_model(areas_all,train_true, version,cleaner=[]):
    print(" ---- train sax model :");
    sax_model = SaxModel(version=version);
    result = analysis.get_preliminary_volume(areas_all,cleaner=cleaner);
    sax_model.fit(result,train_true);
    sax_predict = sax_model.predict(result);
    analysis.evaluate_pred(sax_predict, train_true);
    return sax_predict;
def train_sax_cnt_model(areas_all, cont_all, train_true,version=2,cleaner=[]):
    #sax model method2, use contour compeleteness to filter result
    print(" ---- train sax countour model :");
    cnt_sax_model = SaxModel(version=version);
    cnt_result = analysis.get_preliminary_volume_cnt(areas_all, cont_all,cleaner=cleaner);
    cnt_sax_model.fit(cnt_result,train_true);
    cnt_sax_predict = cnt_sax_model.predict(cnt_result);
    analysis.evaluate_pred(cnt_sax_predict, train_true);
    return cnt_sax_predict;
def train_oneslice_model(areas_all, train_true):
    #train OneSliceModel using CNN_B result
    turn = dhl.turn()
    dhl.log_time_stamp(turn, 'train_pred.py', '-', 'train_oneslice_model',
                       dhl.START_FUNCTION, ' - ')
    oneslice_model = OneSliceModel()
    oneslice_model.fit(areas_all, train_true)
    oneslice_predict = oneslice_model.predict(areas_all)
    analysis.evaluate_pred(oneslice_predict, train_true)
    dhl.log_time_stamp(turn, 'train_pred.py', '-', 'train_oneslice_model',
                       dhl.END_FUNCTION, ' - ')
    return oneslice_predict
def train_sex_age_model(info, train_true):
    ##train sex_age model
    turn = dhl.turn()
    dhl.log_time_stamp(turn, 'train_pred.py', '-', 'train_sex_age_model',
                       dhl.START_FUNCTION, ' - ')
    sa_model = SexAgeModel()
    sa_model.fit(info, train_true)
    sa_predict = sa_model.predict(info)
    analysis.evaluate_pred(sa_predict, train_true)
    dhl.log_time_stamp(turn, 'train_pred.py', '-', 'train_sex_age_model',
                       dhl.END_FUNCTION, ' - ')
    return sa_predict
def train_sax_model(areas_all, train_true, version, cleaner=[]):
    turn = dhl.turn()
    dhl.log_time_stamp(turn, 'train_pred.py', '-', 'train_sax_model',
                       dhl.START_FUNCTION, ' - ')
    sax_model = SaxModel(version=version)
    result = analysis.get_preliminary_volume(areas_all, cleaner=cleaner)
    sax_model.fit(result, train_true)
    sax_predict = sax_model.predict(result)
    analysis.evaluate_pred(sax_predict, train_true)
    dhl.log_time_stamp(turn, 'train_pred.py', '-', 'train_sax_model',
                       dhl.END_FUNCTION, ' - ')
    return sax_predict
def train_ch4_model(ch4_data, train_true):
    #tencia's 4-ch model, implemented 02/27
    turn = dhl.turn()
    dhl.log_time_stamp(turn, 'train_pred.py', '-', 'train_ch4_model',
                       dhl.START_FUNCTION, ' - ')
    ch4_model = Ch4Model()
    ch4_model.fit(ch4_data, train_true)
    ch4_pred = ch4_model.predict(ch4_data)
    analysis.evaluate_pred(ch4_pred, train_true)
    dhl.log_time_stamp(turn, 'train_pred.py', '-', 'train_ch4_model',
                       dhl.END_FUNCTION, ' - ')
    return ch4_pred
def train_sax_cnt_filter_model(areas_all, cont_all, train_true, cleaner=[]):
    turn = dhl.turn()
    dhl.log_time_stamp(turn, 'train_pred.py', '-',
                       'train_sax_cnt_filter_model', dhl.START_FUNCTION, ' - ')
    cnt_result = analysis.get_preliminary_volume_cnt_filter(areas_all,
                                                            cont_all,
                                                            cleaner=cleaner)
    cnt_sax_model = SaxFilterModel()
    cnt_sax_model.fit(cnt_result, train_true)
    cnt_sax_predict = cnt_sax_model.predict(cnt_result)
    analysis.evaluate_pred(cnt_sax_predict, train_true)
    dhl.log_time_stamp(turn, 'train_pred.py', '-',
                       'train_sax_cnt_filter_model', dhl.END_FUNCTION, ' - ')
    return cnt_sax_predict
def train_sax_cnt_model(areas_all,
                        cont_all,
                        train_true,
                        version=2,
                        cleaner=[]):
    #sax model method2, use contour compeleteness to filter result
    turn = dhl.turn()
    dhl.log_time_stamp(turn, 'train_pred.py', '-', 'train_sax_cnt_model',
                       dhl.START_FUNCTION, ' - ')
    cnt_sax_model = SaxModel(version=version)
    cnt_result = analysis.get_preliminary_volume_cnt(areas_all,
                                                     cont_all,
                                                     cleaner=cleaner)
    cnt_sax_model.fit(cnt_result, train_true)
    cnt_sax_predict = cnt_sax_model.predict(cnt_result)
    analysis.evaluate_pred(cnt_sax_predict, train_true)
    dhl.log_time_stamp(turn, 'train_pred.py', '-', 'train_sax_cnt_model',
                       dhl.END_FUNCTION, ' - ')
    return cnt_sax_predict
    Ntrain = train_true.shape[0];#500, becomes 700 when validate data released
    print("number of train cases is {}".format(Ntrain));
    
    filter_ll = -2000;
    #### train models,
    ########### default models
    sa_predict = train_sex_age_model(info, train_true);
    ch4_predict = train_ch4_model(ch4_data, train_true);
    pick = [0,1];
    qi_best,qi_best_cont = analysis.take_best_contour([qi_areas[i] for i in pick],[qi_cnts[i] for i in pick],method=1, filter_ll = filter_ll);
    oneslice_pred = train_oneslice_model(qi_best,train_true);
    # fit the fall back model, a combination of oneslice_model and 4-ch model, 
    # if it still fails use the sex-age model
    # 0.6 * oneslice_predict + 0.4 * ch4_predict; (use fixed 0.6, 0.4)
    default_pred = build_default_model(oneslice_pred, ch4_predict, sa_predict);
    analysis.evaluate_pred(default_pred, train_true);

    ########## sax based CNN models

    pick = [0,1];
    qi_best,qi_best_cont = analysis.take_best_contour([qi_areas[i] for i in pick],[qi_cnts[i] for i in pick],method=1, filter_ll=filter_ll);
    qi_sax_pred = train_sax_model(qi_best,train_true, version = 2, cleaner = cleaner);
    qi_sax_cnt_pred = train_sax_cnt_model(qi_best, qi_best_cont, train_true,version=2, cleaner = cleaner);
    qi_sax_filter_pred = train_sax_cnt_filter_model(qi_best,qi_best_cont,train_true, cleaner = cleaner);

    # fit the combined model based on the cnn-sax models.
    # when it fails, fall to the previously fitted fall back model
    print(" --------- average models --");
    #Submit V9
    all_models = [qi_sax_pred, qi_sax_cnt_pred, qi_sax_filter_pred];
    ave_model = AverageModel(ll = 1.0e-4);