Best Python code snippet using tempest_python
kamitani_data_handler.py
Source:kamitani_data_handler.py
1from scipy.io import loadmat2import numpy as np3import pandas as pd4import sklearn.preprocessing5from sklearn import preprocessing6class kamitani_data_handler():7 """Generate batches for FMRI prediction8 frames_back - how many video frames to take before FMRI frame9 frames_forward - how many video frames to take after FMRI frame10 """11 def __init__(self, matlab_file ,test_img_csv = 'KamitaniData/imageID_test.csv',train_img_csv = 'KamitaniData/imageID_training.csv',voxel_spacing =3,log = 0 ):12 mat = loadmat(matlab_file)13 self.data = mat['dataSet'][:,3:]14 self.sample_meta = mat['dataSet'][:,:3]15 meta = mat['metaData']16 self.meta_keys = list(l[0] for l in meta[0][0][0][0])17 self.meta_desc = list(l[0] for l in meta[0][0][1][0])18 self.voxel_meta = np.nan_to_num(meta[0][0][2][:,3:])19 test_img_df = pd.read_csv(test_img_csv, header=None)20 train_img_df =pd.read_csv(train_img_csv, header=None)21 self.test_img_id = test_img_df[0].values22 self.train_img_id = train_img_df[0].values23 self.sample_type = {'train':1 , 'test':2 , 'test_imagine' : 3}24 self.voxel_spacing = voxel_spacing25 self.log = log26 def get_meta_field(self,field = 'DataType'):27 index = self.meta_keys.index(field)28 if(index <3): # 3 first keys are sample meta29 return self.sample_meta[:,index]30 else:31 return self.voxel_meta[index]32 def print_meta_desc(self):33 print(self.meta_desc)34 def get_labels(self, imag_data = 0,test_run_list = None):35 le = preprocessing.LabelEncoder()36 img_ids = self.get_meta_field('Label')37 type = self.get_meta_field('DataType')38 train = (type == self.sample_type['train'])39 test = (type == self.sample_type['test'])40 imag = (type == self.sample_type['test_imagine'])41 img_ids_train = img_ids[train]42 img_ids_test = img_ids[test]43 img_ids_imag = img_ids[imag]44 train_labels = []45 test_labels = []46 imag_labels = []47 for id in img_ids_test:48 idx = (np.abs(id - self.test_img_id)).argmin()49 test_labels.append(idx)50 for id in img_ids_train:51 idx = (np.abs(id - self.train_img_id)).argmin()52 train_labels.append(idx)53 for id in img_ids_imag:54 idx = (np.abs(id - self.test_img_id)).argmin()55 imag_labels.append(idx)56 if (test_run_list is not None):57 run = self.get_meta_field('Run')58 test = (self.get_meta_field('DataType') == 2).astype(bool)59 run = run[test]60 select = np.in1d(run, test_run_list)61 test_labels = test_labels[select]62 #imag_labels = le.fit_transform(img_ids_imag)63 if(imag_data):64 return np.array(train_labels), np.array(test_labels), np.array(imag_labels)65 else:66 return np.array(train_labels),np.array(test_labels)67 def get_data(self,normalize =1 ,roi = 'ROI_VC',imag_data = 0,test_run_list = None): # normalize 0-no, 1- per run , 2- train/test seperatly68 type = self.get_meta_field('DataType')69 train = (type == self.sample_type['train'])70 test = (type == self.sample_type['test'])71 test_imag = (type == self.sample_type['test_imagine'])72 test_all = np.logical_or(test,test_imag)73 roi_select = self.get_meta_field(roi).astype(bool)74 data = self.data[:,roi_select]75 if(self.log ==1):76 data = np.log(1+np.abs(data))*np.sign(data)77 if(normalize==1):78 run = self.get_meta_field('Run').astype('int')-179 num_runs = np.max(run)+180 data_norm = np.zeros(data.shape)81 for r in range(num_runs):82 data_norm[r==run] = sklearn.preprocessing.scale(data[r==run])83 train_data = data_norm[train]84 test_data = data_norm[test]85 test_all = data_norm[test_all]86 test_imag = data_norm[test_imag]87 else:88 train_data = data[train]89 test_data = data[test]90 if(normalize==2):91 train_data = sklearn.preprocessing.scale(train_data)92 test_data = sklearn.preprocessing.scale(test_data)93 if(self.log ==2):94 train_data = np.log(1+np.abs(train_data))*np.sign(train_data)95 test_data = np.log(1+np.abs(test_data))*np.sign(test_data)96 train_data = sklearn.preprocessing.scale(train_data)97 test_data = sklearn.preprocessing.scale(test_data)98 test_labels = self.get_labels()[1]99 imag_labels = self.get_labels(1)[2]100 num_labels = max(test_labels)+1101 test_data_avg = np.zeros([num_labels,test_data.shape[1]])102 test_imag_avg = np.zeros([num_labels,test_data.shape[1]])103 if(test_run_list is not None):104 run = self.get_meta_field('Run')105 test = (self.get_meta_field('DataType') == 2).astype(bool)106 run = run[test]107 select = np.in1d(run, test_run_list)108 test_data = test_data[select,:]109 test_labels = test_labels[select]110 for i in range(num_labels):111 test_data_avg[i] = np.mean(test_data[test_labels==i],axis=0)112 test_imag_avg[i] = np.mean(test_imag[imag_labels == i], axis=0)113 if(imag_data):114 return train_data, test_data, test_data_avg,test_imag,test_imag_avg115 else:116 return train_data, test_data, test_data_avg117 def get_voxel_loc(self):118 x = self.get_meta_field('voxel_x')119 y = self.get_meta_field('voxel_y')120 z = self.get_meta_field('voxel_z')121 dim = [int(x.max() -x.min()+1),int(y.max() -y.min()+1), int(z.max() -z.min()+1)]...
main.py
Source:main.py
1from agent import *2training_eps=100003test_runs=54max_len=10005testing_steps=[]6ag=Agent()7for episode in range(training_eps):8 w=World(0,0)9 for step in range(max_len):10 a=ag.e_greedy(w.pos,w.vel)11 nextp,nextv,R=w.move(a)12 a_next=ag.e_greedy(nextp,nextv)13 ag.update(w.pos,w.vel,a,R,nextp,nextv,a_next)14 w.pos=nextp15 w.vel=nextv16 if(R==0):17 break18 elif(w.pos<=w.pmin):19 w.vel=0 # the car bumps to a stop 20 """21 running agent after each simulation22 multiple runs to get a better estimate of agent performance at this episode23 test_run_list stores the corresponding "test_run" number of values24 """25 test_run_list=[]26 for run in range(test_runs):27 w=World(0,0)28 for step in range(max_len):29 a=ag.greedy(w.pos,w.vel)30 nextp,nextv,R=w.move(a)31 a_next=ag.greedy(nextp,nextv)32 """33 The next command is required even in test case because the agent must learn to move in the environment every time it interacts with it. If it encounters a state upon which it is not trained well the agent would fail repeatedly across test runs unless we update it between runs.34 """35 ag.update(w.pos,w.vel,a,R,nextp,nextv,a_next) 36 w.pos=nextp37 w.vel=nextv38 if(R==0):39 break40 elif(w.pos<w.pmin):41 w.vel=042 test_run_list.append(step)43 testing_steps.append(mean(test_run_list))44 if(episode%1000==0):45 print('Done episode {}'.format(episode)) 46# save agent47f='agent.sav'48file=open(f,'wb')49pickle.dump(ag,file)50file.close()51# plot peformance of agent with training episodes52plt.plot(range(1,1+len(testing_steps)),testing_steps,label='No. of steps')53plt.title('No. of steps to reach terminal states vs. training episodes')54plt.xlabel('Training Episodes')55plt.ylabel('No. of steps')56plt.legend()...
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!