import cPickle as pickle
import csv
import glob
import os
import time
import numpy as np
import theano
import theano.tensor as T
import data_loader
import postprocess
import utils
import paths

METADATAS_LOCATION = 'metadatas.pkl'
RELOAD_METADATAS = False
DO_XVAL = True

PREDICTIONS_PATH = paths.INTERMEDIATE_PREDICTIONS_PATH
SUBMISSION_PATH = paths.SUBMISSION_PATH

PRINT_EVERY = 1000  # Print itermediate results during training
LEARNING_RATE = 10  # Learning rate of gradient descend
NR_EPOCHS = 100
C = 0.0001  # L1 regularisation parameters

SELECT_TOP = 10  # Nr of models to select


def _get_all_prediction_files():
    all_files = glob.glob(PREDICTIONS_PATH + '*')
    res = []
    for f in all_files:
        if f in MODELS_TO_USE:
            res.append(f)
    return res


def _construct_all_patient_ids():
    all_patient_ids = {key: [] for key in data_loader.patient_folders}
    for pid in data_loader.id_to_index_map:
        pset, pindex = data_loader.id_to_index_map[pid]
        all_patient_ids[pset].append(pid)
    for arr in all_patient_ids.values():
        arr.sort()
    return all_patient_ids


all_patient_ids = _construct_all_patient_ids()


def load_prediction_file(path):
    try:
        return np.load(path)
    except:
        return False


def _is_empty_prediction(patient_prediction):
    return (
        len(patient_prediction['systole']) == 0
        and len(patient_prediction['diastole']) == 0
        and 'patient' in patient_prediction)


not_predicted_sets = {}


def _register_model(pats_not_predicted):
    not_predicted = tuple(sorted(pats_not_predicted['validation'] + pats_not_predicted['test']))
    not_predicted_sets[not_predicted] = not_predicted_sets.get(not_predicted, 0) + 1


def _compute_pats_not_predicted(metadata):
    pats_not_predicted = {set: [] for set in data_loader.patient_folders}
    pats_predicted = {set: [] for set in data_loader.patient_folders}
    for prediction in metadata['predictions']:
        pid = prediction['patient']
        pset = data_loader.id_to_index_map[pid][0]
        if _is_empty_prediction(prediction):