def printResult(result): ''' Prints the given test result. ''' log.debug("Print test result: %s" % result) summary = result.get(name=SUMMARY_CHANNEL)[0].getSummary() log.info("Print summary of test execution results: %s" % summary) report = "Ran %d of %d test cases in %s" % (summary[COUNTER_TESTS_RUN], summary[COUNTER_N_TESTS], summary[COUNTER_RUN_TIME]) print '\n', report printSeparator(len(report)) if summary[STATUS_PASSED]: print "Tests passed:\t\t%d" % summary[STATUS_PASSED] if summary[STATUS_FAILED]: print "Tests failed:\t\t%d" % summary[STATUS_FAILED] if summary[STATUS_NOT_COMPLETED]: print "Tests not completed:\t%d" % summary[STATUS_NOT_COMPLETED] if summary[COUNTER_CORE_DUMPS]: print "Core dumps:\t\t%d" % summary[COUNTER_CORE_DUMPS] if summary[STATUS_ERROR]: print "Tests error:\t\t%d" % summary[STATUS_ERROR] filechls = [chl for chl in result.get(cls=channels.TestResultFileChannel) if chl.isActive()] if filechls: print "Result file:" if len(filechls) == 1 else "Result files:" for channel in filechls: print "\t%s" % channel.filePath() printSeparator() status = (1 if (summary[STATUS_FAILED] + summary[STATUS_ERROR] + summary[STATUS_NOT_COMPLETED]) else 0) exitWithStatus("FAILURE" if status else "SUCCESS", status)
def base_random_search( mask_list, n_samples, N): # mask_list defines a subspace for searching. N is the dimension. printSeparator() printSeparator() print("Running random search as the base algorithm.") x, y = samplings.batch_sampling(mask_list, n_samples, N) # Just call the batch sampling method return np.min(y) # return the y vector
def runTestCases(tests, locations, devices): ''' A function responsible for running the given test cases. ''' log.debug("Run test cases from '%s' locations using '%s' devices: %s" % (", ".join(locations), ", ".join([str(d) for d in devices]), ", ".join(tests))) loader = TestLoader() amountToRun = 0 for path in locations: location.add(path) if not isinstance(tests, (list, tuple)): tests = [tests] suites, errors = loader.loadFromNames(*tests) ncases = 0 for test in suites: ncases += test.count() print (" LOADED %d TEST CASES " % ncases).center(80, '-') + '\n' if len(errors) > 0: print "There were errors during loading test cases:" for error in errors: print "%s\n%s" % (error.name, error.traceback) printSeparator() print if not ncases: #Nothing to do exitWithStatus(status=0) log.info("Start running tests: %s" % suites) channels.add("SummaryChannel", SUMMARY_CHANNEL) result = testresult.TestResult() runner = TestRunner(devices, suites, result) for device in devices: device.connect() try: runner.start() runner.join() except KeyboardInterrupt: runner.stop() finally: for device in devices: if device.isConnected(): device.disconnect() return result
def printAccessibleDetails(accessible, attribute): ''' Prints details about the given accessible. ''' log.debug("Print details about accessible: %s" % accessible) printSeparator() for attr in _ATTRS_BASIC: name, func = attr[:2] func(accessible, name, *attr[2:]) print for attr in _ATTRS_EXTRA: name, func = attr[:2] if name != attribute and attribute != "all": continue if not func(accessible, name, *attr[2:]) and name == attribute: print "Element has no %s" % name
def printAccessibleTree(accessible): ''' Prints the given accessible tree recursively. ''' log.debug("Print accessible tree: %s" % accessible) lens = [len(attr[2 if len(attr) > 2 else 0]) for attr in _ATTRS_BASIC] _countColumnLens(accessible, lens) # Print column header row = [] for i, attr in enumerate(_ATTRS_BASIC): name = attr[2 if len(attr) > 2 else 0] row.append(name.upper().center(lens[i])) length = (sum(lens) + len(lens) - 1) printSeparator(length) print _COLUMN_SEPARATOR.join(row) printSeparator(length) # Print accessible tree log.info("Print accessible tree using column lengths: %s" % ", ".join([str(i) for i in lens])) _printAccessibleAligned(accessible, lens)
LassoSolver = linear_model.Lasso(fit_intercept=True, alpha=opt.alpha) # Lasso solver def getBasisValue(input,basis,featureIDList): # Return the value of a basis ans=0 for (weight,pos) in basis: # for every term in the basis term=weight for entry in featureIDList[pos]: # for every variable in the term term=term*input[entry] ans+=term return ans selected_degree=opt.degree for currentStage in range(opt.nStage): # Multi-stage Lasso printSeparator() print("Stage",currentStage) print("Sampling..") x,y=batch_sampling(maskList, opt.nSample, opt.N) # Get a few samples at this stage bestAnswer=min(np.min(y), bestAnswer) # Update the best answer for i in range(0,len(y)): for basis in learnedFeature: y[i]-=getBasisValue(x[i],basis,featureIDList) # Remove the linear component previously learned def get_features(x,degree): print("Extending feature vectors with degree "+str(degree)+" ..") featureExtender = PolynomialFeatures(degree, interaction_only=True) # This function generates low degree monomials. It's a little bit slow though. You may write your own function using recursion. tmp=[] for current_x in x: tmp.append(featureExtender.fit_transform(np.array(current_x).reshape(1, -1))[0].tolist()) # Extend feature vectors with monomials
def performRequest(device, options): ''' Performs a request on the given device using the specified options. :param device: A device to perform the request on :type device: tadek.connection.device.Device :param options: Options representing the request :type params: dictionary ''' log.debug("Perform a request on '%s' device using options: %s" % (device, options)) path = accessible.Path(*options.pop("path").split('/')[1:]) device.connect() try: if "action" in options: status = device.doAccessible(path, options["action"]) elif "set-text" in options: text = options["set-text"] status = device.setAccessible(path, text=text) elif "set-text-file" in options: fn = options["set-text-file"] if not os.path.isfile(fn): exitWithError("There is no such file: %s" % fn) fd = None try: fd = open(fn) text = fd.read() finally: if fd: fd.close() status = device.setAccessible(path, text=text) elif "set-value" in options: value = float(options["set-value"]) status = device.setAccessible(path, value=value) elif "mouse-click" in options: x, y = options["mouse-click"] button = options["button"] status = device.mouseEvent(path, int(x), int(y), button, "CLICK") elif "mouse-double-click" in options: x, y = options["mouse-double-click"] button = options["button"] status = device.mouseEvent(path, int(x), int(y), button, "DOUBLE_CLICK") elif "mouse-press" in options: x, y = options["mouse-press"] button = options["button"] status = device.mouseEvent(path, int(x), int(y), button, "PRESS") elif "mouse-release" in options: x, y = options["mouse-release"] button = options["button"] status = device.mouseEvent(path, int(x), int(y), button, "RELEASE") elif "mouse-absolute-motion" in options: x, y = options["mouse-absolute-motion"] status = device.mouseEvent(path, int(x), int(y), '', "ABSOLUTE_MOTION") elif "mouse-relative-motion" in options: x, y = options["mouse-relative-motion"] status = device.mouseEvent(path, int(x), int(y), '', "RELATIVE_MOTION") elif "key" in options: key = options["key"].upper() if key in constants.KEY_SYMS: key = constants.KEY_SYMS[key] elif len(key) == 1: key = ord(key) elif key.startswith("0X"): key = int(key, 16) else: key = int(key) if "modifiers" in options: modifiers = [constants.KEY_CODES[mod] for mod in options["modifiers"]] else: modifiers = [] status = device.keyboardEvent(path, key, modifiers) elif "dump" in options or "dump-all" in options: all = False if "dump" in options: depth = int(options["dump"]) else: depth = -1 if "output" in options: fn = options["output"] all = True obj = device.getAccessible(path, depth, all=all) if obj is None: exitWithStatus("There is no such path: %s" % path, 1) if all: printSeparator() utils.saveXml(obj.marshal(), fn) exitWithStatus("Dump saved to file: %s" % fn) else: printAccessibleTree(obj) exitWithStatus() else: for name in (["all"] + [attr[0] for attr in _ATTRS_EXTRA]): if name in options: break else: exitWithError("Invalid request options: %s" % options) obj = device.getAccessible(path, 0, **{name: True}) if obj is None: exitWithStatus("There is no such path: %s" % path, 1) printAccessibleDetails(obj, name) exitWithStatus() finally: if device.isConnected(): device.disconnect() printSeparator() if status: exitWithStatus("SUCCESS") else: exitWithStatus("FAILURE", 1)
def base_hyperband(mask_list, budget, N): #use hyperband to search the space printSeparator() printSeparator() print("Running hyperband as the base algorithm.") max_iter = 100 # Total iterations eta = 3.0 # hyperparameter eta def logeta(x, eta): return np.log(x) / np.log(eta) s_max = int(np.floor(logeta(max_iter, eta))) # range for hyperparameter s B = budget // (s_max + 1) # budget for each s print("B=", B) print("Smax=", s_max) s_min = 0 # minimum s=0, this case is just random search best_ans = 100000 # best answer print('WARNING! Please implement the intermediate sampling algorithm.') print( 'The current intermediate sampling algorithm is trivial. It cannot be applied to your application.' ) for s in range(s_min, s_max + 1): # For every s, do the following printSeparator() print("s=", s) n = int(np.floor( B / max_iter / (s + 1) * np.power(eta, s))) # number of initial random configurations print("n=", n) x = [] for i in range(n): x.append(samplings.mask_random_sample( mask_list, N)) # Get some random initial configurations remaining = n # The number of remaining configurations endEpoch = int(max_iter * np.power( eta, -s)) # The first time we start to remove a few configurations lastEpoch = 0 # The last time we remove a few configurations for i in range(s + 1): # for s steps print("Remaining..", remaining) print("r=", endEpoch) ########################### # Please implement the intermediate sampling algorithm for y, based on x # The current implementation is a trivial one. CANNOT BE APPLIED TO YOUR APPLICATION! y = samplings.batch_intermediate_sampling(x[:remaining], lastEpoch, endEpoch) ########################### best_ans = min(np.min(y), best_ans) # Update best answer sorted_ind = np.argsort(y) # Sort y remaining = int(np.ceil(remaining / eta)) # remove a few configurations. lastEpoch = endEpoch # Update the last epoch endEpoch = np.ceil(endEpoch * eta) # Update the end epoch if endEpoch > max_iter: endEpoch = max_iter tmpx = x[:] for j in range(0, remaining): # Only keep the best configurations tmpx[j] = x[sorted_ind[j]] x = tmpx[:] return best_ans
def performRequest(device, options): ''' Performs a request on the given device using the specified options. :param device: A device to perform the request on :type device: tadek.connection.device.Device :param options: Options representing the request :type params: dictionary ''' log.debug("Perform a request on '%s' device using options: %s" % (device, options)) path = accessible.Path(*options.pop("path").split('/')[1:]) device.connect() try: if "action" in options: status = device.doAccessible(path, options["action"]) elif "set-text" in options: text = options["set-text"] status = device.setAccessible(path, text=text) elif "set-text-file" in options: fn = options["set-text-file"] if not os.path.isfile(fn): exitWithError("There is no such file: %s" % fn) fd = None try: fd = open(fn) text = fd.read() finally: if fd: fd.close() status = device.setAccessible(path, text=text) elif "set-value" in options: value = float(options["set-value"]) status = device.setAccessible(path, value=value) elif "mouse-click" in options: x, y = options["mouse-click"] button = options["button"] status = device.mouseEvent(path, int(x), int(y), button, "CLICK") elif "mouse-double-click" in options: x, y = options["mouse-double-click"] button = options["button"] status = device.mouseEvent(path, int(x), int(y), button, "DOUBLE_CLICK") elif "mouse-press" in options: x, y = options["mouse-press"] button = options["button"] status = device.mouseEvent(path, int(x), int(y), button, "PRESS") elif "mouse-release" in options: x, y = options["mouse-release"] button = options["button"] status = device.mouseEvent(path, int(x), int(y), button, "RELEASE") elif "mouse-absolute-motion" in options: x, y = options["mouse-absolute-motion"] status = device.mouseEvent(path, int(x), int(y), '', "ABSOLUTE_MOTION") elif "mouse-relative-motion" in options: x, y = options["mouse-relative-motion"] status = device.mouseEvent(path, int(x), int(y), '', "RELATIVE_MOTION") elif "key" in options: key = options["key"].upper() if key in constants.KEY_SYMS: key = constants.KEY_SYMS[key] elif len(key) == 1: key = ord(key) elif key.startswith("0X"): key = int(key, 16) else: key = int(key) if "modifiers" in options: modifiers = [ constants.KEY_CODES[mod] for mod in options["modifiers"] ] else: modifiers = [] status = device.keyboardEvent(path, key, modifiers) elif "dump" in options or "dump-all" in options: all = False if "dump" in options: depth = int(options["dump"]) else: depth = -1 if "output" in options: fn = options["output"] all = True obj = device.getAccessible(path, depth, all=all) if obj is None: exitWithStatus("There is no such path: %s" % path, 1) if all: printSeparator() utils.saveXml(obj.marshal(), fn) exitWithStatus("Dump saved to file: %s" % fn) else: printAccessibleTree(obj) exitWithStatus() else: for name in (["all"] + [attr[0] for attr in _ATTRS_EXTRA]): if name in options: break else: exitWithError("Invalid request options: %s" % options) obj = device.getAccessible(path, 0, **{name: True}) if obj is None: exitWithStatus("There is no such path: %s" % path, 1) printAccessibleDetails(obj, name) exitWithStatus() finally: if device.isConnected(): device.disconnect() printSeparator() if status: exitWithStatus("SUCCESS") else: exitWithStatus("FAILURE", 1)