Example #1
0
    def gen_ballots(self, filename):
        ballots = []
        #vote_num = 0

        with open(filename, 'r') as csvfile:
            csvfile.readline()  # Skip first line
            readCSV = csv.reader(csvfile, delimiter=',')
            i = 0
            for cvr, precinct, ballot_style, vote in readCSV:
                ballot = election.Ballot()
                ballot.set_physical_ballot_num(int(cvr))
                ballot.set_audit_seq_num(int(i))
                #vote_num = vote_num + 1
                print("CVR VALUE: " + cvr)

                self.total_ballots = self.total_ballots + 1
                if vote == 'Reject':
                    ballot.set_reported_value(self._contestants[1])
                    ballot.set_actual_value(self._contestants[1])
                    self.num_reject = self.num_reject + 1
                else:
                    ballot.set_reported_value(self._contestants[0])
                    ballot.set_actual_value(self._contestants[0])
                    self.num_approve = self.num_approve + 1

                ballots.append(ballot)
                i = i + 1

        # for i, ballot in enumerate(ballots):
        #     ballot.set_audit_seq_num(ballot.get_physical_ballot_num())

        self._ballots = ballots
        self._election.set_ballots(ballots)
Example #2
0
def make_ballot(all_contestants, interpretation, contest_id):
    ballot = WAVEelection.Ballot()
    #ballot.set_audit_seq_num(i+1)
    #ballot.get_physical_ballot_num(i+1):
    # interp {'ballot_id': 9104, 'contests': {'congress_district_1': 'David N. Cicilline', 'assembly_19': 'David M. Chenevert', 'council_at_large': 'Peter J. Bradley'}}
    # ballot.set_reported_value(yes) # for ballot comparison
    ballot.set_actual_value(
        all_contestants[interpretation['contests'][contest_id]])
    return ballot
Example #3
0
    def save_and_add_ballot(self):
        self.choose_next_ballot()
        if not self.auditedBallotValue.text().isdigit():
            pass
        elif int(self.auditedBallotValue.text()) >= self.auditTable.rowCount():
            audit_seq = int(self.auditedBallotValue.text())

            reported_value_text = self.reportedValueComboBox.currentText()
            actual_value_text = self.actualValueComboBox.currentText()

            print("Reported Value Text")
            print(reported_value_text)
            print("Actual Value Text")
            print(actual_value_text)

            if reported_value_text == "Select Candidate" or actual_value_text == "Select Candidate":
                return

            reported_value = list(
                filter(lambda x: x.get_name() == reported_value_text,
                       self._election.get_contestants()))[0]

            actual_value = list(
                filter(lambda x: x.get_name() == actual_value_text,
                       self._election.get_contestants()))[0]

            # TODO: FIX THIS JANK
            physical_seq = -1

            ballot = election.Ballot()
            ballot.set_audit_seq_num(audit_seq)
            ballot.set_physical_ballot_num(physical_seq)
            ballot.set_reported_value(reported_value)
            ballot.set_actual_value(actual_value)

            self._election.add_ballot(ballot)
            self.reload_audit_table()
            #self._audit.recompute(self._election.get_ballots(), self._election.get_reported_results())
            # self._audit.recompute(self.audited_ballots, self._election.get_reported_results())
            # self.refresh_audit_status()
        else:
            self.save_ballot()
        #TODO: IS IT OK TO RECOMPUTE AFTER JUST CLICKING THE BUTTON WITHOUT HAVING TO MESS WITH THE VALUES?
        self._audit.recompute(self.audited_ballots,
                              self._election.get_reported_results())
        self.refresh_audit_status()
        self.auditedBallotValue.setText(str(self.auditTable.rowCount()))
        self.reportedValueComboBox.setCurrentIndex(0)
        self.actualValueComboBox.setCurrentIndex(0)
Example #4
0
    def gen_ballots(self, count, error):
        sorted_results = sorted(self._results,
                                key=lambda r: r.get_percentage(),
                                reverse=True)

        ballots = []
        audit_counts = [i for i in range(count)]

        for i, result in enumerate(sorted_results):
            for j in range(int(result.get_percentage() * count)):
                current_count = choice(audit_counts)
                audit_counts.remove(current_count)

                ballot = election.Ballot()

                ballot.set_physical_ballot_num(current_count)
                ballot.set_reported_value(result.get_contestant())

                if i == 0 and j < error * result.get_percentage() * count:
                    ballot.set_actual_value(sorted_results[i +
                                                           1].get_contestant())
                else:
                    ballot.set_actual_value(result.get_contestant())

                ballots.append(ballot)

                for r in self._results:
                    if r.get_contestant() == ballot.get_reported_value():
                        r.set_votes(r.get_votes() + 1)
                        break

        shuffle(ballots)
        shuffle(ballots)
        shuffle(ballots)

        for i, ballot in enumerate(ballots):
            ballot.set_audit_seq_num(i)

        self._ballots = ballots
        self._election.set_ballots(ballots)
Example #5
0
def get_ballot_comparison_results():

    rla = WAVEaudit.Comparison()

    contest_outcomes = []
    reported_results_obj = audit_types['ballot_comparison']['reported_results']
    for results in reported_results_obj:
        contest_id = results['contest_id']
        # TODO: 'all_contests' should probably be a dict instead:
        contest = list(
            filter(lambda c: c['id'] == contest_id,
                   audit_types['ballot_comparison']['all_contests']))[0]
        contest_result = list(
            filter(lambda c: c['contest_id'] == contest_id,
                   reported_results_obj))[0]

        # TODO: also replace these lines with getting directly from CVR (is that the usual way to do it?):
        all_contestant_names = list(
            set(contest['candidates']).union({
                i['contests'][contest['id']]
                for i in audit_state['all_interpretations']
                ['ballot_comparison'] if contest['id'] in i['contests']
            }))
        all_contestant_names = add_non_candidate_choices(all_contestant_names)

        all_contestants = {
            name: make_contestant(name)
            for name in all_contestant_names
        }
        all_contestants['overvote'] = WAVEelection.Overvote()
        all_contestants['undervote'] = WAVEelection.Undervote()

        reported_choices = {k: 0 for k in all_contestant_names}
        for d in contest_result['results']:
            reported_choices[d['candidate']] += d['votes']

        assert (sum(reported_choices.values()) ==
                audit_state['total_number_of_ballots']['ballot_comparison'])

        ballot_count = sum(reported_choices.values())
        reported_results = [
            make_result(all_contestants, r) for r in contest_result['results']
        ]

        rla.init(reported_results, ballot_count, reported_choices)

        # These are, in order:
        #   - Risk Limit   (note it's: float(param[0]) / 100)
        #   - Error Inflation Factor
        #   - Expected 1-vote Overstatement Rate
        #   - Expected 2-vote Overstatement Rate
        #   - Expected 1-vote Understatement Rate
        #   - Expected 2-vote Understatement Rate
        # These values are taken from the RIWAVE tests:
        rla.set_parameters([5, 1.03905, 0.001, 0.0001, 0.001, 0.0001])

        ballots = []
        for interpretation in audit_state['all_interpretations'][
                'ballot_comparison']:
            if contest['id'] in interpretation['contests']:
                ballot = WAVEelection.Ballot()
                # TODO:
                ballot.set_actual_value(
                    all_contestants[interpretation['contests'][contest['id']]])
                ballot.set_audit_seq_num(interpretation['ballot_id'])
                # TODO: this is one way to do the ballot number but it might not be (probably isn't) the best:
                matching_cvr = audit_state['cvrs']['ballot_comparison'][
                    interpretation['ballot_id']]
                ballot.set_reported_value(
                    all_contestants[matching_cvr[contest['title']]])
                ballots.append(ballot)
        # TODO: second condition is a special case for the RI audit, where not every ballot has every contest (but they're all on the last one):
        final_ballot = (
            len(ballots) >= number_of_ballots_to_interpret['ballot_comparison']
        ) or (audit_state['ballot_ids']['ballot_comparison'][-1]
              == audit_state['all_interpretations']['ballot_comparison'][-1]
              ['ballot_id'])
        print("DEBUG: ballots: %d final ballot: %s" %
              (len(ballots), final_ballot))
        if (final_ballot):
            rla.recompute(ballots, reported_results)
        else:
            # Update reported results, but don't do stats calculations
            rla.update_reported_ballots(ballots=ballots,
                                        results=reported_results)
        status = 'Done' if final_ballot else rla.get_status()

        contest_outcomes.append({
            'status':
            status,
            'progress':
            rla.get_progress(final=final_ballot),
            'contest_id':
            contest['id'],
            'upset_prob':
            rla.upset_prob
        })

    return (jsonify({'outcomes': contest_outcomes}))