Best Python code snippet using hypothesis
test_pareto.py
Source:test_pareto.py
1# This file is part of Hypothesis, which may be found at2# https://github.com/HypothesisWorks/hypothesis/3#4# Most of this work is copyright (C) 2013-2020 David R. MacIver5# (david@drmaciver.com), but it contains contributions by others. See6# CONTRIBUTING.rst for a full list of people who may hold copyright, and7# consult the git log if you need to determine who owns an individual8# contribution.9#10# This Source Code Form is subject to the terms of the Mozilla Public License,11# v. 2.0. If a copy of the MPL was not distributed with this file, You can12# obtain one at https://mozilla.org/MPL/2.0/.13#14# END HEADER15import pytest16from hypothesis import HealthCheck, Phase, settings17from hypothesis.database import InMemoryExampleDatabase18from hypothesis.internal.compat import int_to_bytes19from hypothesis.internal.conjecture.data import Status20from hypothesis.internal.conjecture.engine import ConjectureRunner, RunIsComplete21from hypothesis.internal.entropy import deterministic_PRNG22def test_pareto_front_contains_different_interesting_reasons():23 with deterministic_PRNG():24 def test(data):25 data.mark_interesting(data.draw_bits(4))26 runner = ConjectureRunner(27 test,28 settings=settings(29 max_examples=5000,30 database=InMemoryExampleDatabase(),31 suppress_health_check=HealthCheck.all(),32 ),33 database_key=b"stuff",34 )35 runner.run()36 assert len(runner.pareto_front) == 2 ** 437def test_database_contains_only_pareto_front():38 with deterministic_PRNG():39 def test(data):40 data.target_observations["1"] = data.draw_bits(4)41 data.draw_bits(64)42 data.target_observations["2"] = data.draw_bits(8)43 db = InMemoryExampleDatabase()44 runner = ConjectureRunner(45 test,46 settings=settings(47 max_examples=500, database=db, suppress_health_check=HealthCheck.all()48 ),49 database_key=b"stuff",50 )51 runner.run()52 assert len(runner.pareto_front) <= 50053 for v in runner.pareto_front:54 assert v.status >= Status.VALID55 assert len(db.data) == 156 (values,) = db.data.values()57 values = set(values)58 assert len(values) == len(runner.pareto_front)59 for data in runner.pareto_front:60 assert data.buffer in values61 assert data in runner.pareto_front62 for k in values:63 assert runner.cached_test_function(k) in runner.pareto_front64def test_clears_defunct_pareto_front():65 with deterministic_PRNG():66 def test(data):67 data.draw_bits(8)68 data.draw_bits(8)69 db = InMemoryExampleDatabase()70 runner = ConjectureRunner(71 test,72 settings=settings(73 max_examples=10000,74 database=db,75 suppress_health_check=HealthCheck.all(),76 phases=[Phase.reuse],77 ),78 database_key=b"stuff",79 )80 for i in range(256):81 db.save(runner.pareto_key, bytes([i, 0]))82 runner.run()83 assert len(list(db.fetch(runner.pareto_key))) == 184def test_down_samples_the_pareto_front():85 with deterministic_PRNG():86 def test(data):87 data.draw_bits(8)88 data.draw_bits(8)89 db = InMemoryExampleDatabase()90 runner = ConjectureRunner(91 test,92 settings=settings(93 max_examples=1000,94 database=db,95 suppress_health_check=HealthCheck.all(),96 phases=[Phase.reuse],97 ),98 database_key=b"stuff",99 )100 for i in range(10000):101 db.save(runner.pareto_key, int_to_bytes(i, 2))102 with pytest.raises(RunIsComplete):103 runner.reuse_existing_examples()104 assert runner.valid_examples == 1000105def test_stops_loading_pareto_front_if_interesting():106 with deterministic_PRNG():107 def test(data):108 data.draw_bits(8)109 data.draw_bits(8)110 data.mark_interesting()111 db = InMemoryExampleDatabase()112 runner = ConjectureRunner(113 test,114 settings=settings(115 max_examples=1000,116 database=db,117 suppress_health_check=HealthCheck.all(),118 phases=[Phase.reuse],119 ),120 database_key=b"stuff",121 )122 for i in range(10000):123 db.save(runner.pareto_key, int_to_bytes(i, 2))124 runner.reuse_existing_examples()125 assert runner.call_count == 1126def test_uses_tags_in_calculating_pareto_front():127 with deterministic_PRNG():128 def test(data):129 if data.draw_bits(1):130 data.start_example(11)131 data.draw_bits(8)132 data.stop_example()133 runner = ConjectureRunner(134 test,135 settings=settings(max_examples=10, database=InMemoryExampleDatabase()),136 database_key=b"stuff",137 )138 runner.run()139 assert len(runner.pareto_front) == 2140def test_optimises_the_pareto_front():141 def test(data):142 count = 0143 while data.draw_bits(8):144 count += 1145 data.target_observations[""] = min(count, 5)146 runner = ConjectureRunner(147 test,148 settings=settings(max_examples=10000, database=InMemoryExampleDatabase()),149 database_key=b"stuff",150 )151 runner.cached_test_function([255] * 20 + [0])152 runner.pareto_optimise()153 assert len(runner.pareto_front) == 6154 for i, data in enumerate(runner.pareto_front):155 assert list(data.buffer) == [1] * i + [0]156def test_does_not_optimise_the_pareto_front_if_interesting():157 def test(data):158 n = data.draw_bits(8)159 data.target_observations[""] = n160 if n == 255:161 data.mark_interesting()162 runner = ConjectureRunner(163 test,164 settings=settings(max_examples=10000, database=InMemoryExampleDatabase()),165 database_key=b"stuff",166 )167 runner.cached_test_function([0])168 runner.pareto_optimise = None169 runner.optimise_targets()170 assert runner.interesting_examples171def test_stops_optimising_once_interesting():172 hi = 2 ** 16 - 1173 def test(data):174 n = data.draw_bits(16)175 data.target_observations[""] = n176 if n < hi:177 data.mark_interesting()178 runner = ConjectureRunner(179 test,180 settings=settings(max_examples=10000, database=InMemoryExampleDatabase()),181 database_key=b"stuff",182 )183 data = runner.cached_test_function([255] * 2)184 assert data.status == Status.VALID185 runner.pareto_optimise()186 assert runner.call_count <= 20...
extract_predicted_pareto_frontier.py
Source:extract_predicted_pareto_frontier.py
1from __future__ import division2from non_dominated_sort import non_dominated_sort3import os4import csv5lessismore = {}6lessismore["results_SS-L/"] = [False, False]7lessismore["results_SS-H/"] = [False, False]8lessismore["results_SS-B/"] = [False, False]9lessismore["results_SS-J/"] = [False, True]10lessismore["results_SS-D/"] = [False, True]11lessismore["results_SS-E/"] = [False, True]12lessismore["results_SS-I/"] = [False, True]13lessismore["results_SS-K/"] = [False, True]14lessismore["results_SS-A/"] = [False, True]15lessismore["results_SS-C/"] = [False, True]16lessismore["results_SS-G/"] = [False, True]17lessismore["results_SS-F/"] = [False, True]18ePAL_data_folder = "./Data/"19raw_data_folder = "../Data/"20pareto_data_folder = "./Data/"21result_folders = [f+'/' for f in os.listdir(ePAL_data_folder) if '.md' not in f]22print result_folders23for result_folder in result_folders:24 print "--- " * 1025 print result_folder26 # generate a dict to assign objective values27 objective_dict = {}28 raw_filename = raw_data_folder + result_folder.replace('results_', '').replace('/', '') + '.csv'29 assert(os.path.isfile(raw_filename) is True), "Something is wrong"30 # Read content of the raw files from raw_data_folder31 content = open(raw_filename).readlines()32 duplicate_count = 033 for i, line in enumerate(content):34 if i == 0: continue # Skip the first line which contains headers35 line_values = map(float, [v for v in line.strip().split(',')])36 independent_values = map(int, line_values[:-2]) # Since we only consider problems with two objectives37 dependent_values = line_values[-2:] # Since we only consider problems with two objectives38 assert(len(independent_values) + len(dependent_values) == len(line_values)), "Something is wrong"39 independent_key = ",".join(map(str, independent_values))40 if independent_key in objective_dict.keys(): duplicate_count += 141 objective_dict[independent_key] = dependent_values42 # find the objective scores of the pareto front extracted by epal43 pareto_files = [pareto_data_folder + result_folder + f for f in os.listdir(pareto_data_folder + result_folder) if "prediction_error" not in f and 'stop' not in f and ".csv" in f]44 for pareto_file in pareto_files:45 print ". ",pareto_file46 pareto_content = open(pareto_file).readlines()47 assert(len(content) >= len(pareto_content)), "Something is wrong"48 predicted_pareto_front = []49 # Finding objective values for the evaluated solutions returned by ePAL50 for pc in pareto_content:51 pc_value = map(int, map(float, pc.strip().split(';')))52 pareto_key = ",".join(map(str, pc_value))53 predicted_pareto_front.append(objective_dict[pareto_key])54 assert(len(predicted_pareto_front) == len(pareto_content)), "Something is wrong"55 #56 nd_pf_indexes = non_dominated_sort(predicted_pareto_front, lessismore[result_folder])57 nd_pf = sorted([predicted_pareto_front[index] for index in nd_pf_indexes], key=lambda x:x[1], reverse=True)58 # Store Predicted Pareto Data59 pf_store_filename = "./Predicted_Frontier/" + "/".join(pareto_file.split('/')[2:])60 intermediate_folders = "/".join(pf_store_filename.split('/')[:-1]) + "/"61 try:62 os.makedirs(intermediate_folders)63 except:64 pass65 with open(pf_store_filename, "wb") as f:66 writer = csv.writer(f)...
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!!