Skip to content

Commit 0996cdc

Browse files
committed
feature: handle newer topostats file versions
Restructuring of TopoStats to define classes means the `TopoStats` object now holds the `topostats_version` rather than `topostats_file_version`. This commit allows both to be handled and switches to using [`packging.version`](https://packaging.pypa.io/en/stable/version.html) to do so which ensures a more consistent approach to comparing version numbers. Yet to write a test for working with newer `.topostats` where `topostats_version >= 2.3.2` as work is still on-going but parameterised test is in place for when work is complete.
1 parent 3e85533 commit 0996cdc

3 files changed

Lines changed: 13 additions & 5 deletions

File tree

.pylintrc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ py-version=3.9
6262

6363
# When enabled, pylint would attempt to guess common misconfiguration and emit
6464
# user-friendly hints instead of false-positive error messages.
65-
suggestion-mode=yes
65+
# suggestion-mode=yes
6666

6767
# Allow loading of arbitrary C extensions. Extensions are imported into the
6868
# active Python interpreter and may run arbitrary code.

AFMReader/topostats.py

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
import h5py
77

8+
from packaging.version import parse as parse_version
89
from AFMReader.io import unpack_hdf5
910
from AFMReader.logging import logger
1011

@@ -41,10 +42,17 @@ def load_topostats(file_path: Path | str) -> dict[str, Any]:
4142
try:
4243
with h5py.File(file_path, "r") as f:
4344
data = unpack_hdf5(open_hdf5_file=f, group_path="/")
44-
if str(data["topostats_file_version"]) >= "0.2":
45+
# Handle different names for variables holding the file version (<=0.3) or the newer topostats version
46+
version = (
47+
data["topostats_file_version"]
48+
if "topostats_file_version" in data.keys() # pylint: disable=consider-iterating-dictionary
49+
else data["topostats_version"]
50+
)
51+
print(f"\n{str(version)=}\n")
52+
print(f'\n{parse_version(str(version)) >= parse_version("0.2")=}\n')
53+
if parse_version(str(version)) > parse_version("0.2"):
4554
data["img_path"] = Path(data["img_path"])
46-
file_version = data["topostats_file_version"]
47-
logger.info(f"[{filename}] TopoStats file version : {file_version}")
55+
logger.info(f"[{filename}] TopoStats file version : {version}")
4856

4957
except OSError as e:
5058
if "Unable to open file" in str(e):

tests/test_topostats.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ def test_load_topostats(
9999
assert topostats_data["pixel_to_nm_scaling"] == pytest.approx(pixel_to_nm_scaling)
100100
assert topostats_data["image"].shape == image_shape
101101
assert topostats_data["image"].sum() == pytest.approx(image_sum)
102-
if version >= "0.2":
102+
if version > "0.2":
103103
assert isinstance(topostats_data["img_path"], Path)
104104

105105

0 commit comments

Comments
 (0)