def test_property_persistence(orca_session):
    """
    Test persistence of properties across registration, saving, and reloading.
    
    """
    t = LoadTable()
    t.table = 'buildings'
    t.source_type = 'csv'
    t.path = 'data/buildings.csv'
    t.csv_index_cols = 'building_id'
    t.extra_settings = {
        'make_data_awesome': True
    }  # unfortunately not a valid setting
    t.cache = False
    t.cache_scope = 'iteration'
    t.copy_col = False
    t.name = 'buildings-csv'
    t.tags = ['awesome', 'data']
    t.autorun = False

    d1 = t.to_dict()
    modelmanager.register(t)
    modelmanager.initialize()
    d2 = modelmanager.get_step(t.name).to_dict()

    assert d1 == d2
    modelmanager.remove_step(t.name)
def test_observation_sampling():
    mm.initialize()

    m = LargeMultinomialLogitStep()
    m.choosers = 'obs'
    m.alternatives = 'alts'
    m.choice_column = 'choice'
    m.model_expression = 'obsval + altval'

    m.fit()
    assert (len(m.mergedchoicetable.to_frame()) == 190
            )  # 200 after fixing alt sampling

    m.chooser_sample_size = 5
    m.fit()
    assert (len(m.mergedchoicetable.to_frame()) == 95
            )  # 100 after fixing alt sampling

    m.name = 'mnl-test'
    m.register()

    mm.initialize()
    m = mm.get_step('mnl-test')
    assert (m.chooser_sample_size == 5)

    mm.remove_step('mnl-test')
Beispiel #3
0
def test_property_persistence(m):
    """
    Test persistence of properties across registration, saving, and reloading.
    
    """
    m.fit()
    m.name = 'my-model'
    m.tags = ['tag1']
    m.chooser_filters = 'filters1'
    m.chooser_sample_size = 100
    m.alt_filters = 'filter2'
    m.out_choosers = 'choosers2'
    m.out_alternatives = 'alts2'
    m.out_column = 'choices'
    m.out_chooser_filters = 'filters3'
    m.out_alt_filters = 'filters4'
    m.constrained_choices = True
    m.alt_capacity = 'cap'
    m.chooser_size = 'size'
    m.max_iter = 17
    
    d1 = m.to_dict()
    modelmanager.initialize()
    modelmanager.register(m)
    modelmanager.initialize()
    d2 = modelmanager.get_step('my-model').to_dict()
    
    assert d1 == d2
    modelmanager.remove_step('my-model')
def orca_session():
    """
    Set up a clean Orca session and initialize ModelManager.
    
    """
    orca.clear_all()
    modelmanager.initialize()
def test_property_persistence(m):
    """
    Test persistence of properties across registration, saving, and reloading.
    
    """
    m.name = 'test'
    m.tags = ['one', 'two']
    m.fit_all()
    d1 = m.to_dict()
    modelmanager.initialize()
    modelmanager.register(m)
    modelmanager.initialize()
    d2 = modelmanager.get_step('test').to_dict()
    assert d1 == d2
    modelmanager.remove_step('test')
def test_simulation(orca_session):
    """
    Test that predicted values are correctly written to Orca.
    
    """
    modelmanager.initialize()
    
    m = OLSRegressionStep()
    m.tables = 'obs'
    m.model_expression = 'a ~ b'
    m.fit()
    
    m.out_column = 'a_predicted'
    m.run()
    
    assert orca.get_table('obs').to_frame()['a_predicted'].equals(m.predicted_values)
Beispiel #7
0
def orca_session():
    """
    Set up a clean Orca and ModelManager session, with a data table.
    
    """
    orca.clear_all()
    modelmanager.initialize()

    d1 = {
        'id': np.arange(5),
        'a': np.random.random(5),
        'b': np.random.choice(np.arange(20), size=5)
    }

    df = pd.DataFrame(d1).set_index('id')
    orca.add_table('obs', df)
Beispiel #8
0
def test_diagnostic_attributes(data):
    """
    Test that diagnostic attributes are available when expected.
    
    """
    m = LargeMultinomialLogitStep()
    m.choosers = 'obs'
    m.alternatives = 'alts'
    m.choice_column = 'choice'
    m.model_expression = 'obsval + altval'
    m.alt_sample_size = 10
    
    assert(m.model is None)
    assert(m.mergedchoicetable is None)
    assert(m.probabilities is None)
    assert(m.choices is None)
    
    m.fit()
    
    assert(isinstance(m.model, MultinomialLogitResults))
    
    len_mct = len(m.mergedchoicetable.to_frame())
    len_obs_alts = len(orca.get_table(m.choosers).to_frame()) * m.alt_sample_size
    
    assert(len_mct == len_obs_alts)
    
    name = m.name
    modelmanager.register(m)
    modelmanager.initialize()
    m = modelmanager.get_step(name)
    
    assert(isinstance(m.model, MultinomialLogitResults))

    m.run()

    len_mct = len(m.mergedchoicetable.to_frame())
    len_probs = len(m.probabilities)
    len_choices = len(m.choices)
    len_obs = len(orca.get_table(m.choosers).to_frame())
    len_obs_alts = len_obs * m.alt_sample_size
    
    assert(len_mct == len_obs_alts)
    assert(len_probs == len_obs_alts)
    assert(len_choices == len_obs)

    modelmanager.remove_step(name)
def test_out_transform(orca_session):
    """
    Test transformation of the predicted values.
    
    """
    modelmanager.initialize()
    
    m = OLSRegressionStep()
    m.tables = 'obs'
    m.model_expression = 'a ~ b'
    m.fit()
    
    m.out_column = 'a_predicted'
    m.out_transform = 'np.exp'
    m.run()
    
    predictions = m.predicted_values.apply(np.exp)
    
    assert orca.get_table('obs').to_frame()['a_predicted'].equals(predictions)
def test_hdf(orca_session, data):
    """
    Test loading data from an HDF file.
    
    """
    t = LoadTable()
    t.table = 'buildings'
    t.source_type = 'hdf'
    t.path = 'data/buildings.hdf'

    assert 'buildings' not in orca.list_tables()

    modelmanager.register(t)
    assert 'buildings' in orca.list_tables()
    _ = orca.get_table('buildings').to_frame()

    modelmanager.initialize()
    assert 'buildings' in orca.list_tables()

    modelmanager.remove_step(t.name)
def test_ols(orca_session):
    """
    For now this just tests that the code runs.
    
    """
    modelmanager.initialize()

    m = OLSRegressionStep()
    m.tables = 'obs'
    m.model_expression = 'a ~ b'

    m.fit()

    m.name = 'ols-test'
    modelmanager.register(m)

    modelmanager.initialize()
    m = modelmanager.get_step('ols-test')

    modelmanager.remove_step('ols-test')
Beispiel #12
0
def test_binary_logit(orca_session):
    """
    For now this just tests that the code runs.
    
    """
    modelmanager.initialize()

    m = BinaryLogitStep()
    m.tables = 'obs'
    m.model_expression = 'b ~ a'
    
    m.fit()
    
    m.name = 'binary-test'
    modelmanager.register(m)
    
    modelmanager.initialize()
    m = modelmanager.get_step('binary-test')
    
    modelmanager.remove_step('binary-test')
def test_csv(orca_session, data):
    """
    Test loading data from a CSV file.
    
    """
    t = LoadTable()
    t.table = 'buildings'
    t.source_type = 'csv'
    t.path = 'data/buildings.csv'
    t.csv_index_cols = 'building_id'

    assert 'buildings' not in orca.list_tables()

    modelmanager.register(t)
    assert 'buildings' in orca.list_tables()
    _ = orca.get_table('buildings').to_frame()

    modelmanager.initialize()
    assert 'buildings' in orca.list_tables()

    modelmanager.remove_step(t.name)
Beispiel #14
0
def test_property_persistence(orca_session):
    """
    Test persistence of properties across registration, saving, and reloading.
    
    """
    t = SaveTable()
    t.table = 'buildings'
    t.columns = ['window_panes', 'number_of_chimneys']
    t.filters = 'number_of_chimneys > 15'
    t.output_type = 'csv'
    t.path = 'data/buildings.csv'
    t.extra_settings = {'make_data_awesome': True}
    t.name = 'save-buildings-csv'
    t.tags = ['awesome', 'chimneys']

    d1 = t.to_dict()
    modelmanager.register(t)
    modelmanager.initialize()
    d2 = modelmanager.get_step(t.name).to_dict()

    assert d1 == d2
    modelmanager.remove_step(t.name)
def test_extra_settings(orca_session, data):
    """
    Test loading data with extra settings, e.g. for compressed files.
    
    """
    t = LoadTable()
    t.table = 'buildings'
    t.source_type = 'csv'
    t.path = 'data/buildings.csv.gz'
    t.csv_index_cols = 'building_id'
    t.extra_settings = {'compression': 'gzip'}

    assert 'buildings' not in orca.list_tables()

    modelmanager.register(t)
    assert 'buildings' in orca.list_tables()
    _ = orca.get_table('buildings').to_frame()

    modelmanager.initialize()
    assert 'buildings' in orca.list_tables()

    modelmanager.remove_step(t.name)
def test_small_mnl(orca_session):
    """
    Test that the code runs, and that the model_expression is always available.
    
    """
    modelmanager.initialize()

    m = SmallMultinomialLogitStep()
    m.tables = ['households', 'buildings']
    m.choice_column = 'choice'
    m.model_expression = OrderedDict([('intercept', [1, 2]), ('a', [0, 2]),
                                      ('b', [0, 2])])

    m.fit()
    assert (m.model_expression is not None)

    print(m.model_expression)

    m.name = 'small-mnl-test'
    modelmanager.register(m)
    assert (m.model_expression is not None)

    print(m.model_expression)

    # TEST SIMULATION
    m.out_column = 'simulated_choice'

    m.run()
    print(orca.get_table('households').to_frame())

    modelmanager.initialize()
    m = modelmanager.get_step('small-mnl-test')
    assert (m.model_expression is not None)

    print(m.model_expression)

    modelmanager.remove_step('small-mnl-test')
Beispiel #17
0
def test_observation_sampling(orca_session):
    modelmanager.initialize()

    m = LargeMultinomialLogitStep()
    m.choosers = 'obs'
    m.alternatives = 'alts'
    m.choice_column = 'choice'
    m.model_expression = 'obsval + altval'
    
    m.fit()
    assert(len(m.mergedchoicetable.to_frame()) == 200)
    
    m.chooser_sample_size = 5
    m.fit()
    assert(len(m.mergedchoicetable.to_frame()) == 100)
    
    m.name = 'mnl-test'
    modelmanager.register(m)
    
    modelmanager.initialize()
    m = modelmanager.get_step('mnl-test')
    assert(m.chooser_sample_size == 5)
    
    modelmanager.remove_step('mnl-test')
Beispiel #18
0
import time
import argparse
import numpy as np

import orca

from lcog import datasources
from lcog import variables
from lcog import models

### Template imports
from urbansim.models import util
from urbansim_templates import modelmanager as mm
from urbansim_templates.models import OLSRegressionStep

mm.initialize()


def run(forecast_year=2035, random_seed=False):
    """
    Set up and run simulation.
    Parameters
    ----------
    forecast_year : int, optional
        Year to simulate to. If year argument is passed from the terminal, then
        that year is applied here, otherwise a default value is applied.
    random_seed : int, optional
        Random seed.
    Returns
    -------
    _ : None