def build_edge_map(year): #read annual data with open('../resources/' + year + '_cell_data.json', 'r') as f: cells = json.load(f) bridge_cells = {cell for cell in cells if cells[cell]['bridge']} live_cells = {cell for cell in cells if cells[cell]['live']} all_cells = bridge_cells.union(live_cells) edge_map = {} for cell in all_cells: adj_cells = [str(cell) for cell in get_adj(int(cell))] edge_list = [] for adj_cell in adj_cells: if adj_cell in all_cells: edge_list.append(adj_cell) edge_map[cell] = edge_list # write edge map with open('../resources/' + year + '_edge_map.json', 'w') as f: f.write( json.dumps(edge_map, sort_keys=True, indent=4, separators=(',', ': ')))
def cells_within(cells, center_cell, cell_set, n): if n > 0: new_cells = [] for cell in cells: adj_cells = [str(c) for c in get_adj(int(cell))] for adj_cell in adj_cells: if adj_cell not in cell_set: cell_set.add(adj_cell) new_cells.append(adj_cell) return cells_within(new_cells, center_cell, cell_set, n - 1) else: cell_set.remove(center_cell) return cell_set
def add_layer(year): # read annual data with open('../resources/' + year + '_cell_data.json', 'r') as f: cells = json.load(f) live_cells = {k for k in cells if cells[k]['live']} current_border_cells = {k for k in cells if cells[k]['border']} all_cells = live_cells.union(current_border_cells) new_border_cells = set() # if current_border_cells is non-empty (only consider border cells) if current_border_cells: for cell in current_border_cells: adj_cells = [str(c) for c in get_adj(int(cell))] border_cells = { cell for cell in adj_cells if cell not in all_cells } new_border_cells = new_border_cells.union(border_cells) # if current_border_cells is empty (only consider live cells) else: for cell in live_cells: adj_cells = [str(c) for c in get_adj(int(cell))] border_cells = { cell for cell in adj_cells if cell not in live_cells } new_border_cells = new_border_cells.union(border_cells) # set flag for cell in new_border_cells: cells[str(cell)]['border'] = True # write annual data with open('../resources/' + year + '_cell_data.json', 'w') as f: f.write( json.dumps(cells, indent=4, sort_keys=True, separators=(',', ':')))
def build_live_graph(year): # read annual data with open('../resources/' + year + '_cell_data.json', 'r') as f: cells = json.load(f) live_cells = {k for k in cells if cells[k]['live']} edges = set() for cell in live_cells: adj_cells = [str(c) for c in get_adj(int(cell))] for adj_cell in adj_cells: if adj_cell in live_cells: edges.add(tuple(sorted((cell, adj_cell)))) nodes = list(live_cells) edges = list(edges) G = networkx.Graph() G.add_nodes_from(nodes) G.add_edges_from(edges) return G
delta_ranks = [] for cell in pos: delta_ranks.append(vector_data[spans[1][1]][cell]) print('Average delta_rank in all positives: ', round((sum(delta_ranks) / len(delta_ranks)), 3)) print() print('Cells within one hop of selected cells...') new_cells_to_check = set() for cell in pos: adj = [str(c) for c in get_adj(int(cell))] new_cells_to_check.add(cell) for c in adj: if c in vector_data[spans[1][1]]: new_cells_to_check.add(c) still_miss = [] for c in tgts: if c not in new_cells_to_check: still_miss.append(c) print('New results: %d for %d...' % (len(tgts) - len(still_miss), len(new_cells_to_check))) delta_ranks = []