import pandas as pd, numpy as np, json, os
import networkx as nx
import operator
import matplotlib.pyplot as plt
%matplotlib inline
plt.style.use('seaborn-whitegrid')
m=json.loads(open('ignore/member_timelines.json','r').read())
set([i['Csoportok'] for i in m if i['Típus']=='Párt'])
{'⚛️ PP-DD - Partidul Poporului Dan Diaconescu', '⚪️ FDSN - Frontul Democrat al Salvarii Nationale', '⚪️ FSN - Frontul Salvării Naţionale', '✳️ PNTCD - Partidul Naţional Ţărănesc Creştin Democrat', '🌷 RMDSZ - Romániai Magyar Demokrata Szövetség', '🌹 PDSR - Partidul Democraţiei Sociale din România', '🌹 PSD - Partidul Social Democrat', '🌹 PSDR - Partidul Socialist Democratic din România', '🌹 PSM - Partidul Socialist al Muncii', '🍀 FER - Federaţia Ecologistă Română', '🍀 MER - Mişcarea Ecologistă din România', '🍀 PDAR - Partidul Democrat Agrar din România', '🍀 PER - Partidul Ecologist Român', '🍏 PMP - Partidul Mişcarea Populară', '👤 Independent - Independent', '👤 Minorități - Minorități', '📐 PAC - Partidul Alianţei Civice', "📐 PL'93 - Partidul Liberal", '📐 PNL - Partidul Naţional Liberal', '📐 PNL-AT - Partidul Naţional Liberal', '📐 PNL-CD - Partidul Naţional Liberal', '🔘 AUR - Alianţa pentru Unitatea Românilor', '🔘 FC - Forţa Civică', '🔘 GDC - Gruparea Democratică de Centru', '🔘 PAR - Partidul Alternativa României', '🔘 PDM - Partidul Democrat al Muncii', '🔘 PLS - Partidul Liber Schimbist', '🔘 PRNR - Partidul Reconstrucţiei Naţionale din România', '🔘 PTLDR - Partidul Tineretului Liber Democrat din România', '🔘 ULB - Uniunea Liberală "Brătianu"', '🔱 PRM - Partidul România Mare', '🔶 PD - Partidul Democrat', '🔶 PDL - Partidul Democrat Liberal', '🔷 USR - Uniunea Salvaţi România', '🕊️ ALDE - Partidul Alianţa Liberalilor şi Democraţilor', '🕊️ PC - Partidul Conservator', '🕊️ PUNR - Partidul Unităţii Naţionale Române', '🕊️ PUR-SL - Partidul Umanist din România', '🦅 UNPR - Uniunea Naţională pentru Progresul României'}
set([i['Csoportok'] for i in m if i['Típus']=='Kamara'])
--------------------------------------------------------------------------- NameError Traceback (most recent call last) <ipython-input-1-df8294f871c3> in <module> ----> 1 set([i['Csoportok'] for i in m if i['Típus']=='Kamara']) NameError: name 'm' is not defined
set([i['Csoportok'] for i in m if i['Típus']=='Bizottság'])
{'⏫ Gazdaság, ipar', '⏺️ Európa', '♿️ Egészségügy', '➿ Infrastruktúra, energia, kommunikáció', '🅿️ Parlament', '🌐 Külpolitika, diaszpóra', '🎦 Művészet, média', '🏅🅾️ Parlamenti Iroda', '🔀 Állami ügyek', '🔄 Közigazgatás, környezetvédelem', '🔠 Oktatás, tudomány, sport', '🔢 Pénzügy', '🚩⏫ Gazdaság, ipar', '🚩⏺️ Európa', '🚩✅ Választások', '🚩❎ Más', '🚩➿ Infrastruktúra, energia, kommunikáció', '🚩🅿️ Parlament', '🚩🎦 Művészet, média', '🚩💹 Költségvetés', '🚩🔀 Állami ügyek', '🚩🔄 Közigazgatás, környezetvédelem', '🚩🔠 Oktatás, tudomány, sport', '🚩🔢 Pénzügy', '🚩🚰 Mezőgazdaság, erdők', '🚩🚹 Emberi jogok, kisebbségek', '🚩🛂 Állambiztonság', '🚰 Mezőgazdaság, erdők', '🚹 Emberi jogok, kisebbségek', '🚻 Munka, szociális és családi ügyek', '🛂 Állambiztonság', '🛃 Biztonság', '🥇🅾️ Parlamenti Iroda', '🥈🅾️ Parlamenti Iroda', '🥉🅾️ Parlamenti Iroda'}
def replace_all(text, dic):
for i in dic:
text = text.replace(i, dic[i])
return text
medals={'🥈':2,'🥇':3,'🥉':1.5,'🏅':1.25,'🚩':1.5}
medals_none={i:'' for i in medals}
data={'Ország':{},'Bizottság':{},'Párt':{},'Megye':{}}
attention={}
for k,i in enumerate(m):
if k%1000==0: print(k/len(m)*100,'%')
name=i['Simple']
if i['Típus'] in list(data.keys()):
csoport=i['Csoportok']
csoport=replace_all(csoport,medals_none)
if csoport not in data[i['Típus']]:data[i['Típus']][csoport]={}
for t in pd.date_range(i['start'],i['end']):
st=str(t)[:10]
if st not in data[i['Típus']][csoport]:data[i['Típus']][csoport][st]=set()
data[i['Típus']][csoport][st].add(name)
if name not in attention:attention[name]={}
if csoport not in attention[name]:attention[name][csoport]={}
if st not in attention[name][csoport]:attention[name][csoport][st]=[]
for j in medals:
if j in i['Csoportok']:
attention[name][csoport][st].append(j)
0.0 % 2.9080757262919126 % 5.816151452583825 % 8.724227178875738 % 11.63230290516765 % 14.540378631459564 % 17.448454357751476 % 20.35653008404339 % 23.2646058103353 % 26.172681536627213 % 29.08075726291913 % 31.98883298921104 % 34.89690871550295 % 37.804984441794865 % 40.71306016808678 % 43.62113589437869 % 46.5292116206706 % 49.43728734696251 % 52.345363073254426 % 55.25343879954634 % 58.16151452583826 % 61.06959025213017 % 63.97766597842208 % 66.88574170471398 % 69.7938174310059 % 72.70189315729782 % 75.60996888358973 % 78.51804460988164 % 81.42612033617355 % 84.33419606246547 % 87.24227178875738 % 90.15034751504929 % 93.0584232413412 % 95.96649896763311 % 98.87457469392503 %
m[8]
{'start': '1997-02-01 00:00:00', 'end': '2000-07-01 00:00:00', 'Name': 'Ion DUŢU', 'duration': '1246', 'Simple': 'Dutu Ion | 1942-10-07', 'Csoportok': '🇮🇷 Irán', 'Csoportok0': '🇮🇷 Irán', 'Csoportok1': '🇮🇷 Irán', 'Típus': 'Ország', 'Medals': ''}
set([i['Medals'] for i in m])
{'', '🇺🇳 ', '🏅', '🏅🅾️ Parlamenti Iroda', '🏅🇺🇳 ', '🚩', '🥇', '🥇🅾️ Parlamenti Iroda', '🥇🇺🇳 ', '🥇🚩', '🥈', '🥈🅾️ Parlamenti Iroda', '🥈🇺🇳 ', '🥉', '🥉🅾️ Parlamenti Iroda'}
def anneal(t,p=7):
#pd.to_datetime('2020-01-01')-pd.to_datetime('1989-01-01')=11322
return (2-(pd.to_datetime('2020-01-01')-pd.to_datetime(t)).days/11322)**p
tanneal={}
anneal2020=anneal(pd.to_datetime('2020'))
for t in pd.date_range('1990','2020'):
tanneal[t]=anneal(t)/anneal2020+0.1
tanneal[str(t)[:10]]=tanneal[t]
plt.subplots(1,1,figsize=(5,3))
plt.plot(tanneal.keys(),tanneal.values())
plt.ylabel('Elavulási együttható')
plt.show()
edges={}
edges2={}
points={'Ország':1.5,'Bizottság':4,'Párt':2,'Megye':1}
for tipus in data:
for csoport in data[tipus]:
print(csoport)
for t in data[tipus][csoport]:
names=list(data[tipus][csoport][t])
for r in range(len(names)):
name=names[r]
for p in range(r+1,len(names)):
name2=names[p]
nset=tuple(np.sort([name,name2]))
v=points[tipus]
#symmetric
if nset not in edges:edges[nset]={'anneal':0,'values':0}
edges[nset]['values']+=v
edges[nset]['anneal']+=v*tanneal[t]
#asymmetric
nset=tuple(np.sort([name,name2]))
v=points[tipus]*attention[name][csoport][t]
if nset not in edges2:edges2[nset]={'weight':0}
edges2[nset]['weight']+=v*tanneal[t]
nset=tuple(np.sort([name,name2]))
v=points[tipus]*attention[name2][csoport][t]
nset=tuple(np.sort([name,name2])[::-1])
if nset not in edges2:edges2[nset]={'weight':0}
edges2[nset]['weight']+=v*tanneal[t]
🇨🇮 Elefántcsontpart 🇮🇷 Irán Más 🇬🇷 Görögország 🇲🇾 Malajzia 🇧🇬 Bulgária 🇰🇷 Dél-Korea 🇨🇦 Kanada 🇺🇳 🇲🇩 Moldova 🇦🇺 Ausztrália 🇨🇾 Ciprus 🇪🇪 Észtország 🇱🇹 Litvánia 🇮🇹 Olaszország 🇲🇰 Macedónia 🇦🇱 Albánia 🇨🇳 Kína 🇪🇸 Spanyolország 🇫🇷 Franciaország 🇮🇱 Izrael 🇱🇧 Libanon 🇲🇳 Mongólia 🇸🇪 Svédország 🇹🇭 Thaiföld 🇺🇳 Frankofónia 🇻🇪 Venezuela 🇲🇦 Marokkó 🇱🇺 Luxemburg 🇷🇺 Oroszország 🇫🇮 Finnország 🇮🇪 Írország 🇯🇵 Japán 🇧🇷 Brazília 🇨🇺 Kuba 🇭🇷 Horvátország 🇯🇴 Jordánia 🇵🇦 Panama 🇰🇼 Kuvait 🇹🇷 Törökország 🇵🇭 Fülöp-szigetek 🇵🇪 Peru 🇻🇳 Vietnam 🇲🇽 Mexikó 🇦🇿 Azerbajdzsán 🇺🇳 Kelet-Európa 🇺🇳 Fekete tenger 🇪🇨 Ecuador 🇧🇪 Belgium 🇺🇳 Közép-Európa 🇸🇾 Szíria 🇨🇭 Svájc 🇮🇳 India 🇺🇳 Európa Tanács 🇺🇳 Földközi tenger 🇱🇰 Srí Lanka 🇦🇹 Ausztria 🇨🇱 Chile 🇮🇸 Izland 🇧🇴 Bolívia 🇨🇷 Costa Rica 🇨🇿 Czechia 🇸🇮 Szlovénia 🇸🇰 Szlovákia 🇩🇿 Algéria 🇧🇦 Bosznia-Hercegovina 🇱🇻 Lettország 🇵🇹 Portugália 🇦🇲 Örményország 🇧🇭 Bahrein 🇨🇴 Kolumbia 🇬🇧 Egyesült Királyság 🇳🇱 Hollandia 🇳🇴 Norvégia 🇵🇰 Pakisztán 🇭🇺 Magyarország 🇵🇱 Lengyelország 🇩🇪 Németország 🇬🇳 Guinea 🇺🇦 Ukrajna 🇸🇬 Szingapúr 🇿🇦 Dél-Afrika 🇰🇿 Kazahsztán 🇹🇲 Türkmenisztán 🇺🇳 OSCE 🇬🇪 Grúzia 🇧🇾 Fehéroroszország 🇴🇲 Omán 🇹🇳 Tunézia 🇺🇳 NATO 🇮🇶 Irak 🇮🇩 Indonézia 🇺🇾 Uruguay 🇸🇦 Szaúd-Arábia 🇺🇸 Egyesült Államok 🇿🇲 Zambia 🇲🇹 Málta 🇪🇬 Egyiptom 🇦🇪 Egyesült Arab Emírségek 🇩🇰 Dánia 🇦🇷 Argentína 🇲🇪 Montenegró 🇨🇬 Kongó 🇸🇩 Szudán 🇶🇦 Katar 🇺🇳 Ortodoxia 🇺🇳 Délkelet-Európa 🇸🇳 Szenegál 🇺🇳 Europol 🇷🇸 Szerbia 🅿️ Parlament 🚹 Emberi jogok, kisebbségek ♿️ Egészségügy 🔠 Oktatás, tudomány, sport 🚰 Mezőgazdaság, erdők 🅾️ Parlamenti Iroda ⏺️ Európa 🌐 Külpolitika, diaszpóra 🎦 Művészet, média 🚻 Munka, szociális és családi ügyek 🛂 Állambiztonság ⏫ Gazdaság, ipar 🔀 Állami ügyek 🔢 Pénzügy 🔄 Közigazgatás, környezetvédelem ✅ Választások ➿ Infrastruktúra, energia, kommunikáció ❎ Más 💹 Költségvetés 🛃 Biztonság 🔱 PRM - Partidul România Mare ✳️ PNTCD - Partidul Naţional Ţărănesc Creştin Democrat 📐 PAC - Partidul Alianţei Civice 📐 PNL - Partidul Naţional Liberal 🔶 PDL - Partidul Democrat Liberal ⚪️ FSN - Frontul Salvării Naţionale 🔶 PD - Partidul Democrat 🌹 PSM - Partidul Socialist al Muncii 🌷 RMDSZ - Romániai Magyar Demokrata Szövetség 🌹 PSD - Partidul Social Democrat 👤 Independent - Independent 🕊️ ALDE - Partidul Alianţa Liberalilor şi Democraţilor 🌹 PDSR - Partidul Democraţiei Sociale din România 🔷 USR - Uniunea Salvaţi România 🕊️ PUR-SL - Partidul Umanist din România ⚛️ PP-DD - Partidul Poporului Dan Diaconescu 🦅 UNPR - Uniunea Naţională pentru Progresul României 📐 PNL-CD - Partidul Naţional Liberal 🕊️ PC - Partidul Conservator 🕊️ PUNR - Partidul Unităţii Naţionale Române 📐 PL'93 - Partidul Liberal ⚪️ FDSN - Frontul Democrat al Salvarii Nationale 🍏 PMP - Partidul Mişcarea Populară 🔘 PTLDR - Partidul Tineretului Liber Democrat din România 🔘 PLS - Partidul Liber Schimbist 🌹 PSDR - Partidul Socialist Democratic din România 🍀 PER - Partidul Ecologist Român 🔘 PAR - Partidul Alternativa României 🔘 FC - Forţa Civică 🍀 PDAR - Partidul Democrat Agrar din România 🔘 PRNR - Partidul Reconstrucţiei Naţionale din România 🍀 MER - Mişcarea Ecologistă din România 📐 PNL-AT - Partidul Naţional Liberal 🍀 FER - Federaţia Ecologistă Română 🔘 GDC - Gruparea Democratică de Centru 🔘 AUR - Alianţa pentru Unitatea Românilor 🔘 PDM - Partidul Democrat al Muncii 🔘 ULB - Uniunea Liberală "Brătianu" 👤 Minorități - Minorități 🇷🇴 Bukarest 🇷🇴 Bákó 🇷🇴 Fehér 🇷🇴 Temes 🇷🇴 Iași 🇷🇴 Dolj 🇷🇴 Gorj 🇷🇴 Maros 🇷🇴 Hunyad 🇷🇴 Buzău 🇷🇴 Arad 🇷🇴 Ilfov 🇷🇴 Botoșani 🇷🇴 Kolozs 🇷🇴 Konstanca 🇷🇴 Brassó 🇷🇴 Dâmbovița 🇷🇴 Argeș 🇷🇴 Krassó-Szörény 🇷🇴 Olt 🇷🇴 Teleorman 🇷🇴 Galac 🇷🇴 Neamț 🇷🇴 Brăila 🇷🇴 Prahova 🇷🇴 Tulcea 🇷🇴 Máramaros 🇷🇴 Diaszpóra 🇷🇴 Mehedinți 🇷🇴 Vaslui 🇷🇴 Suceava 🇷🇴 Szeben 🇷🇴 Bihar 🇷🇴 Ialomița 🇷🇴 Szatmár 🇷🇴 Vâlcea 🇷🇴 Călărași 🇷🇴 Kovászna 🇷🇴 Vrancea 🇷🇴 Szilágy 🇷🇴 Hargita 🇷🇴 Giurgiu 🇷🇴 Beszterce
Save
import pickle
def save_obj(obj, name ):
with open('ignore/'+ name + '.pkl', 'wb') as f:
pickle.dump(obj, f, pickle.HIGHEST_PROTOCOL)
def load_obj(name ):
with open('ignore/' + name + '.pkl', 'rb') as f:
return pickle.load(f)
save_obj(edges,'plotter_name_edges')
#edges=load_obj('plotter_name_edges')
Weighted
edges2l=[(i[0],i[1],edges2[i]) for i in edges2]
G = nx.DiGraph()
G.add_edges_from(edges2l)
nodes={}
r = nx.pagerank(G,weight='weight')
mr=np.median(list(r.values()))
for i in r:
nodes[i]={'pr':r[i]/mr}
links={}
for i in G.edges(data=True):
name=i[0]
name2=i[1]
z=i[2]['weight']
if name not in links:links[name]={}
if name2 not in links:links[name2]={}
links[name][name2]=z
#save
network={'nodes':nodes,'links':links,'size':len(edges2l)}
print(len(nodes),'nodes and',len(G.edges()),'edges')
open('ignore/plotter_name_weighted.json','w').write(json.dumps(network))
import zipfile
with zipfile.ZipFile("json/plotter_name_weighted.zip", "w", compression=zipfile.ZIP_DEFLATED) as zf:
zf.write("ignore/plotter_name_weighted.json",'plotter_name_weighted.json')
2818 nodes and 1227316 edges
Prune
min_t=365*4*10
edges2l=[(i[0],i[1],edges2[i]) for i in edges2 if edges2[i]['weight']>min_t]
G = nx.DiGraph()
G.add_edges_from(edges2l)
nodes={}
r = nx.pagerank(G,weight='weight')
mr=np.median(list(r.values()))
for i in r:
nodes[i]={'pr':r[i]/mr}
links={}
for i in G.edges(data=True):
name=i[0]
name2=i[1]
z=i[2]['weight']
if name not in links:links[name]={}
if name2 not in links:links[name2]={}
links[name][name2]=z
#save
network={'nodes':nodes,'links':links,'size':len(edges2l)}
print(len(nodes),'nodes and',len(G.edges()),'edges')
open('ignore/plotter_name_weighted_pruned.json','w').write(json.dumps(network))
import zipfile
with zipfile.ZipFile("json/plotter_name_weighted_pruned.zip", "w", compression=zipfile.ZIP_DEFLATED) as zf:
zf.write("ignore/plotter_name_weighted_pruned.json",'plotter_name_weighted_pruned.json')
451 nodes and 4984 edges