Beispiel #1
0
def test_combine_matrices():

    matrix1 = {
        ("s1", "s1"): "1",
        ("s2", "s2"): "2",
    }
    mat1 = KMatrix()
    mat1.label = "A"
    mat1.matrix = matrix1

    matrix2 = {
        ("s2", "s2"): "3",
        ("s3", "s3"): "4",
    }
    mat2 = KMatrix()
    mat2.label = "B"
    mat2.matrix = matrix2

    combined = mat1.combine(mat2)

    assert combined.label == "A+B"
    assert combined.matrix[("s1", "s1")].full_label == "1"
    assert combined.matrix[("s2", "s2")].full_label == "3"
    assert combined.matrix[("s3", "s3")].full_label == "4"
def test_unibranched():

    compartments = ["s1", "s2", "s3"]
    matrix = {
        ("s2", "s1"): "1",
        ("s3", "s2"): "2",
        ("s2", "s2"): "2",
        ("s3", "s3"): "3",
    }

    params = ParameterGroup.from_list([3, 4, 5, 1, 0])
    mat = KMatrix()
    mat.label = ""
    mat.matrix = matrix
    mat = mat.fill(None, params)

    jvec = ["4", "5", "5"]
    con = InitialConcentration()
    con.label = ""
    con.compartments = compartments
    con.parameters = jvec
    con = con.fill(None, params)

    assert not mat.is_unibranched(con)

    matrix = {
        ("s2", "s1"): "1",
        ("s2", "s2"): "2",
    }

    compartments = ["s1", "s2"]
    params = ParameterGroup.from_list([0.55, 0.0404, 1, 0])
    mat = KMatrix()
    mat.label = ""
    mat.matrix = matrix
    mat = mat.fill(None, params)

    jvec = ["3", "4"]
    con = InitialConcentration()
    con.label = ""
    con.compartments = compartments
    con.parameters = jvec
    con = con.fill(None, params)

    print(mat.reduced(compartments))
    assert mat.is_unibranched(con)

    wanted_a_matrix = np.asarray([
        [1, -1.079278],
        [0, 1.079278],
    ])

    print(mat.a_matrix_unibranch(con))
    assert np.allclose(mat.a_matrix_unibranch(con), wanted_a_matrix)
def test_matrix_non_unibranch(matrix):

    params = ParameterGroup.from_list(matrix.params)

    mat = KMatrix()
    mat.label = ""
    mat.matrix = matrix.matrix
    mat = mat.fill(None, params)

    con = InitialConcentration()
    con.label = ""
    con.compartments = matrix.compartments
    con.parameters = matrix.jvec
    con = con.fill(None, params)

    for comp in matrix.compartments:
        assert comp in mat.involved_compartments()

    print(mat.reduced(matrix.compartments))
    assert np.array_equal(mat.reduced(matrix.compartments),
                          matrix.wanted_array)

    print(mat.full(matrix.compartments).T)
    assert np.allclose(mat.full(matrix.compartments), matrix.wanted_full)

    print(mat.eigen(matrix.compartments)[0])
    print(mat.eigen(matrix.compartments)[1])
    vals, vec = mat.eigen(matrix.compartments)
    assert np.allclose(vals, matrix.wanted_eigen_vals)
    assert np.allclose(vec, matrix.wanted_eigen_vec)

    print(mat._gamma(vec, con))
    assert np.allclose(mat._gamma(vec, con), matrix.wanted_gamma)

    print(mat.a_matrix_non_unibranch(con))
    assert np.allclose(mat.a_matrix_non_unibranch(con), matrix.wanted_a_matrix)