Commit 37bc9be3 authored by Chanelle Lee's avatar Chanelle Lee
Browse files

Finished test functions for population and evidence. Not throroughly tested...

Finished test functions for population and evidence. Not throroughly tested for the random stuff but unsure how to do it better
parent 506862db
from numpy.random import choice
from operator import attrgetter
# C module written by CLL
from noComparisonSimulation import helperFunctions as hf
......@@ -19,16 +18,32 @@ class Evidence(object):
contains all the option instances
"""
def __init__(self, numOptions, evidenceRate, optionWeights):
"""
Initialising method for the Evidence class
Parameters
----------
numOptions : int
number of options system is choosing between
evidenceRate : float [0, 1]
chance of an agent receiving a piece of evidence
optionWeights : tuple of floats (sum to 1)
weights for choosing which option will be returned
"""
self.numOptions = numOptions
self.evidenceRate = evidenceRate
if not 0.0 <= evidenceRate <= 1.0:
raise ValueError("Rates must be in [0, 1]")
try:
if len(optionWeights) != numOptions:
raise ValueError("Need a weight per option")
if sum(optionWeights) != 1.0:
raise ValueError("Weights must sum to 1")
except TypeError:
raise TypeError("Passed value must be a tuple")
self.numOptions = int(numOptions)
self.evidenceRate = float(evidenceRate)
self.optionWeights = optionWeights
def receiveEvidenceBool(self):
......
import pytest
from noComparisonSimulation.evidence import *
def test_intialise_valid():
num_options = 4
evidence_rate = 0.4
option_weights = (0.5, 0.1, 0.2, 0.2)
evidence = Evidence(num_options, evidence_rate, option_weights)
assert evidence.numOptions == num_options
assert evidence.evidenceRate == evidence_rate
assert evidence.optionWeights == option_weights
@pytest.mark.parametrize("num_options, evidence_rate, option_weights", [
(3, -1, (0.5, 0.1, 0.4)),
(3, 1.3, (0.5, 0.1, 0.4)),
(3, 1.3, (0.5, 0.5)),
(3, 0.7, (0.5, 0.1, 0.2, 0.2)),
(3, 0.7, (0.5, 0.1, 0.2)),
(3, 0.7, (0.5, 0.1, 0.5)),
])
def test_initialise_invalid_values(num_options, evidence_rate, option_weights):
with pytest.raises(ValueError):
Evidence(num_options, evidence_rate, option_weights)
def test_intialise_invalid_type():
with pytest.raises(TypeError):
Evidence(3, 0.7, 0.5)
@pytest.mark.parametrize("evidence_rate, expected", [
(1, True),
(0, False)
])
def test_receiveEvidenceBool_simple(evidence_rate, expected):
evidence = Evidence(2, evidence_rate, (0.5, 0.5))
result = evidence.receiveEvidenceBool()
assert result == expected
# Not sure how to test this function further as it then just becomes a case of
# testing random.choice which seems rather pointless.
@pytest.mark.parametrize("num_options, option_weights, expected", [
(2, (1, 0), 0),
(2, (0, 1), 1),
(5, (1, 0, 0, 0, 0), 0),
(3, (0, 0, 1), 2)
])
def test_retWeightedRandomOption_simple(num_options, option_weights, expected):
evidence = Evidence(num_options, 1.0, option_weights)
result = evidence.retWeightedRandomOption()
assert result == expected
@pytest.mark.parametrize("num_options, option_weights", [
(3, (0.1, 0.3, 0.6)),
(5, (0.9, 0.1, 0.0, 0.0, 0.0)),
(2, (0.3, 0.7))
])
def test_retWeightedRandomOption_within_correct_range(num_options,
option_weights):
evidence = Evidence(num_options, 1.0, option_weights)
result = evidence.retWeightedRandomOption()
assert result in range(0, num_options)
# Not checking correct weighting to the random picking. Will test this instead
# in test_helperFunctions
......@@ -3,6 +3,7 @@ import numpy.testing
from noComparisonSimulation.population import *
from noComparisonSimulation.agent import Agent
from noComparisonSimulation.evidence import Evidence
def test_initialise_valid():
......@@ -115,19 +116,19 @@ def test_aggregationLocalised_valid():
@pytest.mark.parametrize("neighbourhoodSize, expected", [
(0, np.array([[0.6, 0.3, 0.1],
[0.0, 0.25, 0.75],
[0.1, 0.2, 0.7]])),
[0.1, 0.2, 0.7]])), # 0 == pool -> no agg
(-1, np.array([[0.6, 0.3, 0.1],
[0.0, 0.25, 0.75],
[0.1, 0.2, 0.7]])),
[0.1, 0.2, 0.7]])), # 0 > pool -> no agg
(2, np.array([[0.0, 0.5, 0.5],
[0.0, 0.0870, 0.9130],
[0.3158, 0.3158, 0.3684]])),
[0.3158, 0.3158, 0.3684]])), # pop_size > pool -> local agg
(3, np.array([[0.0, 0.2222, 0.7778],
[0.0, 0.2222, 0.7778],
[0.0, 0.2222, 0.7778]])),
[0.0, 0.2222, 0.7778]])), # pop_size == pool -> total agg
(10, np.array([[0.0, 0.2222, 0.7778],
[0.0, 0.2222, 0.7778],
[0.0, 0.2222, 0.7778]]))
[0.0, 0.2222, 0.7778]])) # pop_size < pool -> total agg
])
def test_aggregation_valid(neighbourhoodSize, expected):
population = Population(3, 3, 0.001, 1.0)
......@@ -143,3 +144,23 @@ def test_aggregation_valid(neighbourhoodSize, expected):
numpy.testing.assert_almost_equal(population.retBeliefs(),
expected,
decimal=4)
@pytest.mark.parametrize(
"num_options, distrust, evidence_rate, option_weights, expected", [
(2, 0.001, 1.0, (1.0, 0.0), np.array([0.999, 0.001])),
(2, 0.2, 1.0, (1.0, 0.0), np.array([0.8, 0.2])),
(2, 0.001, 1.0, (0.0, 1.0), np.array([0.001, 0.999])),
(3, 0.1, 1.0, (0.0, 0.0, 1.0), np.array([0.091, 0.091, 0.8181])),
(4, 0.001, 0.0, (0.1, 0.2, 0.35, 0.35),
np.array([0.25, 0.25, 0.25, 0.25]))
])
def test_receiveEvidence_simple(num_options, distrust, evidence_rate,
option_weights, expected):
population = Population(10, num_options, distrust, 1.0)
evidence = Evidence(num_options, evidence_rate, option_weights)
population.receiveEvidence(evidence)
for belief in population.retBeliefs():
numpy.testing.assert_almost_equal(belief, expected, decimal=4)
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment