In [1]:
import numpy as np, pandas as pd, os
from synthicity.utils import misc
from drcog.models import elcm_simulation, hlcm_simulation, regression_model_simulation, dataset
dset = dataset.DRCOGDataset(os.path.join(misc.data_dir(),'drcog.h5'))
np.random.seed(1)

#Variable Library
from drcog.variables import variable_library
variable_library.calculate_variables(dset)
b = dset.buildings
e = dset.establishments
hh = dset.households
zone_refine = pd.read_csv(os.path.join(misc.data_dir(),'zone_demand_refine.csv'))


Fetching parcels
Fetching modify_table
Fetching buildings
Fetching establishments
Fetching modify_table
Fetching modify_table
Fetching households_for_estimation
Fetching modify_table
Fetching households
Fetching modify_table
Fetching zones
Fetching modify_table
Fetching travel_data
Fetching modify_table

In [2]:
p = dset.parcels

In [3]:
p[np.in1d(p.zone_id,[1822,1853,1857,1845,1860,1846,1858,1848,1851])].groupby('zone_id').size()


Out[3]:
zone_id
1846       17
1848        5
1851        7
1853       12
1857        4
1858        3
1860       16
dtype: int64

In [7]:
1853 in p.zone_id.values


Out[7]:
True

In [14]:
new_building_id = dset.buildings.index.values.max() + 1

In [11]:
pid = p.index.values[p.zone_id==1853][0]

newbuildings = pd.DataFrame({'building_type_id':[4],'improvement_value':[10000],'land_area':[200],'non_residential_sqft':[500],
                                             'parcel_id':[pid],'residential_units':[0],'sqft_per_unit':[0],'stories':[0],'tax_exempt':[0],'year_built':[2000],'bldg_sq_ft':[500],
                                             'unit_price_non_residential':[2.0],'unit_price_residential':[0.0], 'building_sqft_per_job':[250.0],
                                             'non_residential_units':[2],'base_year_jobs':[0.0],'all_units':[2]})

In [15]:
newbuildings.index = np.array([new_building_id])

In [18]:
newbuildings.head()


Out[18]:
all_units base_year_jobs bldg_sq_ft building_sqft_per_job building_type_id improvement_value land_area non_residential_sqft non_residential_units parcel_id residential_units sqft_per_unit stories tax_exempt unit_price_non_residential unit_price_residential year_built
1015772 2 0 500 250 4 10000 200 500 2 687322 0 0 0 0 2 0 2000

In [20]:
pd.concat([dset.buildings,newbuildings]).index.values.max()


Out[20]:
1015772

In [23]:
len(pd.concat([dset.buildings,newbuildings]).columns)


Out[23]:
148

In [24]:
len(dset.buildings.columns)


Out[24]:
148

In [25]:
len(dset.buildings)


Out[25]:
881750

In [26]:
dset.d['buildings'] = pd.concat([dset.buildings,newbuildings])
dset.buildings.index.name = 'building_id'

In [27]:
len(dset.buildings)


Out[27]:
881751

In [37]:
z = dset.zones

In [35]:
1822 in p.zone_id.values


Out[35]:
False

In [40]:
zone_id = 1822

In [51]:
county = z.county.values[z.index.values==zone_id][0]
x = z.zonecentroid_x.values[z.index.values==zone_id][0]
y = z.zonecentroid_y.values[z.index.values==zone_id][0]

In [53]:
y


Out[53]:
1696710.0

In [45]:
if county == 'Denver':
    county_id = 8031
elif county == 'Adams':
    county_id = 8001
elif county == 'Arapahoe':
    county_id = 8005
elif county == 'Boulder':
    county_id = 8013
elif county == 'Broomfield':
    county_id = 8014
elif county == 'Clear Creek':
    county_id = 8019
elif county == 'Douglas':
    county_id = 8035
elif county == 'Elbert':
    county_id = 8039
elif county == 'Gilpin':
    county_id = 8047
elif county == 'Jefferson':
    county_id = 8059
elif county == 'Weld':
    county_id = 8123

In [49]:
pid = p.index.values.max()+1

In [54]:
newparcel = pd.DataFrame({'county_id':[county_id],'parcel_sqft':[43560],'land_value':[0],'zone_id':[zone_id],
                                             'centroid_x':[x],'centroid_y':[y],'dist_bus':[6000],'dist_rail':[6000],'in_ugb':[1],'in_uga':[0],
                                             'prop_constrained':[0.0],'acres':[1.0] })

In [56]:
newparcel.index = np.array([pid])

In [59]:
len(p)


Out[59]:
949484

In [58]:
dset.d['parcels'] = pd.concat([dset.parcels,newparcel])

In [61]:
dset.parcels.index.name = 'parcel_id'

In [28]:
count


Out[28]:
<class 'pandas.core.frame.DataFrame'>
Int64Index: 949484 entries, 434515 to 996465
Data columns (total 34 columns):
county_id                       949484  non-null values
parcel_sqft                     949484  non-null values
land_value                      949484  non-null values
zone_id                         949484  non-null values
lu_type_id                      949484  non-null values
centroid_x                      949484  non-null values
centroid_y                      949484  non-null values
tax_exempt_flag                 949484  non-null values
school_district                 949484  non-null values
zoning_id                       949484  non-null values
x                               949484  non-null values
y                               949484  non-null values
dist_bus                        949484  non-null values
dist_rail                       949484  non-null values
in_ugb                          949484  non-null values
in_uga                          949484  non-null values
env_constr_park                 949484  non-null values
env_constr_lake                 949484  non-null values
env_constr_floodplain           949484  non-null values
env_constr_river                949484  non-null values
env_constr_landslide            949484  non-null values
far_id                          949376  non-null values
prop_constrained                949484  non-null values
in_denver                       949484  non-null values
ln_dist_rail                    949484  non-null values
ln_dist_bus                     949484  non-null values
ln_land_value                   949484  non-null values
land_value_per_sqft             949484  non-null values
rail_within_mile                949484  non-null values
cherry_creek_school_district    949484  non-null values
acres                           949484  non-null values
ln_acres                        949484  non-null values
nonres_far                      798789  non-null values
ln_units_per_acre               798789  non-null values
dtypes: float64(17), int32(3), int64(14)

In [84]:
b.groupby('zone_id').residential_units.sum()+zone_index


Out[84]:
zone_id
1            29
2           954
3           677
4           185
5            46
6          1279
7           201
8           537
9          1142
10          583
11         1602
12          923
13          733
14           22
15          564
...
2790         31
2791         67
2792        153
2793        679
2794        NaN
2795        697
2796       1435
2797        194
2798          7
2799         67
2800       1759
2801        227
2802        186
2803        918
2804        167
Length: 2804, dtype: float64

In [85]:
base_ru_zone = b.groupby('zone_id').residential_units.sum()+zone_index

In [87]:
base_ru_zone = base_ru_zone.fillna(0)

In [88]:
base_ru_zone


Out[88]:
zone_id
1            29
2           954
3           677
4           185
5            46
6          1279
7           201
8           537
9          1142
10          583
11         1602
12          923
13          733
14           22
15          564
...
2790         31
2791         67
2792        153
2793        679
2794          0
2795        697
2796       1435
2797        194
2798          7
2799         67
2800       1759
2801        227
2802        186
2803        918
2804        167
Length: 2804, dtype: float64

In [65]:
z.index.values


Out[65]:
array([1656, 1874, 1875, ..., 2802, 2803, 2804], dtype=int64)

In [68]:
base_ru_zone


Out[68]:
zone_id
1            29
2           954
3           677
4           185
5            46
6          1279
7           201
8           537
9          1142
10          583
11         1602
12          923
13          733
14           22
15          564
...
2789        185
2790         31
2791         67
2792        153
2793        679
2795        697
2796       1435
2797        194
2798          7
2799         67
2800       1759
2801        227
2802        186
2803        918
2804        167
Name: residential_units, Length: 2692, dtype: int32

In [69]:
base_ru_zone.index = z.index.values


---------------------------------------------------------------------------
AssertionError                            Traceback (most recent call last)
<ipython-input-69-a798bbcc6e97> in <module>()
----> 1 base_ru_zone.index = z.index.values

c:\Anaconda\lib\site-packages\pandas\lib.pyd in pandas.lib.SeriesIndex.__set__ (pandas\lib.c:29541)()

AssertionError: Index length did not match values

In [71]:
z.index.name


Out[71]:
'zone_id'

In [79]:
z_index = dset.zones.index
zone_index = pd.Series(index=z_index).fillna(0)

In [80]:
zone_index


Out[80]:
zone_id
1656       0
1874       0
1875       0
1876       0
1877       0
1878       0
1879       0
1880       0
1881       0
1882       0
2082       0
2083       0
2084       0
2085       0
2090       0
...
1861       0
1862       0
1863       0
1864       0
1865       0
1866       0
1867       0
2782       0
2783       0
2784       0
2800       0
2801       0
2802       0
2803       0
2804       0
Length: 2804, dtype: float64

In [82]:
base_ru_zone


Out[82]:
zone_id
1            29
2           954
3           677
4           185
5            46
6          1279
7           201
8           537
9          1142
10          583
11         1602
12          923
13          733
14           22
15          564
...
2789        185
2790         31
2791         67
2792        153
2793        679
2795        697
2796       1435
2797        194
2798          7
2799         67
2800       1759
2801        227
2802        186
2803        918
2804        167
Name: residential_units, Length: 2692, dtype: int32

In [81]:
base_ru_zone+zone_index


Out[81]:
zone_id
1            29
2           954
3           677
4           185
5            46
6          1279
7           201
8           537
9          1142
10          583
11         1602
12          923
13          733
14           22
15          564
...
2790         31
2791         67
2792        153
2793        679
2794        NaN
2795        697
2796       1435
2797        194
2798          7
2799         67
2800       1759
2801        227
2802        186
2803        918
2804        167
Length: 2804, dtype: float64

In [3]:
b[np.in1d(b.zone_id,[1822,1853,1857,1845,1860,1846,1858,1848,1851])].groupby('zone_id').size()


Out[3]:
zone_id
1848        6
1851       19
1853       39
1860        4
dtype: int64

In [26]:
print (hh.zone_id==1656).sum()
print (hh.zone_id==1874).sum()
print (e.zone_id==1656).sum()
print (e.zone_id==1874).sum()


2634
342
398
41

In [2]:
def relocate_agents(agents_joined,zone_id,number_of_agents):
    agent_pool = agents_joined[agents_joined.zone_id!=zone_id]
    shuffled_ids = agent_pool.index.values
    np.random.shuffle(shuffled_ids)
    agents_to_relocate = shuffled_ids[:number_of_agents]
    idx_agents_to_relocate = np.in1d(agents_joined.index.values,agents_to_relocate)
    try:
        new_building_id = b[b.zone_id==zone_id].index.values[0]
        agents_joined.building_id[idx_agents_to_relocate] = new_building_id
    except:
        print 'No buildings in specified zone.  Cannot place agents.'
        
def unplace_agents(agents_joined,zone_id,number_of_agents):
    number_of_agents = -number_of_agents #flip the sign
    agent_pool = agents_joined[agents_joined.zone_id==zone_id] ##Notice the equality instead of disequality
    if len(agent_pool) > number_of_agents:
        shuffled_ids = agent_pool.index.values
        np.random.shuffle(shuffled_ids)
        agents_to_relocate = shuffled_ids[:number_of_agents]
        idx_agents_to_relocate = np.in1d(agents_joined.index.values,agents_to_relocate)
        agents_joined.building_id[idx_agents_to_relocate] = -1 #unplace

In [3]:
def relocate_estabs(agents_joined,zone_id,number_of_agents):
    agent_pool = agents_joined[(agents_joined.zone_id!=zone_id)]
    e_sample = agent_pool.reindex(np.random.permutation(agent_pool.index))
    e_to_move = e_sample[np.cumsum(e_sample['employees'].values)<abs(number_of_agents+10)]
    shuffled_ids = e_to_move.index.values
    np.random.shuffle(shuffled_ids)
    agents_to_relocate = shuffled_ids
    idx_agents_to_relocate = np.in1d(agents_joined.index.values,agents_to_relocate)
    try:
        new_building_id = b[b.zone_id==zone_id].index.values[0]
        agents_joined.building_id[idx_agents_to_relocate] = new_building_id
    except:
        print 'No buildings in specified zone.  Cannot place agents.'

In [4]:
def unplace_estabs(agents_joined,zone_id,number_of_agents):
    number_of_agents = -number_of_agents #flip the sign
    agent_pool = agents_joined[agents_joined.zone_id==zone_id] ##Notice the equality instead of disequality
    if agent_pool.employees.sum() > number_of_agents:
        e_sample = agent_pool.reindex(np.random.permutation(agent_pool.index))
        e_to_move = e_sample[np.cumsum(e_sample['employees'].values)<abs(number_of_agents)]
        shuffled_ids = e_to_move.index.values
        np.random.shuffle(shuffled_ids)
        agents_to_relocate = shuffled_ids
        idx_agents_to_relocate = np.in1d(agents_joined.index.values,agents_to_relocate)
        agents_joined.building_id[idx_agents_to_relocate] = -1 #unplace

In [5]:
current_year=2011
for zone in zone_refine.zone_id.values:
    idx_zone = (zone_refine.zone_id==zone)
    hh_shift = zone_refine.annual_hh_shift[idx_zone].values[0]
    emp_shift = zone_refine.annual_emp_shift[idx_zone].values[0]
    if hh_shift > 0:
        relocate_agents(hh,zone,hh_shift)
    if emp_shift > 0:
        relocate_estabs(e,zone,emp_shift)
    if current_year < 2040:
        if hh_shift < 0:
            unplace_agents(hh,zone,hh_shift)
        if emp_shift < 0:
            unplace_agents(e,zone,emp_shift)

In [6]:
variable_library.calculate_variables(dset)
hh = dset.households
e = dset.establishments
print (hh.zone_id==1656).sum()
print (hh.zone_id==1874).sum()
print (e.zone_id==1656).sum()
print (e.zone_id==1874).sum()


2234
400
423
21

In [8]:
zone_refine.head(50)


Out[8]:
zone_id annual_hh_shift annual_emp_shift
0 1656 100 100
1 1874 -20 -20
2 1875 0 0
3 1876 0 0
4 1877 0 0
5 1878 0 0
6 1879 0 0
7 1880 0 0
8 1881 0 0
9 1882 0 0
10 2082 0 0
11 2083 0 0
12 2084 0 0
13 2085 0 0
14 2090 0 0
15 2 200 200
16 2091 0 0
17 2092 0 0
18 2093 0 0
19 2094 0 0
20 2095 0 0
21 2096 0 0
22 2097 0 0
23 2098 0 0
24 2099 0 0
25 2100 0 0
26 2101 0 0
27 2102 0 0
28 2111 0 0
29 2112 0 0
30 2173 0 0
31 2174 0 0
32 2175 0 0
33 2176 0 0
34 2177 0 0
35 2178 0 0
36 2181 0 0
37 2207 0 0
38 2208 0 0
39 2209 0 0
40 2210 0 0
41 2211 0 0
42 2212 0 0
43 2213 0 0
44 2214 0 0
45 2215 0 0
46 2216 0 0
47 2217 0 0
48 2218 0 0
49 2219 0 0

In [45]:
e_sample = e.reindex(np.random.permutation(e.index))
e_sample.index.name = 'establishment_id'

In [47]:
num = 200
e_to_move = e_sample[np.cumsum(e_sample['employees'].values)<abs(num)]

In [49]:
e_to_move.employees.sum()


Out[49]:
90

In [27]:
for zone in zone_refine.zone_id.values:
    idx_zone = (zone_refine.zone_id==zone)
    hh_shift = zone_refine.annual_hh_shift[idx_zone].values[0]
    emp_shift = zone_refine.annual_emp_shift[idx_zone].values[0]
    if hh_shift > 0:
        relocate_agents(hbp,household_set,zone,hh_shift)
    if emp_shift > 0:
        relocate_agents(jbp,job_set,zone,emp_shift)
    if current_year < 2040:
        if hh_shift < 0:
            unplace_agents(hbp,household_set,zone,hh_shift)
        if emp_shift < 0:
            unplace_agents(jbp,job_set,zone,emp_shift)


---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-27-77fa25c3cbdb> in <module>()
      4     emp_shift = zone_refine.annual_emp_shift[idx_zone].values[0]
      5     if hh_shift > 0:
----> 6         relocate_agents(hbp,household_set,zone,hh_shift)
      7     if emp_shift > 0:
      8         relocate_agents(jbp,job_set,zone,emp_shift)

NameError: name 'hbp' is not defined

In [60]:
e.groupby('zone_id').employees.sum()


Out[60]:
zone_id
1            1
2           79
3           76
4           36
5            1
6          211
7          174
8          358
9          388
10         451
11         346
12         142
13         491
15         358
16         236
...
2786        285
2787         29
2788       1187
2789        154
2790        194
2791         81
2792        286
2793        109
2795        487
2796        694
2797        155
2798          8
2799         12
2800         95
2804         26
Name: employees, Length: 2606, dtype: int32

In [62]:
hh.groupby('zone_id').size().tail(20)


Out[62]:
zone_id
2784        435
2785        172
2786        839
2787         76
2788         75
2789        167
2790         32
2791         82
2792         78
2793        298
2795        681
2796       1154
2797         69
2798         22
2799         55
2800        758
2801        184
2802        170
2803        757
2804        143
dtype: int64

In [4]:
dset.store.buildings[['residential_units',]]


Out[4]:
<class 'pandas.core.frame.DataFrame'>
Int64Index: 881751 entries, 55152 to 1015771
Data columns (total 1 columns):
residential_units    881751  non-null values
dtypes: int32(1)

In [2]:
e = establishments.reset_index()
bids = []
eids = []
hbs = []
sids = []
for idx in e.index:
    for job in range(e.employees[idx]):
        bids.append(e.building_id[idx])
        eids.append(e.index[idx])
        hbs.append(e.home_based_status[idx])
        sids.append(e.sector_id[idx])
print len(bids)
print len(eids)
print len(hbs)
print len(sids)
jobs = pd.DataFrame({'tempid':range(1,len(bids)+1),'building_id':bids,'establishment_id':eids,'home_based_status':hbs,'sector_id':sids})


1606882
1606882
1606882
1606882

In [3]:
jobs


Out[3]:
<class 'pandas.core.frame.DataFrame'>
Int64Index: 1606882 entries, 0 to 1606881
Data columns (total 5 columns):
building_id          1606882  non-null values
establishment_id     1606882  non-null values
home_based_status    1606882  non-null values
sector_id            1606882  non-null values
tempid               1606882  non-null values
dtypes: int64(5)

In [4]:
buildings[['residential_units','centroid_x','centroid_y','external_zone_id']]


Out[4]:
<class 'pandas.core.frame.DataFrame'>
Int64Index: 754153 entries, 55152 to 1015771
Data columns (total 4 columns):
residential_units    754153  non-null values
centroid_x           754153  non-null values
centroid_y           754153  non-null values
external_zone_id     754153  non-null values
dtypes: int32(2), int64(2)

In [5]:
np.unique(buildings.external_zone_id)


Out[5]:
building_id
12617          101010
13011          101020
12491          101030
117777         101040
17617          101050
2589           101060
2103           101070
35744          101080
18842          101090
12033          101100
20053          101110
21339          101120
27209          101130
13294          102010
13726          102020
...
516621         802480
610774         802490
566786         802501
553868         802502
608588         802510
513063         802520
557639         802530
570779         802540
564022         802550
62597          803050
55589          803060
43937          803080
103466         803090
120567         803100
20747          803110
Name: external_zone_id, Length: 2681, dtype: int32

In [13]:
jobs['x'] = buildings.centroid_x[jobs.building_id].values
jobs['y'] = buildings.centroid_y[jobs.building_id].values
jobs['taz05_id'] = buildings.external_zone_id[jobs.building_id].values
jobs['sector_id_six'] = 1*(jobs.sector_id==61) + 2*(jobs.sector_id==71) + 3*np.in1d(jobs.sector_id,[11,21,22,23,31,32,33,42,48,49]) + 4*np.in1d(jobs.sector_id,[7221,7222,7224]) + 5*np.in1d(jobs.sector_id,[44,45,7211,7212,7213,7223]) + 6*np.in1d(jobs.sector_id,[51,52,53,54,55,56,62,81,92])
jobs['jobtypename'] = ''
jobs.jobtypename[jobs.sector_id_six==1] = 'Education'
jobs.jobtypename[jobs.sector_id_six==2] = 'Entertainment'
jobs.jobtypename[jobs.sector_id_six==3] = 'Production'
jobs.jobtypename[jobs.sector_id_six==4] = 'Restaurant'
jobs.jobtypename[jobs.sector_id_six==5] = 'Retail'
jobs.jobtypename[jobs.sector_id_six==6] = 'Service'
jobs['urbancenter_id'] = 0

In [5]:
np.unique(dset.zones['external_zone_id'].values)


Out[5]:
array([101010, 101020, 101030, ..., 803100, 803110, 803120])

In [14]:
jobs.tail()


Out[14]:
building_id establishment_id home_based_status sector_id tempid x y taz05_id sector_id_six jobtypename urbancenter_id
1606877 367318 202188 1 81 1606878 3075432 1244915 107080 6 Service 0
1606878 256128 202189 1 23 1606879 3132919 1655657 508250 3 Production 0
1606879 399735 202190 1 52 1606880 3003022 1240716 101110 6 Service 0
1606880 871464 202191 1 54 1606881 3037898 1750666 201010 6 Service 0
1606881 524578 202192 1 54 1606882 3127188 1667861 408220 6 Service 0

In [10]:
jobs[jobs.building_id==-1]


Out[10]:
<class 'pandas.core.frame.DataFrame'>
Int64Index: 1849 entries, 1564619 to 1606861
Data columns (total 8 columns):
building_id          1849  non-null values
establishment_id     1849  non-null values
home_based_status    1849  non-null values
sector_id            1849  non-null values
tempid               1849  non-null values
x                    0  non-null values
y                    0  non-null values
taz05_id             0  non-null values
dtypes: float64(3), int64(5)

In [32]:
establishments[establishments.building_id==-1]


Out[32]:
<class 'pandas.core.frame.DataFrame'>
Int64Index: 1849 entries, 406861368 to 416085617
Data columns (total 11 columns):
building_id             1849  non-null values
home_based_status       1849  non-null values
infogroup_id            1849  non-null values
qcew_id                 1849  non-null values
sector_id               1849  non-null values
employees               1849  non-null values
zone_id                 0  non-null values
county_id               0  non-null values
sector_id_six           1849  non-null values
sector_id_retail_agg    1849  non-null values
nonres_sqft             0  non-null values
dtypes: float64(3), int32(7), object(1)

In [2]:
alternatives = buildings[(buildings.residential_units>0)]

hlcm_simulation.simulate(dset, year=sim_year,
                         depvar = 'building_id',alternatives=alternatives,simulation_table = 'households',output_names = ("drcog-coeff-hlcm-%s.csv","DRCOG HOUSEHOLD LOCATION CHOICE MODELS (%s)","hh_location_%s","household_building_ids"),
                         agents_groupby= ['income_3_tenure',],
                         transition_config = {'Enabled':True,'control_totals_table':'annual_household_control_totals','scaling_factor':1.0},
                         relocation_config = {'Enabled':True,'relocation_rates_table':'annual_household_relocation_rates','scaling_factor':1.0},
                         )


Fetching annual_household_control_totals
Fetching modify_table
total_number_of_households    23538
persons                       45227
dtype: int32
Adding agents to match target of 68765 
Histogram of agents by year:
2011    23538
dtype: int64
Histogram of relocation rates:
0.00148    156891
0.00000    151587
0.00309    128645
0.00274    126702
0.00145    113687
0.00311     75448
0.00183     64158
0.00348     59075
0.00540     55346
0.00184     49096
0.00110     39096
0.00220     30741
0.00402     26679
0.00099     19702
0.00130     17099
0.00124     16598
0.00199     12906
0.00111     12166
0.00132     12111
0.00117      9626
0.00222      6566
dtype: int64
2448 agents are moving
Total new agents and movers = 25947
Assigning units to 6208 agents of segment 1
Assigning units to 7307 agents of segment 2
Assigning units to 4900 agents of segment 3
Assigning units to 3803 agents of segment 4
Assigning units to 3729 agents of segment 5
Assigned 25947 agents to 13344 locations with 0 unplaced

In [3]:
alternatives = buildings[(buildings.non_residential_sqft>0)]

elcm_simulation.simulate(dset, year=sim_year, 
                         depvar = 'building_id',alternatives=alternatives,simulation_table = 'establishments',output_names = ("drcog-coeff-elcm-%s.csv","DRCOG EMPLOYMENT LOCATION CHOICE MODELS (%s)","emp_location_%s","establishment_building_ids"),
                         agents_groupby= ['sector_id_retail_agg',],
                         transition_config = {'Enabled':True,'control_totals_table':'annual_employment_control_totals','scaling_factor':1.0})


Fetching annual_employment_control_totals
Fetching modify_table
                                 total_number_of_jobs
sector_id_six home_based_status                      
1             1                                  7276
2             1                                 23908
3             1                                  5197
4             1                                 10395
5             1                                 13513
6             1                                 62368
1             0                                 95218
2             0                                312859
3             0                                 68013
4             0                                136026
5             0                                176833
6             0                                816154
Adding agents to match target of 1727760 
action
6680
action
23031
action
-10467
action
10306
action
10690
action
34805
action
-16660
action
280118
action
-271403
action
10785
action
-15220
action
58213
Histogram of agents by year:
2011    106692
dtype: int64
Total new agents and movers = 108209
Assigning units to 23 agents of segment 11
Assigning units to 239 agents of segment 23
Assigning units to 3 agents of segment 31
Assigning units to 2 agents of segment 33
Assigning units to 10 agents of segment 42
Assigning units to 5445 agents of segment 44
Assigning units to 4937 agents of segment 45
Assigning units to 55 agents of segment 48
Assigning units to 3 agents of segment 49
Assigning units to 1064 agents of segment 51
Assigning units to 4134 agents of segment 52
Assigning units to 5522 agents of segment 53
Assigning units to 16194 agents of segment 54
Assigning units to 189 agents of segment 55
Assigning units to 5727 agents of segment 56
Assigning units to 6699 agents of segment 61
Assigning units to 2672 agents of segment 62
Assigning units to 37769 agents of segment 71
WARNING: not enough options to fit agents, will result in unplaced agents
Assigning units to 6095 agents of segment 81
Assigning units to 43 agents of segment 92
WARNING: not enough options to fit agents, will result in unplaced agents
WARNING: not enough options to fit agents, will result in unplaced agents
Assigning units to 203 agents of segment 7211
Assigning units to 3134 agents of segment 7221
Assigning units to 3220 agents of segment 7222
Assigning units to 204 agents of segment 7223
Assigning units to 4623 agents of segment 7224
Assigned 108209 agents to 25015 locations with 25416 unplaced

In [4]:
regression_model_simulation.simulate(dset, year=sim_year, output_varname='unit_price_residential',
                                     simulation_table='buildings', output_names = ["drcog-coeff-reshedonic-%s.csv","DRCOG RESHEDONIC MODEL (%s)","resprice_%s"],
                                     agents_groupby = 'building_type_id', segment_ids = [2,3,20,24])


Generating rents on 24111 buildings
Generating rents on 14447 buildings
Generating rents on 740753 buildings
Generating rents on 31263 buildings

In [5]:
regression_model_simulation.simulate(dset, year=sim_year, 
                                     output_varname='unit_price_non_residential', simulation_table='buildings', output_names = ["drcog-coeff-nrhedonic-%s.csv","DRCOG NRHEDONIC MODEL (%s)","nrprice_%s"],
                                     agents_groupby = 'building_type_id', segment_ids = [5,8,11,16,17,18,21,23,9,22])


Generating rents on 7211 buildings
Generating rents on 845 buildings
Generating rents on 2736 buildings
Generating rents on 1029 buildings
Generating rents on 1808 buildings
Generating rents on 2394 buildings
Generating rents on 11837 buildings
Generating rents on 1280 buildings
Generating rents on 11726 buildings
Generating rents on 790 buildings

In [6]:
developer_configuration = {
                           'enforce_environmental_constraints':True,
                           'enforce_allowable_use_constraints':True,
                           'enforce_ugb':False,
                           'outside_ugb_allowable_density':1.0,
                           'uga_policies':False,
                           'inside_uga_allowable_density':1.0,
                           'max_allowable_far_field_name':'far',
                           'land_property_acquisition_cost_factor':1.0,
                           'profit_factor':1.0,
                           'min_building_sqft':400,
                           'min_lot_sqft':500,
                           'zonal_levers':True
                           }

from urbandeveloper import proforma_developer_model
buildings, newbuildings = proforma_developer_model.run(dset,hh_zone1,emp_zone1,developer_configuration,sim_year)
dset.d['buildings'] = pd.concat([buildings,newbuildings])


industrial Fri Apr 11 17:30:14 2014
residential Fri Apr 11 17:30:18 2014
retail Fri Apr 11 17:30:21 2014
office Fri Apr 11 17:30:25 2014
Fetching zoning
Fetching modify_table
Fetching fars
Fetching modify_table
2 294.689887129
3 294.689887129
5 111.465344077
9 27.8990690346
17 89.2201291344
18 89.2201291344
20 288.672116156
22 31.784575281
24 294.666027128
2335 new buildings for btyp 3
2491 new buildings for btyp 2
2561 new buildings for btyp 24
2458 new buildings for btyp 20
306 new buildings for btyp 5
310 new buildings for btyp 17
228 new buildings for btyp 18
122 new buildings for btyp 9
103 new buildings for btyp 22
       building_type_id  building_sqft   lot_size  residential_units  \
count          10566.00       10566.00   10566.00           10566.00   
mean              12.60        3275.62   22177.25               1.78   
std                9.61        8049.87   35870.86               3.41   
min                2.00         400.00     513.00               0.00   
25%                3.00         623.03    6225.25               1.00   
50%               17.00         835.80    8249.50               1.00   
75%               20.00        1752.60   15459.50               1.00   
max               24.00      146882.19  199818.00              90.00   

       net_residential_units  
count               10566.00  
mean                   -0.54  
std                     9.87  
min                  -216.00  
25%                     0.00  
50%                     0.00  
75%                     1.00  
max                    90.00  
building_type_id
2                   5203096.33
3                   6297830.38
5                   4739535.87
9                   1567143.18
17                  3915675.79
18                  2255506.63
20                  4318938.28
22                  1437377.25
24                  4875112.35
Name: building_sqft, dtype: float64
building_type_id
2                   4733
3                   5354
5                      0
9                      0
17                     0
18                     0
20                  4141
22                     0
24                  4535
Name: residential_units, dtype: float64
Applying price shifts

In [7]:
# sim_year = 2015
# urbancanvas_scenario_id = 0

# def export_to_urbancanvas(building_df,current_year,urbancanvas_scenario_id):
#     import pandas.io.sql as sql
#     import psycopg2
#     import cStringIO
#     conn_string = "host='paris.urbansim.org' dbname='denver' user='drcog' password='M0untains#' port=5433"
#     conn=psycopg2.connect(conn_string)
#     cur = conn.cursor()
    
#     if urbancanvas_scenario_id == 0:
#         query = "select nextval('developmentproject_id_seq');"
#         nextval = sql.read_frame(query,conn)
#         nextval = nextval.values[0][0]
#         query = "select max(id)+1 from scenario_project;"
#         id = sql.read_frame(query,conn)
#         id = id.values[0][0]
#         query = "INSERT INTO scenario(id, name) VALUES(%s, 'Run #%s');" % (nextval,nextval)
#         cur.execute(query)
#         conn.commit()
#         query = "INSERT INTO scenario_project(id, scenario, project) VALUES(%s, %s, 1);" % (id,nextval)
#         cur.execute(query)
#         conn.commit()
#         query = "select max(id)+1 from scenario_project;"
#         id = sql.read_frame(query,conn)
#         id = id.values[0][0]
#         query = "INSERT INTO scenario_project(id, scenario, project) VALUES(%s, %s, %s);" % (id,nextval,nextval)
#         cur.execute(query)
#         conn.commit()
#     else:
#         nextval = urbancanvas_scenario_id
#     nextval_string = '{' + str(nextval) + '}'
#     building_df['projects'] = nextval_string
    
#     valid_from = '{' + str(current_year) + '-1-1}'
#     building_df['valid_from'] = valid_from
#     building_df['land_area'] = 0
#     building_df['tax_exempt'] = 0
#     building_df['srcparc_id'] = '0'
#     building_df['building_id'] = building_df.index.values
#     building_df['stories'] = 30 ###For testing!
#     del building_df['unit_price_residential']
#     del building_df['unit_price_non_residential']
#     del building_df['building_sqft_per_job']
#     del building_df['base_year_jobs']
#     del building_df['non_residential_units']
#     del building_df['all_units']
    
#     print 'Exporting %s buildings to Urbancanvas database for project %s and year %s.' % (building_df.index.size,nextval,current_year)
#     output = cStringIO.StringIO()
#     building_df.to_csv(output, sep='\t', header=False, index=False)
#     output.seek(0)
#     cur.copy_from(output, 'building_footprints', columns =tuple(building_df.columns.values.tolist()))
#     conn.commit()
    
#     return nextval

# nv = export_to_urbancanvas(newbuildings, sim_year, urbancanvas_scenario_id)

In [8]:
urbancanvas_scenario_id = 0
from drcog.variables import urbancanvas_export
nv = urbancanvas_export.export_to_urbancanvas(newbuildings, sim_year, urbancanvas_scenario_id)


Exporting 10566 buildings to Urbancanvas database for project 160 and year 2011.

In [9]:
nv


Out[9]:
160