Пример #1
0
    def test_corrmat_varcol(self):
        '''Test for corrmat (var=col)'''

        x = np.random.rand(100, 10)
        y = np.random.rand(100, 10)

        exp_output = np.corrcoef(x, y, rowvar=0)[:x.shape[1], x.shape[1]:]

        test_output = bdst.corrmat(x, y, var='col')

        np.testing.assert_array_almost_equal(test_output, exp_output)
Пример #2
0
    def test_corrmat_default(self):
        '''Test for corrmat (default, var=row)'''

        x = np.random.rand(100, 10)
        y = np.random.rand(100, 10)

        exp_output = np.corrcoef(x, y)[:x.shape[0], x.shape[0]:]

        test_output = bdst.corrmat(x, y)

        np.testing.assert_array_almost_equal(test_output, exp_output)
Пример #3
0
def calculaAcuracia(pred_y,
                    lbl_test,
                    protoTYPE=1,
                    tipo=1,
                    givenPrototypes=None,
                    givenClasses=None,
                    CMFile=None,
                    showMessages=0):
    classes = loadList('%s/classes.txt' % img_dir)
    if protoTYPE == 1 or protoTYPE == 2 or protoTYPE == 12:
        if tipo == 1:
            savepath = '%s/prototypes_200_4096.pkl' % prototype_dir
        else:
            savepath = '%s/prototypes_200_25088.pkl' % prototype_dir

        with open(savepath, 'rb') as f:
            prototipos = pickle.load(f)

        savepath = '%s/counts.pkl' % prototype_dir

        with open(savepath, 'rb') as f:
            counts = pickle.load(f)

    if protoTYPE == 12:
        classes = classes[:150]
        prototipos = prototipos[:150, :]
        counts = counts[:150]

    if protoTYPE == 2:
        classes = classes[150:]
        prototipos = prototipos[150:, :]
        counts = counts[150:]

    if protoTYPE == 3:
        if tipo == 1:
            classes = loadList('%s/image_prototypes_4096.txt' % prototype_dir)
            savepath = '%s/image_prototypes_4096.pkl' % prototype_dir
        else:
            classes = loadList('%s/image_prototypes_25088.txt' % prototype_dir)
            savepath = '%s/image_prototypes_25088.pkl' % prototype_dir

        with open(savepath, 'rb') as f:
            prototipos = pickle.load(f)

        counts = np.zeros((prototipos.shape[0], ))
        counts += 1

    if protoTYPE == 4:
        classes = givenClasses
        prototipos = givenPrototypes
        counts = np.zeros((prototipos.shape[0], ))
        counts += 1

    if protoTYPE == 5:
        classes = loadList('%s/huge/hugeList.txt' % prototype_dir)
        mean_classes = loadList('%s/classes.txt' % prototype_dir)
        if tipo == 1:
            hugeproto = '%s/huge/prototypes_4096.pkl' % prototype_dir
            meanproto = '%s/prototypes_200_4096.pkl' % prototype_dir
        else:
            hugeproto = '%s/huge/prototypes_25088.pkl' % prototype_dir
            meanproto = '%s/prototypes_200_25088.pkl' % prototype_dir

        with open(hugeproto, 'rb') as f:
            prototipos = pickle.load(f)

        with open(meanproto, 'rb') as f:
            mean_prototipos = pickle.load(f)

        hugecounts = '%s/huge/counts.pkl' % prototype_dir

        with open(hugecounts, 'rb') as f:
            counts = pickle.load(f)

    if protoTYPE == 8:
        classes = classes[:150]
        prototipos = None
        counts = None

    top1_acc = 0
    top5_acc = 0
    top10_acc = 0
    k_score_acc = 0

    testLabels = []
    for lbl in lbl_test:
        className = lbl.split('_')[0]
        if className not in testLabels:
            testLabels.append(className)

    print('Numero de classes', len(testLabels))
    if len(pred_y[0].shape) == 1:
        for i in range(len(pred_y)):
            pred_y[i] = torch.unsqueeze(pred_y[i], dim=0)

    predictions = torch.cat(pred_y)
    predictions = predictions.cpu().data.numpy()

    if protoTYPE == 5:
        mean_classes = mean_classes[150:]
        mean_prototipos = mean_prototipos[150:, :]

        prototipos = np.vstack([mean_prototipos, prototipos])
        classes = mean_classes + classes

    corrMatrix = corrmat(predictions, prototipos)
    top1_acc, top5_acc, top10_acc, k_score_acc = resultadoMatriz(corrMatrix,
                                                                 classes,
                                                                 lbl_test,
                                                                 testLabels,
                                                                 CMFile=CMFile)

    top1_acc = 100 * top1_acc / len(lbl_test)
    top5_acc = 100 * top5_acc / len(lbl_test)
    top10_acc = 100 * top10_acc / len(lbl_test)
    k_score_acc = 100 * k_score_acc / len(lbl_test)

    print('Top 1    : %3.2f%c' % (top1_acc, '%'))
    print('Top 5    : %3.2f%c' % (top5_acc, '%'))
    print('Top 10   : %3.2f%c' % (top10_acc, '%'))
    print('K_score  : %3.2f%c' % (k_score_acc, '%'))

    return top1_acc, top5_acc, top10_acc, k_score_acc
Пример #4
0
def category_identification(inputs):
    '''Runs category identification'''

    sbj, roi, feat = inputs

    res = results_featpred.query(
        "subject == @sbj and roi == @roi and feature ==@feat")

    ## Preparing data
    feature = features.get('value', feat)
    feature_type = features.get('type')
    feature_label = features.get('image_label')
    feature_catlabel = features.get('category_label')

    ## Category identification -----------------------------------------

    # FIXME: is there better way to extract numpy array?
    pred_percept = res['predict_percept'].as_matrix()[0]
    pred_imagery = res['predict_imagery'].as_matrix()[0]

    cat_percept = res['category_test_percept'].as_matrix()[0]
    cat_imagery = res['category_test_percept'].as_matrix()[0]

    ## Get category features
    ind_cattest = feature_type == 3
    feat_cattest = feature[ind_cattest, :]
    featlb_cattest = feature_catlabel[ind_cattest]

    test_feat_cat_percept = bdpy.get_refdata(feat_cattest, featlb_cattest,
                                             cat_percept)
    test_feat_cat_imagery = bdpy.get_refdata(feat_cattest, featlb_cattest,
                                             cat_imagery)

    feat_cat_test = test_feat_cat_percept
    feat_cat_other = feature[feature_type == 4, :]  # Unseen categories

    labels = range(feat_cat_test.shape[0])
    candidate = numpy.vstack([feat_cat_test, feat_cat_other])

    ## Seen categories
    simmat = corrmat(pred_percept, candidate)
    correct_rate_percept = get_pwident_correctrate(simmat, labels)

    ## Imagined categories
    simmat = corrmat(pred_imagery, candidate)
    correct_rate_imagery = get_pwident_correctrate(simmat, labels)

    ## Calculate average correct rate
    correct_rate_percept_ave = numpy.mean(correct_rate_percept)
    correct_rate_imagery_ave = numpy.mean(correct_rate_imagery)

    ## Print results
    res_str = "%s-%s-%s\n" % (sbj, roi, feat)
    res_str += "Correct rate (seen)\t: %.2f %%\n" % (correct_rate_percept_ave *
                                                     100)
    res_str += "Correct rate (imagined)\t: %.2f %%" % (
        correct_rate_imagery_ave * 100)
    print res_str

    return {
        'subject': sbj,
        'roi': roi,
        'feature': feat,
        'correct_rate_percept': correct_rate_percept,
        'correct_rate_imagery': correct_rate_imagery,
        'correct_rate_percept_ave': correct_rate_percept_ave,
        'correct_rate_imagery_ave': correct_rate_imagery_ave
    }
Пример #5
0
def main():
    if pca:
        results_file = dir_path+'/results/feature-decoding-pca-merge/results.pkl'
        output_file = dir_path+'/results/feature-decoding-pca-final/results.pkl'
    else:
        results_file = dir_path+'/results/feature-decoding-merge/results.pkl'
        output_file = dir_path+'/results/feature-decoding-final/results.pkl'

    image_feature_file = config.image_feature_file

    # Load results -----------------------------------------------------
    print('Loading %s' % results_file)
    with open(results_file, 'rb') as f:
        results = pickle.load(f)

    data_feature = bdpy.BData(image_feature_file)

    # Category identification ------------------------------------------
    print('Running pair-wise category identification')

    feature_list = results['feature']
    pred_percept = results['predicted_feature_averaged_percept']
    pred_imagery = results['predicted_feature_averaged_imagery']
    cat_label_percept = results['category_label_set_percept']
    cat_label_imagery = results['category_label_set_imagery']
    cat_feature_percept = results['category_feature_averaged_percept']
    cat_feature_imagery = results['category_feature_averaged_imagery']

    ind_cat_other = (data_feature.select('FeatureType') == 4).flatten()

    pwident_cr_pt = []  # Prop correct in pair-wise identification (perception)
    pwident_cr_im = []  # Prop correct in pair-wise identification (imagery)


    for f, fpt, fim, pred_pt, pred_im in zip(feature_list, cat_feature_percept, cat_feature_imagery,
                                             pred_percept, pred_imagery):

        y = data_feature.select(f)
        if pca:
            print('Shape of y before PCA:', y.shape)
            ipca = IncrementalPCA(n_components=20, batch_size=20)
            ipca.fit(y)
            y=ipca.transform(y)
            print('Shape of y after PCA:', y.shape)
            print(y.shape)
        else:
            y = y[:, :100]#take 100 features for time constraint

        feat_other = y[ind_cat_other, :]

        n_unit = fpt.shape[1]
        feat_other = feat_other[:, :n_unit]

        feat_candidate_pt = np.vstack([fpt, feat_other])
        feat_candidate_im = np.vstack([fim, feat_other])

        simmat_pt = corrmat(pred_pt, feat_candidate_pt)
        simmat_im = corrmat(pred_im, feat_candidate_im)

        cr_pt = get_pwident_correctrate(simmat_pt)
        cr_im = get_pwident_correctrate(simmat_im)

        pwident_cr_pt.append(np.mean(cr_pt))
        pwident_cr_im.append(np.mean(cr_im))

    results['catident_correct_rate_percept'] = pwident_cr_pt
    results['catident_correct_rate_imagery'] = pwident_cr_im

    # Save the merged dataframe ----------------------------------------
    with open(output_file, 'wb') as f:
        pickle.dump(results, f)
    print('Saved %s' % output_file)

    # Show results -----------------------------------------------------
    tb_pt = pd.pivot_table(results, index=['roi'], columns=['feature'],
                           values=['catident_correct_rate_percept'], aggfunc=np.mean)
    tb_im = pd.pivot_table(results, index=['roi'], columns=['feature'],
                           values=['catident_correct_rate_imagery'], aggfunc=np.mean)

    print(tb_pt)
    print(tb_im)