Skip to content

Commit

Permalink
interim commit with all_people test
Browse files Browse the repository at this point in the history
  • Loading branch information
jiffyclub committed Mar 7, 2015
1 parent 3854297 commit 7090031
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 6 deletions.
26 changes: 26 additions & 0 deletions activitysim/cdap/cdap.py
Original file line number Diff line number Diff line change
Expand Up @@ -220,3 +220,29 @@ def apply_final_rules(hh_util, people, hh_id_col, final_rules):
for combo in utils.index]

utils[app] = row.iloc[1]


def apply_all_people(hh_util, all_people):
"""
Apply utility adjustments to household alternatives.
This modifies the `hh_util` data inplace.
Parameters
----------
hh_util : dict of pandas.Series
Keys will be household IDs and values will be Series
mapping alternative choices to their utility.
all_people : pandas.DataFrame
Adjustments to household alternatives, with alternatives in the
index and the adjustment values in the first column.
Index should be household alternatives in the form of tuples
containing individual alternatives, e.g.
('Mandatory', 'Mandatory', 'Mandatory'), where 'Mandatory' is
one of the alternatives available to individual household members.
Note that these may also be expressed as Python code to save space,
so the previous could also be written as ('Mandatory',) * 3.
"""
# evaluate all the expressions in the all_people index
all_people.index = [eval(x) for x in all_people.index]
12 changes: 6 additions & 6 deletions activitysim/cdap/tests/data/cdap_all_people.csv
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Description,Alternative,Value
Three Person All M,MMM,0
Three Person All N,NNN,0
Three Person All H,HHH,0
Four Person All M,MMMM,0
Four Person All N,NNNN,0
Four Person All H,HHHH,0
Three Person All M,"('Mandatory',) * 3",0
Three Person All N,"('NonMandatory',) * 3",0
Three Person All H,"('Home',) * 3",0
Four Person All M,"('Mandatory',) * 4",0
Four Person All N,"('NonMandatory',) * 4",0
Four Person All H,"('Home',) * 4",0
52 changes: 52 additions & 0 deletions activitysim/cdap/tests/test_cdap.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,14 @@ def final_rules():
os.path.dirname(__file__), 'data', 'cdap_final_rules.csv'))


@pytest.fixture
def all_people():
return read_model_spec(
os.path.join(
os.path.dirname(__file__), 'data', 'cdap_all_people.csv'),
expression_name='Alternative')


@pytest.fixture(scope='module')
def hh_id_col():
return 'household'
Expand Down Expand Up @@ -246,3 +254,47 @@ def test_apply_final_rules(hh_utils, final_rules, people, hh_id_col):

for k in expected:
pdt.assert_series_equal(hh_utils[k], expected[k], check_dtype=False)


def test_apply_all_people(hh_utils, all_people):
all_people.at["('Mandatory',) * 3", 'Value'] = 300
all_people.at["('Home',) * 4", 'Value'] = 500

expected = hh_utils.copy()
expected[5] = pd.Series([
301, 1, 2, 3, 3, 4, 1, 1, 2,
0, 0, 1, 2, 2, 3, 0, 0, 1,
0, 0, 1, 2, 2, 3, 0, 0, 1,
], index=hh_utils[5].index)
expected[6] = pd.Series([
302, 2, 4, 2, 2, 4, 5, 5, 7,
0, 0, 2, 0, 0, 2, 3, 3, 5,
0, 0, 2, 0, 0, 2, 3, 3, 5,
], index=hh_utils[6].index)
expected[7] = pd.Series([
4, 8, 4, 8, 12, 8, 4, 8, 4,
3, 7, 3, 7, 11, 7, 3, 7, 3,
3, 7, 3, 7, 11, 7, 3, 7, 3,
1, 5, 1, 5, 9, 5, 1, 5, 1,
0, 4, 0, 4, 8, 4, 0, 4, 0,
0, 4, 0, 4, 8, 4, 0, 4, 0,
1, 5, 1, 5, 9, 5, 1, 5, 1,
0, 4, 0, 4, 8, 4, 0, 4, 0,
0, 4, 0, 4, 8, 4, 0, 4, 500
], index=hh_utils[7].index)
expected[8] = pd.Series([
52, 50, 50, 2, 0, 0, 6, 4, 4,
52, 50, 50, 2, 0, 0, 6, 4, 4,
57, 55, 55, 7, 5, 5, 11, 9, 9,
52, 50, 50, 2, 0, 0, 6, 4, 4,
52, 50, 50, 2, 0, 0, 6, 4, 4,
57, 55, 55, 7, 5, 5, 11, 9, 9,
56, 54, 54, 6, 4, 4, 10, 8, 8,
56, 54, 54, 6, 4, 4, 10, 8, 8,
61, 59, 59, 11, 9, 9, 15, 13, 513
], index=hh_utils[8].index)

cdap.apply_all_people(hh_utils, all_people)

for k in expected:
pdt.assert_series_equal(hh_utils[k], expected[k], check_dtype=False)

0 comments on commit 7090031

Please sign in to comment.