# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3
#     language: python
#     name: python3
# ---

# # Sectorial need calculations
#
# Calculate the needs of each household for each sector based on the answers to the survey.
# In this notebook Protection index is computed

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
sns.set_style('whitegrid')
# %matplotlib inline

df = pd.read_excel('../../data/raw/reach_nga_msna_clean_dataset_final.xlsx',sheet_name='clean_hh_data') 
# Remove respondents who refuse to take the interview
df = df[df['Consent'] == 'Yes, agrees to interview']

# ### Create useful functions

# +
# Create alphabet
# alphabet: list of length 26 with all characters in capital letters
alphabet = []
for letter in range(97,123):
    alphabet.append(chr(letter).upper())
    
# Create list of excel's sheet column headers
# columns_index: list with all column headers in excel (A, B, .. AA, AB..) in order
columns_index = []
for letter1 in alphabet:
    columns_index.append(letter1)
for letter1 in alphabet:
    for letter2 in alphabet:
        columns_index.append(letter1+letter2)
for letter1 in alphabet:
    for letter2 in alphabet:
        for letter3 in alphabet:
            columns_index.append(letter1+letter2+letter3)
            
# Create dictionary that matches excel's headers and pandas column names
# index2name: dictionary where keys are excel headers (RG), values are pandas column names
index2name = {}
for i, index in enumerate(columns_index):
    index2name[index] = df. columns[i]
    if index == 'AJK':
        break
        
def add_column_range_names(columns, column_ranges):
    """Add the names of the columns in your range of interest to the columns list
    columns: list of names of columns of interest
    column_ranges: list of pairs of initial and final indices of ranges of columns of interest"""
    for i, column_range in enumerate(column_ranges):
        initial_index = columns_index.index(column_range[0])
        final_index = columns_index.index(column_range[1])
        for ii in range(initial_index, final_index+1):
            columns.append(index2name[columns_index[ii]])
    return columns

#def reset_df(df)


# -

# ### 1) Shelter sector (MARCO)

# <table style="width:100%">
#   <tr>
#     <th>Indicator</th>
#     <th>Dataset header</th> 
#     <th>Weighting</th>
#   </tr>
#   <tr>
#     <td>HH lives in an inadequate shelter</td>
#     <td>'What is the type of shelter?'</td>
#     <td>2</td>
#   </tr>
#   <tr>
#     <td>HH shelter is damaged (partially or completely)</td>
#     <td>'Is the shelter damaged?'</td>
#     <td>2</td>
#   </tr>
#   <tr>
#     <td>HH is at risk of eviction</td>
#     <td>Are you at risk of being evicted or forced to leave this shelter within the next</td>
#     <td>2</td>
#   </tr>
#   <tr>
#     <td>HH owns less than half of items from basic NFI kit</td>
#     <td>'between VZ and WV (sum of "yes" / all items)'</td>
#     <td>2</td>
#   </tr>
#   <tr>
#     <td>HH shares shelter with 2 or more families</td>
#     <td>'How many households, including yours, share your current accomodation? '</td>
#     <td>2</td>
#   </tr>
# </table>
#
# </body>
# </html>
#

# +
 # Define the columns of interest
Shelter_columns = ['What is the type of shelter?',
               'Is the shelter damaged?',
               'Are you at risk of being evicted or forced to leave this shelter within the next',
               'How many households, including yours, share your current accomodation? ']

# Define the column ranges of interest
# list containing the pairs of initial and last columns to be included in the tage
Shelter_column_ranges = [['VZ', 'WV']]

# Add column names for the ranges to your list of columns of interest
Shelter_columns = add_column_range_names(Shelter_columns, Shelter_column_ranges)
# -

df_Shelter = []
df_Shelter = df[Shelter_columns].copy()


# ##### Question 1)What is the type of shelter? HH lives in an inadequate shelter

question = 'What is the type of shelter?'

np.unique(list(df_Shelter[question]))

# +
#Run this only once
df_Shelter['index_Shelter'] = (((df_Shelter[question]== 'Collective shelter (mosque, school or other public building)')*2 + (df_Shelter[question]== 'Emercapture gency or transitional shelter by an acapture gency')*2 +
                            (df_Shelter[question]== 'Makeshift (thatch house with collected materials)')*2 +
                            (df_Shelter[question]== 'No shelter / sleeps in the open space')*2 +
                            (df_Shelter[question]== 'Tent')*2)  >1)*2
df_Shelter['index_Shelter'].sum()

                          
# -

# ##### Question 2)Is the shelter damaged?

question = 'Is the shelter damaged?'

np.unique(list(df_Shelter[question]))

# +

print(((df_Shelter[question] == 1)*2).sum())
df_Shelter['index_Shelter'] = df_Shelter['index_Shelter']+(df_Shelter[question] == 1)*2
df_Shelter['index_Shelter'].sum()
# -

# ##### Question 3)Are you at risk of being evicted or forced to leave this shelter within the next

question = 'Are you at risk of being evicted or forced to leave this shelter within the next'

np.unique(list(df_Shelter[question]))

# +
#Run this only once

df_Shelter['index_Shelter'] = df_Shelter['index_Shelter']+(df_Shelter[question] == 1)*2
print(((df_Shelter[question] == 1)*2).sum())
df_Shelter['index_Shelter'].sum()
# -

# ##### Question 4)'How many households, including yours, share your current accomodation?'

question = 'How many households, including yours, share your current accomodation? '


np.unique(list(df_Shelter[question]))

# +


df_Shelter['index_Shelter'] = df_Shelter['index_Shelter']+((df_Shelter[question]>2)*2)
                          
df_Shelter['index_Shelter'].sum()
# -

# ##### Question 5)HH owns less than half of items from basic NFI kit

columns = add_column_range_names([], [['VZ', 'WV']])
indicator_weight = 2
columns

df_Shelter_NFI = (df_Shelter == 'Yes')
df_Shelter_NFI

condition_0_columns = ['NFIS OWNED - Blankets',
 'NFIS OWNED - Sleeping mat',
 'NFIS OWNED - Mosquito net',
 'NFIS OWNED - Jerry cans',
 'NFIS OWNED - Laundry detercapture gent / bars',
 'NFIS OWNED - Bath soap',
 'NFIS OWNED - Reusable sanitary pad',
 'NFIS OWNED - Solar lamp',
 'NFIS OWNED - Foldable mattress',
 'NFIS OWNED - Kettle',
 'NFIS OWNED - 10L Basin',
 'NFIS OWNED - Rope',
 'NFIS OWNED - Cooking pots',
 'NFIS OWNED - Stainless trays',
 'NFIS OWNED - Stainless cups',
 'NFIS OWNED - Serving spoons',
 'NFIS OWNED - Kitchen knife',
 'NFIS OWNED - 10L Bucket',
 'NFIS OWNED - Aquatabs',
 'NFIS OWNED - School bags',
 'NFIS OWNED - School notebooks',
 'NFIS OWNED - School textbooks']
df_Shelter['index_Shelter'] = df_Shelter['index_Shelter']+(((df_Shelter_NFI[condition_0_columns].sum(axis=1)<11)*2+ (df_Shelter['NFIS OWNED - None'] == 'Yes')*2)>1)*2

#Be sure that at the beginning this was empty
#df_Shelter['index_Shelter'] = df_Shelter['index_Shelter']+(df_Shelter_NFI[condition_0_columns].sum(axis=1)<11)*2 + (df_Shelter['NFIS OWNED - None'] == 'Yes')*2
#df_Shelter['index_Shelter'].sum()
(((df_Shelter_NFI[condition_0_columns].sum(axis=1)<11)*2+ (df_Shelter['NFIS OWNED - None'] == 'Yes')*2)>1)*2

(df_Shelter['index_Shelter']>4).sum()

(df_Shelter['index_Shelter']>10).sum()

severity = pd.read_csv('severity.csv')
severity['Shelter/NFI'] = df_Shelter['index_Shelter'].values
severity.to_csv('severity.csv')





StarcoderdataJupyterScriptsDedupFiltered