# ---
# 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
# ---

import pandana as pdna
import numpy as np

from urbansim.utils import misc, networks
from scripts import datasources
from scripts import models
from scripts import variables
from urbansim_templates import modelmanager as mm
from urbansim_templates.models import LargeMultinomialLogitStep
import orca
import os; os.chdir('../')
import warnings;warnings.simplefilter('ignore')

# %%time
orca.run(['initialize_network_walk'])

# %%time
orca.run(['network_aggregations_walk'])

nodeswalk = orca.get_table('nodeswalk').to_frame()

parcels = orca.get_table('parcels').to_frame()

merged = pd.merge(nodeswalk, parcels, left_index=True, right_on='node_id_walk')

len(merged)

# +
nodeswalk_upper = nodeswalk.quantile(.99)
nodeswalk_c = nodeswalk.clip_upper(nodeswalk_upper, axis=1)

nodeswalk_c['prop_children_500_walk'] = (nodeswalk_c['children_500_walk'] > 0).astype(int) / nodeswalk_c['hh_500_walk']
nodeswalk_c['prop_singles_500_walk'] = nodeswalk_c['singles_500_walk'] / nodeswalk_c['hh_500_walk']
nodeswalk_c['prop_elderly_500_walk'] = nodeswalk_c['elderly_hh_500_walk'] / nodeswalk_c['hh_500_walk']
nodeswalk_c['prop_black_500_walk'] = nodeswalk_c['pop_black_500_walk'] / nodeswalk_c['pop_500_walk']
nodeswalk_c['prop_white_500_walk'] = nodeswalk_c['pop_white_500_walk'] / nodeswalk_c['pop_500_walk']
nodeswalk_c['prop_asian_500_walk'] = nodeswalk_c['pop_asian_500_walk'] / nodeswalk_c['pop_500_walk']
nodeswalk_c['prop_hisp_500_walk'] = nodeswalk_c['pop_hisp_500_walk'] / nodeswalk_c['pop_500_walk']
nodeswalk_c['prop_rich_500_walk'] = nodeswalk_c['rich_500_walk'] / nodeswalk_c['pop_500_walk']
nodeswalk_c['prop_poor_500_walk'] = nodeswalk_c['poor_500_walk'] / nodeswalk_c['pop_500_walk']

nodeswalk_c['prop_children_1500_walk'] = (nodeswalk_c['children_1500_walk'] > 0).astype(int)/nodeswalk_c['hh_1500_walk']
nodeswalk_c['prop_singles_1500_walk'] = nodeswalk_c['singles_1500_walk'] / nodeswalk_c['hh_1500_walk']
nodeswalk_c['prop_elderly_1500_walk'] = nodeswalk_c['elderly_hh_1500_walk'] / nodeswalk_c['hh_1500_walk']
nodeswalk_c['prop_black_1500_walk'] = nodeswalk_c['pop_black_1500_walk'] / nodeswalk_c['pop_1500_walk']
nodeswalk_c['prop_white_1500_walk'] = nodeswalk_c['pop_white_1500_walk'] / nodeswalk_c['pop_1500_walk']
nodeswalk_c['prop_asian_1500_walk'] = nodeswalk_c['pop_asian_1500_walk'] / nodeswalk_c['pop_1500_walk']
nodeswalk_c['prop_hisp_1500_walk'] = nodeswalk_c['pop_hisp_1500_walk'] / nodeswalk_c['pop_1500_walk']
nodeswalk_c['prop_rich_1500_walk'] = nodeswalk_c['rich_1500_walk'] / nodeswalk_c['pop_1500_walk']
nodeswalk_c['prop_poor_1500_walk'] = nodeswalk_c['poor_1500_walk'] / nodeswalk_c['pop_1500_walk']

nodeswalk_c['pop_jobs_ratio_1500_walk'] = nodeswalk_c['pop_1500_walk'] / (nodeswalk_c['jobs_500_walk']+1)
nodeswalk_c['avg_hhs_500_walk'] = nodeswalk_c['pop_500_walk'] / (nodeswalk_c['hh_500_walk']+1)
nodeswalk_c['avg_hhs_1500_walk'] = nodeswalk_c['pop_1500_walk'] / (nodeswalk_c['hh_1500_walk']+1)

nodeswalk_c['pop_jobs_ratio_1500_walk'] = nodeswalk_c['pop_1500_walk'] / (nodeswalk_c['jobs_500_walk']+1)
nodeswalk_c['avg_hhs_500_walk'] = nodeswalk_c['pop_500_walk'] / (nodeswalk_c['hh_500_walk']+1)
nodeswalk_c['avg_hhs_1500_walk'] = nodeswalk_c['pop_1500_walk'] / (nodeswalk_c['hh_1500_walk']+1)
# -

len(pd.merge(nodeswalk_c, parcels, left_index=True, right_on='node_id_walk'))

nodeswalk_c.to_csv('/home/data/2018-07/nodeswalk_c_max.csv')

nodeswalk = pd.read_csv('/home/data/2018-07/nodeswalk_c_max.csv', index_col='osmid')

len(pd.merge(nodeswalk, parcels, left_index=True, right_on='node_id_walk'))

# %%time
orca.run(['initialize_network_small'])

# %%time
orca.run(['network_aggregations_small'])

nodessmall = orca.get_table('nodessmall').to_frame()

merged = pd.merge(nodessmall, parcels, left_index=True, right_on='node_id_small')

len(merged)

nodessmall_upper = nodessmall.quantile(.99)
nodessmall_c = nodessmall.clip_upper(nodessmall_upper, axis=1)
nodessmall_c['pop_jobs_ratio_10000'] = nodessmall_c['pop_10000'] / (nodessmall_c['jobs_10000'] + 1)
nodessmall_c['pop_jobs_ratio_25000'] = nodessmall_c['pop_25000'] / (nodessmall_c['jobs_25000'] + 1)

merged = pd.merge(nodessmall, parcels, left_index=True, right_on='node_id_small')

len(merged)

nodessmall_c.to_csv('/home/data/2018-07/nodessmall_c_max.csv')

orca.

StarcoderdataJupyterScriptsDedupFiltered