Skip to content
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: 11 additions & 2 deletions AFMReader/spm.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,18 @@ def spm_pixel_to_nm_scaling(filename: str, channel_data: pySPM.SPM.SPM_image) ->
px_to_real[0][0] * unit_dict[px_to_real[0][1]],
px_to_real[1][0] * unit_dict[px_to_real[1][1]],
)[0]
# ns-rse : Perhaps just switch to _always_ using the parameters from channel_data.size to calculate scaling?
if px_to_real[0][0] == 0 and px_to_real[1][0] == 0:
pixel_to_nm_scaling = 1
logger.info(f"[{filename}] : Pixel size not found in metadata, defaulting to 1nm")
logger.info(
f"[{filename}] : Pixel to nm scaling not directly available, calculating from 'channel_data.size['real']' "
"and 'channel_data.size['pixels']'."
)
pixel_to_nm_scaling = (
(channel_data.size["real"]["x"] / channel_data.size["pixels"]["x"])
/ unit_dict[channel_data.size["real"]["unit"]],
(channel_data.size["real"]["y"] / channel_data.size["pixels"]["y"])
/ unit_dict[channel_data.size["real"]["unit"]],
)[0]
logger.info(f"[{filename}] : Pixel to nm scaling : {pixel_to_nm_scaling}")
return pixel_to_nm_scaling

Expand Down
32 changes: 32 additions & 0 deletions tests/test_spm.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,38 @@ def test_load_spm(
assert result_image.sum() == pytest.approx(image_sum)


@patch("pySPM.SPM.SPM_image")
@pytest.mark.parametrize(
("filename", "size", "expected_px2nm"),
[
pytest.param(
"square",
{"pixels": {"x": 1024, "y": 1024}, "real": {"x": 505.859, "y": 505.859, "unit": "nm"}},
0.4940029296875,
id="square 0.494",
),
pytest.param(
"square",
{"pixels": {"x": 2048, "y": 2048}, "real": {"x": 505.859, "y": 505.859, "unit": "nm"}},
0.24700146484375,
id="square 0.247",
),
],
)
def test_spm_pixel_to_nm_scaling_(
mock_spm: "MagicMock",
filename: str,
size: dict[str, dict[str, int | str]],
expected_px2nm: float,
) -> None:
"""Test obtaining scaling directly when ``pixel_to_nm_scale`` attribute is zero."""
# Mock the pxs attribute to be zero which triggers derivation of sacling from the size attributes
mock_spm.pxs.return_value = [(0, "nm"), (0, "nm")]
mock_spm.size = size
result = spm.spm_pixel_to_nm_scaling(filename, mock_spm)
assert result == expected_px2nm


@patch("pySPM.SPM.SPM_image.pxs")
@pytest.mark.parametrize(
("filename", "unit", "x", "y", "expected_px2nm"),
Expand Down