def Do11ClassifierPerfNonAmp(sub, epo, crossvalclass): """Classifier performance NonAmp part, sub -> a subject string or similiar to process epo -> the epoch for the subject crossvalclass -> data structure with results from a train classifier function """ #Connection Object to connect to object store conn = connection.Connection(cloud='openstack') #The container's name in the objectstore ContainerName = 'DroneContainer' #Mock directory to store the data in the object store NonAmpDirectory = f'data/Visual/{sub}/7-ClassifierTraining/'#directory of the data print('Cross Validated model successfully performed') #Saving the data to the object store conn.create_object(ContainerName, NonAmpDirectory + f'{sub}_{epo}_crossvalclass.pkl', data = dumps({f'{sub}_{epo}_crossvalclass': crossvalclass})) #Performance cfg = {'performance': 1, 'category_model': ['Face', 'Landmark', 'Object'], 'classifiernumber': 20} crossvalclass_performance = mvpa_classifierperf(cfg, crossvalclass) print('Performance calculated') #Saving the data to the object store conn.create_object(ContainerName, NonAmpDirectory + f'{sub}_{epo}_crossvalclass_performance.pkl', data = dumps({f'{sub}_{epo}_crossvalclass_performance': crossvalclass_performance})) #Outcommented because falls out of scope, but still good practice #conn.close() return True
def analysis(job, sliceno, slices): #Divide the subject list so that each sliceno processes their own subjects. sublist = array_split(options.subjects, slices)[sliceno] for epo in options.epoch: for sub in sublist: fname = job.input_filename( f'./{sub}/6-ClassificationData/{sub}_{epo}_data_amp.mat') cdata = loadmat(fname, chars_as_strings=True, simplify_cells=True)[f'{sub}_{epo}_data_amp'] directory = f'./data/Visual/{sub}/7-ClassifierTraining/' os.makedirs(os.path.dirname(directory), exist_ok=True) #Partition of the data cfg = {'classifiernumber': 40, 'fold': 10} datapart = dp.mvpa_datapartition(cfg, cdata) #Partition of the data print( 'Data was partitioned and feature reduction for each partition is complete...' ) #Commented out because datapartition is by far the largest dataobject and takes ridicolous space, of course no problem if one has more than like 50 Gb #job.save({f'{sub}_{epo}_datapart_amp': datapart}, directory + f'{sub}_{epo}_datapart_amp.pickle') #Train the cross validated classifier cfg = { 'training_algorithm': 1, 'fold': 10, 'classifiernumber': 40, 'category_model': ['Face', 'Landmark', 'Object'] } crossval = tcc.mvpa_traincrossvalclassifier(cfg, datapart) print('Cross Validated model successfully performed') job.save({f'{sub}_{epo}_crossvalclass_amp': crossval}, directory + f'{sub}_{epo}_crossvalclass_amp.pickle') #Performance cfg = { 'performance': 1, 'category_model': ['Face', 'Landmark', 'Object'], 'classifiernumber': 40 } crossval_perf = cp.mvpa_classifierperf(cfg, crossval) print('Performance calculated') job.save( {f'{sub}_{epo}_crossvalclass_performance_amp': crossval_perf}, directory + f'{sub}_{epo}_crossvalclass_performance_amp.pickle') #Serial code after analysis to aggregate results, or do something which must be serial. #def synthesis(analysis_res): # pass
def iClass(epo): directory = f'./data/Visual/{sub}/7-ClassifierTraining/' #directory for saving of the data #Create the new directory in case it doesn't already exist os.makedirs(os.path.dirname(directory), exist_ok=True) #Python handles loading .mat files almost better than matlab itself cdata = loadmat( f'./Visual/{sub}/6-ClassificationData/{sub}_{epo}_data', chars_as_strings=True, simplify_cells=True )[f'{sub}_{epo}_data'] #Skip first key since it's unecessary. #Partition of the data cfg = {'classifiernumber': 20, 'fold': 10} datapart = mvpa_datapartition(cfg, cdata, random_state=random_state) print( 'Data was partitioned and feature reduction for each partition is complete...' ) #While it is possible to save the files as .mat files with scipy.io I didn't get it to work without giving severe penalaties on performance with open(directory + f'{sub}_{epo}_datapart.pkl', 'wb') as file: dump({f'{sub}_{epo}_datapart': datapart}, file) #Train the cross validated classifier cfg = { 'training_algorithm': 1, 'fold': 10, 'classifiernumber': 20, 'category_model': ['Face', 'Landmark', 'Object'] } crossvalclass = mvpa_traincrossvalclassifier(cfg, datapart) del datapart #Deleting now redundant variable to free memory print('Cross Validated model successfully performed') #Save with open(directory + f'{sub}_{epo}_crossvalclass.pkl', 'wb') as file: dump({f'{sub}_{epo}_crossvalclass': crossvalclass}, file) #Performance cfg = { 'performance': 1, 'category_model': ['Face', 'Landmark', 'Object'], 'classifiernumber': 20 } crossvalclass_performance = mvpa_classifierperf(cfg, crossvalclass) print('Performance calculated') #Save with open(directory + f'{sub}_{epo}_crossvalclass_performance.pkl', 'wb') as file: dump( { f'{sub}_{epo}_crossvalclass_performance': crossvalclass_performance }, file)
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(cloud='openstack') 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
def Do11ClassifierPerfAmp(sub, epo, crossvalclass_amp): """Classifier performance NonAmp part, sub -> a subject string or similiar to process epo -> the epoch for the subject crossvalclass -> data structure with results from a train classifier function """ #Connection Object to connect to object store 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') #The container's name in the objectstore ContainerName = 'DroneContainer' #Mock directory to store the data in the object store AmpDirectory = f'data/Visual/{sub}/7-ClassifierTraining/' print('Cross Validated model successfully performed!') #Saving the data to the object store conn.create_object( ContainerName, AmpDirectory + f'{sub}_{epo}_crossvalclass_amp.pkl', data=dumps({f'{sub}_{epo}_crossvalclass_amp': crossvalclass_amp})) #Performance cfg = { 'performance': 1, 'category_model': ['Face', 'Landmark', 'Object'], 'classifiernumber': 40 } crossvalclass_performance_amp = mvpa_classifierperf(cfg, crossvalclass_amp) print('Performance calculated!') #Saving the data to the object store conn.create_object(ContainerName, AmpDirectory + f'{sub}_{epo}_crossvalclass_performance_amp.pkl', data=dumps({ f'{sub}_{epo}_crossvalclass_performance_amp': crossvalclass_performance_amp })) #Falls out of scope, but good practice #conn.close() return True
def Do12TestClassifier(subject=['Subj01'], epoch=['study']): for j, sub in enumerate(subject): for epo in epoch: #Should rename e, very hard to identify and change etc. 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')
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()
datapart = mvpa_datapartition(cfg, testdata, random_state=0) cfg = { 'training_algorithm': 1, 'fold': 5, 'classifiernumber': 2, 'category_model': ['Face', 'Landmark', 'Object'] } crossdata = mvpa_traincrossvalclassifier(cfg, datapart) cfg = { 'performance': 1, 'category_model': ['Face', 'Landmark', 'Object'], 'classifiernumber': 2 } crossPerf = mvpa_classifierperf(cfg, crossdata) cdata = loadmat(f'./testData/TestData_test_data_visual.mat', chars_as_strings=True, simplify_cells=True)[f'Subj01_test_data_visual'] cfg = { 'fold': 5, 'classifiernumber': 2, 'timebinsnumber': 3, 'category_predict': np.array(['Face', 'Landmark', 'Object']), 'trials': 'all', 'category_model': np.array(['Face', 'Landmark', 'Object']) } applyCross = mvpa_applycrossvalclassifier(cfg=cfg, model=crossdata, data=cdata)
def iClass(epo): directory = f'./data/Visual/{sub}/7-ClassifierTraining/' #Create dir, if not exist os.makedirs(os.path.dirname(directory), exist_ok=True) cdata = loadmat( f'./Visual/{sub}/6-ClassificationData/{sub}_{epo}_data_amp', chars_as_strings=True, simplify_cells=True)[f'{sub}_{epo}_data_amp'] #partition of the data cfg = {'classifiernumber': 40, 'fold': 10} datapart_amp = mvpa_datapartition(cfg, cdata, random_state=random_state) print( 'Data was partitioned and feature reduction for each partition is complete' ) #Again saving data. with open(directory + f'{sub}_{epo}_datapart_amp.pkl', 'wb') as file: dump({f'{sub}_{epo}_datapart_amp': datapart_amp}, file) #Train the cross validated classifier cfg = { 'training_algorithm': 1, 'fold': 10, 'classifiernumber': 40, 'category_model': ['Face', 'Landmark', 'Object'] } crossvalclass_amp = mvpa_traincrossvalclassifier(cfg, datapart_amp) del datapart_amp #Free up memory print('Cross Validated model successfully performed!') with open(directory + f'{sub}_{epo}_crossvalclass_amp.pkl', 'wb') as file: dump({f'{sub}_{epo}_crossvalclass_amp': crossvalclass_amp}, file) #Performance cfg = { 'performance': 1, 'category_model': ['Face', 'Landmark', 'Object'], 'classifiernumber': 40 } crossvalclass_performance_amp = mvpa_classifierperf( cfg, crossvalclass_amp) print('Performance calculated!') #save with open( directory + f'{sub}_{epo}_crossvalclass_performance_amp.pkl', 'wb') as file: dump( { f'{sub}_{epo}_crossvalclass_performance_amp': crossvalclass_performance_amp }, file)
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