def computePairWiseConfusions_old(objects_oi, OUTPATH=None):
    """ For a set of objects and features, run classifiers,
        and output trial structures for all 2x2 tasks.
    """
    
    if type(objects_oi) is dict:
        objs_oi = objects_oi['objs']
        tasks_oi = objects_oi['tasks']
    else:
        tasks_oi = np.array(objects_oi)
        objs_oi = np.array(objects_oi)

    all_features, all_metas = obj.getAllFeatures(objs_oi)
    # features_oi = ['Caffe_fc6', 'Caffe_fc7', 'Caffe', 'VGG_fc6', 'VGG_fc7', 'VGG']
    features_oi = ['Alexnet_fc6', 'Alexnet_fc7', 'Alexnet']
    
    subsample = None
    nsamples_noisemodel = 2
    result = {}

    for feat in features_oi:
        if feat not in all_features.key():
            continue
        features = all_features[feat]
        meta = all_metas[feat]
        tasks = getBinaryTasks(meta, tasks_oi)
        trials, trials_io = [], []
        if 'rep' in feat:
            noise_model = 'rep' #'rep'
        else:
            noise_model = None

        print 'Running machine_objectome : ' + str(feat) + ': ' + str(features.shape) + '\t' + str(noise_model)
        for isample in range(nsamples_noisemodel):
            features_sample = sampleFeatures(features, noise_model, subsample)
            for task in tasks:
                p_, p_s_, t_, t_s_ = testFeatures_base(features, meta, task, objs_oi)
                trials.extend(t_)
	
        trials = format_trials_var(trials)
        if OUTPATH != None:
            if not os.path.exists(OUTPATH):
                os.makedirs(OUTPATH)
            if subsample != None:
                feat = feat + '_' + str(subsample)
        print 'Saved to ' + OUTPATH
        save_trials(trials, objs_oi, OUTPATH + feat + 'full_var_bg.mat')
        result[feat] = trials

    return result
def computePairWiseConfusions(objects_oi, OUTPATH=None, IMGPATH=None):
    """ For a set of objects and features, run classifiers,
        and output trial structures for all 2x2 tasks.
    """
    features_oi = ['PXLn', 'V1', 'HMAX', 'Alexnet_fc6', 'Alexnet_fc7', 'Alexnet', 'VGG_fc6', 'VGG_fc7', 'VGG', 'Googlenet', 'Resnet']
    all_features, all_metas = obj.getAllFeatures(objects_oi, IMGPATH)
    result = testFeatures(all_features, all_metas, features_oi, objects_oi)
    
    for feat in features_oi:
        if feat not in all_features.keys():
            continue
        if OUTPATH != None:
            if not os.path.exists(OUTPATH):
                os.makedirs(OUTPATH)
        print 'Save to ' + OUTPATH
        save_trials(result[feat], result['objs_oi'], OUTPATH + feat + 'full_var_bg.mat')
    return 
def computePairWiseConfusions_base(objects_oi, OUTPATH=None, silence_mode=0):
    """ For a set of objects, compute pixel and v1 features, run classifiers,
        and output trial structures for all 2x2 tasks.
    """
    
    if type(objects_oi) is dict:
        objs_oi = objects_oi['objs']
        tasks_oi = objects_oi['tasks']
    else:
        tasks_oi = objects_oi
        objs_oi = objects_oi

    run_silencing_exp = silence_mode > 0

    print 'Loading features...'
    if run_silencing_exp:
        all_features = pk.load(open('quickload_feature_data/features.pkl', 'r'))
        all_metas = pk.load(open('quickload_feature_data/metas.pkl', 'r'))
    else:
        all_features, all_metas = obj.getAllFeatures(objs_oi)
        features_oi = ['IT', 'V4']

    result = {}
    
    for feat in features_oi:
        print 'Running machine_objectome : \n' + str(objs_oi) + '\n ' + str(feat) + '\n\n'

        features = all_features[feat]
        meta = all_metas[feat]
        tasks = getBinaryTasks(meta, tasks_oi)
        trials, trials_io = [], []
        trials_s = {}

        if run_silencing_exp:
            features_s = silenceFeature(features, silence_mode)
        else:
            features_s = None
        
        for task in tasks:
            p_, p_s_, t_, t_io_, t_s_ = getPerformanceFromFeatures_base(features, meta, task, objs_oi, features_s, nsplits=50)
            trials.extend(t_)
            trials_io.extend(t_io_)

            if run_silencing_exp:
                for fs in features_s:
                    if not (fs in t_s_):
                        trials_s[fs] = []
                    trials_s[fs].extend(t_s_[fs])

        trials = format_trials_var(trials)
        trials_io = format_trials_var(trials_io)
        if run_silencing_exp:
            trials_s = format_trials_var(trials_s)
        
        if OUTPATH != None:
            ts = time.time()
            st = datetime.datetime.fromtimestamp(ts).strftime('%M.%H.%d%m')
            print 'Saving to ' + OUTPATH
            if not os.path.exists(OUTPATH):
                os.makedirs(OUTPATH)

            if run_silencing_exp:
                save_trials(trials, objs_oi, OUTPATH + feat + st + '.mat')
                save_trials(trials_s, objs_oi, OUTPATH + feat + st + 'sil2.mat')
            else:
                save_trials(trials, objs_oi, OUTPATH + feat + 'full_var_bg.mat')
                save_trials(trials_io, objs_oi, OUTPATH + feat + 'full_var_bg_ideal_obs.mat')

        result[feat] = trials

    return result