def test_equal_area_penalties(debug=False): # %% optim_spec = OptimizationSpec(nnls=True, max_nfev=999) noise_spec = NoiseSpec(active=True, seed=1, std_dev=1e-8) wavelengths = np.arange(650, 670, 2) time_p1 = np.linspace(-1, 2, 50, endpoint=False) time_p2 = np.linspace(2, 10, 30, endpoint=False) time_p3 = np.geomspace(10, 50, num=20) times = np.concatenate([time_p1, time_p2, time_p3]) irf_loc = float(times[20]) irf_width = float((times[1] - times[0]) * 10) irf = IrfSpec(irf_loc, irf_width) amplitude = 1 location1 = float(wavelengths[2]) # 2 location2 = float(wavelengths[-3]) # -3 width1 = float((wavelengths[1] - wavelengths[0]) * 5) width2 = float((wavelengths[1] - wavelengths[0]) * 3) shape1 = ShapeSpec(amplitude, location1, width1) shape2 = ShapeSpec(amplitude, location2, width2) dataset_spec = DatasetSpec(times, wavelengths, irf, [shape1, shape2]) wavelengths = dataset_spec.wavelengths equ_interval = [(min(wavelengths), max(wavelengths))] weight = 0.01 # %% The base model specification (mspec) base = { "initial_concentration": { "j1": { "compartments": ["s1", "s2"], "parameters": ["i.1", "i.2"], }, }, "megacomplex": { "mc1": { "k_matrix": ["k1"] }, }, "k_matrix": { "k1": { "matrix": { ("s1", "s1"): "kinetic.1", ("s2", "s2"): "kinetic.2", } } }, "irf": { "irf1": { "type": "gaussian", "center": "irf.center", "width": "irf.width" }, }, "dataset": { "dataset1": { "initial_concentration": "j1", "megacomplex": ["mc1"], "irf": "irf1", }, }, } shape = { "shape": { "sh1": { "type": "gaussian", "amplitude": "shapes.amps.1", "location": "shapes.locs.1", "width": "shapes.width.1", }, "sh2": { "type": "gaussian", "amplitude": "shapes.amps.2", "location": "shapes.locs.2", "width": "shapes.width.2", }, } } dataset_shape = { "shape": { "s1": "sh1", "s2": "sh2", } } equ_area = { "equal_area_penalties": [ { "source": "s1", "target": "s2", "parameter": "rela.1", "source_intervals": equ_interval, "target_intervals": equ_interval, "weight": weight, }, ], } mspec = ModelSpec(base, shape, dataset_shape, equ_area) rela = 1.0 # relation between areas irf = dataset_spec.irf [sh1, sh2] = dataset_spec.shapes pspec_base = { "kinetic": [1e-1, 5e-3], "i": [0.5, 0.5, { "vary": False }], "irf": [["center", irf.location], ["width", irf.width]], } pspec_equa_area = { "rela": [rela, { "vary": False }], } pspec_shape = { "shapes": { "amps": [sh1.amplitude, sh2.amplitude], "locs": [sh1.location, sh2.location], "width": [sh1.width, sh2.width], }, } pspec = ParameterSpec(pspec_base, pspec_equa_area, pspec_shape) # derivates: mspec_sim = dict(deepcopy(mspec.base), **mspec.shape) mspec_sim["dataset"]["dataset1"].update(mspec.dataset_shape) mspec_fit_wp = dict(deepcopy(mspec.base), **mspec.equ_area) mspec_fit_np = dict(deepcopy(mspec.base)) model_sim = KineticSpectrumModel.from_dict(mspec_sim) model_wp = KineticSpectrumModel.from_dict(mspec_fit_wp) model_np = KineticSpectrumModel.from_dict(mspec_fit_np) print(model_np) # %% Parameter specification (pspec) pspec_sim = dict(deepcopy(pspec.base), **pspec.shapes) param_sim = ParameterGroup.from_dict(pspec_sim) # For the wp model we create two version of the parameter specification # One has all inputs fixed, the other has all but the first free # for both we perturb kinetic parameters a bit to give the optimizer some work pspec_wp = dict(deepcopy(pspec.base), **pspec.equal_area) pspec_wp["kinetic"] = [v * 1.01 for v in pspec_wp["kinetic"]] pspec_wp.update({"i": [[1, {"vary": False}], 1]}) pspec_np = dict(deepcopy(pspec.base)) param_wp = ParameterGroup.from_dict(pspec_wp) param_np = ParameterGroup.from_dict(pspec_np) # %% Print models with parameters print(model_sim.markdown(param_sim)) print(model_wp.markdown(param_wp)) print(model_np.markdown(param_np)) # %% simulated_data = model_sim.simulate( "dataset1", param_sim, axes={ "time": times, "spectral": wavelengths }, noise=noise_spec.active, noise_std_dev=noise_spec.std_dev, noise_seed=noise_spec.seed, ) # %% simulated_data = prepare_time_trace_dataset(simulated_data) # make a copy to keep an intact reference data = deepcopy(simulated_data) # %% Optimizing model without penalty (np) dataset = {"dataset1": data} scheme_np = Scheme( model=model_np, parameters=param_np, data=dataset, nnls=optim_spec.nnls, nfev=optim_spec.max_nfev, ) result_np = optimize(scheme_np) print(result_np) # %% Optimizing model with penalty fixed inputs (wp_ifix) scheme_wp = Scheme( model=model_wp, parameters=param_wp, data=dataset, nnls=optim_spec.nnls, nfev=optim_spec.max_nfev, ) result_wp = optimize(scheme_wp) print(result_wp) if debug: # %% Plot results plt_spec = importlib.util.find_spec("matplotlib") if plt_spec is not None: import matplotlib.pyplot as plt plot_overview(result_np.data["dataset1"], "no penalties") plot_overview(result_wp.data["dataset1"], "with penalties") plt.show() # %% Test calculation print(result_wp.data["dataset1"]) area1_np = np.sum( result_np.data["dataset1"].species_associated_spectra.sel( species="s1")) area2_np = np.sum( result_np.data["dataset1"].species_associated_spectra.sel( species="s2")) assert not np.isclose(area1_np, area2_np) area1_wp = np.sum( result_wp.data["dataset1"].species_associated_spectra.sel( species="s1")) area2_wp = np.sum( result_wp.data["dataset1"].species_associated_spectra.sel( species="s2")) assert np.isclose(area1_wp, area2_wp) input_ratio = result_wp.optimized_parameters.get( "i.1") / result_wp.optimized_parameters.get("i.2") assert np.isclose(input_ratio, 1.5038858115)
def test_spectral_relation(): model = KineticSpectrumModel.from_dict({ 'initial_concentration': { 'j1': { 'compartments': ['s1', 's2', 's3', 's4'], 'parameters': ['i.1', 'i.2', 'i.3', 'i.4'] }, }, 'megacomplex': { 'mc1': { 'k_matrix': ['k1'] }, }, 'k_matrix': { "k1": { 'matrix': { ("s1", "s1"): 'kinetic.1', ("s2", "s2"): 'kinetic.1', ("s3", "s3"): 'kinetic.1', ("s4", "s4"): 'kinetic.1', } } }, 'spectral_relations': [ { 'compartment': 's1', 'target': 's2', 'parameter': 'rel.1', 'interval': [(0, 2)], }, { 'compartment': 's1', 'target': 's3', 'parameter': 'rel.2', 'interval': [(0, 2)], }, ], 'dataset': { 'dataset1': { 'initial_concentration': 'j1', 'megacomplex': ['mc1'], }, }, }) print(model) rel1, rel2 = 10, 20 parameter = ParameterGroup.from_dict({ 'kinetic': [1e-4], 'i': [1, 2, 3, 4], 'rel': [rel1, rel2], }) 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) == 4 assert matrix.shape == (time.size, 4) reduced_compartments, relation_matrix = \ create_spectral_relation_matrix(model, parameter, compartments, matrix, 1) print(relation_matrix) assert len(reduced_compartments) == 2 assert relation_matrix.shape == (4, 2) assert np.array_equal(relation_matrix, [ [1., 0.], [10., 0.], [20., 0.], [0., 1.], ]) reduced_compartments, reduced_matrix = \ model.constrain_matrix_function(parameter, compartments, matrix, 1) assert reduced_matrix.shape == (time.size, 2) print(reduced_matrix[0, 0], matrix[0, 0], matrix[0, 1], matrix[0, 2]) assert np.allclose( reduced_matrix[:, 0], matrix[:, 0] + rel1 * matrix[:, 1] + rel2 * matrix[:, 2]) clp = xr.DataArray([[1., 10., 20., 1]], coords=(('spectral', [1]), ('clp_label', ['s1', 's2', 's3', 's4']))) data = model.simulate('dataset1', parameter, clp=clp, axes={ 'time': time, 'spectral': np.array([1]) }) result = model.optimize(parameter, {'dataset1': data}, max_nfev=1) result_data = result.data['dataset1'] print(result_data.species_associated_spectra) assert result_data.species_associated_spectra.shape == (1, 4) assert result_data.species_associated_spectra[0, 1] == \ rel1 * result_data.species_associated_spectra[0, 0] assert result_data.species_associated_spectra[0, 2] == \ rel2 * result_data.species_associated_spectra[0, 0]
def test_spectral_penalties(): model_without_penalty = KineticSpectrumModel.from_dict({ "initial_concentration": { "j1": { "compartments": ["s1", "s2", "s3"], "parameters": ["i.1", "i.2", "i.3"] }, }, "megacomplex": { "mc1": { "k_matrix": ["k1"] }, }, "k_matrix": { "k1": { "matrix": { ("s1", "s1"): "kinetic.1", ("s2", "s2"): "kinetic.1", ("s3", "s3"): "kinetic.1", } } }, "spectral_relations": [ { "compartment": "s1", "target": "s2", "parameter": "rel.1", "interval": [(0, 2)], }, ], "dataset": { "dataset1": { "initial_concentration": "j1", "megacomplex": ["mc1"], }, }, }) weight = 0.1 model_with_penalty = KineticSpectrumModel.from_dict({ "initial_concentration": { "j1": { "compartments": ["s1", "s2", "s3"], "parameters": ["i.1", "i.2", "i.3"] }, }, "megacomplex": { "mc1": { "k_matrix": ["k1"] }, }, "k_matrix": { "k1": { "matrix": { ("s1", "s1"): "kinetic.1", ("s2", "s2"): "kinetic.1", ("s3", "s3"): "kinetic.1", } } }, "equal_area_penalties": [ { "compartment": "s2", "target": "s3", "parameter": "pen.1", "interval": [(0, 2)], "weight": weight, }, ], "spectral_relations": [ { "compartment": "s1", "target": "s2", "parameter": "rel.1", "interval": [(0, 2)], }, ], "dataset": { "dataset1": { "initial_concentration": "j1", "megacomplex": ["mc1"], }, }, }) print(model_with_penalty) rel1 = 2 pen = 0.5 parameter = ParameterGroup.from_dict({ "kinetic": [1e-4], "i": [1, 1, 1], "rel": [rel1], "pen": [pen], }) time = np.asarray(np.arange(0, 50, 1.5)) clp = xr.DataArray([[1.0, 2.0, 4]], coords=(("spectral", [1]), ("clp_label", ["s1", "s2", "s3"]))) data = model_without_penalty.simulate("dataset1", parameter, clp=clp, axes={ "time": time, "spectral": np.array([1]) }) result_with_penalty = model_with_penalty.optimize(parameter, {"dataset1": data}, max_nfev=1) parameter_no_penalty = deepcopy(parameter) del parameter_no_penalty["pen"] result_without_penalty = model_without_penalty.optimize( parameter_no_penalty, {"dataset1": data}, max_nfev=1) result_data = result_with_penalty.data["dataset1"] wanted_penalty = ( result_data.species_associated_spectra.sel(species="s2") - result_data.species_associated_spectra.sel(species="s3") * pen) wanted_penalty *= weight wanted_penalty **= 2 wanted_penalty = np.sum(wanted_penalty.values) additional_penalty = result_with_penalty.chisqr - result_without_penalty.chisqr assert np.isclose(additional_penalty, wanted_penalty)
def test_coherent_artifact(): model_dict = { "initial_concentration": { "j1": { "compartments": ["s1"], "parameters": ["2"] }, }, "megacomplex": { "mc1": { "k_matrix": ["k1"] }, }, "k_matrix": { "k1": { "matrix": { ("s1", "s1"): "1", } } }, "irf": { "irf1": { "type": "gaussian-coherent-artifact", "center": "2", "width": "3", "coherent_artifact_order": 3, }, }, "dataset": { "dataset1": { "initial_concentration": "j1", "megacomplex": ["mc1"], "irf": "irf1", }, }, } model = KineticSpectrumModel.from_dict(model_dict.copy()) parameters = ParameterGroup.from_list([ 101e-4, [10, { "vary": False, "non-negative": False }], [20, { "vary": False, "non-negative": False }], [30, { "vary": False, "non-negative": False }], ]) time = np.asarray(np.arange(0, 50, 1.5)) irf = model.irf["irf1"].fill(model, parameters) irf_same_width = irf.calculate_coherent_artifact(time) model_dict["irf"]["irf1"]["coherent_artifact_width"] = "4" model = KineticSpectrumModel.from_dict(model_dict) irf = model.irf["irf1"].fill(model, parameters) irf_diff_width = irf.calculate_coherent_artifact(time) assert np.array_equal(irf_same_width[0], irf_diff_width[0]) # labels the same assert not np.array_equal(irf_same_width[1], irf_diff_width[1]) # but content is not data = model.dataset["dataset1"].fill(model, parameters) compartments, matrix = kinetic_spectrum_matrix(data, time, 0) assert len(compartments) == 4 for i in range(1, 4): assert compartments[i] == f"coherent_artifact_{i}" assert matrix.shape == (time.size, 4) clp = xr.DataArray( [[1, 1, 1, 1]], coords=[ ("spectral", [0]), ( "clp_label", [ "s1", "coherent_artifact_1", "coherent_artifact_2", "coherent_artifact_3", ], ), ], ) axis = {"time": time, "spectral": clp.spectral} data = model.simulate("dataset1", parameters, axis, clp) dataset = {"dataset1": data} scheme = Scheme(model=model, parameters=parameters, data=dataset, maximum_number_function_evaluations=20) result = optimize(scheme) print(result.optimized_parameters) for label, param in result.optimized_parameters.all(): assert np.allclose(param.value, parameters.get(label).value, rtol=1e-1) resultdata = result.data["dataset1"] assert np.array_equal(data.time, resultdata.time) assert np.array_equal(data.spectral, resultdata.spectral) assert data.data.shape == resultdata.data.shape assert data.data.shape == resultdata.fitted_data.shape assert np.allclose(data.data, resultdata.fitted_data, rtol=1e-2) assert "coherent_artifact_concentration" in resultdata assert resultdata["coherent_artifact_concentration"].shape == (time.size, 3) assert "coherent_artifact_associated_spectra" in resultdata assert resultdata["coherent_artifact_associated_spectra"].shape == (1, 3)
class OneComponentOneChannel: model = KineticSpectrumModel.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"], }, }, }) sim_model = KineticSpectrumModel.from_dict({ "initial_concentration": { "j1": { "compartments": ["s1"], "parameters": ["2"] }, }, "shape": { "sh1": ["one"] }, "megacomplex": { "mc1": { "k_matrix": ["k1"] }, }, "k_matrix": { "k1": { "matrix": { ("s1", "s1"): "1", } } }, "dataset": { "dataset1": { "initial_concentration": "j1", "megacomplex": ["mc1"], "shape": { "s1": "sh1" }, }, }, }) 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(0, 50, 1.5)) spectral = np.asarray([0]) axis = {"time": time, "spectral": spectral}
def test_spectral_relation(): model = KineticSpectrumModel.from_dict({ "initial_concentration": { "j1": { "compartments": ["s1", "s2", "s3", "s4"], "parameters": ["i.1", "i.2", "i.3", "i.4"], }, }, "megacomplex": { "mc1": { "k_matrix": ["k1"] }, }, "k_matrix": { "k1": { "matrix": { ("s1", "s1"): "kinetic.1", ("s2", "s2"): "kinetic.1", ("s3", "s3"): "kinetic.1", ("s4", "s4"): "kinetic.1", } } }, "spectral_relations": [ { "compartment": "s1", "target": "s2", "parameter": "rel.1", "interval": [(0, 2)], }, { "compartment": "s1", "target": "s3", "parameter": "rel.2", "interval": [(0, 2)], }, ], "dataset": { "dataset1": { "initial_concentration": "j1", "megacomplex": ["mc1"], }, }, }) print(model) rel1, rel2 = 10, 20 parameter = ParameterGroup.from_dict({ "kinetic": [1e-4], "i": [1, 2, 3, 4], "rel": [rel1, rel2], }) 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) == 4 assert matrix.shape == (time.size, 4) reduced_compartments, relation_matrix = create_spectral_relation_matrix( model, parameter, compartments, matrix, 1) print(relation_matrix) assert len(reduced_compartments) == 2 assert relation_matrix.shape == (4, 2) assert np.array_equal( relation_matrix, [ [1.0, 0.0], [10.0, 0.0], [20.0, 0.0], [0.0, 1.0], ], ) reduced_compartments, reduced_matrix = model.constrain_matrix_function( parameter, compartments, matrix, 1) assert reduced_matrix.shape == (time.size, 2) print(reduced_matrix[0, 0], matrix[0, 0], matrix[0, 1], matrix[0, 2]) assert np.allclose( reduced_matrix[:, 0], matrix[:, 0] + rel1 * matrix[:, 1] + rel2 * matrix[:, 2]) clp = xr.DataArray([[1.0, 10.0, 20.0, 1]], coords=(("spectral", [1]), ("clp_label", ["s1", "s2", "s3", "s4"]))) data = model.simulate("dataset1", parameter, clp=clp, axes={ "time": time, "spectral": np.array([1]) }) result = model.optimize(parameter, {"dataset1": data}, max_nfev=1) result_data = result.data["dataset1"] print(result_data.species_associated_spectra) assert result_data.species_associated_spectra.shape == (1, 4) assert (result_data.species_associated_spectra[0, 1] == rel1 * result_data.species_associated_spectra[0, 0]) assert (result_data.species_associated_spectra[0, 2] == rel2 * result_data.species_associated_spectra[0, 0])
def test_spectral_constraint(): model = KineticSpectrumModel.from_dict({ "initial_concentration": { "j1": { "compartments": ["s1", "s2"], "parameters": ["i.1", "i.2"], }, }, "megacomplex": { "mc1": { "k_matrix": ["k1"] }, }, "k_matrix": { "k1": { "matrix": { ("s2", "s1"): "kinetic.1", ("s2", "s2"): "kinetic.2", } } }, "spectral_constraints": [ { "type": "zero", "compartment": "s2", "interval": (float("-inf"), float("inf")) }, ], "dataset": { "dataset1": { "initial_concentration": "j1", "megacomplex": ["mc1"], }, }, }) print(model) wanted_parameters = ParameterGroup.from_dict({ "kinetic": [1e-4, 1e-5], "i": [1, 2], }) initial_parameters = ParameterGroup.from_dict({ "kinetic": [2e-4, 2e-5], "i": [1, 2, { "vary": False }], }) time = np.asarray(np.arange(0, 50, 1.5)) dataset = model.dataset["dataset1"].fill(model, wanted_parameters) compartments, matrix = kinetic_image_matrix(dataset, time, 0) assert len(compartments) == 2 assert matrix.shape == (time.size, 2) reduced_compartments, reduced_matrix = apply_spectral_constraints( model, compartments, matrix, 1) print(reduced_matrix) assert len(reduced_compartments) == 1 assert reduced_matrix.shape == (time.size, 1) reduced_compartments, reduced_matrix = model.constrain_matrix_function( "dataset1", wanted_parameters, compartments, matrix, 1) assert reduced_matrix.shape == (time.size, 1) clp = xr.DataArray([[1.0, 10.0, 20.0, 1]], coords=(("spectral", [1]), ("clp_label", ["s1", "s2", "s3", "s4"]))) data = model.simulate("dataset1", wanted_parameters, clp=clp, axes={ "time": time, "spectral": np.array([1]) }) dataset = {"dataset1": data} scheme = Scheme(model=model, parameters=initial_parameters, data=dataset, nfev=20) # the resulting jacobian is singular with pytest.warns(UserWarning): result = optimize(scheme) result_data = result.data["dataset1"] print(result_data.clp_label) print(result_data.clp) # TODO: save reduced clp # assert result_data.clp.shape == (1, 1) print(result_data.species_associated_spectra) assert result_data.species_associated_spectra.shape == (1, 2) assert result_data.species_associated_spectra[0, 1] == 0
sim_model = KineticSpectrumModel.from_dict({ "initial_concentration": { "j1": { "compartments": ["s1", "s2", "s3"], "parameters": ["j.1", "j.0", "j.0"], }, }, "k_matrix": { "k1": { "matrix": { ("s2", "s1"): "kinetic.1", ("s3", "s2"): "kinetic.2", ("s3", "s3"): "kinetic.3", } } }, "megacomplex": { "m1": { "k_matrix": ["k1"], } }, "shape": { "sh1": { "type": "gaussian", "amplitude": "shapes.amps.1", "location": "shapes.locs.1", "width": "shapes.width.1", }, "sh2": { "type": "gaussian", "amplitude": "shapes.amps.2", "location": "shapes.locs.2", "width": "shapes.width.2", }, "sh3": { "type": "gaussian", "amplitude": "shapes.amps.3", "location": "shapes.locs.3", "width": "shapes.width.3", }, }, "irf": { "irf1": { "type": "gaussian", "center": "irf.center", "width": "irf.width" }, }, "dataset": { "dataset1": { "initial_concentration": "j1", "megacomplex": ["m1"], "shape": { "s1": "sh1", "s2": "sh2", "s3": "sh3", }, "irf": "irf1", } }, })
class MultiIrfDispersion: model = KineticSpectrumModel.from_dict({ 'initial_concentration': { 'j1': { 'compartments': ['s1'], 'parameters': ['j.1'] }, }, 'megacomplex': { 'mc1': { 'k_matrix': ['k1'] }, }, 'k_matrix': { "k1": { 'matrix': { ("s1", "s1"): 'kinetic.1', } } }, 'irf': { 'irf1': { 'type': 'spectral-multi-gaussian', 'center': ['irf.center'], 'width': ['irf.width'], 'dispersion_center': 'irf.dispcenter', 'center_dispersion': ['irf.centerdisp1', 'irf.centerdisp2'], 'width_dispersion': ['irf.widthdisp'], }, }, 'dataset': { 'dataset1': { 'initial_concentration': 'j1', 'irf': 'irf1', 'megacomplex': ['mc1'], }, }, }) sim_model = KineticSpectrumModel.from_dict({ 'initial_concentration': { 'j1': { 'compartments': ['s1'], 'parameters': ['j.1'] }, }, 'megacomplex': { 'mc1': { 'k_matrix': ['k1'] }, }, 'k_matrix': { "k1": { 'matrix': { ("s1", "s1"): 'kinetic.1', } } }, 'irf': { 'irf1': { 'type': 'spectral-multi-gaussian', 'center': ['irf.center'], 'width': ['irf.width'], 'dispersion_center': 'irf.dispcenter', 'center_dispersion': ['irf.centerdisp1', 'irf.centerdisp2'], 'width_dispersion': ['irf.widthdisp'], }, }, 'shape': { 'sh1': { 'type': "one", }, }, 'dataset': { 'dataset1': { 'initial_concentration': 'j1', 'irf': 'irf1', 'megacomplex': ['mc1'], 'shape': { 's1': 'sh1' } }, }, }) initial = ParameterGroup.from_dict({ 'j': [ ['1', 1, { 'vary': False, 'non-negative': False }], ], 'kinetic': [["1", 0.5], { 'non-negative': False }], 'irf': [['center', 0.3], ['width', 0.1], ['dispcenter', 400, { 'vary': False }], ['centerdisp1', 0.01], ['centerdisp2', 0.001], ['widthdisp', 0.025]], }) wanted = ParameterGroup.from_dict({ 'j': [ ['1', 1, { 'vary': False, 'non-negative': False }], ], 'kinetic': [ ["1", 0.5], ], 'irf': [['center', 0.3], ['width', 0.1], ['dispcenter', 400, { 'vary': False }], ['centerdisp1', 0.01], ['centerdisp2', 0.001], ['widthdisp', 0.025]], }) time = np.arange(-1, 5, 0.2) spectral = np.arange(300, 500, 25) axis = {"time": time, "spectral": spectral}
class SimpleIrfDispersion: model = KineticSpectrumModel.from_dict({ 'initial_concentration': { 'j1': { 'compartments': ['s1'], 'parameters': ['j.1'] }, }, 'megacomplex': { 'mc1': { 'k_matrix': ['k1'] }, }, 'k_matrix': { "k1": { 'matrix': { ("s1", "s1"): 'kinetic.1', } } }, 'irf': { 'irf1': { 'type': 'spectral-gaussian', 'center': 'irf.center', 'width': 'irf.width', 'dispersion_center': 'irf.dispcenter', 'center_dispersion': ['irf.centerdisp'], }, }, 'dataset': { 'dataset1': { 'initial_concentration': 'j1', 'irf': 'irf1', 'megacomplex': ['mc1'], }, }, }) sim_model = KineticSpectrumModel.from_dict({ 'initial_concentration': { 'j1': { 'compartments': ['s1'], 'parameters': ['j.1'] }, }, 'megacomplex': { 'mc1': { 'k_matrix': ['k1'] }, }, 'k_matrix': { "k1": { 'matrix': { ("s1", "s1"): 'kinetic.1', } } }, 'irf': { 'irf1': { 'type': 'spectral-gaussian', 'center': 'irf.center', 'width': 'irf.width', 'dispersion_center': 'irf.dispcenter', 'center_dispersion': ['irf.centerdisp'], }, }, 'shape': { 'sh1': { 'type': "one", }, }, 'dataset': { 'dataset1': { 'initial_concentration': 'j1', 'irf': 'irf1', 'megacomplex': ['mc1'], 'shape': { 's1': 'sh1' } }, }, }) initial = ParameterGroup.from_dict({ 'j': [ ['1', 1, { 'vary': False, 'non-negative': False }], ], 'kinetic': [["1", 0.5], { 'non-negative': False }], 'irf': [['center', 0.3], ['width', 0.1], ['dispcenter', 400, { 'vary': False }], ['centerdisp', 0.5]], }) wanted = ParameterGroup.from_dict({ 'j': [ ['1', 1, { 'vary': False, 'non-negative': False }], ], 'kinetic': [ ["1", 0.5], ], 'irf': [['center', 0.3], ['width', 0.1], ['dispcenter', 400], ['centerdisp', 0.5]], }) time_p1 = np.linspace(-1, 2, 50, endpoint=False) time_p2 = np.linspace(2, 5, 30, endpoint=False) time_p3 = np.geomspace(5, 10, num=20) time = np.concatenate([time_p1, time_p2, time_p3]) spectral = np.arange(300, 500, 5) axis = {"time": time, "spectral": spectral}
def test_coherent_artifact(): model_dict = { 'initial_concentration': { 'j1': { 'compartments': ['s1'], 'parameters': ['2'] }, }, 'megacomplex': { 'mc1': { 'k_matrix': ['k1'] }, }, 'k_matrix': { "k1": { 'matrix': { ("s1", "s1"): '1', } } }, 'irf': { 'irf1': { 'type': 'gaussian-coherent-artifact', 'center': '2', 'width': '3', 'coherent_artifact_order': 3, }, }, 'dataset': { 'dataset1': { 'initial_concentration': 'j1', 'megacomplex': ['mc1'], 'irf': 'irf1', }, }, } model = KineticSpectrumModel.from_dict(model_dict.copy()) parameter = ParameterGroup.from_list([ 101e-4, [10, { 'vary': False, 'non-negative': False }], [20, { 'vary': False, 'non-negative': False }], [30, { 'vary': False, 'non-negative': False }], ]) time = np.asarray(np.arange(0, 50, 1.5)) irf = model.irf['irf1'].fill(model, parameter) irf_same_width = irf.calculate_coherent_artifact(time) model_dict['irf']['irf1']['coherent_artifact_width'] = '4' model = KineticSpectrumModel.from_dict(model_dict) irf = model.irf['irf1'].fill(model, parameter) irf_diff_width = irf.calculate_coherent_artifact(time) assert not np.array_equal(irf_same_width, irf_diff_width) dataset = model.dataset['dataset1'].fill(model, parameter) compartments, matrix = kinetic_spectrum_matrix(dataset, time, 0) assert len(compartments) == 4 for i in range(1, 4): assert compartments[i] == f'coherent_artifact_{i}' assert matrix.shape == (time.size, 4) clp = xr.DataArray([[1, 1, 1, 1]], coords=[('spectral', [0]), ('clp_label', [ 's1', 'coherent_artifact_1', 'coherent_artifact_2', 'coherent_artifact_3', ])]) axis = {"time": time, "spectral": clp.spectral} dataset = model.simulate('dataset1', parameter, axis, clp) data = {'dataset1': dataset} result = model.optimize(parameter, data, max_nfev=20) print(result.optimized_parameter) for label, param in result.optimized_parameter.all(): assert np.allclose(param.value, parameter.get(label).value, rtol=1e-1) resultdata = result.data["dataset1"] assert np.array_equal(dataset['time'], resultdata['time']) assert np.array_equal(dataset['spectral'], resultdata['spectral']) assert dataset.data.shape == resultdata.data.shape assert dataset.data.shape == resultdata.fitted_data.shape assert np.allclose(dataset.data, resultdata.fitted_data, rtol=1e-2) assert 'coherent_artifact_concentration' in resultdata assert resultdata['coherent_artifact_concentration'].shape == (time.size, 3) assert 'coherent_artifact_associated_spectra' in resultdata assert resultdata['coherent_artifact_associated_spectra'].shape == (1, 3)
class OneComponentOneChannelGaussianIrf: model = KineticSpectrumModel.from_dict({ 'initial_concentration': { 'j1': { 'compartments': ['s1'], 'parameters': ['2'] }, }, 'megacomplex': { 'mc1': { 'k_matrix': ['k1'] }, }, 'k_matrix': { "k1": { 'matrix': { ("s1", "s1"): '1', } } }, 'irf': { 'irf1': { 'type': 'spectral-gaussian', 'center': '2', 'width': '3' }, }, 'dataset': { 'dataset1': { 'initial_concentration': 'j1', 'irf': 'irf1', 'megacomplex': ['mc1'], }, }, }) sim_model = KineticSpectrumModel.from_dict({ 'initial_concentration': { 'j1': { 'compartments': ['s1'], 'parameters': ['4'] }, }, 'shape': { 'sh1': ['one'] }, 'megacomplex': { 'mc1': { 'k_matrix': ['k1'] }, }, 'k_matrix': { "k1": { 'matrix': { ("s1", "s1"): '1', } } }, 'irf': { 'irf1': { 'type': 'spectral-gaussian', 'center': '2', 'width': '3' }, }, 'dataset': { 'dataset1': { 'initial_concentration': 'j1', 'irf': 'irf1', 'megacomplex': ['mc1'], 'shape': { 's1': 'sh1' } }, }, }) 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)) spectral = np.asarray([0]) axis = {"time": time, "spectral": spectral}
class ThreeComponentSequential: model = KineticSpectrumModel.from_dict({ 'initial_concentration': { 'j1': { 'compartments': ['s1', 's2', 's3'], 'parameters': ['j.1', 'j.0', 'j.0'] }, }, 'megacomplex': { 'mc1': { 'k_matrix': ['k1'] }, }, 'k_matrix': { "k1": { 'matrix': { ("s2", "s1"): 'kinetic.1', ("s3", "s2"): 'kinetic.2', ("s3", "s3"): 'kinetic.3', } } }, 'irf': { 'irf1': { 'type': 'spectral-multi-gaussian', 'center': ['irf.center'], 'width': ['irf.width'] }, }, 'dataset': { 'dataset1': { 'initial_concentration': 'j1', 'irf': 'irf1', 'megacomplex': ['mc1'], }, }, }) sim_model = KineticSpectrumModel.from_dict({ 'initial_concentration': { 'j1': { 'compartments': ['s1', 's2', 's3'], 'parameters': ['j.1', 'j.0', 'j.0'] }, }, 'megacomplex': { 'mc1': { 'k_matrix': ['k1'] }, }, 'k_matrix': { "k1": { 'matrix': { ("s2", "s1"): 'kinetic.1', ("s3", "s2"): 'kinetic.2', ("s3", "s3"): 'kinetic.3', } } }, 'shape': { 'sh1': { 'type': "gaussian", 'amplitude': "shape.amps.1", 'location': "shape.locs.1", 'width': "shape.width.1", }, 'sh2': { 'type': "gaussian", 'amplitude': "shape.amps.2", 'location': "shape.locs.2", 'width': "shape.width.2", }, 'sh3': { 'type': "gaussian", 'amplitude': "shape.amps.3", 'location': "shape.locs.3", 'width': "shape.width.3", }, }, 'irf': { 'irf1': { 'type': 'spectral-multi-gaussian', 'center': ['irf.center'], 'width': ['irf.width'] }, }, 'dataset': { 'dataset1': { 'initial_concentration': 'j1', 'irf': 'irf1', 'megacomplex': ['mc1'], 'shape': { 's1': 'sh1', 's2': 'sh2', 's3': 'sh3' } }, }, }) initial = ParameterGroup.from_dict({ 'kinetic': [ ["1", 501e-3], ["2", 202e-4], ["3", 105e-5], { 'non-negative': True }, ], 'irf': [['center', 1.3], ['width', 7.8]], 'j': [['1', 1, { 'vary': False, 'non-negative': False }], ['0', 0, { 'vary': False, 'non-negative': False }]], }) wanted = ParameterGroup.from_dict({ 'kinetic': [ ["1", 501e-3], ["2", 202e-4], ["3", 105e-5], ], 'shape': { 'amps': [3, 1, 5], 'locs': [620, 670, 720], 'width': [10, 30, 50] }, 'irf': [['center', 1.3], ['width', 7.8]], 'j': [['1', 1, { 'vary': False, 'non-negative': False }], ['0', 0, { 'vary': False, 'non-negative': False }]], }) time = np.asarray(np.arange(-10, 50, 1.0)) spectral = np.arange(600, 750, 5.0) axis = {"time": time, "spectral": spectral}
class MultiIrfDispersion: model = KineticSpectrumModel.from_dict({ "initial_concentration": { "j1": { "compartments": ["s1"], "parameters": ["j.1"] }, }, "megacomplex": { "mc1": { "k_matrix": ["k1"] }, }, "k_matrix": { "k1": { "matrix": { ("s1", "s1"): "kinetic.1", } } }, "irf": { "irf1": { "type": "spectral-multi-gaussian", "center": ["irf.center"], "width": ["irf.width"], "dispersion_center": "irf.dispcenter", "center_dispersion": ["irf.centerdisp1", "irf.centerdisp2"], "width_dispersion": ["irf.widthdisp"], }, }, "dataset": { "dataset1": { "initial_concentration": "j1", "irf": "irf1", "megacomplex": ["mc1"], }, }, }) sim_model = KineticSpectrumModel.from_dict({ "initial_concentration": { "j1": { "compartments": ["s1"], "parameters": ["j.1"] }, }, "megacomplex": { "mc1": { "k_matrix": ["k1"] }, }, "k_matrix": { "k1": { "matrix": { ("s1", "s1"): "kinetic.1", } } }, "irf": { "irf1": { "type": "spectral-multi-gaussian", "center": ["irf.center"], "width": ["irf.width"], "dispersion_center": "irf.dispcenter", "center_dispersion": ["irf.centerdisp1", "irf.centerdisp2"], "width_dispersion": ["irf.widthdisp"], }, }, "shape": { "sh1": { "type": "one", }, }, "dataset": { "dataset1": { "initial_concentration": "j1", "irf": "irf1", "megacomplex": ["mc1"], "shape": { "s1": "sh1" }, }, }, }) initial = ParameterGroup.from_dict({ "j": [ ["1", 1, { "vary": False, "non-negative": False }], ], "kinetic": [["1", 0.5], { "non-negative": False }], "irf": [ ["center", 0.3], ["width", 0.1], ["dispcenter", 400, { "vary": False }], ["centerdisp1", 0.01], ["centerdisp2", 0.001], ["widthdisp", 0.025], ], }) wanted = ParameterGroup.from_dict({ "j": [ ["1", 1, { "vary": False, "non-negative": False }], ], "kinetic": [ ["1", 0.5], ], "irf": [ ["center", 0.3], ["width", 0.1], ["dispcenter", 400, { "vary": False }], ["centerdisp1", 0.01], ["centerdisp2", 0.001], ["widthdisp", 0.025], ], }) time = np.arange(-1, 5, 0.2) spectral = np.arange(300, 500, 100) axis = {"time": time, "spectral": spectral}
class ThreeComponentSequential: model = KineticSpectrumModel.from_dict({ "initial_concentration": { "j1": { "compartments": ["s1", "s2", "s3"], "parameters": ["j.1", "j.0", "j.0"] }, }, "megacomplex": { "mc1": { "k_matrix": ["k1"] }, }, "k_matrix": { "k1": { "matrix": { ("s2", "s1"): "kinetic.1", ("s3", "s2"): "kinetic.2", ("s3", "s3"): "kinetic.3", } } }, "irf": { "irf1": { "type": "spectral-multi-gaussian", "center": ["irf.center"], "width": ["irf.width"], }, }, "dataset": { "dataset1": { "initial_concentration": "j1", "irf": "irf1", "megacomplex": ["mc1"], }, }, }) sim_model = KineticSpectrumModel.from_dict({ "initial_concentration": { "j1": { "compartments": ["s1", "s2", "s3"], "parameters": ["j.1", "j.0", "j.0"] }, }, "megacomplex": { "mc1": { "k_matrix": ["k1"] }, }, "k_matrix": { "k1": { "matrix": { ("s2", "s1"): "kinetic.1", ("s3", "s2"): "kinetic.2", ("s3", "s3"): "kinetic.3", } } }, "shape": { "sh1": { "type": "gaussian", "amplitude": "shapes.amps.1", "location": "shapes.locs.1", "width": "shapes.width.1", }, "sh2": { "type": "gaussian", "amplitude": "shapes.amps.2", "location": "shapes.locs.2", "width": "shapes.width.2", }, "sh3": { "type": "gaussian", "amplitude": "shapes.amps.3", "location": "shapes.locs.3", "width": "shapes.width.3", }, }, "irf": { "irf1": { "type": "spectral-multi-gaussian", "center": ["irf.center"], "width": ["irf.width"], }, }, "dataset": { "dataset1": { "initial_concentration": "j1", "irf": "irf1", "megacomplex": ["mc1"], "shape": { "s1": "sh1", "s2": "sh2", "s3": "sh3" }, }, }, }) initial_parameters = ParameterGroup.from_dict({ "kinetic": [ ["1", 501e-3], ["2", 202e-4], ["3", 105e-5], { "non-negative": True }, ], "irf": [["center", 1.3], ["width", 7.8]], "j": [ ["1", 1, { "vary": False, "non-negative": False }], ["0", 0, { "vary": False, "non-negative": False }], ], }) wanted_parameters = ParameterGroup.from_dict({ "kinetic": [ ["1", 501e-3], ["2", 202e-4], ["3", 105e-5], ], "shapes": { "amps": [3, 1, 5], "locs": [620, 670, 720], "width": [10, 30, 50] }, "irf": [["center", 1.3], ["width", 7.8]], "j": [ ["1", 1, { "vary": False, "non-negative": False }], ["0", 0, { "vary": False, "non-negative": False }], ], }) time = np.asarray(np.arange(-10, 50, 1.0)) spectral = np.arange(600, 750, 5.0) axis = {"time": time, "spectral": spectral}
class SimpleIrfDispersion: model = KineticSpectrumModel.from_dict({ "initial_concentration": { "j1": { "compartments": ["s1"], "parameters": ["j.1"] }, }, "megacomplex": { "mc1": { "k_matrix": ["k1"] }, }, "k_matrix": { "k1": { "matrix": { ("s1", "s1"): "kinetic.1", } } }, "irf": { "irf1": { "type": "spectral-gaussian", "center": "irf.center", "width": "irf.width", "dispersion_center": "irf.dispcenter", "center_dispersion": ["irf.centerdisp"], }, }, "dataset": { "dataset1": { "initial_concentration": "j1", "irf": "irf1", "megacomplex": ["mc1"], }, }, }) sim_model = KineticSpectrumModel.from_dict({ "initial_concentration": { "j1": { "compartments": ["s1"], "parameters": ["j.1"] }, }, "megacomplex": { "mc1": { "k_matrix": ["k1"] }, }, "k_matrix": { "k1": { "matrix": { ("s1", "s1"): "kinetic.1", } } }, "irf": { "irf1": { "type": "spectral-gaussian", "center": "irf.center", "width": "irf.width", "dispersion_center": "irf.dispcenter", "center_dispersion": ["irf.centerdisp"], }, }, "shape": { "sh1": { "type": "one", }, }, "dataset": { "dataset1": { "initial_concentration": "j1", "irf": "irf1", "megacomplex": ["mc1"], "shape": { "s1": "sh1" }, }, }, }) initial = ParameterGroup.from_dict({ "j": [ ["1", 1, { "vary": False, "non-negative": False }], ], "kinetic": [["1", 0.5], { "non-negative": False }], "irf": [ ["center", 0.3], ["width", 0.1], ["dispcenter", 400, { "vary": False }], ["centerdisp", 0.5], ], }) wanted = ParameterGroup.from_dict({ "j": [ ["1", 1, { "vary": False, "non-negative": False }], ], "kinetic": [ ["1", 0.5], ], "irf": [["center", 0.3], ["width", 0.1], ["dispcenter", 400], ["centerdisp", 0.5]], }) time_p1 = np.linspace(-1, 2, 50, endpoint=False) time_p2 = np.linspace(2, 5, 30, endpoint=False) time_p3 = np.geomspace(5, 10, num=20) time = np.concatenate([time_p1, time_p2, time_p3]) spectral = np.arange(300, 500, 100) axis = {"time": time, "spectral": spectral}
def test_spectral_penalties(): model_without_penalty = KineticSpectrumModel.from_dict({ 'initial_concentration': { 'j1': { 'compartments': ['s1', 's2', 's3'], 'parameters': ['i.1', 'i.2', 'i.3'] }, }, 'megacomplex': { 'mc1': {'k_matrix': ['k1']}, }, 'k_matrix': { "k1": {'matrix': { ("s1", "s1"): 'kinetic.1', ("s2", "s2"): 'kinetic.1', ("s3", "s3"): 'kinetic.1', }} }, 'spectral_relations': [ { 'compartment': 's1', 'target': 's2', 'parameter': 'rel.1', 'interval': [(0, 2)], }, ], 'dataset': { 'dataset1': { 'initial_concentration': 'j1', 'megacomplex': ['mc1'], }, }, }) weight = 0.1 model_with_penalty = KineticSpectrumModel.from_dict({ 'initial_concentration': { 'j1': { 'compartments': ['s1', 's2', 's3'], 'parameters': ['i.1', 'i.2', 'i.3'] }, }, 'megacomplex': { 'mc1': {'k_matrix': ['k1']}, }, 'k_matrix': { "k1": {'matrix': { ("s1", "s1"): 'kinetic.1', ("s2", "s2"): 'kinetic.1', ("s3", "s3"): 'kinetic.1', }} }, 'equal_area_penalties': [ { 'compartment': 's2', 'target': 's3', 'parameter': 'pen.1', 'interval': [(0, 2)], 'weight': weight }, ], 'spectral_relations': [ { 'compartment': 's1', 'target': 's2', 'parameter': 'rel.1', 'interval': [(0, 2)], }, ], 'dataset': { 'dataset1': { 'initial_concentration': 'j1', 'megacomplex': ['mc1'], }, }, }) print(model_with_penalty) rel1 = 2 pen = 0.5 parameter = ParameterGroup.from_dict({ 'kinetic': [1e-4], 'i': [1, 1, 1], 'rel': [rel1], 'pen': [pen], }) time = np.asarray(np.arange(0, 50, 1.5)) clp = xr.DataArray([[1., 2., 4]], coords=(('spectral', [1]), ('clp_label', ['s1', 's2', 's3']))) data = model_without_penalty.simulate('dataset1', parameter, clp=clp, axes={'time': time, 'spectral': np.array([1])}) result_with_penalty = \ model_with_penalty.optimize(parameter, {'dataset1': data}, max_nfev=1) result_without_penalty = \ model_without_penalty.optimize(parameter, {'dataset1': data}, max_nfev=1) result_data = result_with_penalty.data['dataset1'] wanted_penalty = result_data.species_associated_spectra.sel(species='s2') - \ result_data.species_associated_spectra.sel(species='s3') * pen wanted_penalty *= weight wanted_penalty **= 2 wanted_penalty = np.sum(wanted_penalty.values) additional_penalty = result_with_penalty.chisqr - result_without_penalty.chisqr assert np.isclose(additional_penalty, wanted_penalty)