Skip to content

Commit f6fc37d

Browse files
authored
Merge pull request #181 from AFM-SPM/SylviaWhittle/stop-default-1nm-p2nm
sum loading: Stop defaulting to 1nm/px when pixel to nm scaling cannot be found. Instead we calculate this directly from the `SPM.size` attribute which contains the and `pixels` and `real` which tells us the number of pixels and number of units and the unit type.
2 parents 62e9502 + 2045727 commit f6fc37d

2 files changed

Lines changed: 43 additions & 2 deletions

File tree

AFMReader/spm.py

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,18 @@ def spm_pixel_to_nm_scaling(filename: str, channel_data: pySPM.SPM.SPM_image) ->
3838
px_to_real[0][0] * unit_dict[px_to_real[0][1]],
3939
px_to_real[1][0] * unit_dict[px_to_real[1][1]],
4040
)[0]
41+
# ns-rse : Perhaps just switch to _always_ using the parameters from channel_data.size to calculate scaling?
4142
if px_to_real[0][0] == 0 and px_to_real[1][0] == 0:
42-
pixel_to_nm_scaling = 1
43-
logger.info(f"[{filename}] : Pixel size not found in metadata, defaulting to 1nm")
43+
logger.info(
44+
f"[{filename}] : Pixel to nm scaling not directly available, calculating from 'channel_data.size['real']' "
45+
"and 'channel_data.size['pixels']'."
46+
)
47+
pixel_to_nm_scaling = (
48+
(channel_data.size["real"]["x"] / channel_data.size["pixels"]["x"])
49+
/ unit_dict[channel_data.size["real"]["unit"]],
50+
(channel_data.size["real"]["y"] / channel_data.size["pixels"]["y"])
51+
/ unit_dict[channel_data.size["real"]["unit"]],
52+
)[0]
4453
logger.info(f"[{filename}] : Pixel to nm scaling : {pixel_to_nm_scaling}")
4554
return pixel_to_nm_scaling
4655

tests/test_spm.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,38 @@ def test_load_spm(
4646
assert result_image.sum() == pytest.approx(image_sum)
4747

4848

49+
@patch("pySPM.SPM.SPM_image")
50+
@pytest.mark.parametrize(
51+
("filename", "size", "expected_px2nm"),
52+
[
53+
pytest.param(
54+
"square",
55+
{"pixels": {"x": 1024, "y": 1024}, "real": {"x": 505.859, "y": 505.859, "unit": "nm"}},
56+
0.4940029296875,
57+
id="square 0.494",
58+
),
59+
pytest.param(
60+
"square",
61+
{"pixels": {"x": 2048, "y": 2048}, "real": {"x": 505.859, "y": 505.859, "unit": "nm"}},
62+
0.24700146484375,
63+
id="square 0.247",
64+
),
65+
],
66+
)
67+
def test_spm_pixel_to_nm_scaling_(
68+
mock_spm: "MagicMock",
69+
filename: str,
70+
size: dict[str, dict[str, int | str]],
71+
expected_px2nm: float,
72+
) -> None:
73+
"""Test obtaining scaling directly when ``pixel_to_nm_scale`` attribute is zero."""
74+
# Mock the pxs attribute to be zero which triggers derivation of sacling from the size attributes
75+
mock_spm.pxs.return_value = [(0, "nm"), (0, "nm")]
76+
mock_spm.size = size
77+
result = spm.spm_pixel_to_nm_scaling(filename, mock_spm)
78+
assert result == expected_px2nm
79+
80+
4981
@patch("pySPM.SPM.SPM_image.pxs")
5082
@pytest.mark.parametrize(
5183
("filename", "unit", "x", "y", "expected_px2nm"),

0 commit comments

Comments
 (0)