예제 #1
0
class OneCompartmentDecay:
    scale = 2
    wanted_parameters = ParameterGroup.from_list([101e-4])
    initial_parameters = ParameterGroup.from_list([100e-5, [scale, {"vary": False}]])

    global_axis = np.asarray([1.0])
    model_axis = np.arange(0, 150, 1.5)

    sim_model_dict = {
        "megacomplex": {"m1": {"is_index_dependent": False}, "m2": {"type": "global_complex"}},
        "dataset": {
            "dataset1": {
                "initial_concentration": [],
                "megacomplex": ["m1"],
                "global_megacomplex": ["m2"],
                "kinetic": ["1"],
            }
        },
    }
    sim_model = DecayModel.from_dict(sim_model_dict)
    model_dict = {
        "megacomplex": {"m1": {"is_index_dependent": False}},
        "dataset": {
            "dataset1": {
                "initial_concentration": [],
                "megacomplex": ["m1"],
                "kinetic": ["1"],
                "scale": "2",
            }
        },
    }
    model_dict["dataset"]["dataset1"]["scale"] = "2"
    model = DecayModel.from_dict(model_dict)
예제 #2
0
class TwoCompartmentDecay:
    wanted_parameters = ParameterGroup.from_list([11e-4, 22e-5])
    initial_parameters = ParameterGroup.from_list([10e-4, 20e-5])

    global_axis = np.asarray([1.0])
    model_axis = np.arange(0, 150, 1.5)

    sim_model = DecayModel.from_dict(
        {
            "megacomplex": {"m1": {"is_index_dependent": False}, "m2": {"type": "global_complex"}},
            "dataset": {
                "dataset1": {
                    "initial_concentration": [],
                    "megacomplex": ["m1"],
                    "global_megacomplex": ["m2"],
                    "kinetic": ["1", "2"],
                }
            },
        }
    )
    model = DecayModel.from_dict(
        {
            "megacomplex": {"m1": {"is_index_dependent": False}},
            "dataset": {
                "dataset1": {
                    "initial_concentration": [],
                    "megacomplex": ["m1"],
                    "kinetic": ["1", "2"],
                }
            },
        }
    )
예제 #3
0
class ThreeDatasetDecay:
    wanted_parameters = ParameterGroup.from_list([101e-4, 201e-3])
    initial_parameters = ParameterGroup.from_list([100e-5, 200e-3])

    e_axis = np.asarray([1.0])
    c_axis = np.arange(0, 150, 1.5)

    e_axis2 = np.asarray([1.0, 2.01])
    c_axis2 = np.arange(0, 100, 1.5)

    e_axis3 = np.asarray([0.99, 3.0])
    c_axis3 = np.arange(0, 150, 1.5)

    model_dict = {
        "dataset": {
            "dataset1": {
                "initial_concentration": [],
                "megacomplex": [],
                "kinetic": ["1"]
            },
            "dataset2": {
                "initial_concentration": [],
                "megacomplex": [],
                "kinetic": ["1", "2"]
            },
            "dataset3": {
                "initial_concentration": [],
                "megacomplex": [],
                "kinetic": ["2"]
            },
        },
    }
    sim_model = DecayModel.from_dict(model_dict)
    model = sim_model
예제 #4
0
class OneComponentOneChannelGaussianIrf:
    model = KineticImageModel.from_dict({
        'initial_concentration': {
            'j1': {
                'compartments': ['s1'],
                'parameters': ['2']
            },
        },
        'megacomplex': {
            'mc1': {'k_matrix': ['k1']},
        },
        'k_matrix': {
            "k1": {'matrix': {("s1", "s1"): '1', }}
        },
        'irf': {
            'irf1': {'type': 'gaussian', 'center': '2', 'width': '3'},
        },
        'dataset': {
            'dataset1': {
                'initial_concentration': 'j1',
                'irf': 'irf1',
                'megacomplex': ['mc1'],
            },
        },
    })

    initial = ParameterGroup.from_list([101e-4, 0.1, 5,
                                        [1, {'vary': False, 'non-negative': False}]])
    wanted = ParameterGroup.from_list([101e-3, 0.3, 10,
                                       [1, {'vary': False, 'non-negative': False}]])

    time = np.asarray(np.arange(-10, 50, 1.5))
    axis = {"time": time, "pixel": np.asarray([0])}
    clp = xr.DataArray([[1]], coords=[('pixel', [0]), ('clp_label', ['s1'])])
class OneComponentOneChannelGaussianIrf:
    model = KineticImageModel.from_dict({
        "initial_concentration": {
            "j1": {
                "compartments": ["s1"],
                "parameters": ["2"]
            },
        },
        "megacomplex": {
            "mc1": {
                "k_matrix": ["k1"]
            },
        },
        "k_matrix": {
            "k1": {
                "matrix": {
                    ("s1", "s1"): "1",
                }
            }
        },
        "irf": {
            "irf1": {
                "type": "gaussian",
                "center": "2",
                "width": "3"
            },
        },
        "dataset": {
            "dataset1": {
                "initial_concentration": "j1",
                "irf": "irf1",
                "megacomplex": ["mc1"],
            },
        },
    })

    initial_parameters = ParameterGroup.from_list(
        [101e-4, 0.1, 1, [1, {
            "vary": False,
            "non-negative": False
        }]])
    wanted_parameters = ParameterGroup.from_list([
        [101e-3, {
            "non-negative": True
        }],
        [0.2, {
            "non-negative": True
        }],
        [2, {
            "non-negative": True
        }],
        [1, {
            "vary": False,
            "non-negative": False
        }],
    ])

    time = np.asarray(np.arange(-10, 50, 1.5))
    axis = {"time": time, "pixel": np.asarray([0])}
    clp = xr.DataArray([[1]], coords=[("pixel", [0]), ("clp_label", ["s1"])])
class OneComponentOneChannelMeasuredIrf:
    model = KineticImageModel.from_dict({
        "initial_concentration": {
            "j1": {
                "compartments": ["s1"],
                "parameters": ["2"]
            },
        },
        "megacomplex": {
            "mc1": {
                "k_matrix": ["k1"]
            },
        },
        "k_matrix": {
            "k1": {
                "matrix": {
                    ("s1", "s1"): "1",
                }
            }
        },
        "irf": {
            "irf1": {
                "type": "measured"
            },
        },
        "dataset": {
            "dataset1": {
                "initial_concentration": "j1",
                "irf": "irf1",
                "megacomplex": ["mc1"],
            },
        },
    })

    initial_parameters = ParameterGroup.from_list(
        [101e-4, [1, {
            "vary": False,
            "non-negative": False
        }]])
    wanted_parameters = ParameterGroup.from_list(
        [101e-3, [1, {
            "vary": False,
            "non-negative": False
        }]])

    time = np.asarray(np.arange(-10, 50, 1.5))
    axis = {"time": time, "pixel": np.asarray([0])}

    center = 0
    width = 5
    irf = (1 / np.sqrt(2 * np.pi)) * np.exp(-(time - center) *
                                            (time - center) /
                                            (2 * width * width))
    model.irf["irf1"].irfdata = irf

    clp = xr.DataArray([[1]], coords=[("pixel", [0]), ("clp_label", ["s1"])])
예제 #7
0
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)
예제 #8
0
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)
예제 #9
0
class ThreeDatasetDecay:
    wanted_parameters = ParameterGroup.from_list([101e-4, 201e-3])
    initial_parameters = ParameterGroup.from_list([100e-5, 200e-3])

    global_axis = np.asarray([1.0])
    model_axis = np.arange(0, 150, 1.5)

    global_axis2 = np.asarray([1.0, 2.01])
    model_axis2 = np.arange(0, 100, 1.5)

    global_axis3 = np.asarray([0.99, 3.0])
    model_axis3 = np.arange(0, 150, 1.5)

    sim_model_dict = {
        "megacomplex": {"m1": {"is_index_dependent": False}, "m2": {"type": "global_complex"}},
        "dataset": {
            "dataset1": {
                "initial_concentration": [],
                "megacomplex": ["m1"],
                "global_megacomplex": ["m2"],
                "kinetic": ["1"],
            },
            "dataset2": {
                "initial_concentration": [],
                "megacomplex": ["m1"],
                "global_megacomplex": ["m2"],
                "kinetic": ["1", "2"],
            },
            "dataset3": {
                "initial_concentration": [],
                "megacomplex": ["m1"],
                "global_megacomplex": ["m2"],
                "kinetic": ["2"],
            },
        },
    }
    sim_model = DecayModel.from_dict(sim_model_dict)

    model_dict = {
        "megacomplex": {"m1": {"is_index_dependent": False}},
        "dataset": {
            "dataset1": {"initial_concentration": [], "megacomplex": ["m1"], "kinetic": ["1"]},
            "dataset2": {
                "initial_concentration": [],
                "megacomplex": ["m1"],
                "kinetic": ["1", "2"],
            },
            "dataset3": {"initial_concentration": [], "megacomplex": ["m1"], "kinetic": ["2"]},
        },
    }
    model = DecayModel.from_dict(model_dict)
예제 #10
0
def test_single_dataset():
    model = MockModel.from_dict(
        {"dataset": {
            "dataset1": {
                "megacomplex": [],
            },
        }})
    model.grouped = lambda: True
    print(model.validate())
    assert model.valid()
    assert model.grouped()

    parameters = ParameterGroup.from_list([1, 10])
    print(model.validate(parameters))
    assert model.valid(parameters)
    axis_e = [1, 2, 3]
    axis_c = [5, 7, 9, 12]

    data = {
        "dataset1":
        xr.DataArray(np.ones((3, 4)),
                     coords=[("e", axis_e),
                             ("c", axis_c)]).to_dataset(name="data")
    }

    scheme = Scheme(model, parameters, data)
    problem = Problem(scheme)
    bag = problem.bag
    datasets = problem.groups
    assert len(datasets) == 1
    assert len(bag) == 3
    assert all(p.data.size == 4 for p in bag)
    assert all(p.descriptor[0].label == "dataset1" for p in bag)
    assert all(all(p.descriptor[0].axis == axis_c) for p in bag)
    assert [p.descriptor[0].index for p in bag] == axis_e
예제 #11
0
def test_simulate_dataset():
    model = MockModel.from_dict({
        "dataset": {
            "dataset1": {
                "megacomplex": [],
            },
        }
    })
    print(model.validate())
    assert model.valid()

    parameter = ParameterGroup.from_list([1, 1])
    print(model.validate(parameter))
    assert model.valid(parameter)

    est_axis = np.asarray([1, 1, 1, 1])
    cal_axis = np.asarray([2, 2, 2])

    data = simulate(model, 'dataset1', parameter, {'e': est_axis, 'c': cal_axis})
    assert np.array_equal(data["c"], cal_axis)
    assert np.array_equal(data["e"], est_axis)
    assert data.data.shape == (3, 4)
    assert np.array_equal(data.data, np.asarray([
        [2, 4, 6],
        [4, 10, 16],
        [6, 16, 26],
        [8, 22, 36],
    ]).T)
예제 #12
0
def test_single_dataset():
    model = MockModel.from_dict({
        "dataset": {
            "dataset1": {
                "megacomplex": [],
            },
        }
    })
    print(model.validate())
    assert model.valid()

    parameter = ParameterGroup.from_list([1, 10])
    print(model.validate(parameter))
    assert model.valid(parameter)

    data = {'dataset1': xr.DataArray(
        np.ones((3, 4)),
        coords=[('e', [1, 2, 3]), ('c', [5, 7, 9, 12])]
    ).to_dataset(name="data")}

    group = create_group(model, data)
    assert len(group) == 3
    assert [item[0][0] for _, item in group.items()] == [1, 2, 3]
    assert all([item[0][1].label == 'dataset1' for _, item in group.items()])

    result = [calculate_group_item(item, model, parameter, data) for item in group.values()]
    assert len(result) == 3
    print(result[0])
    assert result[0][1].shape == (4, 2)

    data = create_data_group(model, group, data)
    assert len(data) == 3
    assert data[1].shape[0] == 4
예제 #13
0
def test_multi_dataset_overlap():
    model = MockModel.from_dict({
        "dataset": {
            "dataset1": {
                "megacomplex": [],
            },
            "dataset2": {
                "megacomplex": [],
            },
        }
    })

    model.grouped = lambda: True
    print(model.validate())
    assert model.valid()
    assert model.grouped()

    parameters = ParameterGroup.from_list([1, 10])
    print(model.validate(parameters))
    assert model.valid(parameters)

    axis_e_1 = [1, 2, 3, 5]
    axis_c_1 = [5, 7]
    axis_e_2 = [0, 1.4, 2.4, 3.4, 9]
    axis_c_2 = [5, 7, 9, 12]
    data = {
        "dataset1":
        xr.DataArray(np.ones((4, 2)),
                     coords=[("e", axis_e_1),
                             ("c", axis_c_1)]).to_dataset(name="data"),
        "dataset2":
        xr.DataArray(np.ones((5, 4)),
                     coords=[("e", axis_e_2),
                             ("c", axis_c_2)]).to_dataset(name="data"),
    }

    scheme = Scheme(model, parameters, data, group_tolerance=5e-1)
    problem = Problem(scheme)
    bag = list(problem.bag)
    assert len(problem.groups) == 3
    assert "dataset1dataset2" in problem.groups
    assert problem.groups["dataset1dataset2"] == ["dataset1", "dataset2"]
    assert len(bag) == 6

    assert all(p.data.size == 4 for p in bag[:1])
    assert all(p.descriptor[0].label == "dataset1" for p in bag[1:5])
    assert all(all(p.descriptor[0].axis == axis_c_1) for p in bag[1:5])
    assert [p.descriptor[0].index for p in bag[1:5]] == axis_e_1

    assert all(p.data.size == 6 for p in bag[1:4])
    assert all(p.descriptor[1].label == "dataset2" for p in bag[1:4])
    assert all(all(p.descriptor[1].axis == axis_c_2) for p in bag[1:4])
    assert [p.descriptor[1].index for p in bag[1:4]] == axis_e_2[1:4]

    assert all(p.data.size == 4 for p in bag[5:])
    assert bag[4].descriptor[0].label == "dataset1"
    assert bag[5].descriptor[0].label == "dataset2"
    assert np.array_equal(bag[4].descriptor[0].axis, axis_c_1)
    assert np.array_equal(bag[5].descriptor[0].axis, axis_c_2)
    assert [p.descriptor[0].index for p in bag[1:4]] == axis_e_1[:-1]
예제 #14
0
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)
예제 #15
0
def test_simulate_dataset():
    model = SimpleTestModel.from_dict(
        {"dataset": {
            "dataset1": {
                "megacomplex": [],
            },
        }})
    print(model.validate())
    assert model.valid()

    parameter = ParameterGroup.from_list([1, 1])
    print(model.validate(parameter))
    assert model.valid(parameter)

    est_axis = np.asarray([1, 1, 1, 1])
    cal_axis = np.asarray([2, 2, 2])

    data = simulate(model, "dataset1", parameter, {
        "e": est_axis,
        "c": cal_axis
    })
    assert np.array_equal(data["c"], cal_axis)
    assert np.array_equal(data["e"], est_axis)
    assert data.data.shape == (3, 4)
    assert np.array_equal(
        data.data,
        np.asarray([
            [2, 4, 6],
            [4, 10, 16],
            [6, 16, 26],
            [8, 22, 36],
        ]).T,
    )
예제 #16
0
def test_single_dataset():
    model = MockModel.from_dict(
        {"dataset": {
            "dataset1": {
                "megacomplex": [],
            },
        }})
    print(model.validate())
    assert model.valid()

    parameter = ParameterGroup.from_list([1, 10])
    print(model.validate(parameter))
    assert model.valid(parameter)
    axis_e = [1, 2, 3]
    axis_c = [5, 7, 9, 12]

    data = {
        'dataset1':
        xr.DataArray(np.ones((3, 4)),
                     coords=[('e', axis_e),
                             ('c', axis_c)]).to_dataset(name="data")
    }

    scheme = Scheme(model, parameter, data)
    bag, datasets = create_grouped_bag(scheme)
    bag = bag.compute()
    assert len(datasets) == 0
    assert len(bag) == 3
    assert all([p.data.size == 4 for p in bag])
    assert all([p.descriptor[0].dataset == 'dataset1' for p in bag])
    assert all([all(p.descriptor[0].axis == axis_c) for p in bag])
    assert [p.descriptor[0].index for p in bag] == axis_e
예제 #17
0
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)
예제 #18
0
def test_coherent_artifact():
    model = KineticModel.from_dict({
        'initial_concentration': {
            'j1': {
                'compartments': ['s1'],
                'parameters': ['2']
            },
        },
        'megacomplex': {
            'mc1': {
                'k_matrix': ['k1']
            },
        },
        'k_matrix': {
            "k1": {
                'matrix': {
                    ("s1", "s1"): '1',
                }
            }
        },
        'irf': {
            'irf1': {
                'type': 'gaussian',
                'center': '2',
                'width': '3',
                'coherent_artifact': True,
                'coherent_artifact_order': 3,
            },
        },
        'dataset': {
            'dataset1': {
                'initial_concentration': 'j1',
                'megacomplex': ['mc1'],
                'irf': 'irf1',
            },
        },
    })

    parameter = ParameterGroup.from_list([
        101e-4,
        [10, {
            'vary': False,
            'non-negative': False
        }],
        [20, {
            'vary': False,
            'non-negative': False
        }],
    ])

    time = np.asarray(np.arange(0, 50, 1.5))
    dataset = model.dataset['dataset1'].fill(model, parameter)
    compartments, matrix = calculate_kinetic_matrix(dataset, 0, time)

    assert len(compartments) == 4
    for i in range(1, 4):
        assert compartments[i] == f'irf1_coherent_artifact_{i}'

    assert matrix.shape == (time.size, 4)
예제 #19
0
class TwoCompartmentDecay:
    wanted_parameters = ParameterGroup.from_list([11e-4, 22e-5])
    initial_parameters = ParameterGroup.from_list([10e-4, 20e-5])

    e_axis = np.asarray([1.0])
    c_axis = np.arange(0, 150, 1.5)

    model = DecayModel.from_dict({
        "dataset": {
            "dataset1": {
                "initial_concentration": [],
                "megacomplex": [],
                "kinetic": ["1", "2"]
            }
        },
    })
    sim_model = model
def test_k_matrices(model):
    assert "km1" in model.k_matrix
    parameter = ParameterGroup.from_list([1, 2, 3, 4, 5, 6, 7])
    reduced = model.k_matrix["km1"].fill(model, parameter).reduced(
        ["s1", "s2", "s3", "s4"])
    assert np.array_equal(
        reduced,
        np.asarray([[1, 3, 5, 7], [2, 0, 0, 0], [4, 0, 0, 0], [6, 0, 0, 0]]))
예제 #21
0
def test_multi_dataset_overlap():
    model = MockModel.from_dict(
        {
            "dataset": {
                "dataset1": {
                    "megacomplex": [],
                },
                "dataset2": {
                    "megacomplex": [],
                },
            }
        }
    )

    print(model.validate())
    assert model.valid()

    parameter = ParameterGroup.from_list([1, 10])
    print(model.validate(parameter))
    assert model.valid(parameter)

    axis_e_1 = [1, 2, 3, 5]
    axis_c_1 = [5, 7]
    axis_e_2 = [0, 1.4, 2.4, 3.4, 9]
    axis_c_2 = [5, 7, 9, 12]
    data = {
        "dataset1": xr.DataArray(
            np.ones((4, 2)), coords=[("e", axis_e_1), ("c", axis_c_1)]
        ).to_dataset(name="data"),
        "dataset2": xr.DataArray(
            np.ones((5, 4)), coords=[("e", axis_e_2), ("c", axis_c_2)]
        ).to_dataset(name="data"),
    }

    scheme = Scheme(model, parameter, data, group_tolerance=5e-1)
    bag, datasets = create_grouped_bag(scheme)
    bag = bag.compute()
    assert len(datasets) == 1
    assert "dataset1dataset2" in datasets
    assert datasets["dataset1dataset2"] == ["dataset1", "dataset2"]
    assert len(bag) == 6

    assert all([p.data.size == 4 for p in bag[:1]])
    assert all([p.descriptor[0].dataset == "dataset1" for p in bag[1:5]])
    assert all([all(p.descriptor[0].axis == axis_c_1) for p in bag[1:5]])
    assert [p.descriptor[0].index for p in bag[1:5]] == axis_e_1

    assert all([p.data.size == 6 for p in bag[1:4]])
    assert all([p.descriptor[1].dataset == "dataset2" for p in bag[1:4]])
    assert all([all(p.descriptor[1].axis == axis_c_2) for p in bag[1:4]])
    assert [p.descriptor[1].index for p in bag[1:4]] == axis_e_2[1:4]

    assert all([p.data.size == 4 for p in bag[5:]])
    assert bag[4].descriptor[0].dataset == "dataset1"
    assert bag[5].descriptor[0].dataset == "dataset2"
    assert np.array_equal(bag[4].descriptor[0].axis, axis_c_1)
    assert np.array_equal(bag[5].descriptor[0].axis, axis_c_2)
    assert [p.descriptor[0].index for p in bag[1:4]] == axis_e_1[:-1]
예제 #22
0
class OneCompartmentDecay:
    wanted = ParameterGroup.from_list([101e-4])
    initial = ParameterGroup.from_list([100e-5])

    e_axis = np.asarray([1])
    c_axis = np.arange(0, 150, 1.5)

    model = DecayModel.from_dict({
        'compartment': ["s1"],
        'dataset': {
            'dataset1': {
                'initial_concentration': [],
                'megacomplex': [],
                'kinetic': ['1']
            }
        }
    })
    sim_model = model
예제 #23
0
class OneCompartmentDecay:
    wanted = ParameterGroup.from_list([101e-4])
    initial = ParameterGroup.from_list([100e-5])

    e_axis = np.asarray([1])
    c_axis = np.arange(0, 150, 1.5)

    model = DecayModel.from_dict({
        "compartment": ["s1"],
        "dataset": {
            "dataset1": {
                "initial_concentration": [],
                "megacomplex": [],
                "kinetic": ["1"]
            }
        },
    })
    sim_model = model
예제 #24
0
def test_multi_dataset_no_overlap():
    model = MockModel.from_dict({
        "dataset": {
            "dataset1": {
                "megacomplex": [],
            },
            "dataset2": {
                "megacomplex": [],
            },
        }
    })

    print(model.validate())
    assert model.valid()

    parameter = ParameterGroup.from_list([1, 10])
    print(model.validate(parameter))
    assert model.valid(parameter)

    data = {
        'dataset1': xr.DataArray(
            np.ones((3, 2)),
            coords=[('e', [1, 2, 3]), ('c', [5, 7])]
        ).to_dataset(name="data"),
        'dataset2': xr.DataArray(
            np.ones((3, 3)),
            coords=[('e', [4, 5, 6]), ('c', [5, 7, 9])]
        ).to_dataset(name="data"),
    }

    group = create_group(model, data)
    assert len(group) == 6
    assert [item[0][0] for _, item in group.items()] == [1, 2, 3, 4, 5, 6]
    assert [item[0][1].label for _, item in group.items()] == \
        ['dataset1' for _ in range(3)] + ['dataset2' for _ in range(3)]

    result = [calculate_group_item(item, model, parameter, data) for item in group.values()]
    assert len(result) == 6
    print(result[0])
    assert result[0][1].shape == (2, 2)
    assert result[3][1].shape == (3, 2)

    data = create_data_group(model, group, data)
    assert len(data) == 6
    assert data[1].shape[0] == 2
    assert data[4].shape[0] == 3

    data = {
        'dataset1': xr.DataArray(
            np.ones((3, 2)),
            coords=[('e', [1, 2, 3]), ('c', [5, 7])]
        ).to_dataset(name="data"),
        'dataset2': xr.DataArray(
            np.ones((3, 3)),
            coords=[('e', [4, 5, 6]), ('c', [5, 7, 9])]
        ).to_dataset(name="data"),
    }
예제 #25
0
def test_a_matrix_sequential():

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

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

    initial_concentration = [1, 0, 0]

    assert not mat.is_sequential(compartments, initial_concentration)

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

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

    initial_concentration = [1, 0]

    print(mat.reduced(compartments))
    assert mat.is_sequential(compartments, initial_concentration)

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

    print(mat.a_matrix_sequential(compartments))
    assert np.allclose(mat.a_matrix_sequential(compartments), wanted_a_matrix)
예제 #26
0
class OneComponentOneChannel:
    model = KineticImageModel.from_dict({
        "initial_concentration": {
            "j1": {
                "compartments": ["s1"],
                "parameters": ["2"]
            },
        },
        "megacomplex": {
            "mc1": {
                "k_matrix": ["k1"]
            },
        },
        "k_matrix": {
            "k1": {
                "matrix": {
                    ("s1", "s1"): "1",
                }
            }
        },
        "dataset": {
            "dataset1": {
                "initial_concentration": "j1",
                "megacomplex": ["mc1"],
            },
        },
    })

    initial = ParameterGroup.from_list(
        [101e-4, [1, {
            "vary": False,
            "non-negative": False
        }]])
    wanted = ParameterGroup.from_list(
        [101e-3, [1, {
            "vary": False,
            "non-negative": False
        }]])

    time = np.asarray(np.arange(0, 50, 1.5))
    axis = {"time": time, "pixel": np.asarray([0])}

    clp = xr.DataArray([[1]], coords=[("pixel", [0]), ("clp_label", ["s1"])])
예제 #27
0
def parameter():
    params = [1, 2,
              ['foo', 3],
              ['bar', 4],
              ['baz', 2],
              ['scale_1', 2],
              ['scale_2', 8],
              4e2
              ]
    return ParameterGroup.from_list(params)
예제 #28
0
def test_multi_dataset_overlap():
    model = MockModel.from_dict({
        "dataset": {
            "dataset1": {
                "megacomplex": [],
            },
            "dataset2": {
                "megacomplex": [],
            },
        }
    })

    print(model.validate())
    assert model.valid()

    parameter = ParameterGroup.from_list([1, 10])
    print(model.validate(parameter))
    assert model.valid(parameter)

    data = {
        'dataset1': xr.DataArray(
            np.ones((4, 2)),
            coords=[('e', [0, 1, 2, 3]), ('c', [5, 7])]
        ).to_dataset(name="data"),
        'dataset2': xr.DataArray(
            np.ones((4, 4)),
            coords=[('e', [1.4, 2.4, 3.4, 9]), ('c', [5, 7, 9, 12])]
        ).to_dataset(name="data"),
    }

    scheme = Scheme(model, parameter, data, group_tolerance=5e-1)
    optimizer = Optimizer(scheme)
    group = optimizer._global_problem
    assert len(group) == 5
    assert group[0][0][1].label == 'dataset1'
    assert group[1][0][1].label == 'dataset1'
    assert group[1][1][1].label == 'dataset2'
    assert group[9][0][1].label == 'dataset2'

    optimizer._create_calculate_penalty_job(parameter)
    print(optimizer.matrices)
    result = [m.compute() for m in optimizer.full_matrices.values()]
    assert len(result) == 5
    print(result[0])
    print(result[1])
    assert result[0].shape == (2, 2)
    assert result[1].shape == (6, 2)
    assert result[4].shape == (4, 2)

    data = [d.compute() for d in optimizer._global_data.values()]
    assert len(data) == 5
    assert data[0].shape[0] == 2
    assert data[1].shape[0] == 6
    assert data[4].shape[0] == 4
예제 #29
0
class OneComponentOneChannelMeasuredIrf:
    model = KineticImageModel.from_dict({
        'initial_concentration': {
            'j1': {
                'compartments': ['s1'],
                'parameters': ['2']
            },
        },
        'megacomplex': {
            'mc1': {'k_matrix': ['k1']},
        },
        'k_matrix': {
            "k1": {'matrix': {("s1", "s1"): '1', }}
        },
        'irf': {
            'irf1': {'type': 'measured'},
        },
        'dataset': {
            'dataset1': {
                'initial_concentration': 'j1',
                'irf': 'irf1',
                'megacomplex': ['mc1'],
            },
        },
    })

    initial = ParameterGroup.from_list([101e-4, [1, {'vary': False, 'non-negative': False}]])
    wanted = ParameterGroup.from_list([101e-3, [1, {'vary': False, 'non-negative': False}]])

    time = np.asarray(np.arange(-10, 50, 1.5))
    axis = {"time": time, "pixel": np.asarray([0])}

    center = 0
    width = 5
    irf = (1/np.sqrt(2 * np.pi)) * np.exp(-(time-center) * (time-center)
                                          / (2 * width * width))
    model.irf["irf1"].irfdata = irf

    clp = xr.DataArray([[1]], coords=[('pixel', [0]), ('clp_label', ['s1'])])
예제 #30
0
def test_multi_dataset_no_overlap():
    model = MockModel.from_dict({
        "dataset": {
            "dataset1": {
                "megacomplex": [],
            },
            "dataset2": {
                "megacomplex": [],
            },
        }
    })

    model.grouped = lambda: True
    print(model.validate())
    assert model.valid()
    assert model.grouped()

    parameters = ParameterGroup.from_list([1, 10])
    print(model.validate(parameters))
    assert model.valid(parameters)

    axis_e_1 = [1, 2, 3]
    axis_c_1 = [5, 7]
    axis_e_2 = [4, 5, 6]
    axis_c_2 = [5, 7, 9]
    data = {
        "dataset1":
        xr.DataArray(np.ones((3, 2)),
                     coords=[("e", axis_e_1),
                             ("c", axis_c_1)]).to_dataset(name="data"),
        "dataset2":
        xr.DataArray(np.ones((3, 3)),
                     coords=[("e", axis_e_2),
                             ("c", axis_c_2)]).to_dataset(name="data"),
    }

    scheme = Scheme(model, parameters, data)
    problem = Problem(scheme)
    bag = list(problem.bag)
    assert len(problem.groups) == 2
    assert len(bag) == 6
    assert all(p.data.size == 2 for p in bag[:3])
    assert all(p.descriptor[0].label == "dataset1" for p in bag[:3])
    assert all(all(p.descriptor[0].axis == axis_c_1) for p in bag[:3])
    assert [p.descriptor[0].index for p in bag[:3]] == axis_e_1

    assert all(p.data.size == 3 for p in bag[3:])
    assert all(p.descriptor[0].label == "dataset2" for p in bag[3:])
    assert all(all(p.descriptor[0].axis == axis_c_2) for p in bag[3:])
    assert [p.descriptor[0].index for p in bag[3:]] == axis_e_2
예제 #31
0
def test_multi_dataset_overlap():
    model = MockModel.from_dict({
        "dataset": {
            "dataset1": {
                "megacomplex": [],
            },
            "dataset2": {
                "megacomplex": [],
            },
        }
    })

    print(model.validate())
    assert model.valid()

    parameter = ParameterGroup.from_list([1, 10])
    print(model.validate(parameter))
    assert model.valid(parameter)

    data = {
        'dataset1': xr.DataArray(
            np.ones((4, 2)),
            coords=[('e', [0, 1, 2, 3]), ('c', [5, 7])]
        ).to_dataset(name="data"),
        'dataset2': xr.DataArray(
            np.ones((4, 4)),
            coords=[('e', [1.4, 2.4, 3.4, 9]), ('c', [5, 7, 9, 12])]
        ).to_dataset(name="data"),
    }

    group = create_group(model, data, atol=5e-1)
    assert len(group) == 5
    assert group[0][0][1].label == 'dataset1'
    assert group[1][0][1].label == 'dataset1'
    assert group[1][1][1].label == 'dataset2'
    assert group[9][0][1].label == 'dataset2'

    result = [calculate_group_item(item, model, parameter, data) for item in group.values()]
    assert len(result) == 5
    print(result[0])
    print(result[1])
    assert result[0][1].shape == (2, 2)
    assert result[1][1].shape == (6, 2)
    assert result[4][1].shape == (4, 2)

    data = create_data_group(model, group, data)
    assert len(data) == 5
    assert data[0].shape[0] == 2
    assert data[1].shape[0] == 6
    assert data[9].shape[0] == 4
예제 #32
0
def test_baseline():
    model = KineticImageModel.from_dict({
        'initial_concentration': {
            'j1': {
                'compartments': ['s1'],
                'parameters': ['2']
            },
        },
        'megacomplex': {
            'mc1': {
                'k_matrix': ['k1']
            },
        },
        'k_matrix': {
            "k1": {
                'matrix': {
                    ("s1", "s1"): '1',
                }
            }
        },
        'dataset': {
            'dataset1': {
                'initial_concentration': 'j1',
                'megacomplex': ['mc1'],
                'baseline': True,
            },
        },
    })

    parameter = ParameterGroup.from_list([
        101e-4,
        [1, {
            'vary': False,
            'non-negative': False
        }],
        [42, {
            'vary': False,
            'non-negative': False
        }],
    ])

    time = np.asarray(np.arange(0, 50, 1.5))
    dataset = model.dataset['dataset1'].fill(model, parameter)
    compartments, matrix = kinetic_image_matrix(dataset, time, 0)

    assert len(compartments) == 2
    assert compartments[1] == 'dataset1_baseline'

    assert matrix.shape == (time.size, 2)
    assert np.all(matrix[:, 1] == 1)
예제 #33
0
class OneCompartmentDecay:
    scale = 2
    wanted_parameters = ParameterGroup.from_list([101e-4])
    initial_parameters = ParameterGroup.from_list(
        [100e-5, [scale, {
            "vary": False
        }]])

    e_axis = np.asarray([1.0])
    c_axis = np.arange(0, 150, 1.5)

    model_dict = {
        "dataset": {
            "dataset1": {
                "initial_concentration": [],
                "megacomplex": [],
                "kinetic": ["1"]
            }
        },
    }
    sim_model = DecayModel.from_dict(model_dict)
    model_dict["dataset"]["dataset1"]["scale"] = "2"
    model = DecayModel.from_dict(model_dict)
예제 #34
0
def test_single_dataset():
    model = SimpleTestModel.from_dict({
        "megacomplex": {
            "m1": {
                "is_index_dependent": False
            }
        },
        "dataset_groups": {
            "default": {
                "link_clp": True
            }
        },
        "dataset": {
            "dataset1": {
                "megacomplex": ["m1"],
            },
        },
    })
    print(model.validate())
    assert model.valid()

    parameters = ParameterGroup.from_list([1, 10])
    print(model.validate(parameters))
    assert model.valid(parameters)
    global_axis = [1, 2, 3]
    model_axis = [5, 7, 9, 12]

    data = {
        "dataset1":
        xr.DataArray(np.ones((3, 4)),
                     coords=[("global", global_axis),
                             ("model", model_axis)]).to_dataset(name="data")
    }

    scheme = Scheme(model, parameters, data)
    optimization_group = OptimizationGroup(
        scheme,
        model.get_dataset_groups()["default"])
    bag = optimization_group._calculator.bag
    datasets = optimization_group._calculator.groups
    assert len(datasets) == 1
    assert len(bag) == 3
    assert all(p.data.size == 4 for p in bag)
    assert all(p.dataset_models[0].label == "dataset1" for p in bag)
    assert all(
        all(p.dataset_models[0].axis["model"] == model_axis) for p in bag)
    assert all(
        all(p.dataset_models[0].axis["global"] == global_axis) for p in bag)
    assert [p.dataset_models[0].indices["global"] for p in bag] == [0, 1, 2]
예제 #35
0
def test_multi_dataset_no_overlap():
    model = MockModel.from_dict({
        "dataset": {
            "dataset1": {
                "megacomplex": [],
            },
            "dataset2": {
                "megacomplex": [],
            },
        }
    })

    print(model.validate())
    assert model.valid()

    parameter = ParameterGroup.from_list([1, 10])
    print(model.validate(parameter))
    assert model.valid(parameter)

    data = {
        'dataset1': xr.DataArray(
            np.ones((3, 2)),
            coords=[('e', [1, 2, 3]), ('c', [5, 7])]
        ).to_dataset(name="data"),
        'dataset2': xr.DataArray(
            np.ones((3, 3)),
            coords=[('e', [4, 5, 6]), ('c', [5, 7, 9])]
        ).to_dataset(name="data"),
    }

    scheme = Scheme(model, parameter, data)
    optimizer = Optimizer(scheme)
    group = optimizer._global_problem
    assert len(group) == 6
    assert [problem[0][0] for problem in group.values()] == [1, 2, 3, 4, 5, 6]
    assert [problem[0][1].label for problem in group.values()] == \
        ['dataset1' for _ in range(3)] + ['dataset2' for _ in range(3)]

    optimizer._create_calculate_penalty_job(parameter)
    result = [m.compute() for mat in optimizer.matrices.values() for m in mat]
    assert len(result) == 6
    print(result[0])
    assert result[0].shape == (2, 2)
    assert result[3].shape == (3, 2)

    data = optimizer._global_data
    assert len(data) == 6
    assert list(data.values())[1].compute().shape[0] == 2
    assert list(data.values())[4].compute().shape[0] == 3
예제 #36
0
def test_multi_dataset_no_overlap():
    model = MockModel.from_dict({
        "dataset": {
            "dataset1": {
                "megacomplex": [],
            },
            "dataset2": {
                "megacomplex": [],
            },
        }
    })

    print(model.validate())
    assert model.valid()

    parameter = ParameterGroup.from_list([1, 10])
    print(model.validate(parameter))
    assert model.valid(parameter)

    axis_e_1 = [1, 2, 3]
    axis_c_1 = [5, 7]
    axis_e_2 = [4, 5, 6]
    axis_c_2 = [5, 7, 9]
    data = {
        'dataset1':
        xr.DataArray(np.ones((3, 2)),
                     coords=[('e', axis_e_1),
                             ('c', axis_c_1)]).to_dataset(name="data"),
        'dataset2':
        xr.DataArray(np.ones((3, 3)),
                     coords=[('e', axis_e_2),
                             ('c', axis_c_2)]).to_dataset(name="data"),
    }

    scheme = Scheme(model, parameter, data)
    bag, datasets = create_grouped_bag(scheme)
    bag = bag.compute()
    assert len(datasets) == 0
    assert len(bag) == 6
    assert all([p.data.size == 2 for p in bag[:3]])
    assert all([p.descriptor[0].dataset == 'dataset1' for p in bag[:3]])
    assert all([all(p.descriptor[0].axis == axis_c_1) for p in bag[:3]])
    assert [p.descriptor[0].index for p in bag[:3]] == axis_e_1

    assert all([p.data.size == 3 for p in bag[3:]])
    assert all([p.descriptor[0].dataset == 'dataset2' for p in bag[3:]])
    assert all([all(p.descriptor[0].axis == axis_c_2) for p in bag[3:]])
    assert [p.descriptor[0].index for p in bag[3:]] == axis_e_2
예제 #37
0
def test_coherent_artifact():
    model = KineticModel.from_dict({
        'initial_concentration': {
            'j1': {
                'compartments': ['s1'],
                'parameters': ['2']
            },

        },
        'megacomplex': {
            'mc1': {'k_matrix': ['k1']},
        },
        'k_matrix': {
            "k1": {'matrix': {("s1", "s1"): '1', }}
        },
        'irf': {
            'irf1': {
                'type': 'gaussian',
                'center': '2',
                'width': '3',
                'coherent_artifact': True,
                'coherent_artifact_order': 3,
            },
        },
        'dataset': {
            'dataset1': {
                'initial_concentration': 'j1',
                'megacomplex': ['mc1'],
                'irf': 'irf1',
            },
        },
    })

    parameter = ParameterGroup.from_list([
        101e-4,
        [10, {'vary': False, 'non-negative': False}],
        [20, {'vary': False, 'non-negative': False}],
    ])

    time = np.asarray(np.arange(0, 50, 1.5))
    dataset = model.dataset['dataset1'].fill(model, parameter)
    compartments, matrix = calculate_kinetic_matrix(dataset, time, 0)

    assert len(compartments) == 4
    for i in range(1, 4):
        assert compartments[i] == f'irf1_coherent_artifact_{i}'

    assert matrix.shape == (time.size, 4)
예제 #38
0
def test_baseline():
    model = KineticModel.from_dict({
        'initial_concentration': {
            'j1': {
                'compartments': ['s1'],
                'parameters': ['2']
            },

        },
        'megacomplex': {
            'mc1': {'k_matrix': ['k1']},
        },
        'k_matrix': {
            "k1": {'matrix': {("s1", "s1"): '1', }}
        },
        'dataset': {
            'dataset1': {
                'initial_concentration': 'j1',
                'megacomplex': ['mc1'],
                'baseline': True,
            },
        },
    })

    parameter = ParameterGroup.from_list([
        101e-4,
        [1, {'vary': False, 'non-negative': False}],
        [42, {'vary': False, 'non-negative': False}],
    ])

    time = np.asarray(np.arange(0, 50, 1.5))
    dataset = model.dataset['dataset1'].fill(model, parameter)
    compartments, matrix = calculate_kinetic_matrix(dataset, time, 0)

    assert len(compartments) == 2
    assert compartments[1] == 'dataset1_baseline'

    assert matrix.shape == (time.size, 2)
    assert np.all(matrix[:, 1] == 1)
예제 #39
0
def test_single_dataset():
    model = MockModel.from_dict({
        "dataset": {
            "dataset1": {
                "megacomplex": [],
            },
        }
    })
    print(model.validate())
    assert model.valid()

    parameter = ParameterGroup.from_list([1, 10])
    print(model.validate(parameter))
    assert model.valid(parameter)

    data = {'dataset1': xr.DataArray(
        np.ones((3, 4)),
        coords=[('e', [1, 2, 3]), ('c', [5, 7, 9, 12])]
    ).to_dataset(name="data")}

    scheme = Scheme(model, parameter, data)
    optimizer = Optimizer(scheme)
    group = optimizer._global_problem
    assert len(group) == 3
    assert list(group.keys()) == [f"dataset1_{i}" for i in [1, 2, 3]]
    assert all([p.dataset_descriptor.label == 'dataset1' for p in group.values()])

    optimizer._create_calculate_penalty_job(parameter)
    result = [m.compute() for m in optimizer.matrices.values()]
    assert len(result) == 3
    print(result[0])
    assert result[0].shape == (4, 2)

    data = optimizer._global_data
    assert len(data) == 3
    assert list(data.values())[1].compute().shape[0] == 4