def Do13TestClassifier_ShuffledLabels_permutation(subject=['Subj01'],
                                                  epoch=['study'],
                                                  NOF_PERMUTATIONS=5,
                                                  size=[20, 20]):

    for j, sub in enumerate(subject):
        for e in epoch:
            print(sub)

            cdata = loadmat(
                f'./Visual/{sub}/6-ClassificationData/{sub}_test_data_visual',
                chars_as_strings=True,
                simplify_cells=True)[
                    f'{sub}_test_data_visual']  #Skip first key

            directory = f'./data/Visual/{sub}/7-ClassifierTraining/'
            #Create new dir if not exist
            os.makedirs(os.path.dirname(directory), exist_ok=True)

            with open(directory + f'{sub}_{e}_crossvalclass.pkl',
                      'rb') as file:
                crossdata = load(file)[
                    f'{sub}_{e}_crossvalclass']  #give imput data

            #Create a permutation of already existing data
            for permutation in range(
                    NOF_PERMUTATIONS):  #Number of Permutations
                R = np.random.permutation(len(cdata['category_name']))
                per = {
                    'category_name': cdata['category_name'][R],
                    'category': cdata['category'][R],
                    'feature_name': cdata['feature_name'],
                    'trial_info': cdata['trialinfo'],
                    'feature': cdata['feature'],
                    'numclassifiers': cdata['numclassifiers']
                }

                #Apply calssifier into the shuffled category trials
                cfg = {
                    'fold': 10,
                    'classifiernumber': 20,
                    'timebinsnumber': 20,
                    'category_predict': ['Face', 'Landmark', 'Object'],
                    'trials': 'all',
                    'category_model': ['Face', 'Landmark', 'Object']
                }
                per_predtest = mvpa_applycrossvalclassifier(
                    cfg, crossdata, per)  #PER%d_predtest

                #Construct and save the Permutation Map
                sub_P = np.zeros((size[0], size[1]))
                for col in range(size[0]):
                    #Easier to read, and doesn't have to do three seperate lookups, saving some performance
                    tbCol = per_predtest['timebin'][col]['confmatfinal']
                    for row in range(size[1]):
                        confRow = tbCol[row]

                        trials = np.sum(confRow, axis=1)
                        sub_P[row, col] = ((confRow[0, 0] / trials[0] * 100) +
                                           (confRow[1, 1] / trials[1] * 100) +
                                           (confRow[2, 2] / trials[2] * 100))

                directory = f'./data/Visual/{sub}/8-ClassifierTesting/PermutationStudyDecodeTestVisual/'
                os.makedirs(os.path.dirname(directory), exist_ok=True)

                with open(directory + f'{sub}_P{permutation+1}.pkl',
                          'wb') as file:
                    dump({f'{sub}_P{permutation+1}': sub_P}, file)
Exemplo n.º 2
0
def analysis(job, sliceno, slices):
    #Divide the subject list so that each sliceno processes their own subjects.
    sublist = array_split(options.subjects, slices)[sliceno]

    size = options.size
    NOF_PERMUTATIONS = options.NOF_PERMUTATIONS
    jobbig = jobs.do11Job

    for epo in options.epoch:
        for sub in sublist:
            print(sub)

            fname = job.input_filename(
                f'./{sub}/6-ClassificationData/{sub}_test_data_visual.mat')
            cdata = loadmat(fname, chars_as_strings=True,
                            simplify_cells=True)[f'{sub}_test_data_visual']

            directory = f'./data/Visual/{sub}/7-ClassifierTraining/'  #directory of the data

            crossdata = jobbig.load(
                directory + f'{sub}_{epo}_crossvalclass.pickle')[
                    f'{sub}_{epo}_crossvalclass']  #give imput data

            for permutation in range(
                    NOF_PERMUTATIONS):  #Number of Permutations
                R = np.random.permutation(len(cdata['category_name']))
                per = {
                    'category_name': cdata['category_name'][R],
                    'category': cdata['category'][R],
                    'feature_name': cdata['feature_name'],
                    'trial_info': cdata['trialinfo'],
                    'feature': cdata['feature'],
                    'numclassifiers': cdata['numclassifiers']
                }

                #Apply calssifier into the shuffled category trials
                cfg = {
                    'fold': 10,
                    'classifiernumber': 20,
                    'timebinsnumber': 20,
                    'category_predict': ['Face', 'Landmark', 'Object'],
                    'trials': 'all',
                    'category_model': ['Face', 'Landmark', 'Object']
                }

                per_predtest = acc.mvpa_applycrossvalclassifier(
                    cfg, crossdata, per)  #PER%d_predtest

                #Construct and save the Permutation Map
                sub_P = np.zeros((size[0], size[1]))
                for col in range(size[0]):
                    for row in range(size[1]):
                        trials = np.sum(
                            per_predtest['timebin'][col]['confmatfinal'][row],
                            axis=1)
                        sub_P[row, col] = (
                            (per_predtest['timebin'][col]['confmatfinal'][row][
                                0, 0] / trials[0] * 100) +
                            (per_predtest['timebin'][col]['confmatfinal'][row][
                                1, 1] / trials[1] * 100) +
                            (per_predtest['timebin'][col]['confmatfinal'][row][
                                2, 2] / trials[2] * 100))

                directory = f'./data/Visual/{sub}/8-ClassifierTesting/PermutationStudyDecodeTestVisual/'

                os.makedirs(os.path.dirname(directory), exist_ok=True)

                job.save({f'{sub}_P{permutation+1}': sub_P},
                         directory + f'{sub}_P{permutation+1}.pickle')
Exemplo n.º 3
0
def Do12TestClassifier(sub, epo, crossdata):
    """Do12 Clsasifier testing
	sub -> the subject to be tested
	epo -> the epoch the subject's in
	crossdata -> data structure with classifier of Do11
	"""
    print(sub)
    #Connection object to talk with openstack
    conn = connection.Connection(
        region_name='RegionOne',
        auth=dict(auth_url='https://xerces.ericsson.net:5000',
                  username='******',
                  password='******',
                  project_id='b5c7a743b2554fd6972b841d3e853ac9',
                  user_domain_name='xerces',
                  project_name='bcix'),
        compute_api_version='3',
        identity_interface='public')

    ContainerName = 'DroneContainer'

    #Mock Directory to load from object store.
    directory = f'data/Visual/{sub}/8-ClassifierTesting/'

    #Retrieve data as a raw binary data.
    cFile = BytesIO()
    #Retrieve data from objectstore
    conn.get_object(
        ContainerName,
        f'Visual/{sub}/6-ClassificationData/{sub}_test_data_visual.mat',
        outfile=cFile)

    cdata = sio.loadmat(cFile, chars_as_strings=True,
                        simplify_cells=True)[f'{sub}_test_data_visual']

    #Clean up after ourselves so we don't destroy stuff and free memory
    cFile.close()

    cfg = {
        'fold': 10,
        'classifiernumber': 20,
        'timebinsnumber': 20,
        'category_predict': np.array(['Face', 'Landmark', 'Object']),
        'trials': 'all',
        'category_model': np.array(['Face', 'Landmark', 'Object'])
    }

    #Apply calssifier into the correct visual/ correct visual trials
    predtest_visual = mvpa_applycrossvalclassifier(cfg=cfg,
                                                   model=crossdata,
                                                   data=cdata)

    #Store data in object store
    conn.create_object(ContainerName,
                       directory + f'{sub}_{epo}_predtest_visual.pkl',
                       data=dumps(
                           {f'{sub}_{epo}_predtest_visual': predtest_visual}))

    cfg = {
        'performance': 2,
        'category_model': np.array(['Face', 'Landmark', 'Object']),
        'category_predict': np.array(['Face', 'Landmark', 'Object']),
        'classifiernumber': 20,
        'timebinsnumber': 20
    }

    predtest_visual_performance = mvpa_classifierperf(
        cfg, predtest_visual)  #Performance

    #Store data in object store
    conn.create_object(ContainerName,
                       directory + f'{sub}_{epo}_predtest_visual.pkl',
                       data=dumps({
                           f'{sub}_{epo}_predtest_visual_performance':
                           predtest_visual_performance
                       }))

    #Cleanup Crew reporting for duty!
    conn.close()

    print('Performance calculated')

    return cdata
Exemplo n.º 4
0
def Do13TestClassifier_ShuffledLabels_permutation(sub, epo, perm, size, cdata,
                                                  crossdata):
    """Do13 shuffling labels, create a permutation from old data
	sub -> current subject to process
	epo -> current epoch
	perm -> current permutation number
	size -> list with dimension of size ie. [20,20]
	cdata -> subject test data, classificationdata from 6-ClassificationData
	crossdata -> classifier from Do11
	"""

    #Connection object to talk with openstack
    conn = connection.Connection(
        region_name='RegionOne',
        auth=dict(auth_url='https://xerces.ericsson.net:5000',
                  username='******',
                  password='******',
                  project_id='b5c7a743b2554fd6972b841d3e853ac9',
                  user_domain_name='xerces',
                  project_name='bcix'),
        compute_api_version='3',
        identity_interface='public')

    #Openstack container name
    ContainerName = 'DroneContainer'

    #Create permutation by reshuffling old data randomly
    R = np.random.permutation(len(cdata['category_name']))
    per = {
        'category_name': cdata['category_name'][R],
        'category': cdata['category'][R],
        'feature_name': cdata['feature_name'],
        'trial_info': cdata['trialinfo'],
        'feature': cdata['feature'],
        'numclassifiers': cdata['numclassifiers']
    }

    #Apply classifier into the shuffled category trials
    cfg = {
        'fold': 10,
        'classifiernumber': 20,
        'timebinsnumber': 20,
        'category_predict': ['Face', 'Landmark', 'Object'],
        'trials': 'all',
        'category_model': ['Face', 'Landmark', 'Object']
    }
    per_predtest = mvpa_applycrossvalclassifier(cfg, crossdata,
                                                per)  #PER%d_predtest

    #Construct and save the Permutation Map
    sub_P = np.zeros((size[0], size[1]))
    for col in range(size[0]):
        for row in range(size[1]):
            trials = np.sum(per_predtest['timebin'][col]['confmatfinal'][row],
                            axis=1)
            sub_P[row, col] = (
                (per_predtest['timebin'][col]['confmatfinal'][row][0, 0] /
                 trials[0] * 100) +
                (per_predtest['timebin'][col]['confmatfinal'][row][1, 1] /
                 trials[1] * 100) +
                (per_predtest['timebin'][col]['confmatfinal'][row][2, 2] /
                 trials[2] * 100))

    #Create mock name and save to objectstore in openstack
    filename = f'data/Visual/{sub}/8-ClassifierTesting/PermutationStudyDecodeTestVisual/{sub}_P{perm+1}'
    conn.create_object(ContainerName, filename, data=pickle.dumps(sub_P))

    #Clean upp connectionobject, not necessary but good practice and potentially less problems
    conn.close()

    #Apply gaussian filter before returning sub_P
    return filter.gaussian(sub_P, sigma=1, truncate=2, preserve_range=True)
Exemplo n.º 5
0
def analysis(job, sliceno, slices):
    #Divide the subject list so that each sliceno processes their own subjects.
    sublist = array_split(options.subjects, slices)[sliceno]

    jobbig = jobs.do11Job

    for epo in options.epoch:
        for sub in sublist:

            directory = f'./data/Visual/{sub}/7-ClassifierTraining/'  #directory of the data

            fname = job.input_filename(
                f'./{sub}/6-ClassificationData/{sub}_test_data_visual.mat')
            cdata = loadmat(fname, chars_as_strings=True, simplify_cells=True)[
                f'{sub}_test_data_visual']  #give imput data

            crossdata = jobbig.load(
                directory + f'{sub}_{epo}_crossvalclass.pickle')[
                    f'{sub}_{epo}_crossvalclass']  #give classifier

            cfg = {
                'fold': 10,
                'classifiernumber': 20,
                'timebinsnumber': 20,
                'category_predict': np.array(['Face', 'Landmark', 'Object']),
                'trials': 'all',
                'category_model': np.array(['Face', 'Landmark', 'Object'])
            }

            #Apply calssifier into the correct visual/ correct visual trials
            predtest_visual = acc.mvpa_applycrossvalclassifier(
                cfg, crossdata, cdata)

            print(f'{sub} Done')

            directory = f'./data/Visual/{sub}/8-ClassifierTesting/'

            os.makedirs(os.path.dirname(directory), exist_ok=True)

            job.save({f'{sub}_{epo}_predtest_visual': predtest_visual},
                     directory + f'{sub}_{epo}_predtest_visual.pickle')

            cfg = {
                'performance': 2,
                'category_model': np.array(['Face', 'Landmark', 'Object']),
                'category_predict': np.array(['Face', 'Landmark', 'Object']),
                'classifiernumber': 20,
                'timebinsnumber': 20
            }

            predtest_visual_performance = cp.mvpa_classifierperf(
                cfg, predtest_visual)  #Performance

            print('Performance calculated')

            job.save(
                {
                    f'{sub}_{epo}_predtest_visual_performance':
                    predtest_visual_performance
                }, directory + f'{sub}_{epo}_predtest_visual.pickle')


#something serial after prepare and analysis
#def synthesis()
Exemplo n.º 6
0
def Do12TestClassifier(subject=['Subj01'], epoch=['study']):

    for j, sub in enumerate(subject):
        for epo in epoch:

            directory = f'./data/Visual/{sub}/7-ClassifierTraining/'  #directory of the data, should already exist otherwise can't load data

            cdata = loadmat(
                f'./Visual/{sub}/6-ClassificationData/{sub}_test_data_visual',
                chars_as_strings=True,
                simplify_cells=True
            )[f'{sub}_test_data_visual']  #give imput data, skip first key since unecessary.

            with open(directory + f'{sub}_{epo}_crossvalclass.pkl',
                      'rb') as file:
                crossdata = load(file)[
                    f'{sub}_{epo}_crossvalclass']  #give classifier

            cfg = {
                'fold': 10,
                'classifiernumber': 20,
                'timebinsnumber': 20,
                'category_predict': np.array(['Face', 'Landmark', 'Object']),
                'trials': 'all',
                'category_model': np.array(['Face', 'Landmark', 'Object'])
            }

            #Apply calssifier into the correct visual/ correct visual trials
            predtest_visual = mvpa_applycrossvalclassifier(
                cfg, crossdata, cdata)

            print(f'{sub} Done')

            directory = f'./data/Visual/{sub}/8-ClassifierTesting/'
            #Create new dir if not exist
            os.makedirs(os.path.dirname(directory), exist_ok=True)

            with open(directory + f'{sub}_{epo}_predtest_visual.pkl',
                      'wb') as file:
                dump({f'{sub}_{epo}_predtest_visual': predtest_visual}, file)

            cfg = {
                'performance': 2,
                'category_model': np.array(['Face', 'Landmark', 'Object']),
                'category_predict': np.array(['Face', 'Landmark', 'Object']),
                'classifiernumber': 20,
                'timebinsnumber': 20
            }

            predtest_visual_performance = mvpa_classifierperf(
                cfg, predtest_visual)  #Performance

            with open(directory + f'{sub}_{epo}_predtest_visual.pkl',
                      'wb') as file:
                dump(
                    {
                        f'{sub}_{epo}_predtest_visual_performance':
                        predtest_visual_performance
                    }, file)

            print('Performance calculated')