예제 #1
0
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)
    # 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.parameters == ("Param1", )
        for param in range(16):
            assert trg[param].cells[cells].parameters == ("Param2", )
            assert trg(param).cells[cells](param + 100) == offset + param
예제 #2
0
def test_single_param(tmp_path):

    m, s = mx.new_model(), mx.new_space()

    s.new_cells_from_csv(filepath=CSV_SINGLE_PARAM,
                         cells=None,
                         param=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)
    # 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 == ("Param", )
        for param in range(16):
            assert trg.cells[cells](param) == offset + param
예제 #3
0
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
예제 #4
0
def extra_params(request, tmp_path):
    range_, orientation, write_method = 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,
    )

    getattr(mx, write_method)(model, tmp_path)
    # Write twice to check copy from renamed backup.
    m2 = mx.read_model(tmp_path)
    getattr(mx, write_method)(m2, tmp_path)

    target = mx.read_model(tmp_path)

    return model, target
예제 #5
0
def test_fastlife():

    simple = mx.read_model(simplepath)
    fast = mx.read_model(fastpath)

    simplesum = sum(
        [simple.Projection[i].PV_NetCashflow(0) for i in range(1, 301)])
    fastsum = sum(fast.Projection.PV_NetCashflow(0))

    assert simplesum == fastsum
예제 #6
0
def test_dynmodel(dynmodel, tmp_path, write_method, rename):
    """Test dynamic inputs"""

    # Added rename for https://github.com/fumitoh/modelx/issues/42

    path_ = tmp_path / "testdir"
    getattr(mx, write_method)(dynmodel, path_)
    if rename:
        m = mx.read_model(path_, name="renamed")
    else:
        m = mx.read_model(path_)

    assert m.SpaceA[0] is m.SpaceB.RefSpaceA
    assert m.SpaceA[1].foo[2] == 3
예제 #7
0
def test_dyntotal(dyntotal, tmp_path, write_method, rename):

    path_ = tmp_path / "testdir"
    getattr(mx, write_method)(dyntotal, path_)
    dyntotal.close()
    if rename:
        m = mx.read_model(path_, name="renamed")
    else:
        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
    m.close()
예제 #8
0
def test_false_value(tmp_path, write_method):
    # https://github.com/fumitoh/modelx/issues/39
    m = mx.new_model()
    s = mx.new_space()
    s.a = False
    getattr(mx, write_method)(m, tmp_path / "model")
    m2 = mx.read_model(tmp_path / "model")
예제 #9
0
def test_serialize_refmode(tmp_path):
    """
        m---A---RefDynCells --(abs)-->B[1, 2].foo
          |   +-RefSpace --(rel)-->B
          |   +-RefCells --(auto)-->B.foo
          |
          +-B[x, y]---foo
    """
    m = mx.new_model()
    A = mx.new_space('A')
    B = mx.new_space('B')
    B.parameters = ('x', 'y')

    @mx.defcells
    def foo(x):
        return x

    A.absref(RefDynCells=B[1, 2].foo)
    A.relref(RefSpace=B)
    A.RefCells = B.foo

    m.write(tmp_path / 'refpickle')
    m2 = mx.read_model(tmp_path / 'refpickle')

    assert m2.A.RefSpace is m2.B
    assert m2.A.RefDynCells is m2.B[1, 2].foo
    assert m2.A.RefCells is m2.B.foo

    assert m2.A._get_object('RefSpace', as_proxy=True).refmode == "relative"
    assert m2.A._get_object('RefDynCells', as_proxy=True).refmode == "absolute"
    assert m2.A._get_object('RefCells', as_proxy=True).refmode == "auto"
예제 #10
0
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)
예제 #11
0
def test_assign_dynamic_space_to_ref2(tmp_path, write_method):
    """
       m-+-a-d-c<-c()
         |
         +-b
         |
         +-c()-x<-b
    """
    # https://github.com/fumitoh/modelx/issues/37

    m = mx.new_model()
    m.new_space('a')

    def t_arg():
        pass

    m.new_space('b')
    m.new_space('c', formula=t_arg, refs={'x': m.b})
    m.a.new_space('d',refs={'c':m.c()})
    path_ = tmp_path / "model"
    getattr(mx, write_method)(m, path_)
    m.close()
    m2 = mx.read_model(path_)
    assert m2.a.d.c is m2.c()
    m2.close()
예제 #12
0
def test_new_pandas(tmp_path, pdobj, meth, is_relative, save_meth, filetype):

    p = getattr(mx, meth)()
    parent_name = p.name

    path = "files/testpandas.xlsx" if is_relative else (tmp_path /
                                                        "testpandas.xlsx")

    p.new_pandas(name="pdref", path=path, data=pdobj, filetype=filetype)

    if save_meth == "backup":
        datapath = tmp_path / "data"
        datapath.mkdir()
        getattr(p.model, save_meth)(tmp_path / "model", datapath=datapath)

        p.model.close()
        m2 = mx.restore_model(tmp_path / "model", datapath=datapath)

    else:
        getattr(p.model, save_meth)(tmp_path / "model")
        p.model.close()
        m2 = mx.read_model(tmp_path / "model")

    p2 = m2 if meth == "new_model" else m2.spaces[parent_name]

    if isinstance(pdobj, pd.DataFrame):
        pd.testing.assert_frame_equal(getattr(p2, "pdref")(), pdobj)
        pd.testing.assert_frame_equal(getattr(p2, "pdref").value, pdobj)
    elif isinstance(pdobj, pd.Series):
        pd.testing.assert_series_equal(getattr(p2, "pdref")(), pdobj)
        pd.testing.assert_series_equal(getattr(p2, "pdref").value, pdobj)

    m2.close()
예제 #13
0
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
예제 #14
0
def test_dynmodel(dynmodel, tmp_path, write_method):

    path_ = tmp_path / "testdir"
    getattr(mx, write_method)(dynmodel, path_)
    m = mx.read_model(path_)

    assert m.SpaceA[0] is m.SpaceB.RefSpaceA
    assert m.SpaceA[1].foo[2] == 3
예제 #15
0
def test_nestedlife(testdata, func):
    model = mx.read_model(modelpath)

    data = get_nested(func(model), 'PolsSurr')

    with open(testdata, 'rb') as file:
        data_saved = pickle.load(file)

    assert data == data_saved
예제 #16
0
def test_new_excel_range(tmp_path, meth, is_relative, edit_value, save_meth):
    """Check data new_excel_range

    Check data before and after value edit before after saving and reloading
    """

    p = getattr(mx, meth)()
    parent_name = p.name

    for kwargs in testargs:

        kwargs = kwargs.copy()

        kwargs["path"] = ("files/testexcel.xlsx"
                          if is_relative else tmp_path / "testexcel.xlsx")
        kwargs["sheet"] = "TestTables"
        kwargs["loadpath"] = XL_TESTDATA
        expected = kwargs.pop("expected").copy()
        p.new_excel_range(**kwargs)

        xlr = getattr(p, kwargs["name"])

        if edit_value:
            for key, val in xlr.items():
                xlr[key] = 2 * val

            for key, val in expected.items():
                expected[key] = 2 * val

        assert xlr == expected

    if save_meth == "backup":
        datapath = tmp_path / "data"
        datapath.mkdir()
        getattr(p.model, save_meth)(tmp_path / "model", datapath=datapath)

        p.model.close()
        m2 = mx.restore_model(tmp_path / "model", datapath=datapath)

    else:
        getattr(p.model, save_meth)(tmp_path / "model")
        p.model.close()
        m2 = mx.read_model(tmp_path / "model")

    p2 = m2 if meth == "new_model" else m2.spaces[parent_name]

    for kwargs in testargs:

        name = kwargs["name"]
        expected = kwargs["expected"].copy()

        if edit_value:
            expected = {key: 2 * val for key, val in expected.items()}

        assert getattr(p2, name) == expected

    m2.close()
예제 #17
0
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)
예제 #18
0
def test_back_compat_fastlife(tmp_path):

    import lifelib

    projpath = tmp_path / "fastlife"
    lifelib.create("fastlife", projpath)

    m = mx.read_model(projpath / "model")

    assert sum(m.Projection.PV_NetCashflow(0)) == 288922348.168899
예제 #19
0
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
예제 #20
0
def test_dyntotal(dyntotal, tmp_path, write_method):

    path_ = tmp_path / "testdir"
    getattr(mx, write_method)(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
예제 #21
0
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)
    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
예제 #22
0
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)
예제 #23
0
def test_new_module(tmp_path, parent, save_meth, module):

    if parent == "model":
        p = mx.new_model(name="Parent")
    else:
        p = mx.new_model().new_space(name="Parent")

    p.new_module(name="Foo", path="Parent/Foo", module=module)
    p.Bar = p.Foo

    assert p.Foo.modbar(2) == 4

    getattr(p.model, save_meth)(tmp_path / "model")
    p.model.close()

    if save_meth == "backup":
        m2 = mx.restore_model(tmp_path / "model")
    else:
        m2 = mx.read_model(tmp_path / "model")

    p2 = m2 if parent == "model" else m2.spaces["Parent"]

    assert p2.Foo.modbar(2) == 4
    assert p2.Bar is p2.Foo

    # Check saving again
    # https://github.com/fumitoh/modelx/issues/45
    getattr(p2.model, save_meth)(tmp_path / "model")
    m2.close()

    if save_meth == "backup":
        m3 = mx.restore_model(tmp_path / "model")
    else:
        m3 = mx.read_model(tmp_path / "model")

    p3 = m3 if parent == "model" else m3.spaces["Parent"]

    assert p3.Foo.modbar(2) == 4
    assert p3.Bar is p3.Foo
예제 #24
0
def test_package(library, model_dir, target, method, args, expected, tmp_path):

    lib_dir = os.path.join(tmp_path, library)
    lifelib.create(library, lib_dir)

    m = mx.read_model(os.path.join(lib_dir, model_dir))

    actual = mx.core.mxsys.get_object_from_tupleid(target)

    if method:
        actual = getattr(actual, method)(*args)

    assert math.isclose(actual, expected)
예제 #25
0
def extra_multiple_prams(request, tmp_path):

    range_, orientation, write_method = 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],
        names_col=1,
        param_rows=[1],
        param_order=[1, 2, 0],
        transpose=orientation,
    )
    getattr(mx, write_method)(model, tmp_path)
    m2 = mx.read_model(tmp_path)

    # Write twice to check copy from renamed backup.
    getattr(mx, write_method)(m2, tmp_path)
    target = mx.read_model(tmp_path).spaces[space.name]
    return space, target
예제 #26
0
def test_shared_data(tmp_path, parent, save_meth):

    for i in range(2):

        m = mx.new_model("SharedDataTest" + str(i))

        if parent == "model":
            p = m
        else:
            p = m.new_space("SharedDataTest")

        parent_name = p.name

        kwargs = testargs[i].copy()

        kwargs["path"] = tmp_path / "testexcel.xlsx"
        kwargs["sheet"] = "TestTables"
        kwargs["loadpath"] = XL_TESTDATA
        expected = kwargs.pop("expected").copy()
        p.new_excel_range(**kwargs)

        xlr = getattr(p, kwargs["name"])

        for key, val in xlr.items():
            xlr[key] = 2 * val

        for key, val in expected.items():
            expected[key] = 2 * val

        getattr(m, save_meth)(tmp_path / ("model" + str(i)))
        assert xlr == expected

    for i in range(2):
        m = mx.get_models()["SharedDataTest" + str(i)]
        m.close()

    for i in range(2):
        m2 = mx.read_model(tmp_path / ("model" + str(i)))
        p2 = m2 if parent == "model" else m2.spaces["SharedDataTest"]
        kwargs = testargs[i]
        name = kwargs["name"]
        expected = kwargs["expected"].copy()
        expected = {key: 2 * val for key, val in expected.items()}

        assert getattr(p2, name) == expected

    for i in range(2):
        m = mx.get_models()["SharedDataTest" + str(i)]
        m.close()

    assert not mx.core.mxsys._check_sanity()
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)
    # 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
예제 #28
0
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)
예제 #30
0
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)
    target = mx.read_model(tmp_path).spaces[space.name]
    return space, target