def dynamic_poverty_count(myC='PH'): df = pd.read_csv('../output_country/' + myC + '/poverty_duration_no.csv') df = df.loc[(df.hazard == 'HU') & (df.rp == 10)] df = df.loc[(df.region == 'I - Ilocos') | (df.region == 'II - Cagayan Valley') | (df.region == 'CAR')] df = df.reset_index().set_index(['region', 'hazard', 'rp']).drop([ _c for _c in [ 'index', 'ratio_dw_lim_tot', 'dk_sub', 'dk_lim', 'dw_lim', 'dw_sub', 'res_lim', 'res_sub' ] if _c in df.columns ], axis=1) print(df.head()) n_pov = [] for _wk in np.linspace(0, 521, 450) / 52.: n_pov.append(1E-3 * df.loc[(df['c'] > get_poverty_line(myC)) & (df['t_pov_cons'] > _wk), 'pcwgt'].sum()) plt.plot(np.linspace(0, 521, 450) / 52., n_pov) plt.xlim(0, 10.2) plt.ylim(0) plt.xlabel('Years after disaster', weight='bold', labelpad=8) plt.ylabel( 'Filipinos in consumption poverty\ndue to wind damages in Ompong (,000)', weight='bold', labelpad=8) sns.despine() plt.grid(False) plt.gcf().savefig( '/Users/brian/Desktop/Dropbox/Bank/ompong/plots/t_pov_ompong.pdf', format='pdf') print(df.loc[df['c'] > get_poverty_line(myC), 'pcwgt'].sum()) print(df.loc[(df['c'] > get_poverty_line(myC)) & (df['t_pov_cons'] > 12 / 52), 'pcwgt'].sum()) assert (False)
def plot_income_and_consumption_distributions(myC, iah, aReg, aDis, anRP, labels=(False, False, False), currency=''): label_subsistence, label_poverty, label_middleclass = labels iah = iah.reset_index() economy = get_economic_unit(myC) iah = iah.loc[iah.pcwgt_no != 0].copy() iah['pov_line'] = get_poverty_line(myC) try: plt.close('all') except: pass if aReg == 'ompong': reg_crit = "((region=='I - Ilocos')|(region=='II - Cagayan Valley')|(region=='CAR'))" aReg = 'path of Typhoon Mangkhut' else: reg_crit = '(' + economy + '==@aReg)' economy = get_economic_unit(myC) output_plots = os.getcwd() + '/../output_plots/' + myC + '/' # Number of bins in histograms. Ignore the None argument c_bins = [None, 50] # Dictionary for labeling # GLOBAL VARIABLE in new_process_data. haz_dict = { 'SS': 'Storm surge', 'PF': 'Precipitation flood', 'HU': 'Typhoon', 'EQ': 'Earthquake', 'DR': 'Drought', 'FF': 'Fluvial flood', 'CY': 'Cyclone Idai' } simple_plot = True stack_wealthy = True upper_clip = 1E6 if myC == 'PH': if aReg == 'VIII - Eastern Visayas': upper_clip = 1.25E5 # Hack hack hack else: upper_clip = 1.5E5 elif myC == 'FJ': upper_clip = 2E4 elif myC == 'SL': upper_clip = 3.25E5 if aReg == 'Rathnapura': upper_clip = 3.0E5 elif myC == 'MW': if aReg == 'Lilongwe': upper_clip = 4.0E5 else: upper_clip = 2.5E5 elif myC == 'BO': upper_clip = 5E4 elif myC == 'AM': upper_clip = 9E3 elif myC == 'BG': upper_clip = 2E4 elif myC == 'AL': upper_clip = 5.6E3 elif myC == 'RO': upper_clip = 5E4 elif myC == 'GR': upper_clip = 3E4 elif myC == 'TR': upper_clip = 3E4 elif myC == 'HR': upper_clip = 5E4 else: upper_clip = 1E4 sf_x = 1 #if currency.lower() == 'usd': sf_x = get_currency(myC)[2] currency = get_currency(myC)[0] for _fom, _fom_lab in [('c', 'Consumption')]: ax = plt.gca() plt.cla() plt.xlim(0, sf_x * upper_clip) if not stack_wealthy: upper_clip *= 1.1 if currency.lower() != 'usd' and myC == 'PH': plt.xlim([0, 100]) if aReg == 'II - Cagayan Valley' and aDis == 'HU': plt.ylim(0, 400) elif aReg == 'VIII - Eastern Visayas' and aDis == 'HU': plt.ylim(0, 500) elif aReg == 'Rathnapura': plt.ylim(0, 130) #elif aReg == 'Bucharest-Ilfov': plt.ylim(0,220) elif aReg == 'Beni': plt.ylim(0, 40) if aReg == 'Gergharkunik': plt.ylim(0, 25) if aReg == 'Yerevan': plt.ylim(0, 70) if myC == 'BG': plt.ylim(0, 250) if myC == 'GE': plt.ylim(0, 100) if myC == 'GR': plt.ylim(0, 330) if myC == 'TR': plt.ylim(0, 6000) if myC == 'HR': plt.ylim(0, 500) plt.xlabel(_fom_lab + r' (' + currency + ' per person, per year)', labelpad=8, fontsize=8) plt.ylabel('Population' + get_pop_scale_fac(myC)[1], labelpad=8, fontsize=8) if _fom == 'i': plt.title( str(anRP) + '-year ' + haz_dict[aDis].lower() + ' in ' + aReg) # Income/Cons dist immediately after disaster cf_heights, cf_bins = np.histogram( sf_x * iah.loc[iah.eval(reg_crit + '&(hazard==@aDis)&(rp==@anRP)'), _fom + '_pre_reco'].clip(upper=upper_clip), bins=c_bins[1], weights=iah.loc[iah.eval(reg_crit + '&(hazard==@aDis)&(rp==@anRP)'), 'pcwgt_no'] / get_pop_scale_fac(myC)[0]) print(aDis, anRP, _fom, (iah.loc[iah.eval(reg_crit + '&(hazard==@aDis)&(rp==@anRP)'), ['c_initial', 'pcwgt_no']].prod(axis=1).sum() / iah.loc[iah.eval(reg_crit + '&(hazard==@aDis)&(rp==@anRP)'), 'pcwgt_no'].sum())) print(_fom, ' final -->', (iah.loc[iah.eval(reg_crit + '&(hazard==@aDis)&(rp==@anRP)'), [_fom + '_pre_reco', 'pcwgt_no']].prod(axis=1).sum() / iah.loc[iah.eval(reg_crit + '&(hazard==@aDis)&(rp==@anRP)'), 'pcwgt_no'].sum())) if c_bins[0] is None: c_bins = [cf_bins, cf_bins] # Income dist before disaster ci_heights, _bins = np.histogram( sf_x * iah.loc[iah.eval(reg_crit + '&(hazard==@aDis)&(rp==@anRP)'), 'c_initial'].clip(upper=upper_clip), bins=c_bins[1], weights=iah.loc[iah.eval(reg_crit + '&(hazard==@aDis)&(rp==@anRP)'), 'pcwgt_no'] / get_pop_scale_fac(myC)[0]) # Income dist after reconstruction #cf_reco_hgt, _bins = np.histogram(sf_x*iah.loc[iah.eval(reg_crit+'&(hazard==@aDis)&(rp==@anRP)'),'c_post_reco'].clip(upper=upper_clip), bins=c_bins[1], # weights=iah.loc[iah.eval(reg_crit+'&(hazard==@aDis)&(rp==@anRP)'),'pcwgt_no']/get_pop_scale_fac(myC)[0]) sns.despine() plt.gca().grid(False) pre_step = ax.step(c_bins[1][1:], ci_heights, label=aReg + ' - FIES income', linewidth=1.25, color=greys_pal[7]) #leg = ax.legend(loc='best',labelspacing=0.75,ncol=1,fontsize=9,borderpad=0.75,fancybox=True,frameon=True,framealpha=0.9) plt.ylim(0) if stack_wealthy: pre_ann = plt.annotate( 'Pre-disaster ' + _fom_lab.lower() + '\n(FIES data)', xy=(c_bins[1][-2], ci_heights[-1]), xytext=(c_bins[1][-4], ci_heights[-1] * 1.075), arrowprops=dict( arrowstyle="-", color=greys_pal[8], connectionstyle="angle,angleA=0,angleB=90,rad=5"), annotation_clip=False, size=8, weight='light', ha='right', va='center', color=greys_pal[8]) else: pre_ann = plt.annotate( 'Pre-disaster ' + _fom_lab.lower() + '\n(FIES data)', xy=((c_bins[1][5] + c_bins[1][6]) / 2, ci_heights[5]), xytext=(c_bins[1][8], ci_heights[5] * 1.02), arrowprops=dict( arrowstyle="-", color=greys_pal[8], connectionstyle="angle,angleA=0,angleB=90,rad=5"), annotation_clip=False, size=8, weight='light', ha='left', va='bottom', color=greys_pal[8]) _success = False _counter = 0 while not _success and _counter < 15: try: _fout = output_plots + 'npr_poverty_' + _fom + '_' + aReg.replace( ' ', '').replace('-', '') + '_' + aDis + '_' + str( anRP) + '_' + currency[-3:].lower() + '_1of3.pdf' #ax.get_figure().savefig(_fout,format='pdf',bbox_inches='tight') _success = True except: print( 'no good! try again in plot_income_and_consumption_distributions (1/3-' + str(_counter) + ')') _counter += 1 #ax.step(c_bins[1][:-1], cf_heights, label=aReg+' - post-disaster', facecolor=q_colors[1],alpha=0.45) #ax.bar(c_bins[1][:-1], -(ci_heights-cf_heights), width=(c_bins[1][1]-c_bins[1][0]), align='edge', # label=aReg+' - post-disaster', facecolor=paired_pal[4],edgecolor=None,linewidth=0,alpha=0.65,bottom=ci_heights) ax.bar(c_bins[1][:-1], cf_heights, width=(c_bins[1][1] - c_bins[1][0]), align='edge', label=aReg + ' - post-disaster', facecolor=paired_pal[4], edgecolor=None, linewidth=0, alpha=0.75) #leg = ax.legend(loc='best',labelspacing=0.75,ncol=1,fontsize=9,borderpad=0.75,fancybox=True,frameon=True,framealpha=0.9) if stack_wealthy: post_ann = plt.annotate( 'Post-disaster ' + _fom_lab.lower() + '\n(modeled)', xy=((c_bins[1][-2] + c_bins[1][-1]) / 1.99, cf_heights[-1] * 0.90), xytext=(c_bins[1][-4], cf_heights[-1] * 0.90), arrowprops=dict(arrowstyle="-", facecolor=greys_pal[8]), annotation_clip=False, size=8, weight='light', ha='right', va='center', color=paired_pal[5]) else: ax.lines.remove(ax.lines[0]) pre_step = ax.step(c_bins[1][1:], ci_heights, label=aReg + ' - FIES income', linewidth=1.0, color=greys_pal[7], alpha=0.65) pre_ann.remove() pre_ann = plt.annotate( 'Pre-disaster ' + _fom_lab.lower() + '\n(FIES data)', xy=((c_bins[1][5] + c_bins[1][6]) / 2, ci_heights[5]), xytext=(c_bins[1][8], ci_heights[5] * 1.02), arrowprops=dict( arrowstyle="-", color=greys_pal[8], connectionstyle="angle,angleA=0,angleB=90,rad=5", alpha=0.65), annotation_clip=False, size=8, weight='light', ha='left', va='bottom', color=greys_pal[8], alpha=0.65) post_ann = plt.annotate( 'Post-disaster ' + _fom_lab.lower() + '\n(modeled)', xy=(c_bins[1][5], 1.1 * cf_heights[6]), xytext=(c_bins[1][9], 1.1 * cf_heights[6]), annotation_clip=False, size=8, weight='light', ha='left', va='center', color=paired_pal[5], arrowprops=dict( arrowstyle="-", color=paired_pal[5], connectionstyle="angle,angleA=0,angleB=90,rad=5")) _success = False _counter = 0 while not _success and _counter < 10: try: _fout = output_plots + 'npr_poverty_' + _fom + '_' + aReg.replace( ' ', '').replace('-', '') + '_' + aDis + '_' + str( anRP) + '_' + currency[-3:].lower() + '_2of3.pdf' #ax.get_figure().savefig(_fout,format='pdf',bbox_inches='tight') _success = True except: print( 'no good! try again in plot_income_and_consumption_distributions (2/3)' ) _counter += 1 # These are done above #plt.annotate('Pre-disaster '+_fom_lab.lower()+'\n(FIES data)',xy=(c_bins[1][-2],ci_heights[-1]),xytext=(c_bins[1][-4],ci_heights[-1]*1.075), # arrowprops=dict(arrowstyle="-",facecolor=greys_pal[8],connectionstyle="angle,angleA=0,angleB=90,rad=5"), # annotation_clip=False,size=7,weight='light',ha='right',va='center',color=greys_pal[8]) #plt.annotate('Post-disaster '+_fom_lab.lower()+'\n(modeled)',xy=((c_bins[1][-2]+c_bins[1][-1])/1.99,cf_heights[-1]*0.90),xytext=(c_bins[1][-4],cf_heights[-1]*0.90), # arrowprops=dict(arrowstyle="-",facecolor=greys_pal[8]),annotation_clip=False,size=7,weight='light',ha='right',va='center',color=paired_pal[5]) if not stack_wealthy: pre_ann.remove() post_ann.remove() #ax.bar(c_bins[1][:-1], cf_reco_hgt, width=(c_bins[1][1]-c_bins[1][0]), label=aReg+' - post-reconstruction', facecolor=q_colors[1],edgecolor=q_colors[1],alpha=0.65) #ax.step(c_bins[1][1:], ci_heights, label=aReg+' - FIES income', linewidth=1.2,color=greys_pal[8]) #if myC_ylim == None: myC_ylim = ax.get_ylim() #plt.ylim(myC_ylim[0],2.5*myC_ylim[1]) # ^ Need this for movie making, but better to let the plot limits float if not #leg = ax.legend(loc='best',labelspacing=0.75,ncol=1,fontsize=9,borderpad=0.75,fancybox=True,frameon=True,framealpha=0.9) #leg.get_frame().set_color('white') #leg.get_frame().set_edgecolor(greys_pal[7]) #leg.get_frame().set_linewidth(0.2) if not simple_plot: ax.annotate('Total asset losses: ' + str( round( sf_x * iah.loc[iah.eval(reg_crit + '&(hazard==@aDis)&(rp==@anRP)'), ['pcwgt_no', 'dk0']].prod(axis=1).sum(), 1)) + currency, xy=(0.03, -0.18), xycoords=leg.get_frame(), size=8, va='top', ha='left', annotation_clip=False, zorder=100) ax.annotate('Reg. well-being losses: ' + str( round( sf_x * iah.loc[iah.eval(reg_crit + '&(hazard==@aDis)&(rp==@anRP)'), ['pcwgt_no', 'dw']].prod(axis=1).sum() / df.wprime.mean(), 1)) + currency, xy=(0.03, -0.50), xycoords=leg.get_frame(), size=8, va='top', ha='left', annotation_clip=False, zorder=100) ax.annotate('Natl. liability: ' + str( round( float(sf_x * public_costs.loc[ (public_costs.contributer != aReg) & (public_costs[economy] == aReg) & (public_costs.hazard == aDis) & (public_costs.rp == anRP), ['transfer_pub']].sum() * 1.E3), 1)) + currency, xy=(0.03, -0.92), xycoords=leg.get_frame(), size=8, va='top', ha='left', annotation_clip=False, zorder=100) ax.annotate('Natl. well-being losses: ' + str( round( sf_x * float(public_costs.loc[ (public_costs.contributer != aReg) & (public_costs[economy] == aReg) & (public_costs.hazard == aDis) & (public_costs.rp == anRP), 'dw_tot_curr'].sum()), 1)) + ',000 ' + currency, xy=(0.03, -1.24), xycoords=leg.get_frame(), size=8, va='top', ha='left', annotation_clip=False, zorder=100) try: net_chg_pov_c = int(iah.loc[iah.eval( reg_crit + '&(hazard==@aDis)&(rp==@anRP)&(c_pre_reco<=pov_line)' ), 'pcwgt_no'].sum() - iah.loc[ iah.eval(reg_crit + '&(hazard==@aDis)&(rp==@anRP)&(c_initial<=pov_line)'), 'pcwgt_no'].sum()) net_chg_pov_i = int(iah.loc[iah.eval( reg_crit + '&(hazard==@aDis)&(rp==@anRP)&(i_pre_reco<=pov_line)' ), 'pcwgt_no'].sum() - iah.loc[ iah.eval(reg_crit + '&(hazard==@aDis)&(rp==@anRP)&(c_initial<=pov_line)'), 'pcwgt_no'].sum()) except: net_chg_pov_c = int(iah.loc[ iah. eval('district==@aReg & hazard==@aDis & c_pre_reco<=pov_line'), 'pcwgt_no'].sum() - iah.loc[iah.eval( 'district==@aReg & hazard==@aDis & c_initial<=pov_line'), 'pcwgt_no'].sum()) net_chg_pov_i = int(iah.loc[ iah. eval('district==@aReg & hazard==@aDis & i_pre_reco<=pov_line'), 'pcwgt_no'].sum() - iah.loc[iah.eval( 'district==@aReg & hazard==@aDis & c_initial<=pov_line'), 'pcwgt_no'].sum()) try: iah['midclass_line_lo'] = get_middleclass_range(myC)[0] iah['midclass_line_hi'] = get_middleclass_range(myC)[1] net_chg_midclass_c = int(iah.loc[iah.eval(reg_crit + ( '&(hazard==@aDis)&(rp==@anRP)&' + '(c_initial>=midclass_line_lo)&(c_pre_reco<midclass_line_lo)') ), 'pcwgt_no'].sum()) net_chg_midclass_i = int(iah.loc[iah.eval(reg_crit + ( '&(hazard==@aDis)&(rp==@anRP)&' + '(c_initial>=midclass_line_lo)&(i_pre_reco<midclass_line_lo)') ), 'pcwgt_no'].sum()) except: net_chg_midclass_c, net_chg_midclass_i = 0, 0 net_chg_pov = int( round( (net_chg_pov_i if _fom == 'i' else net_chg_pov_c) / 100., 0) * 100) net_chg_midclass = int( round((net_chg_midclass_i if _fom == 'i' else net_chg_midclass_c) / 100., 0) * 100) try: net_chg_pov_pct = abs( round( 100. * float(net_chg_pov) / float(iah.loc[iah.eval(reg_crit + '&(hazard==@aDis)&(rp==@anRP)'), 'pcwgt_no'].sum()), 1)) except: net_chg_pov_pct = 0 try: _slice = reg_crit + '&(hazard==@aDis)&(rp==@anRP)&(c_initial>=midclass_line_lo)&(c_initial<midclass_line_hi)' net_chg_midclass_pct = abs( round( 100. * float(net_chg_midclass) / float(iah.loc[iah.eval(_slice), 'pcwgt_no'].sum()), 1)) except: net_chg_midclass_pct = 0 trans = ax.get_xaxis_transform() # x in data units, y in axes fraction mc_anno_y = 0.80 pov_anno_y = 0.99 sub_anno_y = 0.95 anno_y_offset = 0.045 _, mc_anno_y_data = axis_data_coords_sys_transform(ax, 0, mc_anno_y, inverse=False) _, pov_anno_y_data = axis_data_coords_sys_transform(ax, 0, pov_anno_y, inverse=False) _, sub_anno_y_data = axis_data_coords_sys_transform(ax, 0, sub_anno_y, inverse=False) if label_middleclass: middleclass = get_middleclass_range('RO') plt.plot([sf_x * middleclass[0], sf_x * middleclass[0]], [0, mc_anno_y_data], 'k-', lw=1.0, color=greys_pal[8], zorder=100, alpha=0.85, ls=':') #plt.fill_between([sf_x*middleclass[0],sf_x*middleclass[1]],[pov_anno_y_data,pov_anno_y_data],color=greys_pal[2],alpha=0.3) #plt.fill_between([0,sf_x*middleclass[0]],[pov_anno_y_data,pov_anno_y_data],color=greys_pal[2],alpha=0.3) ax.annotate( 'Middle class', xy=(sf_x * (middleclass[0] + 0.1 * iah.pov_line.mean()), mc_anno_y), xycoords=trans, ha='left', va='top', fontsize=9, annotation_clip=False, weight='bold', color=greys_pal[7]) ax.annotate(int_w_commas(net_chg_midclass) + ' drop from middle class\n (' + str(net_chg_midclass_pct) + '% decrease)', weight='light', color=greys_pal[7], xy=(sf_x * (middleclass[0] + 0.1 * iah.pov_line.mean()), mc_anno_y - anno_y_offset), xycoords=trans, ha='left', va='top', fontsize=9, annotation_clip=False) if label_poverty: plt.plot([sf_x * iah.pov_line.mean(), sf_x * iah.pov_line.mean()], [0, pov_anno_y_data], 'k-', lw=1.0, color=greys_pal[8], zorder=100, alpha=0.85, ls=':') ax.annotate('Poverty line', xy=(sf_x * 1.1 * iah.pov_line.mean(), pov_anno_y), xycoords=trans, ha='left', va='top', fontsize=9, annotation_clip=False, weight='bold', color=greys_pal[7]) ax.annotate('Increase of ' + int_w_commas(net_chg_pov) + ' (' + str(net_chg_pov_pct) + '% of regional pop.)\n in ' + _fom_lab.lower() + ' poverty', weight='light', color=greys_pal[7], xy=(sf_x * 1.1 * iah.pov_line.mean(), pov_anno_y - anno_y_offset), xycoords=trans, ha='left', va='top', fontsize=9, annotation_clip=False) sub_line, net_chg_sub = (None if not label_subsistence else get_subsistence_line(myC)), None if sub_line is not None: net_chg_sub = int( round((iah.loc[iah.eval( reg_crit + '&(hazard==@aDis)&(rp==@anRP)&(c_pre_reco<=@sub_line)' ), 'pcwgt_no'].sum() - iah.loc[iah.eval( reg_crit + '&(hazard==@aDis)&(rp==@anRP)&(c_initial<=@sub_line)'), 'pcwgt_no'].sum()) / 100., 0) * 100) if _fom == 'i': net_chg_sub = int( round((iah.loc[iah.eval( reg_crit + '&(hazard==@aDis)&(rp==@anRP)&(i_pre_reco<=@sub_line)' ), 'pcwgt_no'].sum() - iah.loc[iah.eval( reg_crit + '&(hazard==@aDis)&(rp==@anRP)&(c_initial<=@sub_line)'), 'pcwgt_no'].sum()) / 100., 0) * 100) try: net_chg_sub_pct = round( 100. * float(net_chg_sub) / float(iah.loc[iah.eval(reg_crit + '&(hazard==@aDis)&(rp==@anRP)'), 'pcwgt_no'].sum()), 1) except: net_chg_sub_pct = 0 plt.plot([sf_x * sub_line, sf_x * sub_line], [0, sub_anno_y_data], 'k-', lw=1.0, color=greys_pal[8], zorder=100, alpha=0.85, ls=':') ax.annotate('Subsistence line', xy=(sf_x * 1.1 * sub_line, sub_anno_y), xycoords=trans, ha='left', va='top', color=greys_pal[7], fontsize=9, annotation_clip=False, weight='bold') ax.annotate('Increase of ' + int_w_commas(net_chg_sub) + ' (' + str(net_chg_sub_pct) + '% of regional pop.)\n in ' + _fom_lab.lower() + ' subsistence', weight='light', color=greys_pal[7], xy=(sf_x * 1.1 * sub_line, sub_anno_y - anno_y_offset), xycoords=trans, ha='left', va='top', fontsize=9, annotation_clip=False) #print(aReg,aDis,anRP,net_chg_pov,'people into poverty &',net_chg_sub,'into subsistence') fig = ax.get_figure() _success = False _counter = 0 while not _success and _counter < 4: try: _fout = output_plots + 'npr_poverty_' + _fom + '_' + aReg.replace( ' ', '').replace('-', '') + '_' + aDis + '_' + str( anRP) + '_' + currency[-3:].lower() + '.pdf' fig.savefig(_fout, format='pdf', bbox_inches='tight') plt.clf() plt.close('all') _success = True print('wrote ' + aReg + '_poverty_' + _fom + '_' + aDis + '_' + str(anRP) + '.pdf') except: print( 'no good! try again in plot_income_and_consumption_distributions' ) _counter += 1
def run_poverty_duration_plot(myC,myHaz='HU',drop_spots=None,special_event=None): if special_event and special_event.lower() == 'idai': myHaz='CY' # Load file with geographical (region/province/district) as index df = pd.read_csv('../output_country/'+myC+'/poverty_duration_no.csv') df = df.reset_index().set_index(df.columns[1]).sort_index() geo = df.index.name all_geo = np.array(df[~df.index.duplicated(keep='first')].index) # used in groupby df['country'] = myC # assign deciles listofdeciles=np.arange(0.10, 1.01, 0.10) df = df.reset_index().groupby('country',sort=True).apply(lambda x:match_percentiles(x,perc_with_spline(reshape_data(x.c),reshape_data(x.pcwgt),listofdeciles),'decile')) # Load additional SP runs _sp = [] for f in glob.glob('/Users/brian/Desktop/BANK/hh_resilience_model/output_country/'+myC+'/poverty_duration_*.csv'): _ = f.replace('/Users/brian/Desktop/BANK/hh_resilience_model/output_country/'+myC+'/poverty_duration_','').replace('.csv','') _sp.append(_) for iSP in _sp: _ = pd.read_csv('../output_country/'+myC+'/poverty_duration_'+iSP+'.csv') _['t_pov_bool'] = False try: _.loc[_.c>_.pov_line,'t_pov_bool'] = True except: _.loc[_.c>get_poverty_line(myC,by_district=False),'t_pov_bool'] = True df[['t_pov_inc'+iSP,'t_pov_cons'+iSP,'t_pov_bool'+iSP]] = _[['t_pov_inc','t_pov_cons','t_pov_bool']] ############################ # Do some plotting #plot_crit = '(t_pov_bool)&(hazard==@myHaz)&(rp==500)' #df.loc[df.eval(plot_crit)].plot.hexbin('dk0','t_pov_cons') #plt.gca().get_figure().savefig('../output_plots/'+myC+'/poverty_duration_hexbin_no.pdf',format='pdf') #plt.cla() #df.loc[df.eval(plot_crit)].plot.scatter('dk0','t_pov_cons') #plt.gca().get_figure().savefig('../output_plots/'+myC+'/poverty_duration_scatter_no.pdf',format='pdf') #plt.cla() ############################ df = df.reset_index().set_index(['hazard','rp','decile']) df['t_pov_bool'] = False try: df.loc[df.c>df.pov_line,'t_pov_bool'] = True except: df.loc[df.c>get_poverty_line(myC,by_district=False),'t_pov_bool'] = True df_dec = pd.DataFrame(index=df.sum(level=['hazard','rp','decile']).index).sort_index() # Populate the df_dec dataframe now, while its index is set to ['hazard','rp','decile'] # Number of individuals who face income or consumption poverty df_dec['n_new_pov_inc'] = df.loc[df.t_pov_bool==True,'pcwgt'].sum(level=['hazard','rp','decile']) df_dec['n_new_pov_cons'] = df.loc[df.t_pov_bool==True,'pcwgt'].sum(level=['hazard','rp','decile']) # Individuals who face income or consumption poverty as fraction of all individuals df_dec['frac_new_pov_inc'] = df_dec['n_new_pov_inc']/df['pcwgt'].sum(level=['hazard','rp','decile']) df_dec['frac_new_pov_cons'] = df_dec['n_new_pov_cons']/df['pcwgt'].sum(level=['hazard','rp','decile']) # Among people pushed into pov: average time in poverty (months) for iSP in _sp: df_dec['t_pov_inc_avg'+iSP] = 12.*(df.loc[df.eval('t_pov_bool'+iSP+'==True'),['pcwgt','t_pov_inc'+iSP]].prod(axis=1).sum(level=['hazard','rp','decile']) /df.loc[df.eval('t_pov_bool'+iSP+'==True'),'pcwgt'].sum(level=['hazard','rp','decile'])) df_dec['t_pov_cons_avg'+iSP] = 12.*(df.loc[df.eval('t_pov_bool'+iSP+'==True'),['pcwgt','t_pov_cons'+iSP]].prod(axis=1).sum(level=['hazard','rp','decile']) /df.loc[df.eval('t_pov_bool'+iSP+'==True'),'pcwgt'].sum(level=['hazard','rp','decile'])) for iloc in all_geo: df_dec['t_pov_inc_avg_'+iloc] = 12.*(df.loc[df.eval('(t_pov_bool==True)&('+geo+'==@iloc)'),['pcwgt','t_pov_inc']].prod(axis=1).sum(level=['hazard','rp','decile']) /df.loc[df.eval('(t_pov_bool==True)&('+geo+'==@iloc)'),'pcwgt'].sum(level=['hazard','rp','decile'])) df_dec['t_pov_cons_avg_'+iloc] = 12.*(df.loc[df.eval('(t_pov_bool==True)&('+geo+'==@iloc)'),['pcwgt','t_pov_cons']].prod(axis=1).sum(level=['hazard','rp','decile']) /df.loc[df.eval('(t_pov_bool==True)&('+geo+'==@iloc)'),'pcwgt'].sum(level=['hazard','rp','decile'])) df_dec.to_csv('../output_country/'+myC+'/poverty_by_decile.csv') ###################### # Scatter plot of hh that have to delay reconstruction upper_lim = 1E15 df['t_reco'] = (np.log(1.0/0.05)/df['hh_reco_rate']).clip(upper=upper_lim) means = [] xmax = 2.5E5 step = xmax/10. for i in np.linspace(0,10,10): means.append(df.loc[df.eval('(rp==1000)&(c>@i*@step)&(c<=(@i+1)*@step)&(t_reco!=@upper_lim)'),['pcwgt','t_reco']].prod(axis=1).sum()/ df.loc[df.eval('(rp==1000)&(c>@i*@step)&(c<=(@i+1)*@step)&(t_reco!=@upper_lim)'),['pcwgt']].sum()) ax = df.loc[df.eval('(c<@xmax)&(t_reco<12)')].plot.hexbin('c','t_reco',gridsize=25,mincnt=1) plt.plot(step*np.linspace(0,10,10),means,zorder=100) # Do the formatting ax = title_legend_labels(ax,'Precipitation flood in '+myC,lab_x='Pre-disaster consumption [PhP per cap]',lab_y='Time to reconstruct [years]',leg_fs=9) # Do the saving plt.draw() plt.gca().get_figure().savefig('../output_plots/'+myC+'/t_start_reco_scatter.pdf',format='pdf') plt.cla() ###################### # Latex table of poorest quintile poverty time _cons_to_tex = df_dec.drop([i for i in df_dec.columns if i not in ['t_pov_cons_avg_'+j for j in all_geo]],axis=1) _inc_to_tex = df_dec.drop([i for i in df_dec.columns if i not in ['t_pov_inc_avg_'+j for j in all_geo]],axis=1) _cons_to_tex = _cons_to_tex.rename(columns={'t_pov_cons_avg_'+j:j for j in all_geo}).stack() _inc_to_tex = _inc_to_tex.rename(columns={'t_pov_inc_avg_'+j:j for j in all_geo}).stack() _cons_to_tex.index.names = ['hazard','rp','decile',geo] _inc_to_tex.index.names = ['hazard','rp','decile',geo] _to_tex = pd.DataFrame(index=_cons_to_tex.index) _to_tex['Income'] = _inc_to_tex.round(1) _to_tex['Consumption'] = _cons_to_tex.round(1) _to_tex = _to_tex.reset_index().set_index(geo) _to_tex = _to_tex.loc[_to_tex.eval('(hazard==@myHaz)&(rp==10)&(decile==1)')].sort_values('Consumption',ascending=False) _to_tex[['Income','Consumption']].to_latex('latex/'+myC+'/poverty_duration.tex') ###################### # Plot consumption and income poverty (separately) df_dec = df_dec.reset_index() _lab = {'t_pov_cons_avg':'Average time to exit poverty\n(income net of reconstruction & savings) [months]', 't_pov_inc_avg':'Average time to exit poverty (income only) [months]'} for ipov in ['t_pov_cons_avg','t_pov_inc_avg']: # Do the plotting #ax = df_dec.loc[df_dec.eval('(hazard==@myHaz)&(rp==10)')].plot.scatter('decile',ipov+'no',color=sns_pal[1],lw=0,label='Natl. average (RP = 5 years)',zorder=99) #df_dec.loc[df_dec.eval('(hazard==@myHaz)&(rp==1000)')].plot.scatter('decile',ipov+'no',color=sns_pal[3],lw=0,label='Natl. average (RP = 1000 years)',zorder=98,ax=ax) try: ax = df_dec.loc[df_dec.eval('(hazard==@myHaz)&(rp==10)')].plot('decile',ipov+'no',color=sns_pal[1],zorder=97,label='') df_dec.loc[df_dec.eval('(hazard==@myHaz)&(rp==1000)')].plot('decile',ipov+'no',color=sns_pal[3],zorder=96,label='',ax=ax) except: pass icol = 4 # Which areas to plot? _rp = 10 if myC == 'SL': focus = ['Colombo','Rathnapura','Kalutara','Mannar'] elif myC == 'PH': focus = ['NCR'] elif myC == 'RO': focus = ['Center'] elif myC == 'BO': focus = ['La Paz','Beni'] elif myC == 'MW': focus = ['Lilongwe','Chitipa'] if special_event and special_event.lower() == 'idai': focus = ['Chikwawa','Blantyre','Nsanje'] _rp = 1 else: assert(False) for iloc in focus: df_dec.loc[df_dec.eval('(hazard==@myHaz)&(rp=='+str(_rp)+')')].plot.scatter('decile',ipov+'_'+iloc,color=sns_pal[icol],lw=0,label=iloc+' (RP = '+str(_rp)+' years)',zorder=95,ax=ax) df_dec.loc[df_dec.eval('(hazard==@myHaz)&(rp=='+str(_rp)+')')].plot('decile',ipov+'_'+iloc,color=sns_pal[icol],zorder=94,label='',ax=ax) icol+=1 # Do the formatting ax = title_legend_labels(ax,'Precipitation flood in '+myC,lab_x='Decile',lab_y=_lab[ipov],lim_x=[0.5,10.5],lim_y=[-0.1,42],leg_fs=9) ax.xaxis.set_ticks([1,2,3,4,5,6,7,8,9,10]) ax.yaxis.set_ticks([0,6,12,18,24,30,36,42]) # Do the saving ax.get_figure().savefig('../output_plots/'+myC+'/'+ipov+'_by_decile.pdf',format='pdf') plt.cla() ###################### # Plot consumption and income poverty (separately), with alternative SPs try: _lab = {'t_pov_cons_avg':'Average time to exit poverty\n(income net of reconstruction & savings) [months]', 't_pov_inc_avg':'Average time to exit poverty (income only) [months]'} icol=0 ax = plt.gca() for ipov in ['t_pov_cons_avg','t_pov_inc_avg']: # Do the plotting _df_5 = df_dec.loc[df_dec.eval('(hazard==@myHaz)&(rp==5)')].copy() _df_1000 = df_dec.loc[df_dec.eval('(hazard==@myHaz)&(rp==1000)')].copy() for iSP in _sp: plt.fill_between(_df_5['decile'].values,_df_5[ipov+iSP],_df_1000[ipov+iSP],alpha=0.3) _df_5.plot.scatter('decile',ipov+iSP,lw=0,label='',zorder=99,ax=ax) _df_1000.plot.scatter('decile',ipov+iSP,lw=0,label='',zorder=98,ax=ax) _df_5.plot('decile',ipov+iSP,zorder=97,linestyle=':',label=iSP+' natl. average (RP = 5 years)',ax=ax) _df_1000.plot('decile',ipov+iSP,zorder=96,label=iSP+' natl. average (RP = 1000 years)',ax=ax) icol+=1 # Do the formatting ax = title_legend_labels(ax,'Precipitation flood in '+myC,lab_x='Decile',lab_y=_lab[ipov],lim_x=[0.5,10.5],lim_y=[-0.1],leg_fs=9) ax.xaxis.set_ticks([1,2,3,4,5,6,7,8,9,10]) ax.yaxis.set_ticks([0,3,6,9,12,15,18]) # Do the saving ax.get_figure().savefig('../output_plots/'+myC+'/'+ipov+'_with_sps_by_decile.pdf',format='pdf') plt.cla() except: pass plt.close('all') return True
def load_recovery_times(myC, economy): results_dir = '../output_country/{}/'.format(myC) event_level = [economy, 'hazard', 'rp'] out_df = pd.DataFrame() for file in glob.glob(results_dir + "poverty_duration_*.csv"): pol_str = file.replace(results_dir, '').replace('poverty_duration_', '').replace('.csv', '') #if 'no' not in pol_str and 'social_scaleup' not in pol_str: continue if 'no' not in pol_str: continue _df = pd.read_csv(file).set_index(event_level + ['hhid']) transient_poor_inc = (_df.c >= get_poverty_line(myC)) & (_df.t_pov_inc != 0) transient_poor_cons = (_df.c >= get_poverty_line(myC)) & (_df.t_pov_cons != 0) # middleclass_dropout_inc = (_df.c >= get_middleclass_range(myC)[0]) & ( _df.t_mc_inc < 10) middleclass_dropout_cons = (_df.c >= get_middleclass_range(myC)[0]) & ( _df.t_mc_cons < 10) # # # Average time in poverty or out of middle class, for hh that were not poor, or were middleclass, before the disaster out_df[pol_str + '_transpoor_inc'] = ( _df.loc[transient_poor_inc, ['pcwgt', 't_pov_inc']].prod( axis=1).sum(level=event_level) / _df.loc[transient_poor_inc, 'pcwgt'].sum(level=event_level)).round(2) out_df[pol_str + '_transpoor_cons'] = ( _df.loc[transient_poor_cons, ['pcwgt', 't_pov_cons']].prod( axis=1).sum(level=event_level) / _df.loc[transient_poor_cons, 'pcwgt'].sum(level=event_level)).round(2) out_df[pol_str + '_midclassdrop_inc'] = 10. - ( _df.loc[middleclass_dropout_inc, ['pcwgt', 't_mc_inc']].prod( axis=1).sum(level=event_level) / _df.loc[middleclass_dropout_inc, 'pcwgt'].sum(level=event_level)).round(2) out_df[pol_str + '_midclassdrop_cons'] = 10. - ( _df.loc[middleclass_dropout_cons, ['pcwgt', 't_mc_cons']].prod( axis=1).sum(level=event_level) / _df.loc[middleclass_dropout_cons, 'pcwgt'].sum(level=event_level)).round(2) # # Number of individuals pushed into poverty or dropped out of midddle class out_df[pol_str + '_transpoor_inc_n'] = _df.loc[transient_poor_inc, 'pcwgt'].sum( level=event_level).astype('int') out_df[pol_str + '_transpoor_cons_n'] = _df.loc[ transient_poor_cons, 'pcwgt'].sum(level=event_level).astype('int') out_df[pol_str + '_midclassdrop_inc_n'] = _df.loc[ middleclass_dropout_inc, 'pcwgt'].sum(level=event_level).astype('int') out_df[pol_str + '_midclassdrop_cons_n'] = _df.loc[ middleclass_dropout_cons, 'pcwgt'].sum(level=event_level).astype('int') # # Fraction of above who recover within 1 year out_df[pol_str + '_transpoor_inc_frac_1yr'] = ( 1E2 * (_df.loc[transient_poor_inc & (_df.t_pov_inc <= 1), 'pcwgt'].sum(level=event_level) / out_df[pol_str + '_transpoor_inc_n'])).round(1) out_df[pol_str + '_transpoor_cons_frac_1yr'] = ( 1E2 * (_df.loc[transient_poor_cons & (_df.t_pov_cons <= 1), 'pcwgt'].sum(level=event_level) / out_df[pol_str + '_transpoor_cons_n'])).round(1) out_df[pol_str + '_midclassdrop_inc_frac_1yr'] = ( 1E2 * (_df.loc[middleclass_dropout_inc & (_df.t_mc_inc > 9), 'pcwgt'].sum(level=event_level) / out_df[pol_str + '_midclassdrop_inc_n'])).round(1) out_df[pol_str + '_midclassdrop_cons_frac_1yr'] = ( 1E2 * (_df.loc[middleclass_dropout_cons & (_df.t_mc_cons > 9), 'pcwgt'].sum(level=event_level) / out_df[pol_str + '_midclassdrop_cons_n'])).round(1) # Fraction of above who recover within 2 years out_df[pol_str + '_transpoor_inc_frac_2yr'] = ( 1E2 * (_df.loc[transient_poor_inc & (_df.t_pov_inc <= 2), 'pcwgt'].sum(level=event_level) / out_df[pol_str + '_transpoor_inc_n'])).round(1) out_df[pol_str + '_transpoor_cons_frac_2yr'] = ( 1E2 * (_df.loc[transient_poor_cons & (_df.t_pov_cons <= 2), 'pcwgt'].sum(level=event_level) / out_df[pol_str + '_transpoor_cons_n'])).round(1) out_df[pol_str + '_midclassdrop_inc_frac_2yr'] = ( 1E2 * (_df.loc[middleclass_dropout_inc & (_df.t_mc_inc > 8), 'pcwgt'].sum(level=event_level) / out_df[pol_str + '_midclassdrop_inc_n'])).round(1) out_df[pol_str + '_midclassdrop_cons_frac_2yr'] = ( 1E2 * (_df.loc[middleclass_dropout_cons & (_df.t_mc_cons > 8), 'pcwgt'].sum(level=event_level) / out_df[pol_str + '_midclassdrop_cons_n'])).round(1) # Fraction of above who recover within 5 years out_df[pol_str + '_transpoor_inc_frac_5yr'] = ( 1E2 * (_df.loc[transient_poor_inc & (_df.t_pov_inc <= 5), 'pcwgt'].sum(level=event_level) / out_df[pol_str + '_transpoor_inc_n'])).round(1) out_df[pol_str + '_transpoor_cons_frac_5yr'] = ( 1E2 * (_df.loc[transient_poor_cons & (_df.t_pov_cons <= 5), 'pcwgt'].sum(level=event_level) / out_df[pol_str + '_transpoor_cons_n'])).round(1) out_df[pol_str + '_midclassdrop_inc_frac_5yr'] = ( 1E2 * (_df.loc[middleclass_dropout_inc & (_df.t_mc_inc > 5), 'pcwgt'].sum(level=event_level) / out_df[pol_str + '_midclassdrop_inc_n'])).round(1) out_df[pol_str + '_midclassdrop_cons_frac_5yr'] = ( 1E2 * (_df.loc[middleclass_dropout_cons & (_df.t_mc_cons > 5), 'pcwgt'].sum(level=event_level) / out_df[pol_str + '_midclassdrop_cons_n'])).round(1) out_df.sort_index(inplace=True) out_df.to_csv('../output_country/{}/recovery_dynamics.csv'.format(myC)) return out_df
import matplotlib.pyplot as plt from libraries.lib_average_over_rp import * from libraries.lib_common_plotting_functions import * from libraries.lib_country_dir import get_all_rps, get_poverty_line, get_currency from libraries.lib_gather_data import match_percentiles, perc_with_spline, reshape_data pairs_pal = sns.color_palette('Paired', n_colors=12) greys_pal = sns.color_palette('Greys', n_colors=9) myCountry = 'PH' try: myCountry = sys.argv[1] except: pass # Get poverty line _pov = get_poverty_line(myCountry) # Decide whether to do this at decile or quintile level agglev = 'decile' try: _q = pd.read_csv('../output_country/'+myCountry+'/sp_comparison_by_'+agglev+'.csv').set_index(agglev) except: # Load file _f = pd.read_csv('../output_country/'+myCountry+'/poverty_duration_no.csv') _f_up = pd.read_csv('../output_country/'+myCountry+'/poverty_duration_unif_poor.csv') if 'quintile' not in _f.columns or 'decile' not in _f.columns: # Assign deciles _deciles=np.arange(0.10, 1.01, 0.10)
] ############################################ # Do quick agriculture plot if True: try: _df = pd.read_csv('../inputs/PH/FIES2015_ompong.csv') except: _df = pd.read_csv('../inputs/PH/FIES2015.csv') _df = _df.loc[_df.eval('(w_regn==1)|(w_regn==2)|(w_regn==14)')] _df.to_csv('../inputs/PH/FIES2015_ompong.csv') _df['pcwgt'] = _df.eval('hhwgt*fsize') _df['agpcinc'] = _df.eval('aginc/fsize') pov_line = get_poverty_line('PH') sf_aginc = (26.8 * _df[['hhwgt', 'aginc']].prod(axis=1).sum() * 1E-9 / 202.2) / (_df[['hhwgt', 'aginc']].prod(axis=1).sum() * 1E-9) # ^ total ag losses from Ompong ^ AG national accounts _df['pcinc_final'] = _df.eval('pcinc-@sf_aginc*agpcinc') # Calculate poverty headcount pov_increase = _df.loc[(_df['pcinc'] > pov_line) & (_df.eval('pcinc-@sf_aginc*agpcinc<=@pov_line')), 'pcwgt'].sum() pov_gap_init = 1. - _df.loc[ _df['pcinc'] < pov_line, ['pcinc', 'pcwgt']].prod(axis=1).sum() / ( _df.loc[_df['pcinc'] < pov_line, 'pcwgt'].sum() * pov_line) pov_gap_final = 1. - ( _df.loc[_df.eval('(pcinc_final<=@pov_line)'),