Пример #1
0
def write_audit_order(e, pbcid):

    dirpath = os.path.join(multi.ELECTIONS_ROOT, e.election_dirname, "3-audit",
                           "32-audit-orders")
    os.makedirs(dirpath, exist_ok=True)
    ds = utils.datetime_string()
    safe_pbcid = ids.filename_safe(pbcid)
    filename = os.path.join(dirpath,
                            "audit-order-" + safe_pbcid + "-" + ds + ".csv")
    with open(filename, "w") as file:
        fieldnames = [
            "Ballot order", "Collection", "Box", "Position", "Stamp",
            "Ballot id", "Comments"
        ]
        file.write(",".join(fieldnames))
        file.write("\n")
        for i, index in enumerate(e.shuffled_indices_p[pbcid]):
            bid = e.shuffled_bids_p[pbcid][i]
            file.write("{},".format(i))
            file.write("{},".format(pbcid))
            file.write("{},".format(e.boxid_pb[pbcid][bid]))
            file.write("{},".format(e.position_pb[pbcid][bid]))
            file.write("{},".format(e.stamp_pb[pbcid][bid]))
            file.write("{},".format(bid))
            file.write("{},".format(e.comments_pb[pbcid][bid]))
            file.write("\n")
Пример #2
0
def write_33_audited_votes_csv(e):
    """ Write 3-audit/33-audited-votes/audited-votes-PBCID.csv """

    dirpath = os.path.join(OpenAuditTool.ELECTIONS_ROOT,
                           e.election_dirname,
                           "3-audit",
                           "33-audited-votes")
    os.makedirs(dirpath, exist_ok=True)

    pbcids = [pbcid for cid in e.av_cpb for pbcid in e.av_cpb[cid]]
    for pbcid in pbcids:
        safe_pbcid = ids.filename_safe(pbcid)
        filename = os.path.join(dirpath, "audited-votes-" + safe_pbcid+".csv")
        with open(filename, "w") as file:
            fieldnames = ["Collection", "Ballot id", "Contest", "Selections"]
            file.write(",".join(fieldnames))
            file.write("\n")
            for cid in e.av_cpb:
                if pbcid in e.av_cpb[cid]:
                    for bid in e.av_cpb[cid][pbcid]:
                        vote = e.av_cpb[cid][pbcid][bid]
                        file.write("{},".format(pbcid))
                        file.write("{},".format(bid))
                        file.write("{},".format(cid))
                        selections = ",".join(vote)
                        file.write("{}".format(selections))
                        file.write("\n")
Пример #3
0
def process_args(e, args):

    e.election_dirname = ids.filename_safe(args.election_dirname)

    e.election_name = args.election_name

    ELECTIONS_ROOT = args.elections_root

    if args.set_audit_seed != None:
        audit.set_audit_seed(e, args.set_audit_seed)

    if args.read_election_spec:
        print("read_election_spec")
        election_spec.read_election_spec(e)

    elif args.read_reported:
        print("read_reported")
        election_spec.read_election_spec(e)
        reported.read_reported(e)

    elif args.make_audit_orders:
        print("make_audit_orders")
        audit_orders.compute_audit_orders(e)

    elif args.read_audited:
        print("read_audited--NO-OP-TBD")

    elif args.audit:
        election_spec.read_election_spec(e)
        reported.read_reported(e)
        audit.audit(e, args)
Пример #4
0
def write_21_ballot_manifests_csv(e):
                           
    dirpath = os.path.join(OpenAuditTool.ELECTIONS_ROOT,
                           e.election_dirname,
                           "2-reported",
                           "21-reported-ballot-manifests")
    os.makedirs(dirpath, exist_ok=True)

    for pbcid in e.pbcids:
        safe_pbcid = ids.filename_safe(pbcid)
        filename = os.path.join(dirpath, "manifest-"+safe_pbcid+".csv")
        with open(filename, "w") as file:
            fieldnames = ["Collection", "Box", "Position",
                          "Stamp", "Ballot id", "Number of ballots",
                          "Required Contests", "Possible Contests",
                          "Comments"]
            file.write(",".join(fieldnames))
            file.write("\n")
            for bid in e.bids_p[pbcid]:
                file.write("{},".format(pbcid))
                file.write("{},".format(e.boxid_pb[pbcid][bid]))
                file.write("{},".format(e.position_pb[pbcid][bid]))
                file.write("{},".format(e.stamp_pb[pbcid][bid]))
                file.write("{},".format(bid))
                file.write("1") # number of ballots
                file.write("{},".format(""))
                file.write("{},".format(""))
                # no comments
                file.write("\n")
Пример #5
0
def write_22_reported_cvrs_csv(e):

    dirpath = os.path.join(OpenAuditTool.ELECTIONS_ROOT,
                           e.election_dirname,
                           "2-reported",
                           "22-reported-cvrs")
    os.makedirs(dirpath, exist_ok=True)

    scanner = "scanner1"
    for pbcid in e.pbcids:
        # handle cvr pbcids
        if e.cvr_type_p[pbcid]=="CVR": 
            safe_pbcid = ids.filename_safe(pbcid)
            filename = os.path.join(dirpath,
                                    "reported-cvrs-" + safe_pbcid+".csv")
            with open(filename, "w") as file:
                fieldnames = ["Collection", "Scanner", "Ballot id",
                              "Contest", "Selections"]
                file.write(",".join(fieldnames))
                file.write("\n")
                for bid in e.bids_p[pbcid]:
                    for cid in e.cids:
                        if cid in e.rv_cpb:
                            if bid in e.rv_cpb[cid][pbcid]:
                                vote = e.rv_cpb[cid][pbcid][bid]
                                file.write("{},".format(pbcid))
                                file.write("{},".format(scanner))
                                file.write("{},".format(bid))
                                file.write("{},".format(cid))
                                file.write(",".join(vote))
                                file.write("\n")
        # handle noCVR pbcids
        else:
            assert False, "FIX: add write-out of noCVR reported cvrs."
Пример #6
0
def dispatch(e, args):

    e.election_dirname = ids.filename_safe(args.election_dirname)
    e.election_name = e.election_dirname

    dirpath = os.path.join(multi.ELECTIONS_ROOT, e.election_dirname)

    if os.path.exists(dirpath):
        warnings.warn("Erasing previous contents of directory {}.".format(dirpath))
        subdirs = ["1-election-spec",
                   "2-reported",
                   "3-audit"]
        for subdir in subdirs:
            dirpathx = os.path.join(dirpath, subdir)
            if os.path.exists(dirpathx):
                shutil.rmtree(dirpathx)
                warnings.warn("Directory {} erased.".format(dirpathx))

    if args.syn_type == '1':
        syn1.generate_syn_type_1(e, args)
    elif args.syn_type == '2':
        syn2.generate_syn_type_2(e, args)
    else:
        logger.info("Illegal syn_type:", args.syn_type)

    logger.info("  Done. Synthetic election written to: %s", dirpath)
Пример #7
0
def read_reported_cvrs(e):
    """
    Read reported votes 22-reported-cvrs/reported-cvrs-PBCID.csv.
    """

    election_pathname = os.path.join(multi.ELECTIONS_ROOT, e.election_dirname)
    specification_pathname = os.path.join(election_pathname, "2-reported",
                                          "22-reported-cvrs")
    fieldnames = [
        "Collection", "Scanner", "Ballot id", "Contest", "Selections"
    ]
    for pbcid in e.pbcids:
        safe_pbcid = ids.filename_safe(pbcid)
        filename = utils.greatest_name(specification_pathname,
                                       "reported-cvrs-" + safe_pbcid, ".csv")
        file_pathname = os.path.join(specification_pathname, filename)
        rows = csv_readers.read_csv_file(file_pathname,
                                         fieldnames,
                                         varlen=True)
        for row in rows:
            pbcid = row["Collection"]
            scanner = row["Scanner"]
            bid = row["Ballot id"]
            cid = row["Contest"]
            vote = row["Selections"]
            vote = tuple(sorted(vote))  # put vote selids into canonical order
            utils.nested_set(e.rv_cpb, [cid, pbcid, bid], vote)
            utils.nested_set(e.votes_c, [cid, vote], True)
Пример #8
0
def dispatch(e, args):

    e.election_dirname = ids.filename_safe(args.election_dirname)

    e.election_name = args.election_name

    OpenAuditTool.ELECTIONS_ROOT = args.elections_root

    if args.set_audit_seed != None:
        audit.set_audit_seed(e, args.set_audit_seed)

    if args.read_election_spec:
        logger.info("read_election_spec")
        election_spec.read_election_spec(e)

    elif args.read_reported:
        logger.info("read_reported")
        election_spec.read_election_spec(e)
        reported.read_reported(e)

    elif args.make_audit_orders:
        logger.info("make_audit_orders")
        audit_orders.compute_audit_orders(e)

    elif args.read_audited:
        logger.info("read_audited--NO-OP-TBD")

    elif args.audit:
        election_spec.read_election_spec(e)
        reported.read_reported(e)
        audit.audit(e, args)
Пример #9
0
def read_reported_ballot_manifests(e):
    """
    Read ballot manifest file 21-reported-ballot-manifests and expand rows if needed.
    """

    election_pathname = os.path.join(multi.ELECTIONS_ROOT, e.election_dirname)
    specification_pathname = os.path.join(election_pathname, "2-reported",
                                          "21-reported-ballot-manifests")
    fieldnames = [
        "Collection", "Box", "Position", "Stamp", "Ballot id",
        "Number of ballots", "Required Contests", "Possible Contests",
        "Comments"
    ]
    for pbcid in e.pbcids:
        safe_pbcid = ids.filename_safe(pbcid)
        filename = utils.greatest_name(specification_pathname,
                                       "manifest-" + safe_pbcid, ".csv")
        file_pathname = os.path.join(specification_pathname, filename)
        rows = csv_readers.read_csv_file(file_pathname,
                                         fieldnames,
                                         varlen=False)
        for row in rows:
            pbcid = row["Collection"]
            boxid = row["Box"]
            position = row["Position"]
            stamp = row["Stamp"]
            bid = row["Ballot id"]
            try:
                num = int(row["Number of ballots"])
            except ValueError:
                utils.myerror(
                    "Number {} of ballots not an integer.".format(num))
            if num <= 0:
                utils.mywarning(
                    "Number {} of ballots not positive.".format(num))
            req = row["Required Contests"]
            poss = row["Possible Contests"]
            comments = row["Comments"]

            bids = utils.count_on(bid, num)
            stamps = utils.count_on(stamp, num)
            positions = utils.count_on(position, num)

            for i in range(num):
                # utils.nested_set(e.bids_p, [pbcid, bids[i]], True)
                if pbcid not in e.bids_p:
                    e.bids_p[pbcid] = []
                e.bids_p[pbcid].append(bids[i])
                utils.nested_set(e.boxid_pb, [pbcid, bids[i]], boxid)
                utils.nested_set(e.position_pb, [pbcid, bids[i]], position[i])
                utils.nested_set(e.stamp_pb, [pbcid, bids[i]], stamps[i])
                utils.nested_set(e.required_gid_pb, [pbcid, bids[i]], req)
                utils.nested_set(e.possible_gid_pb, [pbcid, bids[i]], poss)
                utils.nested_set(e.comments_pb, [pbcid, bids[i]], comments)
Пример #10
0
def process_args(e, args):

    e.election_dirname = ids.filename_safe(args.election_dirname)
    e.election_name = e.election_dirname

    if args.syn_type == '1':                        
        syn1.generate_syn_type_1(e, args)
    elif args.syn_type == '2':
        syn2.generate_syn_type_2(e, args)
    else:
        print("Illegal syn_type:", args.syn_type)
Пример #11
0
def process_args(e, args):

    e.election_dirname = ids.filename_safe(args.election_dirname)
    e.election_name = e.election_dirname

    dirpath = os.path.join(multi.ELECTIONS_ROOT, e.election_dirname)
    if os.path.exists(dirpath):
        utils.mywarning("Existing directory {} erased.".format(dirpath))
        shutil.rmtree(dirpath)

    if args.syn_type == '1':
        syn1.generate_syn_type_1(e, args)
    elif args.syn_type == '2':
        syn2.generate_syn_type_2(e, args)
    else:
        print("Illegal syn_type:", args.syn_type)

    print("  Done. Synthetic election written to:", dirpath)
Пример #12
0
def read_audited_votes(e):
    """ 
    Read audited votes from 3-audit/33-audited-votes/audited-votes-PBCID.csv 
    """

    election_pathname = os.path.join(multi.ELECTIONS_ROOT, e.election_dirname)
    audited_votes_pathname = os.path.join(election_pathname, "3-audit",
                                          "33-audited-votes")
    for pbcid in e.pbcids:
        safe_pbcid = ids.filename_safe(pbcid)
        filename = utils.greatest_name(audited_votes_pathname,
                                       "audited-votes-" + safe_pbcid, ".csv")
        file_pathname = os.path.join(audited_votes_pathname, filename)
        fieldnames = ["Collection", "Ballot id", "Contest", "Selections"]
        rows = csv_readers.read_csv_file(file_pathname,
                                         fieldnames,
                                         varlen=True)
        for row in rows:
            pbcid = row["Collection"]
            bid = row["Ballot id"]
            cid = row["Contest"]
            vote = row["Selections"]
            utils.nested_set(e.av_cpb, [cid, pbcid, bid], vote)
Пример #13
0
def dispatch(e, args):
    e.election_dirname = ids.filename_safe(args.election_dirname)

    e.election_name = args.election_name

    e.num_winners = int(args.num_winners)
    e.max_num_it = int(args.max_num_it)
    e.sample_by_size = args.sample_by_size
    e.use_discrete_rm = args.use_discrete_rm
    e.pick_county_func = args.pick_county_func

    OpenAuditTool.ELECTIONS_ROOT = args.elections_root

    if args.set_audit_seed != None:
        audit.set_audit_seed(e, args.set_audit_seed)

    if args.read_election_spec:
        logger.info("read_election_spec")
        election_spec.read_election_spec(e)

    elif args.read_reported:
        logger.info("read_reported")
        election_spec.read_election_spec(e)
        reported.read_reported(e)

    elif args.make_audit_orders:
        logger.info("make_audit_orders")
        audit_orders.compute_audit_orders(e)

    elif args.read_audited:
        logger.info("read_audited--NO-OP-TBD")

    elif args.audit:
        election_spec.read_election_spec(e)
        reported.read_reported(e)
        audit.audit(e, args)
Пример #14
0
def test_filename_safe():

    assert ids.filename_safe("abc ") == "abc"
    assert ids.filename_safe("ab(c)d*8=_e-f+[g]h/i") == "abcd8_e-f+ghi"