How to use save_file_as method in SeleniumBase

Best Python code snippet using SeleniumBase

util.py

Source:util.py Github

copy

Full Screen

1from collections import defaultdict2from collections import OrderedDict3from os import path4import logging5import numpy as np6import torch7from properties import *8def init_logger(name='logger'):9 """Initialize and return a logger."""10 logger = logging.getLogger(name)11 logger.setLevel(logging.DEBUG)12 log_fmt = '%(asctime)s/%(name)s[%(levelname)s]: %(message)s'13 logging.basicConfig(format=log_fmt)14 return logger15def to_tensor(numpy_array):16 tensor = torch.from_numpy(numpy_array).float()17 if torch.cuda.is_available():18 tensor = tensor.cuda()19 return tensor20def to_variable(tensor, volatile=False):21 if torch.cuda.is_available():22 tensor = tensor.cuda()23 return torch.autograd.Variable(tensor, volatile)24def get_word_vectors_dicts(filename, dirname=DATA_DIR, save=False,25 save_file_as='en_dict'):26 word2vec = OrderedDict()27 count = 028 print('Read ' + path.join(dirname, filename))29 with open(path.join(dirname, filename), 'r', encoding='utf-8') as f:30 N, D = f.readline().strip().split()31 D = int(D)32 for count, line in enumerate(f.readlines()):33 row = line.strip().split(' ')34 word = row[0]35 vec = np.array(row[1:]).astype(np.float)36 if len(vec) == D:37 word2vec[word] = vec38 count += 139 if count == top_frequent_words:40 break41 if save:42 np.save(path.join(dirname, save_file_as + '.npy'), word2vec)43 return word2vec44def get_validation_set(filename, dirname=DATA_DIR, save=False, save_file_as='validation'):45 true_dict = OrderedDict()46 print('Read ' + path.join(dirname, filename))47 with open(path.join(dirname, filename), 'r', encoding='utf-8') as f:48 rows = f.readlines()49 for row in rows:50 split_row = row.split(" ")51 key = split_row[0]52 value = split_row[1].rstrip("\n")53 if key not in true_dict.keys():54 true_dict[key] = []55 true_dict[split_row[0]].append(value)56 if save:57 np.save(path.join(dirname, save_file_as + '.npy'), true_dict)58 return true_dict59def get_frequencies():60 src = np.load(path.join(DATA_DIR, 'src.freq.npy'))61 trg = np.load(path.join(DATA_DIR, 'tgt.freq.npy'))62 return src, trg63class WeightedSampler():64 def __init__(self, weights, replacement=True):65 """Initialize a sampler.66 Arguments:67 - weights (list) : a list of weights, not necessary summing up to one68 - replacement (bool): if ``True``, samples are drawn with replacement.69 """70 self.weights = torch.DoubleTensor(weights)71 self.replacement = replacement72 def get_iterator(self, batch_size):73 """Generate a batch of samples infinitely."""74 while True:75 yield torch.multinomial(self.weights, batch_size, self.replacement)76 77def downsample_frequent_words(counts, thresh=1e-3):78 """Discount frequent words."""79 total_count = counts.sum()80 indices_zero = counts == 081 counts[indices_zero] = 1.0 # avoid a numerical error82 threshold_count = float(thresh * total_count)83 new_weights = (np.sqrt(counts / threshold_count) + 1) * (threshold_count / counts)84 new_weights = np.maximum(new_weights, 1.0)85 new_weights *= counts86 new_weights[indices_zero] = 0.087 return new_weights / new_weights.sum()88class Utils:89 def __init__(self, params):90 self.data_dir = params.data_dir91 self.src_file = params.src_file92 self.tgt_file = params.tgt_file93 self.src_freq_file = params.src_freq_file94 self.tgt_freq_file = params.tgt_freq_file95 self.validation_file = params.validation_file96 self.top_frequent_words = params.top_frequent_words97 def run(self):98 print("Reading source word embeddings...")99 word2vec_src, _ = self.save_word_vectors(self.src_file, self.src_freq_file,100 save=True, save_file_as='src')101 print("Done.")102 print(word2vec_src.shape)103 print("Reading target word embeddings...")104 word2vec_tgt, _ = self.save_word_vectors(self.tgt_file, self.tgt_freq_file,105 save=True, save_file_as='tgt')106 print("Done.")107 print(word2vec_tgt.shape)108 print("Reading validation file...")109 self.save_validation_set(self.validation_file, save=True)110 print("Done.")111 print("Constructing source word-id map...")112 self.save_word_ids_dicts(self.src_file, save=True, save_file_as='src_ids')113 print("Done.")114 print("Constructing target word-id map...")115 self.save_word_ids_dicts(self.tgt_file, save=True, save_file_as='tgt_ids')116 print("Everything Done.")117 def save_word_vectors(self, emb_file, freq_file=None, save=False, save_file_as='src'):118 word2freq = defaultdict(int)119 if freq_file:120 with open(path.join(self.data_dir, freq_file), 'r', encoding='utf-8') as f:121 for line in f:122 word, freq = line.strip().split(' ')123 word2freq[word] = int(freq)124 embeddings, freqs = [], []125 print('Read ' + path.join(self.data_dir, emb_file))126 with open(path.join(self.data_dir, emb_file), 'r', encoding='utf-8') as f:127 N, D = f.readline().strip().split()128 D = int(D)129 for count, line in enumerate(f, start=1):130 row = line.strip().split(' ')131 word = row[0]132 vec = np.array(row[1:]).astype(np.float)133 if len(vec) == D:134 embeddings.append(vec)135 freqs.append(word2freq[word])136 if count == top_frequent_words:137 break138 if save:139 np.save(path.join(self.data_dir, save_file_as + '.npy'),140 np.array(embeddings))141 np.save(path.join(self.data_dir, save_file_as + '.freq.npy'),142 np.array(freqs))143 return np.array(embeddings), freqs144 def save_word_ids_dicts(self, file, save=False, save_file_as='src_ids'):145 word2id = {}146 count = 0147 with open(path.join(self.data_dir, file), 'r', encoding='utf-8') as f:148 _, dim = f.readline().strip().split()149 dim = int(dim)150 for row in f:151 split_row = row.strip().split(" ")152 vec = np.array(split_row[1:]).astype(np.float)153 if len(vec) == dim:154 word2id[split_row[0]] = count155 count += 1156 if count == self.top_frequent_words:157 break158 if save:159 np.save(self.data_dir + save_file_as + '.npy', word2id)160 return word2id161 def save_validation_set(self, file, save=False, save_file_as='validation'):162 true_dict = {}163 with open(self.data_dir + file, 'r', encoding='utf-8') as f:164 rows = f.readlines()165 for row in rows:166 split_row = row.split(" ")167 key = split_row[0]168 value = split_row[1].rstrip("\n")169 if key not in true_dict.keys():170 true_dict[key] = []171 true_dict[split_row[0]].append(value)172 if save:173 np.save(self.data_dir + save_file_as + '.npy', true_dict)174 return true_dict175def load_npy_one(data_dir, fname):176 return np.load(data_dir + fname).item()177def load_npy_two(data_dir, src_fname, tgt_fname, dict=False):178 if dict:179 x = np.load(data_dir + src_fname).item()180 y = np.load(data_dir + tgt_fname).item()181 else:182 x = np.load(data_dir + src_fname)183 y = np.load(data_dir + tgt_fname)184 return x, y185# Validation set in a dictionary form {src_wrd: [tgt_wrd_1, tgt_wrd_2, ...]}186def get_validation_set_ids(data_dir, validation_fname='validation.npy'):187 val_dict = load_npy_one(data_dir, validation_fname)188 src_ids, tgt_ids = load_npy_two(data_dir, 'src_ids.npy', 'tgt_ids.npy', dict=True)189 val_dict_ids = {}190 for src_wrd, tgt_list in val_dict.items():191 val_dict_ids[src_ids[src_wrd]] = [tgt_ids[tgt_wrd] for tgt_wrd in tgt_list192 if tgt_wrd in tgt_ids]193 return val_dict_ids194def convert_to_embeddings(emb_array):195 emb_tensor = to_tensor(emb_array)196 v, d = emb_tensor.size()197 emb = torch.nn.Embedding(v, d)198 if torch.cuda.is_available():199 emb = emb.cuda()200 emb.weight.data.copy_(emb_tensor)201 emb.weight.requires_grad = False...

Full Screen

Full Screen

Automation Testing Tutorials

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.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run SeleniumBase automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful