def dp_accuracy():
    logging.basicConfig(level=10) # 5 for more stuff
    CL = CL3
    animal = 66
    session = 60 
    
    room =[[-55,55],[-55,55]]
    bin_size = 5
    K =  50      # Segment length used to calculate firing rates
    CL.delt_t = K*.02
    cluster_profile = 0
    label = 'Task'
    
    cl_prof_name, good_clusters = get_good_clusters(cluster_profile)
    try:
        adat = try_cache('One big data structure')
        correct_dp = adat[CL.name][animal][session][cl_prof_name][bin_size][label][K]
        logging.info('Got data from Cache.cache.')
    except:
        logging.info('Calculating classifications...')
        CL.delt_t=K
        
        fn, trigger_tm = load_mux(animal, session)
        vl = load_vl(animal,fn)
        cls = {tetrode:load_cl(animal,fn,tetrode) for tetrode in range(1,17)}
        label_l = vl['Task']
        
        t_cells = count_cells(vl,cls,trigger_tm,good_clusters)
    
        logging.info('About to generate population vector.')
        #X, Y = gpv(vl, t_cells, label_l, K)
        s=time()
        X, Y = gpv(vl, t_cells, label_l, K, bin_size, room)
        logging.info('%i population vectors generated in %.3f.',X.shape[0],time()-s)
        Y = Y.reshape([-1])
        
        correct_dp = check_classifier(range(X.shape[0]),range(X.shape[0]), 
                                      X, Y, CL, room, bin_size) 

    # Accuracy meter
    plt.figure()
    plt.hist(correct_dp,normed=True)
    plt.xlabel('Accuracy')
    tt = '%s,  K: %i, ClPr: %s, Label:%s'%(CL.name,K,cl_prof_name,
                                                   label)
    plt.title(tt)

    msg = []
    for i in [1,50,75,90,95,99]:
        perc = 1.0*np.sum(correct_dp > i/100.0)/len(correct_dp)*100.0
        msg.append('>%i%%:  %.1f%%'%(i,perc))
    msg = '\n'.join(msg)
    plt.xlim([0,1])
    xcoord = plt.xlim()[0] + (plt.xlim()[1]-plt.xlim()[0])*.1
    ycoord = plt.ylim()[0] + (plt.ylim()[1]-plt.ylim()[0])*.5
    plt.text(xcoord,ycoord,msg)
    plt.show()
    
def run(Folds):
    # Toggle-able parameters
    #CLs = [CL2,CL6,CL5]
    #CLs = [CL6, CL7]
    CLs = [CL10]
    Ks = np.arange(10, 200,
                   20)  # Segment length used to calculate firing rates

    # Sort of toggle-able parameters
    #animal_sess_combs = [(66,60),(70,8),(70,10),(66,61)]
    animal_sess_combs = [(66, 60)]
    #good_trials = try_cache('Good trials')
    #animal_sess_combs = [(animal,session) for animal in range(65,74)
    #                     for session in good_trials[animal]]
    bin_sizes = [5]
    label = 'Task'
    exceptions = []
    cl_profs = [0]

    # Not really toggle-able parameters
    room = [[-55, 55], [-55, 55]]

    cache = try_cache('One big data structure for %i folds' % (Folds, ))
    adat = ({} if cache is None else cache)

    for animal, session in animal_sess_combs:
        fn, trigger_tm = load_mux(animal, session)
        vl = load_vl(animal, fn)
        cls = {
            tetrode: load_cl(animal, fn, tetrode)
            for tetrode in range(1, 17)
        }

        if label == 'Task': label_l = vl['Task']
        else: raise Exception('Not implemented yet.')

        for clust_prof in cl_profs:
            cl_prof_name, good_clusters = get_good_clusters(clust_prof)
            t_cells = count_cells(vl, cls, trigger_tm, good_clusters)

            for bin_size, K in product(bin_sizes, Ks):
                cached = np.zeros(len(CLs))
                for CL in CLs:
                    i = CLs.index(CL)
                    try:
                        raise Exception
                        adat[CL.name][animal][session][cl_prof_name][bin_size][
                            label][K]
                        cached[i] = True
                    except:
                        cached[i] = False

                if np.sum(cached) == len(CLs):
                    print 'Everything already cached'
                    continue  # Everything is already cached!

                logging.info('About to generate population vector.')
                X, Y = gpv(vl, t_cells, label_l, K, bin_size, room)

                # The main data stricture
                dps = {CL: [] for CL in CLs if CL not in cached}

                if Folds > 0:
                    kf = cross_validation.KFold(len(Y),
                                                n_folds=Folds,
                                                shuffle=True)
                else:
                    kf = [(range(len(Y)), range(len(Y)))]
                for train_index, test_index in kf:
                    logging.warning('Training/testing: %i/%i',
                                    len(train_index), len(test_index))
                    for CL in CLs:
                        if cached[CLs.index(CL)]: continue
                        logging.warning('%s, %i seg, (%i, %i)', CL.name, K,
                                        animal, session)
                        if (CL, clust_prof) in exceptions: continue
                        CL.delt_t = K
                        correct_dp = check_classifier(train_index, test_index,
                                                      X, Y, CL, room, bin_size)

                        dps[CL].extend(correct_dp.tolist())
                for CL in CLs:
                    if cached[CLs.index(CL)]: continue
                    to_add = np.array(dps[CL]).reshape([-1])
                    add(adat, CL.name, animal, session, cl_prof_name, bin_size,
                        label, K, to_add)

    store_in_cache('One big data structure for %i folds' % (Folds, ), adat)
def run(Folds):
    # Toggle-able parameters
    #CLs = [CL2,CL6,CL5]
    #CLs = [CL6, CL7]
    CLs = [CL10]
    Ks = np.arange(10,200,20) # Segment length used to calculate firing rates
    

    # Sort of toggle-able parameters
    #animal_sess_combs = [(66,60),(70,8),(70,10),(66,61)]
    animal_sess_combs = [(66,60)]
    #good_trials = try_cache('Good trials')
    #animal_sess_combs = [(animal,session) for animal in range(65,74) 
    #                     for session in good_trials[animal]]
    bin_sizes = [5]
    label = 'Task'
    exceptions = []
    cl_profs = [0]
    
    # Not really toggle-able parameters
    room = [[-55,55],[-55,55]]
    
    
    
    cache = try_cache('One big data structure for %i folds'%(Folds,))
    adat = ({} if cache is None else cache)

    for animal, session in animal_sess_combs:
        fn, trigger_tm = load_mux(animal, session)
        vl = load_vl(animal,fn)
        cls = {tetrode:load_cl(animal,fn,tetrode) for tetrode in range(1,17)}
        
        if label == 'Task': label_l = vl['Task']
        else: raise Exception('Not implemented yet.')
        
        for clust_prof in cl_profs:
            cl_prof_name, good_clusters = get_good_clusters(clust_prof)
            t_cells = count_cells(vl,cls,trigger_tm,good_clusters)
            
            for bin_size, K in product(bin_sizes,Ks):
                cached = np.zeros(len(CLs))
                for CL in CLs:
                    i = CLs.index(CL)
                    try:
                        raise Exception
                        adat[CL.name][animal][session][cl_prof_name][bin_size][label][K]
                        cached[i] = True
                    except:
                        cached[i] = False
                
                if np.sum(cached) == len(CLs): 
                    print 'Everything already cached'
                    continue # Everything is already cached!
                
                
                logging.info('About to generate population vector.')
                X, Y = gpv(vl, t_cells, label_l, K, bin_size, room)
                
                
                # The main data stricture
                dps = {CL:[] for CL in CLs if CL not in cached}
                
                if Folds >0: kf = cross_validation.KFold(len(Y),n_folds=Folds,shuffle=True)
                else: kf = [(range(len(Y)),range(len(Y)))]
                for train_index, test_index in kf:
                    logging.warning('Training/testing: %i/%i',len(train_index),len(test_index))
                    for CL in CLs:
                        if cached[CLs.index(CL)]: continue
                        logging.warning('%s, %i seg, (%i, %i)',CL.name, K, animal, session)
                        if (CL,clust_prof) in exceptions: continue
                        CL.delt_t = K
                        correct_dp = check_classifier(train_index,test_index,X,Y,CL, room, bin_size)
        
                        dps[CL].extend(correct_dp.tolist())
                for CL in CLs:
                    if cached[CLs.index(CL)]: continue
                    to_add = np.array(dps[CL]).reshape([-1])
                    add(adat, CL.name, animal, session, cl_prof_name, bin_size, label, K, to_add)

    store_in_cache('One big data structure for %i folds'%(Folds,),adat)