class MultichannelMulticomponentDecay: wanted_parameters = ParameterGroup.from_dict( { "k": [0.006, 0.003, 0.0003, 0.03], "loc": [ ["1", 14705], ["2", 13513], ["3", 14492], ["4", 14388], ], "amp": [ ["1", 1], ["2", 2], ["3", 5], ["4", 20], ], "del": [ ["1", 400], ["2", 100], ["3", 300], ["4", 200], ], } ) initial_parameters = ParameterGroup.from_dict({"k": [0.006, 0.003, 0.0003, 0.03]}) global_axis = np.arange(12820, 15120, 50) model_axis = np.arange(0, 150, 1.5) sim_model = DecayModel.from_dict( { "megacomplex": { "m1": {"is_index_dependent": False}, "m2": { "type": "global_complex_shaped", "location": ["loc.1", "loc.2", "loc.3", "loc.4"], "delta": ["del.1", "del.2", "del.3", "del.4"], "amplitude": ["amp.1", "amp.2", "amp.3", "amp.4"], }, }, "dataset": { "dataset1": { "megacomplex": ["m1"], "global_megacomplex": ["m2"], "kinetic": ["k.1", "k.2", "k.3", "k.4"], } }, } ) model = DecayModel.from_dict( { "megacomplex": {"m1": {"is_index_dependent": False}}, "dataset": { "dataset1": { "megacomplex": ["m1"], "kinetic": ["k.1", "k.2", "k.3", "k.4"], } }, } )
class ThreeComponentSequential: model = KineticImageModel.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': 'multi-gaussian', 'center': ['irf.center'], 'width': ['irf.width']}, }, 'dataset': { 'dataset1': { 'initial_concentration': 'j1', 'irf': 'irf1', 'megacomplex': ['mc1'], }, }, }) 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], ], '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)) pixel = np.arange(600, 750, 10) axis = {"time": time, "pixel": pixel} clp = _create_gaussian_clp( ['s1', 's2', 's3'], [7, 3, 30], [620, 670, 720], [10, 30, 50], pixel)
class MultichannelMulticomponentDecay: wanted_parameters = ParameterGroup.from_dict({ "k": [0.006, 0.003, 0.0003, 0.03], "loc": [ ["1", 14705], ["2", 13513], ["3", 14492], ["4", 14388], ], "amp": [ ["1", 1], ["2", 2], ["3", 5], ["4", 20], ], "del": [ ["1", 400], ["2", 100], ["3", 300], ["4", 200], ], }) initial_parameters = ParameterGroup.from_dict( {"k": [0.006, 0.003, 0.0003, 0.03]}) e_axis = np.arange(12820, 15120, 50) c_axis = np.arange(0, 150, 1.5) sim_model = GaussianDecayModel.from_dict({ "compartment": ["s1", "s2", "s3", "s4"], "dataset": { "dataset1": { "initial_concentration": [], "megacomplex": [], "kinetic": ["k.1", "k.2", "k.3", "k.4"], "location": ["loc.1", "loc.2", "loc.3", "loc.4"], "delta": ["del.1", "del.2", "del.3", "del.4"], "amplitude": ["amp.1", "amp.2", "amp.3", "amp.4"], } }, }) model = GaussianDecayModel.from_dict({ "compartment": ["s1", "s2", "s3", "s4"], "dataset": { "dataset1": { "initial_concentration": [], "megacomplex": [], "kinetic": ["k.1", "k.2", "k.3", "k.4"], } }, })
class MultichannelMulticomponentDecay: wanted = ParameterGroup.from_dict({ 'k': [.006, 0.003, 0.0003, 0.03], 'loc': [ ['1', 14705], ['2', 13513], ['3', 14492], ['4', 14388], ], 'amp': [ ['1', 1], ['2', 2], ['3', 5], ['4', 20], ], 'del': [ ['1', 400], ['2', 100], ['3', 300], ['4', 200], ] }) initial = ParameterGroup.from_dict({'k': [.006, 0.003, 0.0003, 0.03]}) e_axis = np.arange(12820, 15120, 50) c_axis = np.arange(0, 150, 1.5) sim_model = GaussianDecayModel.from_dict({ 'compartment': ["s1", "s2", "s3", "s4"], 'dataset': { 'dataset1': { 'initial_concentration': [], 'megacomplex': [], 'kinetic': ['k.1', 'k.2', 'k.3', 'k.4'], 'location': ['loc.1', 'loc.2', 'loc.3', 'loc.4'], 'delta': ['del.1', 'del.2', 'del.3', 'del.4'], 'amplitude': ['amp.1', 'amp.2', 'amp.3', 'amp.4'], } } }) model = DecayModel.from_dict({ 'compartment': ["s1", "s2", "s3", "s4"], 'dataset': { 'dataset1': { 'initial_concentration': [], 'megacomplex': [], 'kinetic': ['k.1', 'k.2', 'k.3', 'k.4'] } } })
def test_kinetic_residual_benchmark(benchmark, nnls): suite = ThreeComponentSequential model = suite.model sim_model = suite.sim_model wanted = suite.wanted initial = ParameterGroup.from_dict({ 'kinetic': [ ["1", 501e-2], ["2", 202e-3], ["3", 105e-4], ], 'irf': [['center', 0.3], ['width', 7.8]], 'j': [['1', 1, {'vary': False, 'non-negative': False}], ['0', 0, {'vary': False, 'non-negative': False}]], }) dataset = sim_model.simulate('dataset1', wanted, suite.axis) data = {'dataset1': dataset} scheme = Scheme(model=model, parameter=initial, data=data, nnls=nnls) optimizer = Optimizer(scheme) benchmark(optimizer._calculate_penalty, initial)
def load_parameters(self, file_name: str) -> ParameterGroup: """Create a ParameterGroup instance from the specs defined in a file. Parameters ---------- file_name : str File containing the parameter specs. Returns ------- ParameterGroup ParameterGroup instance created from the file. """ spec = self._load_yml(file_name) if isinstance(spec, list): return ParameterGroup.from_list(spec) else: return ParameterGroup.from_dict(spec)
def test_kinetic_matrix_benchmark(benchmark): model = KineticModel.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': 'gaussian', 'center': ['irf.center'], 'width': ['irf.width']}, }, 'dataset': { 'dataset1': { 'initial_concentration': 'j1', 'irf': 'irf1', 'megacomplex': ['mc1'], }, }, }) parameter = ParameterGroup.from_dict({ 'kinetic': [ ["1", 101e-4], ["2", 302e-3], ["3", 201e-2], ], 'irf': [['center', 0], ['width', 5]], 'j': [['1', 1, {'vary': False}], ['0', 0, {'vary': False}]], }) dataset = model.dataset['dataset1'].fill(model, parameter) time = np.asarray(np.arange(-10, 100, 0.02)) benchmark(calculate_kinetic_matrix, dataset, time, 0)
class ThreeComponentSequential: model = KineticModel.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': 'gaussian', 'center': ['irf.center'], 'width': ['irf.width'] }, }, 'dataset': { 'dataset1': { 'initial_concentration': 'j1', 'irf': 'irf1', 'megacomplex': ['mc1'], }, }, }) sim_model = KineticModel.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': '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 IrfDispersion: model = KineticModel.from_dict({ 'initial_concentration': { 'j1': { 'compartments': ['s1', 's2', 's3', 's4'], 'parameters': ['j.1', 'j.0', 'j.0', 'j.0'] }, }, 'megacomplex': { 'mc1': { 'k_matrix': ['k1'] }, }, 'k_matrix': { "k1": { 'matrix': { ("s2", "s1"): 'kinetic.1', ("s3", "s2"): 'kinetic.2', ("s4", "s3"): 'kinetic.3', ("s4", "s4"): 'kinetic.4', } } }, 'irf': { 'irf1': { 'type': '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 = KineticModel.from_dict({ 'initial_concentration': { 'j1': { 'compartments': ['s1', 's2', 's3', 's4'], 'parameters': ['j.1', 'j.0', 'j.0', 'j.0'] }, }, 'megacomplex': { 'mc1': { 'k_matrix': ['k1'] }, }, 'k_matrix': { "k1": { 'matrix': { ("s2", "s1"): 'kinetic.1', ("s3", "s2"): 'kinetic.2', ("s4", "s3"): 'kinetic.3', ("s4", "s4"): 'kinetic.4', } } }, '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", }, 'sh4': { 'type': "gaussian", 'amplitude': "shape.amps.4", 'location': "shape.locs.4", 'width': "shape.width.4", }, }, 'irf': { 'irf1': { 'type': 'gaussian', 'center': ['irf.center'], 'width': ['irf.width'], 'dispersion_center': 'irf.dispcenter', 'center_dispersion': ['irf.centerdisp'], }, }, 'dataset': { 'dataset1': { 'initial_concentration': 'j1', 'irf': 'irf1', 'megacomplex': ['mc1'], 'shape': { 's1': 'sh1', 's2': 'sh2', 's3': 'sh3', 's4': 'sh4' } }, }, }) initial = ParameterGroup.from_dict({ 'j': [ ['1', 1, { 'vary': False, 'non-negative': False }], ['0', 0, { 'vary': False, 'non-negative': False }], ], 'kinetic': [["1", 1], ["2", 0.4], ["3", 0.05], ["4", 0.009], { 'non-negative': True }], 'irf': [['center', 0.3], ['width', 0.1], ['dispcenter', 400, { 'vary': False }], ['centerdisp', 0.25]], }) wanted = ParameterGroup.from_dict({ 'j': [ ['1', 1, { 'vary': False, 'non-negative': False }], ['0', 0, { 'vary': False, 'non-negative': False }], ], 'kinetic': [ ["1", 1], ["2", 0.4], ["3", 0.05], ["4", 0.009], ], 'shape': { 'amps': [2, 4, 5, 8], 'locs': [320, 380, 420, 460], 'width': [30, 20, 10, 40] }, 'irf': [['center', 0.3], ['width', 0.1], ['dispcenter', 400], ['centerdisp', 0.25]], }) time = np.arange(-1, 30, 0.01) spectral = np.arange(300, 500, 5) 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.], [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., 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)
class OneOscillationWithSequentialModel: sim_model = DampedOscillationsModel.from_dict({ "initial_concentration": { "j1": { "compartments": ["s1", "s2"], "parameters": ["j.1", "j.0"] }, }, "k_matrix": { "k1": { "matrix": { ("s2", "s1"): "kinetic.1", ("s2", "s2"): "kinetic.2", } } }, "megacomplex": { "m1": { "type": "decay", "k_matrix": ["k1"] }, "m2": { "type": "damped_oscillation", "labels": ["osc1"], "frequencies": ["osc.freq"], "rates": ["osc.rate"], }, "m3": { "type": "spectral", "shape": { "osc1_cos": "sh1", "osc1_sin": "sh1", "s1": "sh2", "s2": "sh3", }, }, }, "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", "irf": "irf1", "megacomplex": ["m1", "m2"], "global_megacomplex": ["m3"], } }, }) model = DampedOscillationsModel.from_dict({ "initial_concentration": { "j1": { "compartments": ["s1", "s2"], "parameters": ["j.1", "j.0"] }, }, "k_matrix": { "k1": { "matrix": { ("s2", "s1"): "kinetic.1", ("s2", "s2"): "kinetic.2", } } }, "megacomplex": { "m1": { "type": "decay", "k_matrix": ["k1"] }, "m2": { "type": "damped_oscillation", "labels": ["osc1"], "frequencies": ["osc.freq"], "rates": ["osc.rate"], }, }, "irf": { "irf1": { "type": "gaussian", "center": "irf.center", "width": "irf.width", }, }, "dataset": { "dataset1": { "initial_concentration": "j1", "irf": "irf1", "megacomplex": ["m1", "m2"], } }, }) wanted_parameter = ParameterGroup.from_dict({ "j": [ ["1", 1, { "vary": False, "non-negative": False }], ["0", 0, { "vary": False, "non-negative": False }], ], "kinetic": [ ["1", 0.2], ["2", 0.01], ], "osc": [ ["freq", 25], ["rate", 0.1], ], "shapes": { "amps": [0.07, 2, 4], "locs": [5, 2, 8], "width": [4, 2, 3] }, "irf": [["center", 0.3], ["width", 0.1]], }) parameter = ParameterGroup.from_dict({ "j": [ ["1", 1, { "vary": False, "non-negative": False }], ["0", 0, { "vary": False, "non-negative": False }], ], "kinetic": [ ["1", 0.2], ["2", 0.01], ], "osc": [ ["freq", 25], ["rate", 0.1], ], "irf": [["center", 0.3], ["width", 0.1]], }) time = np.arange(-1, 5, 0.01) spectral = np.arange(0, 10) axis = {"time": time, "spectral": spectral} wanted_clp = ["osc1_cos", "osc1_sin", "s1", "s2"] wanted_shape = (600, 4)
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)
class OneOscillation: sim_model = DampedOscillationsModel.from_dict({ "megacomplex": { "m1": { "type": "damped_oscillation", "labels": ["osc1"], "frequencies": ["osc.freq"], "rates": ["osc.rate"], }, "m2": { "type": "spectral", "shape": { "osc1_cos": "sh1", "osc1_sin": "sh1" }, }, }, "shape": { "sh1": { "type": "gaussian", "amplitude": "shapes.amps.1", "location": "shapes.locs.1", "width": "shapes.width.1", }, }, "dataset": { "dataset1": { "megacomplex": ["m1"], "global_megacomplex": ["m2"] } }, }) model = DampedOscillationsModel.from_dict({ "megacomplex": { "m1": { "type": "damped_oscillation", "labels": ["osc1"], "frequencies": ["osc.freq"], "rates": ["osc.rate"], }, }, "dataset": { "dataset1": { "megacomplex": ["m1"] } }, }) wanted_parameter = ParameterGroup.from_dict({ "osc": [ ["freq", 25.5], ["rate", 0.1], ], "shapes": { "amps": [7], "locs": [5], "width": [4] }, }) parameter = ParameterGroup.from_dict({ "osc": [ ["freq", 20], ["rate", 0.3], ], }) time = np.arange(0, 3, 0.01) spectral = np.arange(0, 10) axis = {"time": time, "spectral": spectral} wanted_clp = ["osc1_cos", "osc1_sin"] wanted_shape = (300, 2)
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)
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}
} }, }) wanted_parameter = ParameterGroup.from_dict({ "j": [ ["1", 1, { "non-negative": False, "vary": False }], ["0", 0, { "non-negative": False, "vary": False }], ], "kinetic": [ ["1", 0.5], ["2", 0.3], ["3", 0.1], ], "shapes": { "amps": [30, 20, 40], "locs": [620, 630, 650], "width": [40, 20, 60] }, "irf": [["center", 0.3], ["width", 0.1]], }) parameter = ParameterGroup.from_dict({ "j": [ ["1", 1, {
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
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}
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 ThreeComponentSequential: model = KineticImageModel.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": "multi-gaussian", "center": ["irf.center"], "width": ["irf.width"], }, }, "dataset": { "dataset1": { "initial_concentration": "j1", "irf": "irf1", "megacomplex": ["mc1"], }, }, }) 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], ], "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)) pixel = np.arange(600, 750, 10) axis = {"time": time, "pixel": pixel} clp = _create_gaussian_clp(["s1", "s2", "s3"], [7, 3, 30], [620, 670, 720], [10, 30, 50], pixel)
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_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])