Skip to content

Commit

Permalink
Merge fc4b97d into f68892b
Browse files Browse the repository at this point in the history
  • Loading branch information
joernweissenborn committed Nov 11, 2019
2 parents f68892b + fc4b97d commit 9a7f23b
Show file tree
Hide file tree
Showing 7 changed files with 314 additions and 168 deletions.
44 changes: 22 additions & 22 deletions glotaran/analysis/residual_calculation.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,12 @@ def create_index_independend_ungrouped_residual(
residuals[label].append(residual)
penalties.append(residual)

if callable(scheme.model.additional_penalty_function):
additional_penalty = dask.delayed(scheme.model.additional_penalty_function)(
parameter, reduced_clp_labels[label], reduced_clps[label], i
)
penalties.append(additional_penalty)
if callable(scheme.model.has_additional_penalty_function):
if scheme.model.has_additional_penalty_function():
additional_penalty = dask.delayed(scheme.model.additional_penalty_function)(
parameter, reduced_clp_labels[label], reduced_clps[label], i
)
penalties.append(additional_penalty)

penalty = dask.delayed(np.concatenate)(penalties)
return reduced_clp_labels, reduced_clps, residuals, penalty
Expand Down Expand Up @@ -63,13 +64,12 @@ def create_index_dependend_ungrouped_residual(
residuals[label].append(residual)
penalties.append(residual)

# call removed because of performance reasons (issue #230)
# if callable(scheme.model.additional_penalty_function):
# additional_penalty = dask.delayed(scheme.model.additional_penalty_function)(
# parameter, clp_label, clp, i
# )
# penalties.append(additional_penalty)
# TODO: re-implement removed functionality (issue: #237)
if callable(scheme.model.has_additional_penalty_function):
if scheme.model.has_additional_penalty_function():
additional_penalty = dask.delayed(scheme.model.additional_penalty_function)(
parameter, clp_label, clp, i
)
penalties.append(additional_penalty)

penalty = dask.delayed(np.concatenate)(penalties)
return reduced_clp_labels, reduced_clps, residuals, penalty
Expand All @@ -86,11 +86,11 @@ def penalty_function(matrix_label, problem, labels_and_matrices):
clp, residual = residual_function(labels_and_matrices[matrix_label].matrix, problem.data)

penalty = residual
if callable(scheme.model.additional_penalty_function):
additional_penalty = scheme.model.additional_penalty_function(
parameter, labels_and_matrices[matrix_label].clp_label, clp, problem.index
)
if additional_penalty:
if callable(scheme.model.has_additional_penalty_function):
if scheme.model.has_additional_penalty_function():
additional_penalty = scheme.model.additional_penalty_function(
parameter, labels_and_matrices[matrix_label].clp_label, clp, problem.index
)
penalty = np.concatenate([penalty, additional_penalty])
return clp, residual, penalty
penalty_bag = \
Expand All @@ -113,11 +113,11 @@ def penalty_function(problem, labels_and_matrices):
clp, residual = residual_function(labels_and_matrices.matrix, problem.data)

penalty = residual
if callable(scheme.model.additional_penalty_function):
additional_penalty = scheme.model.additional_penalty_function(
parameter, labels_and_matrices.clp_label, clp, problem.index
)
if additional_penalty:
if callable(scheme.model.has_additional_penalty_function):
if scheme.model.has_additional_penalty_function():
additional_penalty = scheme.model.additional_penalty_function(
parameter, labels_and_matrices.clp_label, clp, problem.index
)
penalty = np.concatenate([penalty, additional_penalty])
return clp, residual, penalty
penalty_bag = \
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
from .spectral_constraints import SpectralConstraint, apply_spectral_constraints
from .spectral_irf import IrfSpectralMultiGaussian
from .spectral_matrix import spectral_matrix
from .spectral_penalties import EqualAreaPenalty, apply_spectral_penalties
from .spectral_penalties import EqualAreaPenalty, has_spectral_penalties, apply_spectral_penalties
from .spectral_relations import SpectralRelation, apply_spectral_relations
from .spectral_shape import SpectralShape

Expand Down Expand Up @@ -61,6 +61,7 @@ def grouped(model: typing.Type['KineticModel']):
global_matrix=spectral_matrix,
global_dimension='spectral',
constrain_matrix_function=apply_kinetic_model_constraints,
has_additional_penalty_function=has_spectral_penalties,
additional_penalty_function=apply_spectral_penalties,
grouped=grouped,
index_dependend=index_dependend,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,16 +42,17 @@ def applies(interval):
return any([applies(i) for i in self.interval])


def has_spectral_penalties(model: typing.Type['KineticModel']) -> bool:
return len(model.equal_area_penalties) != 0


def apply_spectral_penalties(
model: typing.Type['KineticModel'],
parameter: ParameterGroup,
clp_labels: typing.List[str],
clps: np.ndarray,
index: float) -> np.ndarray:

if not model.equal_area_penalties:
return []

clp_labels, clps = retrieve_clps(model, parameter, clp_labels, clps, index)

penalties = []
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -344,149 +344,11 @@ class ThreeComponentSequential:
axis = {"time": time, "spectral": spectral}


class IrfDispersion:
model = KineticSpectrumModel.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': 'spectral-multi-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', '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': 'spectral-multi-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, 25)
axis = {"time": time, "spectral": spectral}


@pytest.mark.parametrize("suite", [
OneComponentOneChannel,
OneComponentOneChannelGaussianIrf,
ThreeComponentParallel,
ThreeComponentSequential,
IrfDispersion,
])
@pytest.mark.parametrize("nnls", [True, False])
def test_kinetic_model(suite, nnls):
Expand Down

0 comments on commit 9a7f23b

Please sign in to comment.