Skip to content

Commit 409d83c

Browse files
committed
fix caching issue.
With the same scan_file the loader due to caching gave back a mutable ScanImage. Moved caching to only the load Surface from scan-file, but makes every load a new ScanImage object.
1 parent bb4c53f commit 409d83c

2 files changed

Lines changed: 12 additions & 4 deletions

File tree

packages/scratch-core/src/parsers/loaders.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,14 @@
2222

2323

2424
@lru_cache(maxsize=1)
25+
def _load_surface(scan_file: Path) -> Surface:
26+
"""Cache the scan file to a Surface
27+
:param scan_file: The path to the file containing the scanned image data.
28+
:returns: An instance of `Surface`.
29+
"""
30+
return Surface.load(scan_file)
31+
32+
2533
@log_railway_function(
2634
"Failed to load image file",
2735
"Successfully loaded scan file",
@@ -33,7 +41,7 @@ def load_scan_image(scan_file: Path) -> ScanImage:
3341
:param scan_file: The path to the file containing the scanned image data.
3442
:returns: An instance of `ScanImage`.
3543
"""
36-
surface = Surface.load(scan_file)
44+
surface = _load_surface(scan_file)
3745
data = np.asarray(surface.data, dtype=np.float64) * micro
3846
step_x = surface.step_x * micro
3947
step_y = surface.step_y * micro

packages/scratch-core/tests/parsers/test_loaders.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
from container_models.scan_image import ScanImage
1212
from parsers import load_scan_image, subsample_scan_image
13-
from parsers.loaders import make_isotropic
13+
from parsers.loaders import make_isotropic, _load_surface
1414

1515
from ..helper_function import unwrap_result
1616

@@ -54,7 +54,7 @@ class FakeSurfaceTwo:
5454

5555
@pytest.fixture(autouse=True)
5656
def empty_cache_for_test(self):
57-
load_scan_image.cache_clear()
57+
_load_surface.cache_clear()
5858
yield
5959

6060
def test_load_scan_image_is_cached(self, tmp_path: Path) -> None:
@@ -91,7 +91,7 @@ def test_load_scan_image_only_caches_one_image(self, tmp_path: Path) -> None:
9191
_image_3 = load_scan_image(scan_file_2)
9292

9393
# Assert
94-
info = load_scan_image.cache_info()
94+
info = _load_surface.cache_info()
9595
assert info.hits == 1, "one cache hit expected"
9696
assert info.misses == 2, "two different files loaded"
9797
assert info.currsize == 1, "Cache should only hold one item"

0 commit comments

Comments
 (0)