Skip to content

Commit

Permalink
Merge pull request #1 from urbansim/python3
Browse files Browse the repository at this point in the history
PEP 8 formatting and Python 3 compatibility
  • Loading branch information
pksohn authored Apr 3, 2017
2 parents 37d0757 + 86ba171 commit 51fe712
Show file tree
Hide file tree
Showing 4 changed files with 93 additions and 22 deletions.
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -86,4 +86,7 @@ ENV/
.spyderproject

# Rope project settings
.ropeproject
.ropeproject

# PyCharm
.idea
33 changes: 33 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
language: python
sudo: false
python:
- '2.7'
- '3.5'

install:
- if [[ "$TRAVIS_PYTHON_VERSION" == "2.7" ]]; then wget http://repo.continuum.io/miniconda/Miniconda-latest-Linux-x86_64.sh
-O miniconda.sh; else wget http://repo.continuum.io/miniconda/Miniconda3-latest-Linux-x86_64.sh
-O miniconda.sh; fi
- bash miniconda.sh -b -p $HOME/miniconda
- export PATH="$HOME/miniconda/bin:$PATH"
- hash -r
- conda config --set always_yes yes --set changeps1 no
- conda update -q conda
- conda info -a
- |
conda create -q -n test-environment python=$TRAVIS_PYTHON_VERSION pip numpy pandas pytest matplotlib scipy statsmodels pyyaml pycodestyle
- source activate test-environment
- conda list
# UPDATE THESE WHEN LATEST VERSIONS PUSHED TO PIP
- pip install https://github.com/UDST/orca/archive/master.zip
- pip install https://github.com/pksohn/urbansim/archive/python3.zip
- pip install .
# UPDATE BRANCH LATER
- cd .. && git clone -b pep8 git@github.com:urbansim/zone_model.git
- cd zone_model && pip install .
- cd zone_model/data && curl -O https://storage.googleapis.com/urbansim/zone_model/model_data.h5
- cd $TRAVIS_BUILD_DIR && pip install .

script:
- pycodestyle variable_generators
- cd ../zone_model/zone_model/ && python simulate.py
2 changes: 1 addition & 1 deletion variable_generators/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
version = __version__ = '0.1dev'
version = __version__ = '0.1dev'
75 changes: 55 additions & 20 deletions variable_generators/generators.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from __future__ import print_function

import numpy as np
import pandas as pd

Expand All @@ -17,10 +19,12 @@ def make_agg_var(agent, geog, geog_id, var_to_aggregate, agg_function):
Generator function for aggregation variables. Registers with orca.
"""
var_name = agg_function + '_' + var_to_aggregate

@orca.column(geog, var_name, cache=False)
def func():
agents = orca.get_table(agent)
print 'Calculating %s of %s for %s' % (var_name, agent, geog)
print('Calculating {} of {} for {}'
.format(var_name, agent, geog))

groupby = agents[var_to_aggregate].groupby(agents[geog_id])
if agg_function == 'mean':
Expand All @@ -35,7 +39,9 @@ def func():
locations_index = orca.get_table(geog).index
series = pd.Series(data=values, index=locations_index)

# Fillna. For certain functions, must add other options, like puma value or neighboring value
# Fillna.
# For certain functions, must add other options,
# like puma value or neighboring value
if agg_function == 'sum':
series = series.fillna(0)
else:
Expand All @@ -46,30 +52,37 @@ def func():

return func

def make_disagg_var(from_geog_name, to_geog_name, var_to_disaggregate, from_geog_id_name):

def make_disagg_var(from_geog_name, to_geog_name, var_to_disaggregate,
from_geog_id_name):
"""
Generator function for disaggregating variables. Registers with orca.
"""
var_name = from_geog_name + '_' + var_to_disaggregate

@orca.column(to_geog_name, var_name, cache=False)
def func():
print 'Disaggregating %s to %s from %s' % (var_to_disaggregate, to_geog_name, from_geog_name)
print('Disaggregating {} to {} from {}'
.format(var_to_disaggregate, to_geog_name, from_geog_name))

from_geog = orca.get_table(from_geog_name)
to_geog = orca.get_table(to_geog_name)
return misc.reindex(from_geog[var_to_disaggregate], to_geog[from_geog_id_name]).fillna(0)
return misc.reindex(from_geog[var_to_disaggregate],
to_geog[from_geog_id_name]).fillna(0)

return func


def make_size_var(agent, geog, geog_id):
"""
Generator function for size variables. Registers with orca.
"""
var_name = 'total_' + agent

@orca.column(geog, var_name, cache=False)
def func():
agents = orca.get_table(agent)
print 'Calculating number of %s for %s' % (agent, geog)
print('Calculating number of {} for {}'.format(agent, geog))

size = agents[geog_id].value_counts()

Expand All @@ -81,80 +94,102 @@ def func():

return func


def make_proportion_var(agent, geog, geog_id, target_variable, target_value):
"""
Generator function for proportion variables. Registers with orca.
"""
var_name = 'prop_%s_%s'%(target_variable, int(target_value))
var_name = 'prop_%s_%s' % (target_variable, int(target_value))

@orca.column(geog, var_name, cache=False)
def func():
agents = orca.get_table(agent).to_frame(columns=[target_variable, geog_id])
agents = orca.get_table(agent).to_frame(
columns=[target_variable, geog_id])
locations = orca.get_table(geog)
print 'Calculating proportion %s %s for %s' % (target_variable, target_value, geog)
print('Calculating proportion {} {} for {}'
.format(target_variable, target_value, geog))

agent_subset = agents[agents[target_variable] == target_value]
series = agent_subset.groupby(geog_id).size()*1.0/locations['total_' + agent]
series = (agent_subset.groupby(geog_id).size()
* 1.0
/ locations['total_' + agent])
series = series.fillna(0)
return series

return func


def make_dummy_variable(agent, geog_var, geog_id):
"""
Generator function for spatial dummy. Registers with orca.
"""
var_name = geog_var + '_is_' + str(int(geog_id))
var_name = '{}_is_{}'.format(geog_var, str(int(geog_id)))

@orca.column(agent, var_name, cache=True)
def func():
agents = orca.get_table(agent)
return (agents[geog_var] == geog_id).astype('int32')

return func


def make_ratio_var(agent1, agent2, geog):
"""
Generator function for ratio variables. Registers with orca.
"""
var_name = 'ratio_%s_to_%s'%(agent1, agent2)
var_name = 'ratio_%s_to_%s' % (agent1, agent2)

@orca.column(geog, var_name, cache=False)
def func():
locations = orca.get_table(geog)
print 'Calculating ratio of %s to %s for %s' % (agent1, agent2, geog)
print('Calculating ratio of {} to {} for {}'
.format(agent1, agent2, geog))

series = locations['total_' + agent1]*1.0/(locations['total_' + agent2] + 1.0)
series = (locations['total_' + agent1]
* 1.0
/ (locations['total_' + agent2] + 1.0))
series = series.fillna(0)
return series

return func



def make_access_var(name, agent, target_variable=False, target_value=False,
radius=1000, agg_function='sum', decay='flat', log=True,
filters=False):
"""
Generator function for accessibility variables. Registers with orca.
"""

@orca.column('nodes', name, cache=True, cache_scope='iteration')
def func(net):
print 'Calculating %s' % name
print('Calculating {}'.format(name))

nodes = pd.DataFrame(index=net.node_ids)
flds = [target_variable] if target_variable else []

if target_value:
flds += util.columns_in_filters(["%s == %s"%(target_variable,target_value)])
flds += util.columns_in_filters(
["{} == {}".format(target_variable, target_value)])

if filters:
flds += util.columns_in_filters(filters)
flds.append('node_id')

df = orca.get_table(agent).to_frame(flds)

if target_value:
df = util.apply_filter_query(df, ["%s == %s"%(target_variable,target_value)])
df = util.apply_filter_query(df, [
"{} == {}".format(target_variable, target_value)])
if filters:
df = util.apply_filter_query(df, filters)

net.set(df['node_id'], variable=df[target_variable] if target_variable else None)
net.set(df['node_id'],
variable=df[target_variable] if target_variable else None)
nodes[name] = net.aggregate(radius, type=agg_function, decay=decay)

if log:
nodes[name] = nodes[name].apply(eval('np.log1p'))
return nodes[name]

return func

0 comments on commit 51fe712

Please sign in to comment.