Skip to content

Commit ac06413

Browse files
Merge pull request #145 from AFM-SPM/SylviaWhittle/stp-top-µm-support
Courtesy of Eva: Add µm support for .top, .stp files
2 parents 6f1331b + ceed2e6 commit ac06413

File tree

6 files changed

+22
-6
lines changed

6 files changed

+22
-6
lines changed

AFMReader/stp.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212

1313

1414
# pylint: disable=too-many-locals
15+
# pylint: disable=too-many-statements
1516
def load_stp( # noqa: C901 (ignore too complex)
1617
file_path: Path | str, header_encoding: str = "latin-1"
1718
) -> tuple[np.ndarray, float]:
@@ -69,14 +70,18 @@ def load_stp( # noqa: C901 (ignore too complex)
6970
if cols_match is None:
7071
raise ValueError(f"[{filename}] : 'cols' not found in file header.")
7172
cols = int(cols_match.group(1))
72-
x_real_size_match = re.search(r"X Amplitude: (\d+\.?\d*) nm", header_decoded)
73+
x_real_size_match = re.search(r"X Amplitude: (\d+\.?\d*) (µm|nm)", header_decoded)
7374
if x_real_size_match is None:
7475
raise ValueError(f"[{filename}] : 'X Amplitude' not found in file header.")
7576
x_real_size = float(x_real_size_match.group(1))
76-
y_real_size_match = re.search(r"Y Amplitude: (\d+\.?\d*) nm", header_decoded)
77+
x_units = x_real_size_match.group(2)
78+
x_real_size = x_real_size * 1000 if x_units == "µm" else x_real_size
79+
y_real_size_match = re.search(r"Y Amplitude: (\d+\.?\d*) (µm|nm)", header_decoded)
7780
if y_real_size_match is None:
7881
raise ValueError(f"[{filename}] : 'Y Amplitude' not found in file header.")
7982
y_real_size = float(y_real_size_match.group(1))
83+
y_units = y_real_size_match.group(2)
84+
y_real_size = y_real_size * 1000 if y_units == "µm" else y_real_size
8085
if x_real_size != y_real_size:
8186
raise NotImplementedError(
8287
f"[{filename}] : X scan size (nm) does not equal Y scan size (nm) ({x_real_size}, {y_real_size})"

AFMReader/top.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
# pylint: disable=too-many-locals
1515
# pylint: disable=too-many-statements
16+
# pylint: disable=too-many-branches
1617
def load_top( # noqa: C901 (ignore too complex)
1718
file_path: Path | str, header_encoding: str = "latin-1"
1819
) -> tuple[np.ndarray, float]:
@@ -70,14 +71,18 @@ def load_top( # noqa: C901 (ignore too complex)
7071
if cols_match is None:
7172
raise ValueError(f"[{filename}] : 'cols' not found in file header.")
7273
cols = int(cols_match.group(1))
73-
x_real_size_match = re.search(r"X Amplitude: (\d+\.?\d*) nm", header_decoded)
74+
x_real_size_match = re.search(r"X Amplitude: (\d+\.?\d*) (µm|nm)", header_decoded)
7475
if x_real_size_match is None:
7576
raise ValueError(f"[{filename}] : 'X Amplitude' not found in file header.")
7677
x_real_size = float(x_real_size_match.group(1))
77-
y_real_size_match = re.search(r"Y Amplitude: (\d+\.?\d*) nm", header_decoded)
78+
x_units = x_real_size_match.group(2)
79+
x_real_size = x_real_size * 1000 if x_units == "µm" else x_real_size
80+
y_real_size_match = re.search(r"Y Amplitude: (\d+\.?\d*) (µm|nm)", header_decoded)
7881
if y_real_size_match is None:
7982
raise ValueError(f"[{filename}] : 'Y Amplitude' not found in file header.")
8083
y_real_size = float(y_real_size_match.group(1))
84+
y_units = y_real_size_match.group(2)
85+
y_real_size = y_real_size * 1000 if y_units == "µm" else y_real_size
8186
if x_real_size != y_real_size:
8287
raise NotImplementedError(
8388
f"[{filename}] : X scan size (nm) does not equal Y scan size (nm) ({x_real_size}, {y_real_size})"

tests/resources/sample_1_um_scale.stp

2.01 MB
Binary file not shown.

tests/resources/sample_1_um_scale.top

515 KB
Binary file not shown.

tests/test_stp.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,10 @@
1919
"expected_image_dtype",
2020
"expected_image_sum",
2121
),
22-
[pytest.param("sample_0.stp", 0.9765625, (512, 512), float, -15070620.440757688)],
22+
[
23+
pytest.param("sample_0.stp", 0.9765625, (512, 512), float, -15070620.440757688),
24+
pytest.param("sample_1_um_scale.stp", 3.90625, (512, 512), float, -14439959.0625),
25+
],
2326
)
2427
def test_load_stp(
2528
file_name: str,

tests/test_top.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,10 @@
1919
"expected_image_dtype",
2020
"expected_image_sum",
2121
),
22-
[pytest.param("sample_0.top", 0.9765625, (512, 512), float, 6034386.429246264)],
22+
[
23+
pytest.param("sample_0.top", 0.9765625, (512, 512), float, 6034386.429246264),
24+
pytest.param("sample_1_um_scale.top", 3.90625, (512, 512), float, 125175.99999999997),
25+
],
2326
)
2427
def test_load_top(
2528
file_name: str,

0 commit comments

Comments
 (0)