Skip to content

chore: Update output data validations for frames and ctf #484

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

Merged
merged 20 commits into from
May 7, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
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
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,17 @@
TILT_AXIS_ANGLE_REGEX = re.compile(r".*tilt\s*axis\s*angle\s*=\s*([-+]?(?:\d*\.*\d+))")



@pytest.fixture
def mdoc_tilt_axis_angle(mdoc_data: pd.DataFrame) -> float:
# To convert the data from the mdoc into a data frame, all the global records are added to each section's data
titles = mdoc_data["titles"][0]
for title in titles:
if result := re.match(TILT_AXIS_ANGLE_REGEX, title.lower()):
return float(result[1])
pytest.fail("No Tilt axis angle found")


class TiltSeriesHelper(HelperTestMRCZarrHeader):

@pytest.fixture(autouse=True)
Expand All @@ -25,15 +36,6 @@ def tiltseries_metadata_range(self, tiltseries_metadata: dict) -> list[float]:
tiltseries_metadata["tilt_step"],
).tolist()

@pytest.fixture
def mdoc_tilt_axis_angle(self, mdoc_data: pd.DataFrame) -> float:
# To convert the data from the mdoc into a data frame, all the global records are added to each section's data
titles = mdoc_data["titles"][0]
for title in titles:
if result := re.match(TILT_AXIS_ANGLE_REGEX, title.lower()):
return float(result[1])
pytest.fail("No Tilt axis angle found")

@allure.title("Tiltseries: tilt axis angle in mdoc file matches that in tilt series metadata (+/- 10 deg).")
def test_tilt_axis_angle(self, mdoc_tilt_axis_angle: float, tiltseries_metadata: dict[str, Any]):
metadata_tilt_axis = tiltseries_metadata["tilt_axis"]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ def frames_meta_file(frames_dir: str, filesystem: FileSystemApi) -> str:
if filesystem.exists(dst):
return dst
else:
pytest.fail(f"Frames metadata file not found: {dst}")
pytest.fail(f"The frames directory exists, but frames_metadata.json is not found: {dst}")


@pytest.fixture(scope="session")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,29 @@
from data_validation.shared.helper.angles_helper import helper_angles_injection_errors


def matrix_to_angle(matrix: list[list[float]]) -> float:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@uermel Could you sanity check if this is the formula we should be using for our use case?

"""
Converts a 2x2 rotation matrix into an angle in degrees.

Args:
matrix: A 2x2 list of floats representing the rotation matrix.

Returns:
The rotation angle in degrees.
"""
# Extract the elements of the matrix
r00, r01 = matrix[0]
r10, r11 = matrix[1]

# Calculate the angle in radians using atan2
angle_radians = np.arctan2(r10, r00)

# Convert the angle to degrees
angle_degrees = np.rad2deg(angle_radians)

return angle_degrees


@pytest.mark.alignment
@pytest.mark.parametrize("dataset, run_name, aln_dir", pytest.cryoet.dataset_run_alignment_combinations, scope="session")
class TestAlignments:
Expand Down Expand Up @@ -91,19 +114,18 @@ def test_tilt_tiltseries_range(
+ f"\nRange: {alignment_tiltseries_metadata['tilt_range']['min']} to {alignment_tiltseries_metadata['tilt_range']['max']}, with step {alignment_tiltseries_metadata['tilt_step']}"
)

@allure.title("Alignment: tilt axis angle in mdoc file matches that in the alignment metadata [per_section_alignment_parameters.in_plane_rotation] (+/- 10 deg)")
def test_mdoc_tilt_axis_angle_in_alignment_per_section_alignment_parameters(self, mdoc_data: pd.DataFrame, alignment_tiltseries_metadata: dict[str, dict]):
per_section_alignment_parameters = alignment_tiltseries_metadata.get("per_section_alignment_parameters")
@allure.title("Alignment: tilt angle in mdoc file matches that in the alignment metadata [per_section_alignment_parameters.in_plane_rotation] (+/- 10 deg)")
def test_mdoc_tilt_axis_angle_in_alignment_per_section_alignment_parameters(self, mdoc_tilt_axis_angle: float, alignment_metadata: dict[str, dict]):
per_section_alignment_parameters = alignment_metadata.get("per_section_alignment_parameters")
if not per_section_alignment_parameters:
pytest.skip("Alignment metadata missing per_section_alignment_parameters.")
errors = []
for i, psap in enumerate(per_section_alignment_parameters):
if (in_plane_rotation := psap.get("in_plane_rotation")) is not None:
try:
assert abs(mdoc_data["TiltAxisAngle"].iloc[0] - in_plane_rotation) < 10, \
f"Tilt axis angle in mdoc file {mdoc_data['TiltAxisAngle'].iloc[0]} does not match alignment metadata['per_section_alignment_parameter'][{i}]['tilt_angle']: {in_plane_rotation}"
except AssertionError as e:
errors.append(str(e))
assert not errors, "\n".join(errors)
# convert all in_plane_rotation angles to degrees and sort them in ascending order
in_plane_rotations = [matrix_to_angle(psap["in_plane_rotation"]) for psap in per_section_alignment_parameters]
# check that all in_plane_rotation angles are equal
assert len(set(in_plane_rotations)) == 1, "in_plane_rotation angles are not all equal."
# check that in_plane_roation against mdoc_tilt_axis_angle
in_plane_rotation = in_plane_rotations[0]
assert in_plane_rotation == pytest.approx(mdoc_tilt_axis_angle, rel=10), f"Mdoc tilt axis angle {mdoc_tilt_axis_angle} does not match alignment metadata['per_section_alignment_parameters'][*]['in_plane_rotation']: {in_plane_rotation}"


### END Tiltseries consistency tests ###
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,13 @@ def test_is_gain_corrected_false(self,
frame_metadata: Dict,
gain_headers: Dict[str, Union[List[tifffile.TiffPage], MrcInterpreter]], # this is skipped if it is not found
):
if frame_metadata.get("is_gain_corrected") is False: # todo is none considered false?
if not frame_metadata.get("is_gain_corrected"):
assert len(gain_headers) > 0

@allure.title("Frames: max(acquisitionOrder) <= number of frames -1")
def test_max_acquisition_order(self, frame_metadata: Dict):
acquisition_order_max = max(f.get("acquisition_order", 0) for f in frame_metadata["frames"])
assert acquisition_order_max <= len(frame_metadata["frames"]) -1
assert acquisition_order_max <= len(frame_metadata["frames"]) - 1

@allure.title("Frames: Sorting acquisitionOrder low-to-high and accumulatedDose low-to-high results in the same order")
def test_sorting_acquisition_order_and_accumulated_dose(self, frame_metadata: Dict):
Expand Down