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, write_method): 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" getattr(mx, write_method)(m, modelpath) assert ziputil.exists(modelpath / s.name / CSV_MULTI_PARAMS.name) m2 = mx.read_model(modelpath) # Write twice to check copy from renamed backup. getattr(mx, write_method)(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 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_single_param(tmp_path, write_method): 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" getattr(mx, write_method)(m, modelpath) assert ziputil.exists(modelpath.joinpath(CSV_SINGLE_PARAM.name)) m2 = mx.read_model(modelpath) # Write twice to check copy from renamed backup. getattr(mx, write_method)(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 == ("Param", ) for param in range(16): assert trg.cells[cells](param) == offset + param
def test_read_write_model(testmodel, tmp_path): path_ = tmp_path / "testdir" path_.mkdir() write_model(testmodel, path_) m = read_model(path_) testutil.compare_model(testmodel, m)
def test_consts(consts): # Compare values src, trg = consts for cells, value in zip(["Cells1", "Cells2"], [1, 2]): assert trg.cells[cells]() == value # Compare components compare_model(src.model, trg.model)
def test_consts(consts): # Compare values src, trg = consts for product, offset in zip(["A", "B"], [0, 1]): for cells, value in zip(["Cells1", "Cells2"], [1, 2]): assert trg.TestSpace[product].cells[cells]() == value + offset # Compare components compare_model(src, trg)
def test_single_param(single_param): # Compare values src, trg = single_param 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 # Compare components compare_model(src.model, trg.model)
def test_empty_params(empty_params): # Compare values src, trg = empty_params for cells, offset in zip(["Cells1", "Cells2"], [0, 1]): assert trg.cells[cells]() == 0 + offset assert trg.cells[cells](1) == 1 + offset assert trg.cells[cells](None, 2) == 2 + offset # Compare components compare_model(src.model, trg.model)
def test_multiple_params(multiple_prams): # Compare values src, trg = multiple_prams 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 # Compare components compare_model(src.model, trg.model)
def test_write_cells_from_pandas(tmp_path, write_method): m, space = mx.new_model(), mx.new_space() space.new_cells_from_pandas(testdf) modelpath = tmp_path / "write_cells_from_pandas" getattr(mx, write_method)(m, modelpath) m2 = mx.read_model(modelpath) compare_model(m, m2) assert space.frame.equals(m2.spaces[space.name].frame)
def test_extra_params(extra_params): # Compare values src, trg = extra_params for cells, offset in zip(["Cells1", "Cells2"], [1000, 2000]): assert trg.cells[cells].parameters == ("Sex", "Param") for param in range(16): assert trg.cells[cells]("M", param) == offset + param assert trg.cells[cells]("F", param) == offset + param + 1000 # Compare components compare_model(src.model, trg.model)
def test_nested_space(tmp_path, write_method): m, s = mx.new_model(), mx.new_space() ns = s.new_space() @mx.defcells def foo(x): return x foo[0] = 2 getattr(mx, write_method)(m, tmp_path / "model") m2 = read_model(tmp_path / "model") testutil.compare_model(m, m2)
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 test_extra_multiple_prams(extra_multiple_prams): # Compare values src, trg = extra_multiple_prams for cells, offset1 in zip(["Cells1", "Cells2"], [1000, 2000]): assert trg.cells[cells].parameters == ("Product", "Sex", "Year") for product, offset2 in zip(["A", "B"], [0, 1000]): for sex, offset3 in zip(["M", "F"], [0, 1000]): for year in range(10): assert (trg.cells[cells](product, sex, year) == year + offset1 + offset2 + offset3) # Compare components compare_model(src.model, trg.model)
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_nested_derived(tmp_path, write_method): """ m---A---B | C---B* """ m = mx.new_model() A = m.new_space('A') B = A.new_space('B') C = m.new_space('C', bases=A) assert C.B._is_derived() getattr(mx, write_method)(m, tmp_path / "model") m2 = read_model(tmp_path / "model") testutil.compare_model(m, m2)
def test_extra_params(extra_params): # Compare values src, trg = extra_params for product, offset1 in zip(["A", "B"], [0, 1000]): child = trg.TestSpace[product] assert child.Product == product for cells, offset2 in zip(["Cells1", "Cells2"], [1000, 2000]): assert child.cells[cells].parameters == ("Sex", "Year") for sex, offset3 in zip(["M", "F"], [0, 1000]): for year in range(10): assert (child.cells[cells](sex, year) == year + offset1 + offset2 + offset3) # Compare components compare_model(src, trg)
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 test_read_write_model(testmodel, tmp_path, name, version, as_method, write_method): path_ = tmp_path / "testdir" if as_method: getattr(testmodel, write_method)(path_) else: if as_method == "zip": kwargs = dict(compression=zipfile.ZIP_STORED, version=version) else: kwargs = {} getattr(mx, write_method + "_model")(testmodel, path_, **kwargs) 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_null_object(tmp_path, write_method): """ m---A---B +---b <- B | C(A) """ m = mx.new_model() A = m.new_space('A') B = A.new_space('B') A.b = B C = m.new_space('C', bases=A) del A.B assert not A.b._is_valid() assert not C.b._is_valid() getattr(mx, write_method)(m, tmp_path / "model") m2 = read_model(tmp_path / "model") assert not m2.A.b._is_valid() assert not m2.C.b._is_valid() testutil.compare_model(m, m2)
def test_null_object(tmp_path): """ m---A---B +---b <- B | C(A) """ m = mx.new_model() A = m.new_space('A') B = A.new_space('B') A.b = B C = m.new_space('C', bases=A) del A.B assert not A.b._is_valid() assert not C.b._is_valid() m.backup(tmp_path / "model") m2 = mx.restore_model(tmp_path / "model") assert not m2.A.b._is_valid() assert not m2.C.b._is_valid() testutil.compare_model(m, m2)