Skip to content

Commit

Permalink
Merge 0ca7135 into 30fa467
Browse files Browse the repository at this point in the history
  • Loading branch information
smmaurer committed Jan 21, 2019
2 parents 30fa467 + 0ca7135 commit bd86f9f
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 4 deletions.
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

setup(
name='urbansim_templates',
version='0.1',
version='0.1.1.dev0',
description='UrbanSim extension for managing model steps',
author='UrbanSim Inc.',
author_email='info@urbansim.com',
Expand Down
24 changes: 24 additions & 0 deletions tests/test_large_multinomial_logit.py
Original file line number Diff line number Diff line change
Expand Up @@ -259,3 +259,27 @@ def test_diagnostic_attributes(data):
modelmanager.remove_step(name)


def test_simulation_join_key_as_filter(m):
"""
This tests that it's possible to use a join key as a both a data filter for one of
the tables, and as a choice column for the model.
This came up because MergedChoiceTable doesn't allow the observations and
alternatives to have any column names in common -- the rationale is to maintain data
traceability by avoiding any of-the-fly renaming or dropped columns.
In the templates, in order to support things like using 'households.building_id' as a
filter column and 'buildings.building_id' as a choice column, we apply the filters
and then drop columns that are no longer needed before merging the tables.
"""
obs = orca.get_table('obs')
obs['aid'] = obs.get_column('choice')

m.out_choosers = 'obs'
m.out_chooser_filters = 'aid > 50'
m.out_alternatives = 'alts'
m.out_column = 'aid'

m.run()

2 changes: 1 addition & 1 deletion urbansim_templates/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
version = __version__ = '0.1'
version = __version__ = '0.1.1.dev0'
14 changes: 12 additions & 2 deletions urbansim_templates/models/large_multinomial_logit.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
from __future__ import print_function

import orca
from urbansim.models.util import columns_in_formula

from .. import modelmanager
from ..utils import get_data, update_column, version_greater_or_equal
from ..utils import get_data, update_column, to_list, version_greater_or_equal
from .shared import TemplateStep


Expand Down Expand Up @@ -371,7 +372,6 @@ def out_alternatives(self, value):
self.__out_alternatives = self._normalize_table_param(value)
self.send_to_listeners('out_alternatives', value)


@property
def out_column(self):
return self.__out_column
Expand Down Expand Up @@ -561,7 +561,17 @@ def run(self, chooser_batch_size=None, interaction_terms=None):
if len(alternatives) == 0:
print("No valid alternatives")
return

# Remove filter columns before merging, in case column names overlap
expr_cols = columns_in_formula(self.model_expression)

obs_cols = set(observations.columns) & set(expr_cols + to_list(obs_extra_cols))
observations = observations[list(obs_cols)]

alt_cols = set(alternatives.columns) & set(expr_cols + to_list(alts_extra_cols))
alternatives = alternatives[list(alt_cols)]

# Callables for iterative choices
def mct(obs, alts):
return MergedChoiceTable(
obs, alts, sample_size=self.alt_sample_size,
Expand Down

0 comments on commit bd86f9f

Please sign in to comment.