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

feature: Returns just dictionary when loading .topostats #112

Merged
merged 1 commit into from
Feb 4, 2025
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
13 changes: 6 additions & 7 deletions AFMReader/topostats.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

from __future__ import annotations
from pathlib import Path
from typing import Any

import h5py

Expand All @@ -11,7 +12,7 @@
logger.enable(__package__)


def load_topostats(file_path: Path | str) -> tuple:
def load_topostats(file_path: Path | str) -> dict[str, Any]:
"""
Extract image and pixel to nm scaling from the .topostats (HDF5 format) file.

Expand All @@ -22,9 +23,9 @@ def load_topostats(file_path: Path | str) -> tuple:

Returns
-------
tuple(np.ndarray, float)
A tuple containing the image, its pixel to nm scaling factor and the data dictionary
containing all the extra image data and metadata in dictionary format.
dict[str, Any]
A dictionary containing the image, its pixel to nm scaling factor and nested Numpy arrays representing the
analyses performed on the data.

Raises
------
Expand All @@ -45,12 +46,10 @@ def load_topostats(file_path: Path | str) -> tuple:
data["img_path"] = Path(data["img_path"])
file_version = data["topostats_file_version"]
logger.info(f"[{filename}] TopoStats file version : {file_version}")
image = data["image"]
pixel_to_nm_scaling = data["pixel_to_nm_scaling"]

except OSError as e:
if "Unable to open file" in str(e):
logger.error(f"[{filename}] File not found : {file_path}")
raise e

return (image, pixel_to_nm_scaling, data)
return data
22 changes: 8 additions & 14 deletions tests/test_topostats.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
from pathlib import Path
import pytest

import numpy as np

from AFMReader import topostats

Expand Down Expand Up @@ -55,27 +54,22 @@
def test_load_topostats(
file_name: str,
topostats_file_version: float,
image_shape: tuple[int, int],
image_shape: tuple,
pixel_to_nm_scaling: float,
data_keys: set[str],
image_sum: float,
) -> None:
"""Test the normal operation of loading a .topostats (HDF5 format) file."""
result_image = np.ndarray
result_pixel_to_nm_scaling = float
result_data = dict

file_path = RESOURCES / file_name
result_image, result_pixel_to_nm_scaling, result_data = topostats.load_topostats(file_path)
topostats_data = topostats.load_topostats(file_path)

assert result_pixel_to_nm_scaling == pixel_to_nm_scaling
assert isinstance(result_image, np.ndarray)
assert result_image.shape == image_shape
assert set(result_data.keys()) == data_keys # type: ignore
assert result_data["topostats_file_version"] == topostats_file_version
assert result_image.sum() == image_sum
assert set(topostats_data.keys()) == data_keys # type: ignore
assert topostats_data["topostats_file_version"] == topostats_file_version
assert topostats_data["pixel_to_nm_scaling"] == pixel_to_nm_scaling
assert topostats_data["image"].shape == image_shape
assert topostats_data["image"].sum() == image_sum
if topostats_file_version >= 0.2:
assert isinstance(result_data["img_path"], Path)
assert isinstance(topostats_data["img_path"], Path)


def test_load_topostats_file_not_found() -> None:
Expand Down