예제 #1
0
def dependencies(scoreType, POP, AREA):
    """returns the updaters dependencies for whatever scoretype
    is passed in. To be used as a checking device against the
    user-specified validators and score functions
    NOTE: this does not check efficiency/mean-median/mean-thirdian.

    :scoreType: name of function to check dependencies of
    :POP: name of population column in graph object
    :AREA: name of area column in graph object

    """
    depends = {}
    if scoreType == "areas":
        depends = {"areas": updates.Tally(AREA, alias="areas")}

    elif scoreType == "population":
        depends = {"population": updates.Tally(POP, alias="population")}

    elif scoreType == "perimeters":
        depends = {
            'boundary_nodes': updates.boundary_nodes,
            'cut_edges': updates.cut_edges,
            'cut_edges_by_part': updates.cut_edges_by_part,
            'exterior_boundaries': updates.exterior_boundaries,
            'perimeters': updates.perimeters
        }

    elif scoreType == "polsby_popper":
        depends = {
            **dependencies("areas", POP, AREA),
            **dependencies("perimeters", POP, AREA)
        }
        depends["polsby_popper"] = updates.polsby_popper_updater

    elif scoreType == "L1_reciprocal_polsby_popper":
        depends = dependencies("polsby_popper", POP, AREA)

    elif scoreType == "no_vanishing_districts":
        depends = dependencies("population", POP, AREA)

    elif scoreType == "fast_connected":
        depends = {}

    elif scoreType == "within_percent_of_ideal_population":
        depends = dependencies("population", POP, AREA)

    elif scoreType == "p_value":
        depends = {
            "mean_median": scores.mean_median,
            "mean_thirdian": scores.mean_thirdian
        }

    return depends
예제 #2
0
def read_basic_config(configFileName):
    """Reads basic configuration file and sets up a chain run

    :configFileName: relative path to config file
    :returns: Partition instance and MarkovChain instance

    """
    # set up the config file parser
    config = configparser.ConfigParser()
    config.read(configFileName)

    # SET UP GRAPH AND PARTITION SECTION
    # create graph and get global names for required graph attributes
    graph, POP, AREA, CD = gsource_gdata(config, 'GRAPH_SOURCE', 'GRAPH_DATA')

    voteDataList = vsource_vdata(graph, config, 'VOTE_DATA_SOURCE',
                                 'VOTE_DATA')
    # create a list of vote columns to update
    DataUpdaters = {v: updates.Tally(v) for v in voteDataList}
    # construct initial districting plan
    assignment = {x[0]: x[1][CD] for x in graph.nodes(data=True)}
    # set up validator functions and create Validator class instance
    validatorsUpdaters = []
    validators = []
    if config.has_section('VALIDITY') and len(list(
            config['VALIDITY'].keys())) > 0:
        validators = list(config['VALIDITY'].values())
        for i, x in enumerate(validators):
            if len(x.split(',')) == 1:
                validators[i] = getattr(valids, x)
            else:
                [y, z] = x.split(',')
                validators[i] = valids.WithinPercentRangeOfBounds(
                    getattr(valids, y), z)
        validatorsUpdaters.extend(
            [x.split(',')[0] for x in config['VALIDITY'].values()])

    validators = valids.Validator(validators)
    # add updaters required by this list of validators to list of updaters
    for x in validatorsUpdaters:
        DataUpdaters.update(dependencies(x, POP, AREA))
    # END SET UP GRAPH AND PARTITION SECTION

    # SET UP MARKOVCHAIN RUN SECTION
    # set up parameters for markovchain run
    chainparams = config['MARKOV_CHAIN']
    # number of steps to run
    num_steps = 1000
    if 'num_steps' in list(chainparams.keys()):
        num_steps = int(chainparams['num_steps'])
    # type of flip to use
    proposal = proposals.propose_random_flip
    if 'proposal' in list(chainparams.keys()):
        proposal = getattr(proposals, chainparams['proposal'])
    # acceptance function to use
    accept = accepts.always_accept
    if 'accept' in list(chainparams.keys()):
        accept = getattr(accepts, chainparams['accept'])
    # END SET UP MARKOVCHAIN RUN SECTION

    # SET UP DATA PROCESSOR FOR CHAIN RUN
    # get evaluation scores to compute and the columns to use for each
    escores, cfunc, elist, sVisType, outFName = escores_edata(
        config, "EVALUATION_SCORES", "EVALUATION_SCORES_DATA")

    # add evaluation scores updaters to list of updators
    for x in elist:
        DataUpdaters.update(dependencies(x, POP, AREA))

    # END SET UP DATA PROCESSOR FOR CHAIN RUN
    updaters = DataUpdaters

    # create markovchain instance
    initial_partition = Partition(graph, assignment, updaters)
    chain = MarkovChain(proposal, validators, accept, initial_partition,
                        num_steps)

    return chain, cfunc, escores, sVisType, outFName