diff --git a/AFMReader/stp.py b/AFMReader/stp.py index a3defe5..bb5a39e 100644 --- a/AFMReader/stp.py +++ b/AFMReader/stp.py @@ -12,6 +12,7 @@ # pylint: disable=too-many-locals +# pylint: disable=too-many-statements def load_stp( # noqa: C901 (ignore too complex) file_path: Path | str, header_encoding: str = "latin-1" ) -> tuple[np.ndarray, float]: @@ -69,14 +70,18 @@ def load_stp( # noqa: C901 (ignore too complex) if cols_match is None: raise ValueError(f"[{filename}] : 'cols' not found in file header.") cols = int(cols_match.group(1)) - x_real_size_match = re.search(r"X Amplitude: (\d+\.?\d*) nm", header_decoded) + x_real_size_match = re.search(r"X Amplitude: (\d+\.?\d*) (µm|nm)", header_decoded) if x_real_size_match is None: raise ValueError(f"[{filename}] : 'X Amplitude' not found in file header.") x_real_size = float(x_real_size_match.group(1)) - y_real_size_match = re.search(r"Y Amplitude: (\d+\.?\d*) nm", header_decoded) + x_units = x_real_size_match.group(2) + x_real_size = x_real_size * 1000 if x_units == "µm" else x_real_size + y_real_size_match = re.search(r"Y Amplitude: (\d+\.?\d*) (µm|nm)", header_decoded) if y_real_size_match is None: raise ValueError(f"[{filename}] : 'Y Amplitude' not found in file header.") y_real_size = float(y_real_size_match.group(1)) + y_units = y_real_size_match.group(2) + y_real_size = y_real_size * 1000 if y_units == "µm" else y_real_size if x_real_size != y_real_size: raise NotImplementedError( f"[{filename}] : X scan size (nm) does not equal Y scan size (nm) ({x_real_size}, {y_real_size})" diff --git a/AFMReader/top.py b/AFMReader/top.py index 75d76d0..e85dd2a 100644 --- a/AFMReader/top.py +++ b/AFMReader/top.py @@ -13,6 +13,7 @@ # pylint: disable=too-many-locals # pylint: disable=too-many-statements +# pylint: disable=too-many-branches def load_top( # noqa: C901 (ignore too complex) file_path: Path | str, header_encoding: str = "latin-1" ) -> tuple[np.ndarray, float]: @@ -70,14 +71,18 @@ def load_top( # noqa: C901 (ignore too complex) if cols_match is None: raise ValueError(f"[{filename}] : 'cols' not found in file header.") cols = int(cols_match.group(1)) - x_real_size_match = re.search(r"X Amplitude: (\d+\.?\d*) nm", header_decoded) + x_real_size_match = re.search(r"X Amplitude: (\d+\.?\d*) (µm|nm)", header_decoded) if x_real_size_match is None: raise ValueError(f"[{filename}] : 'X Amplitude' not found in file header.") x_real_size = float(x_real_size_match.group(1)) - y_real_size_match = re.search(r"Y Amplitude: (\d+\.?\d*) nm", header_decoded) + x_units = x_real_size_match.group(2) + x_real_size = x_real_size * 1000 if x_units == "µm" else x_real_size + y_real_size_match = re.search(r"Y Amplitude: (\d+\.?\d*) (µm|nm)", header_decoded) if y_real_size_match is None: raise ValueError(f"[{filename}] : 'Y Amplitude' not found in file header.") y_real_size = float(y_real_size_match.group(1)) + y_units = y_real_size_match.group(2) + y_real_size = y_real_size * 1000 if y_units == "µm" else y_real_size if x_real_size != y_real_size: raise NotImplementedError( f"[{filename}] : X scan size (nm) does not equal Y scan size (nm) ({x_real_size}, {y_real_size})" diff --git a/tests/resources/sample_1_um_scale.stp b/tests/resources/sample_1_um_scale.stp new file mode 100644 index 0000000..14e7d79 Binary files /dev/null and b/tests/resources/sample_1_um_scale.stp differ diff --git a/tests/resources/sample_1_um_scale.top b/tests/resources/sample_1_um_scale.top new file mode 100644 index 0000000..37b52e6 Binary files /dev/null and b/tests/resources/sample_1_um_scale.top differ diff --git a/tests/test_stp.py b/tests/test_stp.py index 897c365..8b2ffc4 100644 --- a/tests/test_stp.py +++ b/tests/test_stp.py @@ -19,7 +19,10 @@ "expected_image_dtype", "expected_image_sum", ), - [pytest.param("sample_0.stp", 0.9765625, (512, 512), float, -15070620.440757688)], + [ + pytest.param("sample_0.stp", 0.9765625, (512, 512), float, -15070620.440757688), + pytest.param("sample_1_um_scale.stp", 3.90625, (512, 512), float, -14439959.0625), + ], ) def test_load_stp( file_name: str, diff --git a/tests/test_top.py b/tests/test_top.py index 6eef2bf..6318f93 100644 --- a/tests/test_top.py +++ b/tests/test_top.py @@ -19,7 +19,10 @@ "expected_image_dtype", "expected_image_sum", ), - [pytest.param("sample_0.top", 0.9765625, (512, 512), float, 6034386.429246264)], + [ + pytest.param("sample_0.top", 0.9765625, (512, 512), float, 6034386.429246264), + pytest.param("sample_1_um_scale.top", 3.90625, (512, 512), float, 125175.99999999997), + ], ) def test_load_top( file_name: str,