コード例 #1
0
def main(options):
    generator_module = importlib.import_module(options.module)
    m = Map.from_file(options.input_map)
    with open(options.input, 'rb') as file:
        o = pickle.load(file)
    m.districts = generator_module.get_districts(m.voters, o['triangles'], o['graph'], options.representatives, options.seed)
    m.to_file(options.output)
コード例 #2
0
def main(options):
    generator_module = importlib.import_module(options.module)
    m = Map.from_file(options.input)
    m.districts = generator_module.get_districts(m.voters,
                                                 options.representatives,
                                                 options.seed)
    m.to_file(options.output)
コード例 #3
0
def main(options):
    m = Map.from_file(options.input_map)
    with open(options.input, 'rb') as file:
        o = pickle.load(file)
    polygons = [t['polygon'] for t in o['triangles']]
    print('Counting better')
    start_time = time.time()
    populations = get_population_in_polygons(m.voters, polygons[:10])
    print(populations)
    print('Done in: ' + str(time.time() - start_time))
    print('Counting basic')
    start_time = time.time()
    populations = get_population_in_polygons_basic(m.voters, polygons[:10])
    print(populations)
    print('Done in: ' + str(time.time() - start_time))
コード例 #4
0
def main(options):
    generator_module = importlib.import_module(options.module)
    m = Map.from_file(options.input)
    triangles = generator_module.get_triangles(m.voters, options.representatives, options.seed)
    print('Forming graph')
    graph = nx.Graph()
    graph.graph['node_weight_attr'] = 'population'
    graph.add_nodes_from(list(range(len(triangles))))
    for index, triangle in enumerate(triangles):
        print(str(index + 1) + '/' + str(len(triangles)), flush=True)
        graph.nodes[index]['population'] = triangle['population']
        adj_trs = find_adjacent_triangle(index, triangles)
        for tr in adj_trs:
            graph.add_edge(index, tr)
    o = {
        'graph': graph,
        'triangles': triangles
    }
    with open(options.output + '.pickle', 'wb') as file:
        pickle.dump(o, file)
    draw_voters(m.voters)
    polygons = [t['polygon'] for t in triangles]
    draw_polygons(polygons)
    plt.savefig(options.output + '.png')
コード例 #5
0
def main(options):
    m = Map.from_file(options.input_map)
    raw_triangles = read_triangles_from_file(options.input)
    populations = get_voters_in_polygons(m.voters, raw_triangles)
    print('Counting triangles')
    triangles = []
    for i, t in enumerate(raw_triangles):
        party_distribution = defaultdict(int)
        new_voters = populations[i]
        for voter in new_voters:
            this_pref = voter.preference  # party preferences for this voter
            this_party = np.argmax(
                this_pref)  # the party most preferred by this voter
            party_distribution[this_party] += 1
        triangles.append({
            "polygon": t,
            "population": len(new_voters),
            "party_distribution": party_distribution
        })
    print('Forming graph')
    graph = nx.Graph()
    graph.graph['node_weight_attr'] = 'population'
    graph.add_nodes_from(list(range(len(triangles))))
    for index, triangle in enumerate(triangles):
        print(str(index + 1) + '/' + str(len(triangles)), flush=True)
        graph.nodes[index]['population'] = triangle['population']
        adj_trs = find_adjacent_triangle(index, triangles)
        for tr in adj_trs:
            graph.add_edge(index, tr)
    o = {'graph': graph, 'triangles': triangles}
    with open(options.output + '.pickle', 'wb') as file:
        pickle.dump(o, file)
    draw_voters(m.voters)
    polygons = [t['polygon'] for t in triangles]
    draw_polygons(polygons)
    plt.savefig(options.output + '.png')
コード例 #6
0
def main(options):
    generator_module = importlib.import_module(options.module)
    m = Map.from_file(options.input)
    triangles = generator_module.get_triangles(m.voters,
                                               options.representatives,
                                               options.seed)
    populations = get_voters_in_polygons(m.voters, triangles)
    print('Forming graph')
    graph = nx.Graph()
    graph.graph['node_weight_attr'] = 'population'
    graph.add_nodes_from(list(range(len(triangles))))
    triangles_dicts = []
    for index, triangle in enumerate(triangles):
        print(str(index + 1) + '/' + str(len(triangles)), flush=True)
        graph.nodes[index]['population'] = len(populations[index])
        party_distribution = defaultdict(int)
        for voter in populations[index]:
            this_pref = voter.preference  # party preferences for this voter
            this_party = np.argmax(
                this_pref)  # the party most preferred by this voter
            party_distribution[this_party] += 1
        triangles_dicts.append({
            "polygon": triangle,
            "population": len(populations[index]),
            "party_distribution": party_distribution
        })
        adj_trs = find_adjacent_triangle(index, triangles)
        for tr in adj_trs:
            graph.add_edge(index, tr)
    o = {'graph': graph, 'triangles': triangles_dicts}
    with open(options.output + '.pickle', 'wb') as file:
        pickle.dump(o, file)
    draw_voters(m.voters)
    polygons = [t for t in triangles]
    draw_polygons(polygons)
    plt.savefig(options.output + '.png')
コード例 #7
0
import pyglet
from election.g6.src.utils import get_population_in_polygons_basic
from election.g6.src.map import Map
import pymunk
from pymunk.vec2d import Vec2d
from pymunk.pyglet_util import DrawOptions

from shapely.geometry import Polygon, Point

from election.g6.src.trianglegenerator import naive_partition
from election.g6.src.utils import get_population_in_polygons_basic, get_population_in_polygons, save_triangles_to_file

map_path = "maps/g7/2party.map"

# load the map
m = Map.from_file(map_path)
voters = m.voters
random.seed(1234)
sampled_voters = random.sample(voters, 6666)

# ALL THE PARAMETERS
num_levels = 9
partition = naive_partition(num_levels, True)

window = pyglet.window.Window(1280, 980, vsync=False)
options = DrawOptions()

space = pymunk.Space()
space.gravity = 0, 0
space.damping = .9