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_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 test_modelmanager_registration(orca_session): """ Check that modelmanager registration and auto-run work as expected. """ c = ColumnFromExpression() c.data.table = 'obs' c.data.expression = 'a + b' c.output.column_name = 'c' modelmanager.register(c) modelmanager.remove_step(c.meta.name) assert ('c' in orca.get_table('obs').columns)
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_without_autorun(orca_session, data): """ Confirm that disabling autorun works. """ t = LoadTable() t.table = 'buildings' t.source_type = 'csv' t.path = 'data/buildings.csv' t.csv_index_cols = 'building_id' t.autorun = False modelmanager.register(t) assert 'buildings' not in orca.list_tables() modelmanager.remove_step(t.name)
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_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')
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_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_expression_with_standalone_columns(orca_session): """ Check that expression can assemble data from stand-alone columns that are not part of the core DataFrame wrapped by a table. """ c = ColumnFromExpression() c.data.table = 'obs' c.data.expression = 'a + b' c.output.column_name = 'c' modelmanager.register(c) modelmanager.remove_step(c.meta.name) d = ColumnFromExpression() d.data.table = 'obs' d.data.expression = 'a + c' d.output.column_name = 'd' d.run() assert ('d' in orca.get_table('obs').columns)
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)
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_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_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')
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')