Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improvements to application of clp_penalties (equal area) #801

Merged
merged 2 commits into from
Sep 4, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion glotaran/analysis/problem_grouped.py
Original file line number Diff line number Diff line change
Expand Up @@ -293,7 +293,12 @@ def calculate_residual(self):
self._weighted_residuals = list(map(lambda result: result[2], results))
self._residuals = list(map(lambda result: result[3], results))
self._additional_penalty = calculate_clp_penalties(
self.model, self.parameters, self._clp_labels, self._grouped_clps, self._full_axis
self.model,
self.parameters,
self._clp_labels,
self._grouped_clps,
self._full_axis,
self.dataset_models,
)

return self._reduced_clps, self._clps, self._weighted_residuals, self._residuals
Expand Down
7 changes: 6 additions & 1 deletion glotaran/analysis/problem_ungrouped.py
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,12 @@ def _calculate_residual(self, label: str, dataset_model: DatasetModel):

clp_labels = self._get_clp_labels(label)
additional_penalty = calculate_clp_penalties(
self.model, self.parameters, clp_labels, self._clps[label], global_axis
self.model,
self.parameters,
clp_labels,
self._clps[label],
global_axis,
self.dataset_models,
)
if additional_penalty.size != 0:
self._additional_penalty.append(additional_penalty)
Expand Down
61 changes: 43 additions & 18 deletions glotaran/analysis/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -194,27 +194,48 @@ def calculate_clp_penalties(
clp_labels: list[list[str]] | list[str],
clps: list[np.ndarray],
global_axis: np.ndarray,
dataset_models: dict[str, DatasetModel],
) -> np.ndarray:

# TODO: make a decision on how to handle clp_penalties per dataset
# 1. sum up contributions per dataset on each dataset_axis (v0.4.1)
# 2. sum up contributions on the global_axis (future?)

penalties = []
for penalty in model.clp_area_penalties:
penalty = penalty.fill(model, parameters)
source_area = _get_area(
penalty.source,
clp_labels,
clps,
penalty.source_intervals,
global_axis,
)

target_area = _get_area(
penalty.target,
clp_labels,
clps,
penalty.target_intervals,
global_axis,
)

source_area = np.array([])
target_area = np.array([])
for _, dataset_model in dataset_models.items():
dataset_axis = dataset_model.get_global_axis()

source_area = np.concatenate(
[
source_area,
_get_area(
penalty.source,
clp_labels,
clps,
penalty.source_intervals,
global_axis,
dataset_axis,
),
]
)

target_area = np.concatenate(
[
target_area,
_get_area(
penalty.target,
clp_labels,
clps,
penalty.target_intervals,
global_axis,
dataset_axis,
),
]
)
area_penalty = np.abs(np.sum(source_area) - penalty.parameter * np.sum(target_area))

penalties.append(area_penalty * penalty.weight)
Expand All @@ -228,14 +249,18 @@ def _get_area(
clps: list[np.ndarray],
intervals: list[tuple[float, float]],
global_axis: np.ndarray,
dataset_axis: np.ndarray,
) -> np.ndarray:
area = []

for interval in intervals:
if interval[0] > global_axis[-1]:
continue

start_idx, end_idx = get_idx_from_interval(interval, global_axis)
bounded_interval = (
max(interval[0], np.min(dataset_axis)),
min(interval[1], np.max(dataset_axis)),
)
start_idx, end_idx = get_idx_from_interval(bounded_interval, global_axis)
for i in range(start_idx, end_idx + 1):
index_clp_labels = clp_labels[i] if isinstance(clp_labels[0], list) else clp_labels
if clp_label in index_clp_labels:
Expand Down
1 change: 1 addition & 0 deletions glotaran/model/clp_penalties.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ def apply_spectral_penalties(
group_tolerance: float,
) -> np.ndarray:

# TODO: seems to duplicate calculate_clp_penalties
penalties = []
for penalty in model.clp_area_penalties:

Expand Down