def test_consensus_scaled(self): reports = [[0.3, 0.2, 0, 0], [0.5, 0.3, 0, 0], [0.4, 0.1, 0, 0], [0.2, 0.7, 1, 0], [0.1, 0.3, 1, 1], [0.15, 0.2, 1, 1]] scalar_event_params = [ { "scaled": True, "min": 0.1, "max": 0.5 }, { "scaled": True, "min": 0.2, "max": 0.7 }, { "scaled": False, "min": 0, "max": 1 }, { "scaled": False, "min": 0, "max": 1 }, ] oracle = Oracle(reports=reports, event_bounds=scalar_event_params) outcome = oracle.consensus() self.assertTrue(0 <= outcome["certainty"] <= 1) self.assertTrue(0 <= outcome["participation"] <= 1) self.assertAlmostEquals(outcome["certainty"], 0.362414826111, places=11)
def setUp(self): self.reports = [[1, 1, 0, 0], [1, 0, 0, 0], [1, 1, 0, 0], [1, 1, 1, 0], [0, 0, 1, 1], [0, 0, 1, 1]] self.oracle = Oracle(reports=self.reports) self.c = [1, 2, 3, np.nan, 3] self.c2 = ma.masked_array(self.c, np.isnan(self.c)) self.c3 = [2, 3, -1, 4, 0]
def test_consensus_weighted(self): reputation = np.array([[1], [1], [1], [1], [1], [1]]) oracle = Oracle(votes=self.votes, reputation=reputation) outcome = oracle.consensus() self.assertTrue(0 <= outcome["Certainty"] <= 1) self.assertTrue(0 <= outcome["Participation"] <= 1) self.assertAlmostEquals(outcome["Certainty"], 0.228237569612, places=11)
def test_consensus_weighted_scaled_nans(self): reports = np.array([[0.3, 0.2, 0, 0], [0.5, 0.3, np.nan, 0], [0.4, 0.1, 0, 0], [0.2, 0.7, 1, 0], [0.1, 0.3, 1, 1], [0.15, 0.2, 1, 1]]) scalar_event_params = [ { "scaled": True, "min": 0.1, "max": 0.5 }, { "scaled": True, "min": 0.2, "max": 0.7 }, { "scaled": False, "min": 0, "max": 1 }, { "scaled": False, "min": 0, "max": 1 }, ] oracle = Oracle(reports=reports, event_bounds=scalar_event_params, reputation=np.array([1] * 6)) outcome = oracle.consensus() self.assertTrue(0 <= outcome["certainty"] <= 1) self.assertTrue(0 <= outcome["participation"] <= 1) self.assertAlmostEquals(outcome["certainty"], 0.384073052730, places=11)
def test_consensus_array(self): oracle = Oracle(reports=np.array(self.reports)) outcome = oracle.consensus() self.assertTrue(0 <= outcome["certainty"] <= 1) self.assertTrue(0 <= outcome["participation"] <= 1) self.assertAlmostEquals(outcome["certainty"], 0.228237569613, places=11)
def test_consensus_nans(self): reports = np.array([[1, 1, 0, 0], [1, 0, 0, 0], [1, 1, np.nan, 0], [1, 1, 1, 0], [0, 0, 1, 1], [0, 0, 1, 1]]) oracle = Oracle(reports=reports) outcome = oracle.consensus() self.assertTrue(0 <= outcome["certainty"] <= 1) self.assertTrue(0 <= outcome["participation"] <= 1) self.assertAlmostEquals(outcome["certainty"], 0.28865265952, places=11)
def test_consensus_weighted(self): reputation = np.array([1, 1, 1, 1, 1, 1]) oracle = Oracle(reports=self.reports, reputation=reputation) outcome = oracle.consensus() self.assertTrue(0 <= outcome["certainty"] <= 1) self.assertTrue(0 <= outcome["participation"] <= 1) self.assertAlmostEquals(outcome["certainty"], 0.228237569612, places=11)
def test_catch(self): expected = [0, 1, 0.5, 0] actual = [ self.oracle.catch(0.4), self.oracle.catch(0.6), Oracle(reports=self.reports, catch_tolerance=0.3).catch(0.4), Oracle(reports=self.reports, catch_tolerance=0.1).catch(0.4) ] self.assertEqual(actual, expected)
def test_consensus_weighted_nans(self): reports = np.array([[1, 1, 0, 0], [1, 0, 0, 0], [1, 1, np.nan, 0], [1, 1, 1, 0], [0, 0, 1, 1], [0, 0, 1, 1]]) reputation = np.array([1, 1, 1, 1, 1, 1]) oracle = Oracle(reports=reports, reputation=reputation) outcome = oracle.consensus() # print(outcome["Agents"]["OldRep"]) # print(outcome["Agents"]["ThisRep"]) self.assertTrue(0 <= outcome["certainty"] <= 1) self.assertTrue(0 <= outcome["participation"] <= 1) self.assertAlmostEquals(outcome["certainty"], 0.28865265952, places=11)
def test_consensus_nans(self): votes = np.array([[1, 1, 0, 0], [1, 0, 0, 0], [1, 1, np.nan, 0], [1, 1, 1, 0], [0, 0, 1, 1], [0, 0, 1, 1]]) oracle = Oracle(votes=votes) outcome = oracle.consensus() self.assertTrue(0 <= outcome["Certainty"] <= 1) self.assertTrue(0 <= outcome["Participation"] <= 1) self.assertAlmostEquals(outcome["Certainty"], 0.28865265952, places=11)
def test_consensus_weighted_nans(self): votes = np.array([[1, 1, 0, 0], [1, 0, 0, 0], [1, 1, np.nan, 0], [1, 1, 1, 0], [0, 0, 1, 1], [0, 0, 1, 1]]) reputation = np.array([[1], [1], [1], [1], [1], [1]]) oracle = Oracle(votes=votes, reputation=reputation) outcome = oracle.consensus() # print(outcome["Agents"]["OldRep"]) # print(outcome["Agents"]["ThisRep"]) self.assertTrue(0 <= outcome["Certainty"] <= 1) self.assertTrue(0 <= outcome["Participation"] <= 1) self.assertAlmostEquals(outcome["Certainty"], 0.28865265952, places=11)
def test_consensus_scaled(self): votes = [[ 0.3, 0.2, 0, 0], [ 0.5, 0.3, 0, 0], [ 0.4, 0.1, 0, 0], [ 0.2, 0.7, 1, 0], [ 0.1, 0.3, 1, 1], [0.15, 0.2, 1, 1]] scalar_decision_params = [ {"scaled": True, "min": 0.1, "max": 0.5}, {"scaled": True, "min": 0.2, "max": 0.7}, {"scaled": False, "min": 0, "max": 1}, {"scaled": False, "min": 0, "max": 1}, ] oracle = Oracle(votes=votes, decision_bounds=scalar_decision_params) outcome = oracle.consensus() self.assertTrue(0 <= outcome["Certainty"] <= 1) self.assertTrue(0 <= outcome["Participation"] <= 1) self.assertAlmostEquals(outcome["Certainty"], 0.362414826111, places=11)
def test_consensus_weighted_scaled_nans(self): votes = np.array([[ 0.3, 0.2, 0, 0], [ 0.5, 0.3, np.nan, 0], [ 0.4, 0.1, 0, 0], [ 0.2, 0.7, 1, 0], [ 0.1, 0.3, 1, 1], [0.15, 0.2, 1, 1]]) scalar_decision_params = [ {"scaled": True, "min": 0.1, "max": 0.5}, {"scaled": True, "min": 0.2, "max": 0.7}, {"scaled": False, "min": 0, "max": 1}, {"scaled": False, "min": 0, "max": 1}, ] oracle = Oracle(votes=votes, decision_bounds=scalar_decision_params, reputation=np.array([[1]] * 6)) outcome = oracle.consensus() self.assertTrue(0 <= outcome["Certainty"] <= 1) self.assertTrue(0 <= outcome["Participation"] <= 1) self.assertAlmostEquals(outcome["Certainty"], 0.384073052730, places=11)
def setUp(self): self.votes = [[1, 1, 0, 0], [1, 0, 0, 0], [1, 1, 0, 0], [1, 1, 1, 0], [0, 0, 1, 1], [0, 0, 1, 1]] self.oracle = Oracle(votes=self.votes) self.c = [1, 2, 3, np.nan, 3] self.c2 = ma.masked_array(self.c, np.isnan(self.c)) self.c3 = [2, 3, -1, 4, 0]
# QID1 QID2 QID3 QID4 QID5 QID6 QID7 QID8 QID9 QID10 # Scaled 0 0 0 0 0 0 0 0 0 0 # Min 0 0 0 0 0 0 0 0 0 0 # Max 1 1 1 1 1 1 1 1 1 1 # reports = [[1, 0.0, 1.0, 1, 0.4498141, 0, 0, 1, 1, 0.7488008], # [0, 0.5, 0.5, np.nan, 0.4460967, 0, 0, 1, 0, 0.7488008], # [1, 0.0, 1.0, 1, 0.4498141, 0, 0, 1, 1, np.nan]] reports = [[1, 0.5, 0, 0], [1, 0.5, 0, 0], [1, 1, 0, 0], [1, 0.5, 0, 0], [1, 0.5, 0, 0], [1, 0.5, 0, 0], [1, 0.5, 0, 0]] num_rows = len(reports) num_cols = len(reports[0]) Results = Oracle(votes=reports).consensus() index = [] for i in range(1, num_rows + 1): index.append("Reporter " + str(i)) columns = [] for j in range(1, num_cols + 1): columns.append("E" + str(j)) df = pd.DataFrame(Results["filled"], columns=columns, index=index) df["Var1"] = df.index print df mResults = pd.melt(df, id_vars=["Var1"], var_name="Var2")
class TestConsensus(unittest.TestCase): def setUp(self): self.reports = [[1, 1, 0, 0], [1, 0, 0, 0], [1, 1, 0, 0], [1, 1, 1, 0], [0, 0, 1, 1], [0, 0, 1, 1]] self.oracle = Oracle(reports=self.reports) self.c = [1, 2, 3, np.nan, 3] self.c2 = ma.masked_array(self.c, np.isnan(self.c)) self.c3 = [2, 3, -1, 4, 0] def test_consensus(self): outcome = self.oracle.consensus() self.assertAlmostEquals(outcome["certainty"], 0.228237569613, places=11) def test_consensus_verbose(self): self.oracle = Oracle(reports=self.reports, verbose=True) outcome = self.oracle.consensus() self.assertAlmostEquals(outcome["certainty"], 0.228237569613, places=11) def test_consensus_weighted(self): reputation = np.array([1, 1, 1, 1, 1, 1]) oracle = Oracle(reports=self.reports, reputation=reputation) outcome = oracle.consensus() self.assertTrue(0 <= outcome["certainty"] <= 1) self.assertTrue(0 <= outcome["participation"] <= 1) self.assertAlmostEquals(outcome["certainty"], 0.228237569612, places=11) def test_consensus_nans(self): reports = np.array([[1, 1, 0, 0], [1, 0, 0, 0], [1, 1, np.nan, 0], [1, 1, 1, 0], [0, 0, 1, 1], [0, 0, 1, 1]]) oracle = Oracle(reports=reports) outcome = oracle.consensus() self.assertTrue(0 <= outcome["certainty"] <= 1) self.assertTrue(0 <= outcome["participation"] <= 1) self.assertAlmostEquals(outcome["certainty"], 0.28865265952, places=11) def test_consensus_weighted_nans(self): reports = np.array([[1, 1, 0, 0], [1, 0, 0, 0], [1, 1, np.nan, 0], [1, 1, 1, 0], [0, 0, 1, 1], [0, 0, 1, 1]]) reputation = np.array([1, 1, 1, 1, 1, 1]) oracle = Oracle(reports=reports, reputation=reputation) outcome = oracle.consensus() # print(outcome["Agents"]["OldRep"]) # print(outcome["Agents"]["ThisRep"]) self.assertTrue(0 <= outcome["certainty"] <= 1) self.assertTrue(0 <= outcome["participation"] <= 1) self.assertAlmostEquals(outcome["certainty"], 0.28865265952, places=11) def test_consensus_scaled(self): reports = [[0.3, 0.2, 0, 0], [0.5, 0.3, 0, 0], [0.4, 0.1, 0, 0], [0.2, 0.7, 1, 0], [0.1, 0.3, 1, 1], [0.15, 0.2, 1, 1]] scalar_event_params = [ { "scaled": True, "min": 0.1, "max": 0.5 }, { "scaled": True, "min": 0.2, "max": 0.7 }, { "scaled": False, "min": 0, "max": 1 }, { "scaled": False, "min": 0, "max": 1 }, ] oracle = Oracle(reports=reports, event_bounds=scalar_event_params) outcome = oracle.consensus() self.assertTrue(0 <= outcome["certainty"] <= 1) self.assertTrue(0 <= outcome["participation"] <= 1) self.assertAlmostEquals(outcome["certainty"], 0.362414826111, places=11) def test_consensus_scaled_nans(self): reports = np.array([[0.3, 0.2, 0, 0], [0.5, 0.3, np.nan, 0], [0.4, 0.1, 0, 0], [0.2, 0.7, 1, 0], [0.1, 0.3, 1, 1], [0.15, 0.2, 1, 1]]) scalar_event_params = [ { "scaled": True, "min": 0.1, "max": 0.5 }, { "scaled": True, "min": 0.2, "max": 0.7 }, { "scaled": False, "min": 0, "max": 1 }, { "scaled": False, "min": 0, "max": 1 }, ] oracle = Oracle(reports=reports, event_bounds=scalar_event_params) outcome = oracle.consensus() self.assertTrue(0 <= outcome["certainty"] <= 1) self.assertTrue(0 <= outcome["participation"] <= 1) self.assertAlmostEquals(outcome["certainty"], 0.384073052730, places=11) # self.assertAlmostEquals(outcome["certainty"], 0.362414826111, places=11) def test_consensus_weighted_scaled_nans(self): reports = np.array([[0.3, 0.2, 0, 0], [0.5, 0.3, np.nan, 0], [0.4, 0.1, 0, 0], [0.2, 0.7, 1, 0], [0.1, 0.3, 1, 1], [0.15, 0.2, 1, 1]]) scalar_event_params = [ { "scaled": True, "min": 0.1, "max": 0.5 }, { "scaled": True, "min": 0.2, "max": 0.7 }, { "scaled": False, "min": 0, "max": 1 }, { "scaled": False, "min": 0, "max": 1 }, ] oracle = Oracle(reports=reports, event_bounds=scalar_event_params, reputation=np.array([1] * 6)) outcome = oracle.consensus() self.assertTrue(0 <= outcome["certainty"] <= 1) self.assertTrue(0 <= outcome["participation"] <= 1) self.assertAlmostEquals(outcome["certainty"], 0.384073052730, places=11) # self.assertAlmostEquals(outcome["certainty"], 0.362414826111, places=11) def test_consensus_array(self): oracle = Oracle(reports=np.array(self.reports)) outcome = oracle.consensus() self.assertTrue(0 <= outcome["certainty"] <= 1) self.assertTrue(0 <= outcome["participation"] <= 1) self.assertAlmostEquals(outcome["certainty"], 0.228237569613, places=11) def test_consensus_masked_array(self): oracle = Oracle( reports=ma.masked_array(self.reports, np.isnan(self.reports))) outcome = oracle.consensus() self.assertTrue(0 <= outcome["certainty"] <= 1) self.assertTrue(0 <= outcome["participation"] <= 1) self.assertAlmostEquals(outcome["certainty"], 0.228237569613, places=11) def test_catch(self): expected = [0, 1, 0.5, 0] actual = [ self.oracle.catch(0.4), self.oracle.catch(0.6), Oracle(reports=self.reports, catch_tolerance=0.3).catch(0.4), Oracle(reports=self.reports, catch_tolerance=0.1).catch(0.4) ] self.assertEqual(actual, expected) def test_weighted_prin_comp(self): expected = np.array([ -0.81674714, -0.35969107, -0.81674714, -0.35969107, 1.17643821, 1.17643821 ]) actual = self.oracle.weighted_prin_comp(self.reports)[1] result = [] for i in range(len(actual)): result.append((actual[i] - expected[i])**2) self.assertLess(sum(result), 0.000000000001) def test_main(self): main() main(argv=('', '-h')) self.assertRaises(main(argv=('', '-q'))) def tearDown(self): del self.reports del self.c del self.c2 del self.c3
def test_consensus_verbose(self): self.oracle = Oracle(reports=self.reports, verbose=True) outcome = self.oracle.consensus() self.assertAlmostEquals(outcome["certainty"], 0.228237569613, places=11)
def test_consensus_verbose(self): self.oracle = Oracle(votes=self.votes, verbose=True) outcome = self.oracle.consensus() self.assertAlmostEquals(outcome["Certainty"], 0.228237569613, places=11)
class TestConsensus(unittest.TestCase): def setUp(self): self.votes = [[1, 1, 0, 0], [1, 0, 0, 0], [1, 1, 0, 0], [1, 1, 1, 0], [0, 0, 1, 1], [0, 0, 1, 1]] self.oracle = Oracle(votes=self.votes) self.c = [1, 2, 3, np.nan, 3] self.c2 = ma.masked_array(self.c, np.isnan(self.c)) self.c3 = [2, 3, -1, 4, 0] def test_consensus(self): outcome = self.oracle.consensus() self.assertAlmostEquals(outcome["Certainty"], 0.228237569613, places=11) def test_consensus_verbose(self): self.oracle = Oracle(votes=self.votes, verbose=True) outcome = self.oracle.consensus() self.assertAlmostEquals(outcome["Certainty"], 0.228237569613, places=11) def test_consensus_weighted(self): reputation = np.array([[1], [1], [1], [1], [1], [1]]) oracle = Oracle(votes=self.votes, reputation=reputation) outcome = oracle.consensus() self.assertTrue(0 <= outcome["Certainty"] <= 1) self.assertTrue(0 <= outcome["Participation"] <= 1) self.assertAlmostEquals(outcome["Certainty"], 0.228237569612, places=11) def test_consensus_nans(self): votes = np.array([[1, 1, 0, 0], [1, 0, 0, 0], [1, 1, np.nan, 0], [1, 1, 1, 0], [0, 0, 1, 1], [0, 0, 1, 1]]) oracle = Oracle(votes=votes) outcome = oracle.consensus() self.assertTrue(0 <= outcome["Certainty"] <= 1) self.assertTrue(0 <= outcome["Participation"] <= 1) self.assertAlmostEquals(outcome["Certainty"], 0.28865265952, places=11) def test_consensus_weighted_nans(self): votes = np.array([[1, 1, 0, 0], [1, 0, 0, 0], [1, 1, np.nan, 0], [1, 1, 1, 0], [0, 0, 1, 1], [0, 0, 1, 1]]) reputation = np.array([[1], [1], [1], [1], [1], [1]]) oracle = Oracle(votes=votes, reputation=reputation) outcome = oracle.consensus() # print(outcome["Agents"]["OldRep"]) # print(outcome["Agents"]["ThisRep"]) self.assertTrue(0 <= outcome["Certainty"] <= 1) self.assertTrue(0 <= outcome["Participation"] <= 1) self.assertAlmostEquals(outcome["Certainty"], 0.28865265952, places=11) def test_consensus_scaled(self): votes = [[ 0.3, 0.2, 0, 0], [ 0.5, 0.3, 0, 0], [ 0.4, 0.1, 0, 0], [ 0.2, 0.7, 1, 0], [ 0.1, 0.3, 1, 1], [0.15, 0.2, 1, 1]] scalar_decision_params = [ {"scaled": True, "min": 0.1, "max": 0.5}, {"scaled": True, "min": 0.2, "max": 0.7}, {"scaled": False, "min": 0, "max": 1}, {"scaled": False, "min": 0, "max": 1}, ] oracle = Oracle(votes=votes, decision_bounds=scalar_decision_params) outcome = oracle.consensus() self.assertTrue(0 <= outcome["Certainty"] <= 1) self.assertTrue(0 <= outcome["Participation"] <= 1) self.assertAlmostEquals(outcome["Certainty"], 0.362414826111, places=11) def test_consensus_scaled_nans(self): votes = np.array([[ 0.3, 0.2, 0, 0], [ 0.5, 0.3, np.nan, 0], [ 0.4, 0.1, 0, 0], [ 0.2, 0.7, 1, 0], [ 0.1, 0.3, 1, 1], [0.15, 0.2, 1, 1]]) scalar_decision_params = [ {"scaled": True, "min": 0.1, "max": 0.5}, {"scaled": True, "min": 0.2, "max": 0.7}, {"scaled": False, "min": 0, "max": 1}, {"scaled": False, "min": 0, "max": 1}, ] oracle = Oracle(votes=votes, decision_bounds=scalar_decision_params) outcome = oracle.consensus() self.assertTrue(0 <= outcome["Certainty"] <= 1) self.assertTrue(0 <= outcome["Participation"] <= 1) self.assertAlmostEquals(outcome["Certainty"], 0.384073052730, places=11) # self.assertAlmostEquals(outcome["Certainty"], 0.362414826111, places=11) def test_consensus_weighted_scaled_nans(self): votes = np.array([[ 0.3, 0.2, 0, 0], [ 0.5, 0.3, np.nan, 0], [ 0.4, 0.1, 0, 0], [ 0.2, 0.7, 1, 0], [ 0.1, 0.3, 1, 1], [0.15, 0.2, 1, 1]]) scalar_decision_params = [ {"scaled": True, "min": 0.1, "max": 0.5}, {"scaled": True, "min": 0.2, "max": 0.7}, {"scaled": False, "min": 0, "max": 1}, {"scaled": False, "min": 0, "max": 1}, ] oracle = Oracle(votes=votes, decision_bounds=scalar_decision_params, reputation=np.array([[1]] * 6)) outcome = oracle.consensus() self.assertTrue(0 <= outcome["Certainty"] <= 1) self.assertTrue(0 <= outcome["Participation"] <= 1) self.assertAlmostEquals(outcome["Certainty"], 0.384073052730, places=11) # self.assertAlmostEquals(outcome["Certainty"], 0.362414826111, places=11) def test_consensus_array(self): oracle = Oracle(votes=np.array(self.votes)) outcome = oracle.consensus() self.assertTrue(0 <= outcome["Certainty"] <= 1) self.assertTrue(0 <= outcome["Participation"] <= 1) self.assertAlmostEquals(outcome["Certainty"], 0.228237569613, places=11) def test_consensus_masked_array(self): oracle = Oracle(votes=ma.masked_array(self.votes, np.isnan(self.votes))) outcome = oracle.consensus() self.assertTrue(0 <= outcome["Certainty"] <= 1) self.assertTrue(0 <= outcome["Participation"] <= 1) self.assertAlmostEquals(outcome["Certainty"], 0.228237569613, places=11) def test_Catch(self): expected = [0, 1, 0.5, 0] actual = [self.oracle.Catch(0.4), self.oracle.Catch(0.6), Oracle(votes=self.votes, catch_tolerance=0.3).Catch(0.4), Oracle(votes=self.votes, catch_tolerance=0.1).Catch(0.4)] self.assertEqual(actual, expected) def test_Influence(self): expected = [np.array([0.88888889]), np.array([1.33333333]), np.array([1.0]), np.array([1.33333333])] actual = self.oracle.Influence(self.oracle.GetWeight(self.c2)) result = [] for i in range(len(actual)): result.append((actual[i] - expected[i])**2) self.assertLess(sum(result), 0.000000000001) def test_WeightedPrinComp(self): expected = np.array([-0.81674714, -0.35969107, -0.81674714, -0.35969107, 1.17643821, 1.17643821]) actual = self.oracle.WeightedPrinComp(self.votes)[1] result = [] for i in range(len(actual)): result.append((actual[i] - expected[i])**2) self.assertLess(sum(result), 0.000000000001) def test_main(self): main() main(argv=('', '-h')) self.assertRaises(main(argv=('', '-q'))) def tearDown(self): del self.votes del self.c del self.c2 del self.c3
def test_consensus_masked_array(self): oracle = Oracle(votes=ma.masked_array(self.votes, np.isnan(self.votes))) outcome = oracle.consensus() self.assertTrue(0 <= outcome["Certainty"] <= 1) self.assertTrue(0 <= outcome["Participation"] <= 1) self.assertAlmostEquals(outcome["Certainty"], 0.228237569613, places=11)