def test_multiple_params(tmp_path): m = mx.new_model() s = m.new_space_from_csv( filepath=CSV_MULTI_PARAMS, space=None, cells=None, param=None, space_params=[0], index_col=[0, 1] ) modelpath = tmp_path / "csv_mult_params" mx.write_model(m, modelpath) assert modelpath.joinpath(CSV_MULTI_PARAMS.name).exists() m2 = mx.read_model(modelpath) # Compare components compare_model(m, m2) # Compare values trg = m2.spaces[s.name] for cells, offset in zip(["Cells1", "Cells2"], [1000, 2000]): assert trg.parameters == ("Param1",) for param in range(16): assert trg[param].cells[cells].parameters == ("Param2",) assert trg(param).cells[cells](param + 100) == offset + param
def test_multiple_params(tmp_path): m, s = mx.new_model(), mx.new_space() s.new_cells_from_csv(filepath=CSV_MULTI_PARAMS, cells=None, param=None, index_col=[0, 1]) modelpath = tmp_path / "csv_mult_params" mx.write_model(m, modelpath) assert modelpath.joinpath(CSV_MULTI_PARAMS.name).exists() m2 = mx.read_model(modelpath) # Write twice to check copy from renamed backup. mx.write_model(m2, modelpath) m2 = mx.read_model(modelpath) # Compare components compare_model(m, m2) # Compare values trg = m2.spaces[s.name] for cells, offset in zip(["Cells1", "Cells2"], [1000, 2000]): assert trg.cells[cells].parameters == ("Param1", "Param2") for param in range(16): assert trg.cells[cells](param, param + 100) == offset + param
def test_single_param(tmp_path): m = mx.new_model() s = m.new_space_from_csv( filepath=CSV_SINGLE_PARAM, space=None, cells=None, param=None, space_params=None, index_col=0 ) modelpath = tmp_path / "csv_single_param" mx.write_model(m, modelpath) assert modelpath.joinpath(CSV_SINGLE_PARAM.name).exists() m2 = mx.read_model(modelpath) # Compare components compare_model(m, m2) # Compare values trg = m2.spaces[s.name] for cells, offset in zip(["Cells1", "Cells2"], [1000, 2000]): assert trg.cells[cells].parameters == ("Param",) for param in range(16): assert trg.cells[cells](param) == offset + param
def extra_params(request, tmp_path): range_, orientation = request.param model = mx.new_model() space = model.new_space_from_excel( book=XL_TESTDATA, range_=range_, sheet="TestSpaceTables", name="TestSpace", names_row=0, param_cols=[0, 1], names_col=1, param_rows=[1], space_param_order=[1], cells_param_order=[2, 0], transpose=orientation, ) mx.write_model(model, tmp_path) # Write twice to check copy from renamed backup. m2 = mx.read_model(tmp_path) mx.write_model(m2, tmp_path) target = mx.read_model(tmp_path) return model, target
def test_with_lifelib(testpaths, project): build_path, write_path = testpaths from lifelib.commands import create testproj = project + "_test" projpath = build_path / testproj create.main([ "--template", project, str(projpath) ]) with SysPath(str(projpath.parent)): module = importlib.import_module(testproj + "." + project) with SysPath(str(projpath)): m = module.build() m.hoge = "hoge" m.foo = 1 m.bar = m.Input m.Input.new_cells(formula=lambda x: 3 * x) m.none = None write_model(m, str(write_path / project)) m2 = read_model(str(write_path / project)) testutil.compare_model(m, m2) _PROJECTS[project](m, m2)
def test_dynmodel(dynmodel, tmp_path): path_ = tmp_path / "testdir" mx.write_model(dynmodel, path_) m = mx.read_model(path_) assert m.SpaceA[0] is m.SpaceB.RefSpaceA assert m.SpaceA[1].foo[2] == 3
def test_dyntotal(dyntotal, tmp_path): path_ = tmp_path / "testdir" mx.write_model(dyntotal, path_) m = mx.read_model(path_) assert m.s(-1).a(2) == 2 * 10 assert m.s(0).a(2) == 2 assert m.s(-1).b(2, 3) == 2 * 3 * 10 assert m.s(0).b(2, 3) == 2 * 3
def test_write_cells_from_pandas(tmp_path): m, space = mx.new_model(), mx.new_space() space.new_cells_from_pandas(testdf) modelpath = tmp_path / "write_cells_from_pandas" mx.write_model(m, modelpath) m2 = mx.read_model(modelpath) compare_model(m, m2) assert space.frame.equals(m2.spaces[space.name].frame)
def test_write_space_from_pandas(tmp_path): m = mx.new_model() s = m.new_space_from_pandas(testdf, space_params=[0]) modelpath = tmp_path / "write_space_from_pandas" mx.write_model(m, modelpath) m2 = mx.read_model(modelpath) s2 = m2.spaces[s.name] compare_model(m, m2) for first, second in testdf.index: assert s[first].frame.equals(s2[first].frame)
def empty_params(request, tmp_path): range_, orientation = request.param model, space = mx.new_model(), mx.new_space() space.new_cells_from_excel( book=XL_TESTDATA, range_=range_, sheet="TestTables", names_row=0, param_cols=[0, 1], param_order=[0, 1], transpose=orientation, ) mx.write_model(model, tmp_path) target = mx.read_model(tmp_path).spaces[space.name] return space, target
def test_read_write_model(testmodel, tmp_path, name, version, as_method): path_ = tmp_path / "testdir" if as_method: testmodel.write(path_) else: write_model(testmodel, path_, version=version) m = read_model(path_, name=name) assert m.name == (name if name else "TestModel") if name is None: testutil.compare_model(testmodel, m) # Check identities of modelx objects as refs assert m is m.TestSpace.v assert m.TestSpace.foo is m.TestSpace.w assert m.TestSpace.s is m.Another
def test_with_lifelib(testpaths, project): build_path, write_path, zip_path = testpaths import lifelib testproj = project + "_test" projpath = build_path / testproj if lifelib.VERSION > (0, 0, 14): lifelib.create(project, projpath) scriptpath = projpath / "scripts" else: from lifelib.commands import create create.main(["--template", project, str(projpath)]) scriptpath = projpath.parent with SysPath(str(scriptpath)): if lifelib.VERSION > (0, 0, 14): module = importlib.import_module(project) else: module = importlib.import_module(testproj + "." + project) with SysPath(str(projpath)): m = module.build() m.hoge = "hoge" m.foo = 1 m.bar = m.Input m.Input.new_cells(formula=lambda x: 3 * x) m.none = None mx.write_model(m, str(write_path / project)) mx.zip_model(m, str(zip_path / project)) m2 = mx.read_model(str(write_path / project)) testutil.compare_model(m, m2) m3 = mx.read_model(str(zip_path / project)) testutil.compare_model(m, m3) _PROJECTS[project](m, m2) _PROJECTS[project](m, m3)
def consts(request, tmp_path): range_, orientation = request.param model = mx.new_model() space = model.new_space_from_excel( book=XL_TESTDATA, range_=range_, sheet="TestSpaceTables", name="TestSpace", names_row=0, param_cols=[0], space_param_order=[0], cells_param_order=[], transpose=orientation, ) mx.write_model(model, tmp_path) target = mx.read_model(tmp_path) return model, target
def consts(request, tmp_path): range_, orientation = request.param model, space = mx.new_model(), mx.new_space() space.new_cells_from_excel( book=XL_TESTDATA, range_=range_, sheet="TestTables", names_row=0, param_cols=[], param_order=[], transpose=orientation, ) mx.write_model(model, tmp_path) m2 = mx.read_model(tmp_path) # Write twice to check copy from renamed backup. mx.write_model(m2, tmp_path) target = mx.read_model(tmp_path).spaces[space.name] return space, target
def test_reference_identity(pickletest, tmp_path, name): path_ = tmp_path / "testdir" write_model(pickletest, path_) m = read_model(path_, name=name) assert m.SpaceA.another_space is m.SpaceB foo, another, model = m.SpaceA.iflist assert foo is m.SpaceA.foo assert another is m.SpaceA.another_space assert model is m assert m.SpaceA.ifwrap.a is foo assert m.SpaceA.ifwrap.b is m.SpaceA.another_space assert m is m.SpaceA.ifwrap.c # same objects assert m.SpaceA.o is m.SpaceB.ou[0] assert m.SpaceA.u is m.SpaceB.ou[1] # Cells input data assert dict(m.SpaceA.foo) == {0: 0, 1: m} assert dict(m.SpaceA.lambdacells) == {0: "123", m.SpaceA: 3}
def mx_write_model(self, model, modelpath, backup): import modelx as mx mx.write_model(mx.get_models()[model], modelpath, backup)