Skip to content

Commit 137d298

Browse files
authored
Merge pull request #87 from AFM-SPM/ns-rse/86-spm_px_to_nm_scaling-test
2 parents 5808a82 + f4f6c9d commit 137d298

2 files changed

Lines changed: 50 additions & 3 deletions

File tree

tests/conftest.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
"""Fixtures for testing."""
2+
3+
from pathlib import Path
4+
5+
import pySPM
6+
import pytest
7+
8+
BASE_DIR = Path.cwd()
9+
RESOURCES = BASE_DIR / "tests" / "resources"
10+
11+
12+
@pytest.fixture()
13+
def spm_channel_data() -> pySPM.SPM.SPM_image:
14+
"""Instantiate channel data from a LoadScans object."""
15+
scan = pySPM.Bruker(RESOURCES / "sample_0.spm")
16+
return scan.get_channel("Height")

tests/test_spm.py

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,19 @@
11
"""Test the loading of spm files."""
22

33
from pathlib import Path
4-
import pytest
4+
from unittest.mock import patch
55

66
import numpy as np
7+
import pySPM
8+
import pytest
79

8-
from AFMReader.spm import load_spm
10+
from AFMReader import spm
911

1012
BASE_DIR = Path.cwd()
1113
RESOURCES = BASE_DIR / "tests" / "resources"
1214

15+
# pylint: disable=too-many-positional-arguments
16+
1317

1418
@pytest.mark.parametrize(
1519
("file_name", "channel", "pixel_to_nm_scaling", "image_shape", "image_dtype", "image_sum"),
@@ -32,10 +36,37 @@ def test_load_spm(
3236
result_pixel_to_nm_scaling = float
3337

3438
file_path = RESOURCES / file_name
35-
result_image, result_pixel_to_nm_scaling = load_spm(file_path, channel=channel)
39+
result_image, result_pixel_to_nm_scaling = spm.load_spm(file_path, channel=channel)
3640

3741
assert result_pixel_to_nm_scaling == pixel_to_nm_scaling
3842
assert isinstance(result_image, np.ndarray)
3943
assert result_image.shape == image_shape
4044
assert result_image.dtype == image_dtype
4145
assert result_image.sum() == image_sum
46+
47+
48+
@patch("pySPM.SPM.SPM_image.pxs")
49+
@pytest.mark.parametrize(
50+
("filename", "unit", "x", "y", "expected_px2nm"),
51+
[
52+
pytest.param("square_mm", "mm", 0.01, 0.01, 10000, id="mm units; square"),
53+
pytest.param("square_um", "um", 1.5, 1.5, 1500, id="um units; square"),
54+
pytest.param("square_nm", "nm", 50, 50, 50, id="nm units; square"),
55+
pytest.param("square_pm", "pm", 233, 233, 0.233, id="pm units; square"),
56+
pytest.param("rectangle_thin_pm", "pm", 1, 512, 0.001, id="pm units; rectangular (thin)"),
57+
pytest.param("rectangle_tall_pm", "pm", 512, 1, 0.512, id="pm units; rectangular (tall)"),
58+
],
59+
)
60+
def test__spm_pixel_to_nm_scaling(
61+
mock_pxs,
62+
spm_channel_data: pySPM.SPM.SPM_image,
63+
filename: str,
64+
unit: str,
65+
x: int,
66+
y: int,
67+
expected_px2nm: float,
68+
) -> None:
69+
"""Test extraction of pixels to nanometer scaling."""
70+
mock_pxs.return_value = [(x, unit), (y, unit)] # issue is that pxs is a func that returns the data
71+
result = spm.spm_pixel_to_nm_scaling(filename, spm_channel_data)
72+
assert result == expected_px2nm

0 commit comments

Comments
 (0)