Best Python code snippet using autotest_python
train_and_clf_cv.py
Source:train_and_clf_cv.py
1#!/usr/bin/env python32# -*- coding: utf-8 -*-3# author : Santosh Kesiraju4# e-mail : kcraj2[AT]gmail[DOT]com5# Date created : 30 Nov 20176# Last modified : 22 Aug 20197"""8Gaussian linear classifier or multi-class logistic regression on i-vectors.9Gaussian linear classifier with uncertainty on i-vector posterior dists.10"""11import os12import sys13import argparse14from time import time15import h5py16import numpy as np17import scipy18import sklearn19from sklearn.metrics import log_loss20from sklearn.model_selection import StratifiedKFold as SKFold21from sklearn.linear_model import LogisticRegressionCV22from glcu import GLCU23from glc import GLC24def kfold_cv_train_set(train_feats, train_labels, args):25 """ Run k-fold cross validation on train set26 Args:27 train_feats (np.ndarray): training features (n_samples x dim)28 train_labels (np.ndarray): corresponding labels29 args: args30 Returns:31 np.float64: average classification accuracy over k-folds32 np.float64: average cross-entropy loss over k-folds33 """34 skf = SKFold(n_splits=args.nf, shuffle=True, random_state=0)35 # [acc, x_entropy]36 scores = np.zeros(shape=(args.nf, 2))37 i = 038 for trn_ixs, dev_ixs in skf.split(train_feats, train_labels):39 (_, _, acc, xen), _, _ = run_clf(train_feats[trn_ixs],40 train_labels[trn_ixs],41 train_feats[dev_ixs],42 train_labels[dev_ixs], args)43 scores[i, :2] = acc, xen44 i += 145 return np.mean(scores[:, 0]), np.mean(scores[:, 1])46def run_clf(train_feats, train_labels, test_feats, test_labels, args):47 """ Train and classify using Gaussian linear classifier or48 multi-class logistic regression """49 if args.clf == 'glc':50 glc = GLC(est_prior=True)51 glc.train(train_feats, train_labels)52 train_pred = glc.predict(train_feats)53 train_prob = glc.predict(train_feats, return_probs=True)54 test_pred = glc.predict(test_feats)55 test_prob = glc.predict(test_feats, return_probs=True)56 elif args.clf == "lr":57 mclr = LogisticRegressionCV(Cs=[0.01, 0.1, 0.2, 0.5, 1.0, 10.0],58 multi_class='multinomial', cv=5,59 random_state=0, n_jobs=1,60 class_weight='balanced',61 max_iter=3000)62 mclr.fit(train_feats, train_labels)63 train_pred = mclr.predict(train_feats)64 train_prob = mclr.predict_proba(train_feats)65 test_pred = mclr.predict(test_feats)66 test_prob = mclr.predict_proba(test_feats)67 else:68 glcu = GLCU(args.trn, cov_type='diag', est_prior=True)69 glcu.train_b(train_feats, train_labels, args.bs)70 train_prob = glcu.predict_b(train_feats, return_labels=False,71 bsize=args.bs)72 train_pred = np.argmax(train_prob, axis=1)73 test_prob = glcu.predict_b(test_feats, return_labels=False,74 bsize=args.bs)75 test_pred = np.argmax(test_prob, axis=1)76 train_acc = np.mean(train_labels == train_pred) * 100.77 train_xen = log_loss(train_labels, train_prob)78 test_acc = np.mean(test_labels == test_pred) * 100.79 test_xen = log_loss(test_labels, test_prob)80 return (train_acc, train_xen, test_acc, test_xen), test_pred, test_prob81def print_and_save_scores(scores, res_f, ovr):82 """ Print and save scores """83 # print('scores:', scores.shape)84 dev_acc_ix = np.argmax(scores[:, 0])85 dev_xen_ix = np.argmin(scores[:, 1])86 print(" dev_A dev_X trn_A trn_X test_A test_X xtr_iter")87 print("acc {:.4f} {:.4f} {:.4f} {:.4f} {:.4f} \88{:.4f} {:.0f}".format(*scores[dev_acc_ix]))89 print("xen {:.4f} {:.4f} {:.4f} {:.4f} {:.4f} \90{:.4f} {:.0f}".format(*scores[dev_xen_ix]))91 header = "\ndev_acc,dev_xen,train_acc,train_xen,test_acc,test_xen,xtr_iter"92 if ovr:93 mode = 'wb'94 else:95 mode = 'ab'96 with open(res_f, mode) as fpw:97 np.savetxt(fpw, scores, fmt='%.4f', header=header)98 print("Saved to", res_f)99 with open(res_f.replace('results_', 'best_score_'), 'w') as fpw:100 np.savetxt(fpw, scores[dev_acc_ix].reshape(1, -1),101 fmt='%.4f', header=header[1:])102def run(train_h5, test_h5, max_iters, args):103 """ Train and classify for every iteration of extracted i-vector """104 # columns in each row:105 # dev_acc, dev_xen, train_acc, train_xen, test_acc, test_xen, xtr_iter106 scores = []107 train_labels = np.loadtxt(args.train_label_f, dtype=int)108 test_labels = np.loadtxt(args.train_label_f.replace("train", "test"),109 dtype=int)110 if min(train_labels) == 1:111 train_labels -= 1112 if min(test_labels) == 1:113 test_labels -= 1114 if args.final:115 args.start = max_iters116 for i in range(args.start, max_iters+1):117 if str(i) not in train_h5:118 continue119 score = [0, 0, 0, 0, 0, 0, i]120 train_feats = train_h5.get(str(i))[()]121 test_feats = test_h5.get(str(i))[()]122 if train_feats.shape[0] != train_labels.shape[0]:123 train_feats = train_feats.T124 if test_feats.shape[0] != test_labels.shape[0]:125 test_feats = test_feats.T126 if args.clf in ("glc", "lr"):127 # Get only the mean parameter from the post. dist128 dim = train_feats.shape[1] // 2129 train_feats = train_feats[:, :dim]130 test_feats = test_feats[:, :dim]131 score[:2] = kfold_cv_train_set(train_feats, train_labels, args)132 score[2:6], test_pred, test_prob = run_clf(train_feats, train_labels,133 test_feats, test_labels,134 args)135 scores.append(score)136 if args.verbose:137 print("i-vec xtr no: {:4d}/{:4d}".format(i, max_iters), end=" ")138 print("{:.2f} {:.4f} {:.2f} {:.4f} {:.2f} \139{:.4f} {:.0f}".format(*score))140 return np.asarray(scores), np.asarray(test_pred, dtype=int), test_prob141def main():142 """ main method """143 stime = time()144 args = parse_arguments()145 train_ivecs_h5f = args.train_ivecs_h5146 if not os.path.exists(train_ivecs_h5f):147 print(train_ivecs_h5f, "not found.")148 sys.exit()149 test_ivecs_h5f = train_ivecs_h5f.replace("train_", "test_")150 if not os.path.exists(test_ivecs_h5f):151 print(test_ivecs_h5f, "not found.")152 sys.exit()153 max_iters = int(os.path.splitext(os.path.basename(154 train_ivecs_h5f))[0].split("_")[-1][1:])155 mbase = os.path.splitext(156 os.path.basename(train_ivecs_h5f))[0].split("_")[-2]157 print('max_iters :', max_iters, 'model_base:', mbase)158 try:159 train_h5f = h5py.File(train_ivecs_h5f, 'r')160 train_h5 = train_h5f.get('ivecs')161 test_h5f = h5py.File(test_ivecs_h5f, 'r')162 test_h5 = test_h5f.get('ivecs')163 # results file164 res_f = os.path.realpath(os.path.dirname(train_ivecs_h5f) + "/../")165 sfx = "_" + mbase + "_" + str(max_iters) + "_cv"166 if args.final:167 sfx += "_final"168 res_f += "/results/results_" + args.clf + sfx + ".txt"169 if os.path.exists(res_f) and args.ovr is False:170 print(res_f, 'already EXISTS.')171 sys.exit()172 scores, test_pred, test_prob = run(train_h5, test_h5, max_iters, args)173 print_and_save_scores(scores, res_f, args)174 np.savetxt(res_f.replace("results_", "test_pred_"),175 test_pred.reshape(-1, 1), fmt="%d")176 np.savetxt(res_f.replace("results_", "test_prob_"),177 test_prob, fmt="%f")178 except IOError as err:179 print(err)180 finally:181 train_h5f.close()182 test_h5f.close()183 print("== Done: {:.2f} sec ==".format(time() - stime))184def parse_arguments():185 """ parse command line args """186 parser = argparse.ArgumentParser(description=__doc__)187 parser.add_argument("train_ivecs_h5", help="path to train_ivecs.h5 file")188 parser.add_argument("train_label_f", help="path to train label file")189 parser.add_argument("clf", default="glc", type=str,190 choices=["glcu", "glc", "lr"],191 help="Choice of classifier: glcu or glc or lr")192 parser.add_argument("-trn", type=int, default=10,193 help='GLCU training iters')194 parser.add_argument("-bs", type=int, default=1500,195 help="batch size for training GLCU")196 parser.add_argument("-nf", type=int, default=5,197 help="Number of folds for k-fold CV")198 parser.add_argument("-start", type=int, default=1, help="start iter num")199 parser.add_argument("-mkl", default="1", help="MKL threads")200 parser.add_argument("--final", action="store_true",201 help="use only final iteration of emebddings")202 parser.add_argument("--ovr", action="store_true",203 help="over-write results file")204 parser.add_argument("--verbose", action="store_true", help="verbose")205 parser.add_argument("--versions", action="store_true", help="verbose")206 args = parser.parse_args()207 os.environ['OMP_NUM_THREADS'] = args.mkl208 os.environ['MKL_NUM_THREADS'] = args.mkl209 if args.versions:210 versions()211 return args212def versions():213 """ Print versions of packages """214 print("python :", sys.version)215 print("numpy :", np.__version__)216 print("scipy :", scipy.__version__)217 print("h5py :", h5py.__version__)218 print("sklearn:", sklearn.__version__)219if __name__ == "__main__":...
test_module.py
Source:test_module.py
...4run with `--ansible-host-pattern=localhost`.5"""6import pytest7@pytest.mark.parametrize("execute", [True, False])8def test_xen(ansible_module, execute):9 """ Test the xen module.10 11 """12 params = {13 "execute": execute,14 }15 result = ansible_module.xen(**params)16 host = result["localhost"]17 assert not host.get("failed", False)18 assert host["changed"] == execute19 assert host["execute"] == execute20 return21# Make the module executable.22if __name__ == "__main__":...
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!!