def simulation_onedata(num_activ,fname,TimeShift,WidthShift,IntensityRange,mitc_,outputdir,minic=2,plotall = True): # ActivChan = range(int(num_activ)) # Read the resting state data and compute the concentration matrix data = sio.loadmat(fname,appendmat = False) data = nirs(data) data.CalcOD() data.MBLL(6) data = nirs.PruneChan(data,8) # Pick (num_activ) random channels to simulate the activation num_activ = int(num_activ * len(data.Chan)/100) data.ActivChan = random.sample(data.usefulChan,num_activ) # Add signals to the concentration matrix data = GenerateSignals(data,data.ActivChan,[5,20],TimeShift,WidthShift,IntensityRange) temp = deepcopy(data) data.conc[:,:,0] = BPFilter(data.conc[:,:,0],data,[0.005,1.4]) # data = nirs.Normalize(data) # Compute the block average HRF for each channel temp.conc[:,:,0] = BPFilter(temp.conc[:,:,0],data,[0.005,0.5]) temp = HRFAvg(temp,5,20) data.HRF = temp.HRF data.tHRF = temp.tHRF # Compute the mean block average HRF for the activated channels data.mHRF = np.mean(data.HRF[:,data.ActivChan,:],1) #Now for the ICA procedure int(0.1*np.size(data.Chan)) data.ICA = icastream(data,minic,'Sim',[0.7/20, 1.3/20, 1.4],[5,20],[2.5,mitc_],\ outputdir,plotall = False,dump = False,plotResults = False) # with open(outputdir+'/icastream.dump', 'wb') as output: # cPickle.dump(data, output, cPickle.HIGHEST_PROTOCOL) # try: # ManuscriptPlotSim(data,dirname = outputdir) # except: # print 'Deu ruim no plot final' if hasattr(data,'A_'): if plotall == True: PlotICSpatial(data,data.ICA,dirname=outputdir) return data.ICA
nchan = len(data_aux.Chan) del temp #Now for the ICA procedure for mitcidx in MITC_: criteria = [2.5, mitcidx] outputdir = direc+'/Results/MITC_'+str(mitcidx)+'/'+\ os.path.split(files[file_num])[1].split('.')[0] if not os.path.exists(outputdir): os.makedirs(outputdir) for chromo in ['Phantom']: print chromo worked = 0 try:#int(max(3,0.15*nchan)) setattr(data_aux,'ICA',icastream(data_aux,2,chromo,\ [0.7/posttime,1.3/posttime,1.4], [pretime,posttime],criteria, outputdir,plotall = True,dump=True,plotResults=False)) worked = 1 except: print 'ERROR: icastream failed. This is likely due to minic being larger than the number of channels.' # PlotICSpatial(data_aux,getattr(data_aux,chromo),dirname=outputdir) if worked == 1: if not os.path.exists(direc+'/Results/MITC_'+str(mitcidx)+'/Dump') : os.makedirs(direc+'/Results/MITC_'+str(mitcidx)+'/Dump') with open(direc+'/Results/MITC_'+str(mitcidx)+'/Dump/'+chromo+'_'+os.path.split(files[file_num])[1].split('.')[0]+'.dump', 'wb') as output: cPickle.dump(getattr(data_aux,'ICA'), output, cPickle.HIGHEST_PROTOCOL) ManuscriptPlotSim(data_aux,dirname=direc+'/Results/MITC_'+str(mitcidx)+\ '/'+os.path.split(files[file_num])[1].split('.')[0],res=0.1) ## Collect all data together RealDataStats(direc,MITC_)
# Compute the mean block average HRF for the activated channels if hasattr(data_aux,'ActivChan'): data_aux.mHRF = np.mean(temp.HRF[:,data_aux.ActivChan,:],1) else: if hasattr(data_aux,'usefulChan'): try: data_aux.mHRF = np.mean(temp.HRF[:,data_aux.usefulChan,:],1) except: data_aux.mHRF = np.mean(temp.HRF[:,:,:],1) else: data_aux.mHRF = np.mean(temp.HRF[:,:,:],1) del temp #Now for the ICA procedure for MinIC in [4,5,6,7,12]: outputdir = direc+'/Results/'+\ os.path.split(files[file_num])[1].split('.')[0]+'/'+str(MinIC) if not os.path.exists(outputdir): os.makedirs(outputdir) # def icastream(data,minic,chromo,rfracRng,blockAvgTime,dirname): # try: for chromo in ('HbO','HbR','HbT'): print chromo setattr(data_aux,chromo,icastream(data_aux,MinIC,chromo,\ [0.7/posttime,1.3/posttime,1.4], [pretime,posttime], outputdir,plotall = False)) PlotICSpatial(data_aux,getattr(data_aux,chromo),dirname=outputdir) #Store everything back to the data list data.append(data_aux)
nchan = len(data_aux.Chan) del temp #Now for the ICA procedure for mitcidx in MITC_: criteria = [3.5, mitcidx] outputdir = direc+'/Results/MITC_'+str(mitcidx)+'/'+\ os.path.split(files[file_num])[1].split('.')[0] if not os.path.exists(outputdir): os.makedirs(outputdir) for chromo in ('HbO','HbR'): print chromo worked = 0 try: setattr(data_aux,chromo,icastream(data_aux,int(max(5,0.15*nchan)),chromo,\ [0.7/posttime,1.3/posttime,1.4], [pretime,posttime],criteria, outputdir,plotall = True,dump=True)) worked = 1 except: print 'ERROR: icastream failed. This is likely due to minic being larger than the number of channels.' # PlotICSpatial(data_aux,getattr(data_aux,chromo),dirname=outputdir) if worked == 1: if not os.path.exists(direc+'/Results/MITC_'+str(mitcidx)+'/Dump') : os.makedirs(direc+'/Results/MITC_'+str(mitcidx)+'/Dump') with open(direc+'/Results/MITC_'+str(mitcidx)+'/Dump/'+chromo+'_'+os.path.split(files[file_num])[1].split('.')[0]+'.dump', 'wb') as output: cPickle.dump(getattr(data_aux,chromo), output, cPickle.HIGHEST_PROTOCOL) if data_aux.HbO.ICAFailed == False and data_aux.HbR.ICAFailed == False: ManuscriptPlotVisual(data_aux,dirname=direc_root,\ fname = os.path.split(files[file_num])[1].split('.')[0],res=0.1) ## Collect all data together RealDataStats(direc,MITC_)
else: if hasattr(data_aux,'usefulChan'): try: data_aux.mHRF = np.mean(temp.HRF[:,data_aux.usefulChan,0],1) except: data_aux.mHRF = np.mean(temp.HRF[:,:,0],1) else: data_aux.mHRF = np.mean(temp.HRF[:,:,0],1) del temp #Now for the ICA procedure for MinIC in [2,3,4,5,6]: outputdir = direc+'/Results/'+\ os.path.split(files[file_num])[1].split('.')[0]+'/'+str(MinIC)+'/HbO' if not os.path.exists(outputdir): os.makedirs(outputdir) # def icastream(data,minic,chromo,rfracRng,blockAvgTime,dirname): try: data_aux = icastream(data_aux,MinIC,0,[0.7/posttime,1.3/posttime,1.4], [pretime,posttime],\ outputdir) except: print 'ERROR: icastream failed. This is probably due to bad data or if MinIC is too large.' try: PlotICSpatial(data_aux,dirname=outputdir) except: print 'ERROR: PlotICSpatial has failed. Will resume the analysis for the next dataset.' #Store everything back to the data list data.append(data_aux)