def draw_value(value_dict, useable_ace=True, is_q_dict=False, A=None): # 定义figure fig = plt.figure() # 将figure变为3d ax = Axes3D(fig) # 定义x,y x = np.arange(1, 11, 1) # 庄家第一张牌 y = np.arange(12, 22, 1) # 玩家总分数 # 生成网格数据 X, Y = np.meshgrid(x, y) # 从V字典检索Z轴的高度 row, col = X.shape Z = np.zeros((row, col)) if is_q_dict: n = len(A) for i in range(row): for j in range(col): state_name = str(X[i, j]) + '_' + \ str(Y[i, j]) + '_'+str(useable_ace) if not is_q_dict: Z[i, j] = get_dict(value_dict, state_name) else: assert (A is not None) for a in A: new_state_name = state_name + '_' + str(a) q = get_dict(value_dict, new_state_name) if q >= Z[i, j]: Z[i, j] = q # 绘制3D曲面 ax.plot_surface(X, Y, Z, rstride=1, cstride=1, color="lightgray") plt.show()
def policy_evaluate(episodes,V,Ns): for episode,r in episodes: for s,a in episode: ns = get_dict(Ns,s) v = get_dict(V,s) set_dict(Ns,ns+1,s) set_dict(V,v+(r-v)/(ns+1),s)
def learning_method(self, gamma=0.9, alpha=0.1, epsilon=1e-5, display=False, lambda_=None): self.state = self.env.reset() s0 = self.state if display: self.env.render() # a0 = self.perform_policy(s0, epsilon) # print(self.action_t.name) time_in_episode, total_reward = 0, 0 is_done = False while not is_done: # add code here a0 = self.perform_policy(s0, epsilon) s1, r1, is_done, info, total_reward = self.act(a0) if display: self.env.render() self.policy = greedy_policy a1 = greedy_policy(self.A, s1, self.Q) old_q = get_dict(self.Q, s0, a0) q_prime = get_dict(self.Q, s1, a1) td_target = r1 + gamma * q_prime #alpha = alpha / num_episode new_q = old_q + alpha * (td_target - old_q) set_dict(self.Q, new_q, s0, a0) # s0, a0 = s1, a1 s0 = s1 time_in_episode += 1 if display: print(self.experience.last_episode) return time_in_episode, total_reward
def draw_value(value_dict,useable_ace = True,is_q_dict = False,A = None): fig = plt.figure() #将fig变为3d ax = Axes3D(fig) x = np.arange(1,11,1) y = np.arange(12,22,1) X,Y = np.meshgrid(x,y) row,col = X.shape Z = np.zeros((row,col)) if is_q_dict: n = len(A) for i in range(row): for j in range(col): state_name = str(X[i,j])+"_"+str(Y[i,j])+"_"+str(useable_ace) if not is_q_dict: Z[i,j] = get_dict(value_dict,state_name) #策略控制部分 else: assert(A is not None) for a in A: new_state_name = state_name+"_"+str(a) q = get_dict(value_dict,new_state_name) if q>=Z[i,j]: Z[i,j] = q ax.plot_surface(X,Y,Z,rstride = 1,cstride = 1,color = "lightgray") plt.show()
def draw_value(value_dict, useable_ace=True, is_q_dict=False, A=None): #Definition figure fig = plt.figure() #Turn figure into 3d ax = Axes3D(fig) #Definition x,y x = np.arange(1, 11, 1) #Dealer's first card y = np.arange(12, 22, 1) #Total player score #Generate grid data X, Y = np.meshgrid(x, y) #Retrieve the height of the Z axis from the V dictionary row, col = X.shape Z = np.zeros((row, col)) if is_q_dict: n = len(A) for i in range(row): for j in range(col): state_name = str(X[i, j]) + '_' + str( Y[i, j]) + '_' + str(useable_ace) if not is_q_dict: Z[i, j] = get_dict(value_dict, state_name) else: assert (A is not None) for a in A: new_state_name = state_name + '_' + str(a) q = get_dict(value_dict, new_state_name) if q >= Z[i, j]: Z[i, j] = q #Draw 3D surface ax.plot_surface(X, Y, Z, rstride=1, cstride=1, color='lightgray') plt.show()
def learner(self, gamma=0.9, alpha=0.1, epsilon=1e-5, display=False, lambda_=None): self.state = self.env.reset() s0 = self.state if display: self.env.render() a0 = self.perform_policy(s0, epsilon=epsilon) time_in_episode, total_reward = 0, 0 is_done = False while not is_done: s1, r1, is_done, info, total_reward = self.act(a0) if display: self.env.render() a1 = self.perform_policy(s1, epsilon=epsilon) old_q = get_dict(self.Q, s0, a0) q_prime = get_dict(self.Q, s1, a1) td_target = r1 + gamma * q_prime new_q = old_q + alpha * (td_target - old_q) set_dict(self.Q, new_q, s0, a0) s0, a0 = s1, a1 time_in_episode += 1 if display: print(self.experience.last_episode) return time_in_episode, total_reward
def init_dict(): # args = utils.get_parse() args = dict() dict_username = args.get('dict_username', "username.txt") dict_password = args.get('dict_password', "password.txt") utils.get_dict(dict_username, dict_password) return dict_username, dict_password
def learn_Q(self, episode, r): #Learn the Q value from the state sequence ''' Learn from an episode ''' for s, a in episode: nsa = get_dict(self.Nsa, s, a) set_dict(self.Nsa, nsa + 1, s, a) q = get_dict(self.Q, s, a) set_dict(self.Q, q + (r - q) / (nsa + 1), s, a) self.total_learning_times += 1
def policy_evaluate(episodes, V, Ns): '''统计一个状态的价值, 衰减因子为1, 中间状态的即时奖励为0, 递增式蒙特卡罗策略评估 V,Ns保存着蒙特卡罗策略评估进程中的价值和统计次数数据, 我们使用是的每次访问计数的方法''' for episode, r in episodes: for s, a in episode: ns = get_dict(Ns, s) v = get_dict(V, s) set_dict(Ns, ns + 1, s) set_dict(V, v + (r - v) / (ns + 1), s) pass
def exchange_for_tokens(self, request): state = request.args.get('state') or '' redirect_uri = self.__class__.build_redirect_uri(request.base_url) auth_flow = flow.Flow.from_client_secrets_file( self.CLIENT_FILE_PATH, scopes=self.SCOPES, state=state, ) auth_flow.redirect_uri = redirect_uri authorization_response = request.url try: auth_flow.fetch_token( authorization_response=authorization_response) except Exception as err: self.note = str(err) return self self.credential = get_dict(auth_flow.credentials) google_token = self.credential['token'] get_profile_url = f'{self.GET_PROFILE_BASE_URL}?key={self.API_KEY}' get_profile_headers = dict( authorization=f'{self.AUTH_TOKEN_HEADER} {google_token}') self.response = requests.get(get_profile_url, headers=get_profile_headers).json() return self
def filterPPI(fname=config.PRO_PRO_PATH): fin = open(fname) print 'Reading: %s' % fname gen2Id = utils.load_obj(config.PROTEIN_MAP) fin.readline() nGen = len(gen2Id) mat = np.zeros((nGen, nGen)) for line in fin: gene_id1, gene_id2 = line.strip().split(',') id1 = utils.get_dict(gen2Id, gene_id1, -1) id2 = utils.get_dict(gen2Id, gene_id2, -1) if id1 != -1 and id2 != -1: mat[id1, id2] = mat[id2, id1] = 1 mat = sp.csr_matrix(mat) utils.save_obj(mat, config.PROCESSED_PROTEINPROTEIN)
def filter_targets(fname=config.DRUG_PRO_PATH): gen2Id = dict() drug2Id = utils.load_obj(config.DRUG_MAP) fin = open(fname) print 'Reading: %s' % fname fin.readline() drug2Protein = [[] for i in range(len(drug2Id))] for line in fin: stitch_id, gene = line.strip().split(',') genId = utils.get_update_dict_index(gen2Id, gene) drugId = utils.get_dict(drug2Id, stitch_id) proteins = drug2Protein[drugId] proteins.append(genId) print len(gen2Id) print gen2Id mat = np.zeros((len(drug2Id), len(gen2Id))) for drugId, proteins in enumerate(drug2Protein): for proteinId in proteins: mat[drugId][proteinId] = 1 drugProteinAdj = sp.csr_matrix(mat) proteinDrugAdj = drugProteinAdj.transpose(copy=True) utils.save_obj(drugProteinAdj, config.PROCESSED_DRUGPROTEIN) utils.save_obj(proteinDrugAdj, config.PROCESSED_PROTEINDRUG) utils.save_obj(gen2Id, config.PROTEIN_MAP)
def food_cafe(): lat = request.args.get('lat') long = request.args.get('lng') url = '''https://maps.googleapis.com/maps/api/place/nearbysearch/json?parameters&key=%s\ &location=%s,%s\ &radius=2000\ &type=cafe''' % (google_places_api_key, lat, long) results_json = requests.get(url).json() arr = [] for i in results_json['results']: d = get_dict(id=i['place_id'], \ name=i['name'], \ address=i['vicinity'], \ distance=int(vincenty((lat, long), ( float(i['geometry']['location']['lat']), float(i['geometry']['location']['lng']))).meters), \ type=i['types'], lat=i['geometry']['location']['lat'], lng=i['geometry']['location']['lng'] ) try: d['rating'] = i['rating'] except: d['rating'] = '0.0' try: d['open_now'] = i['opening_hours']['open_now'] except: d['open_now'] = 'no response' arr.append(copy.deepcopy(d)) return json.dumps(arr)
def extract_features_path(self, path_audio, static=True, plots=False, fmt="npy", kaldi_file=""): """ Extract the representation learning features for audios inside a path :param path_audio: directory with (.wav) audio files inside, sampled at 16 kHz :param static: whether to compute and return statistic functionals over the feature matrix, or return the feature matrix computed over frames :param plots: timeshift to extract the features :param fmt: format to return the features (npy, dataframe, torch, kaldi) :param kaldi_file: file to store kaldifeatures, only valid when fmt=="kaldi" :returns: features computed from the audio file. >>> replearning=RepLearning('CAE') >>> path_audio="../audios/" >>> features1=phonological.replearning(path_audio, static=True, plots=False, fmt="npy") >>> features2=phonological.replearning(path_audio, static=True, plots=False, fmt="csv") >>> features3=phonological.replearning(path_audio, static=False, plots=True, fmt="torch") >>> replearning.extract_features_path(path_audio, static=False, plots=False, fmt="kaldi", kaldi_file="./test.ark") """ hf=os.listdir(path_audio) hf.sort() pbar=tqdm(range(len(hf))) ids=[] Features=[] for j in pbar: pbar.set_description("Processing %s" % hf[j]) audio_file=path_audio+hf[j] feat=self.extract_features_file(audio_file, static=static, plots=plots, fmt="npy") Features.append(feat) if static: ids.append(hf[j]) else: ids.append(np.repeat(hf[j], feat.shape[0])) Features=np.vstack(Features) ids=np.hstack(ids) if fmt in("npy","txt"): return Features if fmt in("dataframe","csv"): if static: df={} for e, k in enumerate(self.head_st): df[k]=Features[:,e] else: df={} for e, k in enumerate(self.head_dyn): df[k]=Features[:,e] df["id"]=ids return pd.DataFrame(df) if fmt=="torch": return torch.from_numpy(Features) if fmt=="kaldi": if static: raise ValueError("Kaldi is only supported for dynamic features") dictX=get_dict(Features, ids) save_dict_kaldimat(dictX, kaldi_file) else: raise ValueError(fmt+" is not supported")
def learning_method(self, lambda_=0.9, gamma=0.9, alpha=0.1, epsilon=1e-5, display=False): self.state = self.env.reset() s0 = self.state if display: self.env.render() a0 = self.perform_policy(s0, epsilon) # print(self.action_t.name) time_in_episode, total_reward = 0, 0 is_done = False E = {} while not is_done: # add code here s1, r1, is_done, info, total_reward = self.act(a0) if display: self.env.render() a1 = self.perform_policy(s1, epsilon) q = get_dict(self.Q, s0, a0) # old q q_prime = get_dict(self.Q, s1, a1) # new q # delta = R + gamma * Q(s',s') - Q(s,s) delta = r1 + gamma * q_prime - q e = get_dict(E, s0, a0) e += 1 set_dict(E, e, s0, a0) # for all s in S, a in A for s in self.S: for a in self.A: e_value = get_dict(E, s, a) old_q = get_dict(self.Q, s, a) # Q(s,a) = Q(s,a) + alpha * delta * E(s,a) new_q = old_q + alpha * delta * e_value # E(s,a) = gamma * lambda * E(s,a) new_e = gamma * lambda_ * e_value set_dict(self.Q, new_q, s, a) set_dict(E, new_e, s, a) # s=s', a=a' s0, a0 = s1, a1 time_in_episode += 1 if display: print(self.experience.last_episode) return time_in_episode, total_reward
def statistic(): revenue_dict = utils.get_dict('revenue_dict') value_list = [] key_list = [] for key in revenue_dict: key_list.append(key) revenue_list = revenue_dict.get(key, []) avg_value = sum(revenue_list) / float(len(revenue_list)) value_list.append(avg_value) df = pd.DataFrame({'days': key_list, 'revenue': value_list}) utils.save(df, 'red_candle_revenue')
def validate(self): errors = {} for validate_function in self.validators: try: validate_function() except Exception as err: err = get_dict(err) for key, value in err.items(): errors.setdefault(key, []) errors[key].extend(value) return errors
def exportAEOLUSBio2RDFFeature(): allDrug2BioRDFFeature = loadAllBio2RDFFeature() fin = open(const.AEOLUS_ADR_PATH) fout = open(const.AEOLUS_BIO2RDF_PATH, "w") while True: line = fin.readline() if line == "": break drugId = line.strip().split("|")[0] feature = utils.get_dict(allDrug2BioRDFFeature, drugId, "") fout.write("%s|%s\n" % (drugId, feature)) fin.close() fout.close()
def save(self, **new_values): self.modified = datetime.now() collection = self.get_collection() if not collection: return None, dict(__all__='collection not found') try: if hasattr(self, '_id'): _id = ObjectId(self._id) if new_values: updating_values = new_values else: updating_values = get_dict(self) updating_values.pop('_id') collection.update_one(dict(_id=_id), {'$set': updating_values}) else: _id = collection.insert(self.__dict__) except Exception as err: return None, get_dict(err) instance = self.__class__.find_one(_id=_id) return instance, {}
def learning_method(self, gamma=0.9, alpha=0.1,epsilon=1e-5,display=False,lambda_=0.9 ): self.state = self.env.reset() s0 = self.state if display: self.env.render() a0 = self.perform_policy(s0, self.Q, epsilon) time_in_episode,total_reward = 0, 0 is_done = False E = {} # 效用值 while not is_done: s1, r1, is_done, info, total_reward = self.act(a0) if display: self.env.render() a1 = self.perform_policy(s1, self.Q, epsilon) q = get_dict(self.Q, s0, a0) q_prime = get_dict(self.Q, s1, a1) delta = r1 + gamma * (q_prime - q) e = get_dict(E, s0, a0) e += 1 set_dict(E, e, s0, a0) for s in self.S: for a in self.A: e_value = get_dict(E, s, a) old_q = get_dict(self.Q, s, a) new_q = old_q + alpha * delta * e_value new_e = gamma * lambda_ *e_value set_dict(self.Q, new_q, s, a) set_dict(E, new_e, s, a) s0, a0 = s1, a1 time_in_episode += 1 if display: print(self.experience.last_episode) return time_in_episode, total_reward
def save_series(): workbook = xlwt.Workbook(encoding='utf-8') sheet = workbook.add_sheet("datelist", cell_overwrite_ok=True) style = xlwt.XFStyle() font = xlwt.Font() font.name = 'SimSun' # 指定“宋体” style.font = font series_dict = utils.get_dict('series_dict') sheet.write(0, 0, '代码') sheet.write(0, 1, '日期') row = 0 for keys in series_dict: row = row + 1 date_list = series_dict.get(keys, []) sheet.write(row, 0, keys) for i in range(len(date_list)): sheet.write(row, i + 1, date_list[i]) workbook.save(os.path.dirname(os.getcwd()) + '\\data\\xiaoyangxian.xls')
def caculate_revenue(): series_dict = utils.get_dict('series_dict') revenue_days = 60 revenue_dict = {} for key in series_dict: value_list = series_dict[key] code_df = utils.read(key) for date_string in value_list: print key print date_string series_date = (datetime.datetime.strptime(date_string, '%Y-%m-%d')).date() series_df = code_df[code_df['date'].isin([date_string])] series_close = float(series_df['close']) revenue_date = series_date for days in range(revenue_days): revenue_list = revenue_dict.get(str(days), []) revenue_date = revenue_date + datetime.timedelta(days=1) revenue_date_string = revenue_date.strftime('%Y-%m-%d') revenue_df = code_df[code_df['date'].isin( [revenue_date_string])] if revenue_df.empty: for nextday in range(10): revenue_date = revenue_date + datetime.timedelta( days=1) revenue_date_string = revenue_date.strftime('%Y-%m-%d') revenue_df = code_df[code_df['date'].isin( [revenue_date_string])] if not revenue_df.empty: break if revenue_df.empty: continue revenue_close = float(revenue_df['close']) revenue = revenue_close / series_close revenue_list.append(revenue) revenue_dict[str(days)] = revenue_list # print days # print revenue_date # print revenue utils.save_dict(revenue_dict, 'revenue_dict')
def process_raw_data(training_path, output_folder): user_dict = dict() movie_dict = dict() with open(training_path, 'r') as file: output_str, movie_dict, user_dict, movie_count, user_count, train_count = get_dict( file, movie_dict, user_dict) write_to_file(output_folder, output_str) # write_dict_to_file(output_folder + '/user_dict.txt', user_dict) # write_dict_to_file(output_folder + '/movie_dict.txt', movie_dict) count_file_content = str(user_count) + "," + str(movie_count) + '\n' + str( train_count) # write_to_file(output_folder + '/count.txt', count_file_content) print("Number of movie: " + str(movie_count)) print("Number of user: "******"Number of train: " + str(train_count)) return movie_dict, user_dict, movie_count, user_count
# Generates csv for test dataset containing id, landmark_id and bounding boxes from downloaded images import os image_ids = os.listdir("test") import csv from utils import get_dict # Creates dictionary to obtain landmark id from train_dict and bounding boxes from box_dict test_dict = get_dict("train.csv", 2) box_dict = get_dict("boxes_split2.csv", 1) with open('test_downloaded.csv', 'w', newline='') as csvfile: fieldnames = ['id', 'landmark_id', 'box'] writer = csv.DictWriter(csvfile, fieldnames=fieldnames) writer.writeheader() for id in image_ids: actual_id = str(id[:len(id)-4]) writer.writerow({'id': actual_id, 'landmark_id': test_dict[actual_id], 'box': box_dict[actual_id]})
def from_config(config): return utils.get_dict("inventory", config)
# print("用户名或密码错误") return False else: pass else: print("连接异常") except Exception as e: print(e) # except requests.exceptions.ConnectionError: # print("代理不可用,更换代理") # CUR_PROXY = utils.get_proxy() # print("当前使用代理:{}".format(CUR_PROXY)) # except requests.exceptions.Timeout: # print("代理连接速度过慢,更换代理!") # CUR_PROXY = utils.get_proxy() # print("当前使用代理:{}".format(CUR_PROXY)) # except: # # print('未知情况') # pass if __name__ == "__main__": # 开启代理池 args = utils.get_parse() dict_username = args.get('dict_username', "username.txt") dict_password = args.get('dict_password', "password.txt") utils.get_dict(dict_username, dict_password) bruteforce(login_bypass_ip_limit, thread_num=5)
def list_regions(): d = get_dict(settings.BASE_URL + "/regions/all") return [result['region'] for result in d['results']]
import os import time import pickle as cPickle if os.path.isfile(pickle_file): a = time.time() x_train, i_train, x_train_len, y_train, \ u_train, p_train, u_train_count, p_train_count, \ x_dev, i_dev, x_dev_len, y_dev, \ u_dev, p_dev, u_dev_count, p_dev_count, \ x_test, i_test, x_test_len, y_test, \ u_test, p_test, u_test_count, p_test_count, \ x_dict, u_dict, p_dict, u_freq, p_freq, \ x_vectors = cPickle.load(open(pickle_file, 'rb')) else: x_dict = utils.get_dict(train_file, x_index) u_dict, p_dict, u_freq, p_freq = utils.get_up_dict(train_file, u_index, p_index) x_train, i_train, x_train_len, y_train, u_train, p_train, \ u_train_count, p_train_count = utils.get_flat_data(train_file, x_index, y_index, x_dict, num_classes, u_index, p_index, u_dict, p_dict, u_freq, p_freq) x_dev, i_dev, x_dev_len, y_dev, u_dev, p_dev, \ u_dev_count, p_dev_count = utils.get_flat_data(dev_file, x_index, y_index, x_dict, num_classes, u_index, p_index, u_dict, p_dict, u_freq, p_freq) x_test, i_test, x_test_len, y_test, u_test, p_test, \ u_test_count, p_test_count = utils.get_flat_data(test_file, x_index, y_index, x_dict, num_classes, u_index, p_index, u_dict, p_dict, u_freq, p_freq) x_vectors = utils.get_vectors(x_dict, vec_file, emb_size) cPickle.dump([x_train, i_train, x_train_len, y_train, \ u_train, p_train, u_train_count, p_train_count, \ x_dev, i_dev, x_dev_len, y_dev, \ u_dev, p_dev, u_dev_count, p_dev_count, \
def create(cls, show_id): show = cls(show_id=show_id) url = settings.BASE_URL + "/show/" + id show.d = get_dict(url)['results'][0] return show
def get_all_shows(limit1=50, limit=25): url = settings.BASE_URL + "/shows/all/%s/%s/all/all" % (limit1, limit2) d = get_dict(url) return [result['title'] for result in d['results']]
# files = [f for f in os.listdir('.') if os.path.isfile(f)] # for f in files: # shutil.copy(f, output_subdir) return output_subdir def load_weights_if_resume_training(training_model): if cfg.resume_training: training_model.load_weights(cfg.load_model_path) return training_model if __name__ == '__main__': os.environ['CUDA_VISIBLE_DEVICES'] = cfg.gpus # os.environ['CUDA_VISIBLE_DEVICES'] = '0' idx_char_dict, char_idx_dict = get_dict(cfg.label_pkl_path) print('dict init') test_sample_list = load_test_sample( img_root=os.path.join(cfg.test_dir, 'img'), label_root=os.path.join(cfg.test_dir, 'txt'), char_idx_dict=char_idx_dict) print('test data init') training_model, prediction_model = model_STN(cfg, idx_char_dict) print('model init') opt = get_optimizer() print('optimizer init') output_subdir = create_output_directory() image_generator = ImageGenerator(cfg.font_path) print('image_generator init') train_generator, val_generator = get_generators(output_subdir, char_idx_dict,
def POST(self): get_dict() return self.data_handle()
# Family layer order_folder = os.listdir("prediction/rna/") path = "prediction/rna/" for order in order_folder: if len(order.split(".")) == 2: continue cnt = utils.get_leaf_num(order) if cnt == 0: continue # Start to run CHEER os.system("bash clean_all_pre_script.sh") os.system("cp "+path+order+"/* validation") os.system("bash code/pre_train_script.sh") os.system("python show_result.py --gpus 0 --n "+str(cnt)+" --t 0.6 --embed ../pkl/Family/"+ order +"_Embed.pkl --classifier ../pkl/Family/"+ order +"_Params.pkl") family_dict = utils.get_dict(order) family_list = list(family_dict.values()) family_name_string = " ".join(family_list) family_dir_list = ["rna/"+order+"/"+name for name in family_list] family_dir_string = " ".join(family_dir_list) os.system("python split_data.py --path rna/"+order+" --dir "+ family_dir_string + " --child_list "+ family_name_string) tmp = os.listdir("prediction") if "early_stop.fasta" in tmp: os.system("mv prediction/early_stop.fasta prediction/rna/"+order+"/") # Genus layer family_folder = os.listdir("prediction/rna/"+order+"/") for family in family_folder: continue
def get_show_information(id): url = settings.BASE_URL + "/show/" + id d = get_dict(url) return d['results'][0]['title']