import pandas as pd, numpy as np, json, os
import matplotlib.pyplot as plt
%matplotlib inline
plt.style.use('seaborn-whitegrid')
#!pip install d3IpyPlus
from d3IpyPlus import ScatterPlot, LinePlot, BarPlot, StackedArea
#!pip install pyecharts
m=json.loads(open('ignore/member_timelines.json','r').read())
members=json.loads(open('ignore/member_simple.json','r').read())
d=pd.DataFrame(members['deaths'],index=['v']).T
d['v']=' †'
s=pd.DataFrame(members['s2'],index=['s']).T
s=s.join(d)
s['v']=s['v'].fillna('')
s['s']=s['s']+s['v']
s=s[['s']]
s['w']=s['s'].str.split(' ').str[0].str[0]+s['s'].str.split(' ').str[-1].str[0]
szd_color='#E91C62'
pcolors=json.loads(open('json/pcolors.json','r').read())
g=pd.DataFrame(members['megye'],index=['Megye ']).T
g.head()
Megye | |
---|---|
Arama Viorel | 1938-03-10 | 🇷🇴 Bákó |
Pop Petru | 1939-03-01 | 🇷🇴 Argeș |
Les Ioan | 1947-09-01 | 🇷🇴 Szeben |
Carp Mihai | 1956-05-20 | 🇷🇴 Bihar |
Craciun Gheorghe | 1954-12-13 | 🇷🇴 Olt |
data={}
attention={}
for k,i in enumerate(m):
if k%1000==0: print(k/len(m)*100,'%')
if i['Típus']=='Párt':
if i['Csoportok'] not in data:data[i['Csoportok']]={'start':pd.to_datetime('2030'),'end':pd.to_datetime('1980')}
start=pd.to_datetime(i['start'])
end=pd.to_datetime(i['end'])
data[i['Csoportok']]['start']=min(data[i['Csoportok']]['start'],start)
data[i['Csoportok']]['end']=max(data[i['Csoportok']]['end'],end)
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 %
part_timelines=pd.DataFrame(data).T
part_timelines.index.name='id'
part_timelines['start']=part_timelines['start'].astype(str).str[:10]
part_timelines['end']=part_timelines['end'].astype(str).str[:10]
p=part_timelines.reset_index()[['id']]
p['Color']=p['id'].str.split(' ').str[1].str.strip()
p['Part']=p['id'].str.split('-').str[0].str.strip()
part_timelines=part_timelines.join(p.set_index('id').drop_duplicates())
logos={'🕊️':'ALDE', '🌹':'PSD', '📐':'PNL', '🔶':'PD', '⚛️':'PP-DD', '👤':'Independent', '⚪️':'FSN',
'✳️':'PNTCD', '🔱':'PRM', '🌷':'RMDSZ', '🍀':'ECO', '🍏':'PMP',
'🔷':'USR', '🦅':'UNPR', '🔘':'Mas'}
part_timelines['logo']=part_timelines['Part'].str.split(' ').str[0]
part_timelines['group']=[logos[i] for i in part_timelines['logo'].values]
part_timelines['label']=[i+' '+logos[i] for i in part_timelines['logo'].values]
pcolors=json.loads(open('json/pcolors.json','r').read())
part_timelines=part_timelines.join(pd.DataFrame(pcolors,index=['cvalue']).T,on='group').fillna('grey')
part_timelines
end | start | Color | Part | logo | group | label | cvalue | |
---|---|---|---|---|---|---|---|---|
id | ||||||||
🔱 PRM - Partidul România Mare | 2008-12-13 | 1992-10-21 | PRM | 🔱 PRM | 🔱 | PRM | 🔱 PRM | #0d5ca4 |
✳️ PNTCD - Partidul Naţional Ţărănesc Creştin Democrat | 2014-03-01 | 1990-06-18 | PNTCD | ✳️ PNTCD | ✳️ | PNTCD | ✳️ PNTCD | #04843c |
📐 PAC - Partidul Alianţei Civice | 1996-11-22 | 1992-10-21 | PAC | 📐 PAC | 📐 | PNL | 📐 PNL | #f4d42c |
📐 PNL - Partidul Naţional Liberal | 2019-07-01 | 1990-06-18 | PNL | 📐 PNL | 📐 | PNL | 📐 PNL | #f4d42c |
🔶 PDL - Partidul Democrat Liberal | 2015-02-01 | 2004-12-17 | PDL | 🔶 PDL | 🔶 | PD | 🔶 PD | darkOrange |
⚪️ FSN - Frontul Salvării Naţionale | 1994-05-23 | 1990-06-18 | FSN | ⚪️ FSN | ⚪️ | FSN | ⚪️ FSN | grey |
🔶 PD - Partidul Democrat | 2008-12-13 | 1992-10-26 | PD | 🔶 PD | 🔶 | PD | 🔶 PD | darkOrange |
🌹 PSM - Partidul Socialist al Muncii | 1996-11-22 | 1992-10-21 | PSM | 🌹 PSM | 🌹 | PSD | 🌹 PSD | #dc2c24 |
🌷 RMDSZ - Romániai Magyar Demokrata Szövetség | 2019-07-01 | 1990-06-18 | RMDSZ | 🌷 RMDSZ | 🌷 | RMDSZ | 🌷 RMDSZ | green |
🌹 PSD - Partidul Social Democrat | 2019-07-01 | 2000-12-15 | PSD | 🌹 PSD | 🌹 | PSD | 🌹 PSD | #dc2c24 |
👤 Independent - Independent | 2019-07-01 | 1990-06-18 | Independent | 👤 Independent | 👤 | Independent | 👤 Independent | #555 |
🕊️ ALDE - Partidul Alianţa Liberalilor şi Democraţilor | 2019-07-01 | 2016-12-21 | ALDE | 🕊️ ALDE | 🕊️ | ALDE | 🕊️ ALDE | #046cab |
🌹 PDSR - Partidul Democraţiei Sociale din România | 2001-06-01 | 1992-10-26 | PDSR | 🌹 PDSR | 🌹 | PSD | 🌹 PSD | #dc2c24 |
🔷 USR - Uniunea Salvaţi România | 2019-07-01 | 2016-12-21 | USR | 🔷 USR | 🔷 | USR | 🔷 USR | #04abe4 |
🕊️ PUR-SL - Partidul Umanist din România | 2005-05-01 | 2000-12-15 | PUR-SL | 🕊️ PUR | 🕊️ | ALDE | 🕊️ ALDE | #046cab |
⚛️ PP-DD - Partidul Poporului Dan Diaconescu | 2015-08-01 | 2012-12-20 | PP-DD | ⚛️ PP | ⚛️ | PP-DD | ⚛️ PP-DD | #9966cc |
🦅 UNPR - Uniunea Naţională pentru Progresul României | 2016-12-21 | 2012-12-20 | UNPR | 🦅 UNPR | 🦅 | UNPR | 🦅 UNPR | #a8190f |
📐 PNL-CD - Partidul Naţional Liberal | 2000-11-30 | 1992-10-21 | PNL-CD | 📐 PNL | 📐 | PNL | 📐 PNL | #f4d42c |
🕊️ PC - Partidul Conservator | 2016-12-21 | 2004-12-17 | PC | 🕊️ PC | 🕊️ | ALDE | 🕊️ ALDE | #046cab |
🕊️ PUNR - Partidul Unităţii Naţionale Române | 2000-11-30 | 1990-06-18 | PUNR | 🕊️ PUNR | 🕊️ | ALDE | 🕊️ ALDE | #046cab |
📐 PL'93 - Partidul Liberal | 1996-11-22 | 1992-10-28 | PL'93 | 📐 PL'93 | 📐 | PNL | 📐 PNL | #f4d42c |
⚪️ FDSN - Frontul Democrat al Salvarii Nationale | 1993-07-01 | 1990-06-18 | FDSN | ⚪️ FDSN | ⚪️ | FSN | ⚪️ FSN | grey |
🍏 PMP - Partidul Mişcarea Populară | 2019-07-01 | 2016-12-21 | PMP | 🍏 PMP | 🍏 | PMP | 🍏 PMP | #0484cc |
🔘 PTLDR - Partidul Tineretului Liber Democrat din România | 1992-10-16 | 1990-06-18 | PTLDR | 🔘 PTLDR | 🔘 | Mas | 🔘 Mas | #777 |
🔘 PLS - Partidul Liber Schimbist | 1992-10-16 | 1990-06-18 | PLS | 🔘 PLS | 🔘 | Mas | 🔘 Mas | #777 |
🌹 PSDR - Partidul Socialist Democratic din România | 2001-06-01 | 1990-06-18 | PSDR | 🌹 PSDR | 🌹 | PSD | 🌹 PSD | #dc2c24 |
🍀 PER - Partidul Ecologist Român | 2000-11-30 | 1990-06-18 | PER | 🍀 PER | 🍀 | ECO | 🍀 ECO | #69a54f |
🔘 PAR - Partidul Alternativa României | 2000-11-30 | 1996-11-27 | PAR | 🔘 PAR | 🔘 | Mas | 🔘 Mas | #777 |
🔘 FC - Forţa Civică | 2015-07-01 | 2012-12-20 | FC | 🔘 FC | 🔘 | Mas | 🔘 Mas | #777 |
🍀 PDAR - Partidul Democrat Agrar din România | 1996-11-22 | 1990-06-18 | PDAR | 🍀 PDAR | 🍀 | ECO | 🍀 ECO | #69a54f |
🔘 PRNR - Partidul Reconstrucţiei Naţionale din România | 1992-10-16 | 1990-06-18 | PRNR | 🔘 PRNR | 🔘 | Mas | 🔘 Mas | #777 |
🍀 MER - Mişcarea Ecologistă din România | 1992-10-16 | 1990-06-18 | MER | 🍀 MER | 🍀 | ECO | 🍀 ECO | #69a54f |
📐 PNL-AT - Partidul Naţional Liberal | 1993-02-25 | 1992-10-21 | PNL-AT | 📐 PNL | 📐 | PNL | 📐 PNL | #f4d42c |
🍀 FER - Federaţia Ecologistă Română | 2000-11-30 | 1996-11-27 | FER | 🍀 FER | 🍀 | ECO | 🍀 ECO | #69a54f |
🔘 GDC - Gruparea Democratică de Centru | 1992-10-16 | 1990-06-18 | GDC | 🔘 GDC | 🔘 | Mas | 🔘 Mas | #777 |
🔘 AUR - Alianţa pentru Unitatea Românilor | 1992-10-16 | 1990-06-18 | AUR | 🔘 AUR | 🔘 | Mas | 🔘 Mas | #777 |
🔘 PDM - Partidul Democrat al Muncii | 1992-10-16 | 1990-06-18 | PDM | 🔘 PDM | 🔘 | Mas | 🔘 Mas | #777 |
🔘 ULB - Uniunea Liberală "Brătianu" | 1992-10-16 | 1990-06-18 | ULB | 🔘 ULB | 🔘 | Mas | 🔘 Mas | #777 |
👤 Minorități - Minorități | 2019-07-01 | 2016-12-21 | Minorități | 👤 Minorități | 👤 | Independent | 👤 Independent | #555 |
part_timelines_list=list(part_timelines.reset_index().T.to_dict().values())
open('json/part_timelines.json','w').write(json.dumps(part_timelines_list))
9795
Resolve for non-activity
legs={'2016':['2016-12-21','2019-07-01'],
'2012':['2012-12-20','2016-12-21'],
'2008':['2008-12-19','2012-12-20'],
'2004':['2004-12-19','2008-12-13'],
'2000':['2000-12-15','2004-11-30'],
'1996':['1996-11-27','2000-11-30'],
'1992':['1992-10-28','1996-11-22'],
'1990':['1990-06-19','1992-10-16']}
switch_months=set([str(pd.to_datetime(j))[:7] for i in legs for j in legs[i]])
from calendar import monthrange
data2={}
parties={}
for k,i in enumerate(m):
if k%1000==0: print(k/len(m)*100,'%')
if i['Típus']=='Párt':
name=i['Name']
part=i['Csoportok']
if part not in parties:parties[part]=[]
parties[part].append(name)
if part not in data2:data2[part]={}
for t in pd.date_range(i['start'],i['end']):
st=str(t)[:10]
if st not in data2[part]:data2[part][st]=0
data2[part][st]+=1
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 %
df=pd.DataFrame(data2)
df=df.stack().reset_index()
df.columns=['x','id','y']
df=df.set_index(['id','x'])
dqs=[]
for d in df.index.get_level_values(0).unique():
dq=df.loc[d]
dq.index=pd.to_datetime(dq.index)
dq=dq.sort_index()
dq=dq.resample('1M').sum()
dq['id']=d
dq['month_length']=[monthrange(t.year,t.month)[1] for t in dq.index]
dqs.append(dq)
df=pd.concat(dqs).reset_index()
df=df.sort_values(by='id')
df['y']=df['y']/df['month_length']
#df=df.groupby(['id','x']).mean().rolling(12).mean().reset_index().dropna()
df2=df.join(part_timelines,on='id')
df2['x']=df2['x'].astype(str).str[:7]
df2=df2[df2['y']>0]
values=list(df2.T.to_dict().values())
open('json/part_timelines2.json','w').write(json.dumps(values))
1068158