def test_parameter_summary(): m = larch.example(1, legacy=True) m.load_data() m.loglike_null() m.set_values( **{ 'ASC_BIKE': -2.3763275319243244, 'ASC_SR2': -2.1780143286612037, 'ASC_SR3P': -3.725078388760564, 'ASC_TRAN': -0.6708609582690096, 'ASC_WALK': -0.20677521181801753, 'hhinc#2': -0.0021699381002406883, 'hhinc#3': 0.0003577067151217295, 'hhinc#4': -0.00528632366072714, 'hhinc#5': -0.012807975284603574, 'hhinc#6': -0.009686302933787567, 'totcost': -0.00492023540098787, 'tottime': -0.05134209452571549, }) m.loglike() assert m.parameter_summary('xml').tostring() == ( '<div><table><thead><tr><th style="text-align: left;">Category</th><th style="text-align: left;">Parameter</th>' '<th>Value</th><th>Null Value</th></tr></thead><tbody><tr><th rowspan="2" style="vertical-align: top; text-alig' 'n: left;">LOS</th><th style="vertical-align: top; text-align: left;">totcost</th><td>-0.00492</td><td>0.0</td>' '</tr><tr><th style="vertical-align: top; text-align: left;">tottime</th><td>-0.05134</td><td>0.0</td></tr><tr>' '<th rowspan="5" style="vertical-align: top; text-align: left;">ASCs</th><th style="vertical-align: top; text-a' 'lign: left;">ASC_BIKE</th><td>-2.376</td><td>0.0</td></tr><tr><th style="vertical-align: top; text-align: left' ';">ASC_SR2</th><td>-2.178</td><td>0.0</td></tr><tr><th style="vertical-align: top; text-align: left;">ASC_SR3P' '</th><td>-3.725</td><td>0.0</td></tr><tr><th style="vertical-align: top; text-align: left;">ASC_TRAN</th><td>-' '0.6709</td><td>0.0</td></tr><tr><th style="vertical-align: top; text-align: left;">ASC_WALK</th><td>-0.2068</t' 'd><td>0.0</td></tr><tr><th rowspan="5" style="vertical-align: top; text-align: left;">Income</th><th style="ve' 'rtical-align: top; text-align: left;">hhinc#2</th><td>-0.00217</td><td>0.0</td></tr><tr><th style="vertical-al' 'ign: top; text-align: left;">hhinc#3</th><td>0.0003577</td><td>0.0</td></tr><tr><th style="vertical-align: top' '; text-align: left;">hhinc#4</th><td>-0.005286</td><td>0.0</td></tr><tr><th style="vertical-align: top; text-a' 'lign: left;">hhinc#5</th><td>-0.01281</td><td>0.0</td></tr><tr><th style="vertical-align: top; text-align: lef' 't;">hhinc#6</th><td>-0.009686</td><td>0.0</td></tr></tbody></table></div>' ) m.ordering = () assert m.parameter_summary('xml').tostring() == ( '<div><table><thead><tr><th style="text-align: left;">Parameter</th><th>Value</th><th>Null Value</th></tr></the' 'ad><tbody><tr><th style="vertical-align: top; text-align: left;">ASC_BIKE</th><td>-2.376</td><td>0.0</td></tr>' '<tr><th style="vertical-align: top; text-align: left;">ASC_SR2</th><td>-2.178</td><td>0.0</td></tr><tr><th sty' 'le="vertical-align: top; text-align: left;">ASC_SR3P</th><td>-3.725</td><td>0.0</td></tr><tr><th style="vertic' 'al-align: top; text-align: left;">ASC_TRAN</th><td>-0.6709</td><td>0.0</td></tr><tr><th style="vertical-align:' ' top; text-align: left;">ASC_WALK</th><td>-0.2068</td><td>0.0</td></tr><tr><th style="vertical-align: top; tex' 't-align: left;">hhinc#2</th><td>-0.00217</td><td>0.0</td></tr><tr><th style="vertical-align: top; text-align: ' 'left;">hhinc#3</th><td>0.0003577</td><td>0.0</td></tr><tr><th style="vertical-align: top; text-align: left;">h' 'hinc#4</th><td>-0.005286</td><td>0.0</td></tr><tr><th style="vertical-align: top; text-align: left;">hhinc#5</' 'th><td>-0.01281</td><td>0.0</td></tr><tr><th style="vertical-align: top; text-align: left;">hhinc#6</th><td>-0' '.009686</td><td>0.0</td></tr><tr><th style="vertical-align: top; text-align: left;">totcost</th><td>-0.00492</' 'td><td>0.0</td></tr><tr><th style="vertical-align: top; text-align: left;">tottime</th><td>-0.05134</td><td>0.' '0</td></tr></tbody></table></div>')
def test_excel_metadata(): import larch.util.excel if larch.util.excel.xlsxwriter is not None: from larch.util.excel import _make_excel_writer from larch.util.temporaryfile import TemporaryDirectory tempdir = TemporaryDirectory() os.path.join(tempdir, 'larchtest.xlsx') m = larch.example(1, legacy=True) m.load_data() m.loglike_null() m.maximize_loglike() xl = _make_excel_writer(m, os.path.join(tempdir, 'larchtest.xlsx'), save_now=False) # xl.add_metadata('self', m) # now automatic xl.add_metadata('short', 123) xl.add_metadata('plain', 'text') xl.save() md = larch.util.excel.load_metadata( os.path.join(tempdir, 'larchtest.xlsx')) assert len(md) == 3 assert isinstance(md['_self_'], larch.Model) assert md['short'] == 123 assert md['plain'] == 'text' md_m = larch.util.excel.load_metadata( os.path.join(tempdir, 'larchtest.xlsx'), '_self_') assert isinstance(md_m, larch.Model) assert larch.util.excel.load_metadata( os.path.join(tempdir, 'larchtest.xlsx'), 'short') == 123 from numpy import inf assert md_m._get_cached_loglike_values() == approx({ 'nil': 0.0, 'null': -7309.600971749679, 'constants_only': 0.0, 'best': -inf }) md_m.estimation_statistics()
def simple_nl(): m = larch.example(1, legacy=True) mot = m.graph.new_node(parameter="MuSR", children=[2, 3]) m.set_values( **{ 'ASC_BIKE': -2.369583417234141, 'ASC_SR2': -2.1003772176440494, 'ASC_SR3P': -3.1652310001168176, 'ASC_TRAN': -0.6716514097035154, 'ASC_WALK': -0.20573835594484602, 'MuSR': 0.656152401524297, 'hhinc#2': -0.0018493368570253318, 'hhinc#3': -0.0005878635612891558, 'hhinc#4': -0.0051673845801924615, 'hhinc#5': -0.012777495307881156, 'hhinc#6': -0.00967649032713631, 'totcost': -0.004808452908429623, 'tottime': -0.05107162036470516, }) m.load_data() return m
def test_joint_parameter_summary(): m = larch.example(1, legacy=True) m.load_data() m.loglike_null() m.set_values( **{ 'ASC_BIKE': -2.3763275319243244, 'ASC_SR2': -2.1780143286612037, 'ASC_SR3P': -3.725078388760564, 'ASC_TRAN': -0.6708609582690096, 'ASC_WALK': -0.20677521181801753, 'hhinc#2': -0.0021699381002406883, 'hhinc#3': 0.0003577067151217295, 'hhinc#4': -0.00528632366072714, 'hhinc#5': -0.012807975284603574, 'hhinc#6': -0.009686302933787567, 'totcost': -0.00492023540098787, 'tottime': -0.05134209452571549, }) m.loglike() m.maximize_loglike() m_c = larch.Model(dataservice=m.dataservice) m_c.choice_ca_var = '_choice_' m_c.availability_var = '_avail_' m_c.utility_co[2] = P("ASC_SR2") m_c.utility_co[3] = P("ASC_SR3P") m_c.utility_co[4] = P("ASC_TRAN") m_c.utility_co[5] = P("ASC_BIKE") m_c.utility_co[6] = P("ASC_WALK") m_c.title = "Constants Only" m_c.load_data() m_c.loglike_null() m_c.maximize_loglike() from larch.util.summary import joint_parameter_summary stable_df(joint_parameter_summary([m, m_c], bases=[m_c]), 'joint_parameter_summary')
def test_dfs_feathers(): import tempfile m = example(1, legacy=True) with tempfile.TemporaryDirectory() as td: m.load_data() filename = os.path.join(td, 'dfs') m.dataframes.to_feathers(filename) d_co = m.dataframes.data_co.copy() d_ca = m.dataframes.data_ca.copy() d_ch = m.dataframes.data_ch.copy() d_av = m.dataframes.data_av.copy() m.dataframes.data_co.iloc[:] = 0.0 m.dataframes.data_ca.iloc[:] = 0.0 m.dataframes.data_ch.iloc[:] = 0.0 m.dataframes.data_av.iloc[:] = 0.0 assert all(m.dataframes.array_co().reshape(-1) == 0) assert all(m.dataframes.array_ca().reshape(-1) == 0) assert all(m.dataframes.array_ch().reshape(-1) == 0) assert all(m.dataframes.array_av().reshape(-1) == 0) m.dataframes.inject_feathers(filename) pandas.testing.assert_frame_equal(m.dataframes.data_co, d_co) pandas.testing.assert_frame_equal(m.dataframes.data_ca, d_ca) pandas.testing.assert_frame_equal(m.dataframes.data_ch, d_ch) pandas.testing.assert_frame_equal(m.dataframes.data_av, d_av) df = pandas.read_csv(example_file("MTCwork.csv.gz")) df.set_index(['casenum', 'altnum'], inplace=True) ds = DataFrames(df) filename2 = os.path.join(td, 'dfs1') ds.to_feathers(filename2) d_ce = ds.data_ce.copy() ds.data_ce.iloc[:] = 0.0 assert all(ds.array_ce().reshape(-1) == 0) ds.inject_feathers(filename2) pandas.testing.assert_frame_equal(ds.data_ce, d_ce) filename3 = os.path.join(td, 'dfs2') ds.to_feathers(filename3) ds2 = DataFrames.from_feathers(filename3) pandas.testing.assert_index_equal( ds.alternative_codes(), ds2.alternative_codes(), check_names=False, ) pandas.testing.assert_frame_equal( ds.data_ce, ds2.data_ce, ) pandas.testing.assert_frame_equal( ds.data_av, ds2.data_av, ) pandas.testing.assert_index_equal( ds.caseindex, ds2.caseindex, ) dfs2 = DataFrames.from_feathers(filename) pandas.testing.assert_index_equal( m.dataframes.alternative_codes(), dfs2.alternative_codes(), check_names=False, ) pandas.testing.assert_frame_equal( m.dataframes.data_co, dfs2.data_co, ) pandas.testing.assert_frame_equal( m.dataframes.data_ca, dfs2.data_ca, ) pandas.testing.assert_frame_equal( m.dataframes.data_av, dfs2.data_av, ) pandas.testing.assert_index_equal( m.dataframes.caseindex, dfs2.caseindex, )