In [2]:
%matplotlib inline
import networkx as nx
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
plt.rcParams['figure.figsize'] = (12, 7)
# key = {"Milan": 12591813, "Bruselas": 55965974, "Amberes": 7153308, "Cordoba": 12602098,
#        'Lerida': 12602126, 'Madrid': 12578024, 'Valencia': 12602139 , 'Sevilla': 12602104 , 'Toledo': 12602114,
#        'Lisboa': 2346573, 'Tarragona': 12602127 , 'Zaragoza':12602107 , 'Mexico': 0 , 'Alcala':12578024,
#        'Pamplona': 12578026 , 'Coimbra': 12578026, 'Barcelona': 12578026, 'Valladolid': 12578026}

In [3]:
mod_df = pd.read_csv("data/communities/no_auth_patron.csv", encoding="utf-8")
# ORDER - 0 3 6 5 7 1

In [4]:
mod = mod_df.groupby("Modularity Class").size()

In [5]:
mod.plot(kind="bar")


Out[5]:
<matplotlib.axes._subplots.AxesSubplot at 0x7f9eedb90f60>

In [6]:
mod_roles = mod_df.groupby(["Modularity Class", "role"]).size()
grouped = mod_roles.groupby(level=0)
divided = mod_roles.divide(grouped.sum())
divided = divided.unstack(level=0).fillna(0)
trans = divided.transpose()
trans.plot(kind="bar", stacked=True)


Out[6]:
<matplotlib.axes._subplots.AxesSubplot at 0x7f9eed539a20>

In [8]:
# import json
# with open("maps/western_europe_data/provinces.json", "r") as f:
#     prov = json.load(f)

In [11]:
# with open("maps/western_europe_data/provinces_with_id.json", "w") as f:
#     json.dump(prov, f)

In [9]:
# ids = {}
# for x in features:
#     props = x["properties"]
#     ids[props["woe_id"]] = props["gn_name"]

In [10]:
# ids

In [11]:
map_key = {"Milan": 12591813, "Bruselas": 22525998, "Amberes": 7153308, "Cordoba": 12602098,
       'Lerida': 12602126, 'Madrid': 12578024, 'Valencia': 12602139 , 'Sevilla': 12602104 , 'Toledo': 12602114,
       'Lisboa': 2346573, 'Tarragona': 12602127 , 'Zaragoza':12602107 , 'Mexico': 0 , 'Alcala':12578024,
       'Pamplona': 12578026 , 'Coimbra': 2346567, 'Barcelona': 12602124, 'Valladolid': 12602122,
       'Malaga': 12602102 , 'Burgos': 12602116, 'Roma': 12591802, 'Lyon': 12597185, 'Venetia': 12591860,
        'Medina': 12602122 , 'Salamanca': 12602119 , 'Paris': 12597155}

In [13]:
mod_places = mod_df.groupby(["Modularity Class", "top_place"]).size()
mod_places


Out[13]:
Modularity Class  top_place 
0                 Barcelona      3
                  Valencia       2
1                 Mexico         1
2                 Cordoba        3
                  Madrid         1
3                 Madrid         4
4                 Pamplona       5
5                 Zaragoza       9
6                 Milan          2
7                 Alcala         1
                  Amberes        1
                  Anvers         2
                  Barcelona      2
                  Madrid         5
                  Sevilla        1
                  Valladolid     5
8                 Amberes        1
                  Barcelona      1
                  Bruselas       6
                  Zaragoza       6
9                 Barcelona      3
                  Lerida         4
10                Coimbra        1
                  Lisboa        21
11                Barcelona     15
                  Madrid         1
                  Tarragona      2
                  Toledo         1
                  Valencia       5
12                Alcala         1
                  Barcelona      1
                  Madrid        28
                  Sevilla        1
                  Valladolid     5
13                Mexico         1
14                Bruselas       3
15                Valencia       4
dtype: int64

In [14]:
import csv
def write_csv(d, map_key, name):
    count = {}
    for k, v in d.items():
        id = map_key.get(k, "")
        if id:
            count.setdefault(id, 0)
            count[id] += v
    with open(name, "w") as f:
        writer = csv.writer(f)
        writer.writerow(["id", "count"])
        for k, v in count.items():
            writer.writerow([k, v])

In [15]:
mod_places[3].to_dict()


Out[15]:
{'Madrid': 4}

In [18]:
write_csv(mod_places[12].to_dict(), map_key, 'js_viz/maps/western_europe_data/tsv/no_auth_mod12.csv')

In [19]:
write_csv(mod_places[11].to_dict(), map_key, 'js_viz/maps/western_europe_data/tsv/no_auth_mod11.csv')

In [20]:
write_csv(mod_places[10].to_dict(), map_key, 'js_viz/maps/western_europe_data/tsv/no_auth_mod10.csv')

In [21]:
write_csv(mod_places[7].to_dict(), map_key, 'js_viz/maps/western_europe_data/tsv/no_auth_mod7.csv')

In [22]:
write_csv(mod_places[8].to_dict(), map_key, 'js_viz/maps/western_europe_data/tsv/no_auth_mod8.csv')

In [23]:
write_csv(mod_places[5].to_dict(), map_key, 'js_viz/maps/western_europe_data/tsv/no_auth_mod5.csv')

In [24]:
write_csv(mod_places[9].to_dict(), map_key, 'js_viz/maps/western_europe_data/tsv/no_auth_mod9.csv')

In [25]:
write_csv(mod_places[4].to_dict(), map_key, 'js_viz/maps/western_europe_data/tsv/no_auth_mod4.csv')

In [26]:
write_csv(mod_places[0].to_dict(), map_key, 'js_viz/maps/western_europe_data/tsv/no_auth_mod0.csv')

In [27]:
write_csv(mod_places[3].to_dict(), map_key, 'js_viz/maps/western_europe_data/tsv/no_auth_mod3.csv')

In [28]:
write_csv(mod_places[2].to_dict(), map_key, 'js_viz/maps/western_europe_data/tsv/no_auth_mod2.csv')

In [29]:
write_csv(mod_places[15].to_dict(), map_key, 'js_viz/maps/western_europe_data/tsv/no_auth_mod15.csv')

In [41]:
mod_places


Out[41]:
Modularity Class  top_place 
0                 Alcala         1
                  Amberes        2
                  Anvers         2
                  Barcelona      3
                  Madrid        13
                  Sevilla        1
                  Toledo         1
                  Valencia       4
                  Valladolid     6
1                 Barcelona      6
                  Bruselas       4
                  Madrid         1
                  Pamplona       5
                  Valencia       5
2                 Bruselas       4
3                 Alcala         1
                  Barcelona      6
                  Lerida         1
                  Madrid        26
                  Sevilla        2
                  Toledo         1
                  Valladolid     1
4                 Mexico         4
5                 Cordoba        3
                  Lisboa        21
                  Madrid         1
6                 Barcelona      5
                  Coimbra        1
                  Lisboa         2
                  Madrid         5
                  Milan          3
                  Tarragona      2
                  Zaragoza      10
7                 Barcelona      5
                  Bruselas       2
                  Lerida         4
                  Lisboa         1
                  Madrid         2
                  Valencia       2
                  Valladolid     4
                  Zaragoza       6
dtype: int64

In [ ]: