Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
print("Scoring Generation {}".format(self.generation))
# Score population
scores = self.score_all()
results = list(zip(scores, range(len(scores))))
results.sort(key=itemgetter(0), reverse=True)
# Report
if self.print_output:
print("Generation", self.generation, "| Best Score:", results[0][0], repr(self.population[results[0][
1]])) # prints best result
# Write the data
# Note: if using this for analysis, for reproducability it may be useful to
# pass type(opponent) for each of the opponents. This will allow verification of results post run
row = [self.generation, mean(scores), pstdev(scores), results[0][0],
repr(self.population[results[0][1]])]
self.outputer.write_row(row)
# Next Population
indices_to_keep = [p for (s, p) in results[0: self.bottleneck]]
self.subset_population(indices_to_keep)
# Add mutants of the best players
best_mutants = [p.copy() for p in self.population]
for p in best_mutants:
p.mutate()
self.population.append(p)
# Add random variants
random_params = [self.params_class(**self.params_kwargs)
for _ in range(self.bottleneck // 2)]
params_to_modify = [params.copy() for params in self.population]
def one_cigar_test(self, cigar, dist_min, dist_max):
qscores = []
for _ in range(self.trials):
q = self.model.get_qscore(cigar)
q = badread.qscore_model.qscore_char_to_val(q)
qscores.append(q)
target_mean = (dist_min + dist_max) / 2
target_stdev = math.sqrt(((dist_max - dist_min + 1) ** 2 - 1) / 12)
self.assertAlmostEqual(statistics.mean(qscores), target_mean, delta=0.5)
self.assertAlmostEqual(statistics.stdev(qscores), target_stdev, delta=0.5)
def test_gen_tsv():
output_tsv = io.StringIO()
percentiles = {'views': 60, 'global_usage_count': 10}
pop_fields = ['views', 'global_usage_count']
with open(os.path.join(RESOURCES, 'mock_popularity_dump.tsv'), 'r') as tsv:
generate_popularity_tsv(tsv, output_tsv, percentiles, pop_fields)
output_tsv.seek(0)
scores = _parse_normalized_tsv(output_tsv)
# Scores should be floats ranging from 0 to 100.
for _, score in scores.items():
assert 0 < score < 100
# The score of the third row should be the average of the first and second
assert statistics.mean([scores[0], scores[1]]) == scores[2]
def setup_method(self, method):
"""Set up things to be run when tests are started."""
self.hass = get_test_home_assistant()
self.values = [17, 20, 15.2, 5, 3.8, 9.2, 6.7, 14, 6]
self.count = len(self.values)
self.min = min(self.values)
self.max = max(self.values)
self.total = sum(self.values)
self.mean = round(sum(self.values) / len(self.values), 2)
self.median = round(statistics.median(self.values), 2)
self.deviation = round(statistics.stdev(self.values), 2)
self.variance = round(statistics.variance(self.values), 2)
self.change = round(self.values[-1] - self.values[0], 2)
self.average_change = round(self.change / (len(self.values) - 1), 2)
self.change_rate = round(self.average_change / (60 * (self.count - 1)), 2)
dice_score_bi, actual_bi, TP_bi, FN_bi, FP_bi = dice.get_score_bigrams(actual_terms)
VV_TF_IDF_Tests.total_dice_bi += dice_score_bi
VV_TF_IDF_Tests.dice_n.append(dice_score_n)
VV_TF_IDF_Tests.dice_u.append(dice_score_u)
VV_TF_IDF_Tests.dice_bi.append(dice_score_bi)
if VV_TF_IDF_Tests.n_tests > 1:
print(
f" dice_n: avg={statistics.mean(VV_TF_IDF_Tests.dice_n):0.3},"
f" std={statistics.stdev(VV_TF_IDF_Tests.dice_n):0.3}")
print(
f" dice_u: avg={statistics.mean(VV_TF_IDF_Tests.dice_u):0.3},"
f" std={statistics.stdev(VV_TF_IDF_Tests.dice_u):0.3}")
print(
f" dice_bi: avg={statistics.mean(VV_TF_IDF_Tests.dice_bi):0.3},"
f" std={statistics.stdev(VV_TF_IDF_Tests.dice_bi):0.3}")
# shall we do try as well?
VV_TF_IDF_Tests.n_tests += 1
if dice_score_u < self.dice_threshold:
tokenised_expected_terms_n = dice.expected_token_ngrams
tokenised_expected_terms_u = dice.expected_token_unigrams
tokenised_expected_terms_bi = dice.expected_token_bigrams
self.fail(
f'\n===================N-GRAMS============================\n'
f'expected: {tokenised_expected_terms_n} \n'
def test_statistics():
"""Statistics.
The statistics module calculates basic statistical properties (the mean, median,
variance, etc.) of numeric data.
"""
data = [2.75, 1.75, 1.25, 0.25, 0.5, 1.25, 3.5]
assert statistics.mean(data) == 1.6071428571428572
assert statistics.median(data) == 1.25
assert statistics.variance(data) == 1.3720238095238095
x = list(map(secfxp, x))
self.assertAlmostEqual(mpc.run(mpc.output(mean(x))), 3, delta=1)
self.assertAlmostEqual(mpc.run(mpc.output(median(x))), 3)
self.assertAlmostEqual(mpc.run(mpc.output(mode(x))), 4)
x = [1, 1, 1, 1, 2, 2, 3, 4, 4, 4, 4, 5, 6, 6, 6] * 100
random.shuffle(x)
x = list(map(lambda a: a * 2**-4, x))
x = list(map(secfxp, x))
self.assertAlmostEqual(mpc.run(mpc.output(mean(x))), (2**-4) * 10/3, delta=1)
y = [1.75, 1.25, -0.25, 0.5, 1.25, -3.5] * 5
random.shuffle(y)
x = list(map(secfxp, y))
self.assertAlmostEqual(mpc.run(mpc.output(mean(x))), statistics.mean(y), 4)
self.assertAlmostEqual(mpc.run(mpc.output(variance(x))), statistics.variance(y), 2)
self.assertAlmostEqual(mpc.run(mpc.output(stdev(x))), statistics.stdev(y), 3)
self.assertAlmostEqual(mpc.run(mpc.output(pvariance(x))), statistics.pvariance(y), 2)
self.assertAlmostEqual(mpc.run(mpc.output(pstdev(x))), statistics.pstdev(y), 3)
self.assertAlmostEqual(mpc.run(mpc.output(median(x))), statistics.median(y), 4)
x = list(map(secfxp, [1.0]*10))
self.assertAlmostEqual(mpc.run(mpc.output(mode(x))), 1)
k = mpc.options.sec_param
mpc.options.sec_param = 1 # force no privacy case
self.assertAlmostEqual(mpc.run(mpc.output(mode(x))), 1)
mpc.options.sec_param = k
latency_var = property(lambda msgs: statistics.pvariance(msgs.bundles))
latency_avg = property(lambda msgs: statistics.mean(msgs.bundles))
def test_statistics_error(self):
self.assertRaises(statistics.StatisticsError, mean, [])
self.assertRaises(statistics.StatisticsError, variance, [0])
self.assertRaises(statistics.StatisticsError, stdev, [0])
self.assertRaises(statistics.StatisticsError, pvariance, [])
self.assertRaises(statistics.StatisticsError, pstdev, [])
self.assertRaises(statistics.StatisticsError, mode, [])
self.assertRaises(statistics.StatisticsError, median, [])
def test_statistics_error(self):
self.assertRaises(statistics.StatisticsError, mean, [])
self.assertRaises(statistics.StatisticsError, variance, [0])
self.assertRaises(statistics.StatisticsError, stdev, [0])
self.assertRaises(statistics.StatisticsError, pvariance, [])
self.assertRaises(statistics.StatisticsError, pstdev, [])
self.assertRaises(statistics.StatisticsError, mode, [])
self.assertRaises(statistics.StatisticsError, median, [])