def draw_3():
    song = fm.FontProperties(fname=os.path.join(base_dir, '../simsun.ttc'), size=10.5)
    sns.set(style='ticks', palette='Set2')
    c = {'family': 'sans-serif', 'sans-serif': ['Times New Roman', 'NSimSun'], 'size': 10.5}
    rc('font', **c)
    plt.rcParams['axes.unicode_minus'] = False
    plt.figure(dpi=200)
    fig, ax = plt.subplots(num=1, figsize=(3.54, 2.26))
    plt.subplots_adjust(right=0.99, left=0.125, bottom=0.14, top=0.975)
    x1, y1 = degree_sequence(get_shanghai_subway_graph())
    p1 = ax.bar(x1, y1, width=0.35)
    x2, y2 = degree_sequence(get_chengdu_subway_graph())
    new_x2 = list()
    for i in range(0, len(x2)):
        new_x2.append(x2[i] + 0.35)
    p2 = ax.bar(new_x2, y2, width=0.35)
    ax.set_xticks([d + 0.525 for d in x1])
    ax.set_xticklabels(x1)
    ax.set_xlabel("度", fontproperties=song)
    ax.set_ylabel("比例(%)", fontproperties=song)
    ax.legend(
        [p1, p2],
        [u'上海', u'成都'],
        prop=dict(fname=os.path.join(base_dir, '../simsun.ttc'), size=10.5)
    )
    plt.show()
Esempio n. 2
0
def degree():
    G = get_chengdu_subway_graph()
    degree_sequence = sorted([d for n, d in G.degree()], reverse=True)
    degree_count = collections.Counter(degree_sequence)
    deg, cnt = zip(*degree_count.items())
    print(deg)
    print(cnt)
Esempio n. 3
0
def cal_data(shanghai=True):
    data = list()
    if shanghai:
        with open(os.path.join(base_dir, '上海BC排序.csv'), 'r',
                  encoding='utf-8') as f:
            r = csv.DictReader(f)
            for row in r:
                data.append(row['BC'])
        p = os.path.join(base_dir, '上海BC分布.csv')
        g = get_shanghai_subway_graph()
    else:
        with open(os.path.join(base_dir, 'BC排序-七号线开通后.csv'),
                  'r',
                  encoding='utf-8') as f:
            r = csv.DictReader(f)
            for row in r:
                data.append(row['BC'])
        p = os.path.join(base_dir, '成都BC分布.csv')
        g = get_chengdu_subway_graph()
    r = [0, 0, 0, 0, 0, 0]
    for i in data:
        temp = int(float(i) // 0.05)
        r[temp] += 1
    result = list()
    for i in range(0, len(r)):
        result.append({
            'num': (i + 1) * 0.05,
            'distribution': r[i] / len(g.nodes)
        })
    with open(p, 'a') as f:
        w = csv.DictWriter(f, ['num', 'distribution'])
        w.writeheader()
        w.writerows(result)
Esempio n. 4
0
def highest_bc_attack_list(G, fraction=0.0):
    nodes_num = len(G)
    remove_num = int(fraction * nodes_num)
    result = list()
    bc = bc_list(get_chengdu_subway_graph())
    for i in range(0, remove_num):
        result.append(bc[i][0])
    return result
def get_graph(fraction=0.0):
    g = get_chengdu_subway_graph()
    attack_list = largest_degree_attack_list(g, 0.4)
    protect_list = highest_bc_attack_list(g, 0.3)
    for node in attack_list:
        if node not in protect_list:
            g.remove_node(node)
    nx.write_gexf(g,
                  'chengdu_lg_0.4_attack_bc_0.3_protect.gexf'.format(fraction))
def generate_chengdu_bc_list():
    """
    生成成都BC排序的结果。
    :return:
    """
    result = bc_list(get_chengdu_subway_graph())
    l = list()
    for i in result:
        l.append({'name': i[0], 'BC': i[1]})
    with open(os.path.join(base_dir, 'BC排序-七号线开通后.csv'), 'a',
              encoding='utf-8') as f:
        w = csv.DictWriter(f, ['name', 'BC'])
        w.writeheader()
        w.writerows(l)

    result = bc_list(get_chengdu_subway_graph(with_7th_line=False))
    l = list()
    for i in result:
        l.append({'name': i[0], 'BC': i[1]})
    with open(os.path.join(base_dir, 'BC排序-七号线开通前.csv'), 'a',
              encoding='utf-8') as f:
        w = csv.DictWriter(f, ['name', 'BC'])
        w.writeheader()
        w.writerows(l)
Esempio n. 7
0
def draw(stations, name):
    pos = relative_position(stations)
    dwg = svgwrite.Drawing('../picture/{}.svg'.format(name),
                           profile='full',
                           size=('2000', '3000'))
    for i in pos:
        dwg.add(
            svgwrite.shapes.Circle(center=(i['latitude'], i['longitude']),
                                   fill='#8fddff',
                                   r=8,
                                   fill_opacity="0.5"))
        dwg.add(
            svgwrite.text.Text(
                text=i['name'],
                insert=(i['latitude'], i['longitude']),
                font_size="5",
                font_family="黑体",
                style="text-anchor: middle; dominant-baseline: central;"))
    g = get_chengdu_subway_graph()
    for e in g.edges.data():
        if (e[0] not in stations) or (e[1] not in stations):
            continue
        pos_a = get_location(e[0], pos)
        pos_b = get_location(e[1], pos)
        dwg.add(
            svgwrite.path.Path(stroke_width="1",
                               stroke_opacity="0.5",
                               stroke="#ffaa5c",
                               d="M {},{} L {},{}".format(
                                   pos_a[0], pos_a[1], pos_b[0], pos_b[1])))
        dwg.add(
            svgwrite.text.Text(
                text=e[2]['weight'],
                font_size="5",
                font_family="黑体",
                style="text-anchor: middle; dominant-baseline: central;",
                insert=((pos_a[0] + pos_b[0]) / 2, (pos_a[1] + pos_b[1]) / 2)))
    dwg.save()
            if node in core.nodes:
                result += 1
    result = result / 10.0
    result = result / len(attack_list)
    return result


def check_attack(g, attack):
    attack_name = attack.__name__.replace('_attack_list', '')
    result = list()
    fractions = list()
    for i in range(1, 25):
        fractions.append(i * d)
    for i in fractions:
        print(i)
        result.append({'fraction': i, 'result': cal_core_branch(g, attack, i)})
    with open(os.path.join(base_dir, '{}.csv'.format(attack_name)), 'a') as f:
        w = csv.DictWriter(f, ['fraction', 'result'])
        w.writeheader()
        w.writerows(result)


if __name__ == "__main__":
    g = get_chengdu_subway_graph()
    attack_list = [
        random_attack_list, largest_degree_attack_list, highest_bc_attack_list,
        highest_bt_attack_list
    ]
    for attack in attack_list:
        check_attack(g, attack)
Esempio n. 9
0
def highest_bc_attack(fraction=0.0):
    g = get_chengdu_subway_graph()
    for i in highest_bc_attack_list(g, fraction):
        g.remove_node(i)
    return g
Esempio n. 10
0
def largest_degree_attack(fraction=0.0):
    g = get_chengdu_subway_graph()
    for i in largest_degree_attack_list(g, fraction):
        g.remove_node(i)
    return g
Esempio n. 11
0
def random_attack(fraction=0.0):
    g = get_chengdu_subway_graph()
    for i in random_attack_list(g, fraction):
        g.remove_node(i)
    return g