<jupyter_start><jupyter_text>Exercise 2 - Building vulnerbaility functions In this exercise, we will build a simple vulnerability function using the assumption in impact function from Emanuel (2011), availabile in equation 12 here https://gmd.copernicus.org/articles/12/3085/2019/  import python libraries<jupyter_code>import math
import pandas as pd<jupyter_output><empty_output><jupyter_text>retrieve intensity bins<jupyter_code>df_intensity = pd.read_csv('model_data/intensity_bin_dict.csv')

df_intensity<jupyter_output><empty_output><jupyter_text>apply the equation<jupyter_code># function to calculate damage factor f_ij from max windspeed v_ij
def get_damage_factor(v_ij,v_thresh,v_half):
    v_ij = max(v_ij - v_thresh,0) / (v_half - v_thresh)
    f_ij = (v_ij**3) / (1 + v_ij**3)
    return f_ij
#vulnerability functions
vulnerability_functions = {'residential': {'vulnerability_id':1, 'v_thresh': 25.7, 'v_half': 74.7}}

vulnerability_functions
# generate vulnerability file

mps_to_knots = 1.94384

v_lst = []
d_lst = []

for v in vulnerability_functions:
    v_id = vulnerability_functions[v]['vulnerability_id']
    v_thresh = vulnerability_functions[v]['v_thresh'] * mps_to_knots
    v_half = vulnerability_functions[v]['v_half'] * mps_to_knots
    
    for index,row in df_intensity.iterrows():
        intensity = row['intensity_bin_index']
        damage_bin = intensity
        ws_from = (row['max_windspeed'] - 2.5)
        ws_to = (row['max_windspeed'] + 2.5)
        damage_from = get_damage_factor(ws_from,v_thresh,v_half)
        damage_to = get_damage_factor(ws_to,v_thresh,v_half)
        
        v_row = [v_id,intensity,damage_bin]
        v_lst.append(v_row)
        
        d_row = [damage_bin,damage_from,damage_to]
        d_lst.append(d_row)
        
df_vuln = pd.DataFrame(data=v_lst,columns=['vulnerability_id','intensity_bin_index','damage_bin_index'],dtype='int')
df_vuln['prob']=1
df_vuln.to_csv('model_data/vulnerability.csv',index=False)

df_vuln<jupyter_output><empty_output><jupyter_text>discretise the damage factors<jupyter_code># generate damage file

df_damage = pd.DataFrame(data=d_lst,columns=['damage_bin_index','damage_from','damage_to'])
df_damage['interpolation']=df_damage['damage_from'] + (df_damage['damage_to']-df_damage['damage_from'])/2
df_damage.to_csv('model_data/damage_bin_dict.csv',index=False)

df_damage<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.11551292743953294, 'fraction_numerical': 0.016680567139282735, 'mean_word_length': 5.73876404494382, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 17, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '4', 'dir': 'jupyter-structured-clean-dedup', 'id': '473798', 'license': 'BSD-3-Clause', 'n_tokens_mistral': 938, 'n_tokens_neox': 902, 'n_words': 137, 'path': 'Exercise_2_Vulnerability.ipynb', 'repo_name': 'OasisLMF/Workshop2021', 'size': '2398'}