From 7090031d55f7e8fecb9b6775d399fd4354a4f9aa Mon Sep 17 00:00:00 2001 From: Matt Davis Date: Fri, 6 Mar 2015 22:55:23 -0800 Subject: [PATCH] interim commit with all_people test --- activitysim/cdap/cdap.py | 26 ++++++++++ .../cdap/tests/data/cdap_all_people.csv | 12 ++--- activitysim/cdap/tests/test_cdap.py | 52 +++++++++++++++++++ 3 files changed, 84 insertions(+), 6 deletions(-) diff --git a/activitysim/cdap/cdap.py b/activitysim/cdap/cdap.py index 2d12492e70..4af73a5b8e 100644 --- a/activitysim/cdap/cdap.py +++ b/activitysim/cdap/cdap.py @@ -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] diff --git a/activitysim/cdap/tests/data/cdap_all_people.csv b/activitysim/cdap/tests/data/cdap_all_people.csv index 661d2f32d7..7d838aea5e 100644 --- a/activitysim/cdap/tests/data/cdap_all_people.csv +++ b/activitysim/cdap/tests/data/cdap_all_people.csv @@ -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 diff --git a/activitysim/cdap/tests/test_cdap.py b/activitysim/cdap/tests/test_cdap.py index 7f8c0bfe44..65ccf569f7 100644 --- a/activitysim/cdap/tests/test_cdap.py +++ b/activitysim/cdap/tests/test_cdap.py @@ -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' @@ -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)