Skip to content

Commit

Permalink
new test for testing refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
tjlane committed Nov 4, 2024
1 parent 736f55c commit cfae8e8
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 13 deletions.
9 changes: 4 additions & 5 deletions meteor/testing.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import numpy as np

from .rsmap import Map
from .settings import MAP_SAMPLING


@dataclass
Expand All @@ -27,11 +28,9 @@ def assert_phases_allclose(array1: np.ndarray, array2: np.ndarray, atol: float =
raise AssertionError(msg)


# TODO: test
def rms_between_coefficients(map1: Map, map2: Map) -> float:
map_sampling = 3
map1_array = np.array(map1.to_ccp4_map(map_sampling=map_sampling).grid)
map2_array = np.array(map2.to_ccp4_map(map_sampling=map_sampling).grid)
def diffmap_realspace_rms(map1: Map, map2: Map) -> float:
map1_array = np.array(map1.to_ccp4_map(map_sampling=MAP_SAMPLING).grid)
map2_array = np.array(map2.to_ccp4_map(map_sampling=MAP_SAMPLING).grid)

# standardize
map1_array /= map1_array.std()
Expand Down
17 changes: 11 additions & 6 deletions test/unit/test_iterative.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
iterative_tv_phase_retrieval,
)
from meteor.rsmap import Map
from meteor.testing import rms_between_coefficients
from meteor.testing import diffmap_realspace_rms
from meteor.validate import map_negentropy


Expand Down Expand Up @@ -45,9 +45,14 @@ def test_tv_denoise_complex_difference_sf() -> None:

def test_iterative_tv_different_indices(noise_free_map: Map, very_noisy_map: Map) -> None:
# regression test to make sure we can accept maps with different indices
hkl = [(1, 2),] * 3,
labels = pd.MultiIndex.from_arrays(hkl, names=("H", "K", "L"))

labels = pd.MultiIndex.from_arrays(
[
(1, 2),
]
* 3,
names=("H", "K", "L"),
)

n = len(very_noisy_map)
very_noisy_map.drop(labels, inplace=True)
assert len(very_noisy_map) == n - 2
Expand Down Expand Up @@ -79,8 +84,8 @@ def test_iterative_tv(noise_free_map: Map, very_noisy_map: Map) -> None:
assert isinstance(metadata, pd.DataFrame)

# test correctness by comparing denoised dataset to noise-free
noisy_error = rms_between_coefficients(very_noisy_map, noise_free_map)
denoised_error = rms_between_coefficients(denoised_map, noise_free_map)
noisy_error = diffmap_realspace_rms(very_noisy_map, noise_free_map)
denoised_error = diffmap_realspace_rms(denoised_map, noise_free_map)

# insist on 1% or better improvement
assert 1.01 * denoised_error < noisy_error
Expand Down
14 changes: 14 additions & 0 deletions test/unit/test_testing.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import pytest

from meteor import testing as mt
from meteor.rsmap import Map


def test_map_columns_smoke() -> None:
Expand All @@ -21,6 +22,19 @@ def test_phases_allclose() -> None:
mt.assert_phases_allclose(close1, far)


def test_diffmap_realspace_rms(noise_free_map: Map) -> None:
assert mt.diffmap_realspace_rms(noise_free_map, noise_free_map) == 0.0

map2 = noise_free_map.copy()
map2 += 1
map3 = noise_free_map.copy()
map3 += 2

dist12 = mt.diffmap_realspace_rms(noise_free_map, map2)
dist13 = mt.diffmap_realspace_rms(noise_free_map, map3)
assert dist13 > dist12


def test_single_carbon_structure_smoke() -> None:
carbon_position = (4.0, 5.0, 6.0)
space_group = gemmi.find_spacegroup_by_name("P212121")
Expand Down
4 changes: 2 additions & 2 deletions test/unit/test_tv.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

from meteor import tv
from meteor.rsmap import Map
from meteor.testing import rms_between_coefficients
from meteor.testing import diffmap_realspace_rms
from meteor.validate import map_negentropy

DEFAULT_WEIGHTS_TO_SCAN = np.logspace(-2, 0, 25)
Expand Down Expand Up @@ -88,7 +88,7 @@ def test_tv_denoise_map(
noisy_map: Map,
) -> None:
def rms_to_noise_free(test_map: Map) -> float:
return rms_between_coefficients(test_map, noise_free_map)
return diffmap_realspace_rms(test_map, noise_free_map)

# 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.
Expand Down

0 comments on commit cfae8e8

Please sign in to comment.