Skip to content

Commit

Permalink
yes golden works
Browse files Browse the repository at this point in the history
  • Loading branch information
tjlane committed Aug 25, 2024
1 parent 8deccf2 commit 5973c0f
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 14 deletions.
2 changes: 1 addition & 1 deletion meteor/tv.py
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ def negentropy_objective(tv_lambda: float):
)
assert optimizer_result.success, "Golden minimization failed to find optimal TV lambda"
optimal_lambda = optimizer_result.x
optimal_negentropy = negentropy_objective(optimal_lambda)
optimal_negentropy = -1.0 * negentropy_objective(optimal_lambda)

# denoise using the optimized parameters and convert to an rs.DataSet
final_map = _tv_denoise_array(map_as_array=difference_map_as_array, weight=optimal_lambda)
Expand Down
29 changes: 16 additions & 13 deletions test/unit/test_tv.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,12 @@
from meteor import tv
from meteor.utils import MapLabels, compute_map_from_coefficients

LAMBDAS_TO_SCAN = np.logspace(-3, 1, 50)
DEFAULT_LAMBDA_VALUES_TO_SCAN = np.logspace(-2, 0, 30)


def rms_between_coefficients(ds1: rs.DataSet, ds2: rs.DataSet, diffmap_labels: MapLabels, map_sampling: int = 3) -> float:
def rms_between_coefficients(
ds1: rs.DataSet, ds2: rs.DataSet, diffmap_labels: MapLabels, map_sampling: int = 3
) -> float:
map1 = compute_map_from_coefficients(
map_coefficients=ds1,
amplitude_label=diffmap_labels.amplitude,
Expand All @@ -27,7 +29,7 @@ def rms_between_coefficients(ds1: rs.DataSet, ds2: rs.DataSet, diffmap_labels: M
map1_array = np.array(map1.grid)
map2_array = np.array(map2.grid)

# standardize -- TODO better to scale? think...
# standardize
map1_array /= map1_array.std()
map2_array /= map2_array.std()

Expand Down Expand Up @@ -63,28 +65,30 @@ def test_tv_denoise_difference_map_smoke(
else:
assert isinstance(output, rs.DataSet)

# TODO re-implement None
@pytest.mark.parametrize("lambda_values_to_scan", [LAMBDAS_TO_SCAN,])

@pytest.mark.parametrize("lambda_values_to_scan", [None, DEFAULT_LAMBDA_VALUES_TO_SCAN])
def test_tv_denoise_difference_map(
lambda_values_to_scan: None | Sequence[float],
noise_free_map: rs.DataSet,
noisy_map: rs.DataSet,
diffmap_labels: MapLabels,
) -> None:

def rms_to_noise_free(test_map: rs.DataSet) -> float:
return rms_between_coefficients(test_map, noise_free_map, diffmap_labels)

# Normally, the `tv_denoise_difference_map` function only returns the best result -- since we
# Normally, the `tv_denoise_difference_map` function only returns the best result -- since we
# know the ground truth, work around this to test all possible results.

lowest_rms: float = np.inf
best_lambda: float = 0.0

for trial_lambda in LAMBDAS_TO_SCAN:
denoised_map = tv.tv_denoise_difference_map(
for trial_lambda in DEFAULT_LAMBDA_VALUES_TO_SCAN:
denoised_map, result = tv.tv_denoise_difference_map(
difference_map_coefficients=noisy_map,
lambda_values_to_scan=[trial_lambda,]
lambda_values_to_scan=[
trial_lambda,
],
full_output=True,
)
rms = rms_to_noise_free(denoised_map)
if rms < lowest_rms:
Expand All @@ -101,5 +105,4 @@ def rms_to_noise_free(test_map: rs.DataSet) -> float:

rms_after_denoising = rms_to_noise_free(denoised_map)
assert rms_after_denoising < rms_to_noise_free(noisy_map)
np.testing.assert_approx_equal(result.optimal_lambda, best_lambda)
np.testing.assert_approx_equal(rms_after_denoising, lowest_rms)
np.testing.assert_allclose(result.optimal_lambda, best_lambda, rtol=0.2)

0 comments on commit 5973c0f

Please sign in to comment.