Exemple #1
0
    def test_read_write_boxes(self):
        scope = Scope(package_file('model', 'tests', 'road_test.yaml'))
        db = SQLiteDB()
        scope.store_scope(db)

        s1 = Box(name="Speedy", scope=scope)
        s1.set_upper_bound('build_travel_time', 70)

        s2 = Box(name="Notable", scope=scope, parent="Speedy")
        s2.set_lower_bound('expand_capacity', 20)

        u = Boxes(s1, s2, scope=scope)

        db.write_boxes(u)

        scope2 = Scope(package_file('model', 'tests', 'road_test.yaml'))
        u2 = db.read_boxes(scope=scope2)

        assert u == u2
        assert u["Notable"].parent_box_name == u2["Notable"].parent_box_name

        s1_ = db.read_box(scope.name, "Speedy")
        s2_ = db.read_box(scope.name, "Notable")

        assert s1 == s1_
        assert s2 == s2_
        assert s1.relevant_features == s1_.relevant_features
        assert s2.relevant_features == s2_.relevant_features
Exemple #2
0
 def test_correlated_latin_hypercube_conflict(self):
     scope_file = emat.package_file("model", "tests", "road_test_corr_conflict.yaml")
     scp = Scope(scope_file)
     with pytest.raises(emat.exceptions.AsymmetricCorrelationError):
         scp.design_experiments(
             n_samples_per_factor=10,
             random_seed=1234,
             sampler='lhs',
         )
Exemple #3
0
 def test_correlated_latin_hypercube_bad(self):
     scope_file = emat.package_file("model", "tests", "road_test_corr_bad.yaml")
     scp = Scope(scope_file)
     with pytest.raises(np.linalg.LinAlgError):
         scp.design_experiments(
             n_samples_per_factor=10,
             random_seed=1234,
             sampler='lhs',
         )
Exemple #4
0
    def test_dump_scope(self):
        scp = Scope(self.scope_file)
        dumped = scp.dump()
        # print("="*40)
        # print(dumped)
        # print("="*40)
        loaded = Scope(scope_def=dumped, scope_file="fake/filename.yaml")
        assert loaded == scp  # filename is intentionally different but let it go
        # but everything else is the same
        assert loaded.name == scp.name
        assert loaded.get_measures() == scp.get_measures()
        assert loaded.get_parameters() == scp.get_parameters()
        assert loaded.scope_file != scp.scope_file
        assert loaded.scope_file == "fake/filename.yaml"

        # fix name, still get equality
        loaded.scope_file = scp.scope_file
        assert loaded == scp
Exemple #5
0
    def test_nonuniform_latin_hypercube(self):
        scope_file = emat.package_file("model", "tests", "road_test_nonuniform.yaml")
        scp = Scope(scope_file)
        exp_def = scp.design_experiments(
            n_samples_per_factor=1000,
            random_seed=1234,
            sampler='lhs',
        )
        assert len(exp_def) == scp.n_sample_factors() * 1000
        assert (exp_def['free_flow_time'] == 60).all()
        assert (exp_def['initial_capacity'] == 100).all()
        assert np.corrcoef([exp_def.alpha, exp_def.beta])[0, 1] == approx(0.75, rel=0.05)
        assert np.corrcoef([exp_def.alpha, exp_def.expand_capacity])[0, 1] == approx(0.0, abs=0.02)
        assert np.corrcoef([exp_def.input_flow, exp_def.value_of_time])[0, 1] == approx(-0.5, rel=0.05)
        assert np.corrcoef([exp_def.unit_cost_expansion, exp_def.value_of_time])[0, 1] == approx(0.9, rel=0.05)

        assert exp_def.interest_rate_lock.sum() == approx(len(exp_def) * 0.2)

        assert np.percentile(exp_def.alpha,np.linspace(0,100,50)) == approx(
              [0.10037393, 0.10722119, 0.10994485, 0.11204394, 0.11383709,
               0.11544182, 0.11691345, 0.11829399, 0.11959909, 0.12084863,
               0.12205279, 0.12321800, 0.12435285, 0.12546474, 0.12655958,
               0.12763503, 0.12869873, 0.12975137, 0.13079620, 0.13183375,
               0.13287082, 0.13390854, 0.13494651, 0.13598528, 0.13703149,
               0.13808180, 0.13914411, 0.14021784, 0.14130323, 0.14240609,
               0.14352608, 0.14466756, 0.14583411, 0.14702908, 0.14825720,
               0.14951875, 0.15082844, 0.15218376, 0.15359963, 0.15508120,
               0.15664534, 0.15831425, 0.16010073, 0.16203921, 0.16418886,
               0.16662357, 0.16946999, 0.17301416, 0.17804383, 0.19662857])

        assert np.percentile(exp_def.beta,np.linspace(0,100,50)) == approx(
              [3.51654751, 3.72503059, 3.82437701, 3.90088124, 3.96222432,
               4.01360346, 4.06112277, 4.10640347, 4.14456476, 4.18084719,
               4.21812584, 4.24926944, 4.28049053, 4.31181127, 4.34390502,
               4.37561590, 4.40541815, 4.43276143, 4.45517485, 4.48062290,
               4.50726296, 4.53334164, 4.55737738, 4.57893875, 4.60371011,
               4.62590595, 4.64885523, 4.67335218, 4.69475909, 4.71546469,
               4.73676622, 4.75796550, 4.77690613, 4.79738177, 4.81947505,
               4.84481408, 4.86954326, 4.89379651, 4.91771359, 4.94100213,
               4.97169370, 5.00298714, 5.03525103, 5.07100437, 5.11028866,
               5.15061419, 5.19925284, 5.24775527, 5.32086547, 5.49345120])

        assert np.percentile(exp_def.input_flow,np.linspace(0,100,50)) == approx(
              [ 80.06332381,  83.71770678,  85.93721426,  87.82355332,
                89.52967083,  91.11148891,  92.60789787,  94.03509345,
                95.41186604,  96.74598771,  98.04479355,  99.31122788,
               100.55462827, 101.77808514, 102.98041937, 104.16517008,
               105.33959643, 106.50165383, 107.65366600, 108.79827639,
               109.93328795, 111.06707367, 112.19316408, 113.31493454,
               114.43982739, 115.56182157, 116.68454749, 117.80620837,
               118.93524815, 120.06752762, 121.20470208, 122.34781469,
               123.50083542, 124.66086016, 125.83390291, 127.02146142,
               128.22485477, 129.44681602, 130.68613841, 131.95601658,
               133.25403807, 134.58951122, 135.96442305, 137.39295642,
               138.89092217, 140.47204147, 142.17835057, 144.06540067,
               146.28064479, 149.94588322])
Exemple #6
0
    def test_box_universe(self):
        scope = Scope(package_file('model', 'tests', 'road_test.yaml'))

        s = Box(name="Speedy", scope=scope)
        s.set_upper_bound('build_travel_time', 70)

        s2 = Box(name="Notable", scope=scope, parent="Speedy")
        s2.set_lower_bound('expand_capacity', 20)

        u = Boxes(s, s2, scope=scope)
        assert u.fancy_names() == [
            'Scope: EMAT Road Test', '▶ Speedy', '▷ ▶ Notable'
        ]
        assert u.plain_names() == [None, 'Speedy', 'Notable']
Exemple #7
0
 def test_correlated_monte_carlo(self):
     scope_file = emat.package_file("model", "tests", "road_test_corr.yaml")
     scp = Scope(scope_file)
     exp_def = scp.design_experiments(
         n_samples_per_factor=100,
         random_seed=1234,
         sampler='mc',
     )
     assert len(exp_def) == scp.n_sample_factors() * 100
     assert (exp_def['free_flow_time'] == 60).all()
     assert (exp_def['initial_capacity'] == 100).all()
     assert np.corrcoef([exp_def.alpha, exp_def.beta])[0, 1] == approx(0.75, rel=0.05)
     assert np.corrcoef([exp_def.alpha, exp_def.expand_capacity])[0, 1] == approx(0.0, abs=0.05)
     assert np.corrcoef([exp_def.input_flow, exp_def.value_of_time])[0, 1] == approx(-0.5, rel=0.05)
     assert np.corrcoef([exp_def.unit_cost_expansion, exp_def.value_of_time])[0, 1] == approx(0.9, rel=0.05)
Exemple #8
0
    def test_box(self):
        scope = Scope(package_file('model', 'tests', 'road_test.yaml'))

        with pytest.raises(TypeError):
            s = Box(scope=scope)

        s = Box(name="Speedy", scope=scope)
        s.set_upper_bound('build_travel_time', 70)
        with pytest.raises(ScopeError):
            s.set_upper_bound('not_a_thing', 70)
        assert len(s) == 1
        assert 'build_travel_time' in s
        assert s.parent_box_name is None

        s2 = Box(name="Notable", scope=scope, parent="Speedy")
        s2.set_lower_bound('expand_capacity', 20)
        assert len(s2) == 1
        assert 'build_travel_time' not in s2
        assert s2.parent_box_name == 'Speedy'
Exemple #9
0
    def test_read_write_box(self):
        scope = Scope(package_file('model', 'tests', 'road_test.yaml'))
        db = SQLiteDB()
        scope.store_scope(db)

        s1 = Box(name="Speedy", scope=scope)
        s1.set_upper_bound('build_travel_time', 70)
        s1.relevant_features.add('debt_type')

        s2 = Box(name="Notable", scope=scope, parent="Speedy")
        s2.set_lower_bound('expand_capacity', 20)

        db.write_box(s1)
        db.write_box(s2)

        s1_ = db.read_box(scope.name, "Speedy")
        s2_ = db.read_box(scope.name, "Notable")

        assert s1 == s1_
        assert s2 == s2_
        assert s1.thresholds == s1_.thresholds
        assert s2.thresholds == s2_.thresholds
        assert s1.relevant_features == s1_.relevant_features
        assert s2.relevant_features == s2_.relevant_features
class TestCoreModelMethods(unittest.TestCase):
    ''' 
        tests model and meta-model methods     
    '''
    corem_scope_file = emat.package_file("model", "tests",
                                         "core_model_test.yaml")
    scp = Scope(corem_scope_file)

    corem = PythonCoreModel(
        function=Dummy(),
        configuration={
            'archive_path':
            config.get_subdir('test_directory', 'core_dummy_archive')
        },
        scope=scp,
    )

    # =============================================================================
    #
    #      Core model tests
    #
    # =============================================================================

    def test_create_scenario(self):
        exp_def = {'Auto IVTT Sensitivity': 0.9122442817924445}
        self.corem.setup(exp_def)

    @pytest.mark.skip(reason="TODO")
    def test_set_invalid_exp_variable(self):
        exp_def = {'unsupported': 1}
        with self.assertRaises(KeyError):
            self.corem.setup(exp_def)

    @pytest.mark.skip(reason="TODO")
    def test_post_process(self):
        exp_def = {'Land Use - CBD Focus': 1}
        pm = ['Region-wide VMT']
        self.corem.post_process(exp_def, pm)

    @pytest.mark.skip(reason="TODO")
    def test_archive_model(self):
        exp_id = 1
        archive_path = self.corem.get_exp_archive_path(self.scp.scp_name,
                                                       exp_id)
        self.corem.archive(archive_path)

    @pytest.mark.skip(reason="TODO")
    def atest_hook_presence(self):
        ''' confirm that hooks are present for all performance measures, exp vars'''
        # TODO
        # set experiment variables

        # post process

        # import performance measure

    @pytest.mark.skip(reason="TODO")
    def test_pm_import(self):
        pm = [
            'Peak Walk-to-transit Boarding', 'Total LRT Boardings',
            "PM Trip Time (minutes)", "Daily Transit Share",
            "Households within 30 min of CBD",
            "Number of Home-based work tours taking <= 45 minutes via transit",
            "Downtown to Airport Travel Time", 'OD Volume District 1 to 1',
            '190 Daily VHT'
        ]
        pm_vals = self.corem.import_perf_meas(pm)

        expected_pm = {
            'Peak Walk-to-transit Boarding': 56247.88692999999,
            'Total LRT Boardings': 24784.475588,
            "PM Trip Time (minutes)": 15.652833,
            "Daily Transit Share": 0.019905000000000003,
            "Households within 30 min of CBD": 379894,
            "Number of Home-based work tours taking <= 45 minutes via transit":
            322069.75,
            "Downtown to Airport Travel Time": 14.734342999999999,
            'OD Volume District 1 to 1': 55642.74609400001,
            '190 Daily VHT': 272612.499025
        }
        self.assertEqual(expected_pm, pm_vals)
Exemple #11
0
class TestMetaModelMethods(unittest.TestCase):
    ''' 
        tests model and meta-model methods     
    '''
    metam_scope_file = emat.package_file("model","tests","metam_test.yaml")
    metam_scp = Scope(metam_scope_file)

    
# =============================================================================
#     
#      Meta model tests
#     
# =============================================================================



    def test_derive_meta(self):
        from emat.examples import road_test

        s, db, m = road_test()

        db.get_db_info()

        m.design_experiments(n_samples=10, design_name='tiny')

        db.read_experiment_all(None, None)

        with pytest.raises(emat.PendingExperimentsError):
            m.create_metamodel_from_design('tiny', random_state=123)

        m.run_experiments(design_name='tiny')

        mm = emat.create_metamodel(
            m.scope,
            db.read_experiment_all(s.name, 'tiny'),
            random_state=123,
            metamodel_id=db.get_new_metamodel_id(None),
        )
        mm.db = db # add db after creation to prevent writing it into the db
        assert mm.scope == m.scope

        tiny2 = m.design_experiments(n_samples=10, design_name='tiny2', random_seed=456)

        assert tiny2.iloc[0]['debt_type'] == 'GO Bond'

        stable_df('./test_tiny2.pkl.gz',tiny2)

        result2 = mm.run_experiments('tiny2')

        tiny2out = mm.read_experiment_measures('tiny2')
        stable_df('./test_tiny2out.pkl.gz', tiny2out)

        with pytest.raises(ValueError):
            # no metamodels stored
            mm3 = db.read_metamodel(None, None)

        db.write_metamodel(mm)
        mm2 = db.read_metamodel(None, 1)
        mm3 = db.read_metamodel(None, None)
        assert mm2 == mm == mm3
        assert mm2 is not mm

        print(mm2.function(**(tiny2.iloc[0])))

        assert mm2.function(**(tiny2.iloc[0])) == approx({
            'no_build_travel_time': 83.57502327972276,
            'build_travel_time': 62.221693766038015,
            'time_savings': 57.612063365257995,
            'value_of_time_savings': 3749.2913256457214,
            'net_benefits': 395.55020765212254,
            'cost_of_capacity_expansion': 1252.6916865286616,
            'present_cost_expansion': 23000.275573551233,
        })

        mm3.metamodel_id = db.get_new_metamodel_id(None)
        db.write_metamodel(mm3)

        with pytest.raises(ValueError):
            # now too many to get without giving an ID
            mm4 = db.read_metamodel(None, None)

    def test_derive_meta_w_transform(self):
        from emat.examples import road_test

        s, db, m = road_test(yamlfile='road_test2.yaml')

        db.get_db_info()

        m.design_experiments(n_samples=10, design_name='tiny')

        db.read_experiment_all(None, None)

        with pytest.raises(emat.PendingExperimentsError):
            m.create_metamodel_from_design('tiny', random_state=123)

        m.run_experiments(design_name='tiny')

        mm = emat.create_metamodel(
            m.scope,
            db.read_experiment_all(s.name, 'tiny'),
            random_state=123,
            metamodel_id=db.get_new_metamodel_id(None),
        )

        assert mm.scope != m.scope  # now not equal as road_test2 has transforms that are stripped.
        mm.db = db
        tiny2 = m.design_experiments(n_samples=10, design_name='tiny2', random_seed=456)

        assert tiny2.iloc[0]['debt_type'] == 'GO Bond'

        assert dict(tiny2.iloc[0].drop('debt_type')) == approx({
            'alpha': 0.10428005571929212,
            'amortization_period': 33,
            'beta': 4.8792451185772014,
            'expand_capacity': 61.4210886403998,
            'input_flow': 137,
            'interest_rate': 0.03099304322197216,
            'interest_rate_lock': 0,
            'unit_cost_expansion': 121.85520427974882,
            'value_of_time': 0.002953613029133872,
            'yield_curve': 0.016255990123028242,
            'free_flow_time': 60,
            'initial_capacity': 100})

        result2 = mm.run_experiments('tiny2')

        # print(dict(mm.read_experiment_measures('tiny2').iloc[0]))
        #
        # print({
        #     'no_build_travel_time': 81.6839454971052,
        #     'build_travel_time': 61.91038371206646,
        #     'time_savings': 44.94189289289446,
        #     'value_of_time_savings': 2904.081661408463,
        #     'net_benefits': -34.09931528157315,
        #     'cost_of_capacity_expansion': 1085.3565091745982,
        #     'present_cost_expansion': 19923.66625500023,
        # })
        #
        assert dict(mm.read_experiment_measures('tiny2').iloc[0]) == approx({
            'no_build_travel_time': 81.6839454971052,
            'build_travel_time': 61.91038371206646,
            'log_build_travel_time': 4.120826572003798,
            'time_savings': 44.94189289289446,
            'value_of_time_savings': 2904.081661408463,
            'net_benefits': -34.09931528157315,
            'cost_of_capacity_expansion': 1085.3565091745982,
            'present_cost_expansion': 19923.66625500023,
        })

        assert m.run_experiment(tiny2.iloc[0]) == approx({
            'no_build_travel_time': 89.07004237532217,
            'build_travel_time': 62.81032484779827,
            'log_build_travel_time': np.log(62.81032484779827),
            'time_savings': 26.259717527523904,
            'value_of_time_savings': 10.62586300480175,
            'present_cost_expansion': 7484.479303360477,
            'cost_of_capacity_expansion': 395.69034710662226,
            'net_benefits': -385.0644841018205,
        })

        with pytest.raises(ValueError):
            # no metamodels stored
            mm3 = db.read_metamodel(None, None)

        db.write_metamodel(mm)
        mm2 = db.read_metamodel(None, 1)
        mm3 = db.read_metamodel(None, None)
        assert mm2 == mm == mm3
        assert mm2 is not mm

        assert mm2.function(**(tiny2.iloc[0])) == approx({
            'no_build_travel_time': 81.6839454971052,
            'build_travel_time': 61.91038371206646,
            'log_build_travel_time': 4.120826572003798,
            'time_savings': 44.94189289289446,
            'value_of_time_savings': 2904.081661408463,
            'net_benefits': -34.09931528157315,
            'cost_of_capacity_expansion': 1085.3565091745982,
            'present_cost_expansion': 19923.66625500023,
        })

        mm3.metamodel_id = db.get_new_metamodel_id(None)
        db.write_metamodel(mm3)

        with pytest.raises(ValueError):
            # now too many to get without giving an ID
            mm4 = db.read_metamodel(None, None)


    def test_exogenously_stratified_k_fold(self):
        from emat.learn.splits import ExogenouslyStratifiedKFold
        X = np.arange(20)
        Y = np.asarray([1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 0, 0, 1])
        S = np.asarray([1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0])
        correct = [np.array([0, 1, 2, 14, 15]),
                   np.array([3, 4, 5, 16]),
                   np.array([6, 7, 8, 17]),
                   np.array([9, 10, 11, 18]),
                   np.array([12, 13, 19])]
        for j, (_, k) in zip(correct, ExogenouslyStratifiedKFold(n_splits=5, exo_data=S).split(X, Y)):
            assert np.array_equal(j, k)
Exemple #12
0
class TestMetaModelMethods(unittest.TestCase):
    ''' 
        tests model and meta-model methods     
    '''
    metam_scope_file = emat.package_file("model", "tests", "metam_test.yaml")
    metam_scp = Scope(metam_scope_file)

    # =============================================================================
    #
    #      Meta model tests
    #
    # =============================================================================

    def test_derive_meta(self):
        from emat.examples import road_test

        s, db, m = road_test()

        db.get_db_info()

        m.design_experiments(n_samples=10, design_name='tiny')

        db.read_experiment_all(None, None)

        with pytest.raises(emat.PendingExperimentsError):
            m.create_metamodel_from_design('tiny', random_state=123)

        m.run_experiments(design_name='tiny')

        mm = m.create_metamodel_from_design('tiny', random_state=123)
        assert mm.scope == m.scope

        tiny2 = m.design_experiments(n_samples=10,
                                     design_name='tiny2',
                                     random_seed=456)

        assert tiny2.iloc[0]['debt_type'] == 'GO Bond'

        assert dict(tiny2.iloc[0].drop('debt_type')) == approx({
            'alpha':
            0.10428005571929212,
            'amortization_period':
            33,
            'beta':
            4.8792451185772014,
            'expand_capacity':
            61.4210886403998,
            'input_flow':
            137,
            'interest_rate':
            0.03099304322197216,
            'interest_rate_lock':
            0,
            'unit_cost_expansion':
            121.85520427974882,
            'value_of_time':
            0.002953613029133872,
            'yield_curve':
            0.016255990123028242,
            'free_flow_time':
            60,
            'initial_capacity':
            100
        })

        result2 = mm.run_experiments('tiny2')

        assert dict(mm.read_experiment_measures('tiny2').iloc[0]) == approx({
            'no_build_travel_time':
            81.6839454971052,
            'build_travel_time':
            61.91038371206646,
            'time_savings':
            44.94189289289446,
            'value_of_time_savings':
            2904.081661408463,
            'net_benefits':
            -34.09931528157315,
            'cost_of_capacity_expansion':
            1085.3565091745982,
            'present_cost_expansion':
            19923.66625500023
        })

        assert m.function(**(tiny2.iloc[0])) == approx({
            'no_build_travel_time':
            89.07004237532217,
            'build_travel_time':
            62.81032484779827,
            'time_savings':
            26.259717527523904,
            'value_of_time_savings':
            10.62586300480175,
            'present_cost_expansion':
            7484.479303360477,
            'cost_of_capacity_expansion':
            395.69034710662226,
            'net_benefits':
            -385.0644841018205
        })

        with pytest.raises(ValueError):
            # no metamodels stored
            mm3 = db.read_metamodel(None, None)

        db.write_metamodel(mm)
        mm2 = db.read_metamodel(None, 1)
        mm3 = db.read_metamodel(None, None)
        assert mm2 == mm == mm3
        assert mm2 is not mm

        assert mm2.function(**(tiny2.iloc[0])) == approx({
            'no_build_travel_time':
            81.6839454971052,
            'build_travel_time':
            61.91038371206646,
            'time_savings':
            44.94189289289446,
            'value_of_time_savings':
            2904.081661408463,
            'net_benefits':
            -34.09931528157315,
            'cost_of_capacity_expansion':
            1085.3565091745982,
            'present_cost_expansion':
            19923.66625500023
        })

        mm3.metamodel_id = db.get_new_metamodel_id(None)
        db.write_metamodel(mm3)

        with pytest.raises(ValueError):
            # now too many to get without giving an ID
            mm4 = db.read_metamodel(None, None)

    def test_derive_meta_w_transform(self):
        from emat.examples import road_test

        s, db, m = road_test(yamlfile='road_test2.yaml')

        db.get_db_info()

        m.design_experiments(n_samples=10, design_name='tiny')

        db.read_experiment_all(None, None)

        with pytest.raises(emat.PendingExperimentsError):
            m.create_metamodel_from_design('tiny', random_state=123)

        m.run_experiments(design_name='tiny')

        mm = m.create_metamodel_from_design('tiny', random_state=123)

        assert mm.scope != m.scope  # now not equal as road_test2 has transforms that are stripped.

        tiny2 = m.design_experiments(n_samples=10,
                                     design_name='tiny2',
                                     random_seed=456)

        assert tiny2.iloc[0]['debt_type'] == 'GO Bond'

        assert dict(tiny2.iloc[0].drop('debt_type')) == approx({
            'alpha':
            0.10428005571929212,
            'amortization_period':
            33,
            'beta':
            4.8792451185772014,
            'expand_capacity':
            61.4210886403998,
            'input_flow':
            137,
            'interest_rate':
            0.03099304322197216,
            'interest_rate_lock':
            0,
            'unit_cost_expansion':
            121.85520427974882,
            'value_of_time':
            0.002953613029133872,
            'yield_curve':
            0.016255990123028242,
            'free_flow_time':
            60,
            'initial_capacity':
            100
        })

        result2 = mm.run_experiments('tiny2')

        # print(dict(mm.read_experiment_measures('tiny2').iloc[0]))
        #
        # print({
        #     'no_build_travel_time': 81.6839454971052,
        #     'build_travel_time': 61.91038371206646,
        #     'time_savings': 44.94189289289446,
        #     'value_of_time_savings': 2904.081661408463,
        #     'net_benefits': -34.09931528157315,
        #     'cost_of_capacity_expansion': 1085.3565091745982,
        #     'present_cost_expansion': 19923.66625500023,
        # })
        #
        assert dict(mm.read_experiment_measures('tiny2').iloc[0]) == approx({
            'no_build_travel_time':
            81.6839454971052,
            'build_travel_time':
            61.91038371206646,
            'log_build_travel_time':
            4.120826572003798,
            'time_savings':
            44.94189289289446,
            'value_of_time_savings':
            2904.081661408463,
            'net_benefits':
            -34.09931528157315,
            'cost_of_capacity_expansion':
            1085.3565091745982,
            'present_cost_expansion':
            19923.66625500023,
        })

        assert m.run_experiment(tiny2.iloc[0]) == approx({
            'no_build_travel_time':
            89.07004237532217,
            'build_travel_time':
            62.81032484779827,
            'log_build_travel_time':
            np.log(62.81032484779827),
            'time_savings':
            26.259717527523904,
            'value_of_time_savings':
            10.62586300480175,
            'present_cost_expansion':
            7484.479303360477,
            'cost_of_capacity_expansion':
            395.69034710662226,
            'net_benefits':
            -385.0644841018205,
        })

        with pytest.raises(ValueError):
            # no metamodels stored
            mm3 = db.read_metamodel(None, None)

        db.write_metamodel(mm)
        mm2 = db.read_metamodel(None, 1)
        mm3 = db.read_metamodel(None, None)
        assert mm2 == mm == mm3
        assert mm2 is not mm

        assert mm2.function(**(tiny2.iloc[0])) == approx({
            'no_build_travel_time':
            81.6839454971052,
            'build_travel_time':
            61.91038371206646,
            'log_build_travel_time':
            4.120826572003798,
            'time_savings':
            44.94189289289446,
            'value_of_time_savings':
            2904.081661408463,
            'net_benefits':
            -34.09931528157315,
            'cost_of_capacity_expansion':
            1085.3565091745982,
            'present_cost_expansion':
            19923.66625500023,
        })

        mm3.metamodel_id = db.get_new_metamodel_id(None)
        db.write_metamodel(mm3)

        with pytest.raises(ValueError):
            # now too many to get without giving an ID
            mm4 = db.read_metamodel(None, None)
class TestExperimentMethods(unittest.TestCase):
    ''' 
        tests generating experiments      
    '''
    #
    # one time test setup
    #
    scope_file = emat.package_file("scope", "tests", "scope_test.yaml")
    scp = Scope(scope_file)

    db_test = SQLiteDB(":memory:", initialize=True)
    scp.store_scope(db_test)

    def test_latin_hypercube(self):
        exp_def = self.scp.design_experiments(
            n_samples_per_factor=10,
            random_seed=1234,
            sampler='lhs',
            db=self.db_test,
        )
        assert len(exp_def) == self.scp.n_sample_factors() * 10
        assert (exp_def['TestRiskVar'] == 1.0).all()
        assert (exp_def['Land Use - CBD Focus']).mean() == approx(1.0326,
                                                                  abs=1e-3)
        assert (exp_def['Freeway Capacity']).mean() == approx(1.5, abs=1e-3)

        exp_def2 = self.db_test.read_experiment_parameters(
            self.scp.name, 'lhs')
        assert (exp_def[exp_def2.columns] == exp_def2).all().all()

    def test_latin_hypercube_not_joint(self):
        exp_def = self.scp.design_experiments(
            n_samples_per_factor=5,
            random_seed=1234,
            sampler='lhs',
            db=self.db_test,
            jointly=False,
            design_name='lhs_not_joint',
        )
        # assert len(exp_def) == len(self.scp.get_uncertainties())*5 * len(self.scp.get_levers())*5
        assert len(
            exp_def
        ) == 80  # there are only 4 unique policies, times 2 scenarios
        assert (exp_def['TestRiskVar'] == 1.0).all()
        assert (exp_def['Land Use - CBD Focus']).mean() == approx(1.033,
                                                                  abs=1e-2)
        assert (exp_def['Freeway Capacity']).mean() == approx(1.5, abs=1e-2)

        exp_def2 = self.db_test.read_experiment_parameters(
            self.scp.name, 'lhs_not_joint')
        assert (exp_def[exp_def2.columns] == exp_def2).all().all()

    def test_monte_carlo(self):
        exp_def = self.scp.design_experiments(
            n_samples_per_factor=10,
            random_seed=1234,
            sampler='mc',
            db=self.db_test,
        )
        assert len(exp_def) == self.scp.n_sample_factors() * 10
        assert (exp_def['TestRiskVar'] == 1.0).all()
        assert (exp_def['Land Use - CBD Focus']).mean() == approx(1.0326,
                                                                  abs=0.01)
        assert (exp_def['Freeway Capacity']).mean() == approx(1.5, abs=0.01)

        exp_def2 = self.db_test.read_experiment_parameters(self.scp.name, 'mc')
        assert (exp_def[exp_def2.columns] == exp_def2).all().all()

    def test_sensitivity_tests(self):
        exp_def = self.scp.design_experiments(
            sampler='uni',
            db=self.db_test,
        )
        cols = [
            'TestRiskVar', 'Land Use - CBD Focus', 'Freeway Capacity',
            'Auto IVTT Sensitivity', 'Shared Mobility',
            'Kensington Decommissioning', 'LRT Extension'
        ]
        correct = '{"TestRiskVar":{"0":1.0,"1":1.0,"2":1.0,"3":1.0,"4":1.0,"5":1.0,"6":1.0,"7":1.0},' \
                  '"Land Use - CBD Focus":{"0":1.0,"1":0.82,"2":1.37,"3":1.0,"4":1.0,"5":1.0,"6":1.0,"7":1.0},' \
                  '"Freeway Capacity":{"0":1.0,"1":1.0,"2":1.0,"3":2.0,"4":1.0,"5":1.0,"6":1.0,"7":1.0},' \
                  '"Auto IVTT Sensitivity":{"0":1.0,"1":1.0,"2":1.0,"3":1.0,"4":0.75,"5":1.0,"6":1.0,"7":1.0},' \
                  '"Shared Mobility":{"0":0.0,"1":0.0,"2":0.0,"3":0.0,"4":0.0,"5":1.0,"6":0.0,"7":0.0},' \
                  '"Kensington Decommissioning":{"0":false,"1":false,"2":false,"3":false,"4":false,' \
                  '"5":false,"6":true,"7":false},"LRT Extension":{"0":false,"1":false,"2":false,"3":false,' \
                  '"4":false,"5":false,"6":false,"7":true}}'
        correct = pd.read_json(correct)
        for k in cols:
            assert (exp_def[k].values == approx(correct[k].values))

        exp_def2 = self.db_test.read_experiment_parameters(
            self.scp.name, 'uni')
        for k in cols:
            assert (exp_def2[k].values == approx(correct[k].values))

    def test_nonuniform_latin_hypercube(self):
        scope_file = emat.package_file("model", "tests",
                                       "road_test_nonuniform.yaml")
        scp = Scope(scope_file)
        exp_def = scp.design_experiments(
            n_samples_per_factor=1000,
            random_seed=1234,
            sampler='lhs',
        )
        assert len(exp_def) == scp.n_sample_factors() * 1000
        assert (exp_def['free_flow_time'] == 60).all()
        assert (exp_def['initial_capacity'] == 100).all()
        assert np.corrcoef([exp_def.alpha,
                            exp_def.beta])[0, 1] == approx(0.75, rel=0.05)
        assert np.corrcoef([exp_def.alpha,
                            exp_def.expand_capacity])[0, 1] == approx(0.0,
                                                                      abs=0.02)
        assert np.corrcoef([exp_def.input_flow,
                            exp_def.value_of_time])[0, 1] == approx(-0.5,
                                                                    rel=0.05)
        assert np.corrcoef(
            [exp_def.unit_cost_expansion,
             exp_def.value_of_time])[0, 1] == approx(0.9, rel=0.05)

        assert exp_def.interest_rate_lock.sum() == approx(len(exp_def) * 0.2)

        assert np.percentile(exp_def.alpha, np.linspace(0, 100, 50)) == approx(
            [
                0.10037393, 0.10722119, 0.10994485, 0.11204394, 0.11383709,
                0.11544182, 0.11691345, 0.11829399, 0.11959909, 0.12084863,
                0.12205279, 0.12321800, 0.12435285, 0.12546474, 0.12655958,
                0.12763503, 0.12869873, 0.12975137, 0.13079620, 0.13183375,
                0.13287082, 0.13390854, 0.13494651, 0.13598528, 0.13703149,
                0.13808180, 0.13914411, 0.14021784, 0.14130323, 0.14240609,
                0.14352608, 0.14466756, 0.14583411, 0.14702908, 0.14825720,
                0.14951875, 0.15082844, 0.15218376, 0.15359963, 0.15508120,
                0.15664534, 0.15831425, 0.16010073, 0.16203921, 0.16418886,
                0.16662357, 0.16946999, 0.17301416, 0.17804383, 0.19662857
            ])

        assert np.percentile(exp_def.beta, np.linspace(0, 100, 50)) == approx([
            3.51654751, 3.72503059, 3.82437701, 3.90088124, 3.96222432,
            4.01360346, 4.06112277, 4.10640347, 4.14456476, 4.18084719,
            4.21812584, 4.24926944, 4.28049053, 4.31181127, 4.34390502,
            4.37561590, 4.40541815, 4.43276143, 4.45517485, 4.48062290,
            4.50726296, 4.53334164, 4.55737738, 4.57893875, 4.60371011,
            4.62590595, 4.64885523, 4.67335218, 4.69475909, 4.71546469,
            4.73676622, 4.75796550, 4.77690613, 4.79738177, 4.81947505,
            4.84481408, 4.86954326, 4.89379651, 4.91771359, 4.94100213,
            4.97169370, 5.00298714, 5.03525103, 5.07100437, 5.11028866,
            5.15061419, 5.19925284, 5.24775527, 5.32086547, 5.49345120
        ])

        assert np.percentile(exp_def.input_flow, np.linspace(
            0, 100, 50)) == approx([
                80.06332381, 83.71770678, 85.93721426, 87.82355332,
                89.52967083, 91.11148891, 92.60789787, 94.03509345,
                95.41186604, 96.74598771, 98.04479355, 99.31122788,
                100.55462827, 101.77808514, 102.98041937, 104.16517008,
                105.33959643, 106.50165383, 107.65366600, 108.79827639,
                109.93328795, 111.06707367, 112.19316408, 113.31493454,
                114.43982739, 115.56182157, 116.68454749, 117.80620837,
                118.93524815, 120.06752762, 121.20470208, 122.34781469,
                123.50083542, 124.66086016, 125.83390291, 127.02146142,
                128.22485477, 129.44681602, 130.68613841, 131.95601658,
                133.25403807, 134.58951122, 135.96442305, 137.39295642,
                138.89092217, 140.47204147, 142.17835057, 144.06540067,
                146.28064479, 149.94588322
            ])
Exemple #14
0
 def test_null_scope(self):
     scp = Scope(None)
     assert repr(scp) == "<emat.Scope with no content>"
     assert len(scp.get_measures()) == 0
     assert len(scp.get_parameters()) == 0
Exemple #15
0
 def test_save_scope(self):
     scp = Scope(self.scope_file)
     scp.store_scope(self.db_test)