diff --git a/.gitignore b/.gitignore index a0ad585..520f23c 100644 --- a/.gitignore +++ b/.gitignore @@ -86,4 +86,7 @@ ENV/ .spyderproject # Rope project settings -.ropeproject \ No newline at end of file +.ropeproject + +# PyCharm +.idea \ No newline at end of file diff --git a/.travis.yml b/.travis.yml new file mode 100644 index 0000000..52e4207 --- /dev/null +++ b/.travis.yml @@ -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 \ No newline at end of file diff --git a/variable_generators/__init__.py b/variable_generators/__init__.py index a5876c1..538e75f 100644 --- a/variable_generators/__init__.py +++ b/variable_generators/__init__.py @@ -1 +1 @@ -version = __version__ = '0.1dev' \ No newline at end of file +version = __version__ = '0.1dev' diff --git a/variable_generators/generators.py b/variable_generators/generators.py index fd4763d..1e526f5 100644 --- a/variable_generators/generators.py +++ b/variable_generators/generators.py @@ -1,3 +1,5 @@ +from __future__ import print_function + import numpy as np import pandas as pd @@ -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': @@ -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: @@ -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() @@ -81,29 +94,37 @@ 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) @@ -111,35 +132,46 @@ def func(): 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') @@ -147,14 +179,17 @@ def func(net): 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