Beispiel #1
0
def publish_ballots_to_audit(seed, n, N, cvrs, manifest):
    """Return lists by county of ballots to audit.
    """

    county_ids = set(cvr['county_id'] for cvr in cvrs)

    ballots_to_audit = []
    for county_id in county_ids:
        county_cvrs = sorted(
            (cvr for cvr in cvrs if cvr['county_id'] == county_id),
            key=lambda cvr: "%s-%s-%s" %
            (cvr['scanner_id'], cvr['batch_id'], cvr['record_id']))
        N = len(county_cvrs)
        # n is based on auditing Regent contest.
        # TODO: perhaps calculate from margin etc
        n = 11
        seed = "01234567890123456789"

        _, new_list = sampler.generate_outputs(n, True, 0, N, seed, False)

        logging.debug("Random selections, N=%d, n=%d, seed=%s: %s" %
                      (N, n, seed, new_list))

        selected = []
        for i, cvr in enumerate(county_cvrs):
            if i in new_list:
                cvr['record_type'] = 'AUDITOR_ENTERED'
                selected.append(cvr)
                logging.debug("selected cvr %d: %s" % (i, cvr['imprinted_id']))

        ballots_to_audit.append([county_id, selected])

    return ballots_to_audit
Beispiel #2
0
    def select_ballots(self, seed, n):
        "Randomly select n ballots using Rivest's sampler library"

        N = len(self.cvr) - 1
        print("Ballot count: %d" % N)

        old_output_list, new_output_list = sampler.generate_outputs(n, True, 0, N, seed, False)

        # print new_output_list
        new_output_list = sorted(new_output_list)

        self.selected = self.cvr.iloc[new_output_list]

        # print header row, with same column names as Stark's auditTools.htm
        print('sorted_number,ballot, batch_label, which_ballot_in_batch')

        for i, (seqid, ballot) in enumerate(self.selected.iterrows()):
            # print i, ballot

            m = ballotIDre.match(ballot['BallotID'])

            batch = "%s-%s" % (m.groups()[:2])
            image = int(m.group('image'))

            print "%d,%d,%s,%d" % (i + 1, seqid, batch, (image - 10000) / 2)
Beispiel #3
0
def set_seed():
    j = request.get_json()
    if 'seed' in j:
        global audit_state
        # global seed
        # global main_contest_in_progress
        audit_state['seed'] = j['seed']

        # 'ballot_type' is a misnomer here: TODO:
        ballot_types = []
        if audit_state['audit_type_name'] == 'ri_pilot':
            ballot_types = ['ballot_polling', 'ballot_comparison']
        else:
            ballot_types = [audit_state['audit_type_name']]

        for ballot_type in ballot_types:

            [], audit_state['ballot_ids'][
                ballot_type] = sampler.generate_outputs(
                    seed=audit_state['seed'],
                    with_replacement=False,
                    n=(audit_state['num_ballots_already_sampled'] +
                       number_of_ballots_to_interpret[ballot_type]),
                    a=(audit_state['num_ballots_already_sampled'] + 1),
                    b=audit_state['total_number_of_ballots'][ballot_type],
                    skip=0)
            audit_state[
                'num_ballots_already_sampled'] += number_of_ballots_to_interpret[
                    ballot_type]
            # At least in RI we will be running them in sorted order:
            #audit_state['ballot_ids'][ballot_type] = sorted(audit_state['ballot_ids'][ballot_type])

        for contest_name in audit_state['cvrs'].keys():
            imprint_dict = {}
            for ballot_id in audit_state['ballot_ids'][contest_name]:
                imprint_dict[ballot_id] = audit_state['cvrs'][contest_name][
                    ballot_id - 1]['Serial Number']
            audit_state['imprinted_ids'][contest_name] = imprint_dict

        # This is a special case, due to running several different audit types
        #   in the 2019 RI pilot:
        # TODO: find a better place for this kind of configuration (maybe!):
        if 'ballot_polling' in audit_state['ballot_ids']:
            x = audit_state['ballot_ids']['ballot_polling']
            audit_state['ballot_ids']['ballot_polling'] = sorted(
                x[0:64]) + sorted(x[64:(64 + 8)]) + sorted(
                    x[(64 + 8):(64 + 8 + 64)]) + sorted(
                        x[(64 + 8 + 64):(64 + 8 + 64 + 64)])
        if 'ballot_comparison' in audit_state['ballot_ids']:
            x = audit_state['ballot_ids']['ballot_comparison']
            audit_state['ballot_ids']['ballot_comparison'] = sorted(
                x[0:50]) + sorted(x[50:100])

        return ''  # jsonify({'ballot_ids': audit_state['ballot_ids'][ballot_type]}) # TODO: do we want to return anything here?
    else:
        return 'Key "seed" is not present', 422
def select_ballots(seed, n, N):
    "Randomly select n of N ballots using Rivest's sampler library"

    old_output_list, new_output_list = sampler.generate_outputs(n, True, 0, N, seed, False)

    new_output_list = sorted(new_output_list)

    logging.warning("Output list: %s" % new_output_list)

    return (new_output_list)
Beispiel #5
0
def shuffle():
    """
    audit.py shuffle dirname
    Produce an audit order for this audit.
    
    Assumes that seed.js has been set, e.g. by a command
    of the form "set seed 3544523"
    """
    if not len(sys.argv) == 3:
        printstr("--- Error: incorrect number of arguments for shuffle:" +
                 str(len(sys.argv) - 1))
        printstr("--- Usage: audit.py set dirname varname value")
        return
    dirname = os.path.realpath(sys.argv[2])
    if not os.path.isdir(dirname):
        printstr("--- Error: not an existing directory:" + dirname)
        printstr("--- Usage: audit.py shuffle dirname")
        return

    contest = os.path.basename(dirname)
    printvarvalue("Contest", contest)
    printvarvalue("Contest directory", dirname)

    seed_filename = os.path.join(dirname, "seed.js")
    seed_file = open(seed_filename, "r")
    seed = json.load(seed_file)
    printvarvalue("Seed", seed)

    reported_filename = os.path.join(dirname, "reported.js")
    reported_file = open(reported_filename, "r")
    reported = json.load(reported_file)
    n = len(reported)
    printvarvalue("Number of reported ballots", n)
    skip, sample = sampler.generate_outputs(n, False, 0, n - 1, seed, 0)
    shuffled_filename = os.path.join(dirname, "shuffled.js")
    shuffled_file = open(shuffled_filename, "w")
    ids = sorted(reported.keys())
    shuffled_ids = [ids[sample[i]] for i in xrange(len(sample))]
    shuffled_file.write("{\n")
    for i, id in enumerate(shuffled_ids):
        shuffled_file.write('   "' + str(id) + '": ""')
        if i + 1 < len(shuffled_ids):
            shuffled_file.write(",")
        shuffled_file.write("\n")
    shuffled_file.write("}\n")
    shuffled_file.close()
    printvarvalue(
        "Filename for shuffled file written, and hash of shuffled file",
        shuffled_filename)
    printstr(indent + "hash:" + hash_file(shuffled_filename))