Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_set_generator(self):
random = Random(SEED)
representation = ["a", "b", "c", "d", "e", "f"]
max_size = 5
variable_size = False
expected = [[0, 1, 2, 4, 5],
[0, 2, 3, 4, 5],
[0, 1, 2, 3, 5],
[1, 2, 3, 4, 5],
[0, 2, 3, 4, 5]]
for i in range(len(expected)):
candidate = set_generator(random, dict(representation=representation,
max_size=max_size,
variable_size=variable_size))
assert candidate == expected[i]
def test_multiple_chromosome_set_indel(self):
genome = MultipleChromosomeGenome(["A", "B"])
genome["A"] = [1, 2, 3, 4]
genome["B"] = [1, 5, 7, 10]
representation = list(range(10))
args = {
"A_representation": representation,
"B_representation": representation,
"A_indel_rate": 1,
"B_indel_rate": 1
}
random = Random(SEED)
new_individuals = multiple_chromosome_set_indel(random, [genome for _ in range(5)], args)
assert new_individuals[0]["A"] == OrderedSet([1, 2, 3, 4, 7])
assert new_individuals[0]["B"] == OrderedSet([1, 5, 10])
assert new_individuals[1]["A"] == OrderedSet([2, 3, 4])
assert new_individuals[1]["B"] == OrderedSet([1, 5, 7, 8, 10])
assert new_individuals[2]["A"] == OrderedSet([1, 2, 3, 4, 6])
assert new_individuals[2]["B"] == OrderedSet([1, 5, 7])
assert new_individuals[3]["A"] == OrderedSet([1, 2, 3, 4, 8])
assert new_individuals[3]["B"] == OrderedSet([0, 1, 5, 7, 10])
assert new_individuals[4]["A"] == OrderedSet([1, 2, 3, 4, 7])
assert new_individuals[4]["B"] == OrderedSet([1, 5, 7, 8, 10])
def test_random(self):
random = RandomGenerator()
for _ in range(1000):
assert random.random() >= 0
assert random.random() <= 1
def test_reaction_result(self, model):
representation = [r.id for r in model.reactions]
random = Random(SEED)
args = {"representation": representation}
solutions = BestSolutionArchive()
for _ in range(10000):
solutions.add(set_generator(random, args), random.random(), None, True, 100)
decoder = ReactionSetDecoder(representation, model)
result = TargetOptimizationResult(
model=model,
heuristic_method=None,
simulation_method=fba,
simulation_kwargs=None,
solutions=solutions,
objective_function=None,
target_type="reaction",
def test_set_n_point_crossover(self):
mom = OrderedSet([1, 3, 5, 9, 10])
dad = OrderedSet([2, 3, 7, 8])
args = {
"crossover_rate": 1.0,
"num_crossover_points": 1,
"candidate_size": 10
}
children = set_n_point_crossover(Random(SEED), [mom, dad], args)
bro = OrderedSet([1, 3, 5, 8])
sis = OrderedSet([2, 3, 7, 9, 10])
assert bro == children[0]
assert sis == children[1]
def test_multiple_chromosome_set_generator(self):
random = Random(SEED)
args = dict(keys=["test_key_1", "test_key_2"],
test_key_1_representation=["a1", "a2", "a3", "a4", "a5"],
test_key_2_representation=["b1", "b2", "b3", "b4", "b5", "b6", "b7"],
test_key_1_max_size=3,
test_key_2_max_size=5,
variable_size=False)
candidate = multiple_chromosome_set_generator(random, args)
assert len(candidate['test_key_1']) == 3
assert len(candidate['test_key_2']) == 5
def test_migrate_individuals_without_evaluation(self):
population = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
random = Random(SEED)
migrator = MultiprocessingMigrator(max_migrants=1, host=REDIS_HOST)
assert isinstance(migrator.migrants, RedisQueue)
assert migrator.max_migrants == 1
migrator(random, population, {})
assert len(migrator.migrants) == 1
migrator(random, population, {})
assert len(migrator.migrants) == 1
def test_set_mutation(self):
individual = OrderedSet([1, 3, 5, 9, 10])
representation = list(range(10))
args = {
"representation": representation,
"mutation_rate": 1.0
}
new_individuals = set_mutation(Random(SEED), [individual], args)
assert len(new_individuals[0]) == len(individual)
assert new_individuals[0] != individual
assert new_individuals[0] == [0, 2, 4, 6, 7]
def test_seeded_methods(self):
random = RandomGenerator()
random.seed(SEED)
value = random.random()
random.seed(SEED)
assert value == random.random()
random.seed(SEED)
value = random.randint(1, 10)
random.seed(SEED)
assert value == random.randint(1, 10)
random.seed(SEED)
population = [1, 2, 3, 4, 5]
value = random.sample(population, 2)
random.seed(SEED)
assert value == random.sample(population, 2)
individual = [1, 3, 5, 9, 10]
representation = list(range(10))
args = {
"representation": representation,
"indel_rate": 0.0
}
new_individuals = set_indel(Random(SEED), [individual], args)
assert len(new_individuals[0]) == len(individual)
assert new_individuals[0] == individual
args = {
"representation": representation,
"indel_rate": 1.0,
"variable_size": False
}
new_individuals = set_indel(Random(SEED), [individual], args)
assert len(new_individuals[0]) == len(individual)
assert new_individuals[0] == individual